Enhance debug messages while sending CHANINFO commands
[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  * Send a specific list to a remote server.
149  */
150 static bool
151 Send_List(CLIENT *Client, CHANNEL *Chan, struct list_head *Head, char Type)
152 {
153         struct list_elem *elem;
154
155         elem = Lists_GetFirst(Head);
156         while (elem) {
157                 if (!IRC_WriteStrClient(Client, "MODE %s +%c %s",
158                                         Channel_Name(Chan), Type,
159                                         Lists_GetMask(elem))) {
160                         return DISCONNECTED;
161                 }
162                 elem = Lists_GetNext(elem);
163         }
164         return CONNECTED;
165 }
166
167 /**
168  * Synchronize invite, ban, except, and G-Line lists between servers.
169  *
170  * @param Client New server.
171  * @return CONNECTED or DISCONNECTED.
172  */
173 static bool
174 Synchronize_Lists(CLIENT * Client)
175 {
176         CHANNEL *c;
177         struct list_head *head;
178         struct list_elem *elem;
179
180         assert(Client != NULL);
181
182         /* g-lines */
183         head = Class_GetList(CLASS_GLINE);
184         elem = Lists_GetFirst(head);
185         while (elem) {
186                 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
187                                         Lists_GetMask(elem),
188                                         Lists_GetValidity(elem) - time(NULL),
189                                         Lists_GetReason(elem)))
190                         return DISCONNECTED;
191                 elem = Lists_GetNext(elem);
192         }
193
194         c = Channel_First();
195         while (c) {
196                 if (!Send_List(Client, c, Channel_GetListExcepts(c), 'e'))
197                         return DISCONNECTED;
198                 if (!Send_List(Client, c, Channel_GetListBans(c), 'b'))
199                         return DISCONNECTED;
200                 if (!Send_List(Client, c, Channel_GetListInvites(c), 'I'))
201                         return DISCONNECTED;
202                 c = Channel_Next(c);
203         }
204         return CONNECTED;
205 }
206
207 /**
208  * Send CHANINFO commands to a new server (inform it about existing channels).
209  * @param Client New server
210  * @param Chan Channel
211  */
212 static bool
213 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
214 {
215         char *modes, *topic;
216         bool has_k, has_l;
217
218 #ifdef DEBUG
219         Log(LOG_DEBUG, "Sending CHANINFO commands for \"%s\" ...",
220             Channel_Name(Chan));
221 #endif
222
223         modes = Channel_Modes(Chan);
224         topic = Channel_Topic(Chan);
225
226         if (!*modes && !*topic)
227                 return CONNECTED;
228
229         has_k = Channel_HasMode(Chan, 'k');
230         has_l = Channel_HasMode(Chan, 'l');
231
232         /* send CHANINFO */
233         if (!has_k && !has_l) {
234                 if (!*topic) {
235                         /* "CHANINFO <chan> +<modes>" */
236                         return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
237                                                   Channel_Name(Chan), modes);
238                 }
239                 /* "CHANINFO <chan> +<modes> :<topic>" */
240                 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
241                                           Channel_Name(Chan), modes, topic);
242         }
243         /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
244         return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
245                                   Channel_Name(Chan), modes,
246                                   has_k ? Channel_Key(Chan) : "*",
247                                   has_l ? Channel_MaxUsers(Chan) : 0, topic);
248 } /* Send_CHANINFO */
249
250 #endif /* IRCPLUS */
251
252 /**
253  * Handle ENDOFMOTD (376) numeric and login remote server.
254  * The peer is either an IRC server (no IRC+ protocol), or we got the
255  * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
256  */
257 GLOBAL bool
258 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
259 {
260         int max_hops, i;
261         CLIENT *c;
262         CHANNEL *chan;
263
264         Client_SetType(Client, CLIENT_SERVER);
265
266         Log(LOG_NOTICE | LOG_snotice,
267             "Server \"%s\" registered (connection %d, 1 hop - direct link).",
268             Client_ID(Client), Client_Conn(Client));
269
270         /* Get highest hop count */
271         max_hops = 0;
272         c = Client_First();
273         while (c) {
274                 if (Client_Hops(c) > max_hops)
275                         max_hops = Client_Hops(c);
276                 c = Client_Next(c);
277         }
278
279         /* Inform the new server about all other servers, and announce the
280          * new server to all the already registered ones. Important: we have
281          * to do this "in order" and can't introduce servers of which the
282          * "toplevel server" isn't known already. */
283         for (i = 0; i < (max_hops + 1); i++) {
284                 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
285                         if (Client_Type(c) != CLIENT_SERVER)
286                                 continue;       /* not a server */
287                         if (Client_Hops(c) != i)
288                                 continue;       /* not actual "nesting level" */
289                         if (c == Client || c == Client_ThisServer())
290                                 continue;       /* that's us or the peer! */
291
292                         if (!Announce_Server(Client, c))
293                                 return DISCONNECTED;
294                 }
295         }
296
297         /* Announce all the users to the new server */
298         c = Client_First();
299         while (c) {
300                 if (Client_Type(c) == CLIENT_USER ||
301                     Client_Type(c) == CLIENT_SERVICE) {
302                         if (!Client_Announce(Client, Client_ThisServer(), c))
303                                 return DISCONNECTED;
304                 }
305                 c = Client_Next(c);
306         }
307
308         /* Announce all channels to the new server */
309         chan = Channel_First();
310         while (chan) {
311                 if (Channel_IsLocal(chan)) {
312                         chan = Channel_Next(chan);
313                         continue;
314                 }
315 #ifdef IRCPLUS
316                 /* Send CHANINFO if the peer supports it */
317                 if (Client_HasFlag(Client, 'C')) {
318                         if (!Send_CHANINFO(Client, chan))
319                                 return DISCONNECTED;
320                 }
321 #endif
322
323                 if (!Announce_Channel(Client, chan))
324                         return DISCONNECTED;
325
326                 /* Get next channel ... */
327                 chan = Channel_Next(chan);
328         }
329
330 #ifdef IRCPLUS
331         if (Client_HasFlag(Client, 'L')) {
332                 LogDebug("Synchronizing INVITE- and BAN-lists ...");
333                 if (!Synchronize_Lists(Client))
334                         return DISCONNECTED;
335         }
336 #endif
337
338         if (!IRC_WriteStrClient(Client, "PING :%s",
339             Client_ID(Client_ThisServer())))
340                 return DISCONNECTED;
341
342         return CONNECTED;
343 } /* IRC_Num_ENDOFMOTD */
344
345 /**
346  * Handle ISUPPORT (005) numeric.
347  */
348 GLOBAL bool
349 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
350 {
351         int i;
352         char *key, *value;
353
354         for (i = 1; i < Req->argc - 1; i++) {
355                 key = Req->argv[i];
356                 value = strchr(key, '=');
357                 if (value)
358                         *value++ = '\0';
359                 else
360                         value = "";
361
362                 if (strcmp("NICKLEN", key) == 0) {
363                         if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
364                                 continue;
365
366                         /* Nickname length settings are different! */
367                         Log(LOG_ERR,
368                             "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
369                             Conf_MaxNickLength - 1, atoi(value));
370                         Conn_Close(Client_Conn(Client),
371                                    "Incompatible nickname length",
372                                    NULL, false);
373                         return DISCONNECTED;
374                 }
375         }
376
377         return CONNECTED;
378 } /* IRC_Num_ISUPPORT */
379
380 /* -eof- */