]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/numeric.c
numeric.c: whitespace fixes ...
[ngircd-alex.git] / src / ngircd / numeric.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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  * Handlers for IRC numerics sent to the server
12  */
13
14 #include "portab.h"
15
16 #include "imp.h"
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "defines.h"
23 #include "resolve.h"
24 #include "conn.h"
25 #include "conf.h"
26 #include "conn.h"
27 #include "conn-func.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "irc-write.h"
31 #include "lists.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "parse.h"
35
36 #include "exp.h"
37 #include "numeric.h"
38
39
40 /**
41  * Announce new server in the network
42  * @param Client New server
43  * @param Server Existing server in the network
44  */
45 static bool
46 Announce_Server(CLIENT * Client, CLIENT * Server)
47 {
48         CLIENT *c;
49
50         if (Client_Conn(Server) > NONE) {
51                 /* Announce the new server to the one already registered
52                  * which is directly connected to the local server */
53                 if (!IRC_WriteStrClient
54                     (Server, "SERVER %s %d %d :%s", Client_ID(Client),
55                      Client_Hops(Client) + 1, Client_MyToken(Client),
56                      Client_Info(Client)))
57                         return DISCONNECTED;
58         }
59
60         if (Client_Hops(Server) == 1)
61                 c = Client_ThisServer();
62         else
63                 c = Client_Introducer(Server);
64
65         /* Inform new server about the one already registered in the network */
66         return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
67                 Client_ID(Server), Client_Hops(Server) + 1,
68                 Client_MyToken(Server), Client_Info(Server));
69 } /* Announce_Server */
70
71
72 /**
73  * Announce existing user to a new server
74  * @param Client New server
75  * @param User Existing user in the network
76  */
77 static bool
78 Announce_User(CLIENT * Client, CLIENT * User)
79 {
80         CONN_ID conn;
81         conn = Client_Conn(Client);
82         if (Conn_Options(conn) & CONN_RFC1459) {
83                 /* RFC 1459 mode: separate NICK and USER commands */
84                 if (! Conn_WriteStr(conn, "NICK %s :%d",
85                                     Client_ID(User), Client_Hops(User) + 1))
86                         return DISCONNECTED;
87                 return Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
88                                      Client_ID(User), Client_User(User),
89                                      Client_Hostname(User),
90                                      Client_ID(Client_Introducer(User)),
91                                      Client_Info(User));
92         } else {
93                 /* RFC 2813 mode: one combined NICK command */
94                 return IRC_WriteStrClient(Client, "NICK %s %d %s %s %d +%s :%s",
95                                 Client_ID(User), Client_Hops(User) + 1,
96                                 Client_User(User), Client_Hostname(User),
97                                 Client_MyToken(Client_Introducer(User)),
98                                 Client_Modes(User), Client_Info(User));
99         }
100 } /* Announce_User */
101
102
103 #ifdef IRCPLUS
104
105 /**
106  * Synchronize invite and ban lists between servers
107  * @param Client New server
108  */
109 static bool
110 Synchronize_Lists(CLIENT * Client)
111 {
112         CHANNEL *c;
113         struct list_head *head;
114         struct list_elem *elem;
115
116         assert(Client != NULL);
117
118         c = Channel_First();
119         while (c) {
120                 /* ban list */
121                 head = Channel_GetListBans(c);
122                 elem = Lists_GetFirst(head);
123                 while (elem) {
124                         if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
125                                                 Channel_Name(c),
126                                                 Lists_GetMask(elem))) {
127                                 return DISCONNECTED;
128                         }
129                         elem = Lists_GetNext(elem);
130                 }
131
132                 /* invite list */
133                 head = Channel_GetListInvites(c);
134                 elem = Lists_GetFirst(head);
135                 while (elem) {
136                         if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
137                                                 Channel_Name(c),
138                                                 Lists_GetMask(elem))) {
139                                 return DISCONNECTED;
140                         }
141                         elem = Lists_GetNext(elem);
142                 }
143
144                 c = Channel_Next(c);
145         }
146         return CONNECTED;
147 }
148
149
150 /**
151  * Send CHANINFO commands to a new server (inform it about existing channels).
152  * @param Client New server
153  * @param Chan Channel
154  */
155 static bool
156 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
157 {
158         char *modes, *topic;
159         bool has_k, has_l;
160
161 #ifdef DEBUG
162         Log(LOG_DEBUG, "Sending CHANINFO commands ...");
163 #endif
164
165         modes = Channel_Modes(Chan);
166         topic = Channel_Topic(Chan);
167
168         if (!*modes && !*topic)
169                 return CONNECTED;
170
171         has_k = strchr(modes, 'k') != NULL;
172         has_l = strchr(modes, 'l') != NULL;
173
174         /* send CHANINFO */
175         if (!has_k && !has_l) {
176                 if (!*topic) {
177                         /* "CHANINFO <chan> +<modes>" */
178                         return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
179                                                   Channel_Name(Chan), modes);
180                 }
181                 /* "CHANINFO <chan> +<modes> :<topic>" */
182                 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
183                                           Channel_Name(Chan), modes, topic);
184         }
185         /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
186         return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
187                                   Channel_Name(Chan), modes,
188                                   has_k ? Channel_Key(Chan) : "*",
189                                   has_l ? Channel_MaxUsers(Chan) : 0, topic);
190 } /* Send_CHANINFO */
191
192 #endif /* IRCPLUS */
193
194
195 /**
196  * Handle ENDOFMOTD (376) numeric and login remote server.
197  * The peer is either an IRC server (no IRC+ protocol), or we got the
198  * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
199  */
200 GLOBAL bool
201 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
202 {
203         char str[LINE_LEN];
204         int max_hops, i;
205         CLIENT *c, *cl;
206         CHANNEL *chan;
207         CL2CHAN *cl2chan;
208
209         Client_SetType(Client, CLIENT_SERVER);
210
211         Log(LOG_NOTICE | LOG_snotice,
212             "Server \"%s\" registered (connection %d, 1 hop - direct link).",
213             Client_ID(Client), Client_Conn(Client));
214
215         /* Get highest hop count */
216         max_hops = 0;
217         c = Client_First();
218         while (c) {
219                 if (Client_Hops(c) > max_hops)
220                         max_hops = Client_Hops(c);
221                 c = Client_Next(c);
222         }
223
224         /* Inform the new server about all other servers, and announce the
225          * new server to all the already registered ones. Important: we have
226          * to do this "in order" and can't introduce servers of which the
227          * "toplevel server" isn't known already. */
228         for (i = 0; i < (max_hops + 1); i++) {
229                 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
230                         if (Client_Type(c) != CLIENT_SERVER)
231                                 continue;       /* not a server */
232                         if (Client_Hops(c) != i)
233                                 continue;       /* not actual "nesting level" */
234                         if (c == Client || c == Client_ThisServer())
235                                 continue;       /* that's us or the peer! */
236
237                         if (!Announce_Server(Client, c))
238                                 return DISCONNECTED;
239                 }
240         }
241
242         /* Announce all the users to the new server */
243         c = Client_First();
244         while (c) {
245                 if (Client_Type(c) == CLIENT_USER) {
246                         if (!Announce_User(Client, c))
247                                 return DISCONNECTED;
248                 }
249                 c = Client_Next(c);
250         }
251
252         /* Announce all channels to the new server */
253         chan = Channel_First();
254         while (chan) {
255 #ifdef IRCPLUS
256                 /* Send CHANINFO if the peer supports it */
257                 if (strchr(Client_Flags(Client), 'C')) {
258                         if (!Send_CHANINFO(Client, chan))
259                                 return DISCONNECTED;
260                 }
261 #endif
262
263                 /* Get all the members of this channel */
264                 cl2chan = Channel_FirstMember(chan);
265                 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(chan));
266                 while (cl2chan) {
267                         cl = Channel_GetClient(cl2chan);
268                         assert(cl != NULL);
269
270                         /* Nick name, with modes (if applicable) */
271                         if (str[strlen(str) - 1] != ':')
272                                 strlcat(str, ",", sizeof(str));
273                         if (strchr(Channel_UserModes(chan, cl), 'v'))
274                                 strlcat(str, "+", sizeof(str));
275                         if (strchr(Channel_UserModes(chan, cl), 'o'))
276                                 strlcat(str, "@", sizeof(str));
277                         strlcat(str, Client_ID(cl), sizeof(str));
278
279                         /* Send the data if the buffer is "full" */
280                         if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
281                                 if (!IRC_WriteStrClient(Client, "%s", str))
282                                         return DISCONNECTED;
283                                 snprintf(str, sizeof(str), "NJOIN %s :",
284                                          Channel_Name(chan));
285                         }
286
287                         cl2chan = Channel_NextMember(chan, cl2chan);
288                 }
289
290                 /* Data left in the buffer? */
291                 if (str[strlen(str) - 1] != ':') {
292                         /* Yes, send it ... */
293                         if (!IRC_WriteStrClient(Client, "%s", str))
294                                 return DISCONNECTED;
295                 }
296
297                 /* Get next channel ... */
298                 chan = Channel_Next(chan);
299         }
300
301 #ifdef IRCPLUS
302         if (strchr(Client_Flags(Client), 'L')) {
303                 LogDebug("Synchronizing INVITE- and BAN-lists ...");
304                 if (!Synchronize_Lists(Client))
305                         return DISCONNECTED;
306         }
307 #endif
308
309         return CONNECTED;
310 } /* IRC_Num_ENDOFMOTD */
311
312
313 /**
314  * Handle ISUPPORT (005) numeric.
315  */
316 GLOBAL bool
317 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
318 {
319         int i;
320         char *key, *value;
321
322         for (i = 1; i < Req->argc - 1; i++) {
323                 key = Req->argv[i];
324                 value = strchr(key, '=');
325                 if (value)
326                         *value++ = '\0';
327                 else
328                         value = "";
329
330                 if (strcmp("NICKLEN", key) == 0) {
331                         if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
332                                 continue;
333
334                         /* Nick name length settings are different! */
335                         Log(LOG_ERR,
336                             "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
337                             Conf_MaxNickLength - 1, atoi(value));
338                         Conn_Close(Client_Conn(Client),
339                                    "Incompatible nick name length",
340                                    NULL, false);
341                         return DISCONNECTED;
342                 }
343         }
344
345         return CONNECTED;
346 } /* IRC_Num_ISUPPORT */
347
348
349 /* -eof- */