]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/numeric.c
Remove imp.h and exp.h header files
[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 <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "defines.h"
25 #include "conn.h"
26 #include "conf.h"
27 #include "conn.h"
28 #include "conn-func.h"
29 #include "channel.h"
30 #include "class.h"
31 #include "irc-write.h"
32 #include "lists.h"
33 #include "log.h"
34 #include "messages.h"
35 #include "parse.h"
36
37 #include "numeric.h"
38
39 /**
40  * Announce a channel and its users in the network.
41  */
42 static bool
43 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
44 {
45         CL2CHAN *cl2chan;
46         CLIENT *cl;
47         char str[COMMAND_LEN], *ptr;
48         bool njoin, xop;
49
50         /* Check features of remote server */
51         njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
52         xop = Client_HasFlag(Client, 'X') ? true : false;
53
54         /* Get all the members of this channel */
55         cl2chan = Channel_FirstMember(Chan);
56         snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
57         while (cl2chan) {
58                 cl = Channel_GetClient(cl2chan);
59                 assert(cl != NULL);
60
61                 if (njoin) {
62                         /* RFC 2813: send NJOIN with nicknames and modes
63                          * (if user is channel operator or has voice) */
64                         if (str[strlen(str) - 1] != ':')
65                                 strlcat(str, ",", sizeof(str));
66
67                         /* Prepare user prefix (ChanOp, voiced, ...) */
68                         if (xop && Channel_UserHasMode(Chan, cl, 'q'))
69                                 strlcat(str, "~", sizeof(str));
70                         if (xop && Channel_UserHasMode(Chan, cl, 'a'))
71                                 strlcat(str, "&", sizeof(str));
72                         if (Channel_UserHasMode(Chan, cl, 'o'))
73                                 strlcat(str, "@", sizeof(str));
74                         if (xop && Channel_UserHasMode(Chan, cl, 'h'))
75                                 strlcat(str, "%", sizeof(str));
76                         if (Channel_UserHasMode(Chan, cl, 'v'))
77                                 strlcat(str, "+", sizeof(str));
78
79                         strlcat(str, Client_ID(cl), sizeof(str));
80
81                         /* Send the data if the buffer is "full" */
82                         if (strlen(str) > (sizeof(str) - CLIENT_NICK_LEN - 8)) {
83                                 if (!IRC_WriteStrClient(Client, "%s", str))
84                                         return DISCONNECTED;
85                                 snprintf(str, sizeof(str), "NJOIN %s :",
86                                          Channel_Name(Chan));
87                         }
88                 } else {
89                         /* RFC 1459: no NJOIN, send JOIN and MODE */
90                         if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
91                                                 Channel_Name(Chan)))
92                                 return DISCONNECTED;
93                         ptr = Channel_UserModes(Chan, cl);
94                         while (*ptr) {
95                                 if (!IRC_WriteStrClientPrefix(Client, cl,
96                                                    "MODE %s +%c %s",
97                                                    Channel_Name(Chan), ptr[0],
98                                                    Client_ID(cl)))
99                                         return DISCONNECTED;
100                                 ptr++;
101                         }
102                 }
103
104                 cl2chan = Channel_NextMember(Chan, cl2chan);
105         }
106
107         /* Data left in the buffer? */
108         if (str[strlen(str) - 1] != ':') {
109                 /* Yes, send it ... */
110                 if (!IRC_WriteStrClient(Client, "%s", str))
111                         return DISCONNECTED;
112         }
113
114         return CONNECTED;
115 } /* Announce_Channel */
116
117 /**
118  * Announce new server in the network
119  * @param Client New server
120  * @param Server Existing server in the network
121  */
122 static bool
123 Announce_Server(CLIENT * Client, CLIENT * Server)
124 {
125         CLIENT *c;
126
127         if (Client_Conn(Server) > NONE) {
128                 /* Announce the new server to the one already registered
129                  * which is directly connected to the local server */
130                 if (!IRC_WriteStrClient
131                     (Server, "SERVER %s %d %d :%s", Client_ID(Client),
132                      Client_Hops(Client) + 1, Client_MyToken(Client),
133                      Client_Info(Client)))
134                         return DISCONNECTED;
135         }
136
137         if (Client_Hops(Server) == 1)
138                 c = Client_ThisServer();
139         else
140                 c = Client_TopServer(Server);
141
142         /* Inform new server about the one already registered in the network */
143         return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
144                 Client_ID(Server), Client_Hops(Server) + 1,
145                 Client_MyToken(Server), Client_Info(Server));
146 } /* Announce_Server */
147
148 #ifdef IRCPLUS
149
150 /**
151  * Synchronize invite, ban, G- and K-Line lists between servers.
152  *
153  * @param Client New server.
154  * @return CONNECTED or DISCONNECTED.
155  */
156 static bool
157 Synchronize_Lists(CLIENT * Client)
158 {
159         CHANNEL *c;
160         struct list_head *head;
161         struct list_elem *elem;
162
163         assert(Client != NULL);
164
165         /* g-lines */
166         head = Class_GetList(CLASS_GLINE);
167         elem = Lists_GetFirst(head);
168         while (elem) {
169                 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
170                                         Lists_GetMask(elem),
171                                         Lists_GetValidity(elem) - time(NULL),
172                                         Lists_GetReason(elem)))
173                         return DISCONNECTED;
174                 elem = Lists_GetNext(elem);
175         }
176
177         c = Channel_First();
178         while (c) {
179                 /* ban list */
180                 head = Channel_GetListBans(c);
181                 elem = Lists_GetFirst(head);
182                 while (elem) {
183                         if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
184                                                 Channel_Name(c),
185                                                 Lists_GetMask(elem))) {
186                                 return DISCONNECTED;
187                         }
188                         elem = Lists_GetNext(elem);
189                 }
190
191                 /* invite list */
192                 head = Channel_GetListInvites(c);
193                 elem = Lists_GetFirst(head);
194                 while (elem) {
195                         if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
196                                                 Channel_Name(c),
197                                                 Lists_GetMask(elem))) {
198                                 return DISCONNECTED;
199                         }
200                         elem = Lists_GetNext(elem);
201                 }
202
203                 c = Channel_Next(c);
204         }
205         return CONNECTED;
206 }
207
208 /**
209  * Send CHANINFO commands to a new server (inform it about existing channels).
210  * @param Client New server
211  * @param Chan Channel
212  */
213 static bool
214 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
215 {
216         char *modes, *topic;
217         bool has_k, has_l;
218
219 #ifdef DEBUG
220         Log(LOG_DEBUG, "Sending CHANINFO commands ...");
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- */