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