ManaPlus
guildmanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifdef TMWA_SUPPORT
23 #include "net/tmwa/guildmanager.h"
24 
25 #include "actormanager.h"
26 #include "client.h"
27 #include "configuration.h"
28 #include "guild.h"
29 #include "notifymanager.h"
30 
31 #include "being/localplayer.h"
32 
34 
35 #include "gui/windows/chatwindow.h"
37 
39 
40 #include "net/chathandler.h"
41 #include "net/packetlimiter.h"
42 
43 #include "utils/delete2.h"
44 #include "utils/timer.h"
45 
46 #include "debug.h"
47 
49 
50 static const int requestTimeout = 5;
51 
53 
55  mGotInfo(false),
56  mGotName(false),
57  mSentInfoRequest(false),
58  mSentNameRequest(false),
59  mHavePower(false),
60  mTempList(),
61  mTab(nullptr),
62  mRequest(false),
63  mRequestTime(cur_time + 3),
64  mGotOnlineInfo(false)
65 {
66 }
67 
69 {
70  delete2(mTab)
71 }
72 
74 {
75  int val = serverConfig.getValue("enableGuildBot", -1);
76  if (val == -1)
77  {
78  if (Client::isTmw())
79  val = 1;
80  else
81  val = 0;
82  serverConfig.setValue("enableGuildBot", val);
83  }
84  mEnableGuildBot = (val != 0);
85  if (mEnableGuildBot)
86  {
87  if (guildManager == nullptr)
89  else
91  }
92  else if (guildManager != nullptr)
93  {
95  }
96 }
97 
99 {
100  mGotInfo = false;
101  mGotName = false;
102  mHavePower = false;
103  mRequest = false;
104  mSentNameRequest = false;
105  mSentInfoRequest = false;
106  mGotOnlineInfo = false;
107  mRequestTime = 0;
108  mTempList.clear();
109 
110  if (socialWindow != nullptr)
111  {
112  Guild *const guild = Guild::getGuild(1);
113  if ((guild != nullptr) && (socialWindow != nullptr))
115  }
116  delete2(mTab)
117 }
118 
119 void GuildManager::send(const std::string &msg)
120 {
121  chatHandler->privateMessage("guild", msg);
122 }
123 
124 void GuildManager::chat(const std::string &msg)
125 {
126  if ((localPlayer == nullptr) || (mTab == nullptr))
127  return;
128 
129  chatHandler->privateMessage("guild", msg);
131 }
132 
134 {
135  const Guild *const guild = createGuild();
136  if (guild != nullptr)
137  guild->getNames(names);
138 }
139 
141 {
142  if (mRequest && mRequestTime + 15 < cur_time)
143  return;
144 
145  if (!mGotName && !mSentNameRequest)
146  {
148  return;
149  send("!info " + toString(tick_time));
150  mRequest = true;
151  mSentNameRequest = true;
153  }
154  else if (!mGotInfo && !mSentInfoRequest && !mSentNameRequest)
155  {
157  return;
158  send("!getonlineinfo " + toString(tick_time));
159  mRequest = true;
160  mSentInfoRequest = true;
162  }
163 }
164 
166 {
168  {
170  return;
171  send("!getonlineinfo " + toString(tick_time));
172  mRequest = true;
173  mSentInfoRequest = true;
175  }
176 }
177 
179 {
180  Guild *const guild = Guild::getGuild(1);
181  if (guild != nullptr)
182  {
183  guild->setServerGuild(false);
184  StringVectCIter it = mTempList.begin();
185  const StringVectCIter it_end = mTempList.end();
186  int i = 0;
187  while (it != it_end)
188  {
189  std::string name = *it;
190  const size_t sz = name.size();
191  if (sz > 1)
192  {
193  const int status = atoi(name.substr(sz - 1).c_str());
194 
195  name = name.substr(0, sz - 1);
196  GuildMember *const m = guild->addMember(
197  fromInt(i, BeingId), 0, name);
198  if (m != nullptr)
199  {
200  m->setOnline((status & 1) != 0);
202  if ((status & 2) != 0)
203  m->setPos(10);
204  else
205  m->setPos(0);
206  if (localPlayer != nullptr &&
207  name == localPlayer->getName())
208  {
209  mHavePower = ((status & 2) != 0);
210  m->setOnline(true);
211  }
212  }
213  }
214  ++ it;
215  i ++;
216  }
217  guild->sort();
218  createTab(guild);
219  if (actorManager != nullptr)
220  {
223  }
224  if (socialWindow != nullptr)
226  }
227  mTempList.clear();
228  mSentInfoRequest = false;
229  mGotInfo = true;
230 }
231 
233 {
234  if (mTab == nullptr)
235  {
237  if (config.getBoolValue("showChatHistory"))
238  mTab->loadFromLogFile("#Guild");
239  if (localPlayer != nullptr)
241  }
242 }
243 
245 {
246  Guild *const guild = Guild::getGuild(1);
247  if (guild == nullptr)
248  return nullptr;
249 
250  guild->setServerGuild(false);
251  return guild;
252 }
253 
254 bool GuildManager::processGuildMessage(const std::string &msg)
255 {
256  const bool res = process(msg);
257 
258  if (!mRequest)
260 
261  return res;
262 }
263 
264 bool GuildManager::process(std::string msg)
265 {
266  if (msg.size() > 4 && msg[0] == '#' && msg[1] == '#')
267  msg = msg.substr(3);
268 
269  const bool haveNick = (msg.find(": ") != std::string::npos);
270 
271  if (!haveNick && findCutLast(msg, " is now Offline."))
272  {
273  Guild *const guild = createGuild();
274  if (guild == nullptr)
275  return false;
276  if (msg.size() < 4)
277  return false;
278  if (msg[0] == '#' && msg[1] == '#')
279  msg = msg.substr(3);
280 
281  GuildMember *const m = guild->addMember(msg);
282  if (m != nullptr)
283  m->setOnline(false);
284  guild->sort();
285  mRequest = false;
286  if (mTab != nullptr)
288  if (socialWindow != nullptr)
290  return true;
291  }
292  else if (!haveNick && findCutLast(msg, " is now Online."))
293  {
294  Guild *const guild = createGuild();
295  if (guild == nullptr)
296  return false;
297  if (msg.size() < 4)
298  return false;
299  if (msg[0] == '#' && msg[1] == '#')
300  msg = msg.substr(3);
301  GuildMember *const m = guild->addMember(msg);
302  if (m != nullptr)
303  m->setOnline(true);
304  guild->sort();
305  mRequest = false;
306  if (mTab != nullptr)
308  if (socialWindow != nullptr)
310  return true;
311  }
312  else if (findCutFirst(msg, "Welcome to the "))
313  {
314  Guild *const guild = createGuild();
315  if (guild == nullptr)
316  return false;
317 // logger->log("welcome message: %s", msg.c_str());
318  const size_t pos = msg.find("! (");
319  if (pos == std::string::npos)
320  return false;
321  msg = msg.substr(0, pos);
322  guild->setName(msg);
323  if (localPlayer != nullptr)
325  mGotName = true;
326  mSentNameRequest = false;
327  mRequest = false;
328  return true;
329  }
330  else if (findCutFirst(msg, "Player name: "))
331  {
332  Guild *const guild = createGuild();
333  if (guild == nullptr)
334  return false;
335  size_t pos = msg.find("Access Level: ");
336  if (pos == std::string::npos)
337  return false;
338 
339  msg = msg.substr(pos);
340  if (!findCutFirst(msg, "Access Level: "))
341  return false;
342 
343  pos = msg.find(", Guild:");
344  if (pos == std::string::npos)
345  return false;
346 
347  const int level = atoi(msg.substr(0, pos).c_str());
348  if (level >= 10)
349  mHavePower = true;
350  else
351  mHavePower = false;
352 
353  msg = msg.substr(pos + strlen(", Guild:"));
354  pos = msg.find(", No. Of Online Players: ");
355  if (pos == std::string::npos)
356  return false;
357 
358  msg = msg.substr(0, pos);
359 // logger->log("guild name: %s", msg.c_str());
360 
361  guild->setName(msg);
362  if (localPlayer != nullptr)
364  mGotName = true;
365  mSentNameRequest = false;
366  mRequest = false;
367  return true;
368  }
369  else if (findCutFirst(msg, "OL#"))
370  {
371 // logger->log("OL");
372  mTempList.clear();
374  if (msg.empty() || msg[msg.size() - 1] != '#')
375  updateList();
376  mRequest = false;
377  mGotOnlineInfo = true;
378  return true;
379  }
380  else if (findCutFirst(msg, "oL#"))
381  {
382 // logger->log("oL");
384  if (msg.empty() || msg[msg.size() - 1] != '#')
385  updateList();
386  mRequest = false;
387  mGotOnlineInfo = true;
388  return true;
389  }
390  else if (msg == "You are currently not in a guild. For more information "
391  "or to discuss the possibility of adding you own guild "
392  "please contact Jero.")
393  {
394  mRequest = true;
395  return true;
396  }
397  else if (findCutFirst(msg, "You have been invited to the ")
398  && findCutLast(msg, " guild chat. If you would like to accept "
399  "this invitation please reply \"yes\" and if not then \"no\" ."))
400  {
401  if (socialWindow != nullptr)
403  return true;
404  }
405  else if (!haveNick && (findCutLast(msg, " has been removed "
406  "from the Guild.") || findCutLast(msg, " has left the Guild.")))
407  {
408  Guild *const guild = createGuild();
409  if (guild == nullptr)
410  return false;
411  if (msg.size() < 4)
412  return false;
413  if (msg[0] == '#' && msg[1] == '#')
414  msg = msg.substr(3);
415 
416  if (actorManager != nullptr)
417  {
418  Being *const b = actorManager->findBeingByName(
420 
421  if (b != nullptr)
422  {
423  b->clearGuilds();
424  b->setGuildName("");
425  b->updateColors();
426  }
427  }
428 
429  guild->removeMember(msg);
430  return true;
431  }
432  else if (msg == "You have been removed from the Guild" ||
433  msg == "You have left the Guild")
434  {
435  return afterRemove();
436  }
437  Guild *const guild = createGuild();
438  if (guild == nullptr)
439  return false;
440  if (mTab == nullptr)
441  {
442  createTab(guild);
443  }
444  if (mTab != nullptr)
445  {
446  const size_t pos = msg.find(": ", 0);
447  if (pos != std::string::npos)
448  {
449  const std::string sender_name = msg.substr(0, pos);
450  if (guild->getMember(sender_name) == nullptr)
451  {
452  mTab->chatLog(msg,
456  return true;
457  }
458 
459  msg.erase(0, pos + 2);
460  if (msg.size() > 3 && msg[0] == '#' && msg[1] == '#')
461  msg.erase(0, 3);
462 
463  trim(msg);
464  mTab->chatLog(sender_name, msg);
465  }
466  else
467  {
468  mTab->chatLog(msg,
472  }
473  return true;
474  }
475  return false;
476 }
477 
478 void GuildManager::kick(const std::string &msg)
479 {
480  send("!remove " + msg);
481 }
482 
483 void GuildManager::invite(const std::string &msg)
484 {
485  send("!invite " + msg);
486 }
487 
489 {
490  send("!leave");
491 }
492 
493 void GuildManager::notice(const std::string &msg)
494 {
495  if (msg.empty())
496  send("!removemotd");
497  else
498  send("!setmotd " + msg);
499 }
500 
502 {
503  if (socialWindow != nullptr)
504  {
505  Guild *const guild = Guild::getGuild(1);
506  if (guild != nullptr)
508  }
509 }
510 
511 void GuildManager::inviteResponse(const bool response)
512 {
513  if (response)
514  send("yes");
515  else
516  send("no");
517 }
518 
520 {
521  Guild *const guild = createGuild();
522  if (guild == nullptr)
523  return false;
524  guild->removeFromMembers();
525  guild->clearMembers();
526  if (localPlayer != nullptr)
527  {
530  }
532  delete2(mTab)
533 
534  if (socialWindow != nullptr)
536  if (actorManager != nullptr)
537  {
540  }
541  reload();
542  return true;
543 }
544 
546 {
547  return mTab;
548 }
549 
550 #endif // TMWA_SUPPORT
ActorManager * actorManager
volatile time_t cur_time
Definition: timer.cpp:58
int BeingId
Definition: beingid.h:30
Net::ChatHandler * chatHandler
Definition: net.cpp:86
ChatWindow * chatWindow
Definition: chatwindow.cpp:94
void updatePlayerColors() const
Being * findBeingByName(const std::string &name, const ActorTypeT type) const
void updatePlayerGuild() const
void setOnline(const bool online)
Definition: avatar.h:87
void setGender(const GenderT g)
Definition: avatar.h:165
Definition: being.h:96
void setGuildName(const std::string &name)
Definition: being.cpp:1196
void addGuild(Guild *const guild)
Definition: being.cpp:1237
void updateColors()
Definition: being.cpp:2663
const std::string & getName() const
Definition: being.h:232
void clearGuilds()
Definition: being.cpp:1289
void chatLog(std::string line, ChatMsgTypeT own, const IgnoreRecord ignoreRecord, const TryRemoveColors tryRemoveColors)
Definition: chattab.cpp:111
void showOnline(const std::string &nick, const Online online)
Definition: chattab.cpp:547
void loadFromLogFile(const std::string &name)
Definition: chattab.cpp:510
static bool isTmw()
Definition: client.cpp:838
std::string getValue(const std::string &key, const std::string &deflt) const
bool getBoolValue(const std::string &key) const
void setValue(const std::string &key, const std::string &value)
static void clear()
StringVect mTempList
Definition: guildmanager.h:97
bool process(std::string msg)
static void init()
bool mGotOnlineInfo
Definition: guildmanager.h:101
ChatTab * getTab()
static bool mEnableGuildBot
Definition: guildmanager.h:91
static void inviteResponse(const bool response)
void createTab(Guild *const guild)
bool mSentNameRequest
Definition: guildmanager.h:95
bool mSentInfoRequest
Definition: guildmanager.h:94
static void notice(const std::string &msg)
void chat(const std::string &msg)
static void leave()
static void send(const std::string &msg)
bool afterRemove()
bool processGuildMessage(const std::string &msg)
static void kick(const std::string &msg)
static void invite(const std::string &msg)
static void getNames(StringVect &names)
time_t mRequestTime
Definition: guildmanager.h:100
EmulateGuildTab * mTab
Definition: guildmanager.h:98
static Guild * createGuild()
void requestGuildInfo()
void setPos(const int pos)
Definition: guild.h:50
Definition: guild.h:70
static Guild * getGuild(const int16_t id)
Definition: guild.cpp:374
virtual void privateMessage(const std::string &recipient, const std::string &text) const =0
void updateGuildCounter(const int online, const int total)
void showGuildInvite(const std::string &guildName, const int guildId, const std::string &inviterName)
bool removeTab(Guild *const guild)
Configuration config
Configuration serverConfig
#define delete2(var)
Definition: delete2.h:25
GuildManager * guildManager
static const int requestTimeout
const bool IgnoreRecord_false
Definition: ignorerecord.h:30
#define fromInt(val, name)
Definition: intdefines.h:46
#define nullptr
Definition: localconsts.h:45
LocalPlayer * localPlayer
uint32_t guild
volatile int tick_time
Definition: timer.cpp:53
bool msg(InputEvent &event)
Definition: chat.cpp:39
std::string trim(std::string const &str)
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
@ UNSPECIFIED
Definition: gender.h:33
void notify(const unsigned int message)
bool limitPackets(const PacketTypeT type)
@ PACKET_WHISPER
Definition: packettype.h:41
const bool Online_false
Definition: online.h:30
const bool Online_true
Definition: online.h:30
SocialWindow * socialWindow
void splitToStringVector(StringVect &tokens, const std::string &text, const char separator)
bool findCutFirst(std::string &str1, const std::string &str2)
bool findCutLast(std::string &str1, const std::string &str2)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29
const bool TryRemoveColors_true