summaryrefslogtreecommitdiff
path: root/indra/newview/llimpanel.cpp
blob: 83c08ec602612dc4776c93ba6d0402b685cfb82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/** 
 * @file llimpanel.cpp
 * @brief LLIMPanel class definition
 *
 * Copyright (c) 2001-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include "llviewerprecompiledheaders.h"

#include "llimpanel.h"

#include "indra_constants.h"
#include "llfocusmgr.h"
#include "llfontgl.h"
#include "llrect.h"
#include "llerror.h"
#include "llstring.h"
#include "message.h"
#include "lltextbox.h"

#include "llagent.h"
#include "llbutton.h"
#include "llcallingcard.h"
#include "llconsole.h"
#include "llfloater.h"
#include "llinventory.h"
#include "llinventorymodel.h"
#include "llinventoryview.h"
#include "llfloateravatarinfo.h"
#include "llkeyboard.h"
#include "lllineeditor.h"
#include "llresmgr.h"
#include "lltabcontainer.h"
#include "llimview.h"
#include "llviewertexteditor.h"
#include "llviewermessage.h"
#include "llviewerstats.h"
#include "viewer.h"
#include "llvieweruictrlfactory.h"
#include "lllogchat.h"
#include "llfloaterhtml.h"
#include "llweb.h"

//
// Constants
//
const S32 LINE_HEIGHT = 16;
const S32 MIN_WIDTH = 200;
const S32 MIN_HEIGHT = 130;

//
// Statics
//
//
static LLString sTitleString = "Instant Message with [NAME]";
static LLString sTypingStartString = "[NAME]: ...";

// Member Functions
//

LLFloaterIMPanel::LLFloaterIMPanel(const std::string& name, const LLRect& rect,
					 const std::string& session_label,
					 const LLUUID& session_id, 
					 const LLUUID& other_participant_id,
					 EInstantMessage dialog) :
	LLFloater(name, rect, session_label),
	mInputEditor(NULL),
	mHistoryEditor(NULL),
	mSessionLabel(session_label),
	mSessionUUID(session_id),
	mOtherParticipantUUID(other_participant_id),
	mLureID(),
	mDialog(dialog),
	mTyping(FALSE),
	mOtherTyping(FALSE),
	mTypingLineStartIndex(0),
	mSentTypingState(TRUE),
	mFirstKeystrokeTimer(),
	mLastKeystrokeTimer()
{
	init();
	setLabel(session_label);
	setTitle(session_label);
	mInputEditor->setMaxTextLength(1023);
}


void LLFloaterIMPanel::init()
{
	gUICtrlFactory->buildFloater(this, "floater_instant_message.xml", NULL, FALSE);
}


BOOL LLFloaterIMPanel::postBuild() 
{
	requires("chat_editor", WIDGET_TYPE_LINE_EDITOR);
	requires("profile_btn", WIDGET_TYPE_BUTTON);
	requires("close_btn", WIDGET_TYPE_BUTTON);
	requires("im_history", WIDGET_TYPE_TEXT_EDITOR);
	requires("live_help_dialog", WIDGET_TYPE_TEXT_BOX);
	requires("title_string", WIDGET_TYPE_TEXT_BOX);
	requires("typing_start_string", WIDGET_TYPE_TEXT_BOX);

	if (checkRequirements())
	{
		mInputEditor = LLUICtrlFactory::getLineEditorByName(this, "chat_editor");
		mInputEditor->setFocusReceivedCallback( onInputEditorFocusReceived );
		mInputEditor->setFocusLostCallback( onInputEditorFocusLost );
		mInputEditor->setKeystrokeCallback( onInputEditorKeystroke );
		mInputEditor->setCallbackUserData(this);
		mInputEditor->setCommitOnFocusLost( FALSE );
		mInputEditor->setRevertOnEsc( FALSE );

		LLButton* profile_btn = LLUICtrlFactory::getButtonByName(this, "profile_btn");
		profile_btn->setClickedCallback(&LLFloaterIMPanel::onClickProfile, this);

		LLButton* close_btn = LLUICtrlFactory::getButtonByName(this, "close_btn");
		close_btn->setClickedCallback(&LLFloaterIMPanel::onClickClose, this);

		mHistoryEditor = LLViewerUICtrlFactory::getViewerTextEditorByName(this, "im_history");
		mHistoryEditor->setParseHTML(TRUE);
		if ( gSavedPerAccountSettings.getBOOL("LogShowHistory") )
		{
			LLLogChat::loadHistory(mSessionLabel, &chatFromLogFile, (void *)this); 
		}

		if (IM_SESSION_GROUP_START == mDialog
			|| IM_SESSION_911_START == mDialog)
		{
			profile_btn->setEnabled(FALSE);
			if(IM_SESSION_911_START == mDialog)
			{
				LLTextBox* live_help_text = LLUICtrlFactory::getTextBoxByName(this, "live_help_dialog");
				addHistoryLine(live_help_text->getText());
			}
		}

		LLTextBox* title = LLUICtrlFactory::getTextBoxByName(this, "title_string");
		sTitleString = title->getText();
		
		LLTextBox* typing_start = LLUICtrlFactory::getTextBoxByName(this, "typing_start_string");
				
		sTypingStartString = typing_start->getText();

		return TRUE;
	}

	return FALSE;
}

// virtual
void LLFloaterIMPanel::draw()
{
	if (mTyping)
	{
		// Time out if user hasn't typed for a while.
		if (mLastKeystrokeTimer.getElapsedTimeF32() > LLAgent::TYPING_TIMEOUT_SECS)
		{
			setTyping(FALSE);
		}

		// If we are typing, and it's been a little while, send the
		// typing indicator
		if (!mSentTypingState
			&& mFirstKeystrokeTimer.getElapsedTimeF32() > 1.f)
		{
			sendTypingState(TRUE);
			mSentTypingState = TRUE;
		}
	}

	LLFloater::draw();
}


BOOL LLFloaterIMPanel::addParticipants(const LLDynamicArray<LLUUID>& ids)
{
	if(isAddAllowed())
	{
		llinfos << "LLFloaterIMPanel::addParticipants() - adding participants" << llendl;
		const S32 MAX_AGENTS = 50;
		S32 count = ids.count();
		if(count > MAX_AGENTS) return FALSE;
		LLMessageSystem *msg = gMessageSystem;
		msg->newMessageFast(_PREHASH_ImprovedInstantMessage);
		msg->nextBlockFast(_PREHASH_AgentData);
		msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
		msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
		msg->nextBlockFast(_PREHASH_MessageBlock);
		msg->addBOOLFast(_PREHASH_FromGroup, FALSE);
		msg->addUUIDFast(_PREHASH_ToAgentID, mOtherParticipantUUID);
		msg->addU8Fast(_PREHASH_Offline, IM_ONLINE);
		msg->addU8Fast(_PREHASH_Dialog, mDialog);
		msg->addUUIDFast(_PREHASH_ID, mSessionUUID);
		msg->addU32Fast(_PREHASH_Timestamp, NO_TIMESTAMP); // no timestamp necessary
		std::string name;
		gAgent.buildFullname(name);
		msg->addStringFast(_PREHASH_FromAgentName, name);
		msg->addStringFast(_PREHASH_Message, LLString::null);
		msg->addU32Fast(_PREHASH_ParentEstateID, 0);
		msg->addUUIDFast(_PREHASH_RegionID, LLUUID::null);
		msg->addVector3Fast(_PREHASH_Position, gAgent.getPositionAgent());
		if (IM_SESSION_GROUP_START == mDialog)
		{
			// *HACK: binary bucket contains session label - the server
			// will actually add agents.
			llinfos << "Group IM session name '" << mSessionLabel 
				<< "'" << llendl;
			msg->addStringFast(_PREHASH_BinaryBucket, mSessionLabel);
			gAgent.sendReliableMessage();
		}
		else if (IM_SESSION_911_START == mDialog)
		{
			// HACK -- we modify the name of the session going out to
			// the helpers to help them easily identify "Help"
			// sessions in their collection of IM panels.
			LLString name;
			gAgent.getName(name);
			LLString buffer = LLString("HELP ") +  name;
			llinfos << "LiveHelp IM session '" << buffer << "'." << llendl;
			msg->addStringFast(_PREHASH_BinaryBucket, buffer.c_str());

			// automaticaly open a wormhole when this reliable message gets through
			msg->sendReliable(
					gAgent.getRegionHost(),
					3,      // retries
					TRUE,   // ping-based
					5.0f,   // timeout
					send_lure_911,
					(void**)&mSessionUUID);
		}
		else
		{
			if (mDialog != IM_SESSION_ADD
				&& mDialog != IM_SESSION_OFFLINE_ADD)
			{
				llwarns << "LLFloaterIMPanel::addParticipants() - dialog type " << mDialog
					<< " is not an ADD" << llendl;
			}
			// *FIX: this could suffer from endian issues
			S32 bucket_size = UUID_BYTES * count;
			U8* bucket = new U8[bucket_size];
			U8* pos = bucket;
			for(S32 i = 0; i < count; ++i)
			{
				memcpy(pos, &(ids.get(i)), UUID_BYTES);		/* Flawfinder: ignore */
				pos += UUID_BYTES;
			}
			msg->addBinaryDataFast(_PREHASH_BinaryBucket, bucket, bucket_size);
			delete[] bucket;
			gAgent.sendReliableMessage();
		}

	}
	else
	{
		llinfos << "LLFloaterIMPanel::addParticipants() - no need to add agents for "
				<< mDialog << llendl;
		// successful add, because everyone that needed to get added
		// was added.
	}
	return TRUE;
}

