/[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.28 - (hide annotations) (download) (as text)
Sat Mar 10 06:36:20 2001 UTC (18 years, 7 months ago) by guppy
Branch: MAIN
Changes since 1.27: +56 -24 lines
File MIME type: text/x-chdr
Wingman's utf8 fix ... please test this alot people :P~

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23