irc-cap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2012 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. * Handler for IRC capability ("CAP") commands
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "defines.h"
  20. #include "conn.h"
  21. #include "channel.h"
  22. #include "client-cap.h"
  23. #include "irc-write.h"
  24. #include "log.h"
  25. #include "login.h"
  26. #include "messages.h"
  27. #include "parse.h"
  28. #include "exp.h"
  29. #include "irc-cap.h"
  30. bool Handle_CAP_LS PARAMS((CLIENT *Client, char *Arg));
  31. bool Handle_CAP_LIST PARAMS((CLIENT *Client, char *Arg));
  32. bool Handle_CAP_REQ PARAMS((CLIENT *Client, char *Arg));
  33. bool Handle_CAP_ACK PARAMS((CLIENT *Client, char *Arg));
  34. bool Handle_CAP_CLEAR PARAMS((CLIENT *Client));
  35. bool Handle_CAP_END PARAMS((CLIENT *Client));
  36. void Set_CAP_Negotiation PARAMS((CLIENT *Client));
  37. int Parse_CAP PARAMS((int Capabilities, char *Args));
  38. char *Get_CAP_String PARAMS((int Capabilities));
  39. /**
  40. * Handler for the IRCv3 "CAP" command.
  41. *
  42. * @param Client The client from which this command has been received.
  43. * @param Req Request structure with prefix and all parameters.
  44. * @returns CONNECTED or DISCONNECTED.
  45. */
  46. GLOBAL bool
  47. IRC_CAP(CLIENT *Client, REQUEST *Req)
  48. {
  49. assert(Client != NULL);
  50. assert(Req != NULL);
  51. /* Bad number of prameters? */
  52. if (Req->argc < 1 || Req->argc > 2)
  53. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  54. Client_ID(Client), Req->command);
  55. LogDebug("Got \"%s %s\" command from \"%s\" ...",
  56. Req->command, Req->argv[0], Client_ID(Client));
  57. if (Req->argc == 1) {
  58. if (strcasecmp(Req->argv[0], "CLEAR") == 0)
  59. return Handle_CAP_CLEAR(Client);
  60. if (strcasecmp(Req->argv[0], "END") == 0)
  61. return Handle_CAP_END(Client);
  62. }
  63. if (Req->argc >= 1 && Req->argc <= 2) {
  64. if (strcasecmp(Req->argv[0], "LS") == 0)
  65. return Handle_CAP_LS(Client, Req->argv[1]);
  66. if (strcasecmp(Req->argv[0], "LIST") == 0)
  67. return Handle_CAP_LIST(Client, Req->argv[1]);
  68. }
  69. if (Req->argc == 2) {
  70. if (strcasecmp(Req->argv[0], "REQ") == 0)
  71. return Handle_CAP_REQ(Client, Req->argv[1]);
  72. if (strcasecmp(Req->argv[0], "ACK") == 0)
  73. return Handle_CAP_ACK(Client, Req->argv[1]);
  74. }
  75. return IRC_WriteStrClient(Client, ERR_INVALIDCAP_MSG,
  76. Client_ID(Client), Req->argv[0]);
  77. }
  78. /**
  79. * Handler for the "CAP LS" command.
  80. *
  81. * @param Client The client from which this command has been received.
  82. * @param Arg Command argument or NULL.
  83. * @returns CONNECTED or DISCONNECTED.
  84. */
  85. bool
  86. Handle_CAP_LS(CLIENT *Client, UNUSED char *Arg)
  87. {
  88. assert(Client != NULL);
  89. Set_CAP_Negotiation(Client);
  90. return IRC_WriteStrClient(Client,
  91. "CAP %s LS :multi-prefix",
  92. Client_ID(Client));
  93. }
  94. /**
  95. * Handler for the "CAP LIST" command.
  96. *
  97. * @param Client The client from which this command has been received.
  98. * @param Arg Command argument or NULL.
  99. * @returns CONNECTED or DISCONNECTED.
  100. */
  101. bool
  102. Handle_CAP_LIST(CLIENT *Client, UNUSED char *Arg)
  103. {
  104. assert(Client != NULL);
  105. return IRC_WriteStrClient(Client, "CAP %s LIST :%s", Client_ID(Client),
  106. Get_CAP_String(Client_Cap(Client)));
  107. }
  108. /**
  109. * Handler for the "CAP REQ" command.
  110. *
  111. * @param Client The client from which this command has been received.
  112. * @param Arg Command argument.
  113. * @returns CONNECTED or DISCONNECTED.
  114. */
  115. bool
  116. Handle_CAP_REQ(CLIENT *Client, char *Arg)
  117. {
  118. int new_cap;
  119. assert(Client != NULL);
  120. assert(Arg != NULL);
  121. Set_CAP_Negotiation(Client);
  122. new_cap = Parse_CAP(Client_Cap(Client), Arg);
  123. if (new_cap < 0)
  124. return IRC_WriteStrClient(Client, "CAP %s NAK :%s",
  125. Client_ID(Client), Arg);
  126. Client_CapSet(Client, new_cap);
  127. return IRC_WriteStrClient(Client, "CAP %s ACK :%s",
  128. Client_ID(Client), Arg);
  129. }
  130. /**
  131. * Handler for the "CAP ACK" command.
  132. *
  133. * @param Client The client from which this command has been received.
  134. * @param Arg Command argument.
  135. * @returns CONNECTED or DISCONNECTED.
  136. */
  137. bool
  138. Handle_CAP_ACK(UNUSED CLIENT *Client, UNUSED char *Arg)
  139. {
  140. assert(Client != NULL);
  141. assert(Arg != NULL);
  142. return CONNECTED;
  143. }
  144. /**
  145. * Handler for the "CAP CLEAR" command.
  146. *
  147. * @param Client The client from which this command has been received.
  148. * @returns CONNECTED or DISCONNECTED.
  149. */
  150. bool
  151. Handle_CAP_CLEAR(CLIENT *Client)
  152. {
  153. int cap_old;
  154. assert(Client != NULL);
  155. cap_old = Client_Cap(Client);
  156. if (cap_old & CLIENT_CAP_MULTI_PREFIX)
  157. Client_CapDel(Client, CLIENT_CAP_MULTI_PREFIX);
  158. return IRC_WriteStrClient(Client, "CAP %s ACK :%s", Client_ID(Client),
  159. Get_CAP_String(cap_old));
  160. }
  161. /**
  162. * Handler for the "CAP END" command.
  163. *
  164. * @param Client The client from which this command has been received.
  165. * @returns CONNECTED or DISCONNECTED.
  166. */
  167. bool
  168. Handle_CAP_END(CLIENT *Client)
  169. {
  170. assert(Client != NULL);
  171. if (Client_Type(Client) != CLIENT_USER) {
  172. /* User is still logging in ... */
  173. Client_CapDel(Client, CLIENT_CAP_PENDING);
  174. if (Client_Type(Client) == CLIENT_WAITCAPEND) {
  175. /* Only "CAP END" was missing: log in! */
  176. return Login_User(Client);
  177. }
  178. }
  179. return CONNECTED;
  180. }
  181. /**
  182. * Set CAP negotiation status and mark client as "supports capabilities".
  183. *
  184. * @param Client The client to handle.
  185. */
  186. void
  187. Set_CAP_Negotiation(CLIENT *Client)
  188. {
  189. assert(Client != NULL);
  190. if (Client_Type(Client) != CLIENT_USER)
  191. Client_CapAdd(Client, CLIENT_CAP_PENDING);
  192. Client_CapAdd(Client, CLIENT_CAP_SUPPORTED);
  193. }
  194. /**
  195. * Parse capability string and return numeric flag value.
  196. *
  197. * @param Args The string containing space-separated capability names.
  198. * @return Changed capability flags or 0 on error.
  199. */
  200. int
  201. Parse_CAP(int Capabilities, char *Args)
  202. {
  203. static char tmp[COMMAND_LEN];
  204. char *ptr;
  205. assert(Args != NULL);
  206. strlcpy(tmp, Args, sizeof(tmp));
  207. ptr = strtok(tmp, " ");
  208. while (ptr) {
  209. if (*ptr == '-') {
  210. /* drop capabilities */
  211. ptr++;
  212. if (strcmp(ptr, "multi-prefix") == 0)
  213. Capabilities &= ~CLIENT_CAP_MULTI_PREFIX;
  214. else
  215. return -1;
  216. } else {
  217. /* request capabilities */
  218. if (strcmp(ptr, "multi-prefix") == 0)
  219. Capabilities |= CLIENT_CAP_MULTI_PREFIX;
  220. else
  221. return -1;
  222. }
  223. ptr = strtok(NULL, " ");
  224. }
  225. return Capabilities;
  226. }
  227. /**
  228. * Return textual representation of capability flags.
  229. *
  230. * Please note: this function returns a pointer to a global buffer and
  231. * therefore isn't thread safe!
  232. *
  233. * @param Capabilities Capability flags (bitmask).
  234. * @return Pointer to textual representation.
  235. */
  236. char *
  237. Get_CAP_String(int Capabilities)
  238. {
  239. static char txt[COMMAND_LEN];
  240. txt[0] = '\0';
  241. if (Capabilities & CLIENT_CAP_MULTI_PREFIX)
  242. strlcat(txt, "multi-prefix ", sizeof(txt));
  243. return txt;
  244. }
  245. /* -eof- */