irc-channel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001,2002 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. * IRC channel commands
  12. */
  13. #include "portab.h"
  14. static char UNUSED id[] = "$Id: irc-channel.c,v 1.35.2.4 2007/07/31 18:54:30 alex Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "defines.h"
  21. #include "conn.h"
  22. #include "client.h"
  23. #include "channel.h"
  24. #include "lists.h"
  25. #include "log.h"
  26. #include "match.h"
  27. #include "messages.h"
  28. #include "parse.h"
  29. #include "irc-info.h"
  30. #include "irc-write.h"
  31. #include "resolve.h"
  32. #include "conf.h"
  33. #include "exp.h"
  34. #include "irc-channel.h"
  35. GLOBAL bool
  36. IRC_JOIN( CLIENT *Client, REQUEST *Req )
  37. {
  38. char *channame, *channame_ptr, *key, *key_ptr, *flags, *topic, modes[8];
  39. bool is_new_chan, is_invited, is_banned;
  40. CLIENT *target;
  41. CHANNEL *chan;
  42. assert( Client != NULL );
  43. assert( Req != NULL );
  44. /* Bad number of arguments? */
  45. if (Req->argc < 1 || Req->argc > 2)
  46. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  47. Client_ID(Client), Req->command);
  48. /* Who is the sender? */
  49. if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
  50. else target = Client;
  51. if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  52. /* Are channel keys given? */
  53. if (Req->argc > 1) {
  54. key = Req->argv[1];
  55. key_ptr = strchr(key, ',');
  56. if (key_ptr) *key_ptr = '\0';
  57. }
  58. else
  59. key = key_ptr = NULL;
  60. channame = Req->argv[0];
  61. channame_ptr = strchr(channame, ',');
  62. if (channame_ptr) *channame_ptr = '\0';
  63. /* Channel-Namen durchgehen */
  64. while (channame)
  65. {
  66. chan = NULL; flags = NULL;
  67. /* wird der Channel neu angelegt? */
  68. if( Channel_Search( channame )) {
  69. is_new_chan = false;
  70. } else {
  71. if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
  72. IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
  73. /* Try next name, if any */
  74. channame = strchr(channame, ',');
  75. continue;
  76. }
  77. is_new_chan = true;
  78. }
  79. /* Hat ein Server Channel-User-Modes uebergeben? */
  80. if( Client_Type( Client ) == CLIENT_SERVER )
  81. {
  82. /* Channel-Flags extrahieren */
  83. flags = strchr( channame, 0x7 );
  84. if( flags )
  85. {
  86. *flags = '\0';
  87. flags++;
  88. }
  89. }
  90. /* Local client? */
  91. if( Client_Type( Client ) == CLIENT_USER )
  92. {
  93. /* Test if the user has reached his maximum channel count */
  94. if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
  95. return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
  96. Client_ID( Client ), channame );
  97. /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
  98. if( is_new_chan )
  99. {
  100. /* Erster User im Channel: Operator-Flag setzen */
  101. flags = "o";
  102. }
  103. else
  104. {
  105. /* Existierenden Channel suchen */
  106. chan = Channel_Search( channame );
  107. assert( chan != NULL );
  108. is_banned = Lists_Check(Channel_GetListBans(chan), target );
  109. is_invited = Lists_Check(Channel_GetListInvites(chan), target );
  110. /* Testen, ob Client gebanned ist */
  111. if(( is_banned == true) && ( is_invited == false ))
  112. {
  113. /* Client ist gebanned (und nicht invited): */
  114. IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
  115. /* Try next name, if any */
  116. channame = strchr(channame, ',');
  117. continue;
  118. }
  119. /* Ist der Channel "invite-only"? */
  120. if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
  121. {
  122. /* Channel ist "invite-only" und Client wurde nicht invited: */
  123. IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
  124. /* Try next name, if any */
  125. channame = strchr(channame, ',');
  126. continue;
  127. }
  128. /* Is the channel protected by a key? */
  129. if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
  130. {
  131. /* Bad channel key! */
  132. IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
  133. /* Try next name, if any */
  134. channame = strchr(channame, ',');
  135. continue;
  136. }
  137. /* Are there already too many members? */
  138. if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
  139. {
  140. /* Bad channel key! */
  141. IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
  142. /* Try next name, if any */
  143. channame = strchr(channame, ',');
  144. continue;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. /* Remote server: we don't need to know whether the
  151. * client is invited or not, but we have to make sure
  152. * that the "one shot" entries (generated by INVITE
  153. * commands) in this list become deleted when a user
  154. * joins a channel this way. */
  155. chan = Channel_Search( channame );
  156. if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
  157. }
  158. /* Channel joinen (und ggf. anlegen) */
  159. if( ! Channel_Join( target, channame ))
  160. {
  161. /* naechsten Namen ermitteln */
  162. channame = strchr(channame, ',');
  163. continue;
  164. }
  165. if( ! chan ) chan = Channel_Search( channame );
  166. assert( chan != NULL );
  167. /* Modes setzen (wenn vorhanden) */
  168. while( flags && *flags )
  169. {
  170. Channel_UserModeAdd( chan, target, *flags );
  171. flags++;
  172. }
  173. /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
  174. if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
  175. /* Muessen Modes an andere Server gemeldet werden? */
  176. strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
  177. if( modes[1] ) modes[0] = 0x7;
  178. else modes[0] = '\0';
  179. /* An andere Server weiterleiten */
  180. IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
  181. /* im Channel bekannt machen */
  182. IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
  183. if( modes[1] )
  184. {
  185. /* Modes im Channel bekannt machen */
  186. IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
  187. }
  188. if( Client_Type( Client ) == CLIENT_USER )
  189. {
  190. /* an Client bestaetigen */
  191. IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
  192. /* Send topic to client, if any */
  193. topic = Channel_Topic(chan);
  194. if (*topic) {
  195. IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
  196. Client_ID(Client), channame, topic);
  197. #ifndef STRICT_RFC
  198. IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
  199. Client_ID(Client), channame,
  200. Channel_TopicWho(chan),
  201. Channel_TopicTime(chan));
  202. #endif
  203. }
  204. /* Mitglieder an Client Melden */
  205. IRC_Send_NAMES( Client, chan );
  206. IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
  207. }
  208. /* next channel? */
  209. channame = channame_ptr;
  210. if (channame) {
  211. channame++;
  212. channame_ptr = strchr(channame, ',');
  213. if (channame_ptr) *channame_ptr = '\0';
  214. if (key_ptr) {
  215. key = ++key_ptr;
  216. key_ptr = strchr(key, ',');
  217. if (key_ptr) *key_ptr = '\0';
  218. }
  219. }
  220. }
  221. return CONNECTED;
  222. } /* IRC_JOIN */
  223. GLOBAL bool
  224. IRC_PART( CLIENT *Client, REQUEST *Req )
  225. {
  226. CLIENT *target;
  227. char *chan;
  228. assert( Client != NULL );
  229. assert( Req != NULL );
  230. /* Falsche Anzahl Parameter? */
  231. if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  232. /* Wer ist der Absender? */
  233. if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
  234. else target = Client;
  235. if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  236. /* Channel-Namen durchgehen */
  237. chan = strtok( Req->argv[0], "," );
  238. while( chan )
  239. {
  240. if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
  241. {
  242. /* naechsten Namen ermitteln */
  243. chan = strtok( NULL, "," );
  244. continue;
  245. }
  246. /* naechsten Namen ermitteln */
  247. chan = strtok( NULL, "," );
  248. }
  249. return CONNECTED;
  250. } /* IRC_PART */
  251. GLOBAL bool
  252. IRC_TOPIC( CLIENT *Client, REQUEST *Req )
  253. {
  254. CHANNEL *chan;
  255. CLIENT *from;
  256. char *topic;
  257. bool r;
  258. assert( Client != NULL );
  259. assert( Req != NULL );
  260. /* Falsche Anzahl Parameter? */
  261. if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  262. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  263. else from = Client;
  264. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  265. /* Welcher Channel? */
  266. chan = Channel_Search( Req->argv[0] );
  267. if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
  268. /* Ist der User Mitglied in dem Channel? */
  269. if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
  270. if( Req->argc == 1 )
  271. {
  272. /* Request actual topic */
  273. topic = Channel_Topic(chan);
  274. if (*topic) {
  275. r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
  276. Client_ID(Client), Channel_Name(chan), topic);
  277. #ifndef STRICT_RFC
  278. r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
  279. Client_ID(Client), Channel_Name(chan),
  280. Channel_TopicWho(chan),
  281. Channel_TopicTime(chan));
  282. #endif
  283. return r;
  284. }
  285. else
  286. return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
  287. Client_ID(from), Channel_Name(chan));
  288. }
  289. if( strchr( Channel_Modes( chan ), 't' ))
  290. {
  291. /* Topic Lock. Ist der User ein Channel Operator? */
  292. if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
  293. }
  294. /* Set new topic */
  295. Channel_SetTopic(chan, from, Req->argv[1]);
  296. Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
  297. Client_Mask(from), Channel_Name(chan),
  298. Req->argv[1][0] ? Req->argv[1] : "<none>");
  299. /* im Channel bekannt machen und an Server weiterleiten */
  300. IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
  301. IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
  302. if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
  303. else return CONNECTED;
  304. } /* IRC_TOPIC */
  305. /**
  306. * Handler for the IRC "LIST" command.
  307. * This implementation handles the local case as well as the forwarding of the
  308. * LIST command to other servers in the IRC network.
  309. */
  310. GLOBAL bool
  311. IRC_LIST( CLIENT *Client, REQUEST *Req )
  312. {
  313. char *pattern;
  314. CHANNEL *chan;
  315. CLIENT *from, *target;
  316. assert( Client != NULL );
  317. assert( Req != NULL );
  318. /* Bad number of prameters? */
  319. if( Req->argc > 2 )
  320. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  321. Client_ID( Client ), Req->command );
  322. if( Req->argc > 0 )
  323. pattern = strtok( Req->argv[0], "," );
  324. else
  325. pattern = "*";
  326. /* Get sender from prefix, if any */
  327. if( Client_Type( Client ) == CLIENT_SERVER )
  328. from = Client_Search( Req->prefix );
  329. else
  330. from = Client;
  331. if( ! from )
  332. return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
  333. Client_ID( Client ), Req->prefix );
  334. if( Req->argc == 2 )
  335. {
  336. /* Forward to other server? */
  337. target = Client_Search( Req->argv[1] );
  338. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
  339. return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
  340. Client_ID( Client ), Req->argv[1] );
  341. if( target != Client_ThisServer( ))
  342. {
  343. /* Target is indeed an other server, forward it! */
  344. return IRC_WriteStrClientPrefix( target, from,
  345. "LIST %s :%s", Client_ID( from ),
  346. Req->argv[1] );
  347. }
  348. }
  349. while( pattern )
  350. {
  351. /* Loop through all the channels */
  352. chan = Channel_First( );
  353. while( chan )
  354. {
  355. /* Check search pattern */
  356. if( Match( pattern, Channel_Name( chan )))
  357. {
  358. /* Gotcha! */
  359. if( ! strchr( Channel_Modes( chan ), 's' ) ||
  360. Channel_IsMemberOf( chan, from ))
  361. {
  362. if( ! IRC_WriteStrClient( from,
  363. RPL_LIST_MSG, Client_ID( from ),
  364. Channel_Name( chan ),
  365. Channel_MemberCount( chan ),
  366. Channel_Topic( chan )))
  367. return DISCONNECTED;
  368. }
  369. }
  370. chan = Channel_Next( chan );
  371. }
  372. /* Get next name ... */
  373. if( Req->argc > 0 )
  374. pattern = strtok( NULL, "," );
  375. else
  376. pattern = NULL;
  377. }
  378. return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
  379. } /* IRC_LIST */
  380. GLOBAL bool
  381. IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
  382. {
  383. char modes_add[COMMAND_LEN], l[16], *ptr;
  384. CLIENT *from;
  385. CHANNEL *chan;
  386. int arg_topic;
  387. assert( Client != NULL );
  388. assert( Req != NULL );
  389. /* Bad number of parameters? */
  390. if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  391. /* Compatibility kludge */
  392. if( Req->argc == 5 ) arg_topic = 4;
  393. else if( Req->argc == 3 ) arg_topic = 2;
  394. else arg_topic = -1;
  395. /* Search origin */
  396. from = Client_Search( Req->prefix );
  397. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  398. /* Search or create channel */
  399. chan = Channel_Search( Req->argv[0] );
  400. if( ! chan ) chan = Channel_Create( Req->argv[0] );
  401. if( ! chan ) return CONNECTED;
  402. if( Req->argv[1][0] == '+' )
  403. {
  404. ptr = Channel_Modes( chan );
  405. if( ! *ptr )
  406. {
  407. /* OK, this channel doesn't have modes jet, set the received ones: */
  408. Channel_SetModes( chan, &Req->argv[1][1] );
  409. if( Req->argc == 5 )
  410. {
  411. if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
  412. if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
  413. }
  414. else
  415. {
  416. /* Delete modes which we never want to inherit */
  417. Channel_ModeDel( chan, 'l' );
  418. Channel_ModeDel( chan, 'k' );
  419. }
  420. strcpy( modes_add, "" );
  421. ptr = Channel_Modes( chan );
  422. while( *ptr )
  423. {
  424. if( *ptr == 'l' )
  425. {
  426. snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
  427. strlcat( modes_add, l, sizeof( modes_add ));
  428. }
  429. if( *ptr == 'k' )
  430. {
  431. strlcat( modes_add, " ", sizeof( modes_add ));
  432. strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
  433. }
  434. ptr++;
  435. }
  436. /* Inform members of this channel */
  437. IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
  438. }
  439. }
  440. else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
  441. if( arg_topic > 0 )
  442. {
  443. /* We got a topic */
  444. ptr = Channel_Topic( chan );
  445. if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
  446. {
  447. /* OK, there is no topic jet */
  448. Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
  449. IRC_WriteStrChannelPrefix(Client, chan, from, false,
  450. "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
  451. }
  452. }
  453. /* Forward CHANINFO to other serevrs */
  454. if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
  455. else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
  456. else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
  457. return CONNECTED;
  458. } /* IRC_CHANINFO */
  459. /* -eof- */