/[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.60 - (show annotations) (download) (as text)
Mon Feb 3 11:41:34 2003 UTC (16 years, 8 months ago) by wcc
Branch: MAIN
Changes since 1.59: +6 -5 lines
File MIME type: text/x-chdr
* More botnet removal.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23