/[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.18 - (hide annotations) (download) (as text)
Wed Mar 22 00:42:57 2000 UTC (19 years, 7 months ago) by fabian
Branch: MAIN
Changes since 1.17: +4 -16 lines
File MIME type: text/x-chdr
configure_makefile_fix3 patch

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 fabian 1.18 * $Id: dccutil.c,v 1.17 2000/01/30 19:26:20 fabian 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     int reserved_port = 0;
47 segfault 1.1
48    
49     void init_dcc_max()
50     {
51     int osock = MAXSOCKS;
52    
53     if (max_dcc < 1)
54     max_dcc = 1;
55     if (dcc)
56     dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
57     else
58     dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
59    
60     MAXSOCKS = max_dcc + 10;
61     if (socklist)
62     socklist = (sock_list *) nrealloc((void *) socklist,
63     sizeof(sock_list) * MAXSOCKS);
64     else
65     socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
66     for (; osock < MAXSOCKS; osock++)
67     socklist[osock].flags = SOCK_UNUSED;
68     }
69    
70     int expmem_dccutil()
71     {
72     int tot, i;
73    
74 fabian 1.9 Context;
75 segfault 1.1 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     dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
199     show_motd(idx);
200     dprintf(idx, "Commands start with '.' (like '.quit' or '.help')\n");
201     dprintf(idx, "Everything else goes out to the party line.\n\n");
202     i = dcc[idx].u.chat->channel;
203     dcc[idx].u.chat->channel = 234567;
204     j = dcc[idx].sock;
205     strcpy(dcc[idx].u.chat->con_chan, "***");
206     check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
207 fabian 1.17 /* Still there? */
208 segfault 1.1 if ((idx >= dcc_total) || (dcc[idx].sock != j))
209 fabian 1.17 return; /* Nope */
210     /* Tcl script may have taken control */
211 segfault 1.1 if (dcc[idx].type == &DCC_CHAT) {
212     if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
213     strcpy(dcc[idx].u.chat->con_chan, "*");
214     if (dcc[idx].u.chat->channel == 234567) {
215 fabian 1.17 /* If the chat channel has already been altered it's *highly*
216 segfault 1.1 * probably join/part messages have been broadcast everywhere,
217 fabian 1.17 * so dont bother sending them
218     */
219 segfault 1.1 if (i == -2)
220     i = 0;
221     dcc[idx].u.chat->channel = i;
222     if (dcc[idx].u.chat->channel >= 0) {
223 fabian 1.9 Context;
224 segfault 1.1 if (dcc[idx].u.chat->channel < 100000) {
225     botnet_send_join_idx(idx, -1);
226     }
227     }
228     check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
229     geticon(idx), dcc[idx].sock, dcc[idx].host);
230     }
231 fabian 1.17 /* But *do* bother with sending it locally */
232 segfault 1.1 if (dcc[idx].u.chat->channel == 0) {
233     chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
234     } else if (dcc[idx].u.chat->channel > 0) {
235     chanout_but(-1, dcc[idx].u.chat->channel,
236     "*** %s joined the channel.\n", dcc[idx].nick);
237     }
238     }
239     }
240    
241 fabian 1.14 /* Mark an entry as lost and deconstruct it's contents. It will be securely
242     * removed from the dcc list in the main loop.
243     */
244 segfault 1.1 void lostdcc(int n)
245     {
246 fabian 1.14 if (dcc[n].type && dcc[n].type->kill)
247     dcc[n].type->kill(n, dcc[n].u.other);
248     else if (dcc[n].u.other)
249     nfree(dcc[n].u.other);
250     bzero(&dcc[n], sizeof(struct dcc_t));
251    
252     dcc[n].sock = (-1);
253 fabian 1.11 dcc[n].type = &DCC_LOST;
254     }
255    
256     /* Remove entry from dcc list. Think twice before using this function,
257     * because it invalidates any variables that point to a specific dcc
258     * entry!
259 fabian 1.17 *
260     * Note: The entry will be deconstructed if it was not deconstructed
261     * already. This case should normally not occur.
262 fabian 1.11 */
263     void removedcc(int n)
264     {
265 segfault 1.1 if (dcc[n].type && dcc[n].type->kill)
266     dcc[n].type->kill(n, dcc[n].u.other);
267     else if (dcc[n].u.other)
268     nfree(dcc[n].u.other);
269     dcc_total--;
270     if (n < dcc_total)
271     my_memcpy((char *) &dcc[n], (char *) &dcc[dcc_total],
272     sizeof(struct dcc_t));
273     else
274     bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
275 fabian 1.12 }
276    
277     /* Clean up sockets that were just left for dead.
278     */
279     void dcc_remove_lost(void)
280     {
281     int i;
282    
283     Context;
284     for (i = 0; i < dcc_total; i++) {
285     if (dcc[i].type == &DCC_LOST) {
286 fabian 1.14 dcc[i].type = NULL;
287 fabian 1.12 dcc[i].sock = (-1);
288     removedcc(i);
289     i--;
290     }
291     }
292 segfault 1.1 }
293    
294 fabian 1.17 /* Show list of current dcc's to a dcc-chatter
295     * positive value: idx given -- negative value: sock given
296     */
297 segfault 1.1 void tell_dcc(int zidx)
298     {
299     int i, j, k;
300     char other[160];
301    
302 fabian 1.9 Context;
303 segfault 1.1 spaces[HANDLEN - 9] = 0;
304     dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
305     ,spaces);
306     dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
307     ,spaces);
308     spaces[HANDLEN - 9] = ' ';
309 fabian 1.17 /* Show server */
310 segfault 1.1 for (i = 0; i < dcc_total; i++) {
311     j = strlen(dcc[i].host);
312     if (j > 17)
313     j -= 17;
314     else
315     j = 0;
316     if (dcc[i].type && dcc[i].type->display)
317     dcc[i].type->display(i, other);
318     else {
319     sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
320     break;
321     }
322     k = HANDLEN - strlen(dcc[i].nick);
323     spaces[k] = 0;
324 fabian 1.14 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
325 segfault 1.1 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
326     spaces[k] = ' ';
327     }
328     }
329    
330 fabian 1.17 /* Mark someone on dcc chat as no longer away
331     */
332 segfault 1.1 void not_away(int idx)
333     {
334 fabian 1.9 Context;
335 segfault 1.1 if (dcc[idx].u.chat->away == NULL) {
336     dprintf(idx, "You weren't away!\n");
337     return;
338     }
339     if (dcc[idx].u.chat->channel >= 0) {
340     chanout_but(-1, dcc[idx].u.chat->channel,
341     "*** %s is no longer away.\n", dcc[idx].nick);
342 fabian 1.9 Context;
343 segfault 1.1 if (dcc[idx].u.chat->channel < 100000) {
344     botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
345     }
346     }
347     dprintf(idx, "You're not away any more.\n");
348     nfree(dcc[idx].u.chat->away);
349     dcc[idx].u.chat->away = NULL;
350     check_tcl_away(botnetnick, dcc[idx].sock, NULL);
351     }
352    
353     void set_away(int idx, char *s)
354     {
355     if (s == NULL) {
356     not_away(idx);
357     return;
358     }
359     if (!s[0]) {
360     not_away(idx);
361     return;
362     }
363     if (dcc[idx].u.chat->away != NULL)
364     nfree(dcc[idx].u.chat->away);
365     dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
366     strcpy(dcc[idx].u.chat->away, s);
367     if (dcc[idx].u.chat->channel >= 0) {
368     chanout_but(-1, dcc[idx].u.chat->channel,
369     "*** %s is now away: %s\n", dcc[idx].nick, s);
370 fabian 1.9 Context;
371 segfault 1.1 if (dcc[idx].u.chat->channel < 100000) {
372     botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
373     }
374     }
375     dprintf(idx, "You are now away.\n");
376     check_tcl_away(botnetnick, dcc[idx].sock, s);
377     }
378    
379 fabian 1.17 /* This helps the memory debugging
380     */
381 segfault 1.1 void *_get_data_ptr(int size, char *file, int line)
382     {
383     char *p;
384 fabian 1.9 #ifdef DEBUG_MEM
385 segfault 1.1 char x[1024];
386    
387     simple_sprintf(x, "dccutil.c:%s", file);
388     p = n_malloc(size, x, line);
389     #else
390     p = nmalloc(size);
391     #endif
392     bzero(p, size);
393     return p;
394     }
395    
396 fabian 1.17 /* Make a password, 10-15 random letters and digits
397     */
398 segfault 1.1 void makepass(char *s)
399     {
400 fabian 1.2 int i;
401 segfault 1.1
402     i = 10 + (random() % 6);
403 fabian 1.2 make_rand_str(s, i);
404 segfault 1.1 }
405    
406     void flush_lines(int idx, struct chat_info *ci)
407     {
408     int c = ci->line_count;
409     struct msgq *p = ci->buffer, *o;
410    
411     while (p && c < (ci->max_line)) {
412     ci->current_lines--;
413     tputs(dcc[idx].sock, p->msg, p->len);
414     nfree(p->msg);
415     o = p->next;
416     nfree(p);
417     p = o;
418     c++;
419     }
420     if (p != NULL) {
421     if (dcc[idx].status & STAT_TELNET)
422     tputs(dcc[idx].sock, "[More]: ", 8);
423     else
424     tputs(dcc[idx].sock, "[More]\n", 7);
425     }
426     ci->buffer = p;
427     ci->line_count = 0;
428     }
429    
430     int new_dcc(struct dcc_table *type, int xtra_size)
431     {
432     int i = dcc_total;
433    
434     if (dcc_total == max_dcc)
435     return -1;
436     dcc_total++;
437     bzero((char *) &dcc[i], sizeof(struct dcc_t));
438    
439     dcc[i].type = type;
440     if (xtra_size) {
441     dcc[i].u.other = nmalloc(xtra_size);
442     bzero(dcc[i].u.other, xtra_size);
443     }
444     return i;
445     }
446    
447 fabian 1.3 /* Changes the given dcc entry to another type */
448     void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
449     {
450 fabian 1.17 /* Free old structure */
451 fabian 1.3 if (dcc[i].type && dcc[i].type->kill)
452     dcc[i].type->kill(i, dcc[i].u.other);
453     else if (dcc[i].u.other) {
454     nfree(dcc[i].u.other);
455     dcc[i].u.other = NULL;
456     }
457    
458     dcc[i].type = type;
459     if (xtra_size) {
460     dcc[i].u.other = nmalloc(xtra_size);
461     bzero(dcc[i].u.other, xtra_size);
462     }
463     }
464    
465 segfault 1.1 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
466     {
467     time_t t;
468    
469     if (dcc_flood_thr == 0)
470     return 0;
471     t = now;
472     if (*timer != t) {
473     *timer = t;
474     chat->msgs_per_sec = 0;
475     } else {
476     chat->msgs_per_sec++;
477     if (chat->msgs_per_sec > dcc_flood_thr) {
478     /* FLOOD */
479     dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
480 fabian 1.17 /* Evil assumption here that flags&DCT_CHAT implies chat type */
481 segfault 1.1 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
482     (chat->channel >= 0)) {
483     char x[1024];
484    
485     simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
486     chanout_but(idx, chat->channel, "*** %s", x);
487     if (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     lostdcc(idx);
494     } else {
495     dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
496     dcc_chatter(idx);
497     }
498     return 1; /* <- flood */
499     }
500     }
501     return 0;
502     }
503    
504 fabian 1.17 /* Handle someone being booted from dcc chat.
505     */
506 segfault 1.1 void do_boot(int idx, char *by, char *reason)
507     {
508     int files = (dcc[idx].type != &DCC_CHAT);
509    
510     dprintf(idx, DCC_BOOTED1);
511     dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
512 fabian 1.17 /* If it's a partyliner (chatterer :) */
513     /* Horrible assumption that DCT_CHAT using structure uses same format
514 segfault 1.1 * as DCC_CHAT */
515     if ((dcc[idx].type->flags & DCT_CHAT) &&
516     (dcc[idx].u.chat->channel >= 0)) {
517     char x[1024];
518    
519     simple_sprintf(x, "%s booted %s from the party line%s%s",
520     by, dcc[idx].nick, reason[0] ? ": " : "", reason);
521     chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
522     if (dcc[idx].u.chat->channel < 100000)
523     botnet_send_part_idx(idx, x);
524     }
525     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
526     if ((dcc[idx].sock != STDOUT) || backgrd) {
527     killsock(dcc[idx].sock);
528 fabian 1.11 lostdcc(idx);
529 fabian 1.17 /* Entry must remain in the table so it can be logged by the caller */
530 segfault 1.1 } else {
531     dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
532     dcc_chatter(idx);
533     }
534     return;
535     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23