/[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.27 - (hide annotations) (download) (as text)
Sat Jan 27 06:26:40 2001 UTC (18 years, 9 months ago) by tothwolf
Branch: MAIN
Changes since 1.26: +1 -16 lines
File MIME type: text/x-chdr
*** empty log message ***

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23