/[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.12 - (hide annotations) (download) (as text)
Fri Dec 24 14:21:53 1999 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.11: +18 -1 lines
File MIME type: text/x-chdr
dccremovelost

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23