irc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2004 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 commands
  12. */
  13. #include "portab.h"
  14. static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "ngircd.h"
  20. #include "resolve.h"
  21. #include "conn-func.h"
  22. #include "conf.h"
  23. #include "client.h"
  24. #include "channel.h"
  25. #include "defines.h"
  26. #include "irc-write.h"
  27. #include "log.h"
  28. #include "messages.h"
  29. #include "parse.h"
  30. #include "exp.h"
  31. #include "irc.h"
  32. static char *Option_String PARAMS(( CONN_ID Idx ));
  33. GLOBAL bool
  34. IRC_ERROR( CLIENT *Client, REQUEST *Req )
  35. {
  36. assert( Client != NULL );
  37. assert( Req != NULL );
  38. if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
  39. else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
  40. return CONNECTED;
  41. } /* IRC_ERROR */
  42. /**
  43. * Kill client on request.
  44. * This function implements the IRC command "KILL" wich is used to selectively
  45. * disconnect clients. It can be used by IRC operators and servers, for example
  46. * to "solve" nick collisions after netsplits.
  47. * Please note that this function is also called internally, without a real
  48. * KILL command beeing received over the network! Client is Client_ThisServer()
  49. * in this case. */
  50. GLOBAL bool
  51. IRC_KILL( CLIENT *Client, REQUEST *Req )
  52. {
  53. CLIENT *prefix, *c;
  54. char reason[COMMAND_LEN], *msg;
  55. CONN_ID my_conn, conn;
  56. assert( Client != NULL );
  57. assert( Req != NULL );
  58. if(( Client_Type( Client ) != CLIENT_SERVER ) &&
  59. ( ! Client_OperByMe( Client )))
  60. {
  61. /* The originator of the KILL is neither an IRC operator of
  62. * this server nor a server. */
  63. return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
  64. Client_ID( Client ));
  65. }
  66. if( Req->argc != 2 )
  67. {
  68. /* This command requires exactly 2 parameters! */
  69. return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
  70. Client_ID( Client ), Req->command );
  71. }
  72. if( Req->prefix ) prefix = Client_Search( Req->prefix );
  73. else prefix = Client;
  74. if( ! prefix )
  75. {
  76. Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
  77. Req->prefix );
  78. prefix = Client_ThisServer( );
  79. }
  80. if( Client != Client_ThisServer( ))
  81. {
  82. /* This is a "real" KILL received from the network. */
  83. Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
  84. Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
  85. }
  86. /* Build reason string */
  87. if( Client_Type( Client ) == CLIENT_USER )
  88. {
  89. /* Prefix the "reason" if the originator is a regular user,
  90. * so users can't spoof KILLs of servers. */
  91. snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
  92. Client_ID( Client ), Req->argv[1] );
  93. }
  94. else
  95. strlcpy( reason, Req->argv[1], sizeof( reason ));
  96. /* Inform other servers */
  97. IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
  98. Req->argv[0], reason );
  99. /* Save ID of this connection */
  100. my_conn = Client_Conn( Client );
  101. /* Do we host such a client? */
  102. c = Client_Search( Req->argv[0] );
  103. if( c )
  104. {
  105. if(( Client_Type( c ) != CLIENT_USER ) &&
  106. ( Client_Type( c ) != CLIENT_GOTNICK ))
  107. {
  108. /* Target of this KILL is not a regular user, this is
  109. * invalid! So we ignore this case if we received a
  110. * regular KILL from the network and try to kill the
  111. * client/connection anyway (but log an error!) if the
  112. * origin is the local server. */
  113. if( Client != Client_ThisServer( ))
  114. {
  115. /* Invalid KILL received from remote */
  116. if( Client_Type( c ) == CLIENT_SERVER )
  117. msg = ERR_CANTKILLSERVER_MSG;
  118. else
  119. msg = ERR_NOPRIVILEGES_MSG;
  120. return IRC_WriteStrClient( Client, msg,
  121. Client_ID( Client ));
  122. }
  123. Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
  124. Client_Type( c ), Req->argv[0] );
  125. }
  126. /* Kill client NOW! */
  127. conn = Client_Conn( c );
  128. Client_Destroy( c, NULL, reason, false );
  129. if( conn > NONE )
  130. Conn_Close( conn, NULL, reason, true );
  131. }
  132. else
  133. Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
  134. /* Are we still connected or were we killed, too? */
  135. if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
  136. return CONNECTED;
  137. else
  138. return DISCONNECTED;
  139. } /* IRC_KILL */
  140. GLOBAL bool
  141. IRC_NOTICE( CLIENT *Client, REQUEST *Req )
  142. {
  143. CLIENT *to, *from;
  144. CHANNEL *chan;
  145. assert( Client != NULL );
  146. assert( Req != NULL );
  147. if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
  148. /* Falsche Anzahl Parameter? */
  149. if( Req->argc != 2 ) return CONNECTED;
  150. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  151. else from = Client;
  152. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  153. to = Client_Search( Req->argv[0] );
  154. if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
  155. {
  156. /* Okay, Ziel ist ein User */
  157. return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
  158. }
  159. else
  160. {
  161. chan = Channel_Search(Req->argv[0]);
  162. if (chan)
  163. return Channel_Notice(chan, from, Client, Req->argv[1]);
  164. }
  165. return CONNECTED;
  166. } /* IRC_NOTICE */
  167. GLOBAL bool
  168. IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
  169. {
  170. CLIENT *cl, *from;
  171. CHANNEL *chan;
  172. assert( Client != NULL );
  173. assert( Req != NULL );
  174. /* Falsche Anzahl Parameter? */
  175. if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  176. if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
  177. if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  178. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  179. else from = Client;
  180. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  181. cl = Client_Search( Req->argv[0] );
  182. if( cl )
  183. {
  184. /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
  185. if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
  186. /* Okay, Ziel ist ein User */
  187. if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
  188. {
  189. /* Ziel-User ist AWAY: Meldung verschicken */
  190. if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
  191. }
  192. /* Text senden */
  193. if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
  194. return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
  195. }
  196. chan = Channel_Search( Req->argv[0] );
  197. if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
  198. return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
  199. } /* IRC_PRIVMSG */
  200. GLOBAL bool
  201. IRC_TRACE( CLIENT *Client, REQUEST *Req )
  202. {
  203. CLIENT *from, *target, *c;
  204. CONN_ID idx, idx2;
  205. char user[CLIENT_USER_LEN];
  206. assert( Client != NULL );
  207. assert( Req != NULL );
  208. /* Bad number of arguments? */
  209. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  210. /* Search sender */
  211. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  212. else from = Client;
  213. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  214. /* Search target */
  215. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  216. else target = Client_ThisServer( );
  217. /* Forward command to other server? */
  218. if( target != Client_ThisServer( ))
  219. {
  220. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  221. /* Send RPL_TRACELINK back to initiator */
  222. idx = Client_Conn( Client ); assert( idx > NONE );
  223. idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
  224. if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
  225. /* Forward command */
  226. IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
  227. return CONNECTED;
  228. }
  229. /* Infos about all connected servers */
  230. c = Client_First( );
  231. while( c )
  232. {
  233. if( Client_Conn( c ) > NONE )
  234. {
  235. /* Local client */
  236. if( Client_Type( c ) == CLIENT_SERVER )
  237. {
  238. /* Server link */
  239. strlcpy( user, Client_User( c ), sizeof( user ));
  240. if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
  241. if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
  242. }
  243. if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
  244. {
  245. /* IRC Operator */
  246. if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
  247. }
  248. }
  249. c = Client_Next( c );
  250. }
  251. IRC_SetPenalty( Client, 3 );
  252. return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
  253. } /* IRC_TRACE */
  254. GLOBAL bool
  255. IRC_HELP( CLIENT *Client, REQUEST *Req )
  256. {
  257. COMMAND *cmd;
  258. assert( Client != NULL );
  259. assert( Req != NULL );
  260. /* Bad number of arguments? */
  261. if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  262. cmd = Parse_GetCommandStruct( );
  263. while( cmd->name )
  264. {
  265. if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
  266. cmd++;
  267. }
  268. IRC_SetPenalty( Client, 2 );
  269. return CONNECTED;
  270. } /* IRC_HELP */
  271. static char *
  272. Option_String( CONN_ID Idx )
  273. {
  274. static char option_txt[8];
  275. UINT16 options;
  276. options = Conn_Options(Idx);
  277. strcpy(option_txt, "F"); /* No idea what this means, but the
  278. * original ircd sends it ... */
  279. #ifdef ZLIB
  280. if(options & CONN_ZIP) /* zlib compression supported. */
  281. strcat(option_txt, "z");
  282. #endif
  283. return option_txt;
  284. } /* Option_String */
  285. /* -eof- */