void LLFloaterIMPanel::addHistoryLine(const std::string &utf8msg, const LLColor4& color, bool log_to_file)
{
	LLMultiFloater* hostp = getHost();
	if( !getVisible() && hostp && log_to_file)
	{
		// Only flash for logged ("real") messages
		LLTabContainer* parent = (LLTabContainer*) getParent();
		parent->setTabPanelFlashing( this, TRUE );
	}

	// Now we're adding the actual line of text, so erase the 
	// "Foo is typing..." text segment, and the optional timestamp
	// if it was present. JC
	removeTypingIndicator();

	// Actually add the line
	LLString timestring;
	bool prepend_newline = true;
	if (gSavedSettings.getBOOL("IMShowTimestamps"))
	{
		timestring = mHistoryEditor->appendTime(prepend_newline);
		prepend_newline = false;
	}
	mHistoryEditor->appendColoredText(utf8msg, false, prepend_newline, color);
	
	if (log_to_file
		&& gSavedPerAccountSettings.getBOOL("LogInstantMessages") ) 
	{
		LLString histstr =  timestring + utf8msg;

		LLLogChat::saveHistory(mSessionLabel,histstr);
	}
}


void LLFloaterIMPanel::setVisible(BOOL b)
{
	LLPanel::setVisible(b);

	LLMultiFloater* hostp = getHost();
	if( b && hostp )
	{
		LLTabContainer* parent = (LLTabContainer*) getParent();

		// When this tab is displayed, you can stop flashing.
		parent->setTabPanelFlashing( this, FALSE );

		/* Don't change containing floater title - leave it "Instant Message" JC
		LLUIString title = sTitleString;
		title.setArg("[NAME]", mSessionLabel);
		hostp->setTitle( title );
		*/
	}
}


