]> jfr.im git - irc/evilnet/x3.git/blob - src/proto-p10.c
add extra parameter to exit_func
[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
1370 if (!(sender = GetServerH(origin)))
1371 return 0;
1372 if (sender == self->uplink) {
1373 cManager.uplink->state = CONNECTED;
1374 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1375 irc_burst(iter_data(it));
1376 dict_delete(unbursted_channels);
1377 unbursted_channels = NULL;
1378 irc_eob();
1379 irc_eob_ack();
1380
1381 /* now that we know who our uplink is,
1382 * we can center the routing map and activate auto-routing.
1383 */
1384 //activate_routing(NULL, NULL, NULL);
1385 routing_init();
1386 }
1387 sender->self_burst = 0;
1388 recalc_bursts(sender);
1389 call_server_link_funcs(sender);
1390 /* let auto-routing figure out if we were
1391 * wating on this server to link a child to it */
1392 /* DONT call this if uplink is _US_ */
1393 if(sender->uplink != self)
1394 routing_handle_connect(sender->name, sender->uplink->name);
1395 return 1;
1396 }
1397
1398 static CMD_FUNC(cmd_eob_ack)
1399 {
1400 extern time_t burst_begin;
1401
1402 if (GetServerH(origin) == self->uplink) {
1403 burst_length = now - burst_begin;
1404 self->self_burst = self->burst = 0;
1405 }
1406 cManager.uplink->state = CONNECTED;
1407 return 1;
1408 }
1409
1410 static CMD_FUNC(cmd_rping)
1411 {
1412 struct server *dest;
1413
1414 if (!(dest = GetServerN(argv[1])))
1415 return 1; /* if its a jupe or something, return 1 so its silently ignored */
1416
1417 if (dest == self)
1418 irc_rpong(argv[2], argv[3], argv[4], argv[5]);
1419
1420 return 1;
1421 }
1422
1423 static CMD_FUNC(cmd_ping)
1424 {
1425 struct server *srv;
1426 struct userNode *un;
1427
1428 if (argc > 3)
1429 irc_pong_asll(argv[2], argv[3]);
1430 else if ((srv = GetServerH(origin)))
1431 irc_pong(self->name, srv->numeric);
1432 else if ((un = GetUserH(origin)))
1433 irc_pong(self->name, un->numeric);
1434 else
1435 irc_pong(self->name, origin);
1436
1437 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1438 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1439 timeq_add(now + ping_freq, timed_send_ping, 0);
1440 received_ping();
1441 return 1;
1442 }
1443
1444 static CMD_FUNC(cmd_error_nick)
1445 {
1446 /* Go back to original IRC length .. and try to reconnect :/ */
1447 change_nicklen(9);
1448 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1449 return 1;
1450 }
1451
1452 struct create_desc {
1453 struct userNode *user;
1454 time_t when;
1455 };
1456
1457 static void
1458 join_helper(struct chanNode *chan, void *data)
1459 {
1460 struct create_desc *cd = data;
1461 AddChannelUser(cd->user, chan);
1462 }
1463
1464 static void
1465 create_helper(char *name, void *data)
1466 {
1467 struct create_desc *cd = data;
1468
1469 if (!strcmp(name, "0")) {
1470 while (cd->user->channels.used > 0)
1471 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1472 return;
1473 }
1474
1475 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
1476 }
1477
1478 static CMD_FUNC(cmd_create)
1479 {
1480 struct create_desc cd;
1481 struct userNode *user;
1482
1483 if ((argc < 3) || !(user = GetUserH(origin)))
1484 return 0;
1485 cd.user = user;
1486 cd.when = atoi(argv[2]);
1487 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1488 return 1;
1489 }
1490
1491 static CMD_FUNC(cmd_join)
1492 {
1493 struct create_desc cd;
1494
1495 if (!(cd.user = GetUserH(origin)))
1496 return 0;
1497 if (argc < 2)
1498 return 0;
1499 else if (argc < 3)
1500 cd.when = now;
1501 else
1502 cd.when = atoi(argv[2]);
1503 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1504 return 1;
1505 }
1506
1507 static CMD_FUNC(cmd_svsjoin)
1508 {
1509 struct create_desc cd;
1510
1511 if (!(cd.user = GetUserH(argv[1])))
1512 return 0;
1513 if (argc < 3)
1514 return 0;
1515 else if (argc < 4)
1516 cd.when = now;
1517 else
1518 cd.when = atoi(argv[3]);
1519 parse_foreach(argv[2], join_helper, create_helper, NULL, NULL, &cd);
1520 return 1;
1521 }
1522
1523 static CMD_FUNC(cmd_pong)
1524 {
1525 if (argc < 3)
1526 return 0;
1527 if (!strcmp(argv[2], self->name)) {
1528 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1529 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1530 timeq_add(now + ping_freq, timed_send_ping, 0);
1531 received_ping();
1532 }
1533 return 1;
1534 }
1535
1536 static CMD_FUNC(cmd_nick)
1537 {
1538 struct userNode *user;
1539 if ((user = GetUserH(origin))) {
1540 /* nick change (since the source is a user numeric) */
1541 if (argc < 2)
1542 return 0;
1543 NickChange(user, argv[1], 1);
1544 } else {
1545 struct server *serv;
1546 struct userNode *nuser;
1547 char modes[MAXLEN];
1548 /* new nick */
1549 if (argc < 9)
1550 return 0;
1551 serv = GetServerH(origin);
1552 if (argc > 9)
1553 unsplit_string(argv+6, argc-9, modes);
1554 else
1555 strcpy(modes, "+");
1556 nuser = AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1557 }
1558 return 1;
1559 }
1560
1561 static CMD_FUNC(cmd_account)
1562 {
1563 struct userNode *user;
1564 struct server *server;
1565 struct handle_info *hi;
1566
1567 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
1568 return 0; /* Origin must be server. */
1569
1570 /* This next line appears to tremple origin.. why? */
1571 user = GetUserN(argv[1]);
1572 if (!user)
1573 return 1; /* A QUIT probably passed the ACCOUNT. */
1574
1575 if(!extended_accounts) /* any need for this function without? */
1576 return 1;
1577
1578 if(!strcmp(argv[2],"C"))
1579 {
1580 if((hi = loc_auth(argv[4], argv[5], NULL)))
1581 {
1582 /* Return a AC A */
1583 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1584
1585 log_module(MAIN_LOG, LOG_DEBUG, "loc_auth: %s\n", user->nick);
1586 }
1587 else
1588 {
1589 /* Return a AC D */
1590 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1591 }
1592 return 1;
1593 }
1594 else if(!strcmp(argv[2],"H")) /* New enhanced (host) version of C */
1595 {
1596 if((hi = loc_auth(argv[5], argv[6], argv[4] )))
1597 {
1598 /* Return a AC A */
1599 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1600 }
1601 else
1602 {
1603 /* Return a AC D */
1604 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1605 }
1606 return 1;
1607 }
1608 else if(!strcmp(argv[2],"R"))
1609 call_account_func(user, argv[3]);
1610 else
1611 call_account_func(user, argv[2]); /* For backward compatability */
1612 return 1;
1613 }
1614
1615 static CMD_FUNC(cmd_fakehost)
1616 {
1617 struct userNode *user;
1618
1619 if ((argc < 3) || !origin || !GetServerH(origin))
1620 return 0;
1621 if (!(user = GetUserN(argv[1])))
1622 return 1;
1623 assign_fakehost(user, argv[2], 0);
1624 return 1;
1625 }
1626
1627 static struct {
1628 char *name;
1629 unsigned int priv;
1630 } privtab[] = {
1631 #define P(priv) { #priv, PRIV_ ## priv }
1632 P(CHAN_LIMIT), P(MODE_LCHAN), P(WALK_LCHAN), P(DEOP_LCHAN),
1633 P(SHOW_INVIS), P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
1634 P(LOCAL_KILL), P(REHASH), P(RESTART), P(DIE),
1635 P(GLINE), P(LOCAL_GLINE), P(JUPE), P(LOCAL_JUPE),
1636 P(OPMODE), P(LOCAL_OPMODE), P(SET), P(WHOX),
1637 P(BADCHAN), P(LOCAL_BADCHAN), P(SEE_CHAN), P(PROPAGATE),
1638 P(DISPLAY), P(SEE_OPERS), P(WIDE_GLINE), P(FORCE_OPMODE),
1639 P(FORCE_LOCAL_OPMODE), P(REMOTEREHASH), P(CHECK), P(SEE_SECRET_CHAN),
1640 P(SHUN), P(LOCAL_SHUN), P(WIDE_SHUN), P(ZLINE),
1641 P(LOCAL_ZLINE), P(WIDE_ZLINE), P(LIST_CHAN), P(WHOIS_NOTICE),
1642 P(HIDE_IDLE), P(XTRAOP), P(HIDE_CHANNELS), P(DISPLAY_MODE),
1643 P(FREEFORM), P(REMOVE), P(SPAMFILTER),
1644 #undef P
1645 { 0, 0 }
1646 };
1647
1648 char *client_report_privs(struct userNode *client)
1649 {
1650 int i;
1651
1652 privbuf[0] = '\0';
1653 for (i = 0; privtab[i].name; i++) {
1654 if (HasPriv(client, privtab[i].priv)) {
1655 strcat(privbuf, privtab[i].name);
1656 strcat(privbuf, " ");
1657 }
1658 }
1659
1660 privbuf[strlen(privbuf)] = 0;
1661
1662 return privbuf;
1663 }
1664
1665 int clear_privs(struct userNode *who) {
1666 int i = 0;
1667 assert(0 != who);
1668
1669 for (i = 0; privtab[i].name; i++)
1670 RevokePriv(who, privtab[i].priv);
1671
1672 return 0;
1673 }
1674
1675 int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1676 int i = 0;
1677 assert(0 != priv);
1678 assert(0 != who);
1679
1680 for (i = 0; privtab[i].name; i++) {
1681 if (0 == strcmp(privtab[i].name, priv)) {
1682 if (what == PRIV_ADD)
1683 GrantPriv(who, privtab[i].priv);
1684 else if (what == PRIV_DEL) {
1685 RevokePriv(who, privtab[i].priv);
1686 }
1687 }
1688 }
1689 return 0;
1690 }
1691
1692 int check_priv(char *priv)
1693 {
1694 int i;
1695
1696 for (i = 0; privtab[i].name; i++) {
1697 if (0 == strcmp(privtab[i].name, priv)) {
1698 return 1;
1699 }
1700 }
1701 return 0;
1702 }
1703
1704 void
1705 irc_privs(struct userNode *target, char *flag, int add)
1706 {
1707 client_modify_priv_by_name(target, flag, add);
1708 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1709 }
1710
1711 void
1712 irc_raw_privs(struct userNode *target, const char *privs)
1713 {
1714 putsock("%s " P10_PRIVS " %s %s", self->numeric, target->numeric, privs);
1715 }
1716
1717 static CMD_FUNC(cmd_privs)
1718 {
1719 char *tstr = NULL;
1720 char buf[512] = "";
1721 char *p = 0;
1722 char *tmp;
1723
1724 int what = PRIV_ADD;
1725 int type = 0;
1726
1727 unsigned int i;
1728
1729 struct server *sender;
1730 struct userNode *user;
1731
1732
1733 tstr = conf_get_data("server/type", RECDB_QSTRING);
1734 if(tstr)
1735 type = atoi(tstr);
1736
1737
1738 if (!(sender = GetServerH(origin))) { /* from oper */
1739 return 1; /* ignore as no services have privs set */
1740 } else { /* from server */
1741 if (type < 5)
1742 return 1; /* silently ignore */
1743
1744 user = argc > 1 ? GetUserN(argv[1]) : NULL;
1745
1746 if (!user)
1747 return 0;
1748
1749 for (i=1; i<argc; i++) {
1750 strcat(buf, argv[i]);
1751 strcat(buf, " ");
1752 }
1753
1754 for (i = 2; i < argc; i++) {
1755 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1756 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1757 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1758 tmp = x3_strtok(&p, NULL, ",")) {
1759 if (!strcmp(tmp, "PRIV_NONE")) {
1760 clear_privs(user);
1761 break;
1762 } else
1763 client_modify_priv_by_name(user, tmp, what);
1764 }
1765 }
1766 }
1767 return 1;
1768 }
1769
1770 static CMD_FUNC(cmd_burst)
1771 {
1772 extern int rel_age;
1773 char modes[MAXLEN], *members = "";
1774 static char exemptlist[MAXLEN], banlist[MAXLEN];
1775 unsigned int next = 3, res = 1;
1776 int ctype = 0, echeck = 0, bcheck = 0;
1777 struct chanData *cData;
1778 struct chanNode *cNode;
1779 struct userNode *un;
1780 struct modeNode *mNode;
1781 long mode;
1782 int oplevel = -1;
1783 char *user, *end, sep;
1784 time_t in_timestamp;
1785 char* parm = NULL;
1786
1787 if (argc < 3)
1788 return 0;
1789 modes[0] = 0;
1790
1791 exemptlist[0] = 0;
1792 banlist[0] = 0;
1793
1794 while (next < argc) {
1795 switch (argv[next][0]) {
1796 case '+': {
1797 const char *pos;
1798 int n_modes;
1799 for (pos=argv[next], n_modes = 1; *pos; pos++)
1800 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1801 || (*pos == 'U'))
1802 n_modes++;
1803 if (next + n_modes > argc)
1804 n_modes = argc - next;
1805 unsplit_string(argv+next, n_modes, modes);
1806 next += n_modes;
1807 break;
1808 }
1809 case '%': {
1810 for(parm = mysep(&argv[next], " "); /* parm = first param */
1811 parm; /* While param is not null */
1812 parm = mysep(&argv[next], " ") /* parm = next param */
1813 )
1814 {
1815 switch (parm[0]) {
1816 case '%': {
1817 ctype = 1;
1818 break;
1819 }
1820 case '~': {
1821 ctype = 2;
1822 break;
1823 }
1824 default: {
1825 break;
1826 }
1827 }
1828 if (ctype == 1) {
1829 if (bcheck == 0) {
1830 /* strip % char off start of very first ban */
1831 if (strlen(parm) > 1) {
1832 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1833 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1834 }
1835 bcheck = 1;
1836 } else {
1837 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1838 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1839 }
1840 } else if (ctype == 2) {
1841 if (echeck == 0) {
1842 echeck = 1;
1843 } else {
1844 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1845 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1846 }
1847 }
1848 }
1849 next++;
1850 break;
1851 }
1852 default: members = argv[next++]; break;
1853 }
1854 }
1855
1856 in_timestamp = atoi(argv[2]);
1857 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1858 cNode->timestamp = in_timestamp;
1859 dict_remove(unbursted_channels, cNode->name);
1860 irc_burst(cNode);
1861 }
1862 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1863 cData = cNode->channel_info;
1864
1865 if (!cData) {
1866 if (cNode->modes & MODE_REGISTERED) {
1867 irc_join(opserv, cNode);
1868 irc_mode(opserv, cNode, "-z");
1869 irc_part(opserv, cNode, "");
1870 }
1871 }
1872
1873 /* Burst channel members in now. */
1874 for (user = members, sep = *members, mode = 0; sep; user = end) {
1875 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1876 sep = *end++; end[-1] = 0;
1877 if (sep == ':') {
1878 mode = 0;
1879 while ((sep = *end++)) {
1880 if (sep == 'o') {
1881 mode |= MODE_CHANOP;
1882 oplevel = -1;
1883 } else if (sep == 'h') {
1884 mode |= MODE_HALFOP;
1885 oplevel = -1;
1886 } else if (sep == 'v') {
1887 mode |= MODE_VOICE;
1888 oplevel = -1;
1889 } else if (isdigit(sep)) {
1890 mode |= MODE_CHANOP;
1891 if (oplevel >= 0)
1892 oplevel += parse_oplevel(end);
1893 else
1894 oplevel = parse_oplevel(end);
1895 while (isdigit(*end)) end++;
1896 } else
1897 break;
1898 }
1899 if (rel_age < 0)
1900 mode = 0;
1901 }
1902 if (!(un = GetUserN(user))) {
1903 res = 0;
1904 continue;
1905 }
1906 if ((mNode = AddChannelUser(un, cNode))) {
1907 mNode->modes = mode;
1908 mNode->oplevel = oplevel;
1909 }
1910 }
1911
1912 return res;
1913 }
1914
1915 /* TODO:
1916 * This is a stub that doesn't actually do anything. It should be completed
1917 * so that bans on *!*@markname.* match users as it does in nefarious
1918 */
1919 static CMD_FUNC(cmd_mark)
1920 {
1921 struct userNode *target;
1922 /*
1923 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
1924 */
1925
1926 if(argc < 4)
1927 return 0;
1928 if(!strcasecmp(argv[2], "DNSBL")) {
1929 /* DNSBL <modes> */
1930 return 1;
1931 }
1932 else if(!strcasecmp(argv[2], "DNSBL_DATA")) {
1933 /* DNSBL_DATA name */
1934 target = GetUserH(argv[1]);
1935 if(!target) {
1936 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
1937 return 0;
1938 }
1939 target->mark = strdup(argv[3]);
1940 return 1;
1941
1942 }
1943 else if(!strcasecmp(argv[2], "CVERSION")) {
1944 /* CTCP VERSION mark */
1945 target = GetUserH(argv[1]);
1946 if(!target) {
1947 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
1948 return 0;
1949 }
1950
1951 char *version = unsplit_string(argv + 3, argc - 3, NULL);
1952 if(!version)
1953 version = "";
1954
1955 target->version_reply = strdup(version);
1956
1957 if(match_ircglob(version, "WebTV;*"))
1958 target->no_notice = true; /* webbies cant see notices */
1959
1960 return 1;
1961 }
1962 else if(!strcasecmp(argv[2], "SSLCLIFP")) {
1963 /* SSL fingerprint mark */
1964 target = GetUserH(argv[1]);
1965 if(!target) {
1966 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose SSL fingerprint mark is changing.", argv[1]);
1967 return 0;
1968 }
1969
1970 char *sslfp = unsplit_string(argv + 3, argc - 3, NULL);
1971 if(!sslfp)
1972 sslfp = "";
1973
1974 target->sslfp = strdup(sslfp);
1975
1976 return 1;
1977 }
1978 /* unknown type of mark */
1979 return 1;
1980 }
1981
1982 static CMD_FUNC(cmd_mode)
1983 {
1984 struct chanNode *cn;
1985 struct userNode *un;
1986 char *sethost; // sethost - reed/apples
1987 int i; // sethost - reed/apples
1988
1989 if (argc < 3)
1990 return 0;
1991 if (!IsChannelName(argv[1])) {
1992 un = GetUserH(argv[1]);
1993 if (!un) {
1994 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1995 return 0;
1996 }
1997 // sethost - reed/apples
1998 if (argc == 3)
1999 mod_usermode(un, argv[2]);
2000 else {
2001 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
2002 i = 0;
2003 while((sethost[i++] = *argv[2]++));
2004 i--;
2005 sethost[i++] = ' ';
2006 while((sethost[i++] = *argv[3]++));
2007 mod_usermode(un, sethost); // sethost - reed/apples
2008 }
2009
2010 return 1;
2011 }
2012
2013 if (!(cn = GetChannel(argv[1]))) {
2014 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2015 return 0;
2016 }
2017 if ((un = GetUserH(origin))) {
2018 struct modeNode *mn;
2019 /* Update idle time for person setting the mode */
2020 if ((mn = GetUserMode(cn, un)))
2021 mn->idle_since = now;
2022 } else {
2023 /* If it came from a server, reset timestamp to re-sync. */
2024 cn->timestamp = atoi(argv[argc-1]);
2025 }
2026
2027 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
2028 const char *str;
2029 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2030 if (str) {
2031 char modes[MODELEN];
2032 struct userNode *opserv = GetUserH(str);
2033
2034 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
2035 irc_make_chanmode(cn, modes);
2036 irc_mode(opserv, cn, modes);
2037 irc_mode(opserv, cn, DefConChanModes);
2038 }
2039 return 1;
2040 }
2041
2042
2043 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
2044 }
2045
2046 static CMD_FUNC(cmd_opmode)
2047 {
2048 struct chanNode *cn;
2049 struct userNode *un;
2050
2051 if (argc < 3)
2052 return 0;
2053
2054 if (!(cn = GetChannel(argv[1]))) {
2055 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2056 return 0;
2057 }
2058 if (!(un = GetUserH(origin))) {
2059 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
2060 return 0;
2061 }
2062 if (!IsOper(un)) {
2063 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
2064 return 0;
2065 }
2066
2067 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
2068 }
2069
2070 static int clear_chanmode(struct chanNode *channel, const char *modes);
2071
2072 static CMD_FUNC(cmd_clearmode)
2073 {
2074 struct chanNode *cn;
2075 struct userNode *un;
2076
2077 if (argc < 3)
2078 return 0;
2079
2080 if (!(cn = GetChannel(argv[1]))) {
2081 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2082 return 0;
2083 }
2084 if (!(un = GetUserH(origin))) {
2085 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
2086 return 0;
2087 }
2088 if (!IsOper(un)) {
2089 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
2090 return 0;
2091 }
2092
2093 return clear_chanmode(cn, argv[2]);
2094 }
2095
2096 static CMD_FUNC(cmd_topic)
2097 {
2098 struct chanNode *cn;
2099 time_t chan_ts, topic_ts;
2100 struct userNode *user;
2101
2102 if (argc < 3)
2103 return 0;
2104 if (!(cn = GetChannel(argv[1]))) {
2105 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
2106 return 0;
2107 }
2108
2109
2110 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
2111 user = GetUserH(origin);
2112 chan_ts = atoi(argv[2]);
2113 topic_ts = atoi(argv[3]);
2114 } else if (argc >= 6) { /* Nefarious 0.5.0 */
2115 user = GetUserH(strtok(argv[2], "!"));
2116 chan_ts = atoi(argv[3]);
2117 topic_ts = atoi(argv[4]);
2118 } else { /* Regular IRCu (No Topic Bursting)*/
2119 user = GetUserH(origin);
2120 chan_ts = cn->timestamp;
2121 topic_ts = now;
2122 }
2123
2124 SetChannelTopic(cn, user, user, argv[argc-1], 0);
2125 cn->topic_time = topic_ts;
2126 return 1;
2127 }
2128
2129 static CMD_FUNC(cmd_num_topic)
2130 {
2131 struct chanNode *cn;
2132
2133 if (!argv[0])
2134 return 0; /* huh? */
2135 if (argv[2]) {
2136 cn = GetChannel(argv[2]);
2137 if (!cn) {
2138 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
2139 return 0;
2140 }
2141 } else
2142 return 0;
2143
2144 switch (atoi(argv[0])) {
2145 case 331:
2146 cn->topic_time = 0;
2147 break; /* no topic */
2148 case 332:
2149 if (argc < 4)
2150 return 0;
2151 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2152 break;
2153 case 333:
2154 if (argc < 5)
2155 return 0;
2156 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2157 cn->topic_time = atoi(argv[4]);
2158 break;
2159 default:
2160 return 0; /* should never happen */
2161 }
2162 return 1;
2163 }
2164
2165 static CMD_FUNC(cmd_num_gline)
2166 {
2167 if (argc < 7) {
2168 if (argc < 6)
2169 return 0;
2170 else
2171 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
2172 } else {
2173 if (!strcmp(argv[5], "+"))
2174 gline_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0, 0);
2175 }
2176 return 1;
2177 }
2178
2179 static CMD_FUNC(cmd_num_shun)
2180 {
2181 if (argc < 7) {
2182 if (argc < 6)
2183 return 0;
2184 else
2185 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
2186 } else {
2187 if (!strcmp(argv[5], "+"))
2188 shun_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0);
2189 }
2190 return 1;
2191 }
2192
2193 static CMD_FUNC(cmd_quit)
2194 {
2195 struct userNode *user;
2196 if (argc < 2)
2197 return 0;
2198 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2199 * call DelUser unless we have the user in our grasp. */
2200 if ((user = GetUserH(origin)))
2201 DelUser(user, NULL, false, argv[1]);
2202 return 1;
2203 }
2204
2205 static CMD_FUNC(cmd_kill)
2206 {
2207 struct userNode *user;
2208 if (argc < 2)
2209 return 0;
2210 user = GetUserN(argv[1]);
2211 if (!user) {
2212 /* If we get a KILL for a non-existent user, it could be a
2213 * Ghost response to a KILL we sent out earlier. So we only
2214 * whine if the target is local.
2215 */
2216 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
2217 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
2218 return 0;
2219 }
2220
2221 if (IsLocal(user) && IsService(user)) {
2222 /* TODO: rate limit this so silly things don't happen. */
2223 ReintroduceUser(user);
2224 return 1;
2225 }
2226
2227 DelUser(user, NULL, false, argv[2]);
2228 return 1;
2229 }
2230
2231 static CMD_FUNC(cmd_svspart)
2232 {
2233 struct userNode *user;
2234
2235 if (argc < 3)
2236 return 0;
2237 user = GetUserN(argv[1]);
2238 if (!user)
2239 return 0;
2240 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2241 return 1;
2242 }
2243
2244 static CMD_FUNC(cmd_silence)
2245 {
2246 struct userNode *user;
2247 char *mask;
2248 char *new_mask;
2249 unsigned int i;
2250
2251 if (argc < 2)
2252 return 0;
2253
2254 user = GetUserN(argv[1]);
2255
2256 /* Sanity, go nuts if this happens */
2257 if (!user)
2258 return 0;
2259
2260 /* We can safely ignore this if a user adding a silence is not
2261 * ignored. However this brings up a TODO. If a user logs in and
2262 * they have silences on the IRCd then we need to set them here
2263 * somehow
2264 */
2265 if (!user->handle_info)
2266 return 1;
2267
2268 mask = argv[2];
2269
2270 if (*mask == '-') {
2271 for (i=0; i<user->handle_info->ignores->used; i++) {
2272 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2273 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2274 }
2275 } else {
2276 for (i=0; i<user->handle_info->ignores->used; i++) {
2277 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2278 return 1; /* Already on the users NickServ ignore list, safely ignore */
2279 }
2280
2281 new_mask = strdup(mask+1);
2282 string_list_append(user->handle_info->ignores, new_mask);
2283 }
2284 return 1;
2285 }
2286
2287 static CMD_FUNC(cmd_kick)
2288 {
2289 if (argc < 3)
2290 return 0;
2291 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2292 return 1;
2293 }
2294
2295 static CMD_FUNC(cmd_squit)
2296 {
2297 struct server *server;
2298 char *uplink;
2299
2300 if (argc < 4)
2301 return 0;
2302 if (!(server = GetServerH(argv[1])))
2303 return 0;
2304
2305 if (server == self->uplink) {
2306 /* Force a reconnect to the currently selected server. */
2307 cManager.uplink->tries = 0;
2308 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2309 close_socket();
2310 return 1;
2311 }
2312
2313 uplink = strdup(server->uplink->name);
2314 DelServer(server, 0, argv[3]);
2315 /* if its a pingout and pingout connecting is enabled
2316 or its a read error and readerror connecting is enabled
2317 or were doing a "N" and i need to connect that server somewhere */
2318 routing_handle_squit(argv[1], uplink, argv[3]);
2319 free(uplink);
2320 return 1;
2321 }
2322
2323 static CMD_FUNC(cmd_privmsg)
2324 {
2325 struct privmsg_desc pd;
2326 if (argc < 3)
2327 return 0;
2328 pd.user = GetUserH(origin);
2329 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2330 return 1;
2331
2332 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2333 const char *str;
2334 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2335 if (str)
2336 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2337 return 1;
2338 }
2339
2340 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2341 return 1; /* Silently Ignore */
2342
2343 pd.is_notice = 0;
2344 pd.text = argv[argc - 1];
2345 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2346
2347 return 1;
2348 }
2349
2350 static CMD_FUNC(cmd_notice)
2351 {
2352 struct privmsg_desc pd;
2353 struct server *srv;
2354 int nuser = 0;
2355
2356 if (argc != 3)
2357 return 0;
2358
2359 pd.user = GetUserH(origin);
2360 if(!pd.user)
2361 nuser = 1;
2362 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2363 }
2364 else {
2365 pd.is_notice = 1;
2366 pd.text = argv[2];
2367 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2368 }
2369
2370 srv = GetServerH(origin);
2371 if(srv) {
2372 char *sargv[MAXNUMPARAMS];
2373 int sargc;
2374
2375 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2376
2377 if(!strcasecmp(sargv[0], "Connect:")) {
2378 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2379 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2380 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2381 }
2382 }
2383 else if(!strcasecmp(sargv[0], "Link")) {
2384 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2385 *
2386 * :Link with laptop.afternet.org cancelled: Connection refused
2387 */
2388 if(!strcasecmp(sargv[3], "cancelled:")) {
2389 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2390 }
2391 }
2392 }
2393 return 1;
2394 }
2395
2396 static CMD_FUNC(cmd_away)
2397 {
2398 struct userNode *uNode;
2399
2400 uNode = GetUserH(origin);
2401 if (!uNode)
2402 return 1;
2403 if (argc < 2)
2404 uNode->modes &= ~FLAGS_AWAY;
2405 else
2406 uNode->modes |= FLAGS_AWAY;
2407 return 1;
2408 }
2409
2410 static CMD_FUNC(cmd_gline)
2411 {
2412 if (argc < 3)
2413 return 0;
2414 if (argv[2][0] == '+') {
2415 if (argc < 5)
2416 return 0;
2417 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
2418 return 1;
2419 } else if (argv[2][0] == '-') {
2420 gline_remove(argv[2]+1, 0);
2421 return 1;
2422 } else
2423 return 0;
2424 }
2425
2426 static CMD_FUNC(cmd_sgline)
2427 {
2428 struct server *sender;
2429
2430 if (argc < 4)
2431 return 0;
2432
2433 if (!(sender = GetServerH(origin)))
2434 return 0;
2435
2436 gline_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1, 0);
2437 return 1;
2438 }
2439
2440 static CMD_FUNC(cmd_sshun)
2441 {
2442 struct server *sender;
2443
2444 if (argc < 4)
2445 return 0;
2446
2447 if (!(sender = GetServerH(origin)))
2448 return 0;
2449
2450 shun_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1);
2451 return 1;
2452 }
2453
2454 static CMD_FUNC(cmd_shun)
2455 {
2456 if (argc < 3)
2457 return 0;
2458 if (argv[2][0] == '+') {
2459 if (argc < 5)
2460 return 0;
2461 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
2462 return 1;
2463 } else if (argv[2][0] == '-') {
2464 shun_remove(argv[2]+1, 0);
2465 return 1;
2466 } else
2467 return 0;
2468 }
2469
2470 static CMD_FUNC(cmd_svsnick)
2471 {
2472 struct userNode *target, *dest;
2473 if ((argc < 4)
2474 || !(target = GetUserN(argv[1]))
2475 || !IsLocal(target)
2476 || (dest = GetUserH(argv[2])))
2477 return 0;
2478 NickChange(target, argv[2], 0);
2479 return 1;
2480 }
2481
2482 void
2483 free_user(struct userNode *user)
2484 {
2485 free(user->nick);
2486 free(user);
2487 }
2488
2489 static void
2490 parse_cleanup(UNUSED_ARG(void *extra))
2491 {
2492 unsigned int nn;
2493 free(of_list);
2494 free(of_list_extra);
2495 free(privmsg_funcs);
2496 num_privmsg_funcs = 0;
2497 free(notice_funcs);
2498 num_notice_funcs = 0;
2499 free(mcf_list);
2500 dict_delete(irc_func_dict);
2501 for (nn=0; nn<dead_users.used; nn++)
2502 free_user(dead_users.list[nn]);
2503 userList_clean(&dead_users);
2504 }
2505
2506 static void
2507 p10_conf_reload(void) {
2508 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
2509 his_servername = conf_get_data("server/his_servername", RECDB_QSTRING);
2510 his_servercomment = conf_get_data("server/his_servercomment", RECDB_QSTRING);
2511 }
2512
2513 static void
2514 remove_unbursted_channel(struct chanNode *cNode, UNUSED_ARG(void *extra)) {
2515 if (unbursted_channels)
2516 dict_remove(unbursted_channels, cNode->name);
2517 }
2518
2519 void
2520 init_parse(void)
2521 {
2522 const char *str, *desc;
2523 int numnick, usermask, max_users;
2524 char numer[COMBO_NUMERIC_LEN+1];
2525
2526 /* read config items */
2527 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2528 extended_accounts = str ? enabled_string(str) : 1;
2529 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2530 ping_freq = str ? ParseInterval(str) : 120;
2531 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2532 ping_timeout = str ? ParseInterval(str) : 30;
2533 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2534 force_n2k = str ? enabled_string(str) : 1;
2535 str = conf_get_data("server/numeric", RECDB_QSTRING);
2536 if (!str) {
2537 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2538 exit(1);
2539 }
2540 numnick = atoi(str);
2541 str = conf_get_data("server/max_users", RECDB_QSTRING);
2542 max_users = str ? atoi(str) : 4096;
2543 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2544 usermask--;
2545 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2546 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2547 else
2548 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2549
2550 str = conf_get_data("server/hostname", RECDB_QSTRING);
2551 desc = conf_get_data("server/description", RECDB_QSTRING);
2552 if (!str || !desc) {
2553 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2554 exit(1);
2555 }
2556 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2557 conf_register_reload(p10_conf_reload);
2558
2559 irc_func_dict = dict_new();
2560 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2561 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2562 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2563 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2564 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2565 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2566 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2567 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2568 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2569 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
2570 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2571 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
2572 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2573 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2574 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2575 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2576 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2577 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2578 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2579 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2580 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2581 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2582 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2583 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2584 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2585 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2586 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2587 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2588 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2589 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2590 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2591 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2592 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2593 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2594 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2595 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2596 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2597 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2598 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2599 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
2600 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2601 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
2602 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2603 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2604 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2605 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2606 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2607 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2608 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2609 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2610 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2611 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
2612 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2613 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
2614 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2615 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
2616 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2617 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
2618 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2619 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
2620 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2621 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2622 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2623 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
2624 dict_insert(irc_func_dict, CMD_SGLINE, cmd_sgline);
2625 dict_insert(irc_func_dict, TOK_SGLINE, cmd_sgline);
2626 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2627 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
2628 dict_insert(irc_func_dict, CMD_SSHUN, cmd_sshun);
2629 dict_insert(irc_func_dict, TOK_SSHUN, cmd_sshun);
2630 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2631 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2632 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2633 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2634 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2635 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2636 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2637 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
2638 dict_insert(irc_func_dict, CMD_SMO, cmd_dummy);
2639 dict_insert(irc_func_dict, TOK_SMO, cmd_dummy);
2640 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2641 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
2642
2643 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2644 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
2645 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2646 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2647
2648 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2649 * Apparently to obliterate channels from any servers that think they
2650 * exist?
2651 */
2652 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2653 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2654 /* Ignore invites */
2655 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2656 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2657 /* DESYNCH is just informational, so ignore it */
2658 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2659 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2660 /* Ignore channel operator notices. */
2661 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2662 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2663 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2664 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
2665 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2666 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2667 /* Ignore opers being silly. */
2668 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2669 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
2670 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2671 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2672 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
2673 /* Ignore dnsbl exemptions */
2674 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
2675 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2676 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
2677 /* ignore ALIST for now */
2678 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2679 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
2680 /* ignore SPAMFILTER */
2681 dict_insert(irc_func_dict, TOK_SPAMFILTER, cmd_dummy);
2682 dict_insert(irc_func_dict, CMD_SPAMFILTER, cmd_dummy);
2683 /* Ignore remote luser */
2684 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
2685 /* We have reliable clock! Always! Wraaa! */
2686 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2687 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
2688
2689 /* ignore /trace and /motd commands targetted at us */
2690 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2691 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
2692 dict_insert(irc_func_dict, TOK_UPING, cmd_dummy);
2693
2694 /* handle topics */
2695 dict_insert(irc_func_dict, "331", cmd_num_topic);
2696 dict_insert(irc_func_dict, "332", cmd_num_topic);
2697 dict_insert(irc_func_dict, "333", cmd_num_topic);
2698 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2699 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2700 /* ban list resetting */
2701 /* "stats g" responses */
2702 dict_insert(irc_func_dict, "230", cmd_dummy); /* ignore stats headers */
2703 dict_insert(irc_func_dict, "247", cmd_num_gline);
2704 dict_insert(irc_func_dict, "542", cmd_num_shun);
2705 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2706 /* other numeric responses we might get */
2707 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2708 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2709 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
2710 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
2711 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2712 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2713 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2714 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2715 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2716
2717 num_privmsg_funcs = 16;
2718 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2719 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2720
2721 num_notice_funcs = 16;
2722 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2723 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2724
2725 userList_init(&dead_users);
2726 reg_del_channel_func(remove_unbursted_channel, NULL);
2727 reg_exit_func(parse_cleanup, NULL);
2728 // reg_notice_func(opserv, check_ctcp);
2729 }
2730
2731 int
2732 parse_line(char *line, int recursive)
2733 {
2734 char *argv[MAXNUMPARAMS], *origin;
2735 int argc, cmd, res=0;
2736 cmd_func_t *func;
2737
2738 argc = split_line(line, true, MAXNUMPARAMS, argv);
2739 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2740 if (argc > cmd) {
2741 if (cmd) {
2742 if (argv[0][0] == ':') {
2743 origin = argv[0]+1;
2744 } else if (!argv[0][1] || !argv[0][2]) {
2745 struct server *sNode = GetServerN(argv[0]);
2746 origin = sNode ? sNode->name : 0;
2747 } else {
2748 struct userNode *uNode = GetUserN(argv[0]);
2749 origin = uNode ? uNode->nick : 0;
2750 }
2751 } else
2752 origin = 0;
2753 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2754 res = func(origin, argc-cmd, argv+cmd);
2755 }
2756 if (!res) {
2757 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2758 } else if (!recursive) {
2759 unsigned int i;
2760 for (i=0; i<dead_users.used; i++)
2761 free_user(dead_users.list[i]);
2762 dead_users.used = 0;
2763 }
2764 return res;
2765 }
2766
2767 static void
2768 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2769 {
2770 char *j, old;
2771
2772 do {
2773 j = target_list;
2774 while (*j != 0 && *j != ',')
2775 j++;
2776 old = *j;
2777 *j = 0;
2778
2779 if (IsChannelName(target_list)
2780 || (target_list[0] == '0' && target_list[1] == '\0')) {
2781 struct chanNode *chan = GetChannel(target_list);
2782
2783 if (chan) {
2784 if (cf)
2785 cf(chan, data);
2786 } else {
2787 if (nc)
2788 nc(target_list, data);
2789 }
2790 } else {
2791 struct userNode *user;
2792 struct privmsg_desc *pd = data;
2793
2794 pd->is_qualified = 0;
2795 if (*target_list == '@') {
2796 user = NULL;
2797 } else if (strchr(target_list, '@')) {
2798 struct server *server;
2799
2800 pd->is_qualified = 1;
2801 user = GetUserH(strtok(target_list, "@"));
2802 server = GetServerH(strtok(NULL, "@"));
2803
2804 if (user && (user->uplink != server)) {
2805 /* Don't attempt to index into any arrays
2806 using a user's numeric on another server. */
2807 user = NULL;
2808 }
2809 } else {
2810 user = GetUserN(target_list);
2811 }
2812
2813 if (user) {
2814 if (uf)
2815 uf(user, data);
2816 } else {
2817 if (nu)
2818 nu(target_list, data);
2819 }
2820 }
2821 target_list = j+1;
2822 } while (old == ',');
2823 }
2824
2825 static int
2826 get_local_numeric(void)
2827 {
2828 static unsigned int next_numeric = 0;
2829 if (self->clients > self->num_mask)
2830 return -1;
2831 while (self->users[next_numeric])
2832 if (++next_numeric > self->num_mask)
2833 next_numeric = 0;
2834 return next_numeric;
2835 }
2836
2837 static void
2838 make_numeric(struct server *svr, int local_num, char *outbuf)
2839 {
2840 int slen, llen;
2841
2842 if (force_n2k || svr->numeric[1]) {
2843 slen = 2;
2844 llen = 3;
2845 } else {
2846 slen = 1;
2847 llen = (local_num < 64*64) ? 2 : 3;
2848 }
2849 strncpy(outbuf, svr->numeric, slen);
2850 inttobase64(outbuf+slen, local_num, llen);
2851 outbuf[slen+llen] = 0;
2852 }
2853
2854 struct server *
2855 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link_time, const char *numeric, const char *description)
2856 {
2857 struct server* sNode;
2858 int slen, mlen;
2859
2860 if ((sNode = GetServerN(numeric))) {
2861 /* This means we're trying to re-add an existant server.
2862 * To be safe, we should forget the previous incarnation.
2863 * (And all its linked servers.)
2864 *
2865 * It usually only happens in replays when the original
2866 * had a ping timeout and the replay didn't (because
2867 * replaying a ping timeout invariably gets things wrong).
2868 */
2869 DelServer(sNode, 0, NULL);
2870 }
2871
2872 switch (strlen(numeric)) {
2873 case 5: slen = 2; mlen = 3; break;
2874 case 4: slen = 1; mlen = 3; break;
2875 case 3: slen = 1; mlen = 2; break;
2876 default:
2877 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2878 return NULL;
2879 }
2880
2881 sNode = calloc(1, sizeof(*sNode));
2882 sNode->uplink = uplink;
2883 safestrncpy(sNode->name, name, sizeof(sNode->name));
2884 sNode->num_mask = base64toint(numeric+slen, mlen);
2885 sNode->hops = hops;
2886 sNode->boot = boot;
2887 sNode->link_time = link_time;
2888 strncpy(sNode->numeric, numeric, slen);
2889 safestrncpy(sNode->description, description, sizeof(sNode->description));
2890 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
2891 serverList_init(&sNode->children);
2892 if (sNode->uplink) {
2893 /* uplink may be NULL if we're just building ourself */
2894 serverList_append(&sNode->uplink->children, sNode);
2895 }
2896 servers_num[base64toint(numeric, slen)] = sNode;
2897 dict_insert(servers, sNode->name, sNode);
2898 return sNode;
2899 }
2900
2901 void DelServer(struct server* serv, int announce, const char *message)
2902 {
2903 unsigned int i;
2904
2905 /* If we receive an ERROR command before the SERVER
2906 * command a NULL server can be passed */
2907 if (!serv)
2908 return;
2909
2910 /* Hrm, what's the right way to SQUIT some other server?
2911 * (This code is only to handle killing juped servers.) */
2912 if (announce && (serv->uplink == self) && (serv != self->uplink))
2913 irc_squit(serv, message, NULL);
2914
2915 /* must recursively remove servers linked to this one first */
2916 for (i=serv->children.used;i>0;)
2917 if (serv->children.list[--i] != self)
2918 DelServer(serv->children.list[i], false, NULL);
2919
2920 /* clean up server's user hash tables */
2921 for (i=0;i<=serv->num_mask;i++)
2922 if (serv->users[i])
2923 DelUser(serv->users[i], NULL, false, "server delinked");
2924
2925 /* delete server */
2926 if (serv->uplink)
2927 serverList_remove(&serv->uplink->children, serv);
2928 if (serv == self->uplink)
2929 self->uplink = NULL;
2930 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
2931 dict_remove(servers, serv->name);
2932 serverList_clean(&serv->children);
2933 free(serv->users);
2934 free(serv);
2935 }
2936
2937 struct userNode *
2938 AddLocalUser(const char *nick, const char *ident, const char *hostname, const char *desc, const char *modes)
2939 {
2940 char numeric[COMBO_NUMERIC_LEN+1];
2941 int local_num = get_local_numeric();
2942 time_t timestamp = now;
2943 struct userNode *old_user = GetUserH(nick);
2944
2945 if (!modes)
2946 modes = "+oik";
2947
2948 if (old_user) {
2949 if (IsLocal(old_user))
2950 return old_user;
2951 timestamp = old_user->timestamp - 1;
2952 }
2953 if (local_num == -1) {
2954 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
2955 return 0;
2956 }
2957 if (!hostname)
2958 hostname = self->name;
2959 make_numeric(self, local_num, numeric);
2960 return AddUser(self, nick, ident, hostname, modes, numeric, desc, now, "AAAAAA");
2961 }
2962
2963 struct userNode *
2964 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
2965 {
2966 char numeric[COMBO_NUMERIC_LEN+1];
2967 int local_num = get_local_numeric();
2968 time_t timestamp = now;
2969 struct userNode *old_user = GetUserH(nick);
2970
2971 if (old_user) {
2972 if (IsLocal(old_user))
2973 return old_user;
2974 timestamp = old_user->timestamp - 1;
2975 }
2976 if (local_num == -1) {
2977 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
2978 return 0;
2979 }
2980 make_numeric(self, local_num, numeric);
2981 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2982 }
2983
2984 int
2985 is_valid_nick(const char *nick) {
2986 unsigned int ii;
2987 /* IRC has some of The Most Fucked-Up ideas about character sets
2988 * in the world.. */
2989 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2990 return 0;
2991 for (ii = 0; nick[ii]; ++ii)
2992 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
2993 return 0;
2994 if (strlen(nick) > nicklen)
2995 return 0;
2996 return 1;
2997 }
2998
2999 static struct userNode*
3000 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)
3001 {
3002 struct userNode *oldUser, *uNode;
3003 unsigned int ignore_user, dummy;
3004 char *tstr;
3005 int type;
3006
3007 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
3008 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", (void*)uplink, nick, numeric);
3009 return NULL;
3010 }
3011
3012 if (!uplink) {
3013 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", (void*)uplink, nick, numeric);
3014 return NULL;
3015 }
3016
3017 if (uplink != GetServerN(numeric)) {
3018 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", (void*)uplink, nick, numeric, uplink->name);
3019 return NULL;
3020 }
3021
3022 dummy = modes && modes[0] == '*';
3023 if (dummy) {
3024 ++modes;
3025 } else if (!is_valid_nick(nick)) {
3026 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", (void*)uplink, nick);
3027 return NULL;
3028 }
3029
3030 ignore_user = 0;
3031 if ((oldUser = GetUserH(nick))) {
3032 if (IsLocal(oldUser)
3033 && (IsService(oldUser) || IsPersistent(oldUser))) {
3034 /* The service should collide the new user off - but not
3035 * if the new user is coming in during a burst. (During a
3036 * burst, the bursting server will kill either our user --
3037 * triggering a ReintroduceUser() -- or its own.)
3038 */
3039 oldUser->timestamp = timestamp - 1;
3040 ignore_user = 1;
3041 if (!uplink->burst)
3042 irc_user(oldUser);
3043 } else if (oldUser->timestamp > timestamp) {
3044 /* "Old" user is really newer; remove them */
3045 DelUser(oldUser, 0, 1, "Overruled by older nick");
3046 } else {
3047 /* User being added is too new; do not add them to
3048 * clients, but do add them to the server's list, since it
3049 * will send a KILL and QUIT soon. */
3050 ignore_user = 1;
3051 }
3052 }
3053
3054 /* create new usernode and set all values */
3055 uNode = calloc(1, sizeof(*uNode));
3056 uNode->nick = strdup(nick);
3057 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
3058 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
3059 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
3060 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
3061 irc_p10_pton(&uNode->ip, realip);
3062
3063 tstr = conf_get_data("server/type", RECDB_QSTRING);
3064 type = atoi(tstr);
3065 if (type > 6) {
3066 if (irc_in_addr_is_ipv4(uNode->ip)) {
3067 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
3068 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3069 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
3070 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3071 }
3072 }
3073
3074 if (!uNode->crypthost && uNode->cryptip)
3075 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
3076 uNode->idle_since = timestamp;
3077 uNode->timestamp = timestamp;
3078 modeList_init(&uNode->channels);
3079 uNode->uplink = uplink;
3080 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
3081 uNode->uplink->max_clients = uNode->uplink->clients;
3082 }
3083 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
3084 uNode->uplink->users[uNode->num_local] = uNode;
3085 mod_usermode(uNode, modes);
3086 if (dummy)
3087 uNode->modes |= FLAGS_DUMMY;
3088
3089 set_geoip_info(uNode);
3090
3091 if (ignore_user)
3092 return uNode;
3093
3094 dict_insert(clients, uNode->nick, uNode);
3095 if (dict_size(clients) > max_clients) {
3096 max_clients = dict_size(clients);
3097 max_clients_time = now;
3098 }
3099 if (IsLocal(uNode))
3100 irc_user(uNode);
3101 call_new_user_funcs(uNode);
3102
3103 if ((uNode->loc == 1) && (uNode->handle_info))
3104 send_func_list(uNode);
3105
3106 return uNode;
3107 }
3108
3109 /* removes user from it's server's hash table and nick hash table */
3110 void
3111 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
3112 {
3113
3114 verify(user);
3115
3116 /* mark them as dead, in case anybody cares */
3117 user->dead = 1;
3118
3119 /* remove pending adduser commands */
3120 wipe_adduser_pending(NULL, user);
3121
3122 /* remove user from all channels */
3123 while (user->channels.used > 0)
3124 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, NULL, 0);
3125
3126 /* Call these in reverse order so ChanServ can update presence
3127 information before NickServ nukes the handle_info. */
3128 call_del_user_funcs(user, killer, why);
3129
3130 user->uplink->clients--;
3131 user->uplink->users[user->num_local] = NULL;
3132 if (IsOper(user))
3133 userList_remove(&curr_opers, user);
3134 /* remove from global dictionary, but not if after a collide */
3135 if (user == dict_find(clients, user->nick, NULL))
3136 dict_remove(clients, user->nick);
3137
3138 if (IsInvisible(user))
3139 invis_clients--;
3140
3141 if (announce) {
3142 if (IsLocal(user))
3143 irc_quit(user, why);
3144 else
3145 irc_kill(killer, user, why);
3146 }
3147
3148 if (IsLocal(user)) {
3149 unsigned int num = user->num_local;
3150 if (num < num_privmsg_funcs)
3151 privmsg_funcs[num] = NULL;
3152 if (num < num_notice_funcs)
3153 notice_funcs[num] = NULL;
3154 }
3155
3156 modeList_clean(&user->channels);
3157
3158 /* Clean up version data */
3159 if(user->version_reply) {
3160 free(user->version_reply);
3161 user->version_reply = NULL;
3162 }
3163
3164 /* Clean up SSL fingerprint data */
3165 if(user->sslfp) {
3166 free(user->sslfp);
3167 user->sslfp = NULL;
3168 }
3169
3170 /* clean up mark */
3171 if(user->mark) {
3172 free(user->mark);
3173 user->mark = NULL;
3174 }
3175
3176 /* clean up geoip data if any */
3177 if(user->country_code) free(user->country_code);
3178 if(user->city) free(user->city);
3179 if(user->region) free(user->region);
3180 if(user->postal_code) free(user->postal_code);
3181
3182 /* We don't free them, in case we try to privmsg them or something
3183 * (like when a stupid oper kills themself). We just put them onto
3184 * a list of clients that get freed after processing each line.
3185 */
3186 if (dead_users.size)
3187 userList_append(&dead_users, user);
3188 else
3189 free_user(user);
3190 }
3191
3192 static void call_oper_funcs(struct userNode *user);
3193
3194 void mod_usermode(struct userNode *user, const char *mode_change) {
3195 int add = 1;
3196 const char *word = mode_change;
3197
3198 if (!user || !mode_change)
3199 return;
3200
3201 call_user_mode_funcs(user, mode_change);
3202
3203 while (*word != ' ' && *word) word++;
3204 while (*word == ' ') word++;
3205 while (1) {
3206 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3207 switch (*mode_change++) {
3208 case 0: case ' ': return;
3209 case '+': add = 1; break;
3210 case '-': add = 0; break;
3211 case 'o':
3212 do_user_mode(FLAGS_OPER);
3213 if (!add) {
3214 userList_remove(&curr_opers, user);
3215 } else if (!userList_contains(&curr_opers, user)) {
3216 userList_append(&curr_opers, user);
3217 call_oper_funcs(user);
3218 }
3219 break;
3220 case 'O': do_user_mode(FLAGS_LOCOP); break;
3221 case 'i': do_user_mode(FLAGS_INVISIBLE);
3222 if (add)
3223 invis_clients++;
3224 else
3225 invis_clients--;
3226 break;
3227 case 'w': do_user_mode(FLAGS_WALLOP); break;
3228 case 'd': do_user_mode(FLAGS_DEAF); break;
3229 case 'k': do_user_mode(FLAGS_SERVICE); break;
3230 case 'g': do_user_mode(FLAGS_GLOBAL); break;
3231 case 'B': do_user_mode(FLAGS_BOT); break;
3232 case 'n': do_user_mode(FLAGS_HIDECHANS); break;
3233 case 'I': do_user_mode(FLAGS_HIDEIDLE); break;
3234 case 'X': do_user_mode(FLAGS_XTRAOP); break;
3235 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3236 if (*word) {
3237 char cloakhost[MAXLEN];
3238 unsigned int ii;
3239 for (ii=0; (*word != ' ') && (*word != '\0'); )
3240 cloakhost[ii++] = *word++;
3241 cloakhost[ii] = 0;
3242 while (*word == ' ')
3243 word++;
3244 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3245 }
3246 break;
3247 case 'c': do_user_mode(FLAGS_CLOAKIP);
3248 if (*word) {
3249 char cloakip[MAXLEN];
3250 unsigned int ii;
3251 for (ii=0; (*word != ' ') && (*word != '\0'); )
3252 cloakip[ii++] = *word++;
3253 cloakip[ii] = 0;
3254 while (*word == ' ')
3255 word++;
3256 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3257 }
3258 break;
3259 // sethost - reed/apples
3260 // case 'h': do_user_mode(FLAGS_HELPER); break;
3261 // I check if there's an 'h' in the first part, and if there,
3262 // then everything after the space becomes their new host.
3263 case 'h': do_user_mode(FLAGS_SETHOST);
3264 if (*word) {
3265 char sethost[MAXLEN];
3266 unsigned int ii;
3267 for (ii=0; (*word != ' ') && (*word != '\0'); )
3268 sethost[ii++] = *word++;
3269 sethost[ii] = 0;
3270 while (*word == ' ')
3271 word++;
3272 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3273 }
3274 break;
3275 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3276 case 'r':
3277 if (*word) {
3278 char tag[MAXLEN];
3279 unsigned int ii;
3280 for (ii=0; (*word != ' ') && (*word != '\0'); )
3281 tag[ii++] = *word++;
3282 tag[ii] = 0;
3283 while (*word == ' ')
3284 word++;
3285 call_account_func(user, tag);
3286 }
3287 break;
3288 case 'f':
3289 if (*word) {
3290 char host[MAXLEN];
3291 unsigned int ii;
3292 for (ii=0; (*word != ' ') && (*word != '\0'); )
3293 host[ii++] = *word++;
3294 host[ii] = 0;
3295 while (*word == ' ')
3296 word++;
3297 assign_fakehost(user, host, 0);
3298 }
3299 break;
3300 }
3301 #undef do_user_mode
3302 }
3303 }
3304
3305 static int
3306 keyncpy(char output[], char input[], size_t output_size)
3307 {
3308 size_t ii;
3309
3310 if (input[0] == ':')
3311 {
3312 output[0] = '\0';
3313 return 1;
3314 }
3315
3316 for (ii = 0; (ii + 1 < output_size) && (input[ii] != '\0'); ++ii)
3317 {
3318 output[ii] = input[ii];
3319 }
3320
3321 output[ii] = '\0';
3322 return 0;
3323 }
3324
3325 struct mod_chanmode *
3326 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
3327 {
3328 struct mod_chanmode *change;
3329 unsigned int ii, in_arg, ch_arg, add;
3330
3331 if (argc == 0)
3332 return NULL;
3333 if (!(change = mod_chanmode_alloc(argc - 1)))
3334 return NULL;
3335
3336 for (ii = ch_arg = 0, in_arg = add = 1;
3337 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3338 ++ii) {
3339 switch (modes[0][ii]) {
3340 case '+':
3341 add = 1;
3342 break;
3343 case '-':
3344 add = 0;
3345 break;
3346 #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)
3347 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3348 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3349 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3350 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3351 case 'm': do_chan_mode(MODE_MODERATED); break;
3352 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3353 case 'p': do_chan_mode(MODE_PRIVATE); break;
3354 case 'r': do_chan_mode(MODE_REGONLY); break;
3355 case 's': do_chan_mode(MODE_SECRET); break;
3356 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
3357 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3358 case 'M': do_chan_mode(MODE_MODUNREG); break;
3359 case 'N': do_chan_mode(MODE_NONOTICE); break;
3360 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3361 case 'T': do_chan_mode(MODE_NOAMSG); break;
3362 case 'O': do_chan_mode(MODE_OPERSONLY); break;
3363 case 'a': do_chan_mode(MODE_ADMINSONLY); break;
3364 case 'Z': do_chan_mode(MODE_SSLONLY); break;
3365 case 'L': do_chan_mode(MODE_HIDEMODE); break;
3366 case 'z':
3367 if (!(flags & MCP_REGISTERED)) {
3368 do_chan_mode(MODE_REGISTERED);
3369 } else {
3370 mod_chanmode_free(change);
3371 return NULL;
3372 }
3373 break;
3374 #undef do_chan_mode
3375 case 'l':
3376 if (add) {
3377 if (in_arg >= argc)
3378 goto error;
3379 change->modes_set |= MODE_LIMIT;
3380 change->new_limit = atoi(modes[in_arg++]);
3381 } else {
3382 change->modes_set &= ~MODE_LIMIT;
3383 change->modes_clear |= MODE_LIMIT;
3384 }
3385 break;
3386 case 'k':
3387 if (add) {
3388 if ((in_arg >= argc)
3389 || keyncpy(change->new_key, modes[in_arg++], sizeof(change->new_key)))
3390 goto error;
3391 change->modes_set |= MODE_KEY;
3392 } else {
3393 change->modes_clear |= MODE_KEY;
3394 if (!(flags & MCP_KEY_FREE)) {
3395 if (in_arg >= argc)
3396 goto error;
3397 in_arg++;
3398 }
3399 }
3400 break;
3401 case 'U':
3402 if (add)
3403 {
3404 if ((in_arg >= argc)
3405 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3406 goto error;
3407 change->modes_set |= MODE_UPASS;
3408 } else {
3409 change->modes_clear |= MODE_UPASS;
3410 if (!(flags & MCP_UPASS_FREE)) {
3411 if (in_arg >= argc)
3412 goto error;
3413 in_arg++;
3414 }
3415 }
3416 break;
3417 case 'A':
3418 if (add) {
3419 if ((in_arg >= argc)
3420 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3421 change->modes_set |= MODE_APASS;
3422 } else {
3423 change->modes_clear |= MODE_APASS;
3424 if (!(flags & MCP_APASS_FREE)) {
3425 if (in_arg >= argc)
3426 goto error;
3427 in_arg++;
3428 }
3429 }
3430 break;
3431 case 'b':
3432 if (!(flags & MCP_ALLOW_OVB))
3433 goto error;
3434 if (in_arg >= argc)
3435 goto error;
3436 change->args[ch_arg].mode = MODE_BAN;
3437 if (!add)
3438 change->args[ch_arg].mode |= MODE_REMOVE;
3439 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3440 break;
3441 case 'e':
3442 if (!(flags & MCP_ALLOW_OVB))
3443 goto error;
3444 if (in_arg >= argc)
3445 goto error;
3446 change->args[ch_arg].mode = MODE_EXEMPT;
3447 if (!add)
3448 change->args[ch_arg].mode |= MODE_REMOVE;
3449 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3450 break;
3451 case 'o': case 'h': case 'v':
3452 {
3453 struct userNode *victim;
3454 char *oplevel_str;
3455 int oplevel;
3456
3457 if (in_arg >= argc)
3458 goto error;
3459 oplevel_str = strchr(modes[in_arg], ':');
3460 if (oplevel_str)
3461 {
3462 /* XXYYY M #channel +o XXYYY:<oplevel> */
3463 *oplevel_str++ = '\0';
3464 oplevel = parse_oplevel(oplevel_str);
3465 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3466 oplevel = base_oplevel + 1;
3467 }
3468 else if (channel->modes & MODE_UPASS)
3469 oplevel = base_oplevel + 1;
3470 else
3471 oplevel = -1;
3472
3473 /* Check that oplevel is within bounds. */
3474 if (oplevel > MAXOPLEVEL)
3475 oplevel = MAXOPLEVEL;
3476
3477 if (!(flags & MCP_ALLOW_OVB))
3478 goto error;
3479 if (in_arg >= argc)
3480 goto error;
3481
3482 if (modes[0][ii] == 'o')
3483 change->args[ch_arg].mode = MODE_CHANOP;
3484 else if (modes[0][ii] == 'h')
3485 change->args[ch_arg].mode = MODE_HALFOP;
3486 else if (modes[0][ii] == 'v')
3487 change->args[ch_arg].mode = MODE_VOICE;
3488
3489 if (!add)
3490 change->args[ch_arg].mode |= MODE_REMOVE;
3491 if (flags & MCP_FROM_SERVER)
3492 victim = GetUserN(modes[in_arg++]);
3493 else
3494 victim = GetUserH(modes[in_arg++]);
3495 if (!victim)
3496 continue;
3497 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
3498 {
3499 /* Apply the oplevel change */
3500 change->args[ch_arg].u.member->oplevel = oplevel;
3501 ch_arg++;
3502 }
3503 break;
3504 }
3505 default:
3506 if (!(flags & MCP_FROM_SERVER))
3507 goto error;
3508 break;
3509 }
3510 }
3511 change->argc = ch_arg; /* in case any turned out to be ignored */
3512 if (change->modes_set & MODE_SECRET) {
3513 change->modes_set &= ~(MODE_PRIVATE);
3514 change->modes_clear |= MODE_PRIVATE;
3515 } else if (change->modes_set & MODE_PRIVATE) {
3516 change->modes_set &= ~(MODE_SECRET);
3517 change->modes_clear |= MODE_SECRET;
3518 }
3519 return change;
3520 error:
3521 mod_chanmode_free(change);
3522 return NULL;
3523 }
3524
3525 struct chanmode_buffer {
3526 char modes[MAXLEN];
3527 char args[MAXLEN];
3528 struct chanNode *channel;
3529 struct userNode *actor;
3530 unsigned int modes_used;
3531 unsigned int args_used;
3532 size_t chname_len;
3533 unsigned int is_add : 1;
3534 unsigned int is_chanop : 1;
3535 };
3536
3537 static void
3538 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3539 {
3540 size_t arg_len = strlen(arg);
3541 if (buf->modes_used > (MAXMODEPARAMS) ||
3542 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3543 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3544 buf->modes[buf->modes_used + buf->args_used] = '\0';
3545 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3546 buf->modes[0] = buf->is_add ? '+' : '-';
3547 buf->modes_used = 1;
3548 buf->args_used = 0;
3549 }
3550 buf->modes[buf->modes_used++] = ch;
3551 buf->args[buf->args_used++] = ' ';
3552 memcpy(buf->args + buf->args_used, arg, arg_len);
3553 buf->args_used += arg_len;
3554 }
3555
3556 void
3557 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3558 {
3559 struct chanmode_buffer chbuf;
3560 unsigned int arg;
3561 struct modeNode *mn;
3562 char int_buff[32], mode = '\0';
3563
3564 assert(change->argc <= change->alloc_argc);
3565 memset(&chbuf, 0, sizeof(chbuf));
3566 chbuf.channel = channel;
3567 chbuf.actor = who;
3568 chbuf.chname_len = strlen(channel->name);
3569
3570 mn = GetUserMode(channel, who);
3571 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3572 chbuf.is_chanop = 1;
3573
3574 /* First remove modes */
3575 chbuf.is_add = 0;
3576 if (change->modes_clear) {
3577 if (mode != '-')
3578 chbuf.modes[chbuf.modes_used++] = mode = '-';
3579 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3580 DO_MODE_CHAR(PRIVATE, 'p');
3581 DO_MODE_CHAR(SECRET, 's');
3582 DO_MODE_CHAR(MODERATED, 'm');
3583 DO_MODE_CHAR(TOPICLIMIT, 't');
3584 DO_MODE_CHAR(INVITEONLY, 'i');
3585 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3586 DO_MODE_CHAR(LIMIT, 'l');
3587 DO_MODE_CHAR(DELAYJOINS, 'D');
3588 DO_MODE_CHAR(REGONLY, 'r');
3589 DO_MODE_CHAR(NOCOLORS, 'c');
3590 DO_MODE_CHAR(NOCTCPS, 'C');
3591 DO_MODE_CHAR(STRIPCOLOR, 'S');
3592 DO_MODE_CHAR(MODUNREG, 'M');
3593 DO_MODE_CHAR(NONOTICE, 'N');
3594 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3595 DO_MODE_CHAR(NOAMSG, 'T');
3596 DO_MODE_CHAR(OPERSONLY, 'O');
3597 DO_MODE_CHAR(REGISTERED, 'z');
3598 DO_MODE_CHAR(SSLONLY, 'Z');
3599 DO_MODE_CHAR(HIDEMODE, 'L');
3600 #undef DO_MODE_CHAR
3601 if (change->modes_clear & channel->modes & MODE_KEY)
3602 mod_chanmode_append(&chbuf, 'k', channel->key);
3603 if (change->modes_clear & channel->modes & MODE_UPASS)
3604 mod_chanmode_append(&chbuf, 'U', channel->upass);
3605 if (change->modes_clear & channel->modes & MODE_APASS)
3606 mod_chanmode_append(&chbuf, 'A', channel->apass);
3607 }
3608 for (arg = 0; arg < change->argc; ++arg) {
3609 if (!(change->args[arg].mode & MODE_REMOVE))
3610 continue;
3611 if (mode != '-')
3612 chbuf.modes[chbuf.modes_used++] = mode = '-';
3613 switch (change->args[arg].mode & ~MODE_REMOVE) {
3614 case MODE_BAN:
3615 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3616 break;
3617 case MODE_EXEMPT:
3618 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3619 break;
3620 default:
3621 if (change->args[arg].mode & MODE_CHANOP)
3622 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3623 if (change->args[arg].mode & MODE_HALFOP)
3624 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3625 if (change->args[arg].mode & MODE_VOICE)
3626 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3627 break;
3628 }
3629 }
3630
3631 /* Then set them */
3632 chbuf.is_add = 1;
3633 if (change->modes_set) {
3634 if (mode != '+')
3635 chbuf.modes[chbuf.modes_used++] = mode = '+';
3636 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3637 DO_MODE_CHAR(PRIVATE, 'p');
3638 DO_MODE_CHAR(SECRET, 's');
3639 DO_MODE_CHAR(MODERATED, 'm');
3640 DO_MODE_CHAR(TOPICLIMIT, 't');
3641 DO_MODE_CHAR(INVITEONLY, 'i');
3642 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3643 DO_MODE_CHAR(DELAYJOINS, 'D');
3644 DO_MODE_CHAR(REGONLY, 'r');
3645 DO_MODE_CHAR(NOCOLORS, 'c');
3646 DO_MODE_CHAR(NOCTCPS, 'C');
3647 DO_MODE_CHAR(STRIPCOLOR, 'S');
3648 DO_MODE_CHAR(MODUNREG, 'M');
3649 DO_MODE_CHAR(NONOTICE, 'N');
3650 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3651 DO_MODE_CHAR(NOAMSG, 'T');
3652 DO_MODE_CHAR(OPERSONLY, 'O');
3653 DO_MODE_CHAR(REGISTERED, 'z');
3654 DO_MODE_CHAR(SSLONLY, 'Z');
3655 DO_MODE_CHAR(HIDEMODE, 'L');
3656 #undef DO_MODE_CHAR
3657 if(change->modes_set & MODE_KEY)
3658 mod_chanmode_append(&chbuf, 'k', change->new_key);
3659 if (change->modes_set & MODE_UPASS)
3660 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3661 if (change->modes_set & MODE_APASS)
3662 mod_chanmode_append(&chbuf, 'A', change->new_apass);
3663 if(change->modes_set & MODE_LIMIT) {
3664 sprintf(int_buff, "%d", change->new_limit);
3665 mod_chanmode_append(&chbuf, 'l', int_buff);
3666 }
3667 }
3668 for (arg = 0; arg < change->argc; ++arg) {
3669 if (change->args[arg].mode & MODE_REMOVE)
3670 continue;
3671 if (mode != '+')
3672 chbuf.modes[chbuf.modes_used++] = mode = '+';
3673 switch (change->args[arg].mode) {
3674 case MODE_BAN:
3675 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3676 break;
3677 case MODE_EXEMPT:
3678 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3679 break;
3680 default:
3681 if (change->args[arg].mode & MODE_CHANOP)
3682 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3683 if (change->args[arg].mode & MODE_HALFOP)
3684 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3685 if (change->args[arg].mode & MODE_VOICE)
3686 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3687 break;
3688 }
3689 }
3690
3691 /* Flush the buffer and apply changes locally */
3692 if (chbuf.modes_used > 0) {
3693 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3694 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3695 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3696 }
3697 mod_chanmode_apply(who, channel, change);
3698 }
3699
3700 char *
3701 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3702 {
3703 unsigned int used = 0;
3704 assert(change->argc <= change->alloc_argc);
3705 if (change->modes_clear) {
3706 outbuff[used++] = '-';
3707 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3708 DO_MODE_CHAR(PRIVATE, 'p');
3709 DO_MODE_CHAR(SECRET, 's');
3710 DO_MODE_CHAR(MODERATED, 'm');
3711 DO_MODE_CHAR(TOPICLIMIT, 't');
3712 DO_MODE_CHAR(INVITEONLY, 'i');
3713 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3714 DO_MODE_CHAR(LIMIT, 'l');
3715 DO_MODE_CHAR(KEY, 'k');
3716 DO_MODE_CHAR(UPASS, 'U');
3717 DO_MODE_CHAR(APASS, 'A');
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 }
3733 if (change->modes_set) {
3734 outbuff[used++] = '+';
3735 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3736 DO_MODE_CHAR(PRIVATE, 'p');
3737 DO_MODE_CHAR(SECRET, 's');
3738 DO_MODE_CHAR(MODERATED, 'm');
3739 DO_MODE_CHAR(TOPICLIMIT, 't');
3740 DO_MODE_CHAR(INVITEONLY, 'i');
3741 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3742 DO_MODE_CHAR(DELAYJOINS, 'D');
3743 DO_MODE_CHAR(REGONLY, 'r');
3744 DO_MODE_CHAR(NOCOLORS, 'c');
3745 DO_MODE_CHAR(NOCTCPS, 'C');
3746 DO_MODE_CHAR(STRIPCOLOR, 'S');
3747 DO_MODE_CHAR(MODUNREG, 'M');
3748 DO_MODE_CHAR(NONOTICE, 'N');
3749 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3750 DO_MODE_CHAR(NOAMSG, 'T');
3751 DO_MODE_CHAR(OPERSONLY, 'O');
3752 DO_MODE_CHAR(REGISTERED, 'z');
3753 DO_MODE_CHAR(SSLONLY, 'Z');
3754 DO_MODE_CHAR(HIDEMODE, 'L');
3755 #undef DO_MODE_CHAR
3756 switch (change->modes_set & (MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)) {
3757 /* Doing this implementation has been a pain in the arse, I hope I didn't forget a possible combination */
3758 case MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS:
3759 used += sprintf(outbuff+used, "lkAU %d %s %s %s", change->new_limit, change->new_key,change->new_apass, change->new_upass);
3760 break;
3761
3762 case MODE_KEY|MODE_LIMIT|MODE_APASS:
3763 used += sprintf(outbuff+used, "lkA %d %s %s", change->new_limit, change->new_key, change->new_apass);
3764 break;
3765 case MODE_KEY|MODE_LIMIT|MODE_UPASS:
3766 used += sprintf(outbuff+used, "lkU %d %s %s", change->new_limit, change->new_key, change->new_upass);
3767 break;
3768 case MODE_KEY|MODE_APASS|MODE_UPASS:
3769 used += sprintf(outbuff+used, "kAU %s %s %s", change->new_key, change->new_apass, change->new_upass);
3770 break;
3771
3772 case MODE_KEY|MODE_APASS:
3773 used += sprintf(outbuff+used, "kA %s %s", change->new_key, change->new_apass);
3774 break;
3775 case MODE_KEY|MODE_UPASS:
3776 used += sprintf(outbuff+used, "kU %s %s", change->new_key, change->new_upass);
3777 break;
3778 case MODE_KEY|MODE_LIMIT:
3779 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
3780 break;
3781
3782 case MODE_LIMIT|MODE_UPASS:
3783 used += sprintf(outbuff+used, "lU %d %s", change->new_limit, change->new_upass);
3784 break;
3785 case MODE_LIMIT|MODE_APASS:
3786 used += sprintf(outbuff+used, "lA %d %s", change->new_limit, change->new_apass);
3787 break;
3788 case MODE_APASS|MODE_UPASS:
3789 used += sprintf(outbuff+used, "AU %s %s", change->new_apass, change->new_upass);
3790
3791 case MODE_LIMIT|MODE_APASS|MODE_UPASS:
3792 used += sprintf(outbuff+used, "lAU %d %s %s", change->new_limit, change->new_apass, change->new_upass);
3793 break;
3794
3795 case MODE_APASS:
3796 used += sprintf(outbuff+used, "A %s", change->new_apass);
3797 break;
3798 case MODE_UPASS:
3799 used += sprintf(outbuff+used, "U %s", change->new_upass);
3800 break;
3801 case MODE_KEY:
3802 used += sprintf(outbuff+used, "k %s", change->new_key);
3803 break;
3804 case MODE_LIMIT:
3805 used += sprintf(outbuff+used, "l %d", change->new_limit);
3806 break;
3807 }
3808 }
3809 outbuff[used] = 0;
3810 return outbuff;
3811 }
3812
3813 static int
3814 clear_chanmode(struct chanNode *channel, const char *modes)
3815 {
3816 unsigned int cleared;
3817
3818 for (cleared = 0; *modes; modes++) {
3819 switch (*modes) {
3820 case 'o': cleared |= MODE_CHANOP; break;
3821 case 'h': cleared |= MODE_HALFOP; break;
3822 case 'v': cleared |= MODE_VOICE; break;
3823 case 'p': cleared |= MODE_PRIVATE; break;
3824 case 's': cleared |= MODE_SECRET; break;
3825 case 'm': cleared |= MODE_MODERATED; break;
3826 case 't': cleared |= MODE_TOPICLIMIT; break;
3827 case 'i': cleared |= MODE_INVITEONLY; break;
3828 case 'n': cleared |= MODE_NOPRIVMSGS; break;
3829 case 'k':
3830 cleared |= MODE_KEY;
3831 channel->key[0] = '\0';
3832 break;
3833 case 'A':
3834 cleared |= MODE_APASS;
3835 channel->apass[0] = '\0';
3836 break;
3837 case 'U':
3838 cleared |= MODE_UPASS;
3839 channel->upass[0] = '\0';
3840 break;
3841 case 'l':
3842 cleared |= MODE_LIMIT;
3843 channel->limit = 0;
3844 break;
3845 case 'b': cleared |= MODE_BAN; break;
3846 case 'e': cleared |= MODE_EXEMPT; break;
3847 case 'D': cleared |= MODE_DELAYJOINS; break;
3848 case 'r': cleared |= MODE_REGONLY; break;
3849 case 'c': cleared |= MODE_NOCOLORS; break;
3850 case 'C': cleared |= MODE_NOCTCPS; break;
3851 case 'S': cleared |= MODE_STRIPCOLOR; break;
3852 case 'M': cleared |= MODE_MODUNREG; break;
3853 case 'N': cleared |= MODE_NONOTICE; break;
3854 case 'Q': cleared |= MODE_NOQUITMSGS; break;
3855 case 'T': cleared |= MODE_NOAMSG; break;
3856 case 'O': cleared |= MODE_OPERSONLY; break;
3857 case 'a': cleared |= MODE_ADMINSONLY; break;
3858 case 'z': cleared |= MODE_REGISTERED; break;
3859 case 'Z': cleared |= MODE_SSLONLY; break;
3860 case 'L': cleared |= MODE_HIDEMODE; break;
3861 }
3862 }
3863
3864 if (!cleared)
3865 return 1;
3866
3867 /* Remove simple modes. */
3868 channel->modes &= ~cleared;
3869
3870 /* If removing bans, kill 'em all. */
3871 if ((cleared & MODE_BAN) && channel->banlist.used) {
3872 unsigned int i;
3873 for (i=0; i<channel->banlist.used; i++)
3874 free(channel->banlist.list[i]);
3875 channel->banlist.used = 0;
3876 }
3877
3878 /* If removing exempts, kill 'em all. */
3879 if ((cleared & MODE_EXEMPT) && channel->exemptlist.used) {
3880 unsigned int i;
3881 for (i=0; i<channel->exemptlist.used; i++)
3882 free(channel->exemptlist.list[i]);
3883 channel->exemptlist.used = 0;
3884 }
3885
3886 /* Removed member modes. */
3887 if ((cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
3888 int mask = ~(cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3889 unsigned int i;
3890
3891 for (i = 0; i < channel->members.used; i++)
3892 channel->members.list[i]->modes &= mask;
3893 }
3894
3895 return 1;
3896 }
3897
3898 void
3899 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3900 {
3901 unsigned int numeric = user->num_local;
3902 if (numeric >= num_privmsg_funcs) {
3903 int newnum = numeric + 8, ii;
3904 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
3905 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
3906 privmsg_funcs[ii] = NULL;
3907 num_privmsg_funcs = newnum;
3908 }
3909 if (privmsg_funcs[numeric])
3910 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
3911 privmsg_funcs[numeric] = handler;
3912 }
3913
3914 void
3915 unreg_privmsg_func(struct userNode *user)
3916 {
3917 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
3918 return; /* this really only works with users */
3919
3920 privmsg_funcs[user->num_local] = NULL;
3921 }
3922
3923
3924 void
3925 reg_notice_func(struct userNode *user, privmsg_func_t handler)
3926 {
3927 unsigned int numeric = user->num_local;
3928 if (numeric >= num_notice_funcs) {
3929 int newnum = numeric + 8, ii;
3930 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
3931 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
3932 privmsg_funcs[ii] = NULL;
3933 num_notice_funcs = newnum;
3934 }
3935 if (notice_funcs[numeric])
3936 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
3937 notice_funcs[numeric] = handler;
3938 }
3939
3940 void
3941 unreg_notice_func(struct userNode *user)
3942 {
3943 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
3944 return; /* this really only works with users */
3945
3946 notice_funcs[user->num_local] = NULL;
3947 }
3948
3949 static void
3950 send_burst(void)
3951 {
3952 unsigned int i, hop, max_hop=1;
3953 dict_iterator_t it;
3954
3955 /* burst (juped) servers, closest first (except self, which is sent already) */
3956 for (i=0; i<ArrayLength(servers_num); i++)
3957 if (servers_num[i] && servers_num[i]->hops > max_hop)
3958 max_hop = servers_num[i]->hops;
3959 for (hop=1; hop<=max_hop; hop++) {
3960 for (i=0;i<ArrayLength(servers_num);i++) {
3961 if (servers_num[i]
3962 && (servers_num[i]->hops == hop)
3963 && (servers_num[i] != self->uplink))
3964 irc_server(servers_num[i]);
3965 }
3966 }
3967
3968 /* burst local nicks */
3969 for (i=0; i<=self->num_mask; i++)
3970 if (self->users[i])
3971 irc_user(self->users[i]);
3972
3973 /* build dict of unbursted channel names (just copy existing channels) */
3974 unbursted_channels = dict_new();
3975 for (it = dict_first(channels); it; it = iter_next(it))
3976 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
3977 }
3978
3979 /*
3980 * Oplevel parsing
3981 */
3982 static int
3983 parse_oplevel(char *str)
3984 {
3985 int oplevel = 0;
3986 while (isdigit(*str))
3987 oplevel = oplevel * 10 + *str++ - '0';
3988 return oplevel;
3989 }
3990