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 c23199d9 2002-02-27 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 c23199d9 2002-02-27 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 c23199d9 2002-02-27 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 c23199d9 2002-02-27 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 c23199d9 2002-02-27 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 c23199d9 2002-02-27 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 117c1df6 2002-03-25 alex * $Id: irc-write.c,v 1.3 2002/03/25 17:13:07 alex Exp $
14 c23199d9 2002-02-27 alex * irc-write.c: IRC-Texte und Befehle ueber Netzwerk versenden
18 ca33cbda 2002-03-12 alex #include "portab.h"
20 ca33cbda 2002-03-12 alex #include "imp.h"
21 c23199d9 2002-02-27 alex #include <assert.h>
22 c23199d9 2002-02-27 alex #include <stdarg.h>
23 c23199d9 2002-02-27 alex #include <stdio.h>
25 ca33cbda 2002-03-12 alex #include "defines.h"
27 ca33cbda 2002-03-12 alex #include "exp.h"
28 c23199d9 2002-02-27 alex #include "irc-write.h"
31 117c1df6 2002-03-25 alex LOCAL CHAR *Get_Prefix( CLIENT *Target, CLIENT *Client );
34 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrClient( CLIENT *Client, CHAR *Format, ... )
36 c23199d9 2002-02-27 alex CHAR buffer[1000];
37 c23199d9 2002-02-27 alex BOOLEAN ok = CONNECTED;
40 c23199d9 2002-02-27 alex assert( Client != NULL );
41 c23199d9 2002-02-27 alex assert( Format != NULL );
43 c23199d9 2002-02-27 alex va_start( ap, Format );
44 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
45 c23199d9 2002-02-27 alex va_end( ap );
47 c23199d9 2002-02-27 alex /* an den Client selber */
48 c23199d9 2002-02-27 alex ok = IRC_WriteStrClientPrefix( Client, Client_ThisServer( ), buffer );
51 c23199d9 2002-02-27 alex } /* IRC_WriteStrClient */
54 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrClientPrefix( CLIENT *Client, CLIENT *Prefix, CHAR *Format, ... )
56 c23199d9 2002-02-27 alex /* Text an Clients, lokal bzw. remote, senden. */
58 c23199d9 2002-02-27 alex CHAR buffer[1000];
61 c23199d9 2002-02-27 alex assert( Client != NULL );
62 c23199d9 2002-02-27 alex assert( Format != NULL );
63 c23199d9 2002-02-27 alex assert( Prefix != NULL );
65 c23199d9 2002-02-27 alex va_start( ap, Format );
66 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
67 c23199d9 2002-02-27 alex va_end( ap );
69 c23199d9 2002-02-27 alex return Conn_WriteStr( Client_Conn( Client_NextHop( Client )), ":%s %s", Get_Prefix( Client_NextHop( Client ), Prefix ), buffer );
70 c23199d9 2002-02-27 alex } /* IRC_WriteStrClientPrefix */
73 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrChannel( CLIENT *Client, CHANNEL *Chan, BOOLEAN Remote, CHAR *Format, ... )
75 c23199d9 2002-02-27 alex CHAR buffer[1000];
78 c23199d9 2002-02-27 alex assert( Client != NULL );
79 c23199d9 2002-02-27 alex assert( Format != NULL );
81 c23199d9 2002-02-27 alex va_start( ap, Format );
82 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
83 c23199d9 2002-02-27 alex va_end( ap );
85 c23199d9 2002-02-27 alex return IRC_WriteStrChannelPrefix( Client, Chan, Client_ThisServer( ), Remote, buffer );
86 c23199d9 2002-02-27 alex } /* IRC_WriteStrChannel */
89 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrChannelPrefix( CLIENT *Client, CHANNEL *Chan, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
91 c23199d9 2002-02-27 alex BOOLEAN sock[MAX_CONNECTIONS], is_server[MAX_CONNECTIONS], ok = CONNECTED;
92 c23199d9 2002-02-27 alex CHAR buffer[1000];
93 c23199d9 2002-02-27 alex CL2CHAN *cl2chan;
98 c23199d9 2002-02-27 alex assert( Client != NULL );
99 c23199d9 2002-02-27 alex assert( Chan != NULL );
100 c23199d9 2002-02-27 alex assert( Prefix != NULL );
101 c23199d9 2002-02-27 alex assert( Format != NULL );
103 c23199d9 2002-02-27 alex va_start( ap, Format );
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 for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
109 c23199d9 2002-02-27 alex /* An alle Clients, die in den selben Channels sind.
110 c23199d9 2002-02-27 alex * Dabei aber nur einmal je Remote-Server */
111 c23199d9 2002-02-27 alex cl2chan = Channel_FirstMember( Chan );
112 c23199d9 2002-02-27 alex while( cl2chan )
114 c23199d9 2002-02-27 alex c = Channel_GetClient( cl2chan );
115 c23199d9 2002-02-27 alex if( ! Remote )
117 c23199d9 2002-02-27 alex if( Client_Conn( c ) <= NONE ) c = NULL;
118 c23199d9 2002-02-27 alex else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
120 c23199d9 2002-02-27 alex if( c ) c = Client_NextHop( c );
122 c23199d9 2002-02-27 alex if( c && ( c != Client ))
124 c23199d9 2002-02-27 alex /* Ok, anderer Client */
125 c23199d9 2002-02-27 alex s = Client_Conn( c );
126 c23199d9 2002-02-27 alex assert( s >= 0 );
127 c23199d9 2002-02-27 alex assert( s < MAX_CONNECTIONS );
128 c23199d9 2002-02-27 alex sock[s] = TRUE;
129 c23199d9 2002-02-27 alex if( Client_Type( c ) == CLIENT_SERVER ) is_server[s] = TRUE;
130 c23199d9 2002-02-27 alex else is_server[s] = FALSE;
132 c23199d9 2002-02-27 alex cl2chan = Channel_NextMember( Chan, cl2chan );
135 c23199d9 2002-02-27 alex /* Senden ... */
136 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
138 c23199d9 2002-02-27 alex if( sock[i] )
140 c23199d9 2002-02-27 alex if( is_server[i] ) ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
141 c23199d9 2002-02-27 alex else ok = Conn_WriteStr( i, ":%s %s", Client_Mask( Prefix ), buffer );
142 c23199d9 2002-02-27 alex if( ! ok ) break;
146 c23199d9 2002-02-27 alex } /* IRC_WriteStrChannelPrefix */
149 c23199d9 2002-02-27 alex GLOBAL VOID IRC_WriteStrServers( CLIENT *ExceptOf, CHAR *Format, ... )
151 c23199d9 2002-02-27 alex CHAR buffer[1000];
152 c23199d9 2002-02-27 alex va_list ap;
154 c23199d9 2002-02-27 alex assert( Format != NULL );
156 c23199d9 2002-02-27 alex va_start( ap, Format );
157 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
158 c23199d9 2002-02-27 alex va_end( ap );
160 c23199d9 2002-02-27 alex /* an den Client selber */
161 c23199d9 2002-02-27 alex return IRC_WriteStrServersPrefix( ExceptOf, Client_ThisServer( ), buffer );
162 c23199d9 2002-02-27 alex } /* IRC_WriteStrServers */
165 c23199d9 2002-02-27 alex GLOBAL VOID IRC_WriteStrServersPrefix( CLIENT *ExceptOf, CLIENT *Prefix, CHAR *Format, ... )
167 c23199d9 2002-02-27 alex CHAR buffer[1000];
169 c23199d9 2002-02-27 alex va_list ap;
171 c23199d9 2002-02-27 alex assert( Format != NULL );
172 c23199d9 2002-02-27 alex assert( Prefix != NULL );
174 c23199d9 2002-02-27 alex va_start( ap, Format );
175 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
176 c23199d9 2002-02-27 alex va_end( ap );
178 c23199d9 2002-02-27 alex c = Client_First( );
181 c23199d9 2002-02-27 alex if(( Client_Type( c ) == CLIENT_SERVER ) && ( Client_Conn( c ) > NONE ) && ( c != Client_ThisServer( )) && ( c != ExceptOf ))
183 c23199d9 2002-02-27 alex /* Ziel-Server gefunden */
184 c23199d9 2002-02-27 alex IRC_WriteStrClientPrefix( c, Prefix, buffer );
186 c23199d9 2002-02-27 alex c = Client_Next( c );
188 c23199d9 2002-02-27 alex } /* IRC_WriteStrServersPrefix */
191 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_WriteStrRelatedPrefix( CLIENT *Client, CLIENT *Prefix, BOOLEAN Remote, CHAR *Format, ... )
193 c23199d9 2002-02-27 alex BOOLEAN sock[MAX_CONNECTIONS], is_server[MAX_CONNECTIONS], ok = CONNECTED;
194 c23199d9 2002-02-27 alex CL2CHAN *chan_cl2chan, *cl2chan;
195 c23199d9 2002-02-27 alex CHAR buffer[1000];
196 c23199d9 2002-02-27 alex CHANNEL *chan;
197 c23199d9 2002-02-27 alex va_list ap;
201 c23199d9 2002-02-27 alex assert( Client != NULL );
202 c23199d9 2002-02-27 alex assert( Prefix != NULL );
203 c23199d9 2002-02-27 alex assert( Format != NULL );
205 c23199d9 2002-02-27 alex va_start( ap, Format );
206 c23199d9 2002-02-27 alex vsnprintf( buffer, 1000, Format, ap );
207 c23199d9 2002-02-27 alex va_end( ap );
209 c23199d9 2002-02-27 alex /* initialisieren */
210 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ ) sock[i] = FALSE;
212 c23199d9 2002-02-27 alex /* An alle Clients, die in einem Channel mit dem "Ausloeser" sind,
213 c23199d9 2002-02-27 alex * den Text schicken. An Remote-Server aber jeweils nur einmal. */
214 c23199d9 2002-02-27 alex chan_cl2chan = Channel_FirstChannelOf( Client );
215 c23199d9 2002-02-27 alex while( chan_cl2chan )
217 c23199d9 2002-02-27 alex /* Channel des Users durchsuchen */
218 c23199d9 2002-02-27 alex chan = Channel_GetChannel( chan_cl2chan );
219 c23199d9 2002-02-27 alex cl2chan = Channel_FirstMember( chan );
220 c23199d9 2002-02-27 alex while( cl2chan )
222 c23199d9 2002-02-27 alex c = Channel_GetClient( cl2chan );
223 c23199d9 2002-02-27 alex if( ! Remote )
225 c23199d9 2002-02-27 alex if( Client_Conn( c ) <= NONE ) c = NULL;
226 c23199d9 2002-02-27 alex else if( Client_Type( c ) == CLIENT_SERVER ) c = NULL;
228 c23199d9 2002-02-27 alex if( c ) c = Client_NextHop( c );
230 c23199d9 2002-02-27 alex if( c && ( c != Client ))
232 c23199d9 2002-02-27 alex /* Ok, anderer Client */
233 c23199d9 2002-02-27 alex s = Client_Conn( c );
234 c23199d9 2002-02-27 alex assert( s >= 0 );
235 c23199d9 2002-02-27 alex assert( s < MAX_CONNECTIONS );
236 c23199d9 2002-02-27 alex sock[s] = TRUE;
237 c23199d9 2002-02-27 alex if( Client_Type( c ) == CLIENT_SERVER ) is_server[s] = TRUE;
238 c23199d9 2002-02-27 alex else is_server[s] = FALSE;
240 c23199d9 2002-02-27 alex cl2chan = Channel_NextMember( chan, cl2chan );
243 c23199d9 2002-02-27 alex /* naechsten Channel */
244 c23199d9 2002-02-27 alex chan_cl2chan = Channel_NextChannelOf( Client, chan_cl2chan );
247 c23199d9 2002-02-27 alex /* Senden ... */
248 c23199d9 2002-02-27 alex for( i = 0; i < MAX_CONNECTIONS; i++ )
250 c23199d9 2002-02-27 alex if( sock[i] )
252 c23199d9 2002-02-27 alex if( is_server[i] ) ok = Conn_WriteStr( i, ":%s %s", Client_ID( Prefix ), buffer );
253 c23199d9 2002-02-27 alex else ok = Conn_WriteStr( i, ":%s %s", Client_Mask( Prefix ), buffer );
254 c23199d9 2002-02-27 alex if( ! ok ) break;
258 c23199d9 2002-02-27 alex } /* IRC_WriteStrRelatedPrefix */
261 117c1df6 2002-03-25 alex LOCAL CHAR *Get_Prefix( CLIENT *Target, CLIENT *Client )
263 c23199d9 2002-02-27 alex assert( Target != NULL );
264 c23199d9 2002-02-27 alex assert( Client != NULL );
266 c23199d9 2002-02-27 alex if( Client_Type( Target ) == CLIENT_SERVER ) return Client_ID( Client );
267 c23199d9 2002-02-27 alex else return Client_Mask( Client );
268 c23199d9 2002-02-27 alex } /* Get_Prefix */
271 c23199d9 2002-02-27 alex /* -eof- */