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

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

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


Revision 1.26 - (hide annotations) (download) (as text)
Fri Jan 26 21:18:22 2001 UTC (18 years, 8 months ago) by guppy
Branch: MAIN
Changes since 1.25: +16 -1 lines
File MIME type: text/x-chdr
Tothwolf's lastest i18n fix

1 fabian 1.10 /*
2 segfault 1.1 * dccutil.c -- handles:
3 fabian 1.10 * lots of little functions to send formatted text to
4     * varying types of connections
5     * '.who', '.whom', and '.dccstat' code
6     * memory management for dcc structures
7     * timeout checking for dcc connections
8     *
9 guppy 1.26 * $Id: dccutil.c,v 1.25 2001/01/22 23:47:33 guppy Exp $
10 segfault 1.1 */
11 fabian 1.10 /*
12     * Copyright (C) 1997 Robey Pointer
13 fabian 1.16 * Copyright (C) 1999, 2000 Eggheads
14 fabian 1.10 *
15     * This program is free software; you can redistribute it and/or
16     * modify it under the terms of the GNU General Public License
17     * as published by the Free Software Foundation; either version 2
18     * of the License, or (at your option) any later version.
19     *
20     * This program is distributed in the hope that it will be useful,
21     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23     * GNU General Public License for more details.
24     *
25     * You should have received a copy of the GNU General Public License
26     * along with this program; if not, write to the Free Software
27     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 segfault 1.1 */
29    
30 fabian 1.17 #include <sys/stat.h>
31 segfault 1.1 #include "main.h"
32     #include <errno.h>
33     #include "chan.h"
34     #include "modules.h"
35     #include "tandem.h"
36    
37 fabian 1.17 extern struct dcc_t *dcc;
38     extern int dcc_total, max_dcc, dcc_flood_thr, backgrd, MAXSOCKS;
39     extern char botnetnick[], spaces[], version[];
40     extern time_t now;
41     extern sock_list *socklist;
42 guppy 1.26 extern Tcl_Interp *interp;
43 fabian 1.17
44     char motdfile[121] = "text/motd"; /* File where the motd is stored */
45     int connect_timeout = 15; /* How long to wait before a telnet
46     connection times out */
47 segfault 1.1
48 guppy 1.25 int reserved_port_min = 0;
49     int reserved_port_max = 0;
50 segfault 1.1
51     void init_dcc_max()
52     {
53     int osock = MAXSOCKS;
54    
55     if (max_dcc < 1)
56     max_dcc = 1;
57     if (dcc)
58     dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
59     else
60     dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
61    
62     MAXSOCKS = max_dcc + 10;
63     if (socklist)
64     socklist = (sock_list *) nrealloc((void *) socklist,
65     sizeof(sock_list) * MAXSOCKS);
66     else
67     socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
68     for (; osock < MAXSOCKS; osock++)
69     socklist[osock].flags = SOCK_UNUSED;
70     }
71    
72     int expmem_dccutil()
73     {
74     int tot, i;
75    
76     tot = sizeof(struct dcc_t) * max_dcc + sizeof(sock_list) * MAXSOCKS;
77    
78     for (i = 0; i < dcc_total; i++) {
79     if (dcc[i].type && dcc[i].type->expmem)
80     tot += dcc[i].type->expmem(dcc[i].u.other);
81     }
82     return tot;
83     }
84    
85     static char SBUF[1024];
86    
87 fabian 1.17 /* Replace \n with \r\n */
88 segfault 1.1 char *add_cr(char *buf)
89     {
90     static char WBUF[1024];
91     char *p, *q;
92    
93     for (p = buf, q = WBUF; *p; p++, q++) {
94     if (*p == '\n')
95     *q++ = '\r';
96     *q = *p;
97     }
98     *q = *p;
99     return WBUF;
100     }
101    
102     extern void (*qserver) (int, char *, int);
103 fabian 1.7
104 fabian 1.5 void dprintf EGG_VARARGS_DEF(int, arg1)
105 segfault 1.1 {
106     char *format;
107     int idx, len;
108 fabian 1.17 va_list va;
109 guppy 1.26 #if TCL_MAJOR_VERSION >= 8 && TCL_MINOR_VERSION >= 1
110     Tcl_DString ds;
111     #endif
112 segfault 1.1
113 fabian 1.5 idx = EGG_VARARGS_START(int, arg1, va);
114 segfault 1.1 format = va_arg(va, char *);
115 fabian 1.18 if ((len = egg_vsnprintf(SBUF, 1023, format, va)) < 0)
116 segfault 1.1 SBUF[len = 1023] = 0;
117     va_end(va);
118 guppy 1.26
119     /* Used for unicode text */
120     #if TCL_MAJOR_VERSION >= 8 && TCL_MINOR_VERSION >= 1
121     Tcl_DStringInit(&ds);
122     /* Don't call this before calling init_tcl() */
123     Tcl_UtfToExternalDString(NULL, SBUF, -1, &ds);
124     Tcl_DStringResult(interp, &ds);
125     Tcl_DStringFree(&ds);
126     strncpyz(SBUF, interp->result, sizeof SBUF);
127     #endif
128    
129 segfault 1.1 if (idx < 0) {
130     tputs(-idx, SBUF, len);
131     } else if (idx > 0x7FF0) {
132     switch (idx) {
133     case DP_LOG:
134     putlog(LOG_MISC, "*", "%s", SBUF);
135     break;
136     case DP_STDOUT:
137     tputs(STDOUT, SBUF, len);
138     break;
139     case DP_STDERR:
140     tputs(STDERR, SBUF, len);
141     break;
142     case DP_SERVER:
143     case DP_HELP:
144     case DP_MODE:
145 fabian 1.8 case DP_MODE_NEXT:
146     case DP_SERVER_NEXT:
147     case DP_HELP_NEXT:
148 segfault 1.1 qserver(idx, SBUF, len);
149     break;
150     }
151     return;
152     } else {
153 fabian 1.17 if (len > 500) { /* Truncate to fit */
154 segfault 1.1 SBUF[500] = 0;
155     strcat(SBUF, "\n");
156     len = 501;
157     }
158     if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
159     char *p = add_cr(SBUF);
160    
161     tputs(dcc[idx].sock, p, strlen(p));
162     } else if (dcc[idx].type && dcc[idx].type->output) {
163     dcc[idx].type->output(idx, SBUF, dcc[idx].u.other);
164     } else
165     tputs(dcc[idx].sock, SBUF, len);
166     }
167     }
168    
169 fabian 1.5 void chatout EGG_VARARGS_DEF(char *, arg1)
170 segfault 1.1 {
171     int i;
172     char *format;
173     char s[601];
174 fabian 1.17 va_list va;
175 segfault 1.1
176 fabian 1.5 format = EGG_VARARGS_START(char *, arg1, va);
177 fabian 1.18 if (egg_vsnprintf(s, 511, format, va) < 0)
178 segfault 1.1 s[511] = 0;
179     for (i = 0; i < dcc_total; i++)
180     if (dcc[i].type == &DCC_CHAT)
181     if (dcc[i].u.chat->channel >= 0)
182     dprintf(i, "%s", s);
183     va_end(va);
184     }
185    
186 fabian 1.17 /* Print to all on this channel but one.
187     */
188 fabian 1.5 void chanout_but EGG_VARARGS_DEF(int, arg1)
189 segfault 1.1 {
190     int i, x, chan;
191     char *format;
192     char s[601];
193 fabian 1.17 va_list va;
194 segfault 1.1
195 fabian 1.5 x = EGG_VARARGS_START(int, arg1, va);
196 segfault 1.1 chan = va_arg(va, int);
197     format = va_arg(va, char *);
198 fabian 1.18 if (egg_vsnprintf(s, 511, format, va) < 0)
199 segfault 1.1 s[511] = 0;
200     for (i = 0; i < dcc_total; i++)
201     if ((dcc[i].type == &DCC_CHAT) && (i != x))
202     if (dcc[i].u.chat->channel == chan)
203     dprintf(i, "%s", s);
204     va_end(va);
205     }
206    
207     void dcc_chatter(int idx)
208     {
209     int i, j;
210 fabian 1.17 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
211 segfault 1.1
212     get_user_flagrec(dcc[idx].user, &fr, NULL);
213     show_motd(idx);
214     i = dcc[idx].u.chat->channel;
215     dcc[idx].u.chat->channel = 234567;
216     j = dcc[idx].sock;
217     strcpy(dcc[idx].u.chat->con_chan, "***");
218     check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
219 fabian 1.17 /* Still there? */
220 segfault 1.1 if ((idx >= dcc_total) || (dcc[idx].sock != j))
221 fabian 1.17 return; /* Nope */
222     /* Tcl script may have taken control */
223 segfault 1.1 if (dcc[idx].type == &DCC_CHAT) {
224     if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
225     strcpy(dcc[idx].u.chat->con_chan, "*");
226     if (dcc[idx].u.chat->channel == 234567) {
227 fabian 1.17 /* If the chat channel has already been altered it's *highly*
228 segfault 1.1 * probably join/part messages have been broadcast everywhere,
229 fabian 1.17 * so dont bother sending them
230     */
231 segfault 1.1 if (i == -2)
232     i = 0;
233     dcc[idx].u.chat->channel = i;
234     if (dcc[idx].u.chat->channel >= 0) {
235     if (dcc[idx].u.chat->channel < 100000) {
236     botnet_send_join_idx(idx, -1);
237     }
238     }
239     check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
240     geticon(idx), dcc[idx].sock, dcc[idx].host);
241     }
242 fabian 1.17 /* But *do* bother with sending it locally */
243 guppy 1.24 if (!dcc[idx].u.chat->channel) {
244 segfault 1.1 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
245     } else if (dcc[idx].u.chat->channel > 0) {
246     chanout_but(-1, dcc[idx].u.chat->channel,
247     "*** %s joined the channel.\n", dcc[idx].nick);
248     }
249     }
250     }
251    
252 fabian 1.14 /* Mark an entry as lost and deconstruct it's contents. It will be securely
253     * removed from the dcc list in the main loop.
254     */
255 segfault 1.1 void lostdcc(int n)
256     {
257 fabian 1.14 if (dcc[n].type && dcc[n].type->kill)
258     dcc[n].type->kill(n, dcc[n].u.other);
259     else if (dcc[n].u.other)
260     nfree(dcc[n].u.other);
261 fabian 1.19 egg_bzero(&dcc[n], sizeof(struct dcc_t));
262 fabian 1.14
263     dcc[n].sock = (-1);
264 fabian 1.11 dcc[n].type = &DCC_LOST;
265     }
266    
267     /* Remove entry from dcc list. Think twice before using this function,
268     * because it invalidates any variables that point to a specific dcc
269     * entry!
270 fabian 1.17 *
271     * Note: The entry will be deconstructed if it was not deconstructed
272     * already. This case should normally not occur.
273 fabian 1.11 */
274     void removedcc(int n)
275     {
276 segfault 1.1 if (dcc[n].type && dcc[n].type->kill)
277     dcc[n].type->kill(n, dcc[n].u.other);
278     else if (dcc[n].u.other)
279     nfree(dcc[n].u.other);
280     dcc_total--;
281     if (n < dcc_total)
282 fabian 1.19 egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
283 segfault 1.1 else
284 fabian 1.19 egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
285 fabian 1.12 }
286    
287     /* Clean up sockets that were just left for dead.
288     */
289     void dcc_remove_lost(void)
290     {
291     int i;
292    
293     for (i = 0; i < dcc_total; i++) {
294     if (dcc[i].type == &DCC_LOST) {
295 fabian 1.14 dcc[i].type = NULL;
296 fabian 1.12 dcc[i].sock = (-1);
297     removedcc(i);
298     i--;
299     }
300     }
301 segfault 1.1 }
302    
303 fabian 1.17 /* Show list of current dcc's to a dcc-chatter
304     * positive value: idx given -- negative value: sock given
305     */
306 segfault 1.1 void tell_dcc(int zidx)
307     {
308     int i, j, k;
309     char other[160];
310    
311     spaces[HANDLEN - 9] = 0;
312     dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
313     ,spaces);
314     dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
315     ,spaces);
316     spaces[HANDLEN - 9] = ' ';
317 fabian 1.17 /* Show server */
318 segfault 1.1 for (i = 0; i < dcc_total; i++) {
319     j = strlen(dcc[i].host);
320     if (j > 17)
321     j -= 17;
322     else
323     j = 0;
324     if (dcc[i].type && dcc[i].type->display)
325     dcc[i].type->display(i, other);
326     else {
327     sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
328     break;
329     }
330     k = HANDLEN - strlen(dcc[i].nick);
331     spaces[k] = 0;
332 fabian 1.14 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
333 segfault 1.1 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
334     spaces[k] = ' ';
335     }
336     }
337    
338 fabian 1.17 /* Mark someone on dcc chat as no longer away
339     */
340 segfault 1.1 void not_away(int idx)
341     {
342     if (dcc[idx].u.chat->away == NULL) {
343     dprintf(idx, "You weren't away!\n");
344     return;
345     }
346     if (dcc[idx].u.chat->channel >= 0) {
347     chanout_but(-1, dcc[idx].u.chat->channel,
348     "*** %s is no longer away.\n", dcc[idx].nick);
349     if (dcc[idx].u.chat->channel < 100000) {
350     botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
351     }
352     }
353     dprintf(idx, "You're not away any more.\n");
354     nfree(dcc[idx].u.chat->away);
355     dcc[idx].u.chat->away = NULL;
356     check_tcl_away(botnetnick, dcc[idx].sock, NULL);
357     }
358    
359     void set_away(int idx, char *s)
360     {
361     if (s == NULL) {
362     not_away(idx);
363     return;
364     }
365     if (!s[0]) {
366     not_away(idx);
367     return;
368     }
369     if (dcc[idx].u.chat->away != NULL)
370     nfree(dcc[idx].u.chat->away);
371     dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
372     strcpy(dcc[idx].u.chat->away, s);
373     if (dcc[idx].u.chat->channel >= 0) {
374     chanout_but(-1, dcc[idx].u.chat->channel,
375     "*** %s is now away: %s\n", dcc[idx].nick, s);
376     if (dcc[idx].u.chat->channel < 100000) {
377     botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
378     }
379     }
380     dprintf(idx, "You are now away.\n");
381     check_tcl_away(botnetnick, dcc[idx].sock, s);
382     }
383    
384 fabian 1.17 /* This helps the memory debugging
385     */
386 segfault 1.1 void *_get_data_ptr(int size, char *file, int line)
387     {
388     char *p;
389 fabian 1.9 #ifdef DEBUG_MEM
390 segfault 1.1 char x[1024];
391    
392 fabian 1.20 p = strrchr(file, '/');
393 guppy 1.24 egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
394 segfault 1.1 p = n_malloc(size, x, line);
395     #else
396     p = nmalloc(size);
397     #endif
398 fabian 1.19 egg_bzero(p, size);
399 segfault 1.1 return p;
400     }
401    
402 fabian 1.17 /* Make a password, 10-15 random letters and digits
403     */
404 segfault 1.1 void makepass(char *s)
405     {
406 fabian 1.2 int i;
407 segfault 1.1
408     i = 10 + (random() % 6);
409 fabian 1.2 make_rand_str(s, i);
410 segfault 1.1 }
411    
412     void flush_lines(int idx, struct chat_info *ci)
413     {
414     int c = ci->line_count;
415     struct msgq *p = ci->buffer, *o;
416    
417     while (p && c < (ci->max_line)) {
418     ci->current_lines--;
419     tputs(dcc[idx].sock, p->msg, p->len);
420     nfree(p->msg);
421     o = p->next;
422     nfree(p);
423     p = o;
424     c++;
425     }
426     if (p != NULL) {
427     if (dcc[idx].status & STAT_TELNET)
428     tputs(dcc[idx].sock, "[More]: ", 8);
429     else
430     tputs(dcc[idx].sock, "[More]\n", 7);
431     }
432     ci->buffer = p;
433     ci->line_count = 0;
434     }
435    
436     int new_dcc(struct dcc_table *type, int xtra_size)
437     {
438     int i = dcc_total;
439    
440     if (dcc_total == max_dcc)
441     return -1;
442     dcc_total++;
443 fabian 1.19 egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
444 segfault 1.1
445     dcc[i].type = type;
446     if (xtra_size) {
447     dcc[i].u.other = nmalloc(xtra_size);
448 fabian 1.19 egg_bzero(dcc[i].u.other, xtra_size);
449 segfault 1.1 }
450     return i;
451     }
452    
453 fabian 1.22 /* Changes the given dcc entry to another type.
454     */
455 fabian 1.3 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
456     {
457 fabian 1.22 /* Free old structure. */
458 fabian 1.3 if (dcc[i].type && dcc[i].type->kill)
459     dcc[i].type->kill(i, dcc[i].u.other);
460     else if (dcc[i].u.other) {
461     nfree(dcc[i].u.other);
462     dcc[i].u.other = NULL;
463     }
464    
465     dcc[i].type = type;
466     if (xtra_size) {
467     dcc[i].u.other = nmalloc(xtra_size);
468 fabian 1.19 egg_bzero(dcc[i].u.other, xtra_size);
469 fabian 1.3 }
470     }
471    
472 segfault 1.1 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
473     {
474     time_t t;
475    
476 guppy 1.24 if (!dcc_flood_thr)
477 segfault 1.1 return 0;
478     t = now;
479     if (*timer != t) {
480     *timer = t;
481     chat->msgs_per_sec = 0;
482     } else {
483     chat->msgs_per_sec++;
484     if (chat->msgs_per_sec > dcc_flood_thr) {
485     /* FLOOD */
486     dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
487 fabian 1.17 /* Evil assumption here that flags&DCT_CHAT implies chat type */
488 segfault 1.1 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
489     (chat->channel >= 0)) {
490     char x[1024];
491    
492 guppy 1.24 egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
493 segfault 1.1 chanout_but(idx, chat->channel, "*** %s", x);
494     if (chat->channel < 100000)
495     botnet_send_part_idx(idx, x);
496     }
497     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
498     if ((dcc[idx].sock != STDOUT) || backgrd) {
499     killsock(dcc[idx].sock);
500     lostdcc(idx);
501     } else {
502     dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
503     dcc_chatter(idx);
504     }
505     return 1; /* <- flood */
506     }
507     }
508     return 0;
509     }
510    
511 fabian 1.17 /* Handle someone being booted from dcc chat.
512     */
513 segfault 1.1 void do_boot(int idx, char *by, char *reason)
514     {
515     int files = (dcc[idx].type != &DCC_CHAT);
516    
517     dprintf(idx, DCC_BOOTED1);
518 fabian 1.23 dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot",
519     by, reason[0] ? ": " : ".", reason);
520 fabian 1.17 /* If it's a partyliner (chatterer :) */
521     /* Horrible assumption that DCT_CHAT using structure uses same format
522 segfault 1.1 * as DCC_CHAT */
523     if ((dcc[idx].type->flags & DCT_CHAT) &&
524     (dcc[idx].u.chat->channel >= 0)) {
525     char x[1024];
526    
527 guppy 1.24 egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick,
528     reason[0] ? ": " : "", reason);
529 segfault 1.1 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
530     if (dcc[idx].u.chat->channel < 100000)
531     botnet_send_part_idx(idx, x);
532     }
533     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
534     if ((dcc[idx].sock != STDOUT) || backgrd) {
535     killsock(dcc[idx].sock);
536 fabian 1.11 lostdcc(idx);
537 fabian 1.17 /* Entry must remain in the table so it can be logged by the caller */
538 segfault 1.1 } else {
539     dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
540     dcc_chatter(idx);
541     }
542     return;
543     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23