/[cvs]/eggdrop1.9/src/dccutil.c
ViewVC logotype

Contents of /eggdrop1.9/src/dccutil.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.55 - (show annotations) (download) (as text)
Fri Sep 20 21:41:49 2002 UTC (17 years, 1 month ago) by stdarg
Branch: MAIN
Changes since 1.54: +102 -18 lines
File MIME type: text/x-chdr
* Mo-Ize removed the botnet code, thanks

1 /*
2 * dccutil.c --
3 *
4 * lots of little functions to send formatted text to
5 * varying types of connections
6 * '.who', '.whom', and '.dccstat' code
7 * memory management for dcc structures
8 * timeout checking for dcc connections
9 */
10 /*
11 * Copyright (C) 1997 Robey Pointer
12 * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 #ifndef lint
30 static const char rcsid[] = "$Id: dccutil.c,v 1.54 2002/05/26 08:34:13 stdarg Exp $";
31 #endif
32
33 #include <sys/stat.h>
34 #include "main.h"
35 #include <errno.h>
36 #include "chan.h"
37 #include "modules.h"
38 #include "logfile.h"
39 #include "misc.h"
40 #include "cmdt.h" /* cmd_t */
41 #include "net.h" /* tputs, killsock */
42 #include "core_binds.h" /* check_bind_chon, check_bind_chjn,
43 check_bind_chof, check_bind_away */
44 #include "dccutil.h" /* prototypes */
45 #include "users.h" /* get_user_by_handle */
46
47 extern struct dcc_t *dcc;
48 extern struct userrec *userlist;
49 extern int dcc_total, max_dcc, dcc_flood_thr, backgrd, MAXSOCKS;
50 extern char spaces[], version[];
51 extern time_t now;
52 extern sock_list *socklist;
53
54 #ifndef MAKING_MODS
55 extern struct dcc_table DCC_CHAT, DCC_LOST;
56 #endif /* MAKING_MODS */
57
58 char botnetnick[HANDLEN + 1] = "";
59 char motdfile[121] = "text/motd"; /* File where the motd is stored */
60 int connect_timeout = 15; /* How long to wait before a telnet
61 connection times out */
62
63 int reserved_port_min = 0;
64 int reserved_port_max = 0;
65
66 void init_dcc_max(int newmax)
67 {
68 int osock = MAXSOCKS;
69
70 if (newmax < 1) newmax = 1;
71 dcc = realloc(dcc, sizeof(struct dcc_t) * newmax);
72 if (newmax > max_dcc) memset(dcc+max_dcc, 0, (newmax-max_dcc) * sizeof(*dcc));
73
74 MAXSOCKS = newmax + 10;
75 socklist = (sock_list *)realloc(socklist, sizeof(sock_list) * MAXSOCKS);
76 if (newmax > max_dcc) memset(socklist+max_dcc+10, 0, (newmax-max_dcc) * sizeof(*socklist));
77
78 for (; osock < MAXSOCKS; osock++)
79 socklist[osock].flags = SOCK_UNUSED;
80
81 max_dcc = newmax;
82 }
83
84
85 /* Replace \n with \r\n */
86 char *add_cr(char *buf)
87 {
88 static char WBUF[1024];
89 char *p, *q;
90
91 for (p = buf, q = WBUF; *p; p++, q++) {
92 if (*p == '\n')
93 *q++ = '\r';
94 *q = *p;
95 }
96 *q = *p;
97 return WBUF;
98 }
99
100 extern void (*qserver) (int, char *, int);
101
102 void dprintf EGG_VARARGS_DEF(int, arg1)
103 {
104 static char buf[1024];
105 char *format;
106 int idx, len;
107 va_list va;
108
109 idx = EGG_VARARGS_START(int, arg1, va);
110 format = va_arg(va, char *);
111 len = vsnprintf(buf, 1023, format, va);
112 va_end(va);
113 if (len < 0 || len >= sizeof(buf)) len = sizeof(buf)-1;
114 buf[len] = 0;
115
116 if (idx < 0) {
117 tputs(-idx, buf, len);
118 } else if (idx > 0x7FF0) {
119 switch (idx) {
120 case DP_LOG:
121 putlog(LOG_MISC, "*", "%s", buf);
122 break;
123 case DP_STDOUT:
124 tputs(STDOUT, buf, len);
125 break;
126 case DP_STDERR:
127 tputs(STDERR, buf, len);
128 break;
129 case DP_SERVER:
130 case DP_HELP:
131 case DP_MODE:
132 case DP_MODE_NEXT:
133 case DP_SERVER_NEXT:
134 case DP_HELP_NEXT:
135 qserver(idx, buf, len);
136 break;
137 }
138 return;
139 } else {
140 if (len > 500) { /* Truncate to fit */
141 buf[500] = 0;
142 strcat(buf, "\n");
143 len = 501;
144 }
145 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
146 char *p = add_cr(buf);
147
148 tputs(dcc[idx].sock, p, strlen(p));
149 } else if (dcc[idx].type && dcc[idx].type->output) {
150 dcc[idx].type->output(idx, buf, dcc[idx].u.other);
151 } else
152 tputs(dcc[idx].sock, buf, len);
153 }
154 }
155
156 void chatout EGG_VARARGS_DEF(char *, arg1)
157 {
158 int i, len;
159 char *format;
160 char s[601];
161 va_list va;
162
163 format = EGG_VARARGS_START(char *, arg1, va);
164 vsnprintf(s, 511, format, va);
165 va_end(va);
166 len = strlen(s);
167 if (len > 511)
168 len = 511;
169 s[len + 1] = 0;
170
171 for (i = 0; i < dcc_total; i++)
172 if (dcc[i].type == &DCC_CHAT)
173 if (dcc[i].u.chat->channel >= 0)
174 dprintf(i, "%s", s);
175
176 }
177
178 /* Print to all on this channel but one.
179 */
180 void chanout_but EGG_VARARGS_DEF(int, arg1)
181 {
182 int i, x, chan, len;
183 char *format;
184 char s[601];
185 va_list va;
186
187 x = EGG_VARARGS_START(int, arg1, va);
188 chan = va_arg(va, int);
189 format = va_arg(va, char *);
190 vsnprintf(s, 511, format, va);
191 va_end(va);
192 len = strlen(s);
193 if (len > 511)
194 len = 511;
195 s[len + 1] = 0;
196
197 for (i = 0; i < dcc_total; i++)
198 if ((dcc[i].type == &DCC_CHAT) && (i != x))
199 if (dcc[i].u.chat->channel == chan)
200 dprintf(i, "%s", s);
201
202 }
203
204 void dcc_chatter(int idx)
205 {
206 int i, j;
207 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
208
209 get_user_flagrec(dcc[idx].user, &fr, NULL);
210 show_motd(idx);
211 i = dcc[idx].u.chat->channel;
212 dcc[idx].u.chat->channel = 234567;
213 j = dcc[idx].sock;
214 strcpy(dcc[idx].u.chat->con_chan, "***");
215 check_bind_chon(dcc[idx].nick, idx);
216 /* Still there? */
217 if ((idx >= dcc_total) || (dcc[idx].sock != j))
218 return; /* Nope */
219 /* Tcl script may have taken control */
220 if (dcc[idx].type == &DCC_CHAT) {
221 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
222 strcpy(dcc[idx].u.chat->con_chan, "*");
223 if (dcc[idx].u.chat->channel == 234567) {
224 /* If the chat channel has already been altered it's *highly*
225 * probably join/part messages have been broadcast everywhere,
226 * so dont bother sending them
227 */
228 if (i == -2)
229 i = 0;
230 dcc[idx].u.chat->channel = i;
231 check_bind_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
232 geticon(dcc[idx].user), dcc[idx].sock, dcc[idx].host);
233 }
234 /* But *do* bother with sending it locally */
235 if (!dcc[idx].u.chat->channel) {
236 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
237 } else if (dcc[idx].u.chat->channel > 0) {
238 chanout_but(-1, dcc[idx].u.chat->channel,
239 "*** %s joined the channel.\n", dcc[idx].nick);
240 }
241 }
242 }
243
244 /* Mark an entry as lost and deconstruct it's contents. It will be securely
245 * removed from the dcc list in the main loop.
246 */
247 void lostdcc(int n)
248 {
249 /* Make sure it's a valid dcc index. */
250 if (n < 0 || n >= max_dcc) return;
251
252 if (dcc[n].type && dcc[n].type->kill)
253 dcc[n].type->kill(n, dcc[n].u.other);
254 else if (dcc[n].u.other)
255 free(dcc[n].u.other);
256 memset(&dcc[n], 0, sizeof(struct dcc_t));
257
258 dcc[n].sock = (-1);
259 dcc[n].type = &DCC_LOST;
260 }
261
262 /* Remove entry from dcc list. Think twice before using this function,
263 * because it invalidates any variables that point to a specific dcc
264 * entry!
265 *
266 * Note: The entry will be deconstructed if it was not deconstructed
267 * already. This case should normally not occur.
268 */
269 static void removedcc(int n)
270 {
271 if (dcc[n].type && dcc[n].type->kill)
272 dcc[n].type->kill(n, dcc[n].u.other);
273 else if (dcc[n].u.other)
274 free(dcc[n].u.other);
275
276 /* if we are removing the last entry let's decrease dcc_total */
277 if(n + 1 == dcc_total) dcc_total--;
278
279 memset(&dcc[n], 0, sizeof(struct dcc_t)); /* drummer */
280 }
281
282 /* Clean up sockets that were just left for dead.
283 */
284 void dcc_remove_lost(void)
285 {
286 int i;
287
288
289 for (i = dcc_total; i > 0; --i) {
290 if (dcc[i].type == &DCC_LOST) {
291 dcc[i].type = NULL;
292 removedcc(i);
293 }
294 }
295 }
296
297 /* Show list of current dcc's to a dcc-chatter
298 * positive value: idx given -- negative value: sock given
299 */
300 void tell_dcc(int zidx)
301 {
302 int i, j, k;
303 char other[160];
304
305 spaces[HANDLEN - 9] = 0;
306 dprintf(zidx, "SOCK PORT NICK %s HOST TYPE\n"
307 ,spaces);
308 dprintf(zidx, "---- ----- ---------%s -------------------------- ----\n"
309 ,spaces);
310 spaces[HANDLEN - 9] = ' ';
311 /* Show server */
312 for (i = 0; i < dcc_total; i++) {
313 if (!dcc[i].type) continue;
314 j = strlen(dcc[i].host);
315 if (j > 26)
316 j -= 26;
317 else
318 j = 0;
319 if (dcc[i].type && dcc[i].type->display)
320 dcc[i].type->display(i, other);
321 else {
322 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
323 break;
324 }
325 k = HANDLEN - strlen(dcc[i].nick);
326 spaces[k] = 0;
327 dprintf(zidx, "%-4d %5d %s%s %-26s %s\n", i,
328 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
329 spaces[k] = ' ';
330 }
331 }
332
333 /* Mark someone on dcc chat as no longer away
334 */
335 void not_away(int idx)
336 {
337 if (dcc[idx].u.chat->away == NULL) {
338 dprintf(idx, "You weren't away!\n");
339 return;
340 }
341 if (dcc[idx].u.chat->channel >= 0) {
342 chanout_but(-1, dcc[idx].u.chat->channel,
343 "*** %s is no longer away.\n", dcc[idx].nick);
344 }
345 dprintf(idx, "You're not away any more.\n");
346 free_null(dcc[idx].u.chat->away);
347 check_bind_away(botnetnick, dcc[idx].sock, NULL);
348 }
349
350 void set_away(int idx, char *s)
351 {
352 if (s == NULL) {
353 not_away(idx);
354 return;
355 }
356 if (!s[0]) {
357 not_away(idx);
358 return;
359 }
360 if (dcc[idx].u.chat->away != NULL)
361 free(dcc[idx].u.chat->away);
362 dcc[idx].u.chat->away = strdup(s);
363 if (dcc[idx].u.chat->channel >= 0) {
364 chanout_but(-1, dcc[idx].u.chat->channel,
365 "*** %s is now away: %s\n", dcc[idx].nick, s);
366 }
367 dprintf(idx, "You are now away.\n");
368 check_bind_away(botnetnick, dcc[idx].sock, s);
369 }
370
371 /* Make a password, 10-15 random letters and digits
372 */
373 void makepass(char *s)
374 {
375 int i;
376
377 i = 10 + (random() % 6);
378 make_rand_str(s, i);
379 }
380
381 void flush_lines(int idx, struct chat_info *ci)
382 {
383 int c = ci->line_count;
384 struct msgq *p = ci->buffer, *o;
385
386 while (p && c < (ci->max_line)) {
387 ci->current_lines--;
388 tputs(dcc[idx].sock, p->msg, p->len);
389 free(p->msg);
390 o = p->next;
391 free(p);
392 p = o;
393 c++;
394 }
395 if (p != NULL) {
396 if (dcc[idx].status & STAT_TELNET)
397 tputs(dcc[idx].sock, "[More]: ", 8);
398 else
399 tputs(dcc[idx].sock, "[More]\n", 7);
400 }
401 ci->buffer = p;
402 ci->line_count = 0;
403 }
404
405 int new_dcc(struct dcc_table *type, int xtra_size)
406 {
407 int i = dcc_total;
408
409 for (i = 0; i < dcc_total; i++)
410 if (!dcc[i].type) break;
411
412 if (i == max_dcc)
413 return -1;
414
415 if (i == dcc_total)
416 dcc_total++;
417 memset(&dcc[i], 0, sizeof(*dcc));
418
419 dcc[i].type = type;
420 if (xtra_size)
421 dcc[i].u.other = calloc(1, xtra_size);
422 return i;
423 }
424
425 /* Changes the given dcc entry to another type.
426 */
427 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
428 {
429 /* Free old structure. */
430 if (dcc[i].type && dcc[i].type->kill)
431 dcc[i].type->kill(i, dcc[i].u.other);
432 else if (dcc[i].u.other)
433 free_null(dcc[i].u.other);
434
435 dcc[i].type = type;
436 if (xtra_size)
437 dcc[i].u.other = calloc(1, xtra_size);
438 }
439
440 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
441 {
442 time_t t;
443
444 if (!dcc_flood_thr)
445 return 0;
446 t = now;
447 if (*timer != t) {
448 *timer = t;
449 chat->msgs_per_sec = 0;
450 } else {
451 chat->msgs_per_sec++;
452 if (chat->msgs_per_sec > dcc_flood_thr) {
453 /* FLOOD */
454 dprintf(idx, "*** FLOOD: %s.\n", _("Goodbye"));
455 /* Evil assumption here that flags&DCT_CHAT implies chat type */
456 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
457 (chat->channel >= 0)) {
458 char x[1024];
459
460 snprintf(x, sizeof x, _("%s has been forcibly removed for flooding.\n"), dcc[idx].nick);
461 chanout_but(idx, chat->channel, "*** %s", x);
462 }
463 check_bind_chof(dcc[idx].nick, dcc[idx].sock);
464 if ((dcc[idx].sock != STDOUT) || backgrd) {
465 killsock(dcc[idx].sock);
466 lostdcc(idx);
467 } else {
468 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
469 dcc_chatter(idx);
470 }
471 return 1; /* <- flood */
472 }
473 }
474 return 0;
475 }
476
477 /* Handle someone being booted from dcc chat.
478 */
479 void do_boot(int idx, char *by, char *reason)
480 {
481 int files = (dcc[idx].type != &DCC_CHAT);
482
483 dprintf(idx, _("-=- poof -=-\n"));
484 dprintf(idx, _("Youve been booted from the %s by %s%s%s\n"), files ? "file section" : "bot",
485 by, reason[0] ? ": " : ".", reason);
486 /* If it's a partyliner (chatterer :) */
487 /* Horrible assumption that DCT_CHAT using structure uses same format
488 * as DCC_CHAT */
489 if ((dcc[idx].type->flags & DCT_CHAT) &&
490 (dcc[idx].u.chat->channel >= 0)) {
491 char x[1024];
492
493 snprintf(x, sizeof x, _("%s booted %s from the party line%s%s\n"), by, dcc[idx].nick,
494 reason[0] ? ": " : "", reason);
495 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
496 }
497 check_bind_chof(dcc[idx].nick, dcc[idx].sock);
498 if ((dcc[idx].sock != STDOUT) || backgrd) {
499 killsock(dcc[idx].sock);
500 lostdcc(idx);
501 /* Entry must remain in the table so it can be logged by the caller */
502 } else {
503 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
504 dcc_chatter(idx);
505 }
506 return;
507 }
508
509 int add_note(char *to, char *from, char *msg, int idx, int echo)
510 {
511 int status, i, iaway, sock;
512 char ss[81], ssf[81];
513 struct userrec *u;
514
515 if (strlen(msg) > 450)
516 msg[450] = 0; /* Notes have a limit */
517 /* note length + PRIVMSG header + nickname + date must be <512 */
518 /* Might be form "sock:nick" */
519 splitc(ssf, from, ':');
520 rmspace(ssf);
521 splitc(ss, to, ':');
522 rmspace(ss);
523 if (!ss[0])
524 sock = (-1);
525 else
526 sock = atoi(ss);
527 /* Don't process if there's a note binding for it */
528 if (idx != (-2)) { /* Notes from bots don't trigger it */
529 if (check_bind_note(from, to, msg)) {
530 if ((idx >= 0) && (echo))
531 dprintf(idx, "-> %s: %s\n", to, msg);
532 return NOTE_TCL;
533 }
534 }
535 if (!(u = get_user_by_handle(userlist, to))) {
536 if (idx >= 0)
537 dprintf(idx, _("I dont know anyone by that name.\n"));
538 return NOTE_ERROR;
539 }
540 if (is_bot(u)) {
541 if (idx >= 0)
542 dprintf(idx, _("Thats a bot. You cant leave notes for a bot.\n"));
543 return NOTE_ERROR;
544 }
545 if (match_noterej(u, from)) {
546 if (idx >= 0)
547 dprintf(idx, "%s %s\n", u->handle, "rejected your note.");
548 return NOTE_REJECT;
549 }
550 status = NOTE_STORED;
551 iaway = 0;
552 /* Online right now? */
553 for (i = 0; i < dcc_total; i++) {
554 if (dcc[i].type && (dcc[i].type->flags & DCT_GETNOTES) &&
555 ((sock == (-1)) || (sock == dcc[i].sock)) &&
556 (!strcasecmp(dcc[i].nick, to))) {
557 int aok = 1;
558
559 if (dcc[i].type == &DCC_CHAT)
560 if ((dcc[i].u.chat->away != NULL) &&
561 (idx != (-2))) {
562 /* Only check away if it's not from a bot */
563 aok = 0;
564 if (idx >= 0)
565 dprintf(idx, "%s %s: %s\n", dcc[i].nick, _("is away"),
566 dcc[i].u.chat->away);
567 if (!iaway)
568 iaway = i;
569 status = NOTE_AWAY;
570 }
571 if (aok) {
572 char *p, *fr = from;
573 int l = 0;
574 char work[1024];
575
576 while ((*msg == '<') || (*msg == '>')) {
577 p = newsplit(&msg);
578 if (*p == '<')
579 l += simple_sprintf(work + l, "via %s, ", p + 1);
580 else if (*from == '@')
581 fr = p + 1;
582 }
583 if (idx == -2 || (!strcasecmp(from, botnetnick)))
584 dprintf(i, "*** [%s] %s%s\n", fr, l ? work : "", msg);
585 else
586 dprintf(i, "%cNote [%s]: %s%s\n", 7, fr, l ? work : "", msg);
587 if ((idx >= 0) && (echo))
588 dprintf(idx, "-> %s: %s\n", to, msg);
589 return NOTE_OK;
590 }
591 }
592 }
593 if (idx == (-2))
594 return NOTE_OK; /* Error msg from a tandembot: don't store */
595 status = storenote(from, to, msg, idx, NULL, 0);
596 if (status < 0) status = NOTE_ERROR;
597 else if (status == NOTE_AWAY) {
598 /* User is away in all sessions -- just notify the user that a
599 * message arrived and was stored. (only oldest session is notified.)
600 */
601 dprintf(iaway, "*** %s.\n", _("Note arrived for you"));
602 }
603 return(status);
604 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23