PageRenderTime 624ms CodeModel.GetById 144ms app.highlight 243ms RepoModel.GetById 116ms app.codeStats 0ms

/indra/newview/lllogchat.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 625 lines | 448 code | 82 blank | 95 comment | 83 complexity | 10c508361cba4488b659e9a2df7683a9 MD5 | raw file
  1/** 
  2 * @file lllogchat.cpp
  3 * @brief LLLogChat class implementation
  4 *
  5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llagent.h"
 30#include "llagentui.h"
 31#include "lllogchat.h"
 32#include "lltrans.h"
 33#include "llviewercontrol.h"
 34
 35#include "lldiriterator.h"
 36#include "llinstantmessage.h"
 37#include "llsingleton.h" // for LLSingleton
 38
 39#include <boost/algorithm/string/trim.hpp>
 40#include <boost/algorithm/string/replace.hpp>
 41#include <boost/regex.hpp>
 42#include <boost/regex/v4/match_results.hpp>
 43
 44#if LL_MSVC
 45#pragma warning(push)  
 46// disable warning about boost::lexical_cast unreachable code
 47// when it fails to parse the string
 48#pragma warning (disable:4702)
 49#endif
 50
 51#include <boost/date_time/gregorian/gregorian.hpp>
 52#if LL_MSVC
 53#pragma warning(pop)   // Restore all warnings to the previous state
 54#endif
 55
 56#include <boost/date_time/posix_time/posix_time.hpp>
 57#include <boost/date_time/local_time_adjustor.hpp>
 58
 59const S32 LOG_RECALL_SIZE = 2048;
 60
 61const std::string IM_TIME("time");
 62const std::string IM_TEXT("message");
 63const std::string IM_FROM("from");
 64const std::string IM_FROM_ID("from_id");
 65
 66const static std::string IM_SEPARATOR(": ");
 67const static std::string NEW_LINE("\n");
 68const static std::string NEW_LINE_SPACE_PREFIX("\n ");
 69const static std::string TWO_SPACES("  ");
 70const static std::string MULTI_LINE_PREFIX(" ");
 71
 72/**
 73 *  Chat log lines - timestamp and name are optional but message text is mandatory.
 74 *
 75 *  Typical plain text chat log lines:
 76 *
 77 *  SuperCar: You aren't the owner
 78 *  [2:59]  SuperCar: You aren't the owner
 79 *  [2009/11/20 3:00]  SuperCar: You aren't the owner
 80 *  Katar Ivercourt is Offline
 81 *  [3:00]  Katar Ivercourt is Offline
 82 *  [2009/11/20 3:01]  Corba ProductEngine is Offline
 83 *
 84 * Note: "You" was used as an avatar names in viewers of previous versions
 85 */
 86const 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+)?(.*)$");
 87
 88/**
 89 *  Regular expression suitable to match names like
 90 *  "You", "Second Life", "Igor ProductEngine", "Object", "Mega House"
 91 */
 92const static boost::regex NAME_AND_TEXT("([^:]+[:]{1})?(\\s*)(.*)");
 93
 94/**
 95 * These are recognizers for matching the names of ad-hoc conferences when generating the log file name
 96 * On invited side, an ad-hoc is named like "<first name> <last name> Conference 2010/11/19 03:43 f0f4"
 97 * On initiating side, an ad-hoc is named like Ad-hoc Conference hash<hash>"
 98 * If the naming system for ad-hoc conferences are change in LLIMModel::LLIMSession::buildHistoryFileName()
 99 * then these definition need to be adjusted as well.
100 */
101const static boost::regex INBOUND_CONFERENCE("^[a-zA-Z]{1,31} [a-zA-Z]{1,31} Conference [0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2} [0-9a-f]{4}");
102const static boost::regex OUTBOUND_CONFERENCE("^Ad-hoc Conference hash[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}");
103
104//is used to parse complex object names like "Xstreet SL Terminal v2.2.5 st"
105const static std::string NAME_TEXT_DIVIDER(": ");
106
107// is used for timestamps adjusting
108const static char* DATE_FORMAT("%Y/%m/%d %H:%M");
109const static char* TIME_FORMAT("%H:%M");
110
111const static int IDX_TIMESTAMP = 1;
112const static int IDX_STUFF = 2;
113const static int IDX_NAME = 1;
114const static int IDX_TEXT = 3;
115
116using namespace boost::posix_time;
117using namespace boost::gregorian;
118
119class LLLogChatTimeScanner: public LLSingleton<LLLogChatTimeScanner>
120{
121public:
122	LLLogChatTimeScanner()
123	{
124		// Note, date/time facets will be destroyed by string streams
125		mDateStream.imbue(std::locale(mDateStream.getloc(), new date_input_facet(DATE_FORMAT)));
126		mTimeStream.imbue(std::locale(mTimeStream.getloc(), new time_facet(TIME_FORMAT)));
127		mTimeStream.imbue(std::locale(mTimeStream.getloc(), new time_input_facet(DATE_FORMAT)));
128	}
129
130	date getTodayPacificDate()
131	{
132		typedef	boost::date_time::local_adjustor<ptime, -8, no_dst> pst;
133		typedef boost::date_time::local_adjustor<ptime, -7, no_dst> pdt;
134		time_t t_time = time(NULL);
135		ptime p_time = LLStringOps::getPacificDaylightTime()
136			? pdt::utc_to_local(from_time_t(t_time))
137			: pst::utc_to_local(from_time_t(t_time));
138		struct tm s_tm = to_tm(p_time);
139		return date_from_tm(s_tm);
140	}
141
142	void checkAndCutOffDate(std::string& time_str)
143	{
144		// Cuts off the "%Y/%m/%d" from string for todays timestamps.
145		// Assume that passed string has at least "%H:%M" time format.
146		date log_date(not_a_date_time);
147		date today(getTodayPacificDate());
148
149		// Parse the passed date
150		mDateStream.str(LLStringUtil::null);
151		mDateStream << time_str;
152		mDateStream >> log_date;
153		mDateStream.clear();
154
155		days zero_days(0);
156		days days_alive = today - log_date;
157
158		if ( days_alive == zero_days )
159		{
160			// Yep, today's so strip "%Y/%m/%d" info
161			ptime stripped_time(not_a_date_time);
162
163			mTimeStream.str(LLStringUtil::null);
164			mTimeStream << time_str;
165			mTimeStream >> stripped_time;
166			mTimeStream.clear();
167
168			time_str.clear();
169
170			mTimeStream.str(LLStringUtil::null);
171			mTimeStream << stripped_time;
172			mTimeStream >> time_str;
173			mTimeStream.clear();
174		}
175
176		LL_DEBUGS("LLChatLogParser")
177			<< " log_date: "
178			<< log_date
179			<< " today: "
180			<< today
181			<< " days alive: "
182			<< days_alive
183			<< " new time: "
184			<< time_str
185			<< LL_ENDL;
186	}
187
188
189private:
190	std::stringstream mDateStream;
191	std::stringstream mTimeStream;
192};
193
194//static
195std::string LLLogChat::makeLogFileName(std::string filename)
196{
197	/**
198	* Testing for in bound and out bound ad-hoc file names
199	* if it is then skip date stamping.
200	**/
201	//LL_INFOS("") << "Befor:" << filename << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
202    boost::match_results<std::string::const_iterator> matches;
203	bool inboundConf = boost::regex_match(filename, matches, INBOUND_CONFERENCE);
204	bool outboundConf = boost::regex_match(filename, matches, OUTBOUND_CONFERENCE);
205	if (!(inboundConf || outboundConf))
206	{
207		if( gSavedPerAccountSettings.getBOOL("LogFileNamewithDate") )
208		{
209			time_t now;
210			time(&now);
211			char dbuffer[20];		/* Flawfinder: ignore */
212			if (filename == "chat")
213			{
214				strftime(dbuffer, 20, "-%Y-%m-%d", localtime(&now));
215			}
216			else
217			{
218				strftime(dbuffer, 20, "-%Y-%m", localtime(&now));
219			}
220			filename += dbuffer;
221		}
222	}
223	//LL_INFOS("") << "After:" << filename << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
224	filename = cleanFileName(filename);
225	filename = gDirUtilp->getExpandedFilename(LL_PATH_PER_ACCOUNT_CHAT_LOGS,filename);
226	filename += ".txt";
227	//LL_INFOS("") << "Full:" << filename << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
228	return filename;
229}
230
231std::string LLLogChat::cleanFileName(std::string filename)
232{
233    std::string invalidChars = "\"\'\\/?*:.<>|[]{}~"; // Cannot match glob or illegal filename chars
234	std::string::size_type position = filename.find_first_of(invalidChars);
235	while (position != filename.npos)
236	{
237		filename[position] = '_';
238		position = filename.find_first_of(invalidChars, position);
239	}
240	return filename;
241}
242
243std::string LLLogChat::timestamp(bool withdate)
244{
245	time_t utc_time;
246	utc_time = time_corrected();
247
248	std::string timeStr;
249	LLSD substitution;
250	substitution["datetime"] = (S32) utc_time;
251
252	if (withdate)
253	{
254		timeStr = "["+LLTrans::getString ("TimeYear")+"]/["
255		          +LLTrans::getString ("TimeMonth")+"]/["
256				  +LLTrans::getString ("TimeDay")+"] ["
257				  +LLTrans::getString ("TimeHour")+"]:["
258				  +LLTrans::getString ("TimeMin")+"]";
259	}
260	else
261	{
262		timeStr = "[" + LLTrans::getString("TimeHour") + "]:["
263			      + LLTrans::getString ("TimeMin")+"]";
264	}
265
266	LLStringUtil::format (timeStr, substitution);
267	return timeStr;
268}
269
270
271//static
272void LLLogChat::saveHistory(const std::string& filename,
273			    const std::string& from,
274			    const LLUUID& from_id,
275			    const std::string& line)
276{
277	std::string tmp_filename = filename;
278	LLStringUtil::trim(tmp_filename);
279	if (tmp_filename.empty())
280	{
281		std::string warn = "Chat history filename [" + filename + "] is empty!";
282		llwarning(warn, 666);
283		llassert(tmp_filename.size());
284		return;
285	}
286	
287	llofstream file (LLLogChat::makeLogFileName(filename), std::ios_base::app);
288	if (!file.is_open())
289	{
290		llwarns << "Couldn't open chat history log! - " + filename << llendl;
291		return;
292	}
293
294	LLSD item;
295
296	if (gSavedPerAccountSettings.getBOOL("LogTimestamp"))
297		 item["time"] = LLLogChat::timestamp(gSavedPerAccountSettings.getBOOL("LogTimestampDate"));
298
299	item["from_id"]	= from_id;
300	item["message"]	= line;
301
302	//adding "Second Life:" for all system messages to make chat log history parsing more reliable
303	if (from.empty() && from_id.isNull())
304	{
305		item["from"] = SYSTEM_FROM; 
306	}
307	else
308	{
309		item["from"] = from;
310	}
311
312	file << LLChatLogFormatter(item) << std::endl;
313
314	file.close();
315}
316
317void LLLogChat::loadHistory(const std::string& filename, void (*callback)(ELogLineType, const LLSD&, void*), void* userdata)
318{
319	if(!filename.size())
320	{
321		llwarns << "Filename is Empty!" << llendl;
322		return ;
323	}
324        
325	LLFILE* fptr = LLFile::fopen(makeLogFileName(filename), "r");		/*Flawfinder: ignore*/
326	if (!fptr)
327	{
328		callback(LOG_EMPTY, LLSD(), userdata);
329		return;			//No previous conversation with this name.
330	}
331	else
332	{
333		char buffer[LOG_RECALL_SIZE];		/*Flawfinder: ignore*/
334		char *bptr;
335		S32 len;
336		bool firstline=TRUE;
337
338		if ( fseek(fptr, (LOG_RECALL_SIZE - 1) * -1  , SEEK_END) )		
339		{	//File is smaller than recall size.  Get it all.
340			firstline = FALSE;
341			if ( fseek(fptr, 0, SEEK_SET) )
342			{
343				fclose(fptr);
344				return;
345			}
346		}
347
348		while ( fgets(buffer, LOG_RECALL_SIZE, fptr)  && !feof(fptr) ) 
349		{
350			len = strlen(buffer) - 1;		/*Flawfinder: ignore*/
351			for ( bptr = (buffer + len); (*bptr == '\n' || *bptr == '\r') && bptr>buffer; bptr--)	*bptr='\0';
352			
353			if (!firstline)
354			{
355				LLSD item;
356				std::string line(buffer);
357				std::istringstream iss(line);
358				
359				if (!LLChatLogParser::parse(line, item))
360				{
361					item["message"]	= line;
362					callback(LOG_LINE, item, userdata);
363				}
364				else
365				{
366					callback(LOG_LLSD, item, userdata);
367				}
368			}
369			else
370			{
371				firstline = FALSE;
372			}
373		}
374		callback(LOG_END, LLSD(), userdata);
375		
376		fclose(fptr);
377	}
378}
379
380void append_to_last_message(std::list<LLSD>& messages, const std::string& line)
381{
382	if (!messages.size()) return;
383
384	std::string im_text = messages.back()[IM_TEXT].asString();
385	im_text.append(line);
386	messages.back()[IM_TEXT] = im_text;
387}
388
389// static
390void LLLogChat::loadAllHistory(const std::string& file_name, std::list<LLSD>& messages)
391{
392	if (file_name.empty())
393	{
394		llwarns << "Session name is Empty!" << llendl;
395		return ;
396	}
397	//LL_INFOS("") << "Loading:" << file_name << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
398	//LL_INFOS("") << "Current:" << makeLogFileName(file_name) << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
399	LLFILE* fptr = LLFile::fopen(makeLogFileName(file_name), "r");/*Flawfinder: ignore*/
400	if (!fptr)
401    {
402		fptr = LLFile::fopen(oldLogFileName(file_name), "r");/*Flawfinder: ignore*/
403        if (!fptr)
404        {
405			if (!fptr) return;      //No previous conversation with this name.
406        }
407	}
408 
409    //LL_INFOS("") << "Reading:" << file_name << LL_ENDL;
410	char buffer[LOG_RECALL_SIZE];		/*Flawfinder: ignore*/
411	char *bptr;
412	S32 len;
413	bool firstline = TRUE;
414
415	if (fseek(fptr, (LOG_RECALL_SIZE - 1) * -1  , SEEK_END))
416	{	//File is smaller than recall size.  Get it all.
417		firstline = FALSE;
418		if (fseek(fptr, 0, SEEK_SET))
419		{
420			fclose(fptr);
421			return;
422		}
423	}
424
425	while (fgets(buffer, LOG_RECALL_SIZE, fptr)  && !feof(fptr)) 
426	{
427		len = strlen(buffer) - 1;		/*Flawfinder: ignore*/
428		for (bptr = (buffer + len); (*bptr == '\n' || *bptr == '\r') && bptr>buffer; bptr--)	*bptr='\0';
429		
430		if (firstline)
431		{
432			firstline = FALSE;
433			continue;
434		}
435
436		std::string line(buffer);
437
438		//updated 1.23 plaint text log format requires a space added before subsequent lines in a multilined message
439		if (' ' == line[0])
440		{
441			line.erase(0, MULTI_LINE_PREFIX.length());
442			append_to_last_message(messages, '\n' + line);
443		}
444		else if (0 == len && ('\n' == line[0] || '\r' == line[0]))
445		{
446			//to support old format's multilined messages with new lines used to divide paragraphs
447			append_to_last_message(messages, line);
448		}
449		else
450		{
451			LLSD item;
452			if (!LLChatLogParser::parse(line, item))
453			{
454				item[IM_TEXT] = line;
455			}
456			messages.push_back(item);
457		}
458	}
459	fclose(fptr);
460}
461
462//*TODO mark object's names in a special way so that they will be distinguishable form avatar name 
463//which are more strict by its nature (only firstname and secondname)
464//Example, an object's name can be writen like "Object <actual_object's_name>"
465void LLChatLogFormatter::format(const LLSD& im, std::ostream& ostr) const
466{
467	if (!im.isMap())
468	{
469		llwarning("invalid LLSD type of an instant message", 0);
470		return;
471	}
472
473	if (im[IM_TIME].isDefined())
474{
475		std::string timestamp = im[IM_TIME].asString();
476		boost::trim(timestamp);
477		ostr << '[' << timestamp << ']' << TWO_SPACES;
478	}
479	
480	//*TODO mark object's names in a special way so that they will be distinguishable form avatar name 
481	//which are more strict by its nature (only firstname and secondname)
482	//Example, an object's name can be writen like "Object <actual_object's_name>"
483	if (im[IM_FROM].isDefined())
484	{
485		std::string from = im[IM_FROM].asString();
486		boost::trim(from);
487		if (from.size())
488		{
489			ostr << from << IM_SEPARATOR;
490		}
491	}
492
493	if (im[IM_TEXT].isDefined())
494	{
495		std::string im_text = im[IM_TEXT].asString();
496
497		//multilined text will be saved with prepended spaces
498		boost::replace_all(im_text, NEW_LINE, NEW_LINE_SPACE_PREFIX);
499		ostr << im_text;
500	}
501	}
502
503bool LLChatLogParser::parse(std::string& raw, LLSD& im)
504{
505	if (!raw.length()) return false;
506	
507	im = LLSD::emptyMap();
508
509	//matching a timestamp
510	boost::match_results<std::string::const_iterator> matches;
511	if (!boost::regex_match(raw, matches, TIMESTAMP_AND_STUFF)) return false;
512	
513	bool has_timestamp = matches[IDX_TIMESTAMP].matched;
514	if (has_timestamp)
515	{
516		//timestamp was successfully parsed
517		std::string timestamp = matches[IDX_TIMESTAMP];
518		boost::trim(timestamp);
519		timestamp.erase(0, 1);
520		timestamp.erase(timestamp.length()-1, 1);
521		LLLogChatTimeScanner::instance().checkAndCutOffDate(timestamp);
522		im[IM_TIME] = timestamp;
523	}
524	else
525	{
526		//timestamp is optional
527		im[IM_TIME] = "";
528	}
529
530	bool has_stuff = matches[IDX_STUFF].matched;
531	if (!has_stuff)
532	{
533		return false;  //*TODO should return false or not?
534	}
535
536	//matching a name and a text
537	std::string stuff = matches[IDX_STUFF];
538	boost::match_results<std::string::const_iterator> name_and_text;
539	if (!boost::regex_match(stuff, name_and_text, NAME_AND_TEXT)) return false;
540	
541	bool has_name = name_and_text[IDX_NAME].matched;
542	std::string name = name_and_text[IDX_NAME];
543
544	//we don't need a name/text separator
545	if (has_name && name.length() && name[name.length()-1] == ':')
546	{
547		name.erase(name.length()-1, 1);
548	}
549
550	if (!has_name || name == SYSTEM_FROM)
551	{
552		//name is optional too
553		im[IM_FROM] = SYSTEM_FROM;
554		im[IM_FROM_ID] = LLUUID::null;
555	}
556
557	//possibly a case of complex object names consisting of 3+ words
558	if (!has_name)
559	{
560		U32 divider_pos = stuff.find(NAME_TEXT_DIVIDER);
561		if (divider_pos != std::string::npos && divider_pos < (stuff.length() - NAME_TEXT_DIVIDER.length()))
562		{
563			im[IM_FROM] = stuff.substr(0, divider_pos);
564			im[IM_TEXT] = stuff.substr(divider_pos + NAME_TEXT_DIVIDER.length());
565			return true;
566		}
567	}
568
569	if (!has_name)
570	{
571		//text is mandatory
572		im[IM_TEXT] = stuff;
573		return true; //parse as a message from Second Life
574	}
575	
576	bool has_text = name_and_text[IDX_TEXT].matched;
577	if (!has_text) return false;
578
579	//for parsing logs created in very old versions of a viewer
580	if (name == "You")
581	{
582		std::string agent_name;
583		LLAgentUI::buildFullname(agent_name);
584		im[IM_FROM] = agent_name;
585		im[IM_FROM_ID] = gAgentID;
586	}
587	else
588	{
589		im[IM_FROM] = name;
590	}
591	
592
593	im[IM_TEXT] = name_and_text[IDX_TEXT];
594	return true;  //parsed name and message text, maybe have a timestamp too
595}
596std::string LLLogChat::oldLogFileName(std::string filename)
597{
598    std::string scanResult;
599	std::string directory = gDirUtilp->getPerAccountChatLogsDir();/* get Users log directory */
600	directory += gDirUtilp->getDirDelimiter();/* add final OS dependent delimiter */
601	filename=cleanFileName(filename);/* lest make shure the file name has no invalad charecters befor making the pattern */
602	std::string pattern = (filename+(( filename == "chat" ) ? "-???\?-?\?-??.txt" : "-???\?-??.txt"));/* create search pattern*/
603	//LL_INFOS("") << "Checking:" << directory << " for " << pattern << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
604	std::vector<std::string> allfiles;
605
606	LLDirIterator iter(directory, pattern);
607	while (iter.next(scanResult))
608    {
609		//LL_INFOS("") << "Found   :" << scanResult << LL_ENDL;
610        allfiles.push_back(scanResult);
611    }
612
613    if (allfiles.size() == 0)  // if no result from date search, return generic filename
614    {
615        scanResult = directory + filename + ".txt";
616    }
617    else 
618    {
619        std::sort(allfiles.begin(), allfiles.end());
620        scanResult = directory + allfiles.back();
621        // thisfile is now the most recent version of the file.
622    }
623	//LL_INFOS("") << "Reading:" << scanResult << LL_ENDL;/* uncomment if you want to verify step, delete on commit */
624    return scanResult;
625}