/[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.13 - (hide annotations) (download) (as text)
Fri Dec 24 14:23:01 1999 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.12: +3 -2 lines
File MIME type: text/x-chdr
1.4 hidelost

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.13 * $Id: dccutil.c,v 1.12 1999/12/24 14:21:53 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 fabian 1.13 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", (dcc[i].type == &DCC_LOST) ?
336     (-1) : dcc[i].sock, dcc[i].addr,
337 segfault 1.1 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
338     spaces[k] = ' ';
339     }
340     }
341    
342     /* mark someone on dcc chat as no longer away */
343     void not_away(int idx)
344     {
345 fabian 1.9 Context;
346 segfault 1.1 if (dcc[idx].u.chat->away == NULL) {
347     dprintf(idx, "You weren't away!\n");
348     return;
349     }
350     if (dcc[idx].u.chat->channel >= 0) {
351     chanout_but(-1, dcc[idx].u.chat->channel,
352     "*** %s is no longer away.\n", dcc[idx].nick);
353 fabian 1.9 Context;
354 segfault 1.1 if (dcc[idx].u.chat->channel < 100000) {
355     botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
356     }
357     }
358     dprintf(idx, "You're not away any more.\n");
359     nfree(dcc[idx].u.chat->away);
360     dcc[idx].u.chat->away = NULL;
361     check_tcl_away(botnetnick, dcc[idx].sock, NULL);
362     }
363    
364     void set_away(int idx, char *s)
365     {
366     if (s == NULL) {
367     not_away(idx);
368     return;
369     }
370     if (!s[0]) {
371     not_away(idx);
372     return;
373     }
374     if (dcc[idx].u.chat->away != NULL)
375     nfree(dcc[idx].u.chat->away);
376     dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
377     strcpy(dcc[idx].u.chat->away, s);
378     if (dcc[idx].u.chat->channel >= 0) {
379     chanout_but(-1, dcc[idx].u.chat->channel,
380     "*** %s is now away: %s\n", dcc[idx].nick, s);
381 fabian 1.9 Context;
382 segfault 1.1 if (dcc[idx].u.chat->channel < 100000) {
383     botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
384     }
385     }
386     dprintf(idx, "You are now away.\n");
387     check_tcl_away(botnetnick, dcc[idx].sock, s);
388     }
389    
390     /* this helps the memory debugging */
391     void *_get_data_ptr(int size, char *file, int line)
392     {
393     char *p;
394    
395 fabian 1.9 #ifdef DEBUG_MEM
396 segfault 1.1 char x[1024];
397    
398     simple_sprintf(x, "dccutil.c:%s", file);
399     p = n_malloc(size, x, line);
400     #else
401     p = nmalloc(size);
402     #endif
403     bzero(p, size);
404     return p;
405     }
406    
407     /* make a password, 10-15 random letters and digits */
408     void makepass(char *s)
409     {
410 fabian 1.2 int i;
411 segfault 1.1
412     i = 10 + (random() % 6);
413 fabian 1.2 make_rand_str(s, i);
414 segfault 1.1 }
415    
416     void flush_lines(int idx, struct chat_info *ci)
417     {
418     int c = ci->line_count;
419     struct msgq *p = ci->buffer, *o;
420    
421     while (p && c < (ci->max_line)) {
422     ci->current_lines--;
423     tputs(dcc[idx].sock, p->msg, p->len);
424     nfree(p->msg);
425     o = p->next;
426     nfree(p);
427     p = o;
428     c++;
429     }
430     if (p != NULL) {
431     if (dcc[idx].status & STAT_TELNET)
432     tputs(dcc[idx].sock, "[More]: ", 8);
433     else
434     tputs(dcc[idx].sock, "[More]\n", 7);
435     }
436     ci->buffer = p;
437     ci->line_count = 0;
438     }
439    
440     int new_dcc(struct dcc_table *type, int xtra_size)
441     {
442     int i = dcc_total;
443    
444     if (dcc_total == max_dcc)
445     return -1;
446     dcc_total++;
447     bzero((char *) &dcc[i], sizeof(struct dcc_t));
448    
449     dcc[i].type = type;
450     if (xtra_size) {
451     dcc[i].u.other = nmalloc(xtra_size);
452     bzero(dcc[i].u.other, xtra_size);
453     }
454     return i;
455    
456     }
457    
458 fabian 1.3 /* Changes the given dcc entry to another type */
459     void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
460     {
461     /* free old structure */
462     if (dcc[i].type && dcc[i].type->kill)
463     dcc[i].type->kill(i, dcc[i].u.other);
464     else if (dcc[i].u.other) {
465     nfree(dcc[i].u.other);
466     dcc[i].u.other = NULL;
467     }
468    
469     dcc[i].type = type;
470     if (xtra_size) {
471     dcc[i].u.other = nmalloc(xtra_size);
472     bzero(dcc[i].u.other, xtra_size);
473     }
474     }
475    
476 segfault 1.1 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
477     {
478     time_t t;
479    
480     if (dcc_flood_thr == 0)
481     return 0;
482     t = now;
483     if (*timer != t) {
484     *timer = t;
485     chat->msgs_per_sec = 0;
486     } else {
487     chat->msgs_per_sec++;
488     if (chat->msgs_per_sec > dcc_flood_thr) {
489     /* FLOOD */
490     dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
491     /* evil assumption here that flags&DCT_CHAT implies chat type */
492     if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
493     (chat->channel >= 0)) {
494     char x[1024];
495    
496     simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
497     chanout_but(idx, chat->channel, "*** %s", x);
498     if (chat->channel < 100000)
499     botnet_send_part_idx(idx, x);
500     }
501     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
502     if ((dcc[idx].sock != STDOUT) || backgrd) {
503     killsock(dcc[idx].sock);
504     lostdcc(idx);
505     } else {
506     dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
507     dcc_chatter(idx);
508     }
509     return 1; /* <- flood */
510     }
511     }
512     return 0;
513     }
514    
515     /* handle someone being booted from dcc chat */
516     void do_boot(int idx, char *by, char *reason)
517     {
518     int files = (dcc[idx].type != &DCC_CHAT);
519    
520     dprintf(idx, DCC_BOOTED1);
521     dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
522     /* if it's a partyliner (chatterer :) */
523     /* horrible assumption that DCT_CHAT using structure uses same format
524     * as DCC_CHAT */
525     if ((dcc[idx].type->flags & DCT_CHAT) &&
526     (dcc[idx].u.chat->channel >= 0)) {
527     char x[1024];
528    
529     simple_sprintf(x, "%s booted %s from the party line%s%s",
530     by, dcc[idx].nick, reason[0] ? ": " : "", reason);
531     chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
532     if (dcc[idx].u.chat->channel < 100000)
533     botnet_send_part_idx(idx, x);
534     }
535     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
536     if ((dcc[idx].sock != STDOUT) || backgrd) {
537     killsock(dcc[idx].sock);
538 fabian 1.11 lostdcc(idx);
539 segfault 1.1 /* entry must remain in the table so it can be logged by the caller */
540     } else {
541     dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
542     dcc_chatter(idx);
543     }
544     return;
545     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23