/[cvs]/eggdrop1.9/src/dccutil.c
ViewVC logotype

Contents of /eggdrop1.9/src/dccutil.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.31 - (show annotations) (download) (as text)
Thu Apr 12 02:39:43 2001 UTC (18 years, 6 months ago) by guppy
Branch: MAIN
Changes since 1.30: +15 -15 lines
File MIME type: text/x-chdr
lets update the copyright info for 2001 k? :P~

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23