]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
IRC_HELP(): Code cleanup
[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 /**
308  * Handler for the IRC "HELP" command.
309  *
310  * @param Client The client from which this command has been received.
311  * @param Req Request structure with prefix and all parameters.
312  * @return CONNECTED or DISCONNECTED.
313  */
314 GLOBAL bool
315 IRC_HELP(CLIENT *Client, REQUEST *Req)
316 {
317         COMMAND *cmd;
318
319         assert(Client != NULL);
320         assert(Req != NULL);
321
322         /* Bad number of arguments? */
323         if (Req->argc > 0)
324                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
325                                           Client_ID(Client), Req->command);
326
327         cmd = Parse_GetCommandStruct();
328         while(cmd->name) {
329                 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
330                                         Client_ID(Client), cmd->name))
331                         return DISCONNECTED;
332                 cmd++;
333         }
334
335         IRC_SetPenalty(Client, 2);
336         return CONNECTED;
337 } /* IRC_HELP */
338
339
340 static char *
341 #ifdef ZLIB
342 Option_String(CONN_ID Idx)
343 #else
344 Option_String(UNUSED CONN_ID Idx)
345 #endif
346 {
347         static char option_txt[8];
348 #ifdef ZLIB
349         UINT16 options;
350
351         options = Conn_Options(Idx);
352 #endif
353
354         strcpy(option_txt, "F");        /* No idea what this means, but the
355                                          * original ircd sends it ... */
356 #ifdef ZLIB
357         if(options & CONN_ZIP)          /* zlib compression supported. */
358                 strcat(option_txt, "z");
359 #endif
360
361         return option_txt;
362 } /* Option_String */
363
364
365 static bool
366 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
367 {
368         CLIENT *cl, *from;
369         CL2CHAN *cl2chan;
370         CHANNEL *chan;
371         char *currentTarget = Req->argv[0];
372         char *lastCurrentTarget = NULL;
373         char *message = NULL;
374
375         assert(Client != NULL);
376         assert(Req != NULL);
377
378         if (Req->argc == 0) {
379                 if (!SendErrors)
380                         return CONNECTED;
381                 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
382                                           Client_ID(Client), Req->command);
383         }
384         if (Req->argc == 1) {
385                 if (!SendErrors)
386                         return CONNECTED;
387                 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
388                                           Client_ID(Client));
389         }
390         if (Req->argc > 2) {
391                 if (!SendErrors)
392                         return CONNECTED;
393                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
394                                           Client_ID(Client), Req->command);
395         }
396
397         if (Client_Type(Client) == CLIENT_SERVER)
398                 from = Client_Search(Req->prefix);
399         else
400                 from = Client;
401         if (!from)
402                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
403                                           Client_ID(Client), Req->prefix);
404
405 #ifdef ICONV
406         if (Client_Conn(Client) > NONE)
407                 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
408         else
409 #endif
410                 message = Req->argv[1];
411
412         /* handle msgtarget = msgto *("," msgto) */
413         currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
414         ngt_UpperStr(Req->command);
415
416         while (currentTarget) {
417                 /* Check for and handle valid <msgto> of form:
418                  * RFC 2812 2.3.1:
419                  *   msgto =  channel / ( user [ "%" host ] "@" servername )
420                  *   msgto =/ ( user "%" host ) / targetmask
421                  *   msgto =/ nickname / ( nickname "!" user "@" host )
422                  */
423                 if (strchr(currentTarget, '!') == NULL)
424                         /* nickname */
425                         cl = Client_Search(currentTarget);
426                 else
427                         cl = NULL;
428
429                 if (cl == NULL) {
430                         /* If currentTarget isn't a nickname check for:
431                          * user ["%" host] "@" servername
432                          * user "%" host
433                          * nickname "!" user "@" host
434                          */
435                         char target[COMMAND_LEN];
436                         char * nick = NULL;
437                         char * user = NULL;
438                         char * host = NULL;
439                         char * server = NULL;
440
441                         strlcpy(target, currentTarget, COMMAND_LEN);
442                         server = strchr(target, '@');
443                         if (server) {
444                                 *server = '\0';
445                                 server++;
446                         }
447                         host = strchr(target, '%');
448                         if (host) {
449                                 *host = '\0';
450                                 host++;
451                         }
452                         user = strchr(target, '!');
453                         if (user) {
454                                 /* msgto form: nick!user@host */
455                                 *user = '\0';
456                                 user++;
457                                 nick = target;
458                                 host = server; /* not "@server" but "@host" */
459                         } else {
460                                 user = target;
461                         }
462
463                         for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
464                                 if (Client_Type(cl) != CLIENT_USER &&
465                                     Client_Type(cl) != CLIENT_SERVICE)
466                                         continue;
467                                 if (nick != NULL && host != NULL) {
468                                         if (strcasecmp(nick, Client_ID(cl)) == 0 &&
469                                             strcasecmp(user, Client_User(cl)) == 0 &&
470                                             strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
471                                                 break;
472                                         else
473                                                 continue;
474                                 }
475                                 if (strcasecmp(user, Client_User(cl)) != 0)
476                                         continue;
477                                 if (host != NULL && strcasecmp(host,
478                                                 Client_HostnameDisplayed(cl)) != 0)
479                                         continue;
480                                 if (server != NULL && strcasecmp(server,
481                                                 Client_ID(Client_Introducer(cl))) != 0)
482                                         continue;
483                                 break;
484                         }
485                 }
486
487                 if (cl) {
488                         /* Target is a user, enforce type */
489 #ifndef STRICT_RFC
490                         if (Client_Type(cl) != ForceType &&
491                             !(ForceType == CLIENT_USER &&
492                               (Client_Type(cl) == CLIENT_USER ||
493                                Client_Type(cl) == CLIENT_SERVICE))) {
494 #else
495                         if (Client_Type(cl) != ForceType) {
496 #endif
497                                 if (SendErrors && !IRC_WriteStrClient(
498                                     from, ERR_NOSUCHNICK_MSG,Client_ID(from),
499                                     currentTarget))
500                                         return DISCONNECTED;
501                                 goto send_next_target;
502                         }
503
504 #ifndef STRICT_RFC
505                         if (ForceType == CLIENT_SERVICE &&
506                             (Conn_Options(Client_Conn(Client_NextHop(cl)))
507                              & CONN_RFC1459)) {
508                                 /* SQUERY command but RFC 1459 link: convert
509                                  * request to PRIVMSG command */
510                                 Req->command = "PRIVMSG";
511                         }
512 #endif
513                         if (Client_HasMode(cl, 'b') &&
514                             !Client_HasMode(from, 'R') &&
515                             !Client_HasMode(from, 'o') &&
516                             !(Client_Type(from) == CLIENT_SERVER) &&
517                             !(Client_Type(from) == CLIENT_SERVICE)) {
518                                 if (SendErrors && !IRC_WriteStrClient(from,
519                                                 ERR_NONONREG_MSG,
520                                                 Client_ID(from), Client_ID(cl)))
521                                         return DISCONNECTED;
522                                 goto send_next_target;
523                         }
524
525                         if (Client_HasMode(cl, 'C')) {
526                                 cl2chan = Channel_FirstChannelOf(cl);
527                                 while (cl2chan) {
528                                         chan = Channel_GetChannel(cl2chan);
529                                         if (Channel_IsMemberOf(chan, from))
530                                                 break;
531                                         cl2chan = Channel_NextChannelOf(cl, cl2chan);
532                                 }
533                                 if (!cl2chan) {
534                                         if (SendErrors && !IRC_WriteStrClient(
535                                             from, ERR_NOTONSAMECHANNEL_MSG,
536                                             Client_ID(from), Client_ID(cl)))
537                                                 return DISCONNECTED;
538                                         goto send_next_target;
539                                 }
540                         }
541
542                         if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
543                             && strchr(Client_Modes(cl), 'a')) {
544                                 /* Target is away */
545                                 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
546                                                         Client_ID(from),
547                                                         Client_ID(cl),
548                                                         Client_Away(cl)))
549                                         return DISCONNECTED;
550                         }
551                         if (Client_Conn(from) > NONE) {
552                                 Conn_UpdateIdle(Client_Conn(from));
553                         }
554                         if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
555                                                       Req->command, Client_ID(cl),
556                                                       message))
557                                 return DISCONNECTED;
558                 } else if (ForceType != CLIENT_SERVICE
559                            && (chan = Channel_Search(currentTarget))) {
560                         if (!Channel_Write(chan, from, Client, Req->command,
561                                            SendErrors, message))
562                                         return DISCONNECTED;
563                 } else if (ForceType != CLIENT_SERVICE
564                         /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
565                            && strchr("$#", currentTarget[0])
566                            && strchr(currentTarget, '.')) {
567                         /* targetmask */
568                         if (!Send_Message_Mask(from, Req->command, currentTarget,
569                                                message, SendErrors))
570                                 return DISCONNECTED;
571                 } else {
572                         if (!SendErrors)
573                                 return CONNECTED;
574                         if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
575                                                 Client_ID(from), currentTarget))
576                                 return DISCONNECTED;
577                 }
578
579         send_next_target:
580                 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
581                 if (currentTarget)
582                         Conn_SetPenalty(Client_Conn(Client), 1);
583         }
584
585         return CONNECTED;
586 } /* Send_Message */
587
588
589 static bool
590 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
591                   char * message, bool SendErrors)
592 {
593         CLIENT *cl;
594         bool client_match;
595         char *mask = targetMask + 1;
596         const char *check_wildcards;
597
598         cl = NULL;
599
600         if (strchr(Client_Modes(from), 'o') == NULL) {
601                 if (!SendErrors)
602                         return true;
603                 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
604                                           Client_ID(from));
605         }
606
607         /*
608          * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
609          * dot (".") and no wildcards ("*", "?") following the last one.
610          */
611         check_wildcards = strrchr(targetMask, '.');
612         assert(check_wildcards != NULL);
613         if (check_wildcards &&
614                 check_wildcards[strcspn(check_wildcards, "*?")])
615         {
616                 if (!SendErrors)
617                         return true;
618                 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
619         }
620
621         /* #: hostmask, see RFC 2812, sec. 3.3.1 */
622         if (targetMask[0] == '#') {
623                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
624                         if (Client_Type(cl) != CLIENT_USER)
625                                 continue;
626                         client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
627                         if (client_match)
628                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
629                                                 command, Client_ID(cl), message))
630                                         return false;
631                 }
632         } else {
633                 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
634                 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
635                         if (Client_Type(cl) != CLIENT_USER)
636                                 continue;
637                         client_match = MatchCaseInsensitive(mask,
638                                         Client_ID(Client_Introducer(cl)));
639                         if (client_match)
640                                 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
641                                                 command, Client_ID(cl), message))
642                                         return false;
643                 }
644         }
645         return CONNECTED;
646 } /* Send_Message_Mask */
647
648
649 /* -eof- */