irc-op.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Channel operator commands
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "defines.h"
  21. #include "conn.h"
  22. #include "channel.h"
  23. #include "irc-macros.h"
  24. #include "irc-write.h"
  25. #include "lists.h"
  26. #include "log.h"
  27. #include "messages.h"
  28. #include "parse.h"
  29. #include "exp.h"
  30. #include "irc-op.h"
  31. /* Local functions */
  32. static bool
  33. try_kick(CLIENT *peer, CLIENT* from, const char *nick, const char *channel,
  34. const char *reason)
  35. {
  36. CLIENT *target = Client_Search(nick);
  37. if (!target)
  38. return IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
  39. Client_ID(from), nick);
  40. Channel_Kick(peer, target, from, channel, reason);
  41. return true;
  42. }
  43. /* Global functions */
  44. /**
  45. * Handler for the IRC command "KICK".
  46. *
  47. * @param Client The client from which this command has been received.
  48. * @param Req Request structure with prefix and all parameters.
  49. * @return CONNECTED or DISCONNECTED.
  50. */
  51. GLOBAL bool
  52. IRC_KICK(CLIENT *Client, REQUEST *Req)
  53. {
  54. CLIENT *from;
  55. char *itemList = Req->argv[0];
  56. const char* currentNick, *currentChannel, *reason;
  57. unsigned int channelCount = 1;
  58. unsigned int nickCount = 1;
  59. assert( Client != NULL );
  60. assert( Req != NULL );
  61. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  62. while (*itemList) {
  63. if (*itemList == ',') {
  64. *itemList = '\0';
  65. channelCount++;
  66. }
  67. itemList++;
  68. }
  69. itemList = Req->argv[1];
  70. while (*itemList) {
  71. if (*itemList == ',') {
  72. *itemList = '\0';
  73. nickCount++;
  74. }
  75. itemList++;
  76. }
  77. reason = Req->argc == 3 ? Req->argv[2] : Client_ID(from);
  78. currentNick = Req->argv[1];
  79. currentChannel = Req->argv[0];
  80. if (channelCount == 1) {
  81. while (nickCount > 0) {
  82. if (!try_kick(Client, from, currentNick,
  83. currentChannel, reason))
  84. return false;
  85. while (*currentNick)
  86. currentNick++;
  87. currentNick++;
  88. nickCount--;
  89. }
  90. } else if (channelCount == nickCount) {
  91. while (nickCount > 0) {
  92. if (!try_kick(Client, from, currentNick,
  93. currentChannel, reason))
  94. return false;
  95. while (*currentNick)
  96. currentNick++;
  97. while (*currentChannel)
  98. currentChannel++;
  99. currentNick++;
  100. currentChannel++;
  101. nickCount--;
  102. }
  103. } else {
  104. IRC_SetPenalty(Client, 2);
  105. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  106. Client_ID(Client), Req->command);
  107. }
  108. return true;
  109. } /* IRC_KICK */
  110. /**
  111. * Handler for the IRC command "INVITE".
  112. *
  113. * @param Client The client from which this command has been received.
  114. * @param Req Request structure with prefix and all parameters.
  115. * @return CONNECTED or DISCONNECTED.
  116. */
  117. GLOBAL bool
  118. IRC_INVITE(CLIENT *Client, REQUEST *Req)
  119. {
  120. CHANNEL *chan;
  121. CLIENT *target, *from;
  122. const char *colon_if_necessary;
  123. bool remember = false;
  124. assert( Client != NULL );
  125. assert( Req != NULL );
  126. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  127. /* Search user */
  128. target = Client_Search(Req->argv[0]);
  129. if (!target || (Client_Type(target) != CLIENT_USER))
  130. return IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
  131. Client_ID(Client), Req->argv[0]);
  132. chan = Channel_Search(Req->argv[1]);
  133. if (chan) {
  134. /* Channel exists. Is the user a valid member of the channel? */
  135. if (!Channel_IsMemberOf(chan, from))
  136. return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
  137. Client_ID(Client),
  138. Req->argv[1]);
  139. /* Is the channel "invite-disallow"? */
  140. if (Channel_HasMode(chan, 'V'))
  141. return IRC_WriteErrClient(from, ERR_NOINVITE_MSG,
  142. Client_ID(from),
  143. Channel_Name(chan));
  144. /* Is the channel "invite-only"? */
  145. if (Channel_HasMode(chan, 'i')) {
  146. /* Yes. The user must be channel owner/admin/operator/halfop! */
  147. if (!Channel_UserHasMode(chan, from, 'q') &&
  148. !Channel_UserHasMode(chan, from, 'a') &&
  149. !Channel_UserHasMode(chan, from, 'o') &&
  150. !Channel_UserHasMode(chan, from, 'h'))
  151. return IRC_WriteErrClient(from,
  152. ERR_CHANOPRIVSNEEDED_MSG,
  153. Client_ID(from),
  154. Channel_Name(chan));
  155. remember = true;
  156. }
  157. /* Is the target user already member of the channel? */
  158. if (Channel_IsMemberOf(chan, target))
  159. return IRC_WriteErrClient(from, ERR_USERONCHANNEL_MSG,
  160. Client_ID(from),
  161. Req->argv[0], Req->argv[1]);
  162. /* If the target user is banned on that channel: remember invite */
  163. if (Lists_Check(Channel_GetListBans(chan), target))
  164. remember = true;
  165. if (remember) {
  166. /* We must remember this invite */
  167. if (!Channel_AddInvite(chan, Client_MaskCloaked(target),
  168. true))
  169. return CONNECTED;
  170. }
  171. }
  172. LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from),
  173. Req->argv[0], Req->argv[1]);
  174. /*
  175. * RFC 2812 says:
  176. * 'There is no requirement that the channel [..] must exist or be a valid channel'
  177. * The problem with this is that this allows the "channel" to contain spaces,
  178. * in which case we must prefix its name with a colon to make it clear that
  179. * it is only a single argument.
  180. */
  181. colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
  182. /* Inform target client */
  183. IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
  184. colon_if_necessary, Req->argv[1]);
  185. if (Client_Conn(target) > NONE) {
  186. /* The target user is local, so we have to send the status code */
  187. if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
  188. Client_ID(from), Req->argv[0], colon_if_necessary, Req->argv[1]))
  189. return DISCONNECTED;
  190. if (Client_HasMode(target, 'a') &&
  191. !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
  192. Client_ID(target), Client_Away(target)))
  193. return DISCONNECTED;
  194. }
  195. return CONNECTED;
  196. } /* IRC_INVITE */
  197. /* -eof- */