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