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