void LLFloaterIMPanel::setInputFocus( BOOL b )
{
	mInputEditor->setFocus( b );
}


void LLFloaterIMPanel::selectAll()
{
	mInputEditor->selectAll();
}


void LLFloaterIMPanel::selectNone()
{
	mInputEditor->deselect();
}


BOOL LLFloaterIMPanel::handleKeyHere( KEY key, MASK mask, BOOL called_from_parent )
{
	BOOL handled = FALSE;
	if( getVisible() && mEnabled && !called_from_parent && gFocusMgr.childHasKeyboardFocus(this))
	{
		if( KEY_RETURN == key && mask == MASK_NONE)
		{
			sendMsg();
			handled = TRUE;

			// Close talk panels on hitting return
			// but not shift-return or control-return
			if ( !gSavedSettings.getBOOL("PinTalkViewOpen") && !(mask & MASK_CONTROL) && !(mask & MASK_SHIFT) )
			{
				gIMView->toggle(NULL);
			}
		}
		else if ( KEY_ESCAPE == key )
		{
			handled = TRUE;
			gFocusMgr.setKeyboardFocus(NULL, NULL);

			// Close talk panel with escape
			if( !gSavedSettings.getBOOL("PinTalkViewOpen") )
			{
				gIMView->toggle(NULL);
			}
		}
	}

	// May need to call base class LLPanel::handleKeyHere if not handled
	// in order to tab between buttons.  JNC 1.2.2002
	return handled;
}

