irc.c 10 KB

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