/[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.61 - (show annotations) (download) (as text)
Mon Feb 10 00:09:08 2003 UTC (16 years, 9 months ago) by wcc
Branch: MAIN
Changes since 1.60: +11 -8 lines
File MIME type: text/x-chdr
* Synch 1.6: transfer-cleanups, filesys-move, delbancrash, plsbot_cosmetic,
             stricthostfix, opreverselogic, dontdelfile

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23