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