irc-op.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
  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-write.h"
  24. #include "lists.h"
  25. #include "log.h"
  26. #include "messages.h"
  27. #include "parse.h"
  28. #include "exp.h"
  29. #include "irc-op.h"
  30. static bool
  31. try_kick(CLIENT *peer, CLIENT* from, const char *nick, const char *channel,
  32. const char *reason)
  33. {
  34. CLIENT *target = Client_Search(nick);
  35. if (!target)
  36. return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG, Client_ID(from), nick);
  37. Channel_Kick(peer, target, from, channel, reason);
  38. return true;
  39. }
  40. GLOBAL bool
  41. IRC_KICK(CLIENT *Client, REQUEST *Req)
  42. {
  43. CLIENT *from;
  44. char *itemList = Req->argv[0];
  45. const char* currentNick, *currentChannel, *reason;
  46. unsigned int channelCount = 1;
  47. unsigned int nickCount = 1;
  48. assert( Client != NULL );
  49. assert( Req != NULL );
  50. if ((Req->argc < 2) || (Req->argc > 3))
  51. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  52. Client_ID(Client), Req->command);
  53. while (*itemList) {
  54. if (*itemList == ',') {
  55. *itemList = '\0';
  56. channelCount++;
  57. }
  58. itemList++;
  59. }
  60. itemList = Req->argv[1];
  61. while (*itemList) {
  62. if (*itemList == ',') {
  63. *itemList = '\0';
  64. nickCount++;
  65. }
  66. itemList++;
  67. }
  68. if (Client_Type(Client) == CLIENT_SERVER)
  69. from = Client_Search(Req->prefix);
  70. else
  71. from = Client;
  72. if (!from)
  73. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  74. Client_ID(Client), Req->prefix);
  75. reason = Req->argc == 3 ? Req->argv[2] : Client_ID(from);
  76. currentNick = Req->argv[1];
  77. currentChannel = Req->argv[0];
  78. if (channelCount == 1) {
  79. while (nickCount > 0) {
  80. if (!try_kick(Client, from, currentNick,
  81. currentChannel, reason))
  82. return false;
  83. while (*currentNick)
  84. currentNick++;
  85. currentNick++;
  86. nickCount--;
  87. }
  88. } else if (channelCount == nickCount) {
  89. while (nickCount > 0) {
  90. if (!try_kick(Client, from, currentNick,
  91. currentChannel, reason))
  92. return false;
  93. while (*currentNick)
  94. currentNick++;
  95. while (*currentChannel)
  96. currentChannel++;
  97. currentNick++;
  98. currentChannel++;
  99. nickCount--;
  100. }
  101. } else {
  102. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  103. Client_ID(Client), Req->command);
  104. }
  105. return true;
  106. } /* IRC_KICK */
  107. GLOBAL bool
  108. IRC_INVITE(CLIENT *Client, REQUEST *Req)
  109. {
  110. CHANNEL *chan;
  111. CLIENT *target, *from;
  112. const char *colon_if_necessary;
  113. bool remember = false;
  114. assert( Client != NULL );
  115. assert( Req != NULL );
  116. if (Req->argc != 2)
  117. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  118. Client_ID(Client), Req->command);
  119. if (Client_Type(Client) == CLIENT_SERVER)
  120. from = Client_Search(Req->prefix);
  121. else
  122. from = Client;
  123. if (!from)
  124. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  125. Client_ID(Client), Req->prefix);
  126. /* Search user */
  127. target = Client_Search(Req->argv[0]);
  128. if (!target || (Client_Type(target) != CLIENT_USER))
  129. return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
  130. Client_ID(Client), Req->argv[0]);
  131. chan = Channel_Search(Req->argv[1]);
  132. if (chan) {
  133. /* Channel exists. Is the user a valid member of the channel? */
  134. if (!Channel_IsMemberOf(chan, from))
  135. return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG, Client_ID(Client), Req->argv[1]);
  136. /* Is the channel "invite-disallow"? */
  137. if (strchr(Channel_Modes(chan), 'V'))
  138. return IRC_WriteStrClient(from, ERR_NOINVITE_MSG,
  139. Client_ID(from), Channel_Name(chan));
  140. /* Is the channel "invite-only"? */
  141. if (strchr(Channel_Modes(chan), 'i')) {
  142. /* Yes. The user must be channel owner/admin/operator/halfop! */
  143. if (!strchr(Channel_UserModes(chan, from), 'q') &&
  144. !strchr(Channel_UserModes(chan, from), 'a') &&
  145. !strchr(Channel_UserModes(chan, from), 'o') &&
  146. !strchr(Channel_UserModes(chan, from), 'h'))
  147. return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
  148. Client_ID(from), Channel_Name(chan));
  149. remember = true;
  150. }
  151. /* Is the target user already member of the channel? */
  152. if (Channel_IsMemberOf(chan, target))
  153. return IRC_WriteStrClient(from, ERR_USERONCHANNEL_MSG,
  154. Client_ID(from), Req->argv[0], Req->argv[1]);
  155. /* If the target user is banned on that channel: remember invite */
  156. if (Lists_Check(Channel_GetListBans(chan), target))
  157. remember = true;
  158. if (remember) {
  159. /* We must remember this invite */
  160. if (!Channel_AddInvite(chan, Client_Mask(target), true))
  161. return CONNECTED;
  162. }
  163. }
  164. LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from), Req->argv[0], Req->argv[1]);
  165. /*
  166. * RFC 2812 says:
  167. * 'There is no requirement that the channel [..] must exist or be a valid channel'
  168. * The problem with this is that this allows the "channel" to contain spaces,
  169. * in which case we must prefix its name with a colon to make it clear that
  170. * it is only a single argument.
  171. */
  172. colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
  173. /* Inform target client */
  174. IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
  175. colon_if_necessary, Req->argv[1]);
  176. if (Client_Conn(target) > NONE) {
  177. /* The target user is local, so we have to send the status code */
  178. if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
  179. Client_ID(from), Req->argv[0], colon_if_necessary, Req->argv[1]))
  180. return DISCONNECTED;
  181. if (strchr(Client_Modes(target), 'a') &&
  182. !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
  183. Client_ID(target), Client_Away(target)))
  184. return DISCONNECTED;
  185. }
  186. return CONNECTED;
  187. } /* IRC_INVITE */
  188. /* -eof- */