/[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.40 - (hide annotations) (download) (as text)
Sun Oct 21 20:59:49 2001 UTC (18 years ago) by stdarg
Branch: MAIN
Changes since 1.39: +17 -12 lines
File MIME type: text/x-chdr
* dcc array is now sparse

1 guppy 1.31 /*
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 guppy 1.31 *
9 stdarg 1.40 * $Id: dccutil.c,v 1.39 2001/10/19 01:55:05 tothwolf Exp $
10 segfault 1.1 */
11 guppy 1.31 /*
12     * Copyright (C) 1997 Robey Pointer
13     * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
14     *
15 fabian 1.10 * 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 guppy 1.31 *
20 fabian 1.10 * 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 guppy 1.31 *
25 fabian 1.10 * 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 guppy 1.28 extern Tcl_Interp *interp;
43 fabian 1.17
44     char motdfile[121] = "text/motd"; /* File where the motd is stored */
45     int connect_timeout = 15; /* How long to wait before a telnet
46     connection times out */
47 segfault 1.1
48 guppy 1.25 int reserved_port_min = 0;
49     int reserved_port_max = 0;
50 segfault 1.1
51     void init_dcc_max()
52     {
53     int osock = MAXSOCKS;
54    
55     if (max_dcc < 1)
56     max_dcc = 1;
57     if (dcc)
58 tothwolf 1.37 dcc = realloc(dcc, sizeof(struct dcc_t) * max_dcc);
59 segfault 1.1 else
60 tothwolf 1.37 dcc = malloc(sizeof(struct dcc_t) * max_dcc);
61 segfault 1.1
62     MAXSOCKS = max_dcc + 10;
63     if (socklist)
64 tothwolf 1.37 socklist = (sock_list *) realloc((void *) socklist,
65 segfault 1.1 sizeof(sock_list) * MAXSOCKS);
66     else
67 tothwolf 1.37 socklist = (sock_list *) malloc(sizeof(sock_list) * MAXSOCKS);
68 segfault 1.1 for (; osock < MAXSOCKS; osock++)
69     socklist[osock].flags = SOCK_UNUSED;
70     }
71    
72    
73 fabian 1.17 /* Replace \n with \r\n */
74 segfault 1.1 char *add_cr(char *buf)
75     {
76     static char WBUF[1024];
77     char *p, *q;
78    
79     for (p = buf, q = WBUF; *p; p++, q++) {
80     if (*p == '\n')
81     *q++ = '\r';
82     *q = *p;
83     }
84     *q = *p;
85     return WBUF;
86     }
87    
88     extern void (*qserver) (int, char *, int);
89 fabian 1.7
90 fabian 1.5 void dprintf EGG_VARARGS_DEF(int, arg1)
91 segfault 1.1 {
92 guppy 1.28 static char buf[1024];
93 segfault 1.1 char *format;
94     int idx, len;
95 fabian 1.17 va_list va;
96 segfault 1.1
97 fabian 1.5 idx = EGG_VARARGS_START(int, arg1, va);
98 segfault 1.1 format = va_arg(va, char *);
99 tothwolf 1.39 vsnprintf(buf, 1023, format, va);
100 segfault 1.1 va_end(va);
101 guppy 1.28 /* We can not use the return value vsnprintf() to determine where
102     * to null terminate. The C99 standard specifies that vsnprintf()
103     * shall return the number of bytes that would be written if the
104     * buffer had been large enough, rather then -1.
105     */
106 poptix 1.32 /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
107     * sizeof(buf) bytes. But we're not doing that anyway.
108     */
109     buf[sizeof(buf)-1] = 0;
110 guppy 1.28
111 guppy 1.30 #if (TCL_MAJOR_VERSION >= 8 && TCL_MINOR_VERSION >= 1) || (TCL_MAJOR_VERSION >= 9)
112 guppy 1.29 str_nutf8tounicode(buf, sizeof buf);
113 guppy 1.28 #endif
114 poptix 1.32 len = strlen(buf);
115 guppy 1.28
116 segfault 1.1 if (idx < 0) {
117 guppy 1.28 tputs(-idx, buf, len);
118 segfault 1.1 } else if (idx > 0x7FF0) {
119     switch (idx) {
120     case DP_LOG:
121 guppy 1.28 putlog(LOG_MISC, "*", "%s", buf);
122 segfault 1.1 break;
123     case DP_STDOUT:
124 guppy 1.28 tputs(STDOUT, buf, len);
125 segfault 1.1 break;
126     case DP_STDERR:
127 guppy 1.28 tputs(STDERR, buf, len);
128 segfault 1.1 break;
129     case DP_SERVER:
130     case DP_HELP:
131     case DP_MODE:
132 fabian 1.8 case DP_MODE_NEXT:
133     case DP_SERVER_NEXT:
134     case DP_HELP_NEXT:
135 guppy 1.28 qserver(idx, buf, len);
136 segfault 1.1 break;
137     }
138     return;
139     } else {
140 fabian 1.17 if (len > 500) { /* Truncate to fit */
141 guppy 1.28 buf[500] = 0;
142     strcat(buf, "\n");
143 segfault 1.1 len = 501;
144     }
145     if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
146 guppy 1.28 char *p = add_cr(buf);
147 segfault 1.1
148     tputs(dcc[idx].sock, p, strlen(p));
149     } else if (dcc[idx].type && dcc[idx].type->output) {
150 guppy 1.28 dcc[idx].type->output(idx, buf, dcc[idx].u.other);
151 segfault 1.1 } else
152 guppy 1.28 tputs(dcc[idx].sock, buf, len);
153 segfault 1.1 }
154     }
155    
156 fabian 1.5 void chatout EGG_VARARGS_DEF(char *, arg1)
157 segfault 1.1 {
158 guppy 1.28 int i, len;
159 segfault 1.1 char *format;
160     char s[601];
161 fabian 1.17 va_list va;
162 segfault 1.1
163 fabian 1.5 format = EGG_VARARGS_START(char *, arg1, va);
164 tothwolf 1.39 vsnprintf(s, 511, format, va);
165 guppy 1.28 va_end(va);
166     len = strlen(s);
167     if (len > 511)
168     len = 511;
169 guppy 1.31 s[len + 1] = 0;
170 guppy 1.28
171 segfault 1.1 for (i = 0; i < dcc_total; i++)
172     if (dcc[i].type == &DCC_CHAT)
173     if (dcc[i].u.chat->channel >= 0)
174 guppy 1.28 dprintf(i, "%s", s);
175 guppy 1.31
176 segfault 1.1 }
177    
178 fabian 1.17 /* Print to all on this channel but one.
179     */
180 fabian 1.5 void chanout_but EGG_VARARGS_DEF(int, arg1)
181 segfault 1.1 {
182 guppy 1.28 int i, x, chan, len;
183 segfault 1.1 char *format;
184     char s[601];
185 fabian 1.17 va_list va;
186 segfault 1.1
187 fabian 1.5 x = EGG_VARARGS_START(int, arg1, va);
188 segfault 1.1 chan = va_arg(va, int);
189     format = va_arg(va, char *);
190 tothwolf 1.39 vsnprintf(s, 511, format, va);
191 guppy 1.28 va_end(va);
192     len = strlen(s);
193     if (len > 511)
194     len = 511;
195 guppy 1.31 s[len + 1] = 0;
196    
197 segfault 1.1 for (i = 0; i < dcc_total; i++)
198     if ((dcc[i].type == &DCC_CHAT) && (i != x))
199     if (dcc[i].u.chat->channel == chan)
200 guppy 1.28 dprintf(i, "%s", s);
201 guppy 1.31
202 segfault 1.1 }
203    
204     void dcc_chatter(int idx)
205     {
206     int i, j;
207 fabian 1.17 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
208 segfault 1.1
209     get_user_flagrec(dcc[idx].user, &fr, NULL);
210     show_motd(idx);
211     i = dcc[idx].u.chat->channel;
212     dcc[idx].u.chat->channel = 234567;
213     j = dcc[idx].sock;
214     strcpy(dcc[idx].u.chat->con_chan, "***");
215     check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
216 fabian 1.17 /* Still there? */
217 segfault 1.1 if ((idx >= dcc_total) || (dcc[idx].sock != j))
218 fabian 1.17 return; /* Nope */
219     /* Tcl script may have taken control */
220 segfault 1.1 if (dcc[idx].type == &DCC_CHAT) {
221     if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
222     strcpy(dcc[idx].u.chat->con_chan, "*");
223     if (dcc[idx].u.chat->channel == 234567) {
224 fabian 1.17 /* If the chat channel has already been altered it's *highly*
225 segfault 1.1 * probably join/part messages have been broadcast everywhere,
226 fabian 1.17 * so dont bother sending them
227     */
228 segfault 1.1 if (i == -2)
229     i = 0;
230     dcc[idx].u.chat->channel = i;
231     if (dcc[idx].u.chat->channel >= 0) {
232     if (dcc[idx].u.chat->channel < 100000) {
233     botnet_send_join_idx(idx, -1);
234     }
235     }
236     check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
237     geticon(idx), dcc[idx].sock, dcc[idx].host);
238     }
239 fabian 1.17 /* But *do* bother with sending it locally */
240 guppy 1.24 if (!dcc[idx].u.chat->channel) {
241 segfault 1.1 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
242     } else if (dcc[idx].u.chat->channel > 0) {
243     chanout_but(-1, dcc[idx].u.chat->channel,
244     "*** %s joined the channel.\n", dcc[idx].nick);
245     }
246     }
247     }
248    
249 fabian 1.14 /* Mark an entry as lost and deconstruct it's contents. It will be securely
250     * removed from the dcc list in the main loop.
251     */
252 segfault 1.1 void lostdcc(int n)
253     {
254 fabian 1.14 if (dcc[n].type && dcc[n].type->kill)
255     dcc[n].type->kill(n, dcc[n].u.other);
256     else if (dcc[n].u.other)
257 tothwolf 1.37 free(dcc[n].u.other);
258     memset(&dcc[n], 0, sizeof(struct dcc_t));
259 fabian 1.14
260     dcc[n].sock = (-1);
261 fabian 1.11 dcc[n].type = &DCC_LOST;
262     }
263    
264     /* Remove entry from dcc list. Think twice before using this function,
265     * because it invalidates any variables that point to a specific dcc
266     * entry!
267 fabian 1.17 *
268     * Note: The entry will be deconstructed if it was not deconstructed
269     * already. This case should normally not occur.
270 fabian 1.11 */
271     void removedcc(int n)
272     {
273 segfault 1.1 if (dcc[n].type && dcc[n].type->kill)
274     dcc[n].type->kill(n, dcc[n].u.other);
275     else if (dcc[n].u.other)
276 tothwolf 1.37 free(dcc[n].u.other);
277 stdarg 1.40
278     memset(&dcc[n], 0, sizeof(struct dcc_t)); /* drummer */
279 fabian 1.12 }
280    
281     /* Clean up sockets that were just left for dead.
282     */
283     void dcc_remove_lost(void)
284     {
285     int i;
286    
287     for (i = 0; i < dcc_total; i++) {
288     if (dcc[i].type == &DCC_LOST) {
289 fabian 1.14 dcc[i].type = NULL;
290 fabian 1.12 removedcc(i);
291     i--;
292     }
293     }
294 segfault 1.1 }
295    
296 fabian 1.17 /* Show list of current dcc's to a dcc-chatter
297     * positive value: idx given -- negative value: sock given
298     */
299 segfault 1.1 void tell_dcc(int zidx)
300     {
301     int i, j, k;
302     char other[160];
303    
304     spaces[HANDLEN - 9] = 0;
305 drummer 1.33 dprintf(zidx, "SOCK PORT NICK %s HOST TYPE\n"
306 segfault 1.1 ,spaces);
307 drummer 1.33 dprintf(zidx, "---- ----- ---------%s -------------------------- ----\n"
308 segfault 1.1 ,spaces);
309     spaces[HANDLEN - 9] = ' ';
310 fabian 1.17 /* Show server */
311 segfault 1.1 for (i = 0; i < dcc_total; i++) {
312 stdarg 1.40 if (!dcc[i].type) continue;
313 segfault 1.1 j = strlen(dcc[i].host);
314 drummer 1.33 if (j > 26)
315     j -= 26;
316 segfault 1.1 else
317     j = 0;
318     if (dcc[i].type && dcc[i].type->display)
319     dcc[i].type->display(i, other);
320     else {
321     sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
322     break;
323     }
324     k = HANDLEN - strlen(dcc[i].nick);
325     spaces[k] = 0;
326 stdarg 1.40 dprintf(zidx, "%-4d %5d %s%s %-26s %s\n", i,
327 segfault 1.1 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
328     spaces[k] = ' ';
329     }
330     }
331    
332 fabian 1.17 /* Mark someone on dcc chat as no longer away
333     */
334 segfault 1.1 void not_away(int idx)
335     {
336     if (dcc[idx].u.chat->away == NULL) {
337     dprintf(idx, "You weren't away!\n");
338     return;
339     }
340     if (dcc[idx].u.chat->channel >= 0) {
341     chanout_but(-1, dcc[idx].u.chat->channel,
342     "*** %s is no longer away.\n", dcc[idx].nick);
343     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 tothwolf 1.37 free_null(dcc[idx].u.chat->away);
349 segfault 1.1 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
350     }
351    
352     void set_away(int idx, char *s)
353     {
354     if (s == NULL) {
355     not_away(idx);
356     return;
357     }
358     if (!s[0]) {
359     not_away(idx);
360     return;
361     }
362     if (dcc[idx].u.chat->away != NULL)
363 tothwolf 1.37 free(dcc[idx].u.chat->away);
364     malloc_strcpy(dcc[idx].u.chat->away, s);
365 segfault 1.1 if (dcc[idx].u.chat->channel >= 0) {
366     chanout_but(-1, dcc[idx].u.chat->channel,
367     "*** %s is now away: %s\n", dcc[idx].nick, s);
368     if (dcc[idx].u.chat->channel < 100000) {
369     botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
370     }
371     }
372     dprintf(idx, "You are now away.\n");
373     check_tcl_away(botnetnick, dcc[idx].sock, s);
374     }
375    
376 fabian 1.17 /* Make a password, 10-15 random letters and digits
377     */
378 segfault 1.1 void makepass(char *s)
379     {
380 fabian 1.2 int i;
381 segfault 1.1
382     i = 10 + (random() % 6);
383 fabian 1.2 make_rand_str(s, i);
384 segfault 1.1 }
385    
386     void flush_lines(int idx, struct chat_info *ci)
387     {
388     int c = ci->line_count;
389     struct msgq *p = ci->buffer, *o;
390    
391     while (p && c < (ci->max_line)) {
392     ci->current_lines--;
393     tputs(dcc[idx].sock, p->msg, p->len);
394 tothwolf 1.37 free(p->msg);
395 segfault 1.1 o = p->next;
396 tothwolf 1.37 free(p);
397 segfault 1.1 p = o;
398     c++;
399     }
400     if (p != NULL) {
401     if (dcc[idx].status & STAT_TELNET)
402     tputs(dcc[idx].sock, "[More]: ", 8);
403     else
404     tputs(dcc[idx].sock, "[More]\n", 7);
405     }
406     ci->buffer = p;
407     ci->line_count = 0;
408     }
409    
410     int new_dcc(struct dcc_table *type, int xtra_size)
411     {
412     int i = dcc_total;
413    
414 stdarg 1.40 for (i = 0; i < dcc_total; i++) {
415     if (!dcc[i].type) break;
416     }
417     if (i == dcc_total) {
418     if (dcc_total == max_dcc) {
419     max_dcc += 5;
420     dcc = (struct dcc_t *)realloc(dcc, max_dcc * sizeof(*dcc));
421     memset(dcc+dcc_total, 0, 5 * sizeof(*dcc));
422     }
423     dcc_total++;
424     }
425     memset(&dcc[i], 0, sizeof(*dcc));
426 segfault 1.1
427     dcc[i].type = type;
428 tothwolf 1.37 if (xtra_size)
429 tothwolf 1.38 dcc[i].u.other = calloc(1, xtra_size);
430 segfault 1.1 return i;
431     }
432    
433 fabian 1.22 /* Changes the given dcc entry to another type.
434     */
435 fabian 1.3 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
436     {
437 fabian 1.22 /* Free old structure. */
438 fabian 1.3 if (dcc[i].type && dcc[i].type->kill)
439     dcc[i].type->kill(i, dcc[i].u.other);
440 tothwolf 1.37 else if (dcc[i].u.other)
441     free_null(dcc[i].u.other);
442 fabian 1.3
443     dcc[i].type = type;
444 tothwolf 1.37 if (xtra_size)
445 tothwolf 1.38 dcc[i].u.other = calloc(1, xtra_size);
446 fabian 1.3 }
447    
448 segfault 1.1 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
449     {
450     time_t t;
451    
452 guppy 1.24 if (!dcc_flood_thr)
453 segfault 1.1 return 0;
454     t = now;
455     if (*timer != t) {
456     *timer = t;
457     chat->msgs_per_sec = 0;
458     } else {
459     chat->msgs_per_sec++;
460     if (chat->msgs_per_sec > dcc_flood_thr) {
461     /* FLOOD */
462 ite 1.34 dprintf(idx, "*** FLOOD: %s.\n", _("Goodbye"));
463 fabian 1.17 /* Evil assumption here that flags&DCT_CHAT implies chat type */
464 segfault 1.1 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
465     (chat->channel >= 0)) {
466     char x[1024];
467    
468 tothwolf 1.39 snprintf(x, sizeof x, _("%s has been forcibly removed for flooding.\n"), dcc[idx].nick);
469 segfault 1.1 chanout_but(idx, chat->channel, "*** %s", x);
470     if (chat->channel < 100000)
471     botnet_send_part_idx(idx, x);
472     }
473     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
474     if ((dcc[idx].sock != STDOUT) || backgrd) {
475     killsock(dcc[idx].sock);
476     lostdcc(idx);
477     } else {
478     dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
479     dcc_chatter(idx);
480     }
481     return 1; /* <- flood */
482     }
483     }
484     return 0;
485     }
486    
487 fabian 1.17 /* Handle someone being booted from dcc chat.
488     */
489 segfault 1.1 void do_boot(int idx, char *by, char *reason)
490     {
491     int files = (dcc[idx].type != &DCC_CHAT);
492    
493 ite 1.34 dprintf(idx, _("-=- poof -=-\n"));
494     dprintf(idx, _("Youve been booted from the %s by %s%s%s\n"), files ? "file section" : "bot",
495 fabian 1.23 by, reason[0] ? ": " : ".", reason);
496 fabian 1.17 /* If it's a partyliner (chatterer :) */
497     /* Horrible assumption that DCT_CHAT using structure uses same format
498 segfault 1.1 * as DCC_CHAT */
499     if ((dcc[idx].type->flags & DCT_CHAT) &&
500     (dcc[idx].u.chat->channel >= 0)) {
501     char x[1024];
502    
503 tothwolf 1.39 snprintf(x, sizeof x, _("%s booted %s from the party line%s%s\n"), by, dcc[idx].nick,
504 guppy 1.24 reason[0] ? ": " : "", reason);
505 segfault 1.1 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
506     if (dcc[idx].u.chat->channel < 100000)
507     botnet_send_part_idx(idx, x);
508     }
509     check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
510     if ((dcc[idx].sock != STDOUT) || backgrd) {
511     killsock(dcc[idx].sock);
512 fabian 1.11 lostdcc(idx);
513 fabian 1.17 /* Entry must remain in the table so it can be logged by the caller */
514 segfault 1.1 } else {
515     dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
516     dcc_chatter(idx);
517     }
518     return;
519     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23