BOOL LLFloaterIMPanel::handleDragAndDrop(S32 x, S32 y, MASK mask, BOOL drop,
								  EDragAndDropType cargo_type,
								  void* cargo_data,
								  EAcceptance* accept,
								  LLString& tooltip_msg)
{
	BOOL accepted = FALSE;
	switch(cargo_type)
	{
	case DAD_CALLINGCARD:
		accepted = dropCallingCard((LLInventoryItem*)cargo_data, drop);
		break;
	case DAD_CATEGORY:
		accepted = dropCategory((LLInventoryCategory*)cargo_data, drop);
		break;
	default:
		break;
	}
	if (accepted)
	{
		*accept = ACCEPT_YES_MULTI;
	}
	else
	{
		*accept = ACCEPT_NO;
	}
	return TRUE;
} 

BOOL LLFloaterIMPanel::dropCallingCard(LLInventoryItem* item, BOOL drop)
{
	BOOL rv = isAddAllowed();
	if(rv && item && item->getCreatorUUID().notNull())
	{
		if(drop)
		{
			LLDynamicArray<LLUUID> ids;
			ids.put(item->getCreatorUUID());
			addParticipants(ids);
		}
	}
	else
	{
		// set to false if creator uuid is null.
		rv = FALSE;
	}
	return rv;
}

BOOL LLFloaterIMPanel::dropCategory(LLInventoryCategory* category, BOOL drop)
{
	BOOL rv = isAddAllowed();
	if(rv && category)
	{
		LLInventoryModel::cat_array_t cats;
		LLInventoryModel::item_array_t items;
		LLUniqueBuddyCollector buddies;
		gInventory.collectDescendentsIf(category->getUUID(),
										cats,
										items,
										LLInventoryModel::EXCLUDE_TRASH,
										buddies);
		S32 count = items.count();
		if(count == 0)
		{
			rv = FALSE;
		}
		else if(drop)
		{
			LLDynamicArray<LLUUID> ids;
			for(S32 i = 0; i < count; ++i)
			{
				ids.put(items.get(i)->getCreatorUUID());
			}
			addParticipants(ids);
		}
	}
	return rv;
}

BOOL LLFloaterIMPanel::isAddAllowed() const
{

	return ((IM_SESSION_ADD == mDialog) 
			|| (IM_SESSION_OFFLINE_ADD == mDialog)
			|| (IM_SESSION_GROUP_START == mDialog)
			|| (IM_SESSION_911_START == mDialog)
			|| (IM_SESSION_CARDLESS_START == mDialog));
}


// static
void LLFloaterIMPanel::onTabClick(void* userdata)
{
	LLFloaterIMPanel* self = (LLFloaterIMPanel*) userdata;
	self->setInputFocus(TRUE);
}


// static
void LLFloaterIMPanel::onClickProfile( void* userdata )
{
	//  Bring up the Profile window
	LLFloaterIMPanel* self = (LLFloaterIMPanel*) userdata;
	
	if (self->mOtherParticipantUUID.notNull())
	{
		LLFloaterAvatarInfo::showFromDirectory(self->getOtherParticipantID());
	}
}

// static
void LLFloaterIMPanel::onClickClose( void* userdata )
{
	LLFloaterIMPanel* self = (LLFloaterIMPanel*) userdata;
	if(self)
	{
		self->close();
	}
}

