irc-op.c 5.5 KB

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