/[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.6 - (hide annotations) (download) (as text)
Wed Nov 10 21:35:03 1999 UTC (19 years, 11 months ago) by fabian
Branch: MAIN
Changes since 1.5: +0 -1 lines
File MIME type: text/x-chdr
Resync with 1.4

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23