irc-op.c 5.5 KB

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