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