]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-write.c
8d09f9bcf8eb271672d32de78199fd18e41e26a3
[ngircd-alex.git] / src / ngircd / irc-write.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 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  * Sending IRC commands over the network
17  */
18
19 #include <assert.h>
20 #ifdef PROTOTYPES
21 #       include <stdarg.h>
22 #else
23 #       include <varargs.h>
24 #endif
25 #include <stdio.h>
26
27 #include "conn-func.h"
28 #include "channel.h"
29
30 #include "irc-write.h"
31
32 #define SEND_TO_USER 1
33 #define SEND_TO_SERVER 2
34
35 static const char *Get_Prefix PARAMS((CLIENT *Target, CLIENT *Client));
36 static void cb_writeStrServersPrefixFlag PARAMS((CLIENT *Client,
37                                          CLIENT *Prefix, void *Buffer));
38 static void Send_Marked_Connections PARAMS((CLIENT *Prefix, const char *Buffer));
39
40 /**
41  * Send an error message to a client and enforce a penalty time.
42  *
43  * @param Client The target client.
44  * @param Format Format string.
45  * @return CONNECTED or DISCONNECTED.
46  */
47 #ifdef PROTOTYPES
48 GLOBAL bool
49 IRC_WriteErrClient( CLIENT *Client, const char *Format, ... )
50 #else
51 GLOBAL bool
52 IRC_WriteErrClient( Client, Format, va_alist )
53 CLIENT *Client;
54 const char *Format;
55 va_dcl
56 #endif
57 {
58         char buffer[1000];
59         va_list ap;
60
61         assert(Client != NULL);
62         assert(Format != NULL);
63
64 #ifdef PROTOTYPES
65         va_start(ap, Format);
66 #else
67         va_start(ap);
68 #endif
69         vsnprintf(buffer, sizeof(buffer), Format, ap);
70         va_end(ap);
71
72         IRC_SetPenalty(Client, 2);
73         return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
74                                         "%s", buffer);
75 }
76
77 /**
78  * Send a message to a client.
79  *
80  * @param Client The target client.
81  * @param Format Format string.
82  * @return CONNECTED or DISCONNECTED.
83  */
84 #ifdef PROTOTYPES
85 GLOBAL bool
86 IRC_WriteStrClient( CLIENT *Client, const char *Format, ... )
87 #else
88 GLOBAL bool
89 IRC_WriteStrClient( Client, Format, va_alist )
90 CLIENT *Client;
91 const char *Format;
92 va_dcl
93 #endif
94 {
95         char buffer[1000];
96         va_list ap;
97
98         assert(Client != NULL);
99         assert(Format != NULL);
100
101 #ifdef PROTOTYPES
102         va_start(ap, Format);
103 #else
104         va_start(ap);
105 #endif
106         vsnprintf(buffer, sizeof(buffer), Format, ap);
107         va_end(ap);
108
109         return IRC_WriteStrClientPrefix(Client, Client_ThisServer(),
110                                         "%s", buffer);
111 }
112
113 /**
114  * Send a message to a client using a specific prefix.
115  *
116  * @param Client The target client.
117  * @param Prefix The prefix to use.
118  * @param Format Format string.
119  * @return CONNECTED or DISCONNECTED.
120  */
121 #ifdef PROTOTYPES
122 GLOBAL bool
123 IRC_WriteStrClientPrefix(CLIENT *Client, CLIENT *Prefix, const char *Format, ...)
124 #else
125 GLOBAL bool
126 IRC_WriteStrClientPrefix(Client, Prefix, Format, va_alist)
127 CLIENT *Client;
128 CLIENT *Prefix;
129 const char *Format;
130 va_dcl
131 #endif
132 {
133         /* send text to local and remote clients */
134
135         char buffer[1000];
136         va_list ap;
137
138         assert( Client != NULL );
139         assert( Format != NULL );
140         assert( Prefix != NULL );
141
142 #ifdef PROTOTYPES
143         va_start( ap, Format );
144 #else
145         va_start( ap );
146 #endif
147         vsnprintf(buffer, sizeof(buffer), Format, ap);
148         va_end( ap );
149
150         return Conn_WriteStr(Client_Conn(Client_NextHop(Client)), ":%s %s",
151                         Get_Prefix(Client_NextHop(Client), Prefix), buffer);
152 }
153
154 /**
155  * Send a message to all client in a channel.
156  *
157  * The message is only sent once per remote server.
158  *
159  * @param Client The sending client, excluded while forwarding the message.
160  * @param Channel The target channel.
161  * @param Remote If not set, the message is sent to local clients only.
162  * @param Format Format string.
163  */
164 #ifdef PROTOTYPES
165 GLOBAL void
166 IRC_WriteStrChannel(CLIENT *Client, CHANNEL *Chan, bool Remote,
167                     const char *Format, ...)
168 #else
169 GLOBAL void
170 IRC_WriteStrChannel(Client, Chan, Remote, Format, va_alist)
171 CLIENT *Client;
172 CHANNEL *Chan;
173 bool Remote;
174 const char *Format;
175 va_dcl
176 #endif
177 {
178         char buffer[1000];
179         va_list ap;
180
181         assert( Client != NULL );
182         assert( Format != NULL );
183
184 #ifdef PROTOTYPES
185         va_start( ap, Format );
186 #else
187         va_start( ap );
188 #endif
189         vsnprintf(buffer, sizeof(buffer), Format, ap);
190         va_end( ap );
191
192         IRC_WriteStrChannelPrefix(Client, Chan, Client_ThisServer(),
193                                   Remote, "%s", buffer);
194 }
195
196 /**
197  * Send a message to all client in a channel using a specific prefix.
198  *
199  * The message is only sent once per remote server.
200  *
201  * @param Client The sending client, excluded while forwarding the message.
202  * @param Channel The target channel.
203  * @param Prefix The prefix to use.
204  * @param Remote If not set, the message is sent to local clients only.
205  * @param Format Format string.
206  */
207 #ifdef PROTOTYPES
208 GLOBAL void
209 IRC_WriteStrChannelPrefix(CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix,
210                           bool Remote, const char *Format, ...)
211 #else
212 GLOBAL void
213 IRC_WriteStrChannelPrefix(Client, Chan, Prefix, Remote, Format, va_alist)
214 CLIENT *Client;
215 CHANNEL *Chan;
216 CLIENT *Prefix;
217 bool Remote;
218 const char *Format;
219 va_dcl
220 #endif
221 {
222         char buffer[1000];
223         CL2CHAN *cl2chan;
224         CONN_ID conn;
225         CLIENT *c;
226         va_list ap;
227
228         assert( Client != NULL );
229         assert( Chan != NULL );
230         assert( Prefix != NULL );
231         assert( Format != NULL );
232
233 #ifdef PROTOTYPES
234         va_start( ap, Format );
235 #else
236         va_start( ap  );
237 #endif
238         vsnprintf(buffer, sizeof(buffer), Format, ap);
239         va_end( ap );
240
241         Conn_ClearFlags( );
242
243         cl2chan = Channel_FirstMember( Chan );
244         while(cl2chan) {
245                 c = Channel_GetClient( cl2chan );
246                 if (!Remote) {
247                         if (Client_Conn(c) <= NONE)
248                                 c = NULL;
249                         else if(Client_Type(c) == CLIENT_SERVER)
250                                 c = NULL;
251                 }
252                 if(c)
253                         c = Client_NextHop(c);
254
255                 if(c && c != Client) {
256                         /* Ok, another Client */
257                         conn = Client_Conn(c);
258                         if (Client_Type(c) == CLIENT_SERVER)
259                                 Conn_SetFlag(conn, SEND_TO_SERVER);
260                         else
261                                 Conn_SetFlag(conn, SEND_TO_USER);
262                 }
263                 cl2chan = Channel_NextMember(Chan, cl2chan);
264         }
265         Send_Marked_Connections(Prefix, buffer);
266 }
267
268 /**
269  * Send a message to all the servers in the network.
270  *
271  * @param Client The sending client, excluded while forwarding the message.
272  * @param Format Format string.
273  */
274 #ifdef PROTOTYPES
275 GLOBAL void
276 IRC_WriteStrServers(CLIENT *ExceptOf, const char *Format, ...)
277 #else
278 GLOBAL void
279 IRC_WriteStrServers(ExceptOf, Format, va_alist)
280 CLIENT *ExceptOf;
281 const char *Format;
282 va_dcl
283 #endif
284 {
285         char buffer[1000];
286         va_list ap;
287
288         assert( Format != NULL );
289
290 #ifdef PROTOTYPES
291         va_start( ap, Format );
292 #else
293         va_start( ap );
294 #endif
295         vsnprintf(buffer, sizeof(buffer), Format, ap);
296         va_end( ap );
297
298         IRC_WriteStrServersPrefix(ExceptOf, Client_ThisServer(), "%s", buffer);
299 }
300
301 /**
302  * Send a message to all the servers in the network using a specific prefix.
303  *
304  * @param Client The sending client, excluded while forwarding the message.
305  * @param Prefix The prefix to use.
306  * @param Format Format string.
307  */
308 #ifdef PROTOTYPES
309 GLOBAL void
310 IRC_WriteStrServersPrefix(CLIENT *ExceptOf, CLIENT *Prefix,
311                           const char *Format, ...)
312 #else
313 GLOBAL void
314 IRC_WriteStrServersPrefix(ExceptOf, Prefix, Format, va_alist)
315 CLIENT *ExceptOf;
316 CLIENT *Prefix;
317 const char *Format;
318 va_dcl
319 #endif
320 {
321         char buffer[1000];
322         va_list ap;
323
324         assert( Format != NULL );
325         assert( Prefix != NULL );
326
327 #ifdef PROTOTYPES
328         va_start( ap, Format );
329 #else
330         va_start( ap );
331 #endif
332         vsnprintf(buffer, sizeof(buffer), Format, ap);
333         va_end( ap );
334
335         IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, '\0', "%s", buffer );
336 }
337
338 /**
339  * Send a message to all the servers in the network using a specific prefix
340  * and matching a "client flag".
341  *
342  * @param Client The sending client, excluded while forwarding the message.
343  * @param Prefix The prefix to use.
344  * @param Flag Client flag that must be set on the target.
345  * @param Format Format string.
346  */
347 #ifdef PROTOTYPES
348 GLOBAL void
349 IRC_WriteStrServersPrefixFlag(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
350                               const char *Format, ...)
351 #else
352 GLOBAL void
353 IRC_WriteStrServersPrefixFlag(ExceptOf, Prefix, Flag, Format, va_alist)
354 CLIENT *ExceptOf;
355 CLIENT *Prefix;
356 char Flag;
357 const char *Format;
358 va_dcl
359 #endif
360 {
361         char buffer[1000];
362         va_list ap;
363
364         assert( Format != NULL );
365         assert( Prefix != NULL );
366
367 #ifdef PROTOTYPES
368         va_start( ap, Format );
369 #else
370         va_start( ap );
371 #endif
372         vsnprintf(buffer, sizeof(buffer), Format, ap);
373         va_end( ap );
374
375         IRC_WriteStrServersPrefixFlag_CB(ExceptOf, Prefix, Flag,
376                                          cb_writeStrServersPrefixFlag, buffer);
377 }
378
379 /**
380  * Send a message to all the servers in the network using a specific prefix
381  * and matching a "client flag" using a callback function.
382  *
383  * @param Client The sending client, excluded while forwarding the message.
384  * @param Prefix The prefix to use.
385  * @param Flag Client flag that must be set on the target.
386  * @param callback Callback function.
387  * @param Format Format string.
388  */
389 GLOBAL void
390 IRC_WriteStrServersPrefixFlag_CB(CLIENT *ExceptOf, CLIENT *Prefix, char Flag,
391                 void (*callback)(CLIENT *, CLIENT *, void *), void *cb_data)
392 {
393         CLIENT *c;
394
395         c = Client_First();
396         while(c) {
397                 if (Client_Type(c) == CLIENT_SERVER && Client_Conn(c) > NONE &&
398                     c != Client_ThisServer() && c != ExceptOf) {
399                         /* Found a target server, do the flags match? */
400                         if (Flag == '\0' || Client_HasFlag(c, Flag))
401                                 callback(c, Prefix, cb_data);
402                 }
403                 c = Client_Next(c);
404         }
405 }
406
407 /**
408  * Send a message to all "related" clients.
409  *
410  * Related clients are the one that share one ore more channels with the client
411  * sending this message.
412  *
413  * The message is only sent once per remote server.
414  *
415  * @param Client The sending client, excluded while forwarding the message.
416  * @param Prefix The prefix to use.
417  * @param Remote If not set, the message is sent to local clients only.
418  * @param Format Format string.
419  */
420 #ifdef PROTOTYPES
421 GLOBAL void
422 IRC_WriteStrRelatedPrefix(CLIENT *Client, CLIENT *Prefix, bool Remote,
423                           const char *Format, ...)
424 #else
425 GLOBAL void
426 IRC_WriteStrRelatedPrefix(Client, Prefix, Remote, Format, va_alist)
427 CLIENT *Client;
428 CLIENT *Prefix;
429 bool Remote;
430 const char *Format;
431 va_dcl
432 #endif
433 {
434         CL2CHAN *chan_cl2chan, *cl2chan;
435         char buffer[1000];
436         CHANNEL *chan;
437         CONN_ID conn;
438         va_list ap;
439         CLIENT *c;
440
441         assert( Client != NULL );
442         assert( Prefix != NULL );
443         assert( Format != NULL );
444
445 #ifdef PROTOTYPES
446         va_start( ap, Format );
447 #else
448         va_start( ap );
449 #endif
450         vsnprintf(buffer, sizeof(buffer), Format, ap);
451         va_end( ap );
452
453         Conn_ClearFlags( );
454
455         chan_cl2chan = Channel_FirstChannelOf( Client );
456         while( chan_cl2chan )
457         {
458                 chan = Channel_GetChannel( chan_cl2chan );
459                 cl2chan = Channel_FirstMember( chan );
460                 while( cl2chan )
461                 {
462                         c = Channel_GetClient( cl2chan );
463                         if( ! Remote )
464                         {
465                                 if( Client_Conn( c ) <= NONE ) c = NULL;
466                                 else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
467                         }
468                         if( c ) c = Client_NextHop( c );
469
470                         if( c && ( c != Client ))
471                         {
472                                 conn = Client_Conn( c );
473                                 if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER );
474                                 else Conn_SetFlag( conn, SEND_TO_USER );
475                         }
476                         cl2chan = Channel_NextMember( chan, cl2chan );
477                 }
478
479                 chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
480         }
481         Send_Marked_Connections(Prefix, buffer);
482 } /* IRC_WriteStrRelatedPrefix */
483
484 /**
485  * Send WALLOPS message.
486  *
487  * @param Client The sending client, excluded while forwarding the message.
488  * @param From The (remote) sender of the message.
489  * @param Format Format string.
490 */
491 #ifdef PROTOTYPES
492 GLOBAL void
493 IRC_SendWallops(CLIENT *Client, CLIENT *From, const char *Format, ...)
494 #else
495 GLOBAL void
496 IRC_SendWallops(Client, From, Format, va_alist )
497 CLIENT *Client;
498 CLIENT *From;
499 const char *Format;
500 va_dcl
501 #endif
502 {
503         va_list ap;
504         char msg[1000];
505         CLIENT *to;
506
507 #ifdef PROTOTYPES
508         va_start(ap, Format);
509 #else
510         va_start(ap);
511 #endif
512         vsnprintf(msg, sizeof(msg), Format, ap);
513         va_end(ap);
514
515         for (to=Client_First(); to != NULL; to=Client_Next(to)) {
516                 if (Client_Conn(to) == NONE) /* no local connection */
517                         continue;
518
519                 switch (Client_Type(to)) {
520                 case CLIENT_USER:
521                         if (Client_HasMode(to, 'w'))
522                                 IRC_WriteStrClientPrefix(to, From,
523                                                          "WALLOPS :%s", msg);
524                                 break;
525                 case CLIENT_SERVER:
526                         if (to != Client)
527                                 IRC_WriteStrClientPrefix(to, From,
528                                                          "WALLOPS :%s", msg);
529                                 break;
530                 }
531         }
532 } /* IRC_SendWallops */
533
534 /**
535  * Set a "penalty time" for an IRC client.
536  *
537  * Note: penalty times are never set for server links or remote clients!
538  *
539  * @param Client The client.
540  * @param Seconds The additional "penalty time" to enforce.
541  */
542 GLOBAL void
543 IRC_SetPenalty(CLIENT *Client, time_t Seconds)
544 {
545         CONN_ID c;
546
547         assert(Client != NULL);
548         assert(Seconds > 0);
549
550         if (Client_Type(Client) == CLIENT_SERVER)
551                 return;
552
553         c = Client_Conn(Client);
554         if (c <= NONE)
555                 return;
556
557         Conn_SetPenalty(c, Seconds);
558 } /* IRC_SetPenalty */
559
560 static const char *
561 Get_Prefix(CLIENT *Target, CLIENT *Client)
562 {
563         assert (Target != NULL);
564         assert (Client != NULL);
565
566         if (Client_Type(Target) == CLIENT_SERVER)
567                 return Client_ID(Client);
568         else
569                 return Client_MaskCloaked(Client);
570 } /* Get_Prefix */
571
572 static void
573 cb_writeStrServersPrefixFlag(CLIENT *Client, CLIENT *Prefix, void *Buffer)
574 {
575         IRC_WriteStrClientPrefix(Client, Prefix, "%s", Buffer);
576 } /* cb_writeStrServersPrefixFlag */
577
578 /**
579  * Send a message to all marked connections using a specific prefix.
580  *
581  * @param Prefix The prefix to use.
582  * @param Buffer The message to send.
583  */
584 static void
585 Send_Marked_Connections(CLIENT *Prefix, const char *Buffer)
586 {
587         CONN_ID conn;
588
589         assert(Prefix != NULL);
590         assert(Buffer != NULL);
591
592         conn = Conn_First();
593         while (conn != NONE) {
594                 if (Conn_Flag(conn) == SEND_TO_SERVER)
595                         Conn_WriteStr(conn, ":%s %s",
596                                       Client_ID(Prefix), Buffer);
597                 else if (Conn_Flag(conn) == SEND_TO_USER)
598                         Conn_WriteStr(conn, ":%s %s",
599                                       Client_MaskCloaked(Prefix), Buffer);
600                 conn = Conn_Next(conn);
601         }
602 }
603
604 /* -eof- */