InspIRCd  3.0
clientprotocolmsg.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2016 Attila Molnar <[email protected]>
5  *
6  * This file is part of InspIRCd. InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #pragma once
21 
22 namespace ClientProtocol
23 {
24  namespace Messages
25  {
26  class Numeric;
27  class Join;
28  struct Part;
29  struct Kick;
30  struct Quit;
31  struct Nick;
32  class Mode;
33  struct Topic;
34  class Privmsg;
35  struct Invite;
36  struct Ping;
37  struct Pong;
38  struct Error;
39  }
40 }
41 
48 {
49  char numericstr[4];
50 
51  void InitCommand(unsigned int number)
52  {
53  snprintf(numericstr, sizeof(numericstr), "%03u", number);
54  SetCommand(numericstr);
55  }
56 
57  void InitFromNumeric(const ::Numeric::Numeric& numeric)
58  {
59  InitCommand(numeric.GetNumeric());
60  for (std::vector<std::string>::const_iterator i = numeric.GetParams().begin(); i != numeric.GetParams().end(); ++i)
61  PushParamRef(*i);
62  }
63 
64  public:
69  Numeric(const ::Numeric::Numeric& num, User* user)
70  : ClientProtocol::Message(NULL, (num.GetServer() ? num.GetServer()->GetName() : ServerInstance->Config->ServerName))
71  {
72  if (user->registered & REG_NICK)
73  PushParamRef(user->nick);
74  else
75  PushParam("*");
76  InitFromNumeric(num);
77  }
78 
83  Numeric(const ::Numeric::Numeric& num, const std::string& target)
84  : ClientProtocol::Message(NULL, (num.GetServer() ? num.GetServer()->GetName() : ServerInstance->Config->ServerName))
85  {
86  PushParamRef(target);
87  InitFromNumeric(num);
88  }
89 
93  Numeric(unsigned int num)
94  : ClientProtocol::Message(NULL, ServerInstance->Config->ServerName)
95  {
96  InitCommand(num);
97  PushParam("*");
98  }
99 };
100 
105 {
106  Membership* memb;
107 
108  public:
112  : ClientProtocol::Message("JOIN")
113  , memb(NULL)
114  {
115  }
116 
121  : ClientProtocol::Message("JOIN", Memb->user)
122  {
123  SetParams(Memb);
124  }
125 
130  Join(Membership* Memb, const std::string& sourcestrref)
131  : ClientProtocol::Message("JOIN", sourcestrref, Memb->user)
132  {
133  SetParams(Memb);
134  }
135 
139  void SetParams(Membership* Memb)
140  {
141  memb = Memb;
142  PushParamRef(memb->chan->name);
143  }
144 
148  Membership* GetMember() const { return memb; }
149 };
150 
155 {
160  Part(Membership* memb, const std::string& reason)
161  : ClientProtocol::Message("PART", memb->user)
162  {
163  PushParamRef(memb->chan->name);
164  if (!reason.empty())
165  PushParamRef(reason);
166  }
167 };
168 
173 {
179  Kick(User* source, Membership* memb, const std::string& reason)
180  : ClientProtocol::Message("KICK", source)
181  {
182  PushParamRef(memb->chan->name);
183  PushParamRef(memb->user->nick);
184  PushParamRef(reason);
185  }
186 };
187 
192 {
197  Quit(User* source, const std::string& reason)
198  : ClientProtocol::Message("QUIT", source)
199  {
200  if (!reason.empty())
201  PushParamRef(reason);
202  }
203 };
204 
209 {
214  Nick(User* source, const std::string& newnick)
215  : ClientProtocol::Message("NICK", source)
216  {
217  PushParamRef(newnick);
218  }
219 };
220 
225 {
226  Channel* chantarget;
227  User* usertarget;
228  Modes::ChangeList::List::const_iterator beginit;
229  Modes::ChangeList::List::const_iterator lastit;
230 
237  static std::string ToModeLetters(const Modes::ChangeList::List& list, std::string::size_type maxlinelen, Modes::ChangeList::List::const_iterator beginit, Modes::ChangeList::List::const_iterator& lastit)
238  {
239  std::string ret;
240  std::string::size_type paramlength = 0;
241  char output_pm = '\0'; // current output state, '+' or '-'
242 
243  Modes::ChangeList::List::const_iterator i;
244  for (i = beginit; i != list.end(); ++i)
245  {
246  const Modes::Change& item = *i;
247 
248  const char needed_pm = (item.adding ? '+' : '-');
249  if (needed_pm != output_pm)
250  {
251  output_pm = needed_pm;
252  ret.push_back(output_pm);
253  }
254 
255  if (!item.param.empty())
256  paramlength += item.param.length() + 1;
257  if (ret.length() + 1 + paramlength > maxlinelen)
258  {
259  // Mode sequence is getting too long
260  const char c = *ret.rbegin();
261  if ((c == '+') || (c == '-'))
262  ret.erase(ret.size()-1);
263  break;
264  }
265 
266  ret.push_back(item.mh->GetModeChar());
267  }
268 
269  lastit = i;
270  return ret;
271  }
272 
275  void PushModeParams()
276  {
277  for (Modes::ChangeList::List::const_iterator i = beginit; i != lastit; ++i)
278  {
279  const Modes::Change& item = *i;
280  if (!item.param.empty())
281  PushParamRef(item.param);
282  }
283  }
284 
285  public:
290  static std::string ToModeLetters(const Modes::ChangeList& changelist)
291  {
292  // TODO: This assumes that std::string::max_size() >= UINT_MAX
293  Modes::ChangeList::List::const_iterator dummy;
294  return ToModeLetters(changelist.getlist(), UINT_MAX, changelist.getlist().begin(), dummy);
295  }
296 
304  Mode(User* source, Channel* Chantarget, User* Usertarget, const Modes::ChangeList& changelist, Modes::ChangeList::List::const_iterator beginiter)
305  : ClientProtocol::Message("MODE", source)
306  , chantarget(Chantarget)
307  , usertarget(Usertarget)
308  , beginit(beginiter)
309  {
311  PushParam(ToModeLetters(changelist.getlist(), 450, beginit, lastit));
312  PushModeParams();
313  }
314 
321  Mode(User* source, Channel* Chantarget, User* Usertarget, const Modes::ChangeList& changelist)
322  : ClientProtocol::Message("MODE", source)
323  , chantarget(Chantarget)
324  , usertarget(Usertarget)
325  , beginit(changelist.getlist().begin())
326  {
328  PushParam(ToModeLetters(changelist.getlist(), 450, beginit, lastit));
329  PushModeParams();
330  }
331 
336  : ClientProtocol::Message("MODE", ServerInstance->FakeClient)
337  , chantarget(NULL)
338  , usertarget(NULL)
339  {
340  }
341 
347  void SetParams(Channel* Chantarget, User* Usertarget, const Modes::ChangeList& changelist)
348  {
349  ClearParams();
350 
351  chantarget = Chantarget;
352  usertarget = Usertarget;
353  beginit = changelist.getlist().begin();
354 
356  PushParam(ToModeLetters(changelist.getlist(), 450, beginit, lastit));
357  PushModeParams();
358  }
359 
363  Modes::ChangeList::List::const_iterator GetBeginIterator() const { return beginit; }
364 
368  Modes::ChangeList::List::const_iterator GetEndIterator() const { return lastit; }
369 
375  const std::string& GetStrTarget() const { return (chantarget ? chantarget->name : usertarget->nick); }
376 
380  User* GetUserTarget() const { return usertarget; }
381 
385  Channel* GetChanTarget() const { return chantarget; }
386 };
387 
391 {
397  Topic(User* source, const Channel* chan, const std::string& newtopic)
398  : ClientProtocol::Message("TOPIC", source)
399  {
400  PushParamRef(chan->name);
401  PushParamRef(newtopic);
402  }
403 };
404 
408 {
409  void PushTargetChan(char status, const Channel* targetchan)
410  {
411  if (status)
412  {
413  std::string rawtarget(1, status);
414  rawtarget.append(targetchan->name);
415  PushParam(rawtarget);
416  }
417  else
418  {
419  PushParamRef(targetchan->name);
420  }
421  }
422 
423  void PushTargetUser(const User* targetuser)
424  {
425  if (targetuser->registered & REG_NICK)
426  PushParamRef(targetuser->nick);
427  else
428  PushParam("*");
429  }
430 
431  public:
434  enum NoCopy { nocopy };
435 
440  static const char* CommandStrFromMsgType(MessageType mt)
441  {
442  return ((mt == MSG_PRIVMSG) ? "PRIVMSG" : "NOTICE");
443  }
444 
451  Privmsg(User* source, const std::string& target, const std::string& text, MessageType mt = MSG_PRIVMSG)
452  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
453  {
454  PushParam(target);
455  PushParam(text);
456  }
457 
465  Privmsg(User* source, const Channel* targetchan, const std::string& text, MessageType mt = MSG_PRIVMSG, char status = 0)
466  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
467  {
468  PushTargetChan(status, targetchan);
469  PushParam(text);
470  }
471 
478  Privmsg(User* source, const User* targetuser, const std::string& text, MessageType mt = MSG_PRIVMSG)
479  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
480  {
481  PushTargetUser(targetuser);
482  PushParam(text);
483  }
484 
491  Privmsg(const std::string& source, const std::string& target, const std::string& text, MessageType mt = MSG_PRIVMSG)
492  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
493  {
494  PushParam(target);
495  PushParam(text);
496  }
497 
505  Privmsg(const std::string& source, const Channel* targetchan, const std::string& text, MessageType mt = MSG_PRIVMSG, char status = 0)
506  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
507  {
508  PushTargetChan(status, targetchan);
509  PushParam(text);
510  }
511 
518  Privmsg(const std::string& source, const User* targetuser, const std::string& text, MessageType mt = MSG_PRIVMSG)
519  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
520  {
521  PushTargetUser(targetuser);
522  PushParam(text);
523  }
524 
531  Privmsg(NoCopy, User* source, const std::string& target, const std::string& text, MessageType mt = MSG_PRIVMSG)
532  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
533  {
534  PushParam(target);
535  PushParamRef(text);
536  }
537 
545  Privmsg(NoCopy, User* source, const Channel* targetchan, const std::string& text, MessageType mt = MSG_PRIVMSG, char status = 0)
546  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
547  {
548  PushTargetChan(status, targetchan);
549  PushParamRef(text);
550  }
551 
558  Privmsg(NoCopy, User* source, const User* targetuser, const std::string& text, MessageType mt = MSG_PRIVMSG)
559  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
560  {
561  PushTargetUser(targetuser);
562  PushParamRef(text);
563  }
564 
571  Privmsg(NoCopy, const std::string& source, const std::string& target, const std::string& text, MessageType mt = MSG_PRIVMSG)
572  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
573  {
574  PushParam(target);
575  PushParamRef(text);
576  }
577 
585  Privmsg(NoCopy, const std::string& source, const Channel* targetchan, const std::string& text, MessageType mt = MSG_PRIVMSG, char status = 0)
586  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
587  {
588  PushTargetChan(status, targetchan);
589  PushParamRef(text);
590  }
591 
598  Privmsg(NoCopy, const std::string& source, const User* targetuser, const std::string& text, MessageType mt = MSG_PRIVMSG)
599  : ClientProtocol::Message(CommandStrFromMsgType(mt), source)
600  {
601  PushTargetUser(targetuser);
602  PushParamRef(text);
603  }
604 };
605 
610 {
616  Invite(User* source, User* target, Channel* chan)
617  : ClientProtocol::Message("INVITE", source)
618  {
619  PushParamRef(target->nick);
620  PushParamRef(chan->name);
621  }
622 };
623 
628 {
633  : ClientProtocol::Message("PING")
634  {
635  PushParamRef(ServerInstance->Config->ServerName);
636  }
637 
641  Ping(const std::string& cookie)
642  : ClientProtocol::Message("PING")
643  {
644  PushParamRef(cookie);
645  }
646 };
647 
652 {
656  Pong(const std::string& cookie)
657  : ClientProtocol::Message("PONG", ServerInstance->Config->ServerName)
658  {
659  PushParamRef(ServerInstance->Config->ServerName);
660  PushParamRef(cookie);
661  }
662 };
663 
668 {
672  Error(const std::string& text)
673  : ClientProtocol::Message("ERROR")
674  {
675  PushParam(text);
676  }
677 };
std::string name
Definition: channels.h:85
Privmsg(NoCopy, User *source, const Channel *targetchan, const std::string &text, MessageType mt=MSG_PRIVMSG, char status=0)
Definition: clientprotocolmsg.h:545
Part(Membership *memb, const std::string &reason)
Definition: clientprotocolmsg.h:160
Join()
Definition: clientprotocolmsg.h:111
unsigned int registered
Definition: users.h:345
Definition: clientprotocolmsg.h:191
ServerConfig * Config
Definition: inspircd.h:280
Privmsg(const std::string &source, const std::string &target, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:491
Invite(User *source, User *target, Channel *chan)
Definition: clientprotocolmsg.h:616
Privmsg(const std::string &source, const User *targetuser, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:518
Ping()
Definition: clientprotocolmsg.h:632
void SetParams(Channel *Chantarget, User *Usertarget, const Modes::ChangeList &changelist)
Definition: clientprotocolmsg.h:347
Numeric(unsigned int num)
Definition: clientprotocolmsg.h:93
Definition: clientprotocolmsg.h:407
static const char * CommandStrFromMsgType(MessageType mt)
Definition: clientprotocolmsg.h:440
Privmsg(User *source, const Channel *targetchan, const std::string &text, MessageType mt=MSG_PRIVMSG, char status=0)
Definition: clientprotocolmsg.h:465
Definition: users.h:224
Membership * GetMember() const
Definition: clientprotocolmsg.h:148
Modes::ChangeList::List::const_iterator GetEndIterator() const
Definition: clientprotocolmsg.h:368
Definition: clientprotocolmsg.h:667
Privmsg(NoCopy, User *source, const std::string &target, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:531
Ping(const std::string &cookie)
Definition: clientprotocolmsg.h:641
Definition: clientprotocolmsg.h:154
Mode()
Definition: clientprotocolmsg.h:335
Definition: clientprotocolmsg.h:609
Join(Membership *Memb, const std::string &sourcestrref)
Definition: clientprotocolmsg.h:130
Definition: clientprotocolmsg.h:47
Definition: membership.h:32
const List & getlist() const
Definition: modechange.h:118
void SetParams(Membership *Memb)
Definition: clientprotocolmsg.h:139
Privmsg(NoCopy, const std::string &source, const Channel *targetchan, const std::string &text, MessageType mt=MSG_PRIVMSG, char status=0)
Definition: clientprotocolmsg.h:585
Mode(User *source, Channel *Chantarget, User *Usertarget, const Modes::ChangeList &changelist, Modes::ChangeList::List::const_iterator beginiter)
Definition: clientprotocolmsg.h:304
Definition: modechange.h:30
Quit(User *source, const std::string &reason)
Definition: clientprotocolmsg.h:197
User *const user
Definition: membership.h:41
Definition: clientprotocol.h:180
NoCopy
Definition: clientprotocolmsg.h:434
Definition: clientprotocolmsg.h:224
User * GetUserTarget() const
Definition: clientprotocolmsg.h:380
Join(Membership *Memb)
Definition: clientprotocolmsg.h:120
Error(const std::string &text)
Definition: clientprotocolmsg.h:672
const std::string & GetStrTarget() const
Definition: clientprotocolmsg.h:375
std::string nick
Definition: users.h:302
Definition: clientprotocolmsg.h:104
Numeric(const ::Numeric::Numeric &num, const std::string &target)
Definition: clientprotocolmsg.h:83
Privmsg(User *source, const std::string &target, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:451
Definition: clientprotocolmsg.h:172
Mode(User *source, Channel *Chantarget, User *Usertarget, const Modes::ChangeList &changelist)
Definition: clientprotocolmsg.h:321
void SetCommand(const char *cmd)
Definition: clientprotocol.h:295
Modes::ChangeList::List::const_iterator GetBeginIterator() const
Definition: clientprotocolmsg.h:363
Channel *const chan
Definition: membership.h:45
Nick(User *source, const std::string &newnick)
Definition: clientprotocolmsg.h:214
Topic(User *source, const Channel *chan, const std::string &newtopic)
Definition: clientprotocolmsg.h:397
Privmsg(NoCopy, const std::string &source, const std::string &target, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:571
Definition: clientprotocolmsg.h:390
Definition: clientprotocolmsg.h:208
void PushParamRef(const std::string &str)
Definition: clientprotocol.h:365
Channel * GetChanTarget() const
Definition: clientprotocolmsg.h:385
Pong(const std::string &cookie)
Definition: clientprotocolmsg.h:656
void PushParam(const char *str)
Definition: clientprotocol.h:354
Privmsg(NoCopy, const std::string &source, const User *targetuser, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:598
Privmsg(User *source, const User *targetuser, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:478
Numeric(const ::Numeric::Numeric &num, User *user)
Definition: clientprotocolmsg.h:69
Kick(User *source, Membership *memb, const std::string &reason)
Definition: clientprotocolmsg.h:179
static std::string ToModeLetters(const Modes::ChangeList &changelist)
Definition: clientprotocolmsg.h:290
std::string ServerName
Definition: configreader.h:318
void ClearParams()
Definition: clientprotocol.h:420
Definition: modechange.h:51
Definition: clientprotocolmsg.h:651
Privmsg(NoCopy, User *source, const User *targetuser, const std::string &text, MessageType mt=MSG_PRIVMSG)
Definition: clientprotocolmsg.h:558
Privmsg(const std::string &source, const Channel *targetchan, const std::string &text, MessageType mt=MSG_PRIVMSG, char status=0)
Definition: clientprotocolmsg.h:505
char GetModeChar() const
Definition: mode.h:236
Definition: clientprotocolmsg.h:627
Definition: channels.h:37