2 c23199d9 2002-02-27 alex * ngIRCd -- The Next Generation IRC Daemon
3 c23199d9 2002-02-27 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 490f28ff 2002-12-12 alex * Sending IRC commands over the network
15 ca33cbda 2002-03-12 alex #include "portab.h"
17 c1f32e82 2004-10-20 alex static char UNUSED id[] = "$Id: irc-write.c,v 1.16 2004/10/20 13:47:32 alex Exp $";
19 ca33cbda 2002-03-12 alex #include "imp.h"
20 c23199d9 2002-02-27 alex #include <assert.h>
21 c1f32e82 2004-10-20 alex #ifdef PROTOTYPES
22 c1f32e82 2004-10-20 alex # include <stdarg.h>
24 c1f32e82 2004-10-20 alex # include <varargs.h>
26 c23199d9 2002-02-27 alex #include <stdio.h>
27 3010f034 2002-09-03 alex #include <string.h>
29 b8d7dcec 2002-12-30 alex #include "conn-func.h"
30 c2f60abe 2002-05-27 alex #include "client.h"
31 c2f60abe 2002-05-27 alex #include "channel.h"
32 ca33cbda 2002-03-12 alex #include "defines.h"
34 ca33cbda 2002-03-12 alex #include "exp.h"
35 c23199d9 2002-02-27 alex #include "irc-write.h"
38 4c870f4b 2002-11-04 alex #define SEND_TO_USER 1
39 4c870f4b 2002-11-04 alex #define SEND_TO_SERVER 2
42 c2f60abe 2002-05-27 alex LOCAL CHAR *Get_Prefix PARAMS(( CLIENT *Target, CLIENT *Client ));
45 f7327524 2002-05-30 alex #ifdef PROTOTYPES
46 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
47 c2f60abe 2002-05-27 alex IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
49 f7327524 2002-05-30 alex GLOBAL BOOLEAN
50 f7327524 2002-05-30 alex IRC_WriteStrClient( Client, Format, va_alist )
51 f7327524 2002-05-30 alex CLIENT *Client;
52 f7327524 2002-05-30 alex CHAR *Format;
56 c23199d9 2002-02-27 alex CHAR buffer[1000];
57 c23199d9 2002-02-27 alex BOOLEAN ok = CONNECTED;
60 c23199d9 2002-02-27 alex assert( Client != NULL );
61 c23199d9 2002-02-27 alex assert( Format != NULL );
63 f7327524 2002-05-30 alex #ifdef PROTOTYPES
64 c23199d9 2002-02-27 alex va_start( ap, Format );
66 f7327524 2002-05-30 alex va_start( ap );
68 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
69 c23199d9 2002-02-27 alex va_end( ap );
71 c23199d9 2002-02-27 alex /* an den Client selber */
72 ec474a4b 2002-10-04 alex ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), "%s", buffer );
75 c23199d9 2002-02-27 alex } /* IRC_WriteStrClient */
78 f7327524 2002-05-30 alex #ifdef PROTOTYPES
79 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
80 c2f60abe 2002-05-27 alex IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
82 f7327524 2002-05-30 alex GLOBAL BOOLEAN
83 f7327524 2002-05-30 alex IRC_WriteStrClientPrefix( Client, Prefix, Format, va_alist )
84 f7327524 2002-05-30 alex CLIENT *Client;
85 f7327524 2002-05-30 alex CLIENT *Prefix;
86 f7327524 2002-05-30 alex CHAR *Format;
90 c23199d9 2002-02-27 alex /* Text an Clients, lokal bzw. remote, senden. */
92 c23199d9 2002-02-27 alex CHAR buffer[1000];
95 c23199d9 2002-02-27 alex assert( Client != NULL );
96 c23199d9 2002-02-27 alex assert( Format != NULL );
97 c23199d9 2002-02-27 alex assert( Prefix != NULL );
99 f7327524 2002-05-30 alex #ifdef PROTOTYPES
100 c23199d9 2002-02-27 alex va_start( ap, Format );
102 f7327524 2002-05-30 alex va_start( ap );
104 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
105 c23199d9 2002-02-27 alex va_end( ap );
107 c23199d9 2002-02-27 alex return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Get_Prefix( Client_NextHop( Client ), Prefix ), buffer );
108 c23199d9 2002-02-27 alex } /* IRC_WriteStrClientPrefix */
111 f7327524 2002-05-30 alex #ifdef PROTOTYPES
112 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
113 c2f60abe 2002-05-27 alex IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
115 f7327524 2002-05-30 alex GLOBAL BOOLEAN
116 f7327524 2002-05-30 alex IRC_WriteStrChannel( Client, Chan, Remote, Format, va_alist )
117 f7327524 2002-05-30 alex CLIENT *Client;
118 f7327524 2002-05-30 alex CHANNEL *Chan;
119 f7327524 2002-05-30 alex BOOLEAN Remote;
120 f7327524 2002-05-30 alex CHAR *Format;
124 c23199d9 2002-02-27 alex CHAR buffer[1000];
125 c23199d9 2002-02-27 alex va_list ap;
127 c23199d9 2002-02-27 alex assert( Client != NULL );
128 c23199d9 2002-02-27 alex assert( Format != NULL );
130 f7327524 2002-05-30 alex #ifdef PROTOTYPES
131 c23199d9 2002-02-27 alex va_start( ap, Format );
133 f7327524 2002-05-30 alex va_start( ap );
135 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
136 c23199d9 2002-02-27 alex va_end( ap );
138 ec474a4b 2002-10-04 alex return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, "%s", buffer );
139 c23199d9 2002-02-27 alex } /* IRC_WriteStrChannel */
142 f7327524 2002-05-30 alex #ifdef PROTOTYPES
143 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
144 c2f60abe 2002-05-27 alex IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
146 f7327524 2002-05-30 alex GLOBAL BOOLEAN
147 f7327524 2002-05-30 alex IRC_WriteStrChannelPrefix( Client, Chan, Prefix, Remote, Format, va_alist )
148 f7327524 2002-05-30 alex CLIENT *Client;
149 f7327524 2002-05-30 alex CHANNEL *Chan;
150 f7327524 2002-05-30 alex CLIENT *Prefix;
151 f7327524 2002-05-30 alex BOOLEAN Remote;
152 f7327524 2002-05-30 alex CHAR *Format;
156 601f0ede 2002-11-02 alex BOOLEAN ok = CONNECTED;
157 c23199d9 2002-02-27 alex CHAR buffer[1000];
158 c23199d9 2002-02-27 alex CL2CHAN *cl2chan;
159 601f0ede 2002-11-02 alex CONN_ID conn;
161 c23199d9 2002-02-27 alex va_list ap;
163 c23199d9 2002-02-27 alex assert( Client != NULL );
164 c23199d9 2002-02-27 alex assert( Chan != NULL );
165 c23199d9 2002-02-27 alex assert( Prefix != NULL );
166 c23199d9 2002-02-27 alex assert( Format != NULL );
168 f7327524 2002-05-30 alex #ifdef PROTOTYPES
169 c23199d9 2002-02-27 alex va_start( ap, Format );
171 f7327524 2002-05-30 alex va_start( ap );
173 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
174 c23199d9 2002-02-27 alex va_end( ap );
176 601f0ede 2002-11-02 alex Conn_ClearFlags( );
178 c23199d9 2002-02-27 alex /* An alle Clients, die in den selben Channels sind.
179 c23199d9 2002-02-27 alex * Dabei aber nur einmal je Remote-Server */
180 c23199d9 2002-02-27 alex cl2chan = Channel_FirstMember( Chan );
181 c23199d9 2002-02-27 alex while( cl2chan )
183 c23199d9 2002-02-27 alex c = Channel_GetClient( cl2chan );
184 c23199d9 2002-02-27 alex if( ! Remote )
186 c23199d9 2002-02-27 alex if( Client_Conn( c ) <= NONE ) c = NULL;
187 c23199d9 2002-02-27 alex else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
189 c23199d9 2002-02-27 alex if( c ) c = Client_NextHop( c );
191 c23199d9 2002-02-27 alex if( c && ( c != Client ))
193 c23199d9 2002-02-27 alex /* Ok, anderer Client */
194 601f0ede 2002-11-02 alex conn = Client_Conn( c );
195 4c870f4b 2002-11-04 alex if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER );
196 4c870f4b 2002-11-04 alex else Conn_SetFlag( conn, SEND_TO_USER );
198 c23199d9 2002-02-27 alex cl2chan = Channel_NextMember( Chan, cl2chan );
201 4c870f4b 2002-11-04 alex /* Senden: alle Verbindungen durchgehen ... */
202 601f0ede 2002-11-02 alex conn = Conn_First( );
203 601f0ede 2002-11-02 alex while( conn != NONE )
205 4c870f4b 2002-11-04 alex /* muessen Daten ueber diese Verbindung verschickt werden? */
206 4c870f4b 2002-11-04 alex if( Conn_Flag( conn ) == SEND_TO_SERVER) ok = Conn_WriteStr( conn, ":%s %s", Client_ID( Prefix ), buffer );
207 4c870f4b 2002-11-04 alex else if( Conn_Flag( conn ) == SEND_TO_USER ) ok = Conn_WriteStr( conn, ":%s %s", Client_Mask( Prefix ), buffer );
208 4c870f4b 2002-11-04 alex if( ! ok ) break;
210 4c870f4b 2002-11-04 alex /* naechste Verbindung testen */
211 601f0ede 2002-11-02 alex conn = Conn_Next( conn );
215 c23199d9 2002-02-27 alex } /* IRC_WriteStrChannelPrefix */
218 f7327524 2002-05-30 alex #ifdef PROTOTYPES
219 c2f60abe 2002-05-27 alex GLOBAL VOID
220 c2f60abe 2002-05-27 alex IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
222 f7327524 2002-05-30 alex GLOBAL VOID
223 f7327524 2002-05-30 alex IRC_WriteStrServers( ExceptOf, Format, va_alist )
224 f7327524 2002-05-30 alex CLIENT *ExceptOf;
225 f7327524 2002-05-30 alex CHAR *Format;
229 c23199d9 2002-02-27 alex CHAR buffer[1000];
230 c23199d9 2002-02-27 alex va_list ap;
232 c23199d9 2002-02-27 alex assert( Format != NULL );
234 f7327524 2002-05-30 alex #ifdef PROTOTYPES
235 c23199d9 2002-02-27 alex va_start( ap, Format );
237 f7327524 2002-05-30 alex va_start( ap );
239 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
240 c23199d9 2002-02-27 alex va_end( ap );
242 c23199d9 2002-02-27 alex /* an den Client selber */
243 ec474a4b 2002-10-04 alex IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), "%s", buffer );
244 c23199d9 2002-02-27 alex } /* IRC_WriteStrServers */
247 f7327524 2002-05-30 alex #ifdef PROTOTYPES
248 c2f60abe 2002-05-27 alex GLOBAL VOID
249 c2f60abe 2002-05-27 alex IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
251 f7327524 2002-05-30 alex GLOBAL VOID
252 f7327524 2002-05-30 alex IRC_WriteStrServersPrefix( ExceptOf, Prefix, Format, va_alist )
253 3010f034 2002-09-03 alex CLIENT *ExceptOf;
254 3010f034 2002-09-03 alex CLIENT *Prefix;
255 3010f034 2002-09-03 alex CHAR *Format;
259 3010f034 2002-09-03 alex CHAR buffer[1000];
260 3010f034 2002-09-03 alex va_list ap;
262 3010f034 2002-09-03 alex assert( Format != NULL );
263 3010f034 2002-09-03 alex assert( Prefix != NULL );
265 3010f034 2002-09-03 alex #ifdef PROTOTYPES
266 3010f034 2002-09-03 alex va_start( ap, Format );
268 3010f034 2002-09-03 alex va_start( ap );
270 3010f034 2002-09-03 alex vsnprintf( buffer, 1000, Format, ap );
271 3010f034 2002-09-03 alex va_end( ap );
273 ec474a4b 2002-10-04 alex IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, '\0', "%s", buffer );
274 3010f034 2002-09-03 alex } /* IRC_WriteStrServersPrefix */
277 3010f034 2002-09-03 alex #ifdef PROTOTYPES
278 3010f034 2002-09-03 alex GLOBAL VOID
279 3010f034 2002-09-03 alex IRC_WriteStrServersPrefixFlag( CLIENT *ExceptOf, CLIENT *Prefix, CHAR Flag, CHAR *Format, ... )
281 3010f034 2002-09-03 alex GLOBAL VOID
282 3010f034 2002-09-03 alex IRC_WriteStrServersPrefixFlag( ExceptOf, Prefix, Flag, Format, va_alist )
283 f7327524 2002-05-30 alex CLIENT *ExceptOf;
284 f7327524 2002-05-30 alex CLIENT *Prefix;
286 f7327524 2002-05-30 alex CHAR *Format;
290 c23199d9 2002-02-27 alex CHAR buffer[1000];
292 c23199d9 2002-02-27 alex va_list ap;
294 c23199d9 2002-02-27 alex assert( Format != NULL );
295 c23199d9 2002-02-27 alex assert( Prefix != NULL );
297 f7327524 2002-05-30 alex #ifdef PROTOTYPES
298 c23199d9 2002-02-27 alex va_start( ap, Format );
300 f7327524 2002-05-30 alex va_start( ap );
302 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
303 c23199d9 2002-02-27 alex va_end( ap );
305 c23199d9 2002-02-27 alex c = Client_First( );
308 c23199d9 2002-02-27 alex if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
310 3010f034 2002-09-03 alex /* Ziel-Server gefunden. Nun noch pruefen, ob Flags stimmen */
311 ec474a4b 2002-10-04 alex if(( Flag == '\0' ) || ( strchr( Client_Flags( c ), Flag ) != NULL )) IRC_WriteStrClientPrefix( c, Prefix, "%s", buffer );
313 c23199d9 2002-02-27 alex c = Client_Next( c );
315 3010f034 2002-09-03 alex } /* IRC_WriteStrServersPrefixFlag */
318 f7327524 2002-05-30 alex #ifdef PROTOTYPES
319 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
320 c2f60abe 2002-05-27 alex IRC_WriteStrRelatedPrefix( CLIENT *Client, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
322 f7327524 2002-05-30 alex GLOBAL BOOLEAN
323 f7327524 2002-05-30 alex IRC_WriteStrRelatedPrefix( Client, Prefix, Remote, Format, va_alist )
324 f7327524 2002-05-30 alex CLIENT *Client;
325 f7327524 2002-05-30 alex CLIENT *Prefix;
326 f7327524 2002-05-30 alex BOOLEAN Remote;
327 f7327524 2002-05-30 alex CHAR *Format;
331 601f0ede 2002-11-02 alex BOOLEAN ok = CONNECTED;
332 c23199d9 2002-02-27 alex CL2CHAN *chan_cl2chan, *cl2chan;
333 c23199d9 2002-02-27 alex CHAR buffer[1000];
334 c23199d9 2002-02-27 alex CHANNEL *chan;
335 601f0ede 2002-11-02 alex CONN_ID conn;
336 c23199d9 2002-02-27 alex va_list ap;
339 c23199d9 2002-02-27 alex assert( Client != NULL );
340 c23199d9 2002-02-27 alex assert( Prefix != NULL );
341 c23199d9 2002-02-27 alex assert( Format != NULL );
343 f7327524 2002-05-30 alex #ifdef PROTOTYPES
344 c23199d9 2002-02-27 alex va_start( ap, Format );
346 f7327524 2002-05-30 alex va_start( ap );
348 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
349 c23199d9 2002-02-27 alex va_end( ap );
351 c23199d9 2002-02-27 alex /* initialisieren */
352 601f0ede 2002-11-02 alex Conn_ClearFlags( );
354 c23199d9 2002-02-27 alex /* An alle Clients, die in einem Channel mit dem "Ausloeser" sind,
355 c23199d9 2002-02-27 alex * den Text schicken. An Remote-Server aber jeweils nur einmal. */
356 c23199d9 2002-02-27 alex chan_cl2chan = Channel_FirstChannelOf( Client );
357 c23199d9 2002-02-27 alex while( chan_cl2chan )
359 c23199d9 2002-02-27 alex /* Channel des Users durchsuchen */
360 c23199d9 2002-02-27 alex chan = Channel_GetChannel( chan_cl2chan );
361 c23199d9 2002-02-27 alex cl2chan = Channel_FirstMember( chan );
362 c23199d9 2002-02-27 alex while( cl2chan )
364 c23199d9 2002-02-27 alex c = Channel_GetClient( cl2chan );
365 c23199d9 2002-02-27 alex if( ! Remote )
367 c23199d9 2002-02-27 alex if( Client_Conn( c ) <= NONE ) c = NULL;
368 c23199d9 2002-02-27 alex else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
370 c23199d9 2002-02-27 alex if( c ) c = Client_NextHop( c );
372 c23199d9 2002-02-27 alex if( c && ( c != Client ))
374 c23199d9 2002-02-27 alex /* Ok, anderer Client */
375 601f0ede 2002-11-02 alex conn = Client_Conn( c );
376 4c870f4b 2002-11-04 alex if( Client_Type( c ) == CLIENT_SERVER ) Conn_SetFlag( conn, SEND_TO_SERVER );
377 4c870f4b 2002-11-04 alex else Conn_SetFlag( conn, SEND_TO_USER );
379 c23199d9 2002-02-27 alex cl2chan = Channel_NextMember( chan, cl2chan );
382 c23199d9 2002-02-27 alex /* naechsten Channel */
383 c23199d9 2002-02-27 alex chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
386 4c870f4b 2002-11-04 alex /* Senden: alle Verbindungen durchgehen ... */
387 601f0ede 2002-11-02 alex conn = Conn_First( );
388 601f0ede 2002-11-02 alex while( conn != NONE )
390 4c870f4b 2002-11-04 alex /* muessen ueber diese Verbindung Daten gesendet werden? */
391 4c870f4b 2002-11-04 alex if( Conn_Flag( conn ) == SEND_TO_SERVER ) ok = Conn_WriteStr( conn, ":%s %s", Client_ID( Prefix ), buffer );
392 a6265aa0 2002-11-04 alex else if( Conn_Flag( conn ) == SEND_TO_USER ) ok = Conn_WriteStr( conn, ":%s %s", Client_Mask( Prefix ), buffer );
393 4c870f4b 2002-11-04 alex if( ! ok ) break;
395 4c870f4b 2002-11-04 alex /* naechste Verbindung testen */
396 601f0ede 2002-11-02 alex conn = Conn_Next( conn );
399 c23199d9 2002-02-27 alex } /* IRC_WriteStrRelatedPrefix */
402 7b6e2662 2003-11-05 alex GLOBAL VOID
403 7b6e2662 2003-11-05 alex IRC_SetPenalty( CLIENT *Client, INT Seconds )
407 7b6e2662 2003-11-05 alex assert( Client != NULL );
408 7b6e2662 2003-11-05 alex assert( Seconds > 0 );
410 7b6e2662 2003-11-05 alex if( Client_Type( Client ) == CLIENT_SERVER ) return;
412 7b6e2662 2003-11-05 alex c = Client_Conn( Client );
413 7b6e2662 2003-11-05 alex if( c > NONE ) Conn_SetPenalty( c, Seconds );
414 7b6e2662 2003-11-05 alex } /* IRC_SetPenalty */
417 c2f60abe 2002-05-27 alex LOCAL CHAR *
418 c2f60abe 2002-05-27 alex Get_Prefix( CLIENT *Target, CLIENT *Client )
420 c23199d9 2002-02-27 alex assert( Target != NULL );
421 c23199d9 2002-02-27 alex assert( Client != NULL );
423 c23199d9 2002-02-27 alex if( Client_Type( Target ) == CLIENT_SERVER ) return Client_ID( Client );
424 c23199d9 2002-02-27 alex else return Client_Mask( Client );
425 c23199d9 2002-02-27 alex } /* Get_Prefix */
428 c23199d9 2002-02-27 alex /* -eof- */