/[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.54 - (hide annotations) (download) (as text)
Sun May 26 08:34:13 2002 UTC (17 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.53: +9 -9 lines
File MIME type: text/x-chdr
* Moved creation and maintenance of core binds from tclhash.c to core_binds.c
* Renamed check_tcl_* to check_bind_* in the core

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23