irc-op.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 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 <assert.h>
  17. #include <string.h>
  18. #include "conn.h"
  19. #include "channel.h"
  20. #include "irc-macros.h"
  21. #include "irc-write.h"
  22. #include "lists.h"
  23. #include "log.h"
  24. #include "messages.h"
  25. #include "parse.h"
  26. #include "irc-op.h"
  27. /* Local functions */
  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_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
  35. Client_ID(from), nick);
  36. Channel_Kick(peer, target, from, channel, reason);
  37. return true;
  38. }
  39. /* Global functions */
  40. /**
  41. * Handler for the IRC command "KICK".
  42. *
  43. * @param Client The client from which this command has been received.
  44. * @param Req Request structure with prefix and all parameters.
  45. * @return CONNECTED or DISCONNECTED.
  46. */
  47. GLOBAL bool
  48. IRC_KICK(CLIENT *Client, REQUEST *Req)
  49. {
  50. CLIENT *from;
  51. char *itemList = Req->argv[0];
  52. const char* currentNick, *currentChannel, *reason;
  53. unsigned int channelCount = 1;
  54. unsigned int nickCount = 1;
  55. assert( Client != NULL );
  56. assert( Req != NULL );
  57. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  58. while (*itemList) {
  59. if (*itemList == ',') {
  60. *itemList = '\0';
  61. channelCount++;
  62. }
  63. itemList++;
  64. }
  65. itemList = Req->argv[1];
  66. while (*itemList) {
  67. if (*itemList == ',') {
  68. *itemList = '\0';
  69. nickCount++;
  70. }
  71. itemList++;
  72. }
  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_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  101. Client_ID(Client), Req->command);
  102. }
  103. return true;
  104. } /* IRC_KICK */
  105. /**
  106. * Handler for the IRC command "INVITE".
  107. *
  108. * @param Client The client from which this command has been received.
  109. * @param Req Request structure with prefix and all parameters.
  110. * @return CONNECTED or DISCONNECTED.
  111. */
  112. GLOBAL bool
  113. IRC_INVITE(CLIENT *Client, REQUEST *Req)
  114. {
  115. CHANNEL *chan;
  116. CLIENT *target, *from;
  117. const char *colon_if_necessary;
  118. bool remember = false;
  119. assert( Client != NULL );
  120. assert( Req != NULL );
  121. _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
  122. /* Search user */
  123. target = Client_Search(Req->argv[0]);
  124. if (!target || (Client_Type(target) != CLIENT_USER))
  125. return IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
  126. Client_ID(Client), Req->argv[0]);
  127. if (Req->argv[1][0] == '&') {
  128. /* Local channel. Make sure the target user is on this server! */
  129. if (Client_Conn(target) == NONE)
  130. return IRC_WriteErrClient(from, ERR_USERNOTONSERV_MSG,
  131. Client_ID(Client),
  132. Req->argv[0]);
  133. }
  134. chan = Channel_Search(Req->argv[1]);
  135. if (chan) {
  136. /* Channel exists. Is the user a valid member of the channel? */
  137. if (!Channel_IsMemberOf(chan, from))
  138. return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
  139. Client_ID(Client),
  140. Req->argv[1]);
  141. /* Is the channel "invite-disallow"? */
  142. if (Channel_HasMode(chan, 'V'))
  143. return IRC_WriteErrClient(from, ERR_NOINVITE_MSG,
  144. Client_ID(from),
  145. Channel_Name(chan));
  146. /* Is the channel "invite-only"? */
  147. if (Channel_HasMode(chan, 'i')) {
  148. /* Yes. The user issuing the INVITE command must be
  149. * channel owner/admin/operator/halfop! */
  150. if (!Channel_UserHasMode(chan, from, 'q') &&
  151. !Channel_UserHasMode(chan, from, 'a') &&
  152. !Channel_UserHasMode(chan, from, 'o') &&
  153. !Channel_UserHasMode(chan, from, 'h'))
  154. return IRC_WriteErrClient(from,
  155. ERR_CHANOPRIVSNEEDED_MSG,
  156. Client_ID(from),
  157. Channel_Name(chan));
  158. remember = true;
  159. }
  160. /* Is the target user already member of the channel? */
  161. if (Channel_IsMemberOf(chan, target))
  162. return IRC_WriteErrClient(from, ERR_USERONCHANNEL_MSG,
  163. Client_ID(from),
  164. Req->argv[0], Req->argv[1]);
  165. /* If the target user is banned on that channel: remember invite */
  166. if (Lists_Check(Channel_GetListBans(chan), target))
  167. remember = true;
  168. if (remember) {
  169. /* We must remember this invite */
  170. if (!Channel_AddInvite(chan, Client_MaskCloaked(target),
  171. true, Client_ID(from)))
  172. return CONNECTED;
  173. }
  174. }
  175. LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from),
  176. Req->argv[0], Req->argv[1]);
  177. /*
  178. * RFC 2812 states:
  179. * 'There is no requirement that the channel [..] must exist or be a
  180. * valid channel'. The problem with this is that this allows the
  181. * "channel" to contain spaces, in which case we must prefix its name
  182. * with a colon to make it clear that it is only a single argument.
  183. */
  184. colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
  185. /* Inform target client */
  186. IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
  187. colon_if_necessary, Req->argv[1]);
  188. if (Client_Conn(target) > NONE) {
  189. /* The target user is local, so we have to send the status code */
  190. if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
  191. Client_ID(from), Req->argv[0],
  192. colon_if_necessary, Req->argv[1]))
  193. return DISCONNECTED;
  194. if (Client_HasMode(target, 'a') &&
  195. !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
  196. Client_ID(target), Client_Away(target)))
  197. return DISCONNECTED;
  198. }
  199. return CONNECTED;
  200. } /* IRC_INVITE */
  201. /* -eof- */