summaryrefslogtreecommitdiff
path: root/indra/newview/lllogchat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/newview/lllogchat.cpp')
-rw-r--r--indra/newview/lllogchat.cpp497
1 files changed, 415 insertions, 82 deletions
diff --git a/indra/newview/lllogchat.cpp b/indra/newview/lllogchat.cpp
index 6aa209b787..c8fd1e1d9a 100644
--- a/indra/newview/lllogchat.cpp
+++ b/indra/newview/lllogchat.cpp
@@ -2,141 +2,474 @@
* @file lllogchat.cpp
* @brief LLLogChat class implementation
*
- * $LicenseInfo:firstyear=2002&license=viewergpl$
- *
- * Copyright (c) 2002-2007, Linden Research, Inc.
- *
+ * $LicenseInfo:firstyear=2002&license=viewerlgpl$
* Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlife.com/developers/opensource/gplv2
+ * Copyright (C) 2010, Linden Research, Inc.
*
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at http://secondlife.com/developers/opensource/flossexception
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License only.
*
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "llviewerprecompiledheaders.h"
#include "lllogchat.h"
-#include "llappviewer.h"
-#include "llfloaterchat.h"
+
+// viewer includes
+#include "llagent.h"
+#include "llagentui.h"
+#include "lltrans.h"
+#include "llviewercontrol.h"
+
+// library includes
+#include "llchat.h"
+#include "llinstantmessage.h"
+#include "llsdserialize.h"
+#include "llsingleton.h" // for LLSingleton
+
+#include <boost/algorithm/string/trim.hpp>
+#include <boost/algorithm/string/replace.hpp>
+#include <boost/regex.hpp>
+#include <boost/regex/v4/match_results.hpp>
+
+#if LL_MSVC
+// disable warning about boost::lexical_cast unreachable code
+// when it fails to parse the string
+#pragma warning (disable:4702)
+#endif
+
+#include <boost/date_time/gregorian/gregorian.hpp>
+#if LL_MSVC
+#pragma warning(pop) // Restore all warnings to the previous state
+#endif
+
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/date_time/local_time_adjustor.hpp>
const S32 LOG_RECALL_SIZE = 2048;
+const std::string IM_TIME("time");
+const std::string IM_TEXT("message");
+const std::string IM_FROM("from");
+const std::string IM_FROM_ID("from_id");
+const std::string IM_SOURCE_TYPE("source_type");
+
+const static std::string IM_SEPARATOR(": ");
+const static std::string NEW_LINE("\n");
+const static std::string NEW_LINE_SPACE_PREFIX("\n ");
+const static std::string TWO_SPACES(" ");
+const static std::string MULTI_LINE_PREFIX(" ");
+
+/**
+ * Chat log lines - timestamp and name are optional but message text is mandatory.
+ *
+ * Typical plain text chat log lines:
+ *
+ * SuperCar: You aren't the owner
+ * [2:59] SuperCar: You aren't the owner
+ * [2009/11/20 3:00] SuperCar: You aren't the owner
+ * Katar Ivercourt is Offline
+ * [3:00] Katar Ivercourt is Offline
+ * [2009/11/20 3:01] Corba ProductEngine is Offline
+ *
+ * Note: "You" was used as an avatar names in viewers of previous versions
+ */
+const static boost::regex TIMESTAMP_AND_STUFF("^(\\[\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{2}\\]\\s+|\\[\\d{1,2}:\\d{2}\\]\\s+)?(.*)$");
+
+/**
+ * Regular expression suitable to match names like
+ * "You", "Second Life", "Igor ProductEngine", "Object", "Mega House"
+ */
+const static boost::regex NAME_AND_TEXT("(You:|Second Life:|[^\\s:]+\\s*[:]{1}|\\S+\\s+[^\\s:]+[:]{1})?(\\s*)(.*)");
+
+//is used to parse complex object names like "Xstreet SL Terminal v2.2.5 st"
+const static std::string NAME_TEXT_DIVIDER(": ");
+
+// is used for timestamps adjusting
+const static char* DATE_FORMAT("%Y/%m/%d %H:%M");
+const static char* TIME_FORMAT("%H:%M");
+
+const static int IDX_TIMESTAMP = 1;
+const static int IDX_STUFF = 2;
+const static int IDX_NAME = 1;
+const static int IDX_TEXT = 3;
+
+using namespace boost::posix_time;
+using namespace boost::gregorian;
+
+class LLLogChatTimeScanner: public LLSingleton<LLLogChatTimeScanner>
+{
+public:
+ LLLogChatTimeScanner()
+ {
+ // Note, date/time facets will be destroyed by string streams
+ mDateStream.imbue(std::locale(mDateStream.getloc(), new date_input_facet(DATE_FORMAT)));
+ mTimeStream.imbue(std::locale(mTimeStream.getloc(), new time_facet(TIME_FORMAT)));
+ mTimeStream.imbue(std::locale(mTimeStream.getloc(), new time_input_facet(DATE_FORMAT)));
+ }
+
+ date getTodayPacificDate()
+ {
+ typedef boost::date_time::local_adjustor<ptime, -8, no_dst> pst;
+ typedef boost::date_time::local_adjustor<ptime, -7, no_dst> pdt;
+ time_t t_time = time(NULL);
+ ptime p_time = LLStringOps::getPacificDaylightTime()
+ ? pdt::utc_to_local(from_time_t(t_time))
+ : pst::utc_to_local(from_time_t(t_time));
+ struct tm s_tm = to_tm(p_time);
+ return date_from_tm(s_tm);
+ }
+
+ void checkAndCutOffDate(std::string& time_str)
+ {
+ // Cuts off the "%Y/%m/%d" from string for todays timestamps.
+ // Assume that passed string has at least "%H:%M" time format.
+ date log_date(not_a_date_time);
+ date today(getTodayPacificDate());
+
+ // Parse the passed date
+ mDateStream.str(LLStringUtil::null);
+ mDateStream << time_str;
+ mDateStream >> log_date;
+ mDateStream.clear();
+
+ days zero_days(0);
+ days days_alive = today - log_date;
+
+ if ( days_alive == zero_days )
+ {
+ // Yep, today's so strip "%Y/%m/%d" info
+ ptime stripped_time(not_a_date_time);
+
+ mTimeStream.str(LLStringUtil::null);
+ mTimeStream << time_str;
+ mTimeStream >> stripped_time;
+ mTimeStream.clear();
+
+ time_str.clear();
+
+ mTimeStream.str(LLStringUtil::null);
+ mTimeStream << stripped_time;
+ mTimeStream >> time_str;
+ mTimeStream.clear();
+ }
+
+ LL_DEBUGS("LLChatLogParser")
+ << " log_date: "
+ << log_date
+ << " today: "
+ << today
+ << " days alive: "
+ << days_alive
+ << " new time: "
+ << time_str
+ << LL_ENDL;
+ }
+
+
+private:
+ std::stringstream mDateStream;
+ std::stringstream mTimeStream;
+};
+
//static
-LLString LLLogChat::makeLogFileName(LLString filename)
+std::string LLLogChat::makeLogFileName(std::string filename)
{
- filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_ACCOUNT_CHAT_LOGS,filename.c_str());
- filename += ".txt";
+ filename = cleanFileName(filename);
+ filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_ACCOUNT_CHAT_LOGS,filename);
+ // new files are llsd notation format
+ filename += ".llsd";
return filename;
}
-LLString LLLogChat::timestamp(bool withdate)
+std::string LLLogChat::cleanFileName(std::string filename)
{
- U32 utc_time;
- utc_time = time_corrected();
+ std::string invalidChars = "\"\'\\/?*:<>|";
+ std::string::size_type position = filename.find_first_of(invalidChars);
+ while (position != filename.npos)
+ {
+ filename[position] = '_';
+ position = filename.find_first_of(invalidChars, position);
+ }
+ return filename;
+}
- // There's only one internal tm buffer.
- struct tm* timep;
+std::string LLLogChat::timestamp(bool withdate)
+{
+ time_t utc_time;
+ utc_time = time_corrected();
- // Convert to Pacific, based on server's opinion of whether
- // it's daylight savings time there.
- timep = utc_to_pacific_time(utc_time, gPacificDaylightTime);
+ std::string timeStr;
+ LLSD substitution;
+ substitution["datetime"] = (S32) utc_time;
- LLString text;
if (withdate)
- text = llformat("[%d/%02d/%02d %d:%02d] ", (timep->tm_year-100)+2000, timep->tm_mon+1, timep->tm_mday, timep->tm_hour, timep->tm_min);
+ {
+ timeStr = "["+LLTrans::getString ("TimeYear")+"]/["
+ +LLTrans::getString ("TimeMonth")+"]/["
+ +LLTrans::getString ("TimeDay")+"] ["
+ +LLTrans::getString ("TimeHour")+"]:["
+ +LLTrans::getString ("TimeMin")+"]";
+ }
else
- text = llformat("[%d:%02d] ", timep->tm_hour, timep->tm_min);
+ {
+ timeStr = "[" + LLTrans::getString("TimeHour") + "]:["
+ + LLTrans::getString ("TimeMin")+"]";
+ }
- return text;
+ LLStringUtil::format (timeStr, substitution);
+ return timeStr;
}
//static
-void LLLogChat::saveHistory(LLString filename, LLString line)
+void LLLogChat::saveHistory(const std::string& filename,
+ const std::string& from,
+ const LLUUID& from_id,
+ const std::string& line)
+{
+ LLChat chat;
+ chat.mText = line;
+ chat.mFromName = from;
+ chat.mFromID = from_id;
+ // default to being from an agent
+ chat.mSourceType = CHAT_SOURCE_AGENT;
+ saveHistory(filename, chat);
+}
+
+//static
+void LLLogChat::saveHistory(const std::string& filename, const LLChat& chat)
{
- if(!filename.size())
+ std::string tmp_filename = filename;
+ LLStringUtil::trim(tmp_filename);
+ if (tmp_filename.empty())
+ {
+ std::string warn = "Chat history filename [" + filename + "] is empty!";
+ llwarning(warn, 666);
+ llassert(tmp_filename.size());
+ return;
+ }
+
+ llofstream file (LLLogChat::makeLogFileName(filename), std::ios_base::app);
+ if (!file.is_open())
{
- llinfos << "Filename is Empty!" << llendl;
+ llwarns << "Couldn't open chat history log! - " + filename << llendl;
return;
}
- FILE* fp = LLFile::fopen(LLLogChat::makeLogFileName(filename).c_str(), "a"); /*Flawfinder: ignore*/
- if (!fp)
+ LLSD item;
+
+ if (gSavedPerAccountSettings.getBOOL("LogTimestamp"))
+ item[IM_TIME] = LLLogChat::timestamp(gSavedPerAccountSettings.getBOOL("LogTimestampDate"));
+
+ item[IM_FROM_ID] = chat.mFromID;
+ item[IM_TEXT] = chat.mText;
+ item[IM_SOURCE_TYPE] = chat.mSourceType;
+
+ //adding "Second Life:" for all system messages to make chat log history parsing more reliable
+ if (chat.mFromName.empty() && chat.mFromID.isNull())
{
- llinfos << "Couldn't open chat history log!" << llendl;
+ item[IM_FROM] = SYSTEM_FROM;
}
else
{
- fprintf(fp, "%s\n", line.c_str());
-
- fclose (fp);
+ item[IM_FROM] = chat.mFromName;
}
+
+ file << LLSDOStreamer<LLSDNotationFormatter>(item) << std::endl;
+
+ file.close();
+}
+
+void append_to_last_message(std::list<LLSD>& messages, const std::string& line)
+{
+ if (!messages.size()) return;
+
+ std::string im_text = messages.back()[IM_TEXT].asString();
+ im_text.append(line);
+ messages.back()[IM_TEXT] = im_text;
}
-void LLLogChat::loadHistory(LLString filename , void (*callback)(ELogLineType,LLString,void*), void* userdata)
+// static
+void LLLogChat::loadAllHistory(const std::string& file_name, std::list<LLSD>& messages)
{
- if(!filename.size())
+ if (file_name.empty())
{
- llerrs << "Filename is Empty!" << llendl;
+ llwarns << "Session name is Empty!" << llendl;
+ return ;
}
- FILE* fptr = LLFile::fopen(makeLogFileName(filename).c_str(), "r"); /*Flawfinder: ignore*/
- if (!fptr)
- {
- //LLUIString message = LLFloaterChat::getInstance()->getUIString("IM_logging_string");
- //callback(LOG_EMPTY,"IM_logging_string",userdata);
- callback(LOG_EMPTY,"",userdata);
- return; //No previous conversation with this name.
+ LLFILE* fptr = LLFile::fopen(makeLogFileName(file_name), "r"); /*Flawfinder: ignore*/
+ if (!fptr) return; //No previous conversation with this name.
+
+ char buffer[LOG_RECALL_SIZE]; /*Flawfinder: ignore*/
+ char *bptr;
+ S32 len;
+ bool firstline = TRUE;
+
+ if (fseek(fptr, (LOG_RECALL_SIZE - 1) * -1 , SEEK_END))
+ { //File is smaller than recall size. Get it all.
+ firstline = FALSE;
+ if (fseek(fptr, 0, SEEK_SET))
+ {
+ fclose(fptr);
+ return;
+ }
}
- else
- {
- char buffer[LOG_RECALL_SIZE]; /*Flawfinder: ignore*/
- char *bptr;
- S32 len;
- bool firstline=TRUE;
- if ( fseek(fptr, (LOG_RECALL_SIZE - 1) * -1 , SEEK_END) )
- { //File is smaller than recall size. Get it all.
+ while (fgets(buffer, LOG_RECALL_SIZE, fptr) && !feof(fptr))
+ {
+ len = strlen(buffer) - 1; /*Flawfinder: ignore*/
+ for (bptr = (buffer + len); (*bptr == '\n' || *bptr == '\r') && bptr>buffer; bptr--) *bptr='\0';
+
+ if (firstline)
+ {
firstline = FALSE;
- if ( fseek(fptr, 0, SEEK_SET) )
- {
- fclose(fptr);
- return;
- }
+ continue;
}
- while ( fgets(buffer, LOG_RECALL_SIZE, fptr) && !feof(fptr) )
+ std::string line(buffer);
+
+ //updated 1.23 plaint text log format requires a space added before subsequent lines in a multilined message
+ if (' ' == line[0])
{
- len = strlen(buffer) - 1; /*Flawfinder: ignore*/
- for ( bptr = (buffer + len); (*bptr == '\n' || *bptr == '\r') && bptr>buffer; bptr--) *bptr='\0';
-
- if (!firstline)
- {
- callback(LOG_LINE,buffer,userdata);
- }
- else
+ line.erase(0, MULTI_LINE_PREFIX.length());
+ append_to_last_message(messages, '\n' + line);
+ }
+ else if (0 == len && ('\n' == line[0] || '\r' == line[0]))
+ {
+ //to support old format's multilined messages with new lines used to divide paragraphs
+ append_to_last_message(messages, line);
+ }
+ else
+ {
+ LLSD item;
+ if (!LLChatLogParser::parse(line, item))
{
- firstline = FALSE;
+ item[IM_TEXT] = line;
}
+ messages.push_back(item);
}
- callback(LOG_END,"",userdata);
-
- fclose(fptr);
}
+ fclose(fptr);
+}
+
+// static
+bool LLChatLogParser::parse(const std::string& raw, LLSD& im)
+{
+ if (!raw.length()) return false;
+
+ im = LLSD::emptyMap();
+
+ // In Viewer 2.1 we added UUID to chat/IM logging so we can look up
+ // display names
+ if (raw[0] == '{')
+ {
+ // ...this is a viewer 2.1, new-style LLSD notation format log
+ std::istringstream raw_stream(raw);
+ LLPointer<LLSDParser> parser = new LLSDNotationParser();
+ S32 count = parser->parse(raw_stream, im, raw.length());
+ // expect several map items per parsed line
+ return (count != LLSDParser::PARSE_FAILURE);
+ }
+
+ //matching a timestamp
+ boost::match_results<std::string::const_iterator> matches;
+ if (!boost::regex_match(raw, matches, TIMESTAMP_AND_STUFF)) return false;
+
+ bool has_timestamp = matches[IDX_TIMESTAMP].matched;
+ if (has_timestamp)
+ {
+ //timestamp was successfully parsed
+ std::string timestamp = matches[IDX_TIMESTAMP];
+ boost::trim(timestamp);
+ timestamp.erase(0, 1);
+ timestamp.erase(timestamp.length()-1, 1);
+ LLLogChatTimeScanner::instance().checkAndCutOffDate(timestamp);
+ im[IM_TIME] = timestamp;
+ }
+ else
+ {
+ //timestamp is optional
+ im[IM_TIME] = "";
+ }
+
+ bool has_stuff = matches[IDX_STUFF].matched;
+ if (!has_stuff)
+ {
+ return false; //*TODO should return false or not?
+ }
+
+ //matching a name and a text
+ std::string stuff = matches[IDX_STUFF];
+ boost::match_results<std::string::const_iterator> name_and_text;
+ if (!boost::regex_match(stuff, name_and_text, NAME_AND_TEXT)) return false;
+
+ bool has_name = name_and_text[IDX_NAME].matched;
+ std::string name = name_and_text[IDX_NAME];
+
+ //we don't need a name/text separator
+ if (has_name && name.length() && name[name.length()-1] == ':')
+ {
+ name.erase(name.length()-1, 1);
+ }
+
+ if (!has_name || name == SYSTEM_FROM)
+ {
+ //name is optional too
+ im[IM_FROM] = SYSTEM_FROM;
+ im[IM_FROM_ID] = LLUUID::null;
+ }
+
+ //possibly a case of complex object names consisting of 3+ words
+ if (!has_name)
+ {
+ U32 divider_pos = stuff.find(NAME_TEXT_DIVIDER);
+ if (divider_pos != std::string::npos && divider_pos < (stuff.length() - NAME_TEXT_DIVIDER.length()))
+ {
+ im[IM_FROM] = stuff.substr(0, divider_pos);
+ im[IM_TEXT] = stuff.substr(divider_pos + NAME_TEXT_DIVIDER.length());
+ return true;
+ }
+ }
+
+ if (!has_name)
+ {
+ //text is mandatory
+ im[IM_TEXT] = stuff;
+ return true; //parse as a message from Second Life
+ }
+
+ bool has_text = name_and_text[IDX_TEXT].matched;
+ if (!has_text) return false;
+
+ //for parsing logs created in very old versions of a viewer
+ if (name == "You")
+ {
+ std::string agent_name;
+ LLAgentUI::buildFullname(agent_name);
+ im[IM_FROM] = agent_name;
+ im[IM_FROM_ID] = gAgentID;
+ }
+ else
+ {
+ im[IM_FROM] = name;
+ }
+
+
+ im[IM_TEXT] = name_and_text[IDX_TEXT];
+ return true; //parsed name and message text, maybe have a timestamp too
}