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