/[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.29 - (hide annotations) (download) (as text)
Sun Mar 18 23:00:31 2001 UTC (18 years, 7 months ago) by guppy
Branch: MAIN
Changes since 1.28: +2 -10 lines
File MIME type: text/x-chdr
here is wingman's bug fix, yay

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.29 * $Id: dccutil.c,v 1.28 2001/03/10 06:36:20 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.28 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    
86 fabian 1.17 /* Replace \n with \r\n */
87 segfault 1.1 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 fabian 1.7
103 fabian 1.5 void dprintf EGG_VARARGS_DEF(int, arg1)
104 segfault 1.1 {
105 guppy 1.28 static char buf[1024];
106 segfault 1.1 char *format;
107     int idx, len;
108 fabian 1.17 va_list va;
109 segfault 1.1
110 fabian 1.5 idx = EGG_VARARGS_START(int, arg1, va);
111 segfault 1.1 format = va_arg(va, char *);
112 guppy 1.28 egg_vsnprintf(buf, 1023, format, va);
113 segfault 1.1 va_end(va);
114 guppy 1.28 /* We can not use the return value vsnprintf() to determine where
115     * to null terminate. The C99 standard specifies that vsnprintf()
116     * shall return the number of bytes that would be written if the
117     * buffer had been large enough, rather then -1.
118     */
119     len = strlen(buf);
120     if (len > 1023)
121     len = 1023;
122     buf[len + 1] = 0;
123    
124     #if TCL_MAJOR_VERSION >= 8 && TCL_MINOR_VERSION >= 1
125 guppy 1.29 str_nutf8tounicode(buf, sizeof buf);
126 guppy 1.28 #endif
127    
128 segfault 1.1 if (idx < 0) {
129 guppy 1.28 tputs(-idx, buf, len);
130 segfault 1.1 } else if (idx > 0x7FF0) {
131     switch (idx) {
132     case DP_LOG:
133 guppy 1.28 putlog(LOG_MISC, "*", "%s", buf);
134 segfault 1.1 break;
135     case DP_STDOUT:
136 guppy 1.28 tputs(STDOUT, buf, len);
137 segfault 1.1 break;
138     case DP_STDERR:
139 guppy 1.28 tputs(STDERR, buf, len);
140 segfault 1.1 break;
141     case DP_SERVER:
142     case DP_HELP:
143     case DP_MODE:
144 fabian 1.8 case DP_MODE_NEXT:
145     case DP_SERVER_NEXT:
146     case DP_HELP_NEXT:
147 guppy 1.28 qserver(idx, buf, len);
148 segfault 1.1 break;
149     }
150     return;
151     } else {
152 fabian 1.17 if (len > 500) { /* Truncate to fit */
153 guppy 1.28 buf[500] = 0;
154     strcat(buf, "\n");
155 segfault 1.1 len = 501;
156     }
157     if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
158 guppy 1.28 char *p = add_cr(buf);
159 segfault 1.1
160     tputs(dcc[idx].sock, p, strlen(p));
161     } else if (dcc[idx].type && dcc[idx].type->output) {
162 guppy 1.28 dcc[idx].type->output(idx, buf, dcc[idx].u.other);
163 segfault 1.1 } else
164 guppy 1.28 tputs(dcc[idx].sock, buf, len);
165 segfault 1.1 }
166     }
167    
168 fabian 1.5 void chatout EGG_VARARGS_DEF(char *, arg1)
169 segfault 1.1 {
170 guppy 1.28 int i, len;
171 segfault 1.1 char *format;
172     char s[601];
173 fabian 1.17 va_list va;
174 segfault 1.1
175 fabian 1.5 format = EGG_VARARGS_START(char *, arg1, va);
176 guppy 1.28 egg_vsnprintf(s, 511, format, va);
177     va_end(va);
178     len = strlen(s);
179     if (len > 511)
180     len = 511;
181     s[len + 1] = 0;
182    
183 segfault 1.1 for (i = 0; i < dcc_total; i++)
184     if (dcc[i].type == &DCC_CHAT)
185     if (dcc[i].u.chat->channel >= 0)
186 guppy 1.28 dprintf(i, "%s", s);
187    
188 segfault 1.1 }
189    
190 fabian 1.17 /* Print to all on this channel but one.
191     */
192 fabian 1.5 void chanout_but EGG_VARARGS_DEF(int, arg1)
193 segfault 1.1 {
194 guppy 1.28 int i, x, chan, len;
195 segfault 1.1 char *format;
196     char s[601];
197 fabian 1.17 va_list va;
198 segfault 1.1
199 fabian 1.5 x = EGG_VARARGS_START(int, arg1, va);
200 segfault 1.1 chan = va_arg(va, int);
201     format = va_arg(va, char *);
202 guppy 1.28 egg_vsnprintf(s, 511, format, va);
203     va_end(va);
204     len = strlen(s);
205     if (len > 511)
206     len = 511;
207     s[len + 1] = 0;
208    
209 segfault 1.1 for (i = 0; i < dcc_total; i++)
210     if ((dcc[i].type == &DCC_CHAT) && (i != x))
211     if (dcc[i].u.chat->channel == chan)
212 guppy 1.28 dprintf(i, "%s", s);
213    
214 segfault 1.1 }
215    
216     void dcc_chatter(int idx)
217     {
218     int i, j;
219 fabian 1.17 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
220 segfault 1.1
221     get_user_flagrec(dcc[idx].user, &fr, NULL);
222     show_motd(idx);
223     i = dcc[idx].u.chat->channel;
224     dcc[idx].u.chat->channel = 234567;
225     j = dcc[idx].sock;
226     strcpy(dcc[idx].u.chat->con_chan, "***");
227     check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
228 fabian 1.17 /* Still there? */
229 segfault 1.1 if ((idx >= dcc_total) || (dcc[idx].sock != j))
230 fabian 1.17 return; /* Nope */
231     /* Tcl script may have taken control */
232 segfault 1.1 if (dcc[idx].type == &DCC_CHAT) {
233     if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
234     strcpy(dcc[idx].u.chat->con_chan, "*");
235     if (dcc[idx].u.chat->channel == 234567) {
236 fabian 1.17 /* If the chat channel has already been altered it's *highly*
237 segfault 1.1 * probably join/part messages have been broadcast everywhere,
238 fabian 1.17 * so dont bother sending them
239     */
240 segfault 1.1 if (i == -2)
241     i = 0;
242     dcc[idx].u.chat->channel = i;
243     if (dcc[idx].u.chat->channel >= 0) {
244     if (dcc[idx].u.chat->channel < 100000) {
245     botnet_send_join_idx(idx, -1);
246     }
247     }
248     check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
249     geticon(idx), dcc[idx].sock, dcc[idx].host);
250     }
251 fabian 1.17 /* But *do* bother with sending it locally */
252 guppy 1.24 if (!dcc[idx].u.chat->channel) {
253 segfault 1.1 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
254     } else if (dcc[idx].u.chat->channel > 0) {
255     chanout_but(-1, dcc[idx].u.chat->channel,
256     "*** %s joined the channel.\n", dcc[idx].nick);
257     }
258     }
259     }
260    
261 fabian 1.14 /* Mark an entry as lost and deconstruct it's contents. It will be securely
262     * removed from the dcc list in the main loop.
263     */
264 segfault 1.1 void lostdcc(int n)
265     {
266 fabian 1.14 if (dcc[n].type && dcc[n].type->kill)
267     dcc[n].type->kill(n, dcc[n].u.other);
268     else if (dcc[n].u.other)
269     nfree(dcc[n].u.other);
270 fabian 1.19 egg_bzero(&dcc[n], sizeof(struct dcc_t));
271 fabian 1.14
272     dcc[n].sock = (-1);
273 fabian 1.11 dcc[n].type = &DCC_LOST;
274     }
275    
276     /* Remove entry from dcc list. Think twice before using this function,
277     * because it invalidates any variables that point to a specific dcc
278     * entry!
279 fabian 1.17 *
280     * Note: The entry will be deconstructed if it was not deconstructed
281     * already. This case should normally not occur.
282 fabian 1.11 */
283     void removedcc(int n)
284     {
285 segfault 1.1 if (dcc[n].type && dcc[n].type->kill)
286     dcc[n].type->kill(n, dcc[n].u.other);
287     else if (dcc[n].u.other)
288     nfree(dcc[n].u.other);
289     dcc_total--;
290     if (n < dcc_total)
291 fabian 1.19 egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
292 segfault 1.1 else
293 fabian 1.19 egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
294 fabian 1.12 }
295    
296     /* Clean up sockets that were just left for dead.
297     */
298     void dcc_remove_lost(void)
299     {
300     int i;
301    
302     for (i = 0; i < dcc_total; i++) {
303     if (dcc[i].type == &DCC_LOST) {
304 fabian 1.14 dcc[i].type = NULL;
305 fabian 1.12 dcc[i].sock = (-1);
306     removedcc(i);
307     i--;
308     }
309     }
310 segfault 1.1 }
311    
312 fabian 1.17 /* Show list of current dcc's to a dcc-chatter
313     * positive value: idx given -- negative value: sock given
314     */
315 segfault 1.1 void tell_dcc(int zidx)
316     {
317     int i, j, k;
318     char other[160];
319    
320     spaces[HANDLEN - 9] = 0;
321     dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
322     ,spaces);
323     dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
324     ,spaces);
325     spaces[HANDLEN - 9] = ' ';
326 fabian 1.17 /* Show server */
327 segfault 1.1 for (i = 0; i < dcc_total; i++) {
328     j = strlen(dcc[i].host);
329     if (j > 17)
330     j -= 17;
331     else
332     j = 0;
333     if (dcc[i].type && dcc[i].type->display)
334     dcc[i].type->display(i, other);
335     else {
336     sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
337     break;
338     }
339     k = HANDLEN - strlen(dcc[i].nick);
340     spaces[k] = 0;
341 fabian 1.14 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
342 segfault 1.1 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
343     spaces[k] = ' ';
344     }
345     }
346    
347 fabian 1.17 /* Mark someone on dcc chat as no longer away
348     */
349 segfault 1.1 void not_away(int idx)
350     {
351     if (dcc[idx].u.chat->away == NULL) {
352     dprintf(idx, "You weren't away!\n");
353     return;
354     }
355     if (dcc[idx].u.chat->channel >= 0) {
356     chanout_but(-1, dcc[idx].u.chat->channel,
357     "*** %s is no longer away.\n", dcc[idx].nick);
358     if (dcc[idx].u.chat->channel < 100000) {
359     botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
360     }
361     }
362     dprintf(idx, "You're not away any more.\n");
363     nfree(dcc[idx].u.chat->away);
364     dcc[idx].u.chat->away = NULL;
365     check_tcl_away(botnetnick, dcc[idx].sock, NULL);
366     }
367    
368     void set_away(int idx, char *s)
369     {
370     if (s == NULL) {
371     not_away(idx);
372     return;
373     }
374     if (!s[0]) {
375     not_away(idx);
376     return;
377     }
378     if (dcc[idx].u.chat->away != NULL)
379     nfree(dcc[idx].u.chat->away);
380     dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
381     strcpy(dcc[idx].u.chat->away, s);
382     if (dcc[idx].u.chat->channel >= 0) {
383     chanout_but(-1, dcc[idx].u.chat->channel,
384     "*** %s is now away: %s\n", dcc[idx].nick, s);
385     if (dcc[idx].u.chat->channel < 100000) {
386     botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
387     }
388     }
389     dprintf(idx, "You are now away.\n");
390     check_tcl_away(botnetnick, dcc[idx].sock, s);
391     }
392    
393 fabian 1.17 /* This helps the memory debugging
394     */
395 segfault 1.1 void *_get_data_ptr(int size, char *file, int line)
396     {
397     char *p;
398 fabian 1.9 #ifdef DEBUG_MEM
399 segfault 1.1 char x[1024];
400    
401 fabian 1.20 p = strrchr(file, '/');
402 guppy 1.24 egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
403 segfault 1.1 p = n_malloc(size, x, line);
404     #else
405     p = nmalloc(size);
406     #endif
407 fabian 1.19 egg_bzero(p, size);
408 segfault 1.1 return p;
409     }
410    
411 fabian 1.17 /* Make a password, 10-15 random letters and digits
412     */
413 segfault 1.1 void makepass(char *s)
414     {
415 fabian 1.2 int i;
416 segfault 1.1
417     i = 10 + (random() % 6);
418 fabian 1.2 make_rand_str(s, i);
419 segfault 1.1 }
420    
421     void flush_lines(int idx, struct chat_info *ci)
422     {
423     int c = ci->line_count;
424     struct msgq *p = ci->buffer, *o;
425    
426     while (p && c < (ci->max_line)) {
427     ci->current_lines--;
428     tputs(dcc[idx].sock, p->msg, p->len);
429     nfree(p->msg);
430     o = p->next;
431     nfree(p);
432     p = o;
433     c++;
434     }
435     if (p != NULL) {
436     if (dcc[idx].status & STAT_TELNET)
437     tputs(dcc[idx].sock, "[More]: ", 8);
438     else
439     tputs(dcc[idx].sock, "[More]\n", 7);
440     }
441     ci->buffer = p;
442     ci->line_count = 0;
443     }
444    
445     int new_dcc(struct dcc_table *type, int xtra_size)
446     {
447     int i = dcc_total;
448    
449     if (dcc_total == max_dcc)
450     return -1;
451     dcc_total++;
452 fabian 1.19 egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
453 segfault 1.1
454     dcc[i].type = type;
455     if (xtra_size) {
456     dcc[i].u.other = nmalloc(xtra_size);
457 fabian 1.19 egg_bzero(dcc[i].u.other, xtra_size);
458 segfault 1.1 }
459     return i;
460     }
461    
462 fabian 1.22 /* Changes the given dcc entry to another type.
463     */
464 fabian 1.3 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
465     {
466 fabian 1.22 /* Free old structure. */
467 fabian 1.3 if (dcc[i].type && dcc[i].type->kill)
468     dcc[i].type->kill(i, dcc[i].u.other);
469     else if (dcc[i].u.other) {
470     nfree(dcc[i].u.other);
471     dcc[i].u.other = NULL;
472     }
473    
474     dcc[i].type = type;
475     if (xtra_size) {
476     dcc[i].u.other = nmalloc(xtra_size);
477 fabian 1.19 egg_bzero(dcc[i].u.other, xtra_size);
478 fabian 1.3 }
479     }
480    
481 segfault 1.1 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
482     {
483     time_t t;
484    
485 guppy 1.24 if (!dcc_flood_thr)
486 segfault 1.1 return 0;
487     t = now;
488     if (*timer != t) {
489     *timer = t;
490     chat->msgs_per_sec = 0;
491     } else {
492     chat->msgs_per_sec++;
493     if (chat->msgs_per_sec > dcc_flood_thr) {
494     /* FLOOD */
495     dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
496 fabian 1.17 /* Evil assumption here that flags&DCT_CHAT implies chat type */
497 segfault 1.1 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
498     (chat->channel >= 0)) {
499     char x[1024];
500    
501 guppy 1.24 egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
502 segfault 1.1 chanout_but(idx, chat->channel, "*** %s", x);
503     if (chat->channel < 100000)
504     botnet_send_part_idx(idx, x);
505     }
506     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
507     if ((dcc[idx].sock != STDOUT) || backgrd) {
508     killsock(dcc[idx].sock);
509     lostdcc(idx);
510     } else {
511     dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
512     dcc_chatter(idx);
513     }
514     return 1; /* <- flood */
515     }
516     }
517     return 0;
518     }
519    
520 fabian 1.17 /* Handle someone being booted from dcc chat.
521     */
522 segfault 1.1 void do_boot(int idx, char *by, char *reason)
523     {
524     int files = (dcc[idx].type != &DCC_CHAT);
525    
526     dprintf(idx, DCC_BOOTED1);
527 fabian 1.23 dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot",
528     by, reason[0] ? ": " : ".", reason);
529 fabian 1.17 /* If it's a partyliner (chatterer :) */
530     /* Horrible assumption that DCT_CHAT using structure uses same format
531 segfault 1.1 * as DCC_CHAT */
532     if ((dcc[idx].type->flags & DCT_CHAT) &&
533     (dcc[idx].u.chat->channel >= 0)) {
534     char x[1024];
535    
536 guppy 1.24 egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick,
537     reason[0] ? ": " : "", reason);
538 segfault 1.1 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
539     if (dcc[idx].u.chat->channel < 100000)
540     botnet_send_part_idx(idx, x);
541     }
542     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
543     if ((dcc[idx].sock != STDOUT) || backgrd) {
544     killsock(dcc[idx].sock);
545 fabian 1.11 lostdcc(idx);
546 fabian 1.17 /* Entry must remain in the table so it can be logged by the caller */
547 segfault 1.1 } else {
548     dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
549     dcc_chatter(idx);
550     }
551     return;
552     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23