/[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.41 - (hide annotations) (download) (as text)
Mon Jan 14 02:23:27 2002 UTC (17 years, 9 months ago) by ite
Branch: MAIN
Changes since 1.40: +1 -2 lines
File MIME type: text/x-chdr
* Merged bits of tothwolf cleaunps from his alpha patch.
* Removed some unneeded externs in some files.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23