]> jfr.im git - irc/evilnet/x3.git/blob - src/proto-p10.c
mod-python: add emb_svsquit function
[irc/evilnet/x3.git] / src / proto-p10.c
1 /* proto-p10.c - IRC protocol output
2 * Copyright 2000-2004 srvx Development Team
3 *
4 * This file is part of x3.
5 *
6 * x3 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 #include "nickserv.h"
22 #include "chanserv.h"
23 #include "hash.h"
24 #include "helpfile.h"
25 #include "hosthiding.h"
26 #include "proto-common.c"
27 #include "opserv.h"
28
29 /* Full commands. */
30 #define CMD_ACCOUNT "ACCOUNT"
31 #define CMD_ADMIN "ADMIN"
32 #define CMD_ALIST "ALIST"
33 #define CMD_ASLL "ASLL"
34 #define CMD_AWAY "AWAY"
35 #define CMD_BURST "BURST"
36 #define CMD_CLEARMODE "CLEARMODE"
37 #define CMD_CLOSE "CLOSE"
38 #define CMD_CNOTICE "CNOTICE"
39 #define CMD_CONNECT "CONNECT"
40 #define CMD_CPRIVMSG "CPRIVMSG"
41 #define CMD_CREATE "CREATE"
42 #define CMD_DESTRUCT "DESTRUCT"
43 #define CMD_DESYNCH "DESYNCH"
44 #define CMD_DIE "DIE"
45 #define CMD_DNS "DNS"
46 #define CMD_EOB "END_OF_BURST"
47 #define CMD_EOB_ACK "EOB_ACK"
48 #define CMD_ERROR "ERROR"
49 #define CMD_FAKEHOST "FAKE"
50 #define CMD_GET "GET"
51 #define CMD_GLINE "GLINE"
52 #define CMD_HASH "HASH"
53 #define CMD_HELP "HELP"
54 #define CMD_INFO "INFO"
55 #define CMD_INVITE "INVITE"
56 #define CMD_ISON "ISON"
57 #define CMD_JOIN "JOIN"
58 #define CMD_JUPE "JUPE"
59 #define CMD_KICK "KICK"
60 #define CMD_KILL "KILL"
61 #define CMD_LINKS "LINKS"
62 #define CMD_LIST "LIST"
63 #define CMD_LUSERS "LUSERS"
64 #define CMD_MAP "MAP"
65 #define CMD_MARK "MARK"
66 #define CMD_MODE "MODE"
67 #define CMD_MOTD "MOTD"
68 #define CMD_NAMES "NAMES"
69 #define CMD_NICK "NICK"
70 #define CMD_NOTICE "NOTICE"
71 #define CMD_OPER "OPER"
72 #define CMD_OPMODE "OPMODE"
73 #define CMD_PART "PART"
74 #define CMD_PASS "PASS"
75 #define CMD_PING "PING"
76 #define CMD_PONG "PONG"
77 #define CMD_POST "POST"
78 #define CMD_PRIVMSG "PRIVMSG"
79 #define CMD_PRIVS "PRIVS"
80 #define CMD_PROTO "PROTO"
81 #define CMD_QUIT "QUIT"
82 #define CMD_REHASH "REHASH"
83 #define CMD_RESET "RESET"
84 #define CMD_RESTART "RESTART"
85 #define CMD_RPING "RPING"
86 #define CMD_RPONG "RPONG"
87 #define CMD_SERVER "SERVER"
88 #define CMD_SERVLIST "SERVLIST"
89 #define CMD_SERVSET "SERVSET"
90 #define CMD_SET "SET"
91 #define CMD_SETTIME "SETTIME"
92 #define CMD_SGLINE "SGLINE"
93 #define CMD_SHUN "SHUN"
94 #define CMD_SILENCE "SILENCE"
95 #define CMD_SMO "SMO"
96 #define CMD_SNO "SNO"
97 #define CMD_SSHUN "SSHUN"
98 #define CMD_SPAMFILTER "SPAMFILTER"
99 #define CMD_SQUERY "SQUERY"
100 #define CMD_SQUIT "SQUIT"
101 #define CMD_STATS "STATS"
102 #define CMD_SVSJOIN "SVSJOIN"
103 #define CMD_SVSNICK "SVSNICK"
104 #define CMD_SVSPART "SVSPART"
105 #define CMD_SVSQUIT "SVSQUIT"
106 #define CMD_SWHOIS "SWHOIS"
107 #define CMD_TIME "TIME"
108 #define CMD_TOPIC "TOPIC"
109 #define CMD_TRACE "TRACE"
110 #define CMD_UPING "UPING"
111 #define CMD_USER "USER"
112 #define CMD_USERHOST "USERHOST"
113 #define CMD_USERIP "USERIP"
114 #define CMD_VERSION "VERSION"
115 #define CMD_WALLCHOPS "WALLCHOPS"
116 #define CMD_WALLOPS "WALLOPS"
117 #define CMD_WALLHOPS "WALLHOPS"
118 #define CMD_WALLUSERS "WALLUSERS"
119 #define CMD_WALLVOICES "WALLVOICES"
120 #define CMD_WALLHOPS "WALLHOPS"
121 #define CMD_WHO "WHO"
122 #define CMD_WHOIS "WHOIS"
123 #define CMD_WHOWAS "WHOWAS"
124
125 /* Tokenized commands. */
126 #define TOK_ACCOUNT "AC"
127 #define TOK_ADMIN "AD"
128 #define TOK_ALIST "AL"
129 #define TOK_ASLL "LL"
130 #define TOK_AWAY "A"
131 #define TOK_BURST "B"
132 #define TOK_CLEARMODE "CM"
133 #define TOK_CLOSE "CLOSE"
134 #define TOK_CNOTICE "CN"
135 #define TOK_CONNECT "CO"
136 #define TOK_CPRIVMSG "CP"
137 #define TOK_CREATE "C"
138 #define TOK_DESTRUCT "DE"
139 #define TOK_DESYNCH "DS"
140 #define TOK_DIE "DIE"
141 #define TOK_DNS "DNS"
142 #define TOK_EOB "EB"
143 #define TOK_EOB_ACK "EA"
144 #define TOK_ERROR "Y"
145 #define TOK_EXEMPT "EX"
146 #define TOK_FAKEHOST "FA"
147 #define TOK_GET "GET"
148 #define TOK_GLINE "GL"
149 #define TOK_HASH "HASH"
150 #define TOK_HELP "HELP"
151 #define TOK_INFO "F"
152 #define TOK_INVITE "I"
153 #define TOK_ISON "ISON"
154 #define TOK_JOIN "J"
155 #define TOK_JUPE "JU"
156 #define TOK_KICK "K"
157 #define TOK_KILL "D"
158 #define TOK_LINKS "LI"
159 #define TOK_LIST "LIST"
160 #define TOK_LUSERS "LU"
161 #define TOK_MAP "MAP"
162 #define TOK_MARK "MK"
163 #define TOK_MODE "M"
164 #define TOK_MOTD "MO"
165 #define TOK_NAMES "E"
166 #define TOK_NICK "N"
167 #define TOK_NOTICE "O"
168 #define TOK_OPER "OPER"
169 #define TOK_OPMODE "OM"
170 #define TOK_PART "L"
171 #define TOK_PASS "PA"
172 #define TOK_PING "G"
173 #define TOK_PONG "Z"
174 #define TOK_POST "POST"
175 #define TOK_PRIVMSG "P"
176 #define TOK_PRIVS "PRIVS"
177 #define TOK_PROTO "PROTO"
178 #define TOK_QUIT "Q"
179 #define TOK_REHASH "REHASH"
180 #define TOK_RESET "RESET"
181 #define TOK_RESTART "RESTART"
182 #define TOK_RPING "RI"
183 #define TOK_RPONG "RO"
184 #define TOK_SERVER "S"
185 #define TOK_SERVLIST "SERVSET"
186 #define TOK_SERVSET "SERVSET"
187 #define TOK_SET "SET"
188 #define TOK_SETTIME "SE"
189 #define TOK_SGLINE "SGL"
190 #define TOK_SHUN "SU"
191 #define TOK_SILENCE "U"
192 #define TOK_SMO "SMO"
193 #define TOK_SNO "SNO"
194 #define TOK_SSHUN "SSU"
195 #define TOK_SPAMFILTER "SF"
196 #define TOK_SQUERY "SQUERY"
197 #define TOK_SQUIT "SQ"
198 #define TOK_STATS "R"
199 #define TOK_SVSJOIN "SJ"
200 #define TOK_SVSNICK "SN"
201 #define TOK_SVSPART "SP"
202 #define TOK_SVSQUIT "SX"
203 #define TOK_SWHOIS "SW"
204 #define TOK_TIME "TI"
205 #define TOK_TOPIC "T"
206 #define TOK_TRACE "TR"
207 #define TOK_UPING "UP"
208 #define TOK_USER "USER"
209 #define TOK_USERHOST "USERHOST"
210 #define TOK_USERIP "USERIP"
211 #define TOK_VERSION "V"
212 #define TOK_WALLCHOPS "WC"
213 #define TOK_WALLOPS "WA"
214 #define TOK_WALLHOPS "WH"
215 #define TOK_WALLUSERS "WU"
216 #define TOK_WALLVOICES "WV"
217 #define TOK_WALLHOPS "WH"
218 #define TOK_WHO "H"
219 #define TOK_WHOIS "W"
220 #define TOK_WHOWAS "X"
221
222 /* Protocol messages; aliased to full commands or tokens depending
223 on compile-time configuration. ircu prefers tokens WITH THE
224 EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
225 tokenized, because clients' (ie. a linking server) commands are
226 only checked against the full command list.
227 */
228 #if defined(ENABLE_TOKENS)
229 #define TYPE(NAME) TOK_ ## NAME
230 #else /* !ENABLE_TOKENS */
231 #define TYPE(NAME) CMD_ ## NAME
232 #endif /* ENABLE_TOKENS */
233
234 #define P10_ACCOUNT TYPE(ACCOUNT)
235 #define P10_ADMIN TYPE(ADMIN)
236 #define P10_ASLL TYPE(ASLL)
237 #define P10_AWAY TYPE(AWAY)
238 #define P10_BURST TYPE(BURST)
239 #define P10_CLEARMODE TYPE(CLEARMODE)
240 #define P10_CLOSE TYPE(CLOSE)
241 #define P10_CNOTICE TYPE(CNOTICE)
242 #define P10_CONNECT TYPE(CONNECT)
243 #define P10_CPRIVMSG TYPE(CPRIVMSG)
244 #define P10_CREATE TYPE(CREATE)
245 #define P10_DESTRUCT TYPE(DESTRUCT)
246 #define P10_DESYNCH TYPE(DESYNCH)
247 #define P10_DIE TYPE(DIE)
248 #define P10_DNS TYPE(DNS)
249 #define P10_EOB TYPE(EOB)
250 #define P10_EOB_ACK TYPE(EOB_ACK)
251 #define P10_ERROR TYPE(ERROR)
252 #define P10_FAKEHOST TYPE(FAKEHOST)
253 #define P10_GET TYPE(GET)
254 #define P10_GLINE TYPE(GLINE)
255 #define P10_HASH TYPE(HASH)
256 #define P10_HELP TYPE(HELP)
257 #define P10_INFO TYPE(INFO)
258 #define P10_INVITE TYPE(INVITE)
259 #define P10_ISON TYPE(ISON)
260 #define P10_JOIN TYPE(JOIN)
261 #define P10_JUPE TYPE(JUPE)
262 #define P10_KICK TYPE(KICK)
263 #define P10_KILL TYPE(KILL)
264 #define P10_LINKS TYPE(LINKS)
265 #define P10_LIST TYPE(LIST)
266 #define P10_LUSERS TYPE(LUSERS)
267 #define P10_MAP TYPE(MAP)
268 #define P10_MARK TYPE(MARK)
269 #define P10_MODE TYPE(MODE)
270 #define P10_MOTD TYPE(MOTD)
271 #define P10_NAMES TYPE(NAMES)
272 #define P10_NICK TYPE(NICK)
273 #define P10_NOTICE TYPE(NOTICE)
274 #define P10_OPER TYPE(OPER)
275 #define P10_OPMODE TYPE(OPMODE)
276 #define P10_PART TYPE(PART)
277 #define P10_PASS CMD_PASS
278 #define P10_PING TYPE(PING)
279 #define P10_PONG TYPE(PONG)
280 #define P10_POST TYPE(POST)
281 #define P10_PRIVMSG TYPE(PRIVMSG)
282 #define P10_PRIVS TYPE(PRIVS)
283 #define P10_PROTO TYPE(PROTO)
284 #define P10_QUIT TYPE(QUIT)
285 #define P10_REHASH TYPE(REHASH)
286 #define P10_RESET TYPE(RESET)
287 #define P10_RESTART TYPE(RESTART)
288 #define P10_RPING TYPE(RPING)
289 #define P10_RPONG TYPE(RPONG)
290 #define P10_SERVER CMD_SERVER
291 #define P10_SERVLIST TYPE(SERVLIST)
292 #define P10_SERVSET TYPE(SERVSET)
293 #define P10_SET TYPE(SET)
294 #define P10_SETTIME TYPE(SETTIME)
295 #define P10_SGLINE TYPE(SGLINE)
296 #define P10_SHUN TYPE(SHUN)
297 #define P10_SILENCE TYPE(SILENCE)
298 #define P10_SMO TYPE(SMO)
299 #define P10_SNO TYPE(SNO)
300 #define P10_SSHUN TYPE(SSHUN)
301 #define P10_SPAMFILTER TYPE(SPAMFILTER)
302 #define P10_SQUERY TYPE(SQUERY)
303 #define P10_SQUIT TYPE(SQUIT)
304 #define P10_STATS TYPE(STATS)
305 #define P10_SVSJOIN TYPE(SVSJOIN)
306 #define P10_SVSNICK TYPE(SVSNICK)
307 #define P10_SVSPART TYPE(SVSPART)
308 #define P10_SVSQUIT TYPE(SVSQUIT)
309 #define P10_SWHOIS TYPE(SWHOIS)
310 #define P10_TIME TYPE(TIME)
311 #define P10_TOPIC TYPE(TOPIC)
312 #define P10_TRACE TYPE(TRACE)
313 #define P10_UPING TYPE(UPING)
314 #define P10_USER TYPE(USER)
315 #define P10_USERHOST TYPE(USERHOST)
316 #define P10_USERIP TYPE(USERIP)
317 #define P10_VERSION TYPE(VERSION)
318 #define P10_WALLCHOPS TYPE(WALLCHOPS)
319 #define P10_WALLOPS TYPE(WALLOPS)
320 #define P10_WALLHOPS TYPE(WALLHOPS)
321 #define P10_WALLUSERS TYPE(WALLUSERS)
322 #define P10_WALLVOICES TYPE(WALLVOICES)
323 #define P10_WHO TYPE(WHO)
324 #define P10_WHOIS TYPE(WHOIS)
325 #define P10_WHOWAS TYPE(WHOWAS)
326 #define P10_EXEMPT TYPE(EXEMPT)
327
328 /* Servers claiming to have a boot or link time before PREHISTORY
329 * trigger errors to the log. We hope no server has been running
330 * constantly since September 1994. :)
331 */
332 #define PREHISTORY 780000000
333
334 #define MODELEN 40 + KEYLEN
335
336 static struct server *servers_num[64*64];
337 static privmsg_func_t *privmsg_funcs;
338 static unsigned int num_privmsg_funcs;
339 static privmsg_func_t *notice_funcs;
340 static unsigned int num_notice_funcs;
341 static struct dict *unbursted_channels;
342 static const char *his_servername;
343 static const char *his_servercomment;
344 static int extended_accounts;
345
346 /* These correspond to 1 << X: 012345678901234567 */
347 const char irc_user_mode_chars[] = "o iw dkgn I";
348
349 static struct userNode *AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip);
350
351 extern int off_channel;
352 extern int DefConLevel;
353 extern int DefConTimeOut;
354 extern char *DefConChanModes;
355
356 static int parse_oplevel(char *str);
357
358 char privbuf[512] = "";
359
360 /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
361 * server and Y's indentifying the client on that server. */
362 struct server*
363 GetServerN(const char *numeric)
364 {
365 switch (strlen(numeric)) {
366 default:
367 return servers_num[base64toint(numeric, 2)];
368 case 4:
369 case 3:
370 case 1:
371 return servers_num[base64toint(numeric, 1)];
372 case 0:
373 return NULL;
374 }
375 }
376
377 struct userNode*
378 GetUserN(const char *numeric) /* using numeric */
379 {
380 struct userNode *un;
381 struct server *s;
382 int n, slen, ulen;
383
384 switch (strlen(numeric)) {
385 default:
386 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
387 return NULL;
388 case 5: slen = 2; ulen = 3; break;
389 case 4: slen = 1; ulen = 3; break;
390 case 3: slen = 1; ulen = 2; break;
391 case 2: case 1: case 0:
392 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
393 return NULL;
394 }
395 if (!(s = servers_num[base64toint(numeric, slen)])) {
396 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
397 return NULL;
398 }
399 n = base64toint(numeric+slen, ulen) & s->num_mask;
400 if (!(un = s->users[n])) {
401 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
402 }
403 return un;
404 }
405
406 extern struct userNode *opserv;
407 static void
408 check_ctcp(struct userNode *user, struct userNode *bot, char *text, UNUSED_ARG(int server_qualified))
409 {
410 char *cmd;
411 /* if its a version reply, do an alert check (only alerts with version=something) */
412 if(bot == opserv) {
413 if(text[0] == '\001') {
414 text++;
415 cmd = mysep(&text, " ");
416 if(!irccasecmp(cmd, "VERSION")) {
417 char *version = mysep(&text, "\n");
418 if(!version)
419 version = "";
420 /* opserv_debug("Opserv got CTCP VERSION Notice from %s: %s", user->nick, version); */
421 /* TODO: setup a ctcp_funcs thing to handle this and other CTCPS properly */
422 user->version_reply = strdup(version);
423 /* TODO: put this in the db */
424 if(match_ircglob(version, "WebTV;*"))
425 user->no_notice = true; /* webbies cant see notices */
426 }
427 }
428 }
429 }
430
431
432 static void
433 privmsg_user_helper(struct userNode *un, void *data)
434 {
435 struct privmsg_desc *pd = data;
436 unsigned int num = un->num_local;
437 if (!pd->is_notice) {
438 if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
439 privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
440 }
441 } else {
442 if ((num < num_notice_funcs) && notice_funcs[num]) {
443 check_ctcp(pd->user, un, pd->text, pd->is_qualified);
444 notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
445 }
446 }
447 }
448
449 /* equiv to user doing /connect server port target */
450 void irc_connect(struct userNode *user, char *server, unsigned int port, struct server *target)
451 {
452 putsock("%s " P10_CONNECT " %s %d %s", user->numeric, server, port, target->numeric);
453 }
454
455 void
456 irc_squit_route(struct server *srv, const char *message, ...)
457 {
458 va_list arg_list;
459 char buffer[MAXLEN];
460 va_start(arg_list, message);
461 vsnprintf(buffer, MAXLEN-2, message, arg_list);
462 buffer[MAXLEN-1] = 0;
463
464 /* When would we squit ourselves exactly?? -Rubin */
465 if(srv == self && cManager.uplink->state == CONNECTED ) {
466 unsigned int i;
467
468 /* Quit all clients linked to me. */
469 for(i = 0; i <= self->num_mask; i++) {
470 if(!self->users[i])
471 continue;
472 irc_quit(self->users[i], buffer);
473 }
474 }
475
476 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, buffer);
477
478 if(srv == self) {
479 /* Force a reconnect to the currently selected server. */
480 cManager.uplink->tries = 0;
481 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", buffer);
482 close_socket();
483 }
484 }
485
486 void
487 irc_server(struct server *srv)
488 {
489 char extranum[COMBO_NUMERIC_LEN+1];
490
491 inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
492 if (srv == self) {
493 putsock(P10_SERVER " %s %d " FMT_TIME_T " " FMT_TIME_T " J10 %s%s +s6 :%s",
494 srv->name, srv->hops+1, srv->boot, srv->link_time, srv->numeric, extranum, srv->description);
495 } else {
496 putsock("%s " P10_SERVER " %s %d " FMT_TIME_T " " FMT_TIME_T " %c10 %s%s +s6 :%s",
497 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link_time, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
498 }
499 }
500
501 void
502 irc_p10_pton(irc_in_addr_t *ip, const char *input)
503 {
504 if (strlen(input) == 6) {
505 unsigned int value;
506 memset(ip, 0, 6 * sizeof(ip->in6[0]));
507 value = base64toint(input, 6);
508 if (value)
509 ip->in6[5] = htons(65535);
510 ip->in6[6] = htons(value >> 16);
511 ip->in6[7] = htons(value & 65535);
512 } else {
513 unsigned int pos = 0;
514 do {
515 if (*input == '_') {
516 unsigned int left;
517 for (left = (25 - strlen(input)) / 3; left; left--)
518 ip->in6[pos++] = 0;
519 input++;
520 } else {
521 ip->in6[pos++] = ntohs(base64toint(input, 3));
522 input += 3;
523 }
524 } while (pos < 8);
525 }
526 }
527
528 void
529 irc_p10_ntop(char *output, const irc_in_addr_t *ip)
530 {
531 if (!irc_in_addr_is_valid(*ip)) {
532 strcpy(output, "AAAAAA");
533 } else if (irc_in_addr_is_ipv4(*ip)) {
534 unsigned int in4;
535 in4 = (ntohs(ip->in6[6]) << 16) | ntohs(ip->in6[7]);
536 inttobase64(output, in4, 6);
537 output[6] = '\0';
538 } else if (irc_in_addr_is_ipv6(*ip)) {
539 unsigned int max_start, max_zeros, curr_zeros, zero, ii;
540 /* Can start by printing out the leading non-zero parts. */
541 for (ii = 0; (ip->in6[ii]) && (ii < 8); ++ii) {
542 inttobase64(output, ntohs(ip->in6[ii]), 3);
543 output += 3;
544 }
545 /* Find the longest run of zeros. */
546 for (max_start = zero = ii, max_zeros = curr_zeros = 0; ii < 8; ++ii) {
547 if (!ip->in6[ii])
548 curr_zeros++;
549 else if (curr_zeros > max_zeros) {
550 max_start = ii - curr_zeros;
551 max_zeros = curr_zeros;
552 curr_zeros = 0;
553 }
554 }
555 if (curr_zeros > max_zeros) {
556 max_start = ii - curr_zeros;
557 max_zeros = curr_zeros;
558 curr_zeros = 0;
559 }
560 /* Print the rest of the address */
561 for (ii = zero; ii < 8; ) {
562 if ((ii == max_start) && max_zeros) {
563 *output++ = '_';
564 ii += max_zeros;
565 } else {
566 inttobase64(output, ntohs(ip->in6[ii]), 3);
567 output += 3;
568 }
569 }
570 *output = '\0';
571 } else {
572 strcpy(output, "???");
573 }
574 }
575
576 void
577 irc_user(struct userNode *user)
578 {
579 char b64ip[25];
580 if (!user || IsDummy(user))
581 return;
582 irc_p10_ntop(b64ip, &user->ip);
583 if (user->modes) {
584 char modes[32];
585
586 irc_user_modes(user, modes, sizeof(modes));
587 /* we don't need to put the + in modes because it's in the format string. */
588 putsock("%s " P10_NICK " %s %d " FMT_TIME_T " %s %s +%s %s %s :%s",
589 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
590 } else {
591 putsock("%s " P10_NICK " %s %d " FMT_TIME_T " %s %s %s %s :%s",
592 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
593 }
594 }
595
596 void
597 irc_rename(struct userNode *user, const char *new_handle)
598 {
599 if(extended_accounts)
600 putsock("%s " P10_ACCOUNT " %s M %s", self->numeric, user->numeric, new_handle);
601 }
602
603 void
604 irc_delete(struct userNode *user)
605 {
606 if(extended_accounts)
607 putsock("%s " P10_ACCOUNT " %s U", self->numeric, user->numeric);
608 }
609
610 void
611 irc_account(struct userNode *user, const char *stamp, time_t timestamp)
612 {
613 if(extended_accounts)
614 putsock("%s " P10_ACCOUNT " %s R %s "FMT_TIME_T, self->numeric, user->numeric, stamp, timestamp);
615 else
616 putsock("%s " P10_ACCOUNT " %s %s "FMT_TIME_T, self->numeric, user->numeric, stamp, timestamp);
617 }
618
619 void
620 irc_fakehost(struct userNode *user, const char *host)
621 {
622 putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host);
623 }
624
625 void
626 irc_regnick(UNUSED_ARG(struct userNode *user))
627 {
628 /* no operation here */
629 }
630
631 void
632 irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
633 {
634 putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
635 }
636
637 void
638 irc_fetchtopic(struct userNode *from, const char *to)
639 {
640 if (!from || !to)
641 return;
642 putsock("%s " P10_TOPIC " %s", from->numeric, to);
643 }
644
645 void
646 irc_squit(struct server *srv, const char *message, const char *service_message)
647 {
648 if (!service_message)
649 service_message = message;
650
651 /* Are we leaving the network? */
652 if (srv == self && cManager.uplink->state == CONNECTED) {
653 unsigned int i;
654
655 /* Quit all clients linked to me. */
656 for (i = 0; i <= self->num_mask; i++) {
657 if (!self->users[i])
658 continue;
659 irc_quit(self->users[i], service_message);
660 }
661 }
662
663 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
664
665 if (srv == self) {
666 /* Force a reconnect to the currently selected server. */
667 cManager.uplink->tries = 0;
668 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
669 close_socket();
670 }
671 }
672
673 void
674 irc_wallchops(struct userNode *from, const char *to, const char *message)
675 {
676 putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
677 }
678
679 void
680 irc_wallops(const char *format, ...)
681 {
682 va_list arg_list;
683 char buffer[MAXLEN];
684 va_start(arg_list, format);
685 vsnprintf(buffer, MAXLEN-2, format, arg_list);
686 buffer[MAXLEN-1] = 0;
687 putsock("%s " P10_WALLOPS " :%s", self->numeric, buffer);
688 }
689
690 static int
691 deliver_to_dummy(struct userNode *source, struct userNode *dest, const char *message, int type)
692 {
693 unsigned int num;
694
695 if (!dest || !IsDummy(dest) || !IsLocal(dest))
696 return 0;
697 num = dest->num_local;
698 switch (type) {
699 default:
700 if ((num < num_notice_funcs) && notice_funcs[num])
701 notice_funcs[num](source, dest, message, 0);
702 break;
703 case 1:
704 if ((num < num_privmsg_funcs) && privmsg_funcs[num])
705 privmsg_funcs[num](source, dest, message, 0);
706 break;
707 }
708 return 1;
709 }
710
711 void
712 irc_notice(struct userNode *from, const char *to, const char *message)
713 {
714 if (to[0] == '#' || to[0] == '$'
715 || !deliver_to_dummy(from, GetUserN(to), message, 0))
716 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
717 }
718
719 void
720 irc_notice_user(struct userNode *from, struct userNode *to, const char *message)
721 {
722 if (!deliver_to_dummy(from, to, message, 0))
723 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message);
724 }
725
726 void
727 irc_privmsg(struct userNode *from, const char *to, const char *message)
728 {
729 if (to[0] == '#' || to[0] == '$'
730 || !deliver_to_dummy(from, GetUserN(to), message, 1))
731 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
732 }
733
734 void
735 irc_privmsg_user(struct userNode *from, struct userNode *to, const char *message)
736 {
737 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to->numeric, message);
738 }
739
740 void
741 irc_version_user(struct userNode *from, struct userNode *to)
742 {
743 irc_privmsg_user(from, to, "\001VERSION\001");
744 }
745
746 void
747 irc_eob(void)
748 {
749 putsock("%s " P10_EOB, self->numeric);
750 }
751
752 void
753 irc_eob_ack(void)
754 {
755 putsock("%s " P10_EOB_ACK, self->numeric);
756
757 char *nick;
758 const char *str;
759 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
760 nick = strdup(str);
761
762 if (nick && (DefConLevel < 5)) {
763 DefConProcess(GetUserH(nick));
764
765 if (DefConTimeOut > 0)
766 timeq_add(now + DefConTimeOut, defcon_timeout, NULL);
767 }
768 }
769
770 void
771 irc_rpong(const char *from1, const char *from2, const char *pingtime, const char *clientinfo)
772 {
773 putsock("%s " P10_RPONG " %s %s %s :%s", self->numeric, from1, from2, pingtime, clientinfo);
774 }
775
776 void
777 irc_ping(const char *payload)
778 {
779 putsock("%s " P10_PING " :%s", self->numeric, payload);
780 }
781
782 void
783 irc_pong(const char *who, const char *data)
784 {
785 putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
786 }
787
788 void
789 irc_pong_asll(const char *who, const char *orig_ts)
790 {
791 char *delim;
792 struct timeval orig;
793 struct timeval sys_now;
794 int diff;
795
796 orig.tv_sec = strtoul(orig_ts, &delim, 10);
797 orig.tv_usec = (*delim == '.') ? strtoul(delim + 1, NULL, 10) : 0;
798 gettimeofday(&sys_now, NULL);
799 diff = (sys_now.tv_sec - orig.tv_sec) * 1000 + (sys_now.tv_usec - orig.tv_usec) / 1000;
800 putsock("%s " P10_PONG " %s %s %d %lu.%06lu", self->numeric, who, orig_ts, diff, (unsigned long)sys_now.tv_sec, (unsigned long)sys_now.tv_usec);
801 }
802
803 void
804 irc_pass(const char *passwd)
805 {
806 putsock(P10_PASS " :%s", passwd);
807 }
808
809 void
810 irc_introduce(const char *passwd)
811 {
812 void timed_send_ping(void *data);
813
814 self->self_burst = self->burst = 1;
815 irc_pass(passwd);
816 irc_server(self);
817 burst_length = 0;
818 timeq_add(now + ping_freq, timed_send_ping, 0);
819 }
820
821 void
822 irc_gline(struct server *srv, struct gline *gline, int silent)
823 {
824 putsock("%s " P10_GLINE " %s +%s " FMT_TIME_T " " FMT_TIME_T " :%s<%s> %s",
825 self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, now, silent ? "AUTO " : "", gline->issuer, gline->reason);
826 }
827
828 void
829 irc_shun(struct server *srv, struct shun *shun)
830 {
831 putsock("%s " P10_SHUN " %s +%s " FMT_TIME_T " :<%s> %s",
832 self->numeric, (srv ? srv->numeric : "*"), shun->target, shun->expires-now, shun->issuer, shun->reason);
833 }
834
835 void
836 irc_settime(const char *srv_name_mask, time_t new_time)
837 {
838 ioset_set_time(new_time);
839 if (!strcmp(srv_name_mask, "*"))
840 srv_name_mask = "";
841 putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
842 }
843
844 void
845 irc_ungline(const char *mask)
846 {
847 putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
848 }
849
850 void
851 irc_unshun(const char *mask)
852 {
853 putsock("%s " P10_SHUN " * -%s", self->numeric, mask);
854 }
855
856 void
857 irc_burst(struct chanNode *chan)
858 {
859 char burst_line[512];
860 int pos, base_len, len;
861 struct modeNode *mn;
862 struct banNode *bn;
863 struct exemptNode *en;
864 long last_mode=-1;
865 unsigned int first_ban;
866 unsigned int first_exempt;
867 unsigned int n;
868
869 base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
870 self->numeric, chan->name, chan->timestamp);
871 len = irc_make_chanmode(chan, burst_line+base_len);
872 pos = base_len + len;
873 if (len > 0 && chan->members.used > 0)
874 burst_line[pos++] = ' ';
875
876 if(chan->members.used < 1)
877 return; /* dont burst empty channels (created by discrims) */
878 /* dump the users */
879 for (n=0; n<chan->members.used; n++) {
880 mn = chan->members.list[n];
881 if (pos > 500) {
882 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
883 putsock("%s", burst_line);
884 pos = base_len;
885 last_mode = -1;
886 }
887
888 memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
889 pos += strlen(mn->user->numeric);
890 if (mn->modes && (mn->modes != last_mode)) {
891 last_mode = mn->modes;
892 burst_line[pos++] = ':';
893 if (last_mode & MODE_CHANOP)
894 burst_line[pos++] = 'o';
895 if (last_mode & MODE_HALFOP)
896 burst_line[pos++] = 'h';
897 if (last_mode & MODE_VOICE)
898 burst_line[pos++] = 'v';
899 }
900 if ((n+1)<chan->members.used)
901 burst_line[pos++] = ',';
902 }
903 if (chan->banlist.used) {
904 /* dump the bans */
905 first_ban = 1;
906
907 for (n=0; n<chan->banlist.used; ) {
908 if (first_ban && (pos < 500)) {
909 burst_line[pos++] = ':';
910 burst_line[pos++] = '%';
911 }
912 bn = chan->banlist.list[n];
913 len = strlen(bn->ban);
914 if (pos + 2 + len < 505) {
915 memcpy(burst_line + pos, bn->ban, len);
916 pos += len;
917 burst_line[pos++] = ' ';
918 first_ban = 0;
919 n++;
920 } else {
921 burst_line[pos-1] = 0;
922 putsock("%s", burst_line);
923 pos = base_len;
924 first_ban = 1;
925 }
926 }
927 }
928 if (chan->exemptlist.used) {
929 /* dump the exempts */
930 first_exempt = 1;
931
932 for (n=0; n<chan->exemptlist.used; ) {
933 if (first_exempt && (pos < 500)) {
934 burst_line[pos++] = ' ';
935 burst_line[pos++] = '~';
936 burst_line[pos++] = ' ';
937 }
938 en = chan->exemptlist.list[n];
939 len = strlen(en->exempt);
940 if (pos + 2 + len < 505) {
941 memcpy(burst_line + pos, en->exempt, len);
942 pos += len;
943 burst_line[pos++] = ' ';
944 first_exempt = 0;
945 n++;
946 } else {
947 burst_line[pos-1] = 0;
948 putsock("%s", burst_line);
949 pos = base_len;
950 first_exempt = 1;
951 }
952 }
953 }
954 /* print the last line */
955 burst_line[pos] = 0;
956 putsock("%s", burst_line);
957 }
958
959 void
960 irc_quit(struct userNode *user, const char *message)
961 {
962 putsock("%s " P10_QUIT " :%s", user->numeric, message);
963 }
964
965 void
966 irc_error(const char *to, const char *message)
967 {
968 if (to) {
969 putsock("%s " P10_ERROR " :%s", to, message);
970 } else {
971 putsock(":%s " P10_ERROR " :%s", self->name, message);
972 }
973 }
974
975 void
976 irc_kill(struct userNode *from, struct userNode *target, const char *message)
977 {
978 if (from) {
979 putsock("%s " P10_KILL " %s :%s!%s (%s)",
980 from->numeric, target->numeric, self->name, from->nick, message);
981 } else {
982 putsock("%s " P10_KILL " %s :%s (%s)",
983 self->numeric, target->numeric, self->name, message);
984 }
985 }
986
987 void
988 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
989 {
990 call_channel_mode_funcs(from, target, (char **)&modes, 0);
991 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
992 (from ? from->numeric : self->numeric),
993 target->name, modes, target->timestamp);
994 }
995
996 /* Added to allow services to mode users
997 2005 - 8 - 10 by Life4Christ
998 */
999 void
1000 irc_umode(struct userNode *target, const char *modes)
1001 {
1002 putsock("%s " P10_MODE " %s %s ",self->numeric,target->nick, modes);
1003 }
1004
1005
1006 void
1007 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
1008 {
1009 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
1010 }
1011
1012 void
1013 irc_silence(struct userNode *who, const char *mask, int add)
1014 {
1015 putsock("%s " P10_SILENCE " %s %s%s", self->numeric, who->numeric, add ? "+" : "-", mask);
1016 }
1017
1018 void
1019 irc_join(struct userNode *who, struct chanNode *what)
1020 {
1021 if (what->members.used == 1) {
1022 putsock("%s " P10_CREATE " %s "FMT_TIME_T,
1023 who->numeric, what->name, what->timestamp);
1024 } else {
1025 putsock("%s " P10_JOIN " %s "FMT_TIME_T, who->numeric, what->name, what->timestamp);
1026 }
1027 }
1028
1029 void
1030 irc_svsjoin(struct userNode *from, struct userNode *who, struct chanNode *to)
1031 {
1032 putsock("%s " P10_SVSJOIN " %s %s "FMT_TIME_T, from->uplink->numeric, who->numeric, to->name, now);
1033 }
1034
1035 void
1036 irc_svspart(struct userNode *from, struct userNode *who, struct chanNode *to)
1037 {
1038 putsock("%s " P10_SVSPART " %s %s", from->uplink->numeric, who->numeric, to->name);
1039 }
1040
1041 void
1042 irc_svsquit(struct userNode *from, struct userNode *who, char const *reason)
1043 {
1044 putsock("%s " P10_SVSQUIT " %s :%s", from->uplink->numeric, who->numeric, reason);
1045 }
1046
1047 void
1048 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
1049 {
1050 const char *numeric;
1051 struct modeNode *mn = GetUserMode(channel, who);
1052 numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric;
1053 putsock("%s " P10_KICK " %s %s :%s",
1054 numeric, channel->name, target->numeric, msg);
1055 }
1056
1057 void
1058 irc_stats(struct userNode *from, struct server *target, char type)
1059 {
1060 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
1061 }
1062
1063 void
1064 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
1065 {
1066 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
1067 }
1068
1069 void
1070 irc_swhois(struct userNode *from, struct userNode *target, const char *message)
1071 {
1072 putsock("%s " P10_SWHOIS " %s %s%s", from->uplink->numeric, target->numeric, message ? ":" : "",
1073 message ? message : "");
1074
1075 }
1076
1077 void
1078 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
1079 {
1080 if (reason) {
1081 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
1082 } else {
1083 putsock("%s " P10_PART " %s", who->numeric, what->name);
1084 }
1085 }
1086
1087 void
1088 irc_topic(struct userNode *service, struct userNode *who, struct chanNode *what, const char *topic)
1089 {
1090
1091 int type = 4, host_in_topic = 0, hasident = 0;
1092 const char *hstr, *tstr;
1093 char *host, *hostmask;
1094 char shost[MAXLEN];
1095 char sident[MAXLEN];
1096
1097 tstr = conf_get_data("server/type", RECDB_QSTRING);
1098 hstr = conf_get_data("server/host_in_topic", RECDB_QSTRING);
1099 if(tstr)
1100 type = atoi(tstr);
1101 else
1102 type = 4;/* default to 040 style topics */
1103
1104 if (hstr) {
1105 if (IsFakeHost(who))
1106 safestrncpy(shost, who->fakehost, sizeof(shost));
1107 else if (IsSetHost(who)) {
1108 hostmask = strdup(who->sethost);
1109 if ((host = (strrchr(hostmask, '@')))) {
1110 hasident = 1;
1111 *host++ = '\0';
1112 } else
1113 host = hostmask;
1114
1115 if (hasident)
1116 safestrncpy(sident, hostmask, sizeof(shost));
1117 else
1118 safestrncpy(sident, who->ident, sizeof(shost));
1119
1120 safestrncpy(shost, host, sizeof(shost));
1121 } else
1122 safestrncpy(shost, who->hostname, sizeof(shost));
1123
1124 host_in_topic = atoi(hstr);
1125 }
1126
1127 if (type >= 5) {
1128 putsock("%s " P10_TOPIC " %s %s%s%s%s%s " FMT_TIME_T " " FMT_TIME_T " :%s", service->numeric, what->name,
1129 who->nick, host_in_topic ? "!" : "", host_in_topic ? (IsSetHost(who) ? sident : who->ident) : "",
1130 host_in_topic ? "@" : "", host_in_topic ? shost : "", what->timestamp, now, topic);
1131 } else {
1132 who = service;
1133 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
1134 }
1135 }
1136
1137 void
1138 irc_raw(const char *what)
1139 {
1140 putsock("%s", what);
1141 }
1142
1143 void
1144 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
1145 {
1146 va_list arg_list;
1147 char buffer[MAXLEN];
1148 va_start(arg_list, format);
1149 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1150 buffer[MAXLEN-1] = 0;
1151 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
1152 }
1153
1154 void
1155 irc_mark(struct userNode *user, char *mark)
1156 {
1157 char *host = user->hostname;
1158
1159 /* TODO: Allow mark overwrite. If they are marked, and their fakehost is oldmark.hostname, update it to newmark.hostname so mark can be called multiple times. Probably requires ircd modification also */
1160 if(user->mark)
1161 return;
1162
1163 /* if the mark will put us over the host length, clip some off the left hand side
1164 * to make room...
1165 */
1166 if(strlen(host) + 1 + strlen(mark) > HOSTLEN)
1167 host += 1 + ( (strlen(host) + 1 + strlen(mark)) - HOSTLEN );
1168 putsock("%s " CMD_MARK " %s DNSBL +m %s.%s", self->numeric, user->nick, mark, host);
1169 putsock("%s " CMD_MARK " %s DNSBL_DATA %s", self->numeric, user->nick, mark);
1170
1171 /* Save it in the user */
1172 user->mark = strdup(mark);
1173
1174 /* If they are not otherwise marked, mark their host with fakehost */
1175 if(!IsFakeHost(user) && !IsSetHost(user) && !(IsHiddenHost(user) && user->handle_info) )
1176 {
1177 struct modeNode *mn = NULL;
1178 char fakehost[HOSTLEN];
1179 unsigned int count = 0;
1180 unsigned int n = 0;
1181
1182 putsock("%s " CMD_FAKEHOST " %s %s.%s", self->numeric, user->numeric, mark, host);
1183 putsock("%s " CMD_MODE " %s +x", self->numeric, user->nick);
1184
1185 snprintf(fakehost, sizeof(fakehost), "%s.%s", mark, host);
1186 safestrncpy(user->fakehost, fakehost, sizeof(user->fakehost));
1187
1188 for (n=count=0; n<user->channels.used; n++) {
1189 mn = user->channels.list[n];
1190 if (strlen(mn->channel->name) >= 1) /* Sanity */
1191 check_bans(user, mn->channel->name);
1192 }
1193 }
1194 }
1195
1196 void irc_sno(unsigned int mask, char const* format, ...) {
1197 va_list arg_list;
1198 char buffer[MAXLEN];
1199 va_start(arg_list, format);
1200 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1201 buffer[MAXLEN-1] = 0;
1202 putsock("%s " CMD_SNO " %d :%s", self->numeric, mask, buffer);
1203 }
1204
1205 static void send_burst(void);
1206
1207 static void
1208 change_nicklen(int new_nicklen)
1209 {
1210 unsigned int nn;
1211 char new_nick[NICKLEN+1];
1212 struct userNode *user;
1213
1214 nicklen = new_nicklen;
1215 /* fix up any users we have here */
1216 for (nn=0; nn<=self->num_mask; nn++) {
1217 if (!(user = self->users[nn]))
1218 continue;
1219 safestrncpy(new_nick, user->nick, sizeof(new_nick));
1220 new_nick[nicklen] = 0;
1221 NickChange(user, new_nick, 1);
1222 }
1223 }
1224
1225 static CMD_FUNC(cmd_whois)
1226 {
1227 struct userNode *from;
1228 struct userNode *who;
1229 char buf[MAXLEN];
1230 unsigned int i, mlen, len;
1231
1232 if (argc < 3)
1233 return 0;
1234 if (!(from = GetUserH(origin))) {
1235 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
1236 return 0;
1237 }
1238 if (!(who = GetUserH(argv[2]))) {
1239 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
1240 return 1;
1241 }
1242
1243 if (IsFakeHost(who) && IsHiddenHost(who))
1244 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->fakehost, who->info);
1245 else if (IsHiddenHost(who) && who->handle_info && hidden_host_suffix)
1246 irc_numeric(from, RPL_WHOISUSER, "%s %s %s.%s * :%s", who->nick, who->ident, who->handle_info->handle, hidden_host_suffix, who->info);
1247 else
1248 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
1249
1250 if (IsService(who) || (from == who)) {
1251 struct modeNode *mn;
1252 mlen = strlen(self->name) + strlen(from->nick) + 12 + strlen(who->nick);
1253 len = 0;
1254 *buf = '\0';
1255 for (i = who->channels.used; i > 0; )
1256 {
1257 mn = who->channels.list[--i];
1258
1259 if (!IsOper(from) && (mn->channel->modes & (MODE_PRIVATE | MODE_SECRET)) && !GetUserMode(mn->channel, from))
1260 continue;
1261
1262 if (len + strlen(mn->channel->name) + mlen > MAXLEN - 5)
1263 {
1264 irc_numeric(from, RPL_WHOISCHANNELS, "%s :%s", who->nick, buf);
1265 *buf = '\0';
1266 len = 0;
1267 }
1268
1269 if (IsDeaf(who))
1270 *(buf + len++) = '-';
1271 if ((mn->channel->modes & (MODE_PRIVATE | MODE_SECRET)) && !GetUserMode(mn->channel, from))
1272 *(buf + len++) = '*';
1273 if (mn->modes & MODE_CHANOP)
1274 *(buf + len++) = '@';
1275 else if (mn->modes & MODE_HALFOP)
1276 *(buf + len++) = '%';
1277 else if (mn->modes & MODE_VOICE)
1278 *(buf + len++) = '+';
1279
1280 if (len)
1281 *(buf + len) = '\0';
1282 strcpy(buf + len, mn->channel->name);
1283 len += strlen(mn->channel->name);
1284 strcat(buf + len, " ");
1285 len++;
1286 }
1287 if (buf[0] != '\0')
1288 irc_numeric(from, RPL_WHOISCHANNELS, "%s :%s", who->nick, buf);
1289 }
1290
1291 if (his_servername && his_servercomment && !IsOper(from) && from != who)
1292 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
1293 else
1294 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
1295
1296 if (IsAway(who))
1297 irc_numeric(from, RPL_AWAY, "%s :Away", who->nick);
1298 if (IsOper(who))
1299 irc_numeric(from, RPL_WHOISOPERATOR, "%s :%s", who->nick, IsLocal(who) ? "is a megalomaniacal power hungry tyrant" : "is an IRC Operator");
1300 if (who->handle_info)
1301 irc_numeric(from, RPL_WHOISACCOUNT, "%s %s :is logged in as", who->nick, who->handle_info->handle);
1302 if (IsHiddenHost(who) && who->handle_info && (IsOper(from) || from == who))
1303 irc_numeric(from, RPL_WHOISACTUALLY, "%s %s@%s %s :Actual user@host, Actual IP", who->nick, who->ident, who->hostname, irc_ntoa(&who->ip));
1304 if (IsLocal(who) && !IsService(who) && (IsOper(from) || from == who))
1305 irc_numeric(from, RPL_WHOISIDLE, "%s %ld %ld :seconds idle, signon time", who->nick, now - who->idle_since, who->timestamp);
1306
1307 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
1308 return 1;
1309 }
1310
1311 static CMD_FUNC(cmd_server)
1312 {
1313 struct server *srv;
1314 const char *str;
1315
1316 if (argc < 8)
1317 return 0;
1318 if (self->uplink) {
1319 /* another server introduced us */
1320 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1321 if (!srv)
1322 return 0;
1323 srv->self_burst = argv[5][0] == 'J';
1324 srv->burst = 1;
1325 } else {
1326 /* this must be our uplink */
1327 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1328 if (!srv)
1329 return 0;
1330 srv->self_burst = argv[5][0] == 'J';
1331 srv->burst = 1;
1332 if ((argv[7][0] == '+') && !force_n2k) {
1333 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
1334 }
1335 send_burst();
1336 }
1337
1338 /* Fix up our timestamps if necessary. */
1339 if (srv->boot <= PREHISTORY) {
1340 /* Server from the mists of time.. */
1341 if (srv->hops == 1) {
1342 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
1343 }
1344 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
1345 && enabled_string(str)) {
1346 /* If we have a reliable clock, we just keep our current time. */
1347 } else {
1348 if (srv->boot <= self->boot) {
1349 /* The other server is older than us. Accept their timestamp.
1350 * Alternately, we are same age, but we accept their time
1351 * since we are linking to them. */
1352 self->boot = srv->boot;
1353 ioset_set_time(srv->link_time
1354 );
1355 }
1356 }
1357 if (srv == self->uplink) {
1358 extern time_t burst_begin;
1359 burst_begin = now;
1360 }
1361 return 1;
1362 }
1363
1364
1365 static CMD_FUNC(cmd_eob)
1366 {
1367 struct server *sender;
1368 dict_iterator_t it;
1369 unsigned int ii;
1370
1371 if (!(sender = GetServerH(origin)))
1372 return 0;
1373 if (sender == self->uplink) {
1374 cManager.uplink->state = CONNECTED;
1375 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1376 irc_burst(iter_data(it));
1377 dict_delete(unbursted_channels);
1378 unbursted_channels = NULL;
1379 irc_eob();
1380 irc_eob_ack();
1381
1382 /* now that we know who our uplink is,
1383 * we can center the routing map and activate auto-routing.
1384 */
1385 //activate_routing(NULL, NULL, NULL);
1386 routing_init();
1387 }
1388 sender->self_burst = 0;
1389 recalc_bursts(sender);
1390 for (ii=0; ii<slf_used; ii++)
1391 slf_list[ii](sender);
1392 /* let auto-routing figure out if we were
1393 * wating on this server to link a child to it */
1394 /* DONT call this if uplink is _US_ */
1395 if(sender->uplink != self)
1396 routing_handle_connect(sender->name, sender->uplink->name);
1397 return 1;
1398 }
1399
1400 static CMD_FUNC(cmd_eob_ack)
1401 {
1402 extern time_t burst_begin;
1403
1404 if (GetServerH(origin) == self->uplink) {
1405 burst_length = now - burst_begin;
1406 self->self_burst = self->burst = 0;
1407 }
1408 cManager.uplink->state = CONNECTED;
1409 return 1;
1410 }
1411
1412 static CMD_FUNC(cmd_rping)
1413 {
1414 struct server *dest;
1415
1416 if (!(dest = GetServerN(argv[1])))
1417 return 1; /* if its a jupe or something, return 1 so its silently ignored */
1418
1419 if (dest == self)
1420 irc_rpong(argv[2], argv[3], argv[4], argv[5]);
1421
1422 return 1;
1423 }
1424
1425 static CMD_FUNC(cmd_ping)
1426 {
1427 struct server *srv;
1428 struct userNode *un;
1429
1430 if (argc > 3)
1431 irc_pong_asll(argv[2], argv[3]);
1432 else if ((srv = GetServerH(origin)))
1433 irc_pong(self->name, srv->numeric);
1434 else if ((un = GetUserH(origin)))
1435 irc_pong(self->name, un->numeric);
1436 else
1437 irc_pong(self->name, origin);
1438
1439 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1440 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1441 timeq_add(now + ping_freq, timed_send_ping, 0);
1442 received_ping();
1443 return 1;
1444 }
1445
1446 static CMD_FUNC(cmd_error_nick)
1447 {
1448 /* Go back to original IRC length .. and try to reconnect :/ */
1449 change_nicklen(9);
1450 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1451 return 1;
1452 }
1453
1454 struct create_desc {
1455 struct userNode *user;
1456 time_t when;
1457 };
1458
1459 static void
1460 join_helper(struct chanNode *chan, void *data)
1461 {
1462 struct create_desc *cd = data;
1463 AddChannelUser(cd->user, chan);
1464 }
1465
1466 static void
1467 create_helper(char *name, void *data)
1468 {
1469 struct create_desc *cd = data;
1470
1471 if (!strcmp(name, "0")) {
1472 while (cd->user->channels.used > 0)
1473 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1474 return;
1475 }
1476
1477 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
1478 }
1479
1480 static CMD_FUNC(cmd_create)
1481 {
1482 struct create_desc cd;
1483 struct userNode *user;
1484
1485 if ((argc < 3) || !(user = GetUserH(origin)))
1486 return 0;
1487 cd.user = user;
1488 cd.when = atoi(argv[2]);
1489 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1490 return 1;
1491 }
1492
1493 static CMD_FUNC(cmd_join)
1494 {
1495 struct create_desc cd;
1496
1497 if (!(cd.user = GetUserH(origin)))
1498 return 0;
1499 if (argc < 2)
1500 return 0;
1501 else if (argc < 3)
1502 cd.when = now;
1503 else
1504 cd.when = atoi(argv[2]);
1505 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1506 return 1;
1507 }
1508
1509 static CMD_FUNC(cmd_svsjoin)
1510 {
1511 struct create_desc cd;
1512
1513 if (!(cd.user = GetUserH(argv[1])))
1514 return 0;
1515 if (argc < 3)
1516 return 0;
1517 else if (argc < 4)
1518 cd.when = now;
1519 else
1520 cd.when = atoi(argv[3]);
1521 parse_foreach(argv[2], join_helper, create_helper, NULL, NULL, &cd);
1522 return 1;
1523 }
1524
1525 static CMD_FUNC(cmd_pong)
1526 {
1527 if (argc < 3)
1528 return 0;
1529 if (!strcmp(argv[2], self->name)) {
1530 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1531 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1532 timeq_add(now + ping_freq, timed_send_ping, 0);
1533 received_ping();
1534 }
1535 return 1;
1536 }
1537
1538 static CMD_FUNC(cmd_nick)
1539 {
1540 struct userNode *user;
1541 if ((user = GetUserH(origin))) {
1542 /* nick change (since the source is a user numeric) */
1543 if (argc < 2)
1544 return 0;
1545 NickChange(user, argv[1], 1);
1546 } else {
1547 struct server *serv;
1548 struct userNode *nuser;
1549 char modes[MAXLEN];
1550 /* new nick */
1551 if (argc < 9)
1552 return 0;
1553 serv = GetServerH(origin);
1554 if (argc > 9)
1555 unsplit_string(argv+6, argc-9, modes);
1556 else
1557 strcpy(modes, "+");
1558 nuser = AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1559 }
1560 return 1;
1561 }
1562
1563 static CMD_FUNC(cmd_account)
1564 {
1565 struct userNode *user;
1566 struct server *server;
1567 struct handle_info *hi;
1568
1569 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
1570 return 0; /* Origin must be server. */
1571
1572 /* This next line appears to tremple origin.. why? */
1573 user = GetUserN(argv[1]);
1574 if (!user)
1575 return 1; /* A QUIT probably passed the ACCOUNT. */
1576
1577 if(!extended_accounts) /* any need for this function without? */
1578 return 1;
1579
1580 if(!strcmp(argv[2],"C"))
1581 {
1582 if((hi = loc_auth(argv[4], argv[5], NULL)))
1583 {
1584 /* Return a AC A */
1585 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1586
1587 log_module(MAIN_LOG, LOG_DEBUG, "loc_auth: %s\n", user->nick);
1588 }
1589 else
1590 {
1591 /* Return a AC D */
1592 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1593 }
1594 return 1;
1595 }
1596 else if(!strcmp(argv[2],"H")) /* New enhanced (host) version of C */
1597 {
1598 if((hi = loc_auth(argv[5], argv[6], argv[4] )))
1599 {
1600 /* Return a AC A */
1601 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1602 }
1603 else
1604 {
1605 /* Return a AC D */
1606 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1607 }
1608 return 1;
1609 }
1610 else if(!strcmp(argv[2],"R"))
1611 call_account_func(user, argv[3]);
1612 else
1613 call_account_func(user, argv[2]); /* For backward compatability */
1614 return 1;
1615 }
1616
1617 static CMD_FUNC(cmd_fakehost)
1618 {
1619 struct userNode *user;
1620
1621 if ((argc < 3) || !origin || !GetServerH(origin))
1622 return 0;
1623 if (!(user = GetUserN(argv[1])))
1624 return 1;
1625 assign_fakehost(user, argv[2], 0);
1626 return 1;
1627 }
1628
1629 static struct {
1630 char *name;
1631 unsigned int priv;
1632 } privtab[] = {
1633 #define P(priv) { #priv, PRIV_ ## priv }
1634 P(CHAN_LIMIT), P(MODE_LCHAN), P(WALK_LCHAN), P(DEOP_LCHAN),
1635 P(SHOW_INVIS), P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
1636 P(LOCAL_KILL), P(REHASH), P(RESTART), P(DIE),
1637 P(GLINE), P(LOCAL_GLINE), P(JUPE), P(LOCAL_JUPE),
1638 P(OPMODE), P(LOCAL_OPMODE), P(SET), P(WHOX),
1639 P(BADCHAN), P(LOCAL_BADCHAN), P(SEE_CHAN), P(PROPAGATE),
1640 P(DISPLAY), P(SEE_OPERS), P(WIDE_GLINE), P(FORCE_OPMODE),
1641 P(FORCE_LOCAL_OPMODE), P(REMOTEREHASH), P(CHECK), P(SEE_SECRET_CHAN),
1642 P(SHUN), P(LOCAL_SHUN), P(WIDE_SHUN), P(ZLINE),
1643 P(LOCAL_ZLINE), P(WIDE_ZLINE), P(LIST_CHAN), P(WHOIS_NOTICE),
1644 P(HIDE_IDLE), P(XTRAOP), P(HIDE_CHANNELS), P(DISPLAY_MODE),
1645 P(FREEFORM), P(REMOVE), P(SPAMFILTER),
1646 #undef P
1647 { 0, 0 }
1648 };
1649
1650 char *client_report_privs(struct userNode *client)
1651 {
1652 int i;
1653
1654 privbuf[0] = '\0';
1655 for (i = 0; privtab[i].name; i++) {
1656 if (HasPriv(client, privtab[i].priv)) {
1657 strcat(privbuf, privtab[i].name);
1658 strcat(privbuf, " ");
1659 }
1660 }
1661
1662 privbuf[strlen(privbuf)] = 0;
1663
1664 return privbuf;
1665 }
1666
1667 int clear_privs(struct userNode *who) {
1668 int i = 0;
1669 assert(0 != who);
1670
1671 for (i = 0; privtab[i].name; i++)
1672 RevokePriv(who, privtab[i].priv);
1673
1674 return 0;
1675 }
1676
1677 int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1678 int i = 0;
1679 assert(0 != priv);
1680 assert(0 != who);
1681
1682 for (i = 0; privtab[i].name; i++) {
1683 if (0 == strcmp(privtab[i].name, priv)) {
1684 if (what == PRIV_ADD)
1685 GrantPriv(who, privtab[i].priv);
1686 else if (what == PRIV_DEL) {
1687 RevokePriv(who, privtab[i].priv);
1688 }
1689 }
1690 }
1691 return 0;
1692 }
1693
1694 int check_priv(char *priv)
1695 {
1696 int i;
1697
1698 for (i = 0; privtab[i].name; i++) {
1699 if (0 == strcmp(privtab[i].name, priv)) {
1700 return 1;
1701 }
1702 }
1703 return 0;
1704 }
1705
1706 void
1707 irc_privs(struct userNode *target, char *flag, int add)
1708 {
1709 client_modify_priv_by_name(target, flag, add);
1710 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1711 }
1712
1713 static CMD_FUNC(cmd_privs)
1714 {
1715 char *tstr = NULL;
1716 char buf[512] = "";
1717 char *p = 0;
1718 char *tmp;
1719
1720 int what = PRIV_ADD;
1721 int type = 0;
1722
1723 unsigned int i;
1724
1725 struct server *sender;
1726 struct userNode *user;
1727
1728
1729 tstr = conf_get_data("server/type", RECDB_QSTRING);
1730 if(tstr)
1731 type = atoi(tstr);
1732
1733
1734 if (!(sender = GetServerH(origin))) { /* from oper */
1735 return 1; /* ignore as no services have privs set */
1736 } else { /* from server */
1737 if (type < 5)
1738 return 1; /* silently ignore */
1739
1740 user = argc > 1 ? GetUserN(argv[1]) : NULL;
1741
1742 if (!user)
1743 return 0;
1744
1745 for (i=1; i<argc; i++) {
1746 strcat(buf, argv[i]);
1747 strcat(buf, " ");
1748 }
1749
1750 for (i = 2; i < argc; i++) {
1751 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1752 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1753 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1754 tmp = x3_strtok(&p, NULL, ",")) {
1755 if (!strcmp(tmp, "PRIV_NONE")) {
1756 clear_privs(user);
1757 break;
1758 } else
1759 client_modify_priv_by_name(user, tmp, what);
1760 }
1761 }
1762 }
1763 return 1;
1764 }
1765
1766 static CMD_FUNC(cmd_burst)
1767 {
1768 extern int rel_age;
1769 char modes[MAXLEN], *members = "";
1770 static char exemptlist[MAXLEN], banlist[MAXLEN];
1771 unsigned int next = 3, res = 1;
1772 int ctype = 0, echeck = 0, bcheck = 0;
1773 struct chanData *cData;
1774 struct chanNode *cNode;
1775 struct userNode *un;
1776 struct modeNode *mNode;
1777 long mode;
1778 int oplevel = -1;
1779 char *user, *end, sep;
1780 time_t in_timestamp;
1781 char* parm = NULL;
1782
1783 if (argc < 3)
1784 return 0;
1785 modes[0] = 0;
1786
1787 exemptlist[0] = 0;
1788 banlist[0] = 0;
1789
1790 while (next < argc) {
1791 switch (argv[next][0]) {
1792 case '+': {
1793 const char *pos;
1794 int n_modes;
1795 for (pos=argv[next], n_modes = 1; *pos; pos++)
1796 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1797 || (*pos == 'U'))
1798 n_modes++;
1799 if (next + n_modes > argc)
1800 n_modes = argc - next;
1801 unsplit_string(argv+next, n_modes, modes);
1802 next += n_modes;
1803 break;
1804 }
1805 case '%': {
1806 for(parm = mysep(&argv[next], " "); /* parm = first param */
1807 parm; /* While param is not null */
1808 parm = mysep(&argv[next], " ") /* parm = next param */
1809 )
1810 {
1811 switch (parm[0]) {
1812 case '%': {
1813 ctype = 1;
1814 break;
1815 }
1816 case '~': {
1817 ctype = 2;
1818 break;
1819 }
1820 default: {
1821 break;
1822 }
1823 }
1824 if (ctype == 1) {
1825 if (bcheck == 0) {
1826 /* strip % char off start of very first ban */
1827 if (strlen(parm) > 1) {
1828 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1829 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1830 }
1831 bcheck = 1;
1832 } else {
1833 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1834 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1835 }
1836 } else if (ctype == 2) {
1837 if (echeck == 0) {
1838 echeck = 1;
1839 } else {
1840 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1841 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1842 }
1843 }
1844 }
1845 next++;
1846 break;
1847 }
1848 default: members = argv[next++]; break;
1849 }
1850 }
1851
1852 in_timestamp = atoi(argv[2]);
1853 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1854 cNode->timestamp = in_timestamp;
1855 dict_remove(unbursted_channels, cNode->name);
1856 irc_burst(cNode);
1857 }
1858 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1859 cData = cNode->channel_info;
1860
1861 if (!cData) {
1862 if (cNode->modes & MODE_REGISTERED) {
1863 irc_join(opserv, cNode);
1864 irc_mode(opserv, cNode, "-z");
1865 irc_part(opserv, cNode, "");
1866 }
1867 }
1868
1869 /* Burst channel members in now. */
1870 for (user = members, sep = *members, mode = 0; sep; user = end) {
1871 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1872 sep = *end++; end[-1] = 0;
1873 if (sep == ':') {
1874 mode = 0;
1875 while ((sep = *end++)) {
1876 if (sep == 'o') {
1877 mode |= MODE_CHANOP;
1878 oplevel = -1;
1879 } else if (sep == 'h') {
1880 mode |= MODE_HALFOP;
1881 oplevel = -1;
1882 } else if (sep == 'v') {
1883 mode |= MODE_VOICE;
1884 oplevel = -1;
1885 } else if (isdigit(sep)) {
1886 mode |= MODE_CHANOP;
1887 if (oplevel >= 0)
1888 oplevel += parse_oplevel(end);
1889 else
1890 oplevel = parse_oplevel(end);
1891 while (isdigit(*end)) end++;
1892 } else
1893 break;
1894 }
1895 if (rel_age < 0)
1896 mode = 0;
1897 }
1898 if (!(un = GetUserN(user))) {
1899 res = 0;
1900 continue;
1901 }
1902 if ((mNode = AddChannelUser(un, cNode))) {
1903 mNode->modes = mode;
1904 mNode->oplevel = oplevel;
1905 }
1906 }
1907
1908 return res;
1909 }
1910
1911 /* TODO:
1912 * This is a stub that doesn't actually do anything. It should be completed
1913 * so that bans on *!*@markname.* match users as it does in nefarious
1914 */
1915 static CMD_FUNC(cmd_mark)
1916 {
1917 struct userNode *target;
1918 /*
1919 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
1920 */
1921
1922 if(argc < 4)
1923 return 0;
1924 if(!strcasecmp(argv[2], "DNSBL")) {
1925 /* DNSBL <modes> */
1926 return 1;
1927 }
1928 else if(!strcasecmp(argv[2], "DNSBL_DATA")) {
1929 /* DNSBL_DATA name */
1930 target = GetUserH(argv[1]);
1931 if(!target) {
1932 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
1933 return 0;
1934 }
1935 target->mark = strdup(argv[3]);
1936 return 1;
1937
1938 }
1939 else if(!strcasecmp(argv[2], "CVERSION")) {
1940 /* DNSBL_DATA name */
1941 target = GetUserH(argv[1]);
1942 if(!target) {
1943 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
1944 return 0;
1945 }
1946
1947 char *version = unsplit_string(argv + 3, argc - 3, NULL);
1948 if(!version)
1949 version = "";
1950
1951 target->version_reply = strdup(version);
1952
1953 if(match_ircglob(version, "WebTV;*"))
1954 target->no_notice = true; /* webbies cant see notices */
1955
1956 return 1;
1957 }
1958 /* unknown type of mark */
1959 return 1;
1960 }
1961
1962 static CMD_FUNC(cmd_mode)
1963 {
1964 struct chanNode *cn;
1965 struct userNode *un;
1966 char *sethost; // sethost - reed/apples
1967 int i; // sethost - reed/apples
1968
1969 if (argc < 3)
1970 return 0;
1971 if (!IsChannelName(argv[1])) {
1972 un = GetUserH(argv[1]);
1973 if (!un) {
1974 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1975 return 0;
1976 }
1977 // sethost - reed/apples
1978 if (argc == 3)
1979 mod_usermode(un, argv[2]);
1980 else {
1981 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
1982 i = 0;
1983 while((sethost[i++] = *argv[2]++));
1984 i--;
1985 sethost[i++] = ' ';
1986 while((sethost[i++] = *argv[3]++));
1987 mod_usermode(un, sethost); // sethost - reed/apples
1988 }
1989
1990 return 1;
1991 }
1992
1993 if (!(cn = GetChannel(argv[1]))) {
1994 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1995 return 0;
1996 }
1997 if ((un = GetUserH(origin))) {
1998 struct modeNode *mn;
1999 /* Update idle time for person setting the mode */
2000 if ((mn = GetUserMode(cn, un)))
2001 mn->idle_since = now;
2002 } else {
2003 /* If it came from a server, reset timestamp to re-sync. */
2004 cn->timestamp = atoi(argv[argc-1]);
2005 }
2006
2007 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
2008 const char *str;
2009 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2010 if (str) {
2011 char modes[MODELEN];
2012 struct userNode *opserv = GetUserH(str);
2013
2014 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
2015 irc_make_chanmode(cn, modes);
2016 irc_mode(opserv, cn, modes);
2017 irc_mode(opserv, cn, DefConChanModes);
2018 }
2019 return 1;
2020 }
2021
2022
2023 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
2024 }
2025
2026 static CMD_FUNC(cmd_opmode)
2027 {
2028 struct chanNode *cn;
2029 struct userNode *un;
2030
2031 if (argc < 3)
2032 return 0;
2033
2034 if (!(cn = GetChannel(argv[1]))) {
2035 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2036 return 0;
2037 }
2038 if (!(un = GetUserH(origin))) {
2039 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
2040 return 0;
2041 }
2042 if (!IsOper(un)) {
2043 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
2044 return 0;
2045 }
2046
2047 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
2048 }
2049
2050 static int clear_chanmode(struct chanNode *channel, const char *modes);
2051
2052 static CMD_FUNC(cmd_clearmode)
2053 {
2054 struct chanNode *cn;
2055 struct userNode *un;
2056
2057 if (argc < 3)
2058 return 0;
2059
2060 if (!(cn = GetChannel(argv[1]))) {
2061 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2062 return 0;
2063 }
2064 if (!(un = GetUserH(origin))) {
2065 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
2066 return 0;
2067 }
2068 if (!IsOper(un)) {
2069 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
2070 return 0;
2071 }
2072
2073 return clear_chanmode(cn, argv[2]);
2074 }
2075
2076 static CMD_FUNC(cmd_topic)
2077 {
2078 struct chanNode *cn;
2079 time_t chan_ts, topic_ts;
2080 struct userNode *user;
2081
2082 if (argc < 3)
2083 return 0;
2084 if (!(cn = GetChannel(argv[1]))) {
2085 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
2086 return 0;
2087 }
2088
2089
2090 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
2091 user = GetUserH(origin);
2092 chan_ts = atoi(argv[2]);
2093 topic_ts = atoi(argv[3]);
2094 } else if (argc >= 6) { /* Nefarious 0.5.0 */
2095 user = GetUserH(strtok(argv[2], "!"));
2096 chan_ts = atoi(argv[3]);
2097 topic_ts = atoi(argv[4]);
2098 } else { /* Regular IRCu (No Topic Bursting)*/
2099 user = GetUserH(origin);
2100 chan_ts = cn->timestamp;
2101 topic_ts = now;
2102 }
2103
2104 SetChannelTopic(cn, user, user, argv[argc-1], 0);
2105 cn->topic_time = topic_ts;
2106 return 1;
2107 }
2108
2109 static CMD_FUNC(cmd_num_topic)
2110 {
2111 struct chanNode *cn;
2112
2113 if (!argv[0])
2114 return 0; /* huh? */
2115 if (argv[2]) {
2116 cn = GetChannel(argv[2]);
2117 if (!cn) {
2118 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
2119 return 0;
2120 }
2121 } else
2122 return 0;
2123
2124 switch (atoi(argv[0])) {
2125 case 331:
2126 cn->topic_time = 0;
2127 break; /* no topic */
2128 case 332:
2129 if (argc < 4)
2130 return 0;
2131 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2132 break;
2133 case 333:
2134 if (argc < 5)
2135 return 0;
2136 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2137 cn->topic_time = atoi(argv[4]);
2138 break;
2139 default:
2140 return 0; /* should never happen */
2141 }
2142 return 1;
2143 }
2144
2145 static CMD_FUNC(cmd_num_gline)
2146 {
2147 if (argc < 7) {
2148 if (argc < 6)
2149 return 0;
2150 else
2151 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
2152 } else {
2153 if (!strcmp(argv[5], "+"))
2154 gline_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0, 0);
2155 }
2156 return 1;
2157 }
2158
2159 static CMD_FUNC(cmd_num_shun)
2160 {
2161 if (argc < 7) {
2162 if (argc < 6)
2163 return 0;
2164 else
2165 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
2166 } else {
2167 if (!strcmp(argv[5], "+"))
2168 shun_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0);
2169 }
2170 return 1;
2171 }
2172
2173 static CMD_FUNC(cmd_quit)
2174 {
2175 struct userNode *user;
2176 if (argc < 2)
2177 return 0;
2178 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2179 * call DelUser unless we have the user in our grasp. */
2180 if ((user = GetUserH(origin)))
2181 DelUser(user, NULL, false, argv[1]);
2182 return 1;
2183 }
2184
2185 static CMD_FUNC(cmd_kill)
2186 {
2187 struct userNode *user;
2188 if (argc < 2)
2189 return 0;
2190 user = GetUserN(argv[1]);
2191 if (!user) {
2192 /* If we get a KILL for a non-existent user, it could be a
2193 * Ghost response to a KILL we sent out earlier. So we only
2194 * whine if the target is local.
2195 */
2196 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
2197 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
2198 return 0;
2199 }
2200
2201 if (IsLocal(user) && IsService(user)) {
2202 /* TODO: rate limit this so silly things don't happen. */
2203 ReintroduceUser(user);
2204 return 1;
2205 }
2206
2207 DelUser(user, NULL, false, argv[2]);
2208 return 1;
2209 }
2210
2211 static CMD_FUNC(cmd_svspart)
2212 {
2213 struct userNode *user;
2214
2215 if (argc < 3)
2216 return 0;
2217 user = GetUserN(argv[1]);
2218 if (!user)
2219 return 0;
2220 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2221 return 1;
2222 }
2223
2224 static CMD_FUNC(cmd_silence)
2225 {
2226 struct userNode *user;
2227 char *mask;
2228 char *new_mask;
2229 unsigned int i;
2230
2231 if (argc < 2)
2232 return 0;
2233
2234 user = GetUserN(argv[1]);
2235
2236 /* Sanity, go nuts if this happens */
2237 if (!user)
2238 return 0;
2239
2240 /* We can safely ignore this if a user adding a silence is not
2241 * ignored. However this brings up a TODO. If a user logs in and
2242 * they have silences on the IRCd then we need to set them here
2243 * somehow
2244 */
2245 if (!user->handle_info)
2246 return 1;
2247
2248 mask = argv[2];
2249
2250 if (*mask == '-') {
2251 for (i=0; i<user->handle_info->ignores->used; i++) {
2252 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2253 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2254 }
2255 } else {
2256 for (i=0; i<user->handle_info->ignores->used; i++) {
2257 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2258 return 1; /* Already on the users NickServ ignore list, safely ignore */
2259 }
2260
2261 new_mask = strdup(mask+1);
2262 string_list_append(user->handle_info->ignores, new_mask);
2263 }
2264 return 1;
2265 }
2266
2267 static CMD_FUNC(cmd_kick)
2268 {
2269 if (argc < 3)
2270 return 0;
2271 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2272 return 1;
2273 }
2274
2275 static CMD_FUNC(cmd_squit)
2276 {
2277 struct server *server;
2278 char *uplink;
2279
2280 if (argc < 4)
2281 return 0;
2282 if (!(server = GetServerH(argv[1])))
2283 return 0;
2284
2285 if (server == self->uplink) {
2286 /* Force a reconnect to the currently selected server. */
2287 cManager.uplink->tries = 0;
2288 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2289 close_socket();
2290 return 1;
2291 }
2292
2293 uplink = strdup(server->uplink->name);
2294 DelServer(server, 0, argv[3]);
2295 /* if its a pingout and pingout connecting is enabled
2296 or its a read error and readerror connecting is enabled
2297 or were doing a "N" and i need to connect that server somewhere */
2298 routing_handle_squit(argv[1], uplink, argv[3]);
2299 free(uplink);
2300 return 1;
2301 }
2302
2303 static CMD_FUNC(cmd_privmsg)
2304 {
2305 struct privmsg_desc pd;
2306 if (argc < 3)
2307 return 0;
2308 pd.user = GetUserH(origin);
2309 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2310 return 1;
2311
2312 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2313 const char *str;
2314 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2315 if (str)
2316 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2317 return 1;
2318 }
2319
2320 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2321 return 1; /* Silently Ignore */
2322
2323 pd.is_notice = 0;
2324 pd.text = argv[argc - 1];
2325 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2326
2327 return 1;
2328 }
2329
2330 static CMD_FUNC(cmd_notice)
2331 {
2332 struct privmsg_desc pd;
2333 struct server *srv;
2334 int nuser = 0;
2335
2336 if (argc != 3)
2337 return 0;
2338
2339 pd.user = GetUserH(origin);
2340 if(!pd.user)
2341 nuser = 1;
2342 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2343 }
2344 else {
2345 pd.is_notice = 1;
2346 pd.text = argv[2];
2347 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2348 }
2349
2350 srv = GetServerH(origin);
2351 if(srv) {
2352 char *sargv[MAXNUMPARAMS];
2353 int sargc;
2354
2355 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2356
2357 if(!strcasecmp(sargv[0], "Connect:")) {
2358 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2359 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2360 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2361 }
2362 }
2363 else if(!strcasecmp(sargv[0], "Link")) {
2364 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2365 *
2366 * :Link with laptop.afternet.org cancelled: Connection refused
2367 */
2368 if(!strcasecmp(sargv[3], "cancelled:")) {
2369 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2370 }
2371 }
2372 }
2373 return 1;
2374 }
2375
2376 static CMD_FUNC(cmd_away)
2377 {
2378 struct userNode *uNode;
2379
2380 uNode = GetUserH(origin);
2381 if (!uNode)
2382 return 1;
2383 if (argc < 2)
2384 uNode->modes &= ~FLAGS_AWAY;
2385 else
2386 uNode->modes |= FLAGS_AWAY;
2387 return 1;
2388 }
2389
2390 static CMD_FUNC(cmd_gline)
2391 {
2392 if (argc < 3)
2393 return 0;
2394 if (argv[2][0] == '+') {
2395 if (argc < 5)
2396 return 0;
2397 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
2398 return 1;
2399 } else if (argv[2][0] == '-') {
2400 gline_remove(argv[2]+1, 0);
2401 return 1;
2402 } else
2403 return 0;
2404 }
2405
2406 static CMD_FUNC(cmd_sgline)
2407 {
2408 struct server *sender;
2409
2410 if (argc < 4)
2411 return 0;
2412
2413 if (!(sender = GetServerH(origin)))
2414 return 0;
2415
2416 gline_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1, 0);
2417 return 1;
2418 }
2419
2420 static CMD_FUNC(cmd_sshun)
2421 {
2422 struct server *sender;
2423
2424 if (argc < 4)
2425 return 0;
2426
2427 if (!(sender = GetServerH(origin)))
2428 return 0;
2429
2430 shun_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1);
2431 return 1;
2432 }
2433
2434 static CMD_FUNC(cmd_shun)
2435 {
2436 if (argc < 3)
2437 return 0;
2438 if (argv[2][0] == '+') {
2439 if (argc < 5)
2440 return 0;
2441 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
2442 return 1;
2443 } else if (argv[2][0] == '-') {
2444 shun_remove(argv[2]+1, 0);
2445 return 1;
2446 } else
2447 return 0;
2448 }
2449
2450 static CMD_FUNC(cmd_svsnick)
2451 {
2452 struct userNode *target, *dest;
2453 if ((argc < 4)
2454 || !(target = GetUserN(argv[1]))
2455 || !IsLocal(target)
2456 || (dest = GetUserH(argv[2])))
2457 return 0;
2458 NickChange(target, argv[2], 0);
2459 return 1;
2460 }
2461
2462 void
2463 free_user(struct userNode *user)
2464 {
2465 free(user->nick);
2466 free(user);
2467 }
2468
2469 static void
2470 parse_cleanup(void)
2471 {
2472 unsigned int nn;
2473 free(of_list);
2474 free(privmsg_funcs);
2475 num_privmsg_funcs = 0;
2476 free(notice_funcs);
2477 num_notice_funcs = 0;
2478 free(mcf_list);
2479 dict_delete(irc_func_dict);
2480 for (nn=0; nn<dead_users.used; nn++)
2481 free_user(dead_users.list[nn]);
2482 userList_clean(&dead_users);
2483 }
2484
2485 static void
2486 p10_conf_reload(void) {
2487 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
2488 his_servername = conf_get_data("server/his_servername", RECDB_QSTRING);
2489 his_servercomment = conf_get_data("server/his_servercomment", RECDB_QSTRING);
2490 }
2491
2492 static void
2493 remove_unbursted_channel(struct chanNode *cNode) {
2494 if (unbursted_channels)
2495 dict_remove(unbursted_channels, cNode->name);
2496 }
2497
2498 void
2499 init_parse(void)
2500 {
2501 const char *str, *desc;
2502 int numnick, usermask, max_users;
2503 char numer[COMBO_NUMERIC_LEN+1];
2504
2505 /* read config items */
2506 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2507 extended_accounts = str ? enabled_string(str) : 1;
2508 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2509 ping_freq = str ? ParseInterval(str) : 120;
2510 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2511 ping_timeout = str ? ParseInterval(str) : 30;
2512 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2513 force_n2k = str ? enabled_string(str) : 1;
2514 str = conf_get_data("server/numeric", RECDB_QSTRING);
2515 if (!str) {
2516 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2517 exit(1);
2518 }
2519 numnick = atoi(str);
2520 str = conf_get_data("server/max_users", RECDB_QSTRING);
2521 max_users = str ? atoi(str) : 4096;
2522 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2523 usermask--;
2524 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2525 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2526 else
2527 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2528
2529 str = conf_get_data("server/hostname", RECDB_QSTRING);
2530 desc = conf_get_data("server/description", RECDB_QSTRING);
2531 if (!str || !desc) {
2532 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2533 exit(1);
2534 }
2535 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2536 conf_register_reload(p10_conf_reload);
2537
2538 irc_func_dict = dict_new();
2539 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2540 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2541 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2542 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2543 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2544 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2545 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2546 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2547 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2548 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
2549 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2550 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
2551 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2552 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2553 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2554 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2555 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2556 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2557 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2558 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2559 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2560 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2561 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2562 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2563 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2564 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2565 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2566 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2567 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2568 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2569 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2570 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2571 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2572 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2573 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2574 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2575 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2576 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2577 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2578 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
2579 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2580 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
2581 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2582 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2583 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2584 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2585 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2586 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2587 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2588 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2589 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2590 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
2591 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2592 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
2593 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2594 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
2595 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2596 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
2597 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2598 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
2599 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2600 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2601 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2602 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
2603 dict_insert(irc_func_dict, CMD_SGLINE, cmd_sgline);
2604 dict_insert(irc_func_dict, TOK_SGLINE, cmd_sgline);
2605 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2606 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
2607 dict_insert(irc_func_dict, CMD_SSHUN, cmd_sshun);
2608 dict_insert(irc_func_dict, TOK_SSHUN, cmd_sshun);
2609 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2610 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2611 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2612 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2613 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2614 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2615 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2616 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
2617 dict_insert(irc_func_dict, CMD_SMO, cmd_dummy);
2618 dict_insert(irc_func_dict, TOK_SMO, cmd_dummy);
2619 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2620 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
2621
2622 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2623 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
2624 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2625 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2626
2627 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2628 * Apparently to obliterate channels from any servers that think they
2629 * exist?
2630 */
2631 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2632 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2633 /* Ignore invites */
2634 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2635 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2636 /* DESYNCH is just informational, so ignore it */
2637 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2638 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2639 /* Ignore channel operator notices. */
2640 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2641 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2642 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2643 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
2644 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2645 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2646 /* Ignore opers being silly. */
2647 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2648 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
2649 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2650 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2651 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
2652 /* Ignore dnsbl exemptions */
2653 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
2654 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2655 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
2656 /* ignore ALIST for now */
2657 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2658 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
2659 /* ignore SPAMFILTER */
2660 dict_insert(irc_func_dict, TOK_SPAMFILTER, cmd_dummy);
2661 dict_insert(irc_func_dict, CMD_SPAMFILTER, cmd_dummy);
2662 /* Ignore remote luser */
2663 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
2664 /* We have reliable clock! Always! Wraaa! */
2665 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2666 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
2667
2668 /* ignore /trace and /motd commands targetted at us */
2669 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2670 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
2671 dict_insert(irc_func_dict, TOK_UPING, cmd_dummy);
2672
2673 /* handle topics */
2674 dict_insert(irc_func_dict, "331", cmd_num_topic);
2675 dict_insert(irc_func_dict, "332", cmd_num_topic);
2676 dict_insert(irc_func_dict, "333", cmd_num_topic);
2677 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2678 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2679 /* ban list resetting */
2680 /* "stats g" responses */
2681 dict_insert(irc_func_dict, "230", cmd_dummy); /* ignore stats headers */
2682 dict_insert(irc_func_dict, "247", cmd_num_gline);
2683 dict_insert(irc_func_dict, "542", cmd_num_shun);
2684 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2685 /* other numeric responses we might get */
2686 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2687 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2688 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
2689 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
2690 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2691 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2692 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2693 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2694 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2695
2696 num_privmsg_funcs = 16;
2697 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2698 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2699
2700 num_notice_funcs = 16;
2701 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2702 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2703
2704 userList_init(&dead_users);
2705 reg_del_channel_func(remove_unbursted_channel);
2706 reg_exit_func(parse_cleanup);
2707 // reg_notice_func(opserv, check_ctcp);
2708 }
2709
2710 int
2711 parse_line(char *line, int recursive)
2712 {
2713 char *argv[MAXNUMPARAMS], *origin;
2714 int argc, cmd, res=0;
2715 cmd_func_t *func;
2716
2717 argc = split_line(line, true, MAXNUMPARAMS, argv);
2718 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2719 if (argc > cmd) {
2720 if (cmd) {
2721 if (argv[0][0] == ':') {
2722 origin = argv[0]+1;
2723 } else if (!argv[0][1] || !argv[0][2]) {
2724 struct server *sNode = GetServerN(argv[0]);
2725 origin = sNode ? sNode->name : 0;
2726 } else {
2727 struct userNode *uNode = GetUserN(argv[0]);
2728 origin = uNode ? uNode->nick : 0;
2729 }
2730 } else
2731 origin = 0;
2732 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2733 res = func(origin, argc-cmd, argv+cmd);
2734 }
2735 if (!res) {
2736 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2737 } else if (!recursive) {
2738 unsigned int i;
2739 for (i=0; i<dead_users.used; i++)
2740 free_user(dead_users.list[i]);
2741 dead_users.used = 0;
2742 }
2743 return res;
2744 }
2745
2746 static void
2747 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2748 {
2749 char *j, old;
2750
2751 do {
2752 j = target_list;
2753 while (*j != 0 && *j != ',')
2754 j++;
2755 old = *j;
2756 *j = 0;
2757
2758 if (IsChannelName(target_list)
2759 || (target_list[0] == '0' && target_list[1] == '\0')) {
2760 struct chanNode *chan = GetChannel(target_list);
2761
2762 if (chan) {
2763 if (cf)
2764 cf(chan, data);
2765 } else {
2766 if (nc)
2767 nc(target_list, data);
2768 }
2769 } else {
2770 struct userNode *user;
2771 struct privmsg_desc *pd = data;
2772
2773 pd->is_qualified = 0;
2774 if (*target_list == '@') {
2775 user = NULL;
2776 } else if (strchr(target_list, '@')) {
2777 struct server *server;
2778
2779 pd->is_qualified = 1;
2780 user = GetUserH(strtok(target_list, "@"));
2781 server = GetServerH(strtok(NULL, "@"));
2782
2783 if (user && (user->uplink != server)) {
2784 /* Don't attempt to index into any arrays
2785 using a user's numeric on another server. */
2786 user = NULL;
2787 }
2788 } else {
2789 user = GetUserN(target_list);
2790 }
2791
2792 if (user) {
2793 if (uf)
2794 uf(user, data);
2795 } else {
2796 if (nu)
2797 nu(target_list, data);
2798 }
2799 }
2800 target_list = j+1;
2801 } while (old == ',');
2802 }
2803
2804 static int
2805 get_local_numeric(void)
2806 {
2807 static unsigned int next_numeric = 0;
2808 if (self->clients > self->num_mask)
2809 return -1;
2810 while (self->users[next_numeric])
2811 if (++next_numeric > self->num_mask)
2812 next_numeric = 0;
2813 return next_numeric;
2814 }
2815
2816 static void
2817 make_numeric(struct server *svr, int local_num, char *outbuf)
2818 {
2819 int slen, llen;
2820
2821 if (force_n2k || svr->numeric[1]) {
2822 slen = 2;
2823 llen = 3;
2824 } else {
2825 slen = 1;
2826 llen = (local_num < 64*64) ? 2 : 3;
2827 }
2828 strncpy(outbuf, svr->numeric, slen);
2829 inttobase64(outbuf+slen, local_num, llen);
2830 outbuf[slen+llen] = 0;
2831 }
2832
2833 struct server *
2834 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link_time, const char *numeric, const char *description)
2835 {
2836 struct server* sNode;
2837 int slen, mlen;
2838
2839 if ((sNode = GetServerN(numeric))) {
2840 /* This means we're trying to re-add an existant server.
2841 * To be safe, we should forget the previous incarnation.
2842 * (And all its linked servers.)
2843 *
2844 * It usually only happens in replays when the original
2845 * had a ping timeout and the replay didn't (because
2846 * replaying a ping timeout invariably gets things wrong).
2847 */
2848 DelServer(sNode, 0, NULL);
2849 }
2850
2851 switch (strlen(numeric)) {
2852 case 5: slen = 2; mlen = 3; break;
2853 case 4: slen = 1; mlen = 3; break;
2854 case 3: slen = 1; mlen = 2; break;
2855 default:
2856 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2857 return NULL;
2858 }
2859
2860 sNode = calloc(1, sizeof(*sNode));
2861 sNode->uplink = uplink;
2862 safestrncpy(sNode->name, name, sizeof(sNode->name));
2863 sNode->num_mask = base64toint(numeric+slen, mlen);
2864 sNode->hops = hops;
2865 sNode->boot = boot;
2866 sNode->link_time = link_time;
2867 strncpy(sNode->numeric, numeric, slen);
2868 safestrncpy(sNode->description, description, sizeof(sNode->description));
2869 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
2870 serverList_init(&sNode->children);
2871 if (sNode->uplink) {
2872 /* uplink may be NULL if we're just building ourself */
2873 serverList_append(&sNode->uplink->children, sNode);
2874 }
2875 servers_num[base64toint(numeric, slen)] = sNode;
2876 dict_insert(servers, sNode->name, sNode);
2877 return sNode;
2878 }
2879
2880 void DelServer(struct server* serv, int announce, const char *message)
2881 {
2882 unsigned int i;
2883
2884 /* If we receive an ERROR command before the SERVER
2885 * command a NULL server can be passed */
2886 if (!serv)
2887 return;
2888
2889 /* Hrm, what's the right way to SQUIT some other server?
2890 * (This code is only to handle killing juped servers.) */
2891 if (announce && (serv->uplink == self) && (serv != self->uplink))
2892 irc_squit(serv, message, NULL);
2893
2894 /* must recursively remove servers linked to this one first */
2895 for (i=serv->children.used;i>0;)
2896 if (serv->children.list[--i] != self)
2897 DelServer(serv->children.list[i], false, NULL);
2898
2899 /* clean up server's user hash tables */
2900 for (i=0;i<=serv->num_mask;i++)
2901 if (serv->users[i])
2902 DelUser(serv->users[i], NULL, false, "server delinked");
2903
2904 /* delete server */
2905 if (serv->uplink)
2906 serverList_remove(&serv->uplink->children, serv);
2907 if (serv == self->uplink)
2908 self->uplink = NULL;
2909 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
2910 dict_remove(servers, serv->name);
2911 serverList_clean(&serv->children);
2912 free(serv->users);
2913 free(serv);
2914 }
2915
2916 struct userNode *
2917 AddLocalUser(const char *nick, const char *ident, const char *hostname, const char *desc, const char *modes)
2918 {
2919 char numeric[COMBO_NUMERIC_LEN+1];
2920 int local_num = get_local_numeric();
2921 time_t timestamp = now;
2922 struct userNode *old_user = GetUserH(nick);
2923
2924 if (!modes)
2925 modes = "+oik";
2926
2927 if (old_user) {
2928 if (IsLocal(old_user))
2929 return old_user;
2930 timestamp = old_user->timestamp - 1;
2931 }
2932 if (local_num == -1) {
2933 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
2934 return 0;
2935 }
2936 if (!hostname)
2937 hostname = self->name;
2938 make_numeric(self, local_num, numeric);
2939 return AddUser(self, nick, ident, hostname, modes, numeric, desc, now, "AAAAAA");
2940 }
2941
2942 struct userNode *
2943 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
2944 {
2945 char numeric[COMBO_NUMERIC_LEN+1];
2946 int local_num = get_local_numeric();
2947 time_t timestamp = now;
2948 struct userNode *old_user = GetUserH(nick);
2949
2950 if (old_user) {
2951 if (IsLocal(old_user))
2952 return old_user;
2953 timestamp = old_user->timestamp - 1;
2954 }
2955 if (local_num == -1) {
2956 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
2957 return 0;
2958 }
2959 make_numeric(self, local_num, numeric);
2960 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2961 }
2962
2963 int
2964 is_valid_nick(const char *nick) {
2965 unsigned int ii;
2966 /* IRC has some of The Most Fucked-Up ideas about character sets
2967 * in the world.. */
2968 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2969 return 0;
2970 for (ii = 0; nick[ii]; ++ii)
2971 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
2972 return 0;
2973 if (strlen(nick) > nicklen)
2974 return 0;
2975 return 1;
2976 }
2977
2978 static struct userNode*
2979 AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip)
2980 {
2981 struct userNode *oldUser, *uNode;
2982 unsigned int n, ignore_user, dummy;
2983 char *tstr;
2984 int type;
2985
2986 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
2987 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", (void*)uplink, nick, numeric);
2988 return NULL;
2989 }
2990
2991 if (!uplink) {
2992 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", (void*)uplink, nick, numeric);
2993 return NULL;
2994 }
2995
2996 if (uplink != GetServerN(numeric)) {
2997 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", (void*)uplink, nick, numeric, uplink->name);
2998 return NULL;
2999 }
3000
3001 dummy = modes && modes[0] == '*';
3002 if (dummy) {
3003 ++modes;
3004 } else if (!is_valid_nick(nick)) {
3005 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", (void*)uplink, nick);
3006 return NULL;
3007 }
3008
3009 ignore_user = 0;
3010 if ((oldUser = GetUserH(nick))) {
3011 if (IsLocal(oldUser)
3012 && (IsService(oldUser) || IsPersistent(oldUser))) {
3013 /* The service should collide the new user off - but not
3014 * if the new user is coming in during a burst. (During a
3015 * burst, the bursting server will kill either our user --
3016 * triggering a ReintroduceUser() -- or its own.)
3017 */
3018 oldUser->timestamp = timestamp - 1;
3019 ignore_user = 1;
3020 if (!uplink->burst)
3021 irc_user(oldUser);
3022 } else if (oldUser->timestamp > timestamp) {
3023 /* "Old" user is really newer; remove them */
3024 DelUser(oldUser, 0, 1, "Overruled by older nick");
3025 } else {
3026 /* User being added is too new; do not add them to
3027 * clients, but do add them to the server's list, since it
3028 * will send a KILL and QUIT soon. */
3029 ignore_user = 1;
3030 }
3031 }
3032
3033 /* create new usernode and set all values */
3034 uNode = calloc(1, sizeof(*uNode));
3035 uNode->nick = strdup(nick);
3036 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
3037 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
3038 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
3039 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
3040 irc_p10_pton(&uNode->ip, realip);
3041
3042 tstr = conf_get_data("server/type", RECDB_QSTRING);
3043 type = atoi(tstr);
3044 if (type > 6) {
3045 if (irc_in_addr_is_ipv4(uNode->ip)) {
3046 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
3047 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3048 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
3049 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3050 }
3051 }
3052
3053 if (!uNode->crypthost && uNode->cryptip)
3054 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
3055 uNode->idle_since = timestamp;
3056 uNode->timestamp = timestamp;
3057 modeList_init(&uNode->channels);
3058 uNode->uplink = uplink;
3059 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
3060 uNode->uplink->max_clients = uNode->uplink->clients;
3061 }
3062 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
3063 uNode->uplink->users[uNode->num_local] = uNode;
3064 mod_usermode(uNode, modes);
3065 if (dummy)
3066 uNode->modes |= FLAGS_DUMMY;
3067
3068 set_geoip_info(uNode);
3069
3070 if (ignore_user)
3071 return uNode;
3072
3073 dict_insert(clients, uNode->nick, uNode);
3074 if (dict_size(clients) > max_clients) {
3075 max_clients = dict_size(clients);
3076 max_clients_time = now;
3077 }
3078 if (IsLocal(uNode))
3079 irc_user(uNode);
3080 for (n=0; (n<nuf_used) && !uNode->dead; n++)
3081 nuf_list[n](uNode);
3082
3083 if ((uNode->loc == 1) && (uNode->handle_info))
3084 send_func_list(uNode);
3085
3086 return uNode;
3087 }
3088
3089 /* removes user from it's server's hash table and nick hash table */
3090 void
3091 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
3092 {
3093 unsigned int n;
3094
3095 verify(user);
3096
3097 /* mark them as dead, in case anybody cares */
3098 user->dead = 1;
3099
3100 /* remove pending adduser commands */
3101 wipe_adduser_pending(NULL, user);
3102
3103 /* remove user from all channels */
3104 while (user->channels.used > 0)
3105 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, NULL, 0);
3106
3107 /* Call these in reverse order so ChanServ can update presence
3108 information before NickServ nukes the handle_info. */
3109 for (n = duf_used; n > 0; )
3110 duf_list[--n](user, killer, why);
3111
3112 user->uplink->clients--;
3113 user->uplink->users[user->num_local] = NULL;
3114 if (IsOper(user))
3115 userList_remove(&curr_opers, user);
3116 /* remove from global dictionary, but not if after a collide */
3117 if (user == dict_find(clients, user->nick, NULL))
3118 dict_remove(clients, user->nick);
3119
3120 if (IsInvisible(user))
3121 invis_clients--;
3122
3123 if (announce) {
3124 if (IsLocal(user))
3125 irc_quit(user, why);
3126 else
3127 irc_kill(killer, user, why);
3128 }
3129
3130 if (IsLocal(user)) {
3131 unsigned int num = user->num_local;
3132 if (num < num_privmsg_funcs)
3133 privmsg_funcs[num] = NULL;
3134 if (num < num_notice_funcs)
3135 notice_funcs[num] = NULL;
3136 }
3137
3138 modeList_clean(&user->channels);
3139
3140 /* Clean up version data */
3141 if(user->version_reply) {
3142 free(user->version_reply);
3143 user->version_reply = NULL;
3144 }
3145
3146 /* clean up mark */
3147 if(user->mark) {
3148 free(user->mark);
3149 user->mark = NULL;
3150 }
3151
3152 /* clean up geoip data if any */
3153 if(user->country_code) free(user->country_code);
3154 if(user->city) free(user->city);
3155 if(user->region) free(user->region);
3156 if(user->postal_code) free(user->postal_code);
3157
3158 /* We don't free them, in case we try to privmsg them or something
3159 * (like when a stupid oper kills themself). We just put them onto
3160 * a list of clients that get freed after processing each line.
3161 */
3162 if (dead_users.size)
3163 userList_append(&dead_users, user);
3164 else
3165 free_user(user);
3166 }
3167
3168 static void call_oper_funcs(struct userNode *user);
3169
3170 void mod_usermode(struct userNode *user, const char *mode_change) {
3171 int add = 1;
3172 const char *word = mode_change;
3173
3174 if (!user || !mode_change)
3175 return;
3176
3177 call_user_mode_funcs(user, mode_change);
3178
3179 while (*word != ' ' && *word) word++;
3180 while (*word == ' ') word++;
3181 while (1) {
3182 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3183 switch (*mode_change++) {
3184 case 0: case ' ': return;
3185 case '+': add = 1; break;
3186 case '-': add = 0; break;
3187 case 'o':
3188 do_user_mode(FLAGS_OPER);
3189 if (!add) {
3190 userList_remove(&curr_opers, user);
3191 } else if (!userList_contains(&curr_opers, user)) {
3192 userList_append(&curr_opers, user);
3193 call_oper_funcs(user);
3194 }
3195 break;
3196 case 'O': do_user_mode(FLAGS_LOCOP); break;
3197 case 'i': do_user_mode(FLAGS_INVISIBLE);
3198 if (add)
3199 invis_clients++;
3200 else
3201 invis_clients--;
3202 break;
3203 case 'w': do_user_mode(FLAGS_WALLOP); break;
3204 case 'd': do_user_mode(FLAGS_DEAF); break;
3205 case 'k': do_user_mode(FLAGS_SERVICE); break;
3206 case 'g': do_user_mode(FLAGS_GLOBAL); break;
3207 case 'B': do_user_mode(FLAGS_BOT); break;
3208 case 'n': do_user_mode(FLAGS_HIDECHANS); break;
3209 case 'I': do_user_mode(FLAGS_HIDEIDLE); break;
3210 case 'X': do_user_mode(FLAGS_XTRAOP); break;
3211 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3212 if (*word) {
3213 char cloakhost[MAXLEN];
3214 unsigned int ii;
3215 for (ii=0; (*word != ' ') && (*word != '\0'); )
3216 cloakhost[ii++] = *word++;
3217 cloakhost[ii] = 0;
3218 while (*word == ' ')
3219 word++;
3220 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3221 }
3222 break;
3223 case 'c': do_user_mode(FLAGS_CLOAKIP);
3224 if (*word) {
3225 char cloakip[MAXLEN];
3226 unsigned int ii;
3227 for (ii=0; (*word != ' ') && (*word != '\0'); )
3228 cloakip[ii++] = *word++;
3229 cloakip[ii] = 0;
3230 while (*word == ' ')
3231 word++;
3232 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3233 }
3234 break;
3235 // sethost - reed/apples
3236 // case 'h': do_user_mode(FLAGS_HELPER); break;
3237 // I check if there's an 'h' in the first part, and if there,
3238 // then everything after the space becomes their new host.
3239 case 'h': do_user_mode(FLAGS_SETHOST);
3240 if (*word) {
3241 char sethost[MAXLEN];
3242 unsigned int ii;
3243 for (ii=0; (*word != ' ') && (*word != '\0'); )
3244 sethost[ii++] = *word++;
3245 sethost[ii] = 0;
3246 while (*word == ' ')
3247 word++;
3248 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3249 }
3250 break;
3251 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3252 case 'r':
3253 if (*word) {
3254 char tag[MAXLEN];
3255 unsigned int ii;
3256 for (ii=0; (*word != ' ') && (*word != '\0'); )
3257 tag[ii++] = *word++;
3258 tag[ii] = 0;
3259 while (*word == ' ')
3260 word++;
3261 call_account_func(user, tag);
3262 }
3263 break;
3264 case 'f':
3265 if (*word) {
3266 char host[MAXLEN];
3267 unsigned int ii;
3268 for (ii=0; (*word != ' ') && (*word != '\0'); )
3269 host[ii++] = *word++;
3270 host[ii] = 0;
3271 while (*word == ' ')
3272 word++;
3273 assign_fakehost(user, host, 0);
3274 }
3275 break;
3276 }
3277 #undef do_user_mode
3278 }
3279 }
3280
3281 static int
3282 keyncpy(char output[], char input[], size_t output_size)
3283 {
3284 size_t ii;
3285
3286 if (input[0] == ':')
3287 {
3288 output[0] = '\0';
3289 return 1;
3290 }
3291
3292 for (ii = 0; (ii + 1 < output_size) && (input[ii] != '\0'); ++ii)
3293 {
3294 output[ii] = input[ii];
3295 }
3296
3297 output[ii] = '\0';
3298 return 0;
3299 }
3300
3301 struct mod_chanmode *
3302 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
3303 {
3304 struct mod_chanmode *change;
3305 unsigned int ii, in_arg, ch_arg, add;
3306
3307 if (argc == 0)
3308 return NULL;
3309 if (!(change = mod_chanmode_alloc(argc - 1)))
3310 return NULL;
3311
3312 for (ii = ch_arg = 0, in_arg = add = 1;
3313 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3314 ++ii) {
3315 switch (modes[0][ii]) {
3316 case '+':
3317 add = 1;
3318 break;
3319 case '-':
3320 add = 0;
3321 break;
3322 #define do_chan_mode(FLAG) do { if (add) change->modes_set |= (FLAG), change->modes_clear &= ~(FLAG); else change->modes_clear |= (FLAG), change->modes_set &= ~(FLAG); } while(0)
3323 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3324 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3325 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3326 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3327 case 'm': do_chan_mode(MODE_MODERATED); break;
3328 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3329 case 'p': do_chan_mode(MODE_PRIVATE); break;
3330 case 'r': do_chan_mode(MODE_REGONLY); break;
3331 case 's': do_chan_mode(MODE_SECRET); break;
3332 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
3333 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3334 case 'M': do_chan_mode(MODE_MODUNREG); break;
3335 case 'N': do_chan_mode(MODE_NONOTICE); break;
3336 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3337 case 'T': do_chan_mode(MODE_NOAMSG); break;
3338 case 'O': do_chan_mode(MODE_OPERSONLY); break;
3339 case 'a': do_chan_mode(MODE_ADMINSONLY); break;
3340 case 'Z': do_chan_mode(MODE_SSLONLY); break;
3341 case 'L': do_chan_mode(MODE_HIDEMODE); break;
3342 case 'z':
3343 if (!(flags & MCP_REGISTERED)) {
3344 do_chan_mode(MODE_REGISTERED);
3345 } else {
3346 mod_chanmode_free(change);
3347 return NULL;
3348 }
3349 break;
3350 #undef do_chan_mode
3351 case 'l':
3352 if (add) {
3353 if (in_arg >= argc)
3354 goto error;
3355 change->modes_set |= MODE_LIMIT;
3356 change->new_limit = atoi(modes[in_arg++]);
3357 } else {
3358 change->modes_set &= ~MODE_LIMIT;
3359 change->modes_clear |= MODE_LIMIT;
3360 }
3361 break;
3362 case 'k':
3363 if (add) {
3364 if ((in_arg >= argc)
3365 || keyncpy(change->new_key, modes[in_arg++], sizeof(change->new_key)))
3366 goto error;
3367 change->modes_set |= MODE_KEY;
3368 } else {
3369 change->modes_clear |= MODE_KEY;
3370 if (!(flags & MCP_KEY_FREE)) {
3371 if (in_arg >= argc)
3372 goto error;
3373 in_arg++;
3374 }
3375 }
3376 break;
3377 case 'U':
3378 if (add)
3379 {
3380 if ((in_arg >= argc)
3381 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3382 goto error;
3383 change->modes_set |= MODE_UPASS;
3384 } else {
3385 change->modes_clear |= MODE_UPASS;
3386 if (!(flags & MCP_UPASS_FREE)) {
3387 if (in_arg >= argc)
3388 goto error;
3389 in_arg++;
3390 }
3391 }
3392 break;
3393 case 'A':
3394 if (add) {
3395 if ((in_arg >= argc)
3396 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3397 change->modes_set |= MODE_APASS;
3398 } else {
3399 change->modes_clear |= MODE_APASS;
3400 if (!(flags & MCP_APASS_FREE)) {
3401 if (in_arg >= argc)
3402 goto error;
3403 in_arg++;
3404 }
3405 }
3406 break;
3407 case 'b':
3408 if (!(flags & MCP_ALLOW_OVB))
3409 goto error;
3410 if (in_arg >= argc)
3411 goto error;
3412 change->args[ch_arg].mode = MODE_BAN;
3413 if (!add)
3414 change->args[ch_arg].mode |= MODE_REMOVE;
3415 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3416 break;
3417 case 'e':
3418 if (!(flags & MCP_ALLOW_OVB))
3419 goto error;
3420 if (in_arg >= argc)
3421 goto error;
3422 change->args[ch_arg].mode = MODE_EXEMPT;
3423 if (!add)
3424 change->args[ch_arg].mode |= MODE_REMOVE;
3425 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3426 break;
3427 case 'o': case 'h': case 'v':
3428 {
3429 struct userNode *victim;
3430 char *oplevel_str;
3431 int oplevel;
3432
3433 if (in_arg >= argc)
3434 goto error;
3435 oplevel_str = strchr(modes[in_arg], ':');
3436 if (oplevel_str)
3437 {
3438 /* XXYYY M #channel +o XXYYY:<oplevel> */
3439 *oplevel_str++ = '\0';
3440 oplevel = parse_oplevel(oplevel_str);
3441 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3442 oplevel = base_oplevel + 1;
3443 }
3444 else if (channel->modes & MODE_UPASS)
3445 oplevel = base_oplevel + 1;
3446 else
3447 oplevel = -1;
3448
3449 /* Check that oplevel is within bounds. */
3450 if (oplevel > MAXOPLEVEL)
3451 oplevel = MAXOPLEVEL;
3452
3453 if (!(flags & MCP_ALLOW_OVB))
3454 goto error;
3455 if (in_arg >= argc)
3456 goto error;
3457
3458 if (modes[0][ii] == 'o')
3459 change->args[ch_arg].mode = MODE_CHANOP;
3460 else if (modes[0][ii] == 'h')
3461 change->args[ch_arg].mode = MODE_HALFOP;
3462 else if (modes[0][ii] == 'v')
3463 change->args[ch_arg].mode = MODE_VOICE;
3464
3465 if (!add)
3466 change->args[ch_arg].mode |= MODE_REMOVE;
3467 if (flags & MCP_FROM_SERVER)
3468 victim = GetUserN(modes[in_arg++]);
3469 else
3470 victim = GetUserH(modes[in_arg++]);
3471 if (!victim)
3472 continue;
3473 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
3474 {
3475 /* Apply the oplevel change */
3476 change->args[ch_arg].u.member->oplevel = oplevel;
3477 ch_arg++;
3478 }
3479 break;
3480 }
3481 default:
3482 if (!(flags & MCP_FROM_SERVER))
3483 goto error;
3484 break;
3485 }
3486 }
3487 change->argc = ch_arg; /* in case any turned out to be ignored */
3488 if (change->modes_set & MODE_SECRET) {
3489 change->modes_set &= ~(MODE_PRIVATE);
3490 change->modes_clear |= MODE_PRIVATE;
3491 } else if (change->modes_set & MODE_PRIVATE) {
3492 change->modes_set &= ~(MODE_SECRET);
3493 change->modes_clear |= MODE_SECRET;
3494 }
3495 return change;
3496 error:
3497 mod_chanmode_free(change);
3498 return NULL;
3499 }
3500
3501 struct chanmode_buffer {
3502 char modes[MAXLEN];
3503 char args[MAXLEN];
3504 struct chanNode *channel;
3505 struct userNode *actor;
3506 unsigned int modes_used;
3507 unsigned int args_used;
3508 size_t chname_len;
3509 unsigned int is_add : 1;
3510 unsigned int is_chanop : 1;
3511 };
3512
3513 static void
3514 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3515 {
3516 size_t arg_len = strlen(arg);
3517 if (buf->modes_used > (MAXMODEPARAMS) ||
3518 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3519 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3520 buf->modes[buf->modes_used + buf->args_used] = '\0';
3521 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3522 buf->modes[0] = buf->is_add ? '+' : '-';
3523 buf->modes_used = 1;
3524 buf->args_used = 0;
3525 }
3526 buf->modes[buf->modes_used++] = ch;
3527 buf->args[buf->args_used++] = ' ';
3528 memcpy(buf->args + buf->args_used, arg, arg_len);
3529 buf->args_used += arg_len;
3530 }
3531
3532 void
3533 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3534 {
3535 struct chanmode_buffer chbuf;
3536 unsigned int arg;
3537 struct modeNode *mn;
3538 char int_buff[32], mode = '\0';
3539
3540 assert(change->argc <= change->alloc_argc);
3541 memset(&chbuf, 0, sizeof(chbuf));
3542 chbuf.channel = channel;
3543 chbuf.actor = who;
3544 chbuf.chname_len = strlen(channel->name);
3545
3546 mn = GetUserMode(channel, who);
3547 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3548 chbuf.is_chanop = 1;
3549
3550 /* First remove modes */
3551 chbuf.is_add = 0;
3552 if (change->modes_clear) {
3553 if (mode != '-')
3554 chbuf.modes[chbuf.modes_used++] = mode = '-';
3555 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3556 DO_MODE_CHAR(PRIVATE, 'p');
3557 DO_MODE_CHAR(SECRET, 's');
3558 DO_MODE_CHAR(MODERATED, 'm');
3559 DO_MODE_CHAR(TOPICLIMIT, 't');
3560 DO_MODE_CHAR(INVITEONLY, 'i');
3561 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3562 DO_MODE_CHAR(LIMIT, 'l');
3563 DO_MODE_CHAR(DELAYJOINS, 'D');
3564 DO_MODE_CHAR(REGONLY, 'r');
3565 DO_MODE_CHAR(NOCOLORS, 'c');
3566 DO_MODE_CHAR(NOCTCPS, 'C');
3567 DO_MODE_CHAR(STRIPCOLOR, 'S');
3568 DO_MODE_CHAR(MODUNREG, 'M');
3569 DO_MODE_CHAR(NONOTICE, 'N');
3570 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3571 DO_MODE_CHAR(NOAMSG, 'T');
3572 DO_MODE_CHAR(OPERSONLY, 'O');
3573 DO_MODE_CHAR(REGISTERED, 'z');
3574 DO_MODE_CHAR(SSLONLY, 'Z');
3575 DO_MODE_CHAR(HIDEMODE, 'L');
3576 #undef DO_MODE_CHAR
3577 if (change->modes_clear & channel->modes & MODE_KEY)
3578 mod_chanmode_append(&chbuf, 'k', channel->key);
3579 if (change->modes_clear & channel->modes & MODE_UPASS)
3580 mod_chanmode_append(&chbuf, 'U', channel->upass);
3581 if (change->modes_clear & channel->modes & MODE_APASS)
3582 mod_chanmode_append(&chbuf, 'A', channel->apass);
3583 }
3584 for (arg = 0; arg < change->argc; ++arg) {
3585 if (!(change->args[arg].mode & MODE_REMOVE))
3586 continue;
3587 if (mode != '-')
3588 chbuf.modes[chbuf.modes_used++] = mode = '-';
3589 switch (change->args[arg].mode & ~MODE_REMOVE) {
3590 case MODE_BAN:
3591 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3592 break;
3593 case MODE_EXEMPT:
3594 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3595 break;
3596 default:
3597 if (change->args[arg].mode & MODE_CHANOP)
3598 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3599 if (change->args[arg].mode & MODE_HALFOP)
3600 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3601 if (change->args[arg].mode & MODE_VOICE)
3602 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3603 break;
3604 }
3605 }
3606
3607 /* Then set them */
3608 chbuf.is_add = 1;
3609 if (change->modes_set) {
3610 if (mode != '+')
3611 chbuf.modes[chbuf.modes_used++] = mode = '+';
3612 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3613 DO_MODE_CHAR(PRIVATE, 'p');
3614 DO_MODE_CHAR(SECRET, 's');
3615 DO_MODE_CHAR(MODERATED, 'm');
3616 DO_MODE_CHAR(TOPICLIMIT, 't');
3617 DO_MODE_CHAR(INVITEONLY, 'i');
3618 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3619 DO_MODE_CHAR(DELAYJOINS, 'D');
3620 DO_MODE_CHAR(REGONLY, 'r');
3621 DO_MODE_CHAR(NOCOLORS, 'c');
3622 DO_MODE_CHAR(NOCTCPS, 'C');
3623 DO_MODE_CHAR(STRIPCOLOR, 'S');
3624 DO_MODE_CHAR(MODUNREG, 'M');
3625 DO_MODE_CHAR(NONOTICE, 'N');
3626 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3627 DO_MODE_CHAR(NOAMSG, 'T');
3628 DO_MODE_CHAR(OPERSONLY, 'O');
3629 DO_MODE_CHAR(REGISTERED, 'z');
3630 DO_MODE_CHAR(SSLONLY, 'Z');
3631 DO_MODE_CHAR(HIDEMODE, 'L');
3632 #undef DO_MODE_CHAR
3633 if(change->modes_set & MODE_KEY)
3634 mod_chanmode_append(&chbuf, 'k', change->new_key);
3635 if (change->modes_set & MODE_UPASS)
3636 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3637 if (change->modes_set & MODE_APASS)
3638 mod_chanmode_append(&chbuf, 'A', change->new_apass);
3639 if(change->modes_set & MODE_LIMIT) {
3640 sprintf(int_buff, "%d", change->new_limit);
3641 mod_chanmode_append(&chbuf, 'l', int_buff);
3642 }
3643 }
3644 for (arg = 0; arg < change->argc; ++arg) {
3645 if (change->args[arg].mode & MODE_REMOVE)
3646 continue;
3647 if (mode != '+')
3648 chbuf.modes[chbuf.modes_used++] = mode = '+';
3649 switch (change->args[arg].mode) {
3650 case MODE_BAN:
3651 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3652 break;
3653 case MODE_EXEMPT:
3654 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3655 break;
3656 default:
3657 if (change->args[arg].mode & MODE_CHANOP)
3658 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3659 if (change->args[arg].mode & MODE_HALFOP)
3660 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3661 if (change->args[arg].mode & MODE_VOICE)
3662 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3663 break;
3664 }
3665 }
3666
3667 /* Flush the buffer and apply changes locally */
3668 if (chbuf.modes_used > 0) {
3669 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3670 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3671 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3672 }
3673 mod_chanmode_apply(who, channel, change);
3674 }
3675
3676 char *
3677 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3678 {
3679 unsigned int used = 0;
3680 assert(change->argc <= change->alloc_argc);
3681 if (change->modes_clear) {
3682 outbuff[used++] = '-';
3683 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3684 DO_MODE_CHAR(PRIVATE, 'p');
3685 DO_MODE_CHAR(SECRET, 's');
3686 DO_MODE_CHAR(MODERATED, 'm');
3687 DO_MODE_CHAR(TOPICLIMIT, 't');
3688 DO_MODE_CHAR(INVITEONLY, 'i');
3689 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3690 DO_MODE_CHAR(LIMIT, 'l');
3691 DO_MODE_CHAR(KEY, 'k');
3692 DO_MODE_CHAR(UPASS, 'U');
3693 DO_MODE_CHAR(APASS, 'A');
3694 DO_MODE_CHAR(DELAYJOINS, 'D');
3695 DO_MODE_CHAR(REGONLY, 'r');
3696 DO_MODE_CHAR(NOCOLORS, 'c');
3697 DO_MODE_CHAR(NOCTCPS, 'C');
3698 DO_MODE_CHAR(STRIPCOLOR, 'S');
3699 DO_MODE_CHAR(MODUNREG, 'M');
3700 DO_MODE_CHAR(NONOTICE, 'N');
3701 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3702 DO_MODE_CHAR(NOAMSG, 'T');
3703 DO_MODE_CHAR(OPERSONLY, 'O');
3704 DO_MODE_CHAR(REGISTERED, 'z');
3705 DO_MODE_CHAR(SSLONLY, 'Z');
3706 DO_MODE_CHAR(HIDEMODE, 'L');
3707 #undef DO_MODE_CHAR
3708 }
3709 if (change->modes_set) {
3710 outbuff[used++] = '+';
3711 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3712 DO_MODE_CHAR(PRIVATE, 'p');
3713 DO_MODE_CHAR(SECRET, 's');
3714 DO_MODE_CHAR(MODERATED, 'm');
3715 DO_MODE_CHAR(TOPICLIMIT, 't');
3716 DO_MODE_CHAR(INVITEONLY, 'i');
3717 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3718 DO_MODE_CHAR(DELAYJOINS, 'D');
3719 DO_MODE_CHAR(REGONLY, 'r');
3720 DO_MODE_CHAR(NOCOLORS, 'c');
3721 DO_MODE_CHAR(NOCTCPS, 'C');
3722 DO_MODE_CHAR(STRIPCOLOR, 'S');
3723 DO_MODE_CHAR(MODUNREG, 'M');
3724 DO_MODE_CHAR(NONOTICE, 'N');
3725 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3726 DO_MODE_CHAR(NOAMSG, 'T');
3727 DO_MODE_CHAR(OPERSONLY, 'O');
3728 DO_MODE_CHAR(REGISTERED, 'z');
3729 DO_MODE_CHAR(SSLONLY, 'Z');
3730 DO_MODE_CHAR(HIDEMODE, 'L');
3731 #undef DO_MODE_CHAR
3732 switch (change->modes_set & (MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)) {
3733 /* Doing this implementation has been a pain in the arse, I hope I didn't forget a possible combination */
3734 case MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS:
3735 used += sprintf(outbuff+used, "lkAU %d %s %s %s", change->new_limit, change->new_key,change->new_apass, change->new_upass);
3736 break;
3737
3738 case MODE_KEY|MODE_LIMIT|MODE_APASS:
3739 used += sprintf(outbuff+used, "lkA %d %s %s", change->new_limit, change->new_key, change->new_apass);
3740 break;
3741 case MODE_KEY|MODE_LIMIT|MODE_UPASS:
3742 used += sprintf(outbuff+used, "lkU %d %s %s", change->new_limit, change->new_key, change->new_upass);
3743 break;
3744 case MODE_KEY|MODE_APASS|MODE_UPASS:
3745 used += sprintf(outbuff+used, "kAU %s %s %s", change->new_key, change->new_apass, change->new_upass);
3746 break;
3747
3748 case MODE_KEY|MODE_APASS:
3749 used += sprintf(outbuff+used, "kA %s %s", change->new_key, change->new_apass);
3750 break;
3751 case MODE_KEY|MODE_UPASS:
3752 used += sprintf(outbuff+used, "kU %s %s", change->new_key, change->new_upass);
3753 break;
3754 case MODE_KEY|MODE_LIMIT:
3755 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
3756 break;
3757
3758 case MODE_LIMIT|MODE_UPASS:
3759 used += sprintf(outbuff+used, "lU %d %s", change->new_limit, change->new_upass);
3760 break;
3761 case MODE_LIMIT|MODE_APASS:
3762 used += sprintf(outbuff+used, "lA %d %s", change->new_limit, change->new_apass);
3763 break;
3764 case MODE_APASS|MODE_UPASS:
3765 used += sprintf(outbuff+used, "AU %s %s", change->new_apass, change->new_upass);
3766
3767 case MODE_LIMIT|MODE_APASS|MODE_UPASS:
3768 used += sprintf(outbuff+used, "lAU %d %s %s", change->new_limit, change->new_apass, change->new_upass);
3769 break;
3770
3771 case MODE_APASS:
3772 used += sprintf(outbuff+used, "A %s", change->new_apass);
3773 break;
3774 case MODE_UPASS:
3775 used += sprintf(outbuff+used, "U %s", change->new_upass);
3776 break;
3777 case MODE_KEY:
3778 used += sprintf(outbuff+used, "k %s", change->new_key);
3779 break;
3780 case MODE_LIMIT:
3781 used += sprintf(outbuff+used, "l %d", change->new_limit);
3782 break;
3783 }
3784 }
3785 outbuff[used] = 0;
3786 return outbuff;
3787 }
3788
3789 static int
3790 clear_chanmode(struct chanNode *channel, const char *modes)
3791 {
3792 unsigned int cleared;
3793
3794 for (cleared = 0; *modes; modes++) {
3795 switch (*modes) {
3796 case 'o': cleared |= MODE_CHANOP; break;
3797 case 'h': cleared |= MODE_HALFOP; break;
3798 case 'v': cleared |= MODE_VOICE; break;
3799 case 'p': cleared |= MODE_PRIVATE; break;
3800 case 's': cleared |= MODE_SECRET; break;
3801 case 'm': cleared |= MODE_MODERATED; break;
3802 case 't': cleared |= MODE_TOPICLIMIT; break;
3803 case 'i': cleared |= MODE_INVITEONLY; break;
3804 case 'n': cleared |= MODE_NOPRIVMSGS; break;
3805 case 'k':
3806 cleared |= MODE_KEY;
3807 channel->key[0] = '\0';
3808 break;
3809 case 'A':
3810 cleared |= MODE_APASS;
3811 channel->apass[0] = '\0';
3812 break;
3813 case 'U':
3814 cleared |= MODE_UPASS;
3815 channel->upass[0] = '\0';
3816 break;
3817 case 'l':
3818 cleared |= MODE_LIMIT;
3819 channel->limit = 0;
3820 break;
3821 case 'b': cleared |= MODE_BAN; break;
3822 case 'e': cleared |= MODE_EXEMPT; break;
3823 case 'D': cleared |= MODE_DELAYJOINS; break;
3824 case 'r': cleared |= MODE_REGONLY; break;
3825 case 'c': cleared |= MODE_NOCOLORS; break;
3826 case 'C': cleared |= MODE_NOCTCPS; break;
3827 case 'S': cleared |= MODE_STRIPCOLOR; break;
3828 case 'M': cleared |= MODE_MODUNREG; break;
3829 case 'N': cleared |= MODE_NONOTICE; break;
3830 case 'Q': cleared |= MODE_NOQUITMSGS; break;
3831 case 'T': cleared |= MODE_NOAMSG; break;
3832 case 'O': cleared |= MODE_OPERSONLY; break;
3833 case 'a': cleared |= MODE_ADMINSONLY; break;
3834 case 'z': cleared |= MODE_REGISTERED; break;
3835 case 'Z': cleared |= MODE_SSLONLY; break;
3836 case 'L': cleared |= MODE_HIDEMODE; break;
3837 }
3838 }
3839
3840 if (!cleared)
3841 return 1;
3842
3843 /* Remove simple modes. */
3844 channel->modes &= ~cleared;
3845
3846 /* If removing bans, kill 'em all. */
3847 if ((cleared & MODE_BAN) && channel->banlist.used) {
3848 unsigned int i;
3849 for (i=0; i<channel->banlist.used; i++)
3850 free(channel->banlist.list[i]);
3851 channel->banlist.used = 0;
3852 }
3853
3854 /* If removing exempts, kill 'em all. */
3855 if ((cleared & MODE_EXEMPT) && channel->exemptlist.used) {
3856 unsigned int i;
3857 for (i=0; i<channel->exemptlist.used; i++)
3858 free(channel->exemptlist.list[i]);
3859 channel->exemptlist.used = 0;
3860 }
3861
3862 /* Removed member modes. */
3863 if ((cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
3864 int mask = ~(cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3865 unsigned int i;
3866
3867 for (i = 0; i < channel->members.used; i++)
3868 channel->members.list[i]->modes &= mask;
3869 }
3870
3871 return 1;
3872 }
3873
3874 void
3875 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3876 {
3877 unsigned int numeric = user->num_local;
3878 if (numeric >= num_privmsg_funcs) {
3879 int newnum = numeric + 8, ii;
3880 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
3881 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
3882 privmsg_funcs[ii] = NULL;
3883 num_privmsg_funcs = newnum;
3884 }
3885 if (privmsg_funcs[numeric])
3886 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
3887 privmsg_funcs[numeric] = handler;
3888 }
3889
3890 void
3891 unreg_privmsg_func(struct userNode *user)
3892 {
3893 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
3894 return; /* this really only works with users */
3895
3896 privmsg_funcs[user->num_local] = NULL;
3897 }
3898
3899
3900 void
3901 reg_notice_func(struct userNode *user, privmsg_func_t handler)
3902 {
3903 unsigned int numeric = user->num_local;
3904 if (numeric >= num_notice_funcs) {
3905 int newnum = numeric + 8, ii;
3906 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
3907 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
3908 privmsg_funcs[ii] = NULL;
3909 num_notice_funcs = newnum;
3910 }
3911 if (notice_funcs[numeric])
3912 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
3913 notice_funcs[numeric] = handler;
3914 }
3915
3916 void
3917 unreg_notice_func(struct userNode *user)
3918 {
3919 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
3920 return; /* this really only works with users */
3921
3922 notice_funcs[user->num_local] = NULL;
3923 }
3924
3925 static void
3926 send_burst(void)
3927 {
3928 unsigned int i, hop, max_hop=1;
3929 dict_iterator_t it;
3930
3931 /* burst (juped) servers, closest first (except self, which is sent already) */
3932 for (i=0; i<ArrayLength(servers_num); i++)
3933 if (servers_num[i] && servers_num[i]->hops > max_hop)
3934 max_hop = servers_num[i]->hops;
3935 for (hop=1; hop<=max_hop; hop++) {
3936 for (i=0;i<ArrayLength(servers_num);i++) {
3937 if (servers_num[i]
3938 && (servers_num[i]->hops == hop)
3939 && (servers_num[i] != self->uplink))
3940 irc_server(servers_num[i]);
3941 }
3942 }
3943
3944 /* burst local nicks */
3945 for (i=0; i<=self->num_mask; i++)
3946 if (self->users[i])
3947 irc_user(self->users[i]);
3948
3949 /* build dict of unbursted channel names (just copy existing channels) */
3950 unbursted_channels = dict_new();
3951 for (it = dict_first(channels); it; it = iter_next(it))
3952 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
3953 }
3954
3955 /*
3956 * Oplevel parsing
3957 */
3958 static int
3959 parse_oplevel(char *str)
3960 {
3961 int oplevel = 0;
3962 while (isdigit(*str))
3963 oplevel = oplevel * 10 + *str++ - '0';
3964 return oplevel;
3965 }
3966