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