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