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