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