]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Cumulative Message Patch
[ngircd-alex.git] / src / ngircd / irc.c
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
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "defines.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "match.h"
34 #include "messages.h"
35 #include "parse.h"
36
37 #include "exp.h"
38 #include "irc.h"
39
40
41 static char *Option_String PARAMS((CONN_ID Idx));
42 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType, bool SendErrors));
43 static bool Send_Message_Mask PARAMS((CLIENT *from, char *targetMask, char *message, bool SendErrors));
44 static bool MatchCaseInsensitive PARAMS((const char *pattern, const char *searchme));
45
46
47 GLOBAL bool
48 IRC_ERROR( CLIENT *Client, REQUEST *Req )
49 {
50         assert( Client != NULL );
51         assert( Req != NULL );
52
53         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
54         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
55
56         return CONNECTED;
57 } /* IRC_ERROR */
58
59
60 /**
61  * Kill client on request.
62  * This function implements the IRC command "KILL" wich is used to selectively
63  * disconnect clients. It can be used by IRC operators and servers, for example
64  * to "solve" nick collisions after netsplits.
65  * Please note that this function is also called internally, without a real
66  * KILL command beeing received over the network! Client is Client_ThisServer()
67  * in this case. */
68 GLOBAL bool
69 IRC_KILL( CLIENT *Client, REQUEST *Req )
70 {
71         CLIENT *prefix, *c;
72         char reason[COMMAND_LEN], *msg;
73         CONN_ID my_conn, conn;
74
75         assert( Client != NULL );
76         assert( Req != NULL );
77
78         if(( Client_Type( Client ) != CLIENT_SERVER ) &&
79            ( ! Client_OperByMe( Client )))
80         {
81                 /* The originator of the KILL is neither an IRC operator of
82                  * this server nor a server. */
83                 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
84                                            Client_ID( Client ));
85         }
86
87         if( Req->argc != 2 )
88         {
89                 /* This command requires exactly 2 parameters! */
90                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
91                                            Client_ID( Client ), Req->command );
92         }
93
94         if( Req->prefix ) prefix = Client_Search( Req->prefix );
95         else prefix = Client;
96         if( ! prefix )
97         {
98                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
99                      Req->prefix );
100                 prefix = Client_ThisServer( );
101         }
102
103         if( Client != Client_ThisServer( ))
104         {
105                 /* This is a "real" KILL received from the network. */
106                 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
107                      Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
108         }
109
110         /* Build reason string */
111         if( Client_Type( Client ) == CLIENT_USER )
112         {
113                 /* Prefix the "reason" if the originator is a regular user,
114                  * so users can't spoof KILLs of servers. */
115                 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
116                           Client_ID( Client ), Req->argv[1] );
117         }
118         else
119                 strlcpy( reason, Req->argv[1], sizeof( reason ));
120
121         /* Inform other servers */
122         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
123                                    Req->argv[0], reason );
124
125         /* Save ID of this connection */
126         my_conn = Client_Conn( Client );
127
128         /* Do we host such a client? */
129         c = Client_Search( Req->argv[0] );
130         if( c )
131         {
132                 if(( Client_Type( c ) != CLIENT_USER ) &&
133                    ( Client_Type( c ) != CLIENT_GOTNICK ))
134                 {
135                         /* Target of this KILL is not a regular user, this is
136                          * invalid! So we ignore this case if we received a
137                          * regular KILL from the network and try to kill the
138                          * client/connection anyway (but log an error!) if the
139                          * origin is the local server. */
140
141                         if( Client != Client_ThisServer( ))
142                         {
143                                 /* Invalid KILL received from remote */
144                                 if( Client_Type( c ) == CLIENT_SERVER )
145                                         msg = ERR_CANTKILLSERVER_MSG;
146                                 else
147                                         msg = ERR_NOPRIVILEGES_MSG;
148                                 return IRC_WriteStrClient( Client, msg,
149                                         Client_ID( Client ));
150                         }
151
152                         Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
153                              Client_Type( c ), Req->argv[0] );
154                 }
155
156                 /* Kill client NOW! */
157                 conn = Client_Conn( c );
158                 Client_Destroy( c, NULL, reason, false );
159                 if( conn > NONE )
160                         Conn_Close( conn, NULL, reason, true );
161         }
162         else
163                 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
164
165         /* Are we still connected or were we killed, too? */
166         if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
167                 return CONNECTED;
168         else
169                 return DISCONNECTED;
170 } /* IRC_KILL */
171
172
173 GLOBAL bool
174 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
175 {
176         return Send_Message(Client, Req, CLIENT_USER, false);
177 } /* IRC_NOTICE */
178
179
180 /**
181  * Handler for the IRC command PRIVMSG.
182  */
183 GLOBAL bool
184 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
185 {
186         return Send_Message(Client, Req, CLIENT_USER, true);
187 } /* IRC_PRIVMSG */
188
189
190 /**
191  * Handler for the IRC command SQUERY.
192  */
193 GLOBAL bool
194 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
195 {
196         return Send_Message(Client, Req, CLIENT_SERVICE, true);
197 } /* IRC_SQUERY */
198
199
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
207         assert( Client != NULL );
208         assert( Req != NULL );
209
210         /* Bad number of arguments? */
211         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
212
213         /* Search sender */
214         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
215         else from = Client;
216         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
217
218         /* Search target */
219         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
220         else target = Client_ThisServer( );
221         
222         /* Forward command to other server? */
223         if( target != Client_ThisServer( ))
224         {
225                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
226
227                 /* Send RPL_TRACELINK back to initiator */
228                 idx = Client_Conn( Client ); assert( idx > NONE );
229                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
230                 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;
231
232                 /* Forward command */
233                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
234                 return CONNECTED;
235         }
236
237         /* Infos about all connected servers */
238         c = Client_First( );
239         while( c )
240         {
241                 if( Client_Conn( c ) > NONE )
242                 {
243                         /* Local client */
244                         if( Client_Type( c ) == CLIENT_SERVER )
245                         {
246                                 /* Server link */
247                                 strlcpy( user, Client_User( c ), sizeof( user ));
248                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
249                                 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;
250                         }
251                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
252                         {
253                                 /* IRC Operator */
254                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
255                         }
256                 }
257                 c = Client_Next( c );
258         }
259
260         IRC_SetPenalty( Client, 3 );
261         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
262 } /* IRC_TRACE */
263
264
265 GLOBAL bool
266 IRC_HELP( CLIENT *Client, REQUEST *Req )
267 {
268         COMMAND *cmd;
269
270         assert( Client != NULL );
271         assert( Req != NULL );
272
273         /* Bad number of arguments? */
274         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
275
276         cmd = Parse_GetCommandStruct( );
277         while( cmd->name )
278         {
279                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
280                 cmd++;
281         }
282         
283         IRC_SetPenalty( Client, 2 );
284         return CONNECTED;
285 } /* IRC_HELP */
286
287
288 static char *
289 Option_String( CONN_ID Idx )
290 {
291         static char option_txt[8];
292         UINT16 options;
293
294         options = Conn_Options(Idx);
295
296         strcpy(option_txt, "F");        /* No idea what this means, but the
297                                          * original ircd sends it ... */
298 #ifdef ZLIB
299         if(options & CONN_ZIP)          /* zlib compression supported. */
300                 strcat(option_txt, "z");
301 #endif
302
303         return option_txt;
304 } /* Option_String */
305
306
307 static bool
308 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
309 {
310         CLIENT *cl, *from;
311         CHANNEL *chan;
312         char *targetList = Req->argv[0];
313         char *currentTarget = Req->argv[0];
314         unsigned int targetCount = 1;
315
316         assert( Client != NULL );
317         assert( Req != NULL );
318
319         if (Req->argc == 0) {
320                 if (!SendErrors)
321                         return true;
322                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
323                                           Client_ID(Client), Req->command);
324         }
325         if (Req->argc == 1) {
326                 if (!SendErrors)
327                         return true;
328                 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
329                                           Client_ID(Client));
330         }
331         if (Req->argc > 2) {
332                 if (!SendErrors)
333                         return true;
334                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
335                                           Client_ID(Client), Req->command);
336         }
337
338         if (Client_Type(Client) == CLIENT_SERVER)
339                 from = Client_Search(Req->prefix);
340         else
341                 from = Client;
342         if (!from) {
343                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
344                                           Client_ID(Client), Req->prefix);
345         }
346
347         while (*targetList) {
348                 if (*targetList == ',') {
349                         *targetList = '\0';
350                         targetCount++;
351                 }
352                 targetList++;
353         }
354
355         while (targetCount > 0) {
356                 if (strchr(currentTarget, '!') == NULL)
357                         cl = Client_Search(currentTarget);
358                 else
359                         cl = NULL;
360                 if (cl == NULL) {
361                         char target[513]; // max mesage length plus null terminator
362                         char * nick = NULL;
363                         char * user = NULL;
364                         char * host = NULL;
365                         char * server = NULL;
366
367                         strncpy(target, currentTarget, 512);
368                         target[512] = '\0';
369                         server = strchr(target, '@');
370                         if (server) {
371                                 *server = '\0';
372                                 server++;
373                         }
374                         host = strchr(target, '%');
375                         if (host) {
376                                 *host = '\0';
377                                 host++;
378                         }
379                         user = strchr(target, '!');
380                         if (user) {
381                                 *user = '\0';
382                                 user++;
383                                 nick = target;
384                                 host = server; // <msgto> form: nick!user@host
385                         } else {
386                                 user = target;
387                         }
388
389                         if (user != NULL) {
390                                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
391                                         if (Client_Type(cl) != CLIENT_USER)
392                                                 continue;
393                                         if (nick != NULL) {
394                                                 if (strcmp(nick, Client_ID(cl)) == 0 && strcmp(user, Client_User(cl)) == 0 && strcasecmp(host, Client_Hostname(cl)) == 0)
395                                                         break;
396                                                 else
397                                                         continue;
398                                         }
399                                         if (strcasecmp(user, Client_User(cl)) != 0)
400                                                 continue;
401                                         if (host != NULL && strcasecmp(host, Client_Hostname(cl)) != 0)
402                                                 continue;
403                                         if (server != NULL && strcasecmp(server, Client_ID(Client_Introducer(cl))) != 0)
404                                                 continue;
405                                         break;
406                                 }
407                         }
408                 }
409
410                 if (cl) {
411                         /* Target is a user, enforce type */
412                         if (Client_Type(cl) != ForceType) {
413                                 if (!SendErrors)
414                                         return true;
415                                 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
416                                                           Client_ID(from),
417                                                           currentTarget))
418                                         return false;
419                         } else if ((Client_Type(Client) != CLIENT_SERVER
420                                                 && (strchr(Client_Modes(cl), 'a')))) {
421                                 /* Target is away */
422                                 if (!SendErrors)
423                                         return true;
424                                 if (!IRC_WriteStrClient
425                                     (from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl),
426                                      Client_Away(cl)))
427                                         return DISCONNECTED;
428                         }       if (Client_Conn(from) > NONE) {
429                                 Conn_UpdateIdle(Client_Conn(from));
430                         }
431                         if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
432                                                 Client_ID(cl), Req->argv[1]))
433                                 return false;
434                 } else if (strchr("$#", currentTarget[0]) && strchr(currentTarget, '.')) {
435                         if (!Send_Message_Mask(from, currentTarget, Req->argv[1], SendErrors))
436                                 return false;
437                 } else if ((chan = Channel_Search(currentTarget))) {
438                                 if (!Channel_Write(chan, from, Client, Req->argv[1]))
439                                         return false;
440                 } else {
441                         if (!SendErrors)
442                                 return true;
443                         if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
444                                                 Client_ID(from), currentTarget))
445                                 return false;
446                 }
447
448                 while (*currentTarget)
449                         currentTarget++;
450
451                 currentTarget++;
452                 targetCount--;
453         }
454
455         return CONNECTED;
456
457 } /* Send_Message */
458
459
460 static bool
461 Send_Message_Mask(CLIENT * from, char * targetMask, char * message, bool SendErrors)
462 {
463         CLIENT *cl;
464         bool client_match;
465         char *mask = targetMask + 1;
466
467         cl = NULL;
468
469         if (strchr(Client_Modes(from), 'o') == NULL) {
470                 if (!SendErrors)
471                         return true;
472                 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG, Client_ID(from));
473         }
474
475         if (targetMask[0] == '#') {
476                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
477                         if (Client_Type(cl) != CLIENT_USER)
478                                 continue;
479                         client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
480                         if (client_match)
481                                 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s", Client_ID(cl), message))
482                                         return false;
483                 }
484         } else {
485                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
486                         if (Client_Type(cl) != CLIENT_USER)
487                                 continue;
488                         client_match = MatchCaseInsensitive(mask, Client_ID(Client_Introducer(cl)));
489                         if (client_match)
490                                 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s", Client_ID(cl), message))
491                                         return false;
492                 }
493         }
494         return CONNECTED;
495 } /* Send_Message_Mask */
496
497
498 static bool
499 MatchCaseInsensitive(const char *pattern, const char *searchme)
500 {
501         char haystack[COMMAND_LEN];
502
503         strlcpy(haystack, searchme, sizeof(haystack));
504
505         ngt_LowerStr(haystack);
506
507         return Match(pattern, haystack);
508 }
509
510 /* -eof- */