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