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