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