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
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2018, 2020-2023 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2018 Attila Molnar <attilamolnar@hush.com>
*
* This file is part of InspIRCd. InspIRCd 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, version 2.
*
* 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/>.
*/
#pragma once
namespace ClientProtocol
{
namespace Messages
{
class Numeric;
class Join;
struct Part;
struct Kick;
struct Quit;
struct Nick;
class Mode;
struct Topic;
class Privmsg;
struct Invite;
struct Ping;
struct Pong;
struct Error;
}
}
/** Numeric message.
* Doesn't have a fixed command name, it's always a 3 digit number padded with zeroes if necessary.
* The first parameter is the target of the numeric which is almost always the nick of the user
* the numeric will be sent to.
*/
class CoreExport ClientProtocol::Messages::Numeric
: public ClientProtocol::Message
{
char numericstr[4];
void InitCommand(unsigned int number)
{
snprintf(numericstr, sizeof(numericstr), "%03u", number);
SetCommand(numericstr);
}
void InitFromNumeric(const ::Numeric::Numeric& numeric)
{
AddTags(numeric.GetParams().GetTags());
InitCommand(numeric.GetNumeric());
for (const auto& param : numeric.GetParams())
PushParamRef(param);
}
public:
/** Constructor, target is a User.
* @param num Numeric object to send. Must remain valid as long as this object is alive and must not be modified.
* @param user User to send the numeric to. May be partially connected, must remain valid as long as this object is alive.
*/
Numeric(const ::Numeric::Numeric& num, User* user)
: ClientProtocol::Message(nullptr, (num.GetServer() ? num.GetServer() : ServerInstance->FakeClient->server)->GetPublicName())
{
if (user->connected & User::CONN_NICK)
PushParamRef(user->nick);
else
PushParam("*");
InitFromNumeric(num);
}
/** Constructor, target is a string.
* @param num Numeric object to send. Must remain valid as long as this object is alive and must not be modified.
* @param target Target string, must stay valid as long as this object is alive.
*/
Numeric(const ::Numeric::Numeric& num, const std::string& target)
: ClientProtocol::Message(nullptr, (num.GetServer() ? num.GetServer() : ServerInstance->FakeClient->server)->GetPublicName())
{
PushParamRef(target);
InitFromNumeric(num);
}
/** Constructor. Only the numeric number has to be specified.
* @param num Numeric number.
*/
Numeric(unsigned int num)
: ClientProtocol::Message(nullptr, ServerInstance->Config->GetServerName())
{
InitCommand(num);
PushParam("*");
}
};
/** JOIN message.
* Sent when a user joins a channel.
*/
class CoreExport ClientProtocol::Messages::Join
: public ClientProtocol::Message
{
Membership* memb;
public:
/** Constructor. Does not populate parameters, call SetParams() before sending the message.
*/
Join()
: ClientProtocol::Message("JOIN")
, memb(nullptr)
{
}
/** Constructor.
* @param Memb Membership of the joining user.
*/
Join(Membership* Memb)
: ClientProtocol::Message("JOIN", Memb->user)
{
SetParams(Memb);
}
/** Constructor.
* @param Memb Membership of the joining user.
* @param sourcestrref Message source string, must remain valid as long as this object is alive.
*/
Join(Membership* Memb, const std::string& sourcestrref)
: ClientProtocol::Message("JOIN", sourcestrref, Memb->user)
{
SetParams(Memb);
}
/** Populate parameters from a Membership
* @param Memb Membership of the joining user.
*/
void SetParams(Membership* Memb)
{
memb = Memb;
PushParamRef(memb->chan->name);
}
/** Get the Membership of the joining user.
* @return Membership of the joining user.
*/
Membership* GetMember() const { return memb; }
};
/** PART message.
* Sent when a user parts a channel.
*/
struct CoreExport ClientProtocol::Messages::Part
: public ClientProtocol::Message
{
/** Constructor.
* @param memb Member parting.
* @param reason Part reason, may be empty. If non-empty, must remain valid as long as this object is alive.
*/
Part(Membership* memb, const std::string& reason)
: ClientProtocol::Message("PART", memb->user)
{
PushParamRef(memb->chan->name);
if (!reason.empty())
PushParamRef(reason);
}
};
/** KICK message.
* Sent when a user is kicked from a channel.
*/
struct CoreExport ClientProtocol::Messages::Kick
: public ClientProtocol::Message
{
/** Constructor.
* @param source User that does the kick.
* @param memb Membership of the user being kicked.
* @param reason Kick reason. Must remain valid as long as this object is alive.
*/
Kick(User* source, Membership* memb, const std::string& reason)
: ClientProtocol::Message("KICK", source)
{
PushParamRef(memb->chan->name);
PushParamRef(memb->user->nick);
PushParamRef(reason);
}
};
/** QUIT message.
* Sent when a user quits.
*/
struct CoreExport ClientProtocol::Messages::Quit
: public ClientProtocol::Message
{
/** Constructor.
* @param source User quitting.
* @param reason Quit reason, may be empty. Must remain valid as long as this object is alive.
*/
Quit(User* source, const std::string& reason)
: ClientProtocol::Message("QUIT", source)
{
if (!reason.empty())
PushParamRef(reason);
}
};
/** NICK message.
* Sent when a user changes their nickname.
*/
struct CoreExport ClientProtocol::Messages::Nick
: public ClientProtocol::Message
{
/** Constructor.
* @param source User changing nicks.
* @param newnick New nickname. Must remain valid as long as this object is alive.
*/
Nick(User* source, const std::string& newnick)
: ClientProtocol::Message("NICK", source)
{
PushParamRef(newnick);
}
};
/** MODE message.
* Sent when modes are changed on a user or channel.
*/
class CoreExport ClientProtocol::Messages::Mode
: public ClientProtocol::Message
{
Channel* chantarget;
User* usertarget;
Modes::ChangeList::List::const_iterator beginit;
Modes::ChangeList::List::const_iterator lastit;
/** Convert a range of a mode change list to mode letters and '+', '-' symbols.
* @param list Mode change list.
* @param maxlinelen Maximum output length.
* @param beginit Iterator to the first element in 'list' to process.
* @param lastit Iterator which is set to the first element not processed due to length limitations by the method.
*/
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)
{
std::string ret;
std::string::size_type paramlength = 0;
char output_pm = '\0'; // current output state, '+' or '-'
Modes::ChangeList::List::const_iterator i;
for (i = beginit; i != list.end(); ++i)
{
const Modes::Change& item = *i;
const char needed_pm = (item.adding ? '+' : '-');
if (needed_pm != output_pm)
{
output_pm = needed_pm;
ret.push_back(output_pm);
}
if (!item.param.empty())
paramlength += item.param.length() + 1;
if (ret.length() + 1 + paramlength > maxlinelen)
{
// Mode sequence is getting too long
if ((ret.back() == '+') || (ret.back() == '-'))
ret.pop_back();
break;
}
ret.push_back(item.mh->GetModeChar());
}
lastit = i;
return ret;
}
/** Push mode parameters for modes that have one, starting at beginit to lastit (not including lastit).
*/
void PushModeParams()
{
for (Modes::ChangeList::List::const_iterator i = beginit; i != lastit; ++i)
{
const Modes::Change& item = *i;
if (!item.param.empty())
PushParamRef(item.param);
}
}
public:
/** Convert an entire mode change list into mode letters and '+' and '-' characters.
* @param changelist Mode change list to convert into mode letters.
* @return Mode letters.
*/
static std::string ToModeLetters(const Modes::ChangeList& changelist)
{
std::string dummystr;
Modes::ChangeList::List::const_iterator dummy;
return ToModeLetters(changelist.getlist(), dummystr.max_size(), changelist.getlist().begin(), dummy);
}
/** Constructor, populate parameters starting from a given position in a mode change list.
* @param source User doing the mode change.
* @param Chantarget Channel target of the mode change. May be NULL if Usertarget is non-NULL.
* @param Usertarget User target of the mode change. May be NULL if Chantarget is non-NULL.
* @param changelist Mode change list. Must remain valid and unchanged as long as this object is alive or until the next SetParams() call.
* @param beginiter Starting position of mode changes in 'changelist'.
*/
Mode(User* source, Channel* Chantarget, User* Usertarget, const Modes::ChangeList& changelist, Modes::ChangeList::List::const_iterator beginiter)
: ClientProtocol::Message("MODE", source)
, chantarget(Chantarget)
, usertarget(Usertarget)
, beginit(beginiter)
{
PushParamRef(GetStrTarget());
PushParam(ToModeLetters(changelist.getlist(), 450, beginit, lastit));
PushModeParams();
}
/** Constructor, populate parameters starting from the beginning of a mode change list.
* @param source User doing the mode change.
* @param Chantarget Channel target of the mode change. May be NULL if Usertarget is non-NULL.
* @param Usertarget User target of the mode change. May be NULL if Chantarget is non-NULL.
* @param changelist Mode change list. Must remain valid and unchanged as long as this object is alive or until the next SetParams() call.
*/
Mode(User* source, Channel* Chantarget, User* Usertarget, const Modes::ChangeList& changelist)
: ClientProtocol::Message("MODE", source)
, chantarget(Chantarget)
, usertarget(Usertarget)
, beginit(changelist.getlist().begin())
{
PushParamRef(GetStrTarget());
PushParam(ToModeLetters(changelist.getlist(), 450, beginit, lastit));
PushModeParams();
}
/** Constructor. Does not populate parameters, call SetParams() before sending the message.
* The message source is set to the local server.
*/
Mode()
: ClientProtocol::Message("MODE", ServerInstance->FakeClient)
, chantarget(nullptr)
, usertarget(nullptr)
{
}
/** Set parameters
* @param Chantarget Channel target of the mode change. May be NULL if Usertarget is non-NULL.
* @param Usertarget User target of the mode change. May be NULL if Chantarget is non-NULL.
* @param changelist Mode change list. Must remain valid and unchanged as long as this object is alive or until the next SetParams() call.
*/
void SetParams(Channel* Chantarget, User* Usertarget, const Modes::ChangeList& changelist)
{
ClearParams();
chantarget = Chantarget;
usertarget = Usertarget;
beginit = changelist.getlist().begin();
PushParamRef(GetStrTarget());
PushParam(ToModeLetters(changelist.getlist(), 450, beginit, lastit));
PushModeParams();
}
/** Get first mode change included in this MODE message.
* @return Iterator to the first mode change that is included in this MODE message.
*/
Modes::ChangeList::List::const_iterator GetBeginIterator() const { return beginit; }
/** Get first mode change not included in this MODE message.
* @return Iterator to the first mode change that is not included in this MODE message.
*/
Modes::ChangeList::List::const_iterator GetEndIterator() const { return lastit; }
/** Get mode change target as a string.
* This is the name of the channel if the mode change targets a channel or the nickname of the user
* if the target is a user.
* @return Name of target as a string.
*/
const std::string& GetStrTarget() const { return (chantarget ? chantarget->name : usertarget->nick); }
/** Get user target.
* @return User target or NULL if the mode change targets a channel.
*/
User* GetUserTarget() const { return usertarget; }
/** Get channel target.
* @return Channel target or NULL if the mode change targets a user.
*/
Channel* GetChanTarget() const { return chantarget; }
};
/** TOPIC message.
*/
struct CoreExport ClientProtocol::Messages::Topic
: public ClientProtocol::Message
{
/** Constructor.
* @param source User changing the topic.
* @param chan Channel the topic is being changed on.
* @param newtopic New topic. May be empty, must remain valid as long as this object is alive.
*/
Topic(User* source, const Channel* chan, const std::string& newtopic)
: ClientProtocol::Message("TOPIC", source)
{
PushParamRef(chan->name);
PushParamRef(newtopic);
}
};
/** PRIVMSG and NOTICE message.
*/
class CoreExport ClientProtocol::Messages::Privmsg
: public ClientProtocol::Message
{
void PushTargetChan(char status, const Channel* targetchan)
{
if (status)
{
std::string rawtarget(1, status);
rawtarget.append(targetchan->name);
PushParam(rawtarget);
}
else
{
PushParamRef(targetchan->name);
}
}
void PushTargetUser(const User* targetuser)
{
if (targetuser->connected & User::CONN_NICK)
PushParamRef(targetuser->nick);
else
PushParam("*");
}
public:
/** Used to differentiate constructors that copy the text from constructors that do not.
*/
enum NoCopy { nocopy };
/** Get command name from MessageType.
* @param mt Message type to get command name for.
* @return Command name for the message type.
*/
static const char* CommandStrFromMsgType(MessageType mt)
{
return ((mt == MessageType::PRIVMSG) ? "PRIVMSG" : "NOTICE");
}
/** Constructor, user source, string target, copies text.
* @param source Source user.
* @param target Privmsg target string.
* @param text Privmsg text, will be copied.
* @param mt Message type.
*/
Privmsg(User* source, const std::string& target, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushParam(target);
PushParam(text);
}
/** Constructor, user source, user target, copies text.
* @param source Source user.
* @param targetchan Target channel.
* @param text Privmsg text, will be copied.
* @param mt Message type.
* @param status Prefix character for status messages. If non-zero the message is a status message. Optional, defaults to 0.
*/
Privmsg(User* source, const Channel* targetchan, const std::string& text, MessageType mt = MessageType::PRIVMSG, char status = 0)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetChan(status, targetchan);
PushParam(text);
}
/** Constructor, user source, user target, copies text.
* @param source Source user.
* @param targetuser Target user.
* @param text Privmsg text, will be copied.
* @param mt Message type.
*/
Privmsg(User* source, const User* targetuser, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetUser(targetuser);
PushParam(text);
}
/** Constructor, string source, string target, copies text.
* @param source Source user.
* @param target Target string.
* @param text Privmsg text, will be copied.
* @param mt Message type.
* @param status Prefix character for status messages. If non-zero the message is a status message. Optional, defaults to 0.
*/
Privmsg(const std::string& source, const std::string& target, const std::string& text, MessageType mt = MessageType::PRIVMSG, char status = 0)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
if (status)
{
std::string rawtarget(1, status);
rawtarget.append(target);
PushParam(rawtarget);
}
else
{
PushParam(target);
}
PushParam(text);
}
/** Constructor, string source, channel target, copies text.
* @param source Source string.
* @param targetchan Target channel.
* @param text Privmsg text, will be copied.
* @param status Prefix character for status messages. If non-zero the message is a status message. Optional, defaults to 0.
* @param mt Message type.
*/
Privmsg(const std::string& source, const Channel* targetchan, const std::string& text, MessageType mt = MessageType::PRIVMSG, char status = 0)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetChan(status, targetchan);
PushParam(text);
}
/** Constructor, string source, user target, copies text.
* @param source Source string.
* @param targetuser Target user.
* @param text Privmsg text, will be copied.
* @param mt Message type.
*/
Privmsg(const std::string& source, const User* targetuser, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetUser(targetuser);
PushParam(text);
}
/** Constructor, user source, string target, copies text.
* @param source Source user.
* @param target Target string.
* @param text Privmsg text, will not be copied.
* @param mt Message type.
*/
Privmsg(NoCopy, User* source, const std::string& target, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushParam(target);
PushParamRef(text);
}
/** Constructor, user source, channel target, does not copy text.
* @param source Source user.
* @param targetchan Target channel.
* @param text Privmsg text, will not be copied.
* @param status Prefix character for status messages. If non-zero the message is a status message. Optional, defaults to 0.
* @param mt Message type.
*/
Privmsg(NoCopy, User* source, const Channel* targetchan, const std::string& text, MessageType mt = MessageType::PRIVMSG, char status = 0)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetChan(status, targetchan);
PushParamRef(text);
}
/** Constructor, user source, user target, does not copy text.
* @param source Source user.
* @param targetuser Target user.
* @param text Privmsg text, will not be copied.
* @param mt Message type.
*/
Privmsg(NoCopy, User* source, const User* targetuser, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetUser(targetuser);
PushParamRef(text);
}
/** Constructor, string source, string target, does not copy text.
* @param source Source string.
* @param target Target string.
* @param text Privmsg text, will not be copied.
* @param mt Message type.
*/
Privmsg(NoCopy, const std::string& source, const std::string& target, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushParam(target);
PushParamRef(text);
}
/** Constructor, string source, channel target, does not copy text.
* @param source Source string.
* @param targetchan Target channel.
* @param text Privmsg text, will not be copied.
* @param status Prefix character for status messages. If non-zero the message is a status message. Optional, defaults to 0.
* @param mt Message type.
*/
Privmsg(NoCopy, const std::string& source, const Channel* targetchan, const std::string& text, MessageType mt = MessageType::PRIVMSG, char status = 0)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetChan(status, targetchan);
PushParamRef(text);
}
/** Constructor, string source, user target, does not copy text.
* @param source Source string.
* @param targetuser Target user.
* @param text Privmsg text, will not be copied.
* @param mt Message type.
*/
Privmsg(NoCopy, const std::string& source, const User* targetuser, const std::string& text, MessageType mt = MessageType::PRIVMSG)
: ClientProtocol::Message(CommandStrFromMsgType(mt), source)
{
PushTargetUser(targetuser);
PushParamRef(text);
}
};
/** INVITE message.
* Sent when a user is invited to join a channel.
*/
struct CoreExport ClientProtocol::Messages::Invite
: public ClientProtocol::Message
{
/** Constructor.
* @param source User inviting the target user.
* @param target User being invited by source.
* @param chan Channel the target user is being invited to.
*/
Invite(User* source, User* target, Channel* chan)
: ClientProtocol::Message("INVITE", source)
{
PushParamRef(target->nick);
PushParamRef(chan->name);
}
};
/** PING message.
* Used to check if a connection is still alive.
*/
struct CoreExport ClientProtocol::Messages::Ping
: public ClientProtocol::Message
{
/** Constructor.
* The ping cookie is the name of the local server.
*/
Ping()
: ClientProtocol::Message("PING")
{
PushParamRef(ServerInstance->Config->GetServerName());
}
/** Constructor.
* @param cookie Ping cookie. Must remain valid as long as this object is alive.
*/
Ping(const std::string& cookie)
: ClientProtocol::Message("PING")
{
PushParamRef(cookie);
}
};
/** PONG message.
* Sent as a reply to PING.
*/
struct CoreExport ClientProtocol::Messages::Pong
: public ClientProtocol::Message
{
/** Constructor.
* @param cookie Ping cookie. Must remain valid as long as this object is alive.
* @param server Pinged server. Must remain valid as long as this object is alive.
*/
Pong(const std::string& cookie, const std::string& server = "")
: ClientProtocol::Message("PONG", ServerInstance->Config->GetServerName())
{
PushParamRef(server);
PushParamRef(cookie);
}
};
/** ERROR message.
* Sent to clients upon disconnection.
*/
struct CoreExport ClientProtocol::Messages::Error
: public ClientProtocol::Message
{
/** Constructor.
* @param text Error text.
*/
Error(const std::string& text)
: ClientProtocol::Message("ERROR")
{
PushParam(text);
}
};
|