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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
|
/*
* The Mana World
* Copyright (C) 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <guichan/focushandler.hpp>
#include "browserbox.h"
#include "chat.h"
#include "chatinput.h"
#include "itemlinkhandler.h"
#include "recorder.h"
#include "scrollarea.h"
#include "sdlinput.h"
#include "windowcontainer.h"
#include "widgets/layout.h"
#include "widgets/tab.h"
#include "widgets/tabbedarea.h"
#include "../beingmanager.h"
#ifdef TMWSERV_SUPPORT
#include "../commandhandler.h"
#endif
#include "../channelmanager.h"
#include "../channel.h"
#include "../configuration.h"
#include "../game.h"
#include "../localplayer.h"
#ifdef TMWSERV_SUPPORT
#include "../net/chatserver/chatserver.h"
#include "../net/gameserver/player.h"
#else
#include "../party.h"
#include "../net/messageout.h"
#include "../net/ea/protocol.h"
#endif
#include "../resources/iteminfo.h"
#include "../resources/itemdb.h"
#include "../utils/dtor.h"
#include "../utils/gettext.h"
#include "../utils/strprintf.h"
#include "../utils/stringutils.h"
#ifdef TMWSERV_SUPPORT
ChatWindow::ChatWindow():
Window("Chat"),
#else
ChatWindow::ChatWindow(Network * network):
Window(""), mNetwork(network),
#endif
mTmpVisible(false)
{
setWindowName("Chat");
setResizable(true);
setDefaultSize(0, windowContainer->getHeight() - 123, 600, 123);
setMinWidth(150);
setMinHeight(90);
mItemLinkHandler = new ItemLinkHandler;
mChatInput = new ChatInput;
mChatInput->setActionEventId("chatinput");
mChatInput->addActionListener(this);
BrowserBox *textOutput = new BrowserBox(BrowserBox::AUTO_WRAP);
textOutput->setOpaque(false);
textOutput->setMaxRow((int) config.getValue("ChatLogLength", 0));
textOutput->setLinkHandler(mItemLinkHandler);
ScrollArea *scrollArea = new ScrollArea(textOutput);
scrollArea->setScrollPolicy(gcn::ScrollArea::SHOW_NEVER,
gcn::ScrollArea::SHOW_ALWAYS);
scrollArea->setScrollAmount(0, 1);
scrollArea->setOpaque(false);
mChatTabs = new TabbedArea();
mChatTabs->addTab("General", scrollArea);
place(0, 0, mChatTabs, 5, 5).setPadding(0);
place(0, 5, mChatInput, 5).setPadding(1);
Layout &layout = getLayout();
layout.setRowHeight(0, Layout::AUTO_SET);
layout.setMargin(2);
loadWindowState();
// Add key listener to chat input to be able to respond to up/down
mChatInput->addKeyListener(this);
mCurHist = mHistory.end();
#ifdef EATHENA_SUPPORT
// Read the party prefix
std::string partyPrefix = config.getValue("PartyPrefix", "$");
mPartyPrefix = (partyPrefix.empty() ? '$' : partyPrefix.at(0));
mReturnToggles = config.getValue("ReturnToggles", "0") == "1";
mRecorder = new Recorder(this);
mParty = new Party(this, mNetwork);
// If the player had @assert on in the last session, ask the server to
// run the @assert command for the player again. Convenience for GMs.
if (config.getValue(player_node->getName() + "GMassert", 0))
chatSend(player_node->getName(), "@assert");
#endif
}
ChatWindow::~ChatWindow()
{
#ifdef EATHENA_SUPPORT
char partyPrefix[2] = ".";
*partyPrefix = mPartyPrefix;
config.setValue("PartyPrefix", partyPrefix);
config.setValue("ReturnToggles", mReturnToggles ? "1" : "0");
delete mRecorder;
#endif
}
void ChatWindow::widgetResized(const gcn::Event &event)
{
Window::widgetResized(event);
const gcn::Rectangle area = getChildrenArea();
mChatInput->setPosition(mChatInput->getFrameSize(),
area.height - mChatInput->getHeight() -
mChatInput->getFrameSize());
mChatInput->setWidth(area.width - 2 * mChatInput->getFrameSize());
mChatTabs->setWidth(area.width - 2 * mChatTabs->getFrameSize());
mChatTabs->setHeight(area.height - 2 * mChatTabs->getFrameSize());
const std::string &channelName = getFocused();
ChannelMap::const_iterator chan = mChannels.find(channelName);
if (chan != mChannels.end()) {
ScrollArea *scroll = chan->second.scroll;
scroll->setWidth(area.width - 2 * scroll->getFrameSize());
scroll->setHeight(area.height - 2 * scroll->getFrameSize() -
mChatInput->getHeight() - 5);
scroll->logic();
}
}
void ChatWindow::logic()
{
Window::logic();
const gcn::Rectangle area = getChildrenArea();
const std::string &channelName = getFocused();
ChannelMap::const_iterator chan = mChannels.find(channelName);
if (chan != mChannels.end()) {
ScrollArea *scroll = chan->second.scroll;
scroll->setWidth(area.width - 2 * scroll->getFrameSize());
scroll->setHeight(area.height - 2 * scroll->getFrameSize() -
mChatInput->getHeight() - 5);
scroll->logic();
}
}
void ChatWindow::chatLog(std::string line, int own, std::string channelName,
bool ignoreRecord)
{
if(channelName.empty())
channelName = getFocused();
ChannelMap::const_iterator chan = mChannels.find(channelName);
if (chan == mChannels.end())
return;
BrowserBox * const output = chan->second.browser;
ScrollArea * const scroll = chan->second.scroll;
// Trim whitespace
trim(line);
if (line.empty())
return;
CHATLOG tmp;
tmp.own = own;
tmp.nick = "";
tmp.text = line;
std::string::size_type pos = line.find(" : ");
if (pos != std::string::npos)
{
tmp.nick = line.substr(0, pos);
tmp.text = line.substr(pos + 3);
}
else
{
// Fix the owner of welcome message.
if (line.substr(0, 7) == "Welcome")
{
own = BY_SERVER;
}
}
// *implements actions in a backwards compatible way*
if (own == BY_PLAYER &&
tmp.text.at(0) == '*' &&
tmp.text.at(tmp.text.length()-1) == '*')
{
tmp.text[0] = ' ';
tmp.text.erase(tmp.text.length() - 1);
own = ACT_IS;
}
std::string lineColor = "##C";
switch (own)
{
case BY_GM:
if (tmp.nick.empty())
{
tmp.nick = std::string(_("Global announcement:"));
tmp.nick += " ";
lineColor = "##G";
}
else
{
tmp.nick = strprintf(_("Global announcement from %s:"),
tmp.nick.c_str());
tmp.nick += " ";
lineColor = "##1"; // Equiv. to BrowserBox::RED
}
break;
case BY_PLAYER:
tmp.nick += CAT_NORMAL;
lineColor = "##Y";
break;
case BY_OTHER:
tmp.nick += CAT_NORMAL;
lineColor = "##C";
break;
case BY_SERVER:
tmp.nick = _("Server:");
tmp.nick += " ";
tmp.text = line;
lineColor = "##S";
break;
case BY_CHANNEL:
tmp.nick = "";
// TODO: Use a predefined color
lineColor = "##2"; // Equiv. to BrowserBox::GREEN
break;
#ifdef EATHENA_SUPPORT
case BY_PARTY:
tmp.nick += CAT_NORMAL;
lineColor = "##P";
break;
#endif
case ACT_WHISPER:
tmp.nick = strprintf(_("%s whispers:"), tmp.nick.c_str());
tmp.nick += " ";
lineColor = "##W";
break;
case ACT_IS:
tmp.nick += CAT_IS;
lineColor = "##I";
break;
case BY_LOGGER:
tmp.nick = "";
tmp.text = line;
lineColor = "##L";
break;
}
if (tmp.nick == ": ")
{
tmp.nick = "";
lineColor = "##S";
}
#ifdef EATHENA_SUPPORT
if (tmp.nick.empty() && tmp.text.substr(0, 17) == "Visible GM status")
{
player_node->setGM();
}
#endif
// Get the current system time
time_t t;
time(&t);
// Format the time string properly
std::stringstream timeStr;
timeStr << "[" << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
<< (int) (((t / 60) / 60) % 24)
<< ":" << (((t / 60) % 60 < 10) ? "0" : "")
<< (int) ((t / 60) % 60)
<< "] ";
line = lineColor + timeStr.str() + tmp.nick + tmp.text;
// We look if the Vertical Scroll Bar is set at the max before
// adding a row, otherwise the max will always be a row higher
// at comparison.
if (scroll->getVerticalScrollAmount() >= scroll->getVerticalMaxScroll())
{
output->addRow(line);
scroll->setVerticalScrollAmount(scroll->getVerticalMaxScroll());
}
else
{
output->addRow(line);
}
scroll->logic();
mRecorder->record(line.substr(3));
}
#ifdef EATHENA_SUPPORT
void ChatWindow::chatLog(CHATSKILL act)
{
chatLog(const_msg(act), BY_SERVER);
}
#endif
const std::string &ChatWindow::getFocused() const
{
return mChatTabs->getSelectedTab()->getCaption();
}
void ChatWindow::clearTab(const std::string &tab)
{
ChannelMap::const_iterator chan = mChannels.find(tab);
if (chan != mChannels.end())
chan->second.browser->clearRows();
}
void ChatWindow::action(const gcn::ActionEvent &event)
{
if (event.getId() == "chatinput")
{
std::string message = mChatInput->getText();
if (!message.empty())
{
// If message different from previous, put it in the history
if (mHistory.empty() || message != mHistory.back())
{
mHistory.push_back(message);
}
// Reset history iterator
mCurHist = mHistory.end();
// Send the message to the server
#ifdef TMWSERV_SUPPORT
chatSend(message);
#else
chatSend(player_node->getName(), message);
#endif
// Clear the text from the chat input
mChatInput->setText("");
}
if (message.empty() || !mReturnToggles)
{
// Remove focus and hide input
mFocusHandler->focusNone();
// If the chatWindow is shown up because you want to send a message
// It should hide now
if (mTmpVisible)
setVisible(false);
}
}
}
void ChatWindow::requestChatFocus()
{
// Make sure chatWindow is visible
if (!isVisible())
{
setVisible(true);
/*
* This is used to hide chatWindow after sending the message. There is
* a trick here, because setVisible will set mTmpVisible to false, you
* have to put this sentence *after* setVisible, not before it
*/
mTmpVisible = true;
}
// Give focus to the chat input
mChatInput->setVisible(true);
mChatInput->requestFocus();
}
bool ChatWindow::isInputFocused()
{
return mChatInput->isFocused();
}
void ChatWindow::whisper(const std::string &nick, std::string msg)
{
std::string recvnick = "";
if (msg.substr(0, 1) == "\"")
{
const std::string::size_type pos = msg.find('"', 1);
if (pos != std::string::npos)
{
recvnick = msg.substr(1, pos - 1);
msg.erase(0, pos + 2);
}
}
else
{
const std::string::size_type pos = msg.find(" ");
if (pos != std::string::npos)
{
recvnick = msg.substr(0, pos);
msg.erase(0, pos + 1);
}
}
trim(msg);
std::string playerName = player_node->getName();
std::string tempNick = recvnick;
toLower(playerName);
toLower(tempNick);
if (tempNick.compare(playerName) == 0 || msg.empty())
return;
// TODO: Implement whispering on tmwserv
#ifdef EATHENA_SUPPORT
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_CHAT_WHISPER);
outMsg.writeInt16(msg.length() + 28);
outMsg.writeString(recvnick, 24);
outMsg.writeString(msg, msg.length());
chatLog(strprintf(_("Whispering to %s: %s"),
recvnick.c_str(), msg.c_str()),
BY_PLAYER);
#endif
}
#ifdef TMWSERV_SUPPORT
void ChatWindow::chatSend(std::string &msg)
{
if (msg.empty()) return;
// check for item link
std::string::size_type start = msg.find('[');
if (start != std::string::npos && msg[start+1] != '@')
{
std::string::size_type end = msg.find(']', start);
if (end != std::string::npos)
{
std::string temp = msg.substr(start+1, end-1);
ItemInfo itemInfo = ItemDB::get(temp);
msg.insert(end, "@@");
msg.insert(start+1, "|");
msg.insert(start+1, toString(itemInfo.getId()));
msg.insert(start+1, "@@");
}
}
// Prepare ordinary message
if (msg[0] != '/')
{
if (getFocused() == "General")
{
Net::GameServer::Player::say(msg);
}
else
{
Channel *channel = channelManager->findByName(getFocused());
if (channel)
{
Net::ChatServer::chat(channel->getId(), msg);
}
}
}
else
{
commandHandler->handleCommand(std::string(msg, 1));
}
}
void ChatWindow::removeChannel(short channelId)
{
removeChannel(channelManager->findById(channelId));
}
void ChatWindow::removeChannel(const std::string &channelName)
{
removeChannel(channelManager->findByName(channelName));
}
void ChatWindow::removeChannel(Channel *channel)
{
if (channel)
{
Tab *tab = mChatTabs->getTab(channel->getName());
if (!tab)
return;
clearTab(channel->getName());
mChatTabs->removeTab(tab);
mChannels.erase(channel->getName());
channelManager->removeChannel(channel);
logic();
}
}
void ChatWindow::createNewChannelTab(const std::string &channelName)
{
// Create new channel
BrowserBox *textOutput = new BrowserBox(BrowserBox::AUTO_WRAP);
textOutput->setOpaque(false);
textOutput->disableLinksAndUserColors();
textOutput->setMaxRow((int) config.getValue("ChatLogLength", 0));
ScrollArea *scrollArea = new ScrollArea(textOutput);
scrollArea->setPosition(scrollArea->getFrameSize(), scrollArea->getFrameSize());
scrollArea->setScrollPolicy(gcn::ScrollArea::SHOW_NEVER, gcn::ScrollArea::SHOW_ALWAYS);
scrollArea->setOpaque(false);
scrollArea->setWidth(getChildrenArea().width - 2 * scrollArea->getFrameSize());
scrollArea->setHeight(getChildrenArea().height - 2 * scrollArea->getFrameSize() -
mChatInput->getHeight() - 5);
scrollArea->logic();
textOutput->setWidth(scrollArea->getChildrenArea().width);
textOutput->setHeight(scrollArea->getChildrenArea().height);
// Add channel to the tabbed area
mChatTabs->addTab(channelName, scrollArea);
mChannels.insert(
std::make_pair(channelName, ChatArea(textOutput, scrollArea)));
// Update UI
logic();
}
void ChatWindow::sendToChannel(short channelId,
const std::string &user,
const std::string &msg)
{
Channel *channel = channelManager->findById(channelId);
if (channel)
{
std::string channelName = channel->getName();
chatLog(user + ": " + msg, user == player_node->getName() ? BY_PLAYER : BY_OTHER, channelName);
mChatTabs->getTab(channelName)->setHighlighted(true);
}
}
#else
void ChatWindow::chatSend(const std::string &nick, std::string msg)
{
/* Some messages are managed client side, while others
* require server handling by proper packet. Probably
* those if elses should be replaced by protocol calls */
trim(msg);
if (msg.compare("") == 0)
return;
// Send party message
if (msg.at(0) == mPartyPrefix)
{
msg.erase(0, 1);
std::size_t length = msg.length() + 1;
if (length == 0)
{
chatLog(_("Trying to send a blank party message."), BY_SERVER);
return;
}
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_PARTY_MESSAGE);
outMsg.writeInt16(length + 4);
outMsg.writeString(msg, length);
return;
}
// Check for item link
std::string::size_type start = msg.find('[');
while (start != std::string::npos && msg[start+1] != '@')
{
std::string::size_type end = msg.find(']', start);
if (start+1 != end && end != std::string::npos)
{
// Catch multiple embeds and ignore them
// so it doesn't crash the client.
while ((msg.find('[', start + 1) != std::string::npos) &&
(msg.find('[', start + 1) < end))
{
start = msg.find('[', start + 1);
}
std::string temp = msg.substr(start + 1, end - start - 1);
toLower(trim(temp));
const ItemInfo itemInfo = ItemDB::get(temp);
if (itemInfo.getName() != _("Unknown item"))
{
msg.insert(end, "@@");
msg.insert(start+1, "|");
msg.insert(start+1, toString(itemInfo.getId()));
msg.insert(start+1, "@@");
}
}
start = msg.find('[', start + 1);
}
// Prepare ordinary message
if (msg.substr(0, 1) != "/")
{
msg = nick + " : " + msg;
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_CHAT_MESSAGE);
// Added + 1 in order to let eAthena parse admin commands correctly
outMsg.writeInt16(msg.length() + 4 + 1);
outMsg.writeString(msg, msg.length() + 1);
return;
}
msg.erase(0, 1);
trim(msg);
std::size_t space = msg.find(" ");
std::string command = msg.substr(0, space);
if (space == std::string::npos)
{
msg = "";
}
else
{
msg = msg.substr(space);
trim(msg);
}
if (command == "announce")
{
MessageOut outMsg(mNetwork);
outMsg.writeInt16(0x0099);
outMsg.writeInt16(msg.length() + 4);
outMsg.writeString(msg, msg.length());
}
else if (command == "help")
{
trim(msg);
std::size_t space = msg.find(" ");
std::string msg1;
if (space == std::string::npos)
{
msg1 = "";
}
else
{
msg1 = msg.substr(space + 1, msg.length());
msg = msg.substr(0, space);
}
if (!msg.empty() && msg.at(0) == '/')
{
msg.erase(0, 1);
}
trim(msg1);
help(msg, msg1);
}
else if (command == "where")
{
// Display the current map, X, and Y
std::ostringstream where;
where << map_path << " " << player_node->mX << "," << player_node->mY;
chatLog(where.str(), BY_SERVER);
}
else if (command == "who")
{
MessageOut outMsg(mNetwork);
outMsg.writeInt16(0x00c1);
}
else if (command == "clear")
clearTab(getFocused());
else if (command == "whisper" || command == "msg" || command == "w")
whisper(nick, msg);
else if (command == "record")
mRecorder->changeRecordingStatus(msg);
else if (command == "toggle")
{
if (msg.empty())
{
chatLog(mReturnToggles ? _("Return toggles chat.")
: _("Message closes chat."), BY_SERVER);
return;
}
msg = msg.substr(0, 1);
if (msg == "1" ||
msg == "y" || msg == "Y" ||
msg == "t" || msg == "T")
{
chatLog(_("Return now toggles chat."), BY_SERVER);
mReturnToggles = true;
return;
}
else if (msg == "0" ||
msg == "n" || msg == "N" ||
msg == "f" || msg == "F")
{
chatLog(_("Message now closes chat."), BY_SERVER);
mReturnToggles = false;
return;
}
else
chatLog(_("Options to /toggle are \"yes\", \"no\", \"true\", "
"\"false\", \"1\", \"0\"."), BY_SERVER);
}
else if (command == "party")
{
if (msg.empty())
{
chatLog(_("Unknown party command... Type \"/help\" party for more "
"information."), BY_SERVER);
return;
}
const std::string::size_type space = msg.find(" ");
std::string rest = (space == std::string::npos ? ""
: msg.substr(space + 1, msg.length()));
if (!rest.empty())
{
msg = msg.substr(0, space);
trim(msg);
}
party(msg, rest);
return;
}
else if (command == "cast")
{
/*
* This will eventually be replaced by a GUI, so
* we don't need to get too sophisticated
*/
MessageOut outMsg(mNetwork);
if (msg == "heal")
{
outMsg.writeInt16(0x03f3);
outMsg.writeInt16(0x01);
outMsg.writeInt32(0);
outMsg.writeInt8(0);
outMsg.writeInt8(0);
outMsg.writeString("", 24);
}
else if (msg == "gather")
{
outMsg.writeInt16(0x03f3);
outMsg.writeInt16(0x02);
outMsg.writeInt32(0);
outMsg.writeInt8(0);
outMsg.writeInt8(0);
outMsg.writeString("", 24);
}
else
chatLog(_("No such spell!"), BY_SERVER);
}
else if (command == "present")
{
Beings & beings = beingManager->getAll();
std::string response = "";
for (BeingIterator bi = beings.begin(), be = beings.end();
bi != be; ++bi)
{
if ((*bi)->getType() == Being::PLAYER)
{
if (!response.empty())
{
response += ", ";
}
response += (*bi)->getName();
}
}
if (mRecorder->isRecording())
{
// Get the current system time
time_t t;
time(&t);
// Format the time string properly
std::stringstream timeStr;
timeStr << "[" << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
<< (int) (((t / 60) / 60) % 24)
<< ":" << (((t / 60) % 60 < 10) ? "0" : "")
<< (int) ((t / 60) % 60)
<< "] ";
mRecorder->record(timeStr.str() + _("Present: ") + response + ".");
chatLog(_("Attendance written to record log."), BY_SERVER, std::string(), true);
}
else
{
chatLog(_("Present: ") + response, BY_SERVER);
}
}
else if (command == "me")
{
std::stringstream actionStr;
actionStr << "*" << msg << "*";
chatSend(player_node->getName(), actionStr.str());
}
else
{
chatLog(_("Unknown command"), BY_SERVER);
}
}
std::string ChatWindow::const_msg(CHATSKILL act)
{
std::string msg;
if (act.success == SKILL_FAILED && act.skill == SKILL_BASIC)
{
switch (act.bskill)
{
case BSKILL_TRADE:
msg = _("Trade failed!");
break;
case BSKILL_EMOTE:
msg = _("Emote failed!");
break;
case BSKILL_SIT:
msg = _("Sit failed!");
break;
case BSKILL_CREATECHAT:
msg = _("Chat creating failed!");
break;
case BSKILL_JOINPARTY:
msg = _("Could not join party!");
break;
case BSKILL_SHOUT:
msg = _("Cannot shout!");
break;
}
msg += " ";
switch (act.reason)
{
case RFAIL_SKILLDEP:
msg += _("You have not yet reached a high enough lvl!");
break;
case RFAIL_INSUFHP:
msg += _("Insufficient HP!");
break;
case RFAIL_INSUFSP:
msg += _("Insufficient SP!");
break;
case RFAIL_NOMEMO:
msg += _("You have no memos!");
break;
case RFAIL_SKILLDELAY:
msg += _("You cannot do that right now!");
break;
case RFAIL_ZENY:
msg += _("Seems you need more money... ;-)");
break;
case RFAIL_WEAPON:
msg += _("You cannot use this skill with that kind of weapon!");
break;
case RFAIL_REDGEM:
msg += _("You need another red gem!");
break;
case RFAIL_BLUEGEM:
msg += _("You need another blue gem!");
break;
case RFAIL_OVERWEIGHT:
msg += _("You're carrying to much to do this!");
break;
default:
msg += _("Huh? What's that?");
break;
}
}
else
{
switch (act.skill)
{
case SKILL_WARP :
msg = _("Warp failed...");
break;
case SKILL_STEAL :
msg = _("Could not steal anything...");
break;
case SKILL_ENVENOM :
msg = _("Poison had no effect...");
break;
}
}
return msg;
}
#endif
void ChatWindow::scroll(int amount)
{
if (!isVisible())
return;
ChannelMap::const_iterator chan = mChannels.find(getFocused());
if (chan == mChannels.end())
return;
BrowserBox *browser = chan->second.browser;
ScrollArea *scroll = chan->second.scroll;
int range = scroll->getHeight() / 8 * amount;
gcn::Rectangle scr;
scr.y = scroll->getVerticalScrollAmount() + range;
scr.height = abs(range);
browser->showPart(scr);
}
void ChatWindow::keyPressed(gcn::KeyEvent &event)
{
if (event.getKey().getValue() == Key::DOWN &&
mCurHist != mHistory.end())
{
// Move forward through the history
HistoryIterator prevHist = mCurHist++;
if (mCurHist != mHistory.end())
{
mChatInput->setText(*mCurHist);
mChatInput->setCaretPosition(mChatInput->getText().length());
}
else
{
mCurHist = prevHist;
}
}
else if (event.getKey().getValue() == Key::UP &&
mCurHist != mHistory.begin() && mHistory.size() > 0)
{
// Move backward through the history
mCurHist--;
mChatInput->setText(*mCurHist);
mChatInput->setCaretPosition(mChatInput->getText().length());
}
}
void ChatWindow::setInputText(std::string input_str)
{
mChatInput->setText(mChatInput->getText() + input_str + " ");
requestChatFocus();
}
void ChatWindow::addItemText(const std::string &item)
{
std::ostringstream text;
text << "[" << item << "] ";
mChatInput->setText(mChatInput->getText() + text.str());
requestChatFocus();
}
void ChatWindow::setVisible(bool isVisible)
{
Window::setVisible(isVisible);
/*
* For whatever reason, if setVisible is called, the mTmpVisible effect
* should be disabled.
*/
mTmpVisible = false;
}
#ifdef EATHENA_SUPPORT
void ChatWindow::party(const std::string & command, const std::string & rest)
{
if (command == "prefix")
{
if (rest.empty())
{
char temp[2] = ".";
*temp = mPartyPrefix;
chatLog(_("The current party prefix is ") + std::string(temp),
BY_SERVER);
}
else if (rest.length() != 1)
{
chatLog(_("Party prefix must be one character long."), BY_SERVER);
}
else
{
if (rest == "/")
{
chatLog(_("Cannot use a '/' as the prefix."), BY_SERVER);
}
else
{
mPartyPrefix = rest.at(0);
chatLog(_("Changing prefix to ") + rest, BY_SERVER);
}
}
}
else
mParty->respond(command, rest);
}
#endif
void ChatWindow::help(const std::string & msg1, const std::string & msg2)
{
chatLog(_("-- Help --"), BY_SERVER);
if (msg1.empty())
{
chatLog(_("/announce: Global announcement (GM only)"), BY_SERVER);
chatLog(_("/clear: Clears this window"), BY_SERVER);
chatLog(_("/help: Display this help"), BY_SERVER);
chatLog(_("/me <message>: Tell something about yourself"), BY_SERVER);
chatLog(_("/msg <nick> <message>: Alternate form for /whisper"),
BY_SERVER);
chatLog(_("/party <command> <params>: Party commands"), BY_SERVER);
chatLog(_("/present: Get list of players present"), BY_SERVER);
chatLog(_("/record <filename>: Start recording the chat to an "
"external file"), BY_SERVER);
chatLog(_("/toggle: Determine whether <return> toggles the chat log"),
BY_SERVER);
chatLog(_("/w <nick> <message>: Short form for /whisper"), BY_SERVER);
chatLog(_("/where: Display map name"), BY_SERVER);
chatLog(_("/whisper <nick> <message>: Sends a private <message> "
"to <nick>"), BY_SERVER);
chatLog(_("/who: Display number of online users"), BY_SERVER);
chatLog(_("For more information, type /help <command>"), BY_SERVER);
}
else if (msg1 == "announce")
{
chatLog(_("Command: /announce <msg>"), BY_SERVER);
chatLog(_("*** only available to a GM ***"), BY_SERVER);
chatLog(_("This command sends the message <msg> to "
"all players currently online."), BY_SERVER);
}
else if (msg1 == "clear")
{
chatLog(_("Command: /clear"), BY_SERVER);
chatLog(_("This command clears the chat log of previous chat."),
BY_SERVER);
}
else if (msg1 == "help")
{
chatLog(_("Command: /help"), BY_SERVER);
chatLog(_("This command displays a list of all commands available."),
BY_SERVER);
chatLog(_("Command: /help <command>"), BY_SERVER);
chatLog(_("This command displays help on <command>."), BY_SERVER);
}
else if (msg1 == "me")
{
chatLog(_("Command: /me <msg>"), BY_SERVER);
chatLog(_("This command tell others you are (doing) <msg>."),
BY_SERVER);
}
#ifdef EATHENA_SUPPORT
else if (msg1 == "party")
{
mParty->help(msg2);
}
#endif
else if (msg1 == "present")
{
chatLog(_("Command: /present"), BY_SERVER);
chatLog(_("This command gets a list of players within hearing and "
"sends it to either the record log if recording, or the chat "
"log otherwise."), BY_SERVER);
}
else if (msg1 == "record")
{
chatLog(_("Command: /record <filename>"), BY_SERVER);
chatLog(_("This command starts recording the chat log to the file "
"<filename>."), BY_SERVER);
chatLog(_("Command: /record"), BY_SERVER);
chatLog(_("This command finishes a recording session."), BY_SERVER);
}
else if (msg1 == "toggle")
{
chatLog(_("Command: /toggle <state>"), BY_SERVER);
chatLog(_("This command sets whether the return key should toggle the "
"chat log, or whether the chat log turns off automatically."),
BY_SERVER);
chatLog(_("<state> can be one of \"1\", \"yes\", \"true\" to "
"turn the toggle on, or \"0\", \"no\", \"false\" to turn the "
"toggle off."), BY_SERVER);
chatLog(_("Command: /toggle"), BY_SERVER);
chatLog(_("This command displays the return toggle status."),
BY_SERVER);
}
else if (msg1 == "where")
{
chatLog(_("Command: /where"), BY_SERVER);
chatLog(_("This command displays the name of the current map."),
BY_SERVER);
}
else if (msg1 == "whisper" || msg1 == "msg" || msg1 == "w")
{
chatLog(_("Command: /msg <nick> <msg>"), BY_SERVER);
chatLog(_("Command: /whisper <nick> <msg>"), BY_SERVER);
chatLog(_("Command: /w <nick> <msg>"), BY_SERVER);
chatLog(_("This command sends the message <msg> to <nick>."),
BY_SERVER);
chatLog(_("If the <nick> has spaces in it, enclose it in "
"double quotes (\")."), BY_SERVER);
}
else if (msg1 == "who")
{
chatLog(_("Command: /who"), BY_SERVER);
chatLog(_("This command displays the number of players currently "
"online."), BY_SERVER);
}
else
{
chatLog(_("Unknown command."), BY_SERVER);
chatLog(_("Type /help for a list of commands."), BY_SERVER);
}
}
bool ChatWindow::tabExists(const std::string &tabName)
{
Tab *tab = mChatTabs->getTab(tabName);
return tab != 0;
}
|