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