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