irc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.131 2006/07/23 14:55:40 alex 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. assert( Client != NULL );
  145. assert( Req != NULL );
  146. if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
  147. /* Falsche Anzahl Parameter? */
  148. if( Req->argc != 2 ) return CONNECTED;
  149. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  150. else from = Client;
  151. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  152. to = Client_Search( Req->argv[0] );
  153. if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
  154. {
  155. /* Okay, Ziel ist ein User */
  156. return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
  157. }
  158. else return CONNECTED;
  159. } /* IRC_NOTICE */
  160. GLOBAL bool
  161. IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
  162. {
  163. CLIENT *cl, *from;
  164. CHANNEL *chan;
  165. assert( Client != NULL );
  166. assert( Req != NULL );
  167. /* Falsche Anzahl Parameter? */
  168. if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  169. if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
  170. if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
  171. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  172. else from = Client;
  173. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  174. cl = Client_Search( Req->argv[0] );
  175. if( cl )
  176. {
  177. /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
  178. if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
  179. /* Okay, Ziel ist ein User */
  180. if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
  181. {
  182. /* Ziel-User ist AWAY: Meldung verschicken */
  183. if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
  184. }
  185. /* Text senden */
  186. if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
  187. return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
  188. }
  189. chan = Channel_Search( Req->argv[0] );
  190. if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
  191. return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
  192. } /* IRC_PRIVMSG */
  193. GLOBAL bool
  194. IRC_TRACE( CLIENT *Client, REQUEST *Req )
  195. {
  196. CLIENT *from, *target, *c;
  197. CONN_ID idx, idx2;
  198. char user[CLIENT_USER_LEN];
  199. assert( Client != NULL );
  200. assert( Req != NULL );
  201. /* Bad number of arguments? */
  202. if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  203. /* Search sender */
  204. if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
  205. else from = Client;
  206. if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
  207. /* Search target */
  208. if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
  209. else target = Client_ThisServer( );
  210. /* Forward command to other server? */
  211. if( target != Client_ThisServer( ))
  212. {
  213. if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
  214. /* Send RPL_TRACELINK back to initiator */
  215. idx = Client_Conn( Client ); assert( idx > NONE );
  216. idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
  217. 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;
  218. /* Forward command */
  219. IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
  220. return CONNECTED;
  221. }
  222. /* Infos about all connected servers */
  223. c = Client_First( );
  224. while( c )
  225. {
  226. if( Client_Conn( c ) > NONE )
  227. {
  228. /* Local client */
  229. if( Client_Type( c ) == CLIENT_SERVER )
  230. {
  231. /* Server link */
  232. strlcpy( user, Client_User( c ), sizeof( user ));
  233. if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
  234. 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;
  235. }
  236. if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
  237. {
  238. /* IRC Operator */
  239. if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
  240. }
  241. }
  242. c = Client_Next( c );
  243. }
  244. IRC_SetPenalty( Client, 3 );
  245. return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
  246. } /* IRC_TRACE */
  247. GLOBAL bool
  248. IRC_HELP( CLIENT *Client, REQUEST *Req )
  249. {
  250. COMMAND *cmd;
  251. assert( Client != NULL );
  252. assert( Req != NULL );
  253. /* Bad number of arguments? */
  254. if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
  255. cmd = Parse_GetCommandStruct( );
  256. while( cmd->name )
  257. {
  258. if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
  259. cmd++;
  260. }
  261. IRC_SetPenalty( Client, 2 );
  262. return CONNECTED;
  263. } /* IRC_HELP */
  264. static char *
  265. Option_String( CONN_ID Idx )
  266. {
  267. static char option_txt[8];
  268. UINT16 options;
  269. options = Conn_Options(Idx);
  270. strcpy(option_txt, "F"); /* No idea what this means, but the
  271. * original ircd sends it ... */
  272. #ifdef ZLIB
  273. if(options & CONN_ZIP) /* zlib compression supported. */
  274. strcat(option_txt, "z");
  275. #endif
  276. return option_txt;
  277. } /* Option_String */
  278. /* -eof- */