-
Notifications
You must be signed in to change notification settings - Fork 9
/
chat.cpp
1306 lines (1145 loc) · 39.8 KB
/
chat.cpp
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
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// 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 3 of the License, or
// (at your option) 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "chat.h"
#include "player.h"
#include "iologindata.h"
#include "manager.h"
#include "configmanager.h"
#include "game.h"
extern ConfigManager g_config;
extern Game g_game;
extern Chat g_chat;
uint16_t ChatChannel::staticFlags = CHANNELFLAG_ENABLED | CHANNELFLAG_ACTIVE;
PrivateChatChannel::PrivateChatChannel(uint16_t id, std::string name, uint16_t flags):
ChatChannel(id, name, flags), m_owner(0) {}
bool PrivateChatChannel::isInvited(const Player* player)
{
if(player->getGUID() == m_owner)
return true;
return std::find(m_invites.begin(), m_invites.end(), player->getGUID()) != m_invites.end();
}
bool PrivateChatChannel::addInvited(Player* player)
{
if(std::find(m_invites.begin(), m_invites.end(), player->getGUID()) != m_invites.end())
return false;
m_invites.push_back(player->getGUID());
return true;
}
bool PrivateChatChannel::removeInvited(Player* player)
{
InviteList::iterator it = std::find(m_invites.begin(), m_invites.end(), player->getGUID());
if(it == m_invites.end())
return false;
m_invites.erase(it);
return true;
}
void PrivateChatChannel::invitePlayer(Player* player, Player* invitePlayer)
{
if(player == invitePlayer || !addInvited(invitePlayer))
return;
std::stringstream msg;
msg << player->getName() << " invites you to " << (player->getSex(false) ? "his" : "her") << " private chat channel.";
invitePlayer->sendTextMessage(MSG_INFO_DESCR, msg.str().c_str());
msg.str("");
msg << invitePlayer->getName() << " has been invited.";
player->sendTextMessage(MSG_INFO_DESCR, msg.str().c_str());
Player* tmpPlayer = NULL;
for(UsersMap::iterator cit = m_users.begin(); cit != m_users.end(); ++cit)
{
if((tmpPlayer = cit->second->getPlayer()))
tmpPlayer->sendChannelEvent(m_id, invitePlayer->getName(), CHANNELEVENT_INVITE);
}
}
void PrivateChatChannel::excludePlayer(Player* player, Player* excludePlayer)
{
if(player == excludePlayer || !removeInvited(excludePlayer))
return;
std::string msg = excludePlayer->getName();
msg += " has been excluded.";
player->sendTextMessage(MSG_INFO_DESCR, msg.c_str());
removeUser(excludePlayer, true);
excludePlayer->sendClosePrivate(getId());
Player* tmpPlayer = NULL;
for(UsersMap::iterator cit = m_users.begin(); cit != m_users.end(); ++cit)
{
if((tmpPlayer = cit->second->getPlayer()))
tmpPlayer->sendChannelEvent(m_id, excludePlayer->getName(), CHANNELEVENT_EXCLUDE);
}
}
void PrivateChatChannel::closeChannel()
{
for(UsersMap::iterator it = m_users.begin(); it != m_users.end(); ++it)
it->second->sendClosePrivate(m_id);
}
ChatChannel::ChatChannel(uint16_t id, const std::string& name, uint16_t flags, uint32_t access/* = 0*/,
uint32_t level/* = 1*/, Condition* condition/* = NULL*/, int32_t conditionId/* = -1*/,
const std::string& conditionMessage/* = ""*/, VocationMap* vocationMap/* = NULL*/):
m_id(id), m_flags(flags), m_conditionId(conditionId), m_access(access), m_level(level),
m_name(name), m_conditionMessage(conditionMessage), m_condition(condition),
m_vocationMap(vocationMap)
{
if(hasFlag(CHANNELFLAG_LOGGED))
{
m_file.reset(new std::ofstream(getFilePath(FILE_TYPE_LOG, (std::string)"chat/" + g_config.getString(
ConfigManager::PREFIX_CHANNEL_LOGS) + m_name + (std::string)".log").c_str(), std::ios::app | std::ios::out));
if(!m_file->is_open())
m_flags &= ~CHANNELFLAG_LOGGED;
}
}
bool ChatChannel::addUser(Player* player)
{
if(!player)
return false;
if(m_users.find(player->getID()) != m_users.end())
return true;
ChatChannel* channel = g_chat.getChannel(player, m_id);
if(!channel)
{
#ifdef __DEBUG_CHAT__
std::clog << "ChatChannel::addUser - failed retrieving channel." << std::endl;
#endif
return false;
}
if(m_id == CHANNEL_PARTY || m_id == CHANNEL_GUILD || m_id == CHANNEL_PRIVATE)
{
Player* tmpPlayer = NULL;
for(UsersMap::iterator cit = m_users.begin(); cit != m_users.end(); ++cit)
{
if((tmpPlayer = cit->second->getPlayer()) && (!tmpPlayer->isGhost() || player->getAccess() >= tmpPlayer->getAccess()))
tmpPlayer->sendChannelEvent(m_id, player->getName(), CHANNELEVENT_JOIN);
}
}
m_users[player->getID()] = player;
CreatureEventList joinEvents = player->getCreatureEvents(CREATURE_EVENT_CHANNEL_JOIN);
for(CreatureEventList::iterator it = joinEvents.begin(); it != joinEvents.end(); ++it)
(*it)->executeChannel(player, m_id, m_users);
Manager::getInstance()->addUser(player->getID(), m_id);
return true;
}
bool ChatChannel::removeUser(Player* player, bool exclude/* = false*/)
{
if(!player)
return false;
UsersMap::iterator it = m_users.find(player->getID());
if(it == m_users.end())
return true;
m_users.erase(it);
CreatureEventList leaveEvents = player->getCreatureEvents(CREATURE_EVENT_CHANNEL_LEAVE);
for(CreatureEventList::iterator it = leaveEvents.begin(); it != leaveEvents.end(); ++it)
(*it)->executeChannel(player, m_id, m_users);
if((m_id == CHANNEL_PARTY || m_id == CHANNEL_GUILD || m_id == CHANNEL_PRIVATE) && !exclude)
{
Player* tmpPlayer = NULL;
for(UsersMap::iterator cit = m_users.begin(); cit != m_users.end(); ++cit)
{
if((tmpPlayer = cit->second->getPlayer()) && (!tmpPlayer->isGhost() || player->getAccess() >= tmpPlayer->getAccess()))
tmpPlayer->sendChannelEvent(m_id, player->getName(), CHANNELEVENT_LEAVE);
}
}
Manager::getInstance()->removeUser(player->getID(), m_id);
return true;
}
bool ChatChannel::talk(Player* player, MessageClasses type, const std::string& text, uint32_t statementId)
{
UsersMap::iterator it = m_users.find(player->getID());
if(it == m_users.end())
return false;
if(m_condition && !player->hasFlag(PlayerFlag_CannotBeMuted))
{
if(Condition* condition = m_condition->clone())
player->addCondition(condition);
}
for(it = m_users.begin(); it != m_users.end(); ++it)
it->second->sendCreatureChannelSay(player, type, text, m_id, statementId);
if(hasFlag(CHANNELFLAG_LOGGED) && m_file->is_open())
*m_file << "[" << formatDate() << "] " << player->getName() << ": " << text << std::endl;
return true;
}
bool ChatChannel::talk(std::string nick, MessageClasses type, const std::string& text)
{
for(UsersMap::iterator it = m_users.begin(); it != m_users.end(); ++it)
it->second->sendChannelMessage(nick, text, type, m_id);
if(hasFlag(CHANNELFLAG_LOGGED) && m_file->is_open())
*m_file << "[" << formatDate() << "] " << nick << ": " << text << std::endl;
return true;
}
Chat::~Chat()
{
for(GuildChannelMap::iterator it = m_guildChannels.begin(); it != m_guildChannels.end(); ++it)
delete it->second;
m_guildChannels.clear();
clear();
}
void Chat::clear()
{
for(NormalChannelMap::iterator it = m_normalChannels.begin(); it != m_normalChannels.end(); ++it)
delete it->second;
m_normalChannels.clear();
for(PartyChannelMap::iterator it = m_partyChannels.begin(); it != m_partyChannels.end(); ++it)
delete it->second;
m_partyChannels.clear();
for(PrivateChannelMap::iterator it = m_privateChannels.begin(); it != m_privateChannels.end(); ++it)
delete it->second;
m_privateChannels.clear();
delete dummyPrivate;
}
bool Chat::reload()
{
clear();
return loadFromXml();
}
bool Chat::loadFromXml()
{
xmlDocPtr doc = xmlParseFile(getFilePath(FILE_TYPE_XML, "channels.xml").c_str());
if(!doc)
{
std::clog << "[Warning - Chat::loadFromXml] Cannot load channels file." << std::endl;
std::clog << getLastXMLError() << std::endl;
return false;
}
xmlNodePtr root = xmlDocGetRootElement(doc);
if(xmlStrcmp(root->name,(const xmlChar*)"channels"))
{
std::clog << "[Error - Chat::loadFromXml] Malformed channels file" << std::endl;
xmlFreeDoc(doc);
return false;
}
for(xmlNodePtr p = root->children; p; p = p->next)
parseChannelNode(p);
xmlFreeDoc(doc);
return true;
}
bool Chat::parseChannelNode(xmlNodePtr p)
{
int32_t intValue;
if(xmlStrcmp(p->name, (const xmlChar*)"channel"))
return false;
if(!readXMLInteger(p, "id", intValue) || intValue <= CHANNEL_GUILD)
{
std::clog << "[Warning - Chat::loadFromXml] Invalid or not specified channel id." << std::endl;
return false;
}
uint16_t id = intValue;
std::string strValue;
if(m_normalChannels.find(id) != m_normalChannels.end() && (!readXMLString(p, "override", strValue) || !booleanString(strValue)))
{
std::clog << "[Warning - Chat::loadFromXml] Duplicated channel with id: " << id << "." << std::endl;
return false;
}
if(!readXMLString(p, "name", strValue))
{
std::clog << "[Warning - Chat::loadFromXml] Missing name for channel with id: " << id << "." << std::endl;
return false;
}
std::string name = strValue;
uint16_t flags = ChatChannel::staticFlags;
if(readXMLString(p, "enabled", strValue) && !booleanString(strValue))
flags &= ~CHANNELFLAG_ENABLED;
if(readXMLString(p, "active", strValue) && !booleanString(strValue))
flags &= ~CHANNELFLAG_ACTIVE;
if((readXMLString(p, "logged", strValue) || readXMLString(p, "log", strValue)) && booleanString(strValue))
flags |= CHANNELFLAG_LOGGED;
uint32_t access = 0;
if(readXMLInteger(p, "access", intValue))
access = intValue;
uint32_t level = 1;
if(readXMLInteger(p, "level", intValue))
level = intValue;
int32_t conditionId = -1;
std::string conditionMessage = "You are muted.";
Condition* condition = NULL;
if(readXMLInteger(p, "muted", intValue))
{
conditionId = 3;
int32_t tmp = intValue * 1000;
if(readXMLInteger(p, "conditionId", intValue))
{
conditionId = intValue;
if(conditionId < 3)
std::clog << "[Warning - Chat::parseChannelNode] Using reserved muted condition sub id (" << conditionId << ")" << std::endl;
}
if(readXMLString(p, "conditionMessage", strValue))
conditionMessage = strValue;
if(tmp && !(condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_MUTED, tmp, 0, false, conditionId)))
conditionId = -1;
}
StringVec vocStringVec;
VocationMap vocMap;
std::string error;
for(xmlNodePtr tmpNode = p->children; tmpNode; tmpNode = tmpNode->next)
{
if(!parseVocationNode(tmpNode, vocMap, vocStringVec, error))
std::clog << "[Warning - Chat::loadFromXml] " << error << std::endl;
}
VocationMap* vocationMap = NULL;
if(!vocMap.empty())
vocationMap = new VocationMap(vocMap);
switch(id)
{
case CHANNEL_PARTY:
{
partyName = name;
break;
}
case CHANNEL_PRIVATE:
{
if(ChatChannel* newChannel = new PrivateChatChannel(CHANNEL_PRIVATE, name, flags))
dummyPrivate = newChannel;
break;
}
default:
{
if(ChatChannel* newChannel = new ChatChannel(id, name, flags, access, level,
condition, conditionId, conditionMessage, vocationMap))
m_normalChannels[id] = newChannel;
break;
}
}
return true;
}
ChatChannel* Chat::createChannel(Player* player, uint16_t channelId)
{
if(!player || player->isRemoved() || getChannel(player, channelId))
return NULL;
switch(channelId)
{
case CHANNEL_GUILD:
{
ChatChannel* newChannel = NULL;
if((newChannel = new ChatChannel(channelId, player->getGuildName(), ChatChannel::staticFlags)))
m_guildChannels[player->getGuildId()] = newChannel;
return newChannel;
}
case CHANNEL_PARTY:
{
ChatChannel* newChannel = NULL;
if(player->getParty() && (newChannel = new ChatChannel(channelId, partyName, ChatChannel::staticFlags)))
m_partyChannels[player->getParty()] = newChannel;
return newChannel;
}
case CHANNEL_PRIVATE:
{
//only 1 private channel for each premium player
if(!player->isPremium() || getPrivateChannel(player))
return NULL;
//find a free private channel slot
for(uint16_t i = 100; i < 10000; ++i)
{
if(m_privateChannels.find(i) != m_privateChannels.end())
continue;
uint16_t flags = 0;
if(dummyPrivate)
flags = dummyPrivate->getFlags();
PrivateChatChannel* newChannel = NULL;
if((newChannel = new PrivateChatChannel(i, player->getName() + "'s Channel", flags)))
{
newChannel->setOwner(player->getGUID());
m_privateChannels[i] = newChannel;
}
return newChannel;
}
}
default:
break;
}
return NULL;
}
bool Chat::deleteChannel(Player* player, uint16_t channelId)
{
switch(channelId)
{
case CHANNEL_GUILD:
{
GuildChannelMap::iterator it = m_guildChannels.find(player->getGuildId());
if(it == m_guildChannels.end())
return false;
delete it->second;
m_guildChannels.erase(it);
return true;
}
case CHANNEL_PARTY:
{
PartyChannelMap::iterator it = m_partyChannels.find(player->getParty());
if(it == m_partyChannels.end())
return false;
delete it->second;
m_partyChannels.erase(it);
return true;
}
default:
{
PrivateChannelMap::iterator it = m_privateChannels.find(channelId);
if(it == m_privateChannels.end())
return false;
it->second->closeChannel();
delete it->second;
m_privateChannels.erase(it);
return true;
}
}
return false;
}
ChatChannel* Chat::addUserToChannel(Player* player, uint16_t channelId)
{
ChatChannel* channel = getChannel(player, channelId);
if(channel && channel->addUser(player))
return channel;
return NULL;
}
void Chat::reOpenChannels(Player* player)
{
if(!player || player->isRemoved())
return;
for(NormalChannelMap::iterator it = m_normalChannels.begin(); it != m_normalChannels.end(); ++it)
{
if(it->second->hasUser(player))
player->sendChannel(it->second->getId(), it->second->getName());
}
for(PartyChannelMap::iterator it = m_partyChannels.begin(); it != m_partyChannels.end(); ++it)
{
if(it->second->hasUser(player))
player->sendChannel(it->second->getId(), it->second->getName());
}
for(GuildChannelMap::iterator it = m_guildChannels.begin(); it != m_guildChannels.end(); ++it)
{
if(it->second->hasUser(player))
player->sendChannel(it->second->getId(), it->second->getName());
}
for(PrivateChannelMap::iterator it = m_privateChannels.begin(); it != m_privateChannels.end(); ++it)
{
if(it->second->hasUser(player))
player->sendChannel(it->second->getId(), it->second->getName());
}
}
bool Chat::removeUserFromChannel(Player* player, uint16_t channelId)
{
ChatChannel* channel = getChannel(player, channelId);
if(!channel || !channel->removeUser(player))
return false;
if(channel->getOwner() == player->getGUID())
deleteChannel(player, channelId);
return true;
}
void Chat::removeUserFromChannels(Player* player)
{
if(!player || player->isRemoved())
return;
for(NormalChannelMap::iterator it = m_normalChannels.begin(); it != m_normalChannels.end(); ++it)
it->second->removeUser(player);
for(PartyChannelMap::iterator it = m_partyChannels.begin(); it != m_partyChannels.end(); ++it)
it->second->removeUser(player);
for(GuildChannelMap::iterator it = m_guildChannels.begin(); it != m_guildChannels.end(); ++it)
it->second->removeUser(player);
for(PrivateChannelMap::iterator it = m_privateChannels.begin(); it != m_privateChannels.end(); ++it)
{
it->second->removeUser(player);
if(it->second->getOwner() == player->getGUID())
deleteChannel(player, it->second->getId());
}
}
bool Chat::talk(Player* player, MessageClasses type, const std::string& text, uint16_t channelId,
uint32_t statementId, bool anonymous/* = false*/)
{
if(text.empty())
return false;
ChatChannel* channel = getChannel(player, channelId);
if(!channel)
return false;
if(!player->hasFlag(PlayerFlag_CannotBeMuted))
{
if(!channel->hasFlag(CHANNELFLAG_ACTIVE))
{
player->sendTextMessage(MSG_STATUS_SMALL, "You may not speak into this channel.");
return true;
}
if(player->getLevel() < channel->getLevel())
{
char buffer[100];
sprintf(buffer, "You may not speak into this channel as long as you are on level %d.", channel->getLevel());
player->sendCancel(buffer);
return true;
}
if(channel->getConditionId() >= 0 && player->hasCondition(CONDITION_MUTED, channel->getConditionId()))
{
player->sendCancel(channel->getConditionMessage().c_str());
return true;
}
}
if(isPublicChannel(channelId))
Manager::getInstance()->talk(player->getID(), channelId, type, text);
if(channelId != CHANNEL_GUILD || !g_config.getBool(ConfigManager::INGAME_GUILD_MANAGEMENT)
|| (text[0] != '!' && text[0] != '/'))
{
if(channelId == CHANNEL_GUILD)
{
switch(player->getGuildLevel())
{
case GUILDLEVEL_VICE:
return channel->talk(player, MSG_CHANNEL_HIGHLIGHT, text, statementId);
case GUILDLEVEL_LEADER:
return channel->talk(player, MSG_GAMEMASTER_CHANNEL, text, statementId);
default:
break;
}
}
if(anonymous)
return channel->talk("", type, text);
return channel->talk(player, type, text, statementId);
}
/* TODO: move me to talkactions, please! */
if(!player->getGuildId())
{
player->sendCancel("You are not in a guild.");
return true;
}
if(!IOGuild::getInstance()->guildExists(player->getGuildId()))
{
player->sendCancel("It seems like your guild does not exist anymore.");
return true;
}
char buffer[350];
if(text.substr(1) == "disband")
{
if(player->getGuildLevel() == GUILDLEVEL_LEADER)
{
IOGuild::getInstance()->disbandGuild(player->getGuildId());
channel->talk("", MSG_GAMEMASTER_CHANNEL, "The guild has been disbanded.");
}
else
player->sendCancel("You are not the leader of your guild.");
}
else if(text.substr(1, 6) == "invite")
{
if(player->getGuildLevel() > GUILDLEVEL_MEMBER)
{
if(text.length() > 7)
{
std::string param = text.substr(8);
trimString(param);
Player* paramPlayer = NULL;
if(g_game.getPlayerByNameWildcard(param, paramPlayer) == RET_NOERROR)
{
if(paramPlayer->getGuildId() == 0)
{
if(!paramPlayer->isGuildInvited(player->getGuildId()))
{
sprintf(buffer, "%s has invited you to join the guild, %s. You may join this guild by writing: !joinguild %s", player->getName().c_str(), player->getGuildName().c_str(), player->getGuildName().c_str());
paramPlayer->sendTextMessage(MSG_EVENT_GUILD, buffer);
sprintf(buffer, "%s has invited %s to the guild.", player->getName().c_str(), paramPlayer->getName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
paramPlayer->invitationsList.push_back(player->getGuildId());
}
else
player->sendCancel("A player with that name has already been invited to your guild.");
}
else
player->sendCancel("A player with that name is already in a guild.");
}
else if(IOLoginData::getInstance()->playerExists(param))
{
uint32_t guid;
IOLoginData::getInstance()->getGuidByName(guid, param);
if(!IOGuild::getInstance()->hasGuild(guid))
{
if(!IOGuild::getInstance()->isInvited(player->getGuildId(), guid))
{
if(IOGuild::getInstance()->guildExists(player->getGuildId()))
{
IOGuild::getInstance()->invitePlayer(player->getGuildId(), guid);
sprintf(buffer, "%s has invited %s to the guild.", player->getName().c_str(), param.c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
}
else
player->sendCancel("Your guild does not exist anymore.");
}
else
player->sendCancel("A player with that name has already been invited to your guild.");
}
else
player->sendCancel("A player with that name is already in a guild.");
}
else
player->sendCancel("A player with that name does not exist.");
}
else
player->sendCancel("Invalid guildcommand parameters.");
}
else
player->sendCancel("You don't have rights to invite players to your guild.");
}
else if(text.substr(1, 5) == "leave")
{
if(player->getGuildLevel() < GUILDLEVEL_LEADER)
{
if(!player->hasEnemy())
{
sprintf(buffer, "%s has left the guild.", player->getName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
player->leaveGuild();
}
else
player->sendCancel("Your guild is currently at war, you cannot leave it right now.");
}
else
player->sendCancel("You cannot leave your guild because you are the leader of it, you have to pass the leadership to another member of your guild or disband the guild.");
}
else if(text.substr(1, 6) == "revoke")
{
if(player->getGuildLevel() > GUILDLEVEL_MEMBER)
{
if(text.length() > 7)
{
std::string param = text.substr(8);
trimString(param);
Player* paramPlayer = NULL;
if(g_game.getPlayerByNameWildcard(param, paramPlayer) == RET_NOERROR)
{
if(paramPlayer->getGuildId() == 0)
{
InvitationsList::iterator it = std::find(paramPlayer->invitationsList.begin(), paramPlayer->invitationsList.end(), player->getGuildId());
if(it != paramPlayer->invitationsList.end())
{
sprintf(buffer, "%s has revoked your invite to %s guild.", player->getName().c_str(), (player->getSex(false) ? "his" : "her"));
paramPlayer->sendTextMessage(MSG_EVENT_GUILD, buffer);
sprintf(buffer, "%s has revoked the guildinvite of %s.", player->getName().c_str(), paramPlayer->getName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
paramPlayer->invitationsList.erase(it);
return true;
}
else
player->sendCancel("A player with that name is not invited to your guild.");
}
else
player->sendCancel("A player with that name is already in a guild.");
}
else if(IOLoginData::getInstance()->playerExists(param))
{
uint32_t guid;
IOLoginData::getInstance()->getGuidByName(guid, param);
if(IOGuild::getInstance()->isInvited(player->getGuildId(), guid))
{
if(IOGuild::getInstance()->guildExists(player->getGuildId()))
{
sprintf(buffer, "%s has revoked the guildinvite of %s.", player->getName().c_str(), param.c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
IOGuild::getInstance()->revokeInvite(player->getGuildId(), guid);
}
else
player->sendCancel("It seems like your guild does not exist anymore.");
}
else
player->sendCancel("A player with that name is not invited to your guild.");
}
else
player->sendCancel("A player with that name does not exist.");
}
else
player->sendCancel("Invalid guildcommand parameters.");
}
else
player->sendCancel("You don't have rights to revoke an invite of someone in your guild.");
}
else if(text.substr(1, 7) == "promote" || text.substr(1, 6) == "demote" || text.substr(1, 14) == "passleadership" || text.substr(1, 4) == "kick")
{
if(player->getGuildLevel() == GUILDLEVEL_LEADER)
{
std::string param;
uint32_t length = 0;
if(text[2] == 'r')
length = 9;
else if(text[2] == 'e')
length = 7;
else if(text[2] == 'a')
length = 16;
else
length = 6;
if(text.length() < length)
{
player->sendCancel("Invalid guildcommand parameters.");
return true;
}
param = text.substr(length);
trimString(param);
Player* paramPlayer = NULL;
if(g_game.getPlayerByNameWildcard(param, paramPlayer) == RET_NOERROR)
{
if(paramPlayer->getGuildId())
{
if(IOGuild::getInstance()->guildExists(paramPlayer->getGuildId()))
{
if(player->getGuildId() == paramPlayer->getGuildId())
{
if(text[2] == 'r')
{
if(paramPlayer->getGuildLevel() == GUILDLEVEL_MEMBER)
{
if(paramPlayer->isPremium())
{
paramPlayer->setGuildLevel(GUILDLEVEL_VICE);
sprintf(buffer, "%s has promoted %s to %s.", player->getName().c_str(), paramPlayer->getName().c_str(), paramPlayer->getRankName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
}
else
player->sendCancel("A player with that name does not have a premium account.");
}
else
player->sendCancel("You can only promote Members to Vice-Leaders.");
}
else if(text[2] == 'e')
{
if(paramPlayer->getGuildLevel() == GUILDLEVEL_VICE)
{
paramPlayer->setGuildLevel(GUILDLEVEL_MEMBER);
sprintf(buffer, "%s has demoted %s to %s.", player->getName().c_str(), paramPlayer->getName().c_str(), paramPlayer->getRankName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
}
else
player->sendCancel("You can only demote Vice-Leaders to Members.");
}
else if(text[2] == 'a')
{
if(paramPlayer->getGuildLevel() == GUILDLEVEL_VICE)
{
const uint32_t levelToFormGuild = g_config.getNumber(ConfigManager::LEVEL_TO_FORM_GUILD);
if(paramPlayer->getLevel() >= levelToFormGuild)
{
paramPlayer->setGuildLevel(GUILDLEVEL_LEADER);
player->setGuildLevel(GUILDLEVEL_VICE);
IOGuild::getInstance()->updateOwnerId(paramPlayer->getGuildId(), paramPlayer->getGUID());
sprintf(buffer, "%s has passed the guild leadership to %s.", player->getName().c_str(), paramPlayer->getName().c_str());
channel->talk("", MSG_GAMEMASTER_CHANNEL, buffer);
}
else
{
sprintf(buffer, "The new guild leader has to be at least Level %d.", levelToFormGuild);
player->sendCancel(buffer);
}
}
else
player->sendCancel("A player with that name is not a Vice-Leader.");
}
else
{
if(player->getGuildLevel() > paramPlayer->getGuildLevel())
{
if(!player->hasEnemy())
{
sprintf(buffer, "%s has been kicked from the guild by %s.", paramPlayer->getName().c_str(), player->getName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
paramPlayer->leaveGuild();
}
else
player->sendCancel("Your guild is currently at war, you cannot kick right now.");
}
else
player->sendCancel("You may only kick players with a guild rank below your.");
}
}
else
player->sendCancel("You are not in the same guild as a player with that name.");
}
else
player->sendCancel("Could not find the guild of a player with that name.");
}
else
player->sendCancel("A player with that name is not in a guild.");
}
else if(IOLoginData::getInstance()->playerExists(param))
{
uint32_t guid;
IOLoginData::getInstance()->getGuidByName(guid, param);
if(IOGuild::getInstance()->hasGuild(guid))
{
if(player->getGuildId() == IOGuild::getInstance()->getGuildId(guid))
{
if(text[2] == 'r')
{
if(IOGuild::getInstance()->getGuildLevel(guid) == GUILDLEVEL_MEMBER)
{
if(IOLoginData::getInstance()->isPremium(guid))
{
IOGuild::getInstance()->setGuildLevel(guid, GUILDLEVEL_VICE);
sprintf(buffer, "%s has promoted %s to %s.", player->getName().c_str(), param.c_str(), IOGuild::getInstance()->getRank(guid).c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
}
else
player->sendCancel("A player with that name does not have a premium account.");
}
else
player->sendCancel("You can only promote Members to Vice-Leaders.");
}
else if(text[2] == 'e')
{
if(IOGuild::getInstance()->getGuildLevel(guid) == GUILDLEVEL_VICE)
{
IOGuild::getInstance()->setGuildLevel(guid, GUILDLEVEL_MEMBER);
sprintf(buffer, "%s has demoted %s to %s.", player->getName().c_str(), param.c_str(), IOGuild::getInstance()->getRank(guid).c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
}
else
player->sendCancel("You can only demote Vice-Leaders to Members.");
}
else if(text[2] == 'a')
{
if(IOGuild::getInstance()->getGuildLevel(guid) == GUILDLEVEL_VICE)
{
const uint32_t levelToFormGuild = g_config.getNumber(ConfigManager::LEVEL_TO_FORM_GUILD);
if(IOLoginData::getInstance()->getLevel(guid) >= levelToFormGuild)
{
IOGuild::getInstance()->setGuildLevel(guid, GUILDLEVEL_LEADER);
player->setGuildLevel(GUILDLEVEL_VICE);
sprintf(buffer, "%s has passed the guild leadership to %s.", player->getName().c_str(), param.c_str());
channel->talk("", MSG_GAMEMASTER_CHANNEL, buffer);
}
else
{
sprintf(buffer, "The new guild leader has to be at least Level %d.", levelToFormGuild);
player->sendCancel(buffer);
}
}
else
player->sendCancel("A player with that name is not a Vice-Leader.");
}
else
{
sprintf(buffer, "%s has been kicked from the guild by %s.", param.c_str(), player->getName().c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
IOLoginData::getInstance()->resetGuildInformation(guid);
}
}
}
else
player->sendCancel("A player with that name is not in a guild.");
}
else
player->sendCancel("A player with that name does not exist.");
}
else
player->sendCancel("You are not the leader of your guild.");
}
else if(text.substr(1, 4) == "nick" && text.length() > 5)
{
StringVec params = explodeString(text.substr(6), ",");
if(params.size() >= 2)
{
std::string param1 = params[0], param2 = params[1];
trimString(param1);
trimString(param2);
Player* paramPlayer = NULL;
if(g_game.getPlayerByNameWildcard(param1, paramPlayer) == RET_NOERROR)
{
if(paramPlayer->getGuildId())
{
if(param2.length() > 2)
{
if(param2.length() < 21)
{
if(isValidName(param2, false))
{
if(IOGuild::getInstance()->guildExists(paramPlayer->getGuildId()))
{
if(player->getGuildId() == paramPlayer->getGuildId())
{
if(paramPlayer->getGuildLevel() < player->getGuildLevel() || (player == paramPlayer && player->getGuildLevel() > GUILDLEVEL_MEMBER))
{
paramPlayer->setGuildNick(param2);
if(player != paramPlayer)
sprintf(buffer, "%s has set the guildnick of %s to \"%s\".", player->getName().c_str(), paramPlayer->getName().c_str(), param2.c_str());
else
sprintf(buffer, "%s has set %s guildnick to \"%s\".", player->getName().c_str(), (player->getSex(false) ? "his" : "her"), param2.c_str());
channel->talk("", MSG_CHANNEL_HIGHLIGHT, buffer);
}
else
player->sendCancel("You may only change the guild nick of players that have a lower rank than you.");