/[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.51 - (hide annotations) (download) (as text)
Sun May 5 16:40:38 2002 UTC (17 years, 5 months ago) by tothwolf
Branch: MAIN
Changes since 1.50: +10 -7 lines
File MIME type: text/x-chdr
Grafted GPL header onto newer files.
Storing rcsid in binary files now.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23