void LLFloaterIMPanel::addTeleportButton(const LLUUID& lure_id)
{
	LLButton* btn = LLViewerUICtrlFactory::getButtonByName(this, "Teleport Btn");
	if (!btn)
	{
		S32 BTN_VPAD = 2;
		S32 BTN_HPAD = 2;

		const char* teleport_label = "Teleport";
	
		const LLFontGL* font = gResMgr->getRes( LLFONT_SANSSERIF );
		S32 p_btn_width = 75;
		S32 c_btn_width = 60;
		S32 t_btn_width = 75;
	
		// adjust the size of the editor to make room for the new button
		LLRect rect = mInputEditor->getRect();
		S32 editor_right = rect.mRight - t_btn_width;
		rect.mRight = editor_right;
		mInputEditor->reshape(rect.getWidth(), rect.getHeight(), FALSE);
		mInputEditor->setRect(rect);
	
		const S32 IMPANEL_PAD = 1 + LLPANEL_BORDER_WIDTH;
		const S32 IMPANEL_INPUT_HEIGHT = 20;

		rect.setLeftTopAndSize(
			mRect.getWidth() - IMPANEL_PAD - p_btn_width - c_btn_width - t_btn_width - BTN_HPAD - RESIZE_HANDLE_WIDTH,
			IMPANEL_INPUT_HEIGHT + IMPANEL_PAD - BTN_VPAD,
			t_btn_width,
			BTN_HEIGHT);

		btn = new LLButton( 
			"Teleport Btn", rect, 
			"","",	"",
			&LLFloaterIMPanel::onTeleport, this,
			font, teleport_label, teleport_label );
		
		btn->setFollowsBottom();
		btn->setFollowsRight();
		addChild( btn );
	}
	btn->setEnabled(TRUE);
	mLureID = lure_id;
}

void LLFloaterIMPanel::removeTeleportButton()
{
	// TODO -- purge the button
	LLButton* btn = LLViewerUICtrlFactory::getButtonByName(this, "Teleport Btn");
	if (btn)
	{
		btn->setEnabled(FALSE);
	}
}

// static 
void LLFloaterIMPanel::onTeleport(void* userdata)
{
	LLFloaterIMPanel* self = (LLFloaterIMPanel*) userdata;
	if(self)
	{
		send_simple_im(self->mLureID,
							"",
							IM_LURE_911,
							LLUUID::null);
		self->removeTeleportButton();
	}
}

// static
void LLFloaterIMPanel::onInputEditorFocusReceived( LLUICtrl* caller, void* userdata )
{
	LLFloaterIMPanel* self= (LLFloaterIMPanel*) userdata;
	self->mHistoryEditor->setCursorAndScrollToEnd();
}

// static
void LLFloaterIMPanel::onInputEditorFocusLost(LLLineEditor* caller, void* userdata)
{
	LLFloaterIMPanel* self = (LLFloaterIMPanel*) userdata;
	self->setTyping(FALSE);
}

// static
void LLFloaterIMPanel::onInputEditorKeystroke(LLLineEditor* caller, void* userdata)
{
	LLFloaterIMPanel* self = (LLFloaterIMPanel*)userdata;
	LLString text = self->mInputEditor->getText();
	if (!text.empty())
	{
		self->setTyping(TRUE);
	}
	else
	{
		// Deleting all text counts as stopping typing.
		self->setTyping(FALSE);
	}
}

void LLFloaterIMPanel::onClose(bool app_quitting)
{
	setTyping(FALSE);

	if(mSessionUUID.notNull())
	{
		std::string name;
		gAgent.buildFullname(name);
		pack_instant_message(
			gMessageSystem,
			gAgent.getID(),
			FALSE,
			gAgent.getSessionID(),
			mOtherParticipantUUID,
			name.c_str(), 
			"",
			IM_ONLINE,
			IM_SESSION_DROP,
			mSessionUUID);
		gAgent.sendReliableMessage();
	}
	gIMView->removeSession(mSessionUUID);

	destroy();
}


void LLFloaterIMPanel::sendMsg()
{
	LLWString text = mInputEditor->getWText();
	LLWString::trim(text);
	if (!gAgent.isGodlike() 
		&& (mDialog == IM_NOTHING_SPECIAL)
		&& mOtherParticipantUUID.isNull())
	{
		llinfos << "Cannot send IM to everyone unless you're a god." << llendl;
		return;
	}
	if(text.length() > 0)
	{
		// Truncate and convert to UTF8 for transport
		std::string utf8_text = wstring_to_utf8str(text);
		utf8_text = utf8str_truncate(utf8_text, MAX_MSG_BUF_SIZE - 1);
		std::string name;
		gAgent.buildFullname(name);

		const LLRelationship* info = NULL;
		info = LLAvatarTracker::instance().getBuddyInfo(mOtherParticipantUUID);
		U8 offline = (!info || info->isOnline()) ? IM_ONLINE : IM_OFFLINE;

		// default to IM_SESSION_SEND unless it's nothing special - in
		// which case it's probably an IM to everyone.
		U8 dialog = (mDialog == IM_NOTHING_SPECIAL)
			? (U8)IM_NOTHING_SPECIAL : (U8)IM_SESSION_SEND;
		pack_instant_message(
			gMessageSystem,
			gAgent.getID(),
			FALSE,
			gAgent.getSessionID(),
			mOtherParticipantUUID,
			name.c_str(),
			utf8_text.c_str(),
			offline,
			(EInstantMessage)dialog,
			mSessionUUID);
		gAgent.sendReliableMessage();

		// local echo
		if((mDialog == IM_NOTHING_SPECIAL) && (mOtherParticipantUUID.notNull()))
		{
			std::string history_echo;
			gAgent.buildFullname(history_echo);

			// Look for IRC-style emotes here.
			char tmpstr[5];		/* Flawfinder: ignore */
			strncpy(tmpstr,utf8_text.substr(0,4).c_str(), sizeof(tmpstr) -1);		/* Flawfinder: ignore */
			tmpstr[sizeof(tmpstr) -1] = '\0';
			if (!strncmp(tmpstr, "/me ", 4) || !strncmp(tmpstr, "/me'", 4))
			{
				utf8_text.replace(0,3,"");
			}
			else
			{
				history_echo += ": ";
			}
			history_echo += utf8_text;
			addHistoryLine(history_echo);
		}

		gViewerStats->incStat(LLViewerStats::ST_IM_COUNT);
	}
	mInputEditor->setText("");

	// Don't need to actually send the typing stop message, the other
	// client will infer it from receiving the message.
	mTyping = FALSE;
	mSentTypingState = TRUE;
}


void LLFloaterIMPanel::setTyping(BOOL typing)
{
	if (typing)
	{
		// Every time you type something, reset this timer
		mLastKeystrokeTimer.reset();

		if (!mTyping)
		{
			// You just started typing.
			mFirstKeystrokeTimer.reset();

			// Will send typing state after a short delay.
			mSentTypingState = FALSE;
		}
	}
	else
	{
		if (mTyping)
		{
			// you just stopped typing, send state immediately
			sendTypingState(FALSE);
			mSentTypingState = TRUE;
		}
	}

	mTyping = typing;
}

void LLFloaterIMPanel::sendTypingState(BOOL typing)
{
	// Don't want to send typing indicators to multiple people, potentially too
	// much network traffic.  Only send in person-to-person IMs.
	if (mDialog != IM_NOTHING_SPECIAL) return;

	std::string name;
	gAgent.buildFullname(name);

	pack_instant_message(
		gMessageSystem,
		gAgent.getID(),
		FALSE,
		gAgent.getSessionID(),
		mOtherParticipantUUID,
		name.c_str(),
		"typing",
		IM_ONLINE,
		(typing ? IM_TYPING_START : IM_TYPING_STOP),
		mSessionUUID);
	gAgent.sendReliableMessage();
}


void LLFloaterIMPanel::processIMTyping(const LLIMInfo* im_info, BOOL typing)
{
	if (typing)
	{
		// other user started typing
		addTypingIndicator(im_info);
	}
	else
	{
		// other user stopped typing
		removeTypingIndicator();
	}
}


void LLFloaterIMPanel::addTypingIndicator(const LLIMInfo* im_info)
{
	mTypingLineStartIndex = mHistoryEditor->getText().length();

	LLUIString typing_start = sTypingStartString;
	typing_start.setArg("[NAME]", im_info->mName);
	bool log_to_file = false;
	addHistoryLine(typing_start, LLColor4::grey, log_to_file);
	mOtherTyping = TRUE;
}


void LLFloaterIMPanel::removeTypingIndicator()
{
	if (mOtherTyping)
	{
		// Must do this first, otherwise addHistoryLine calls us again.
		mOtherTyping = FALSE;

		S32 chars_to_remove = mHistoryEditor->getText().length() - mTypingLineStartIndex;
		mHistoryEditor->removeTextFromEnd(chars_to_remove);
	}
}

//static
void LLFloaterIMPanel::chatFromLogFile(LLString line, void* userdata)
{
	LLFloaterIMPanel* self = (LLFloaterIMPanel*)userdata;
	
	//self->addHistoryLine(line, LLColor4::grey, FALSE);
	self->mHistoryEditor->appendColoredText(line, false, true, LLColor4::grey);

}