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

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

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


Revision 1.3.2.3 - (show annotations) (download) (as text)
Tue Feb 8 22:06:01 2011 UTC (8 years, 9 months ago) by thommey
Branch: gettext
Changes since 1.3.2.2: +2 -2 lines
File MIME type: text/x-chdr
merge changes from master into gettext

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.3.2.2 2011/01/12 13:54:00 pseudo Exp $
10 */
11 /*
12 * Copyright (C) 1997 Robey Pointer
13 * Copyright (C) 1999 - 2010 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, dcc_flood_thr, backgrd, copy_to_tmp, max_socks;
39 extern char botnetnick[], 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
47 int reserved_port_min = 0;
48 int reserved_port_max = 0;
49
50 int max_dcc = 0; /* indicates the current dcc limit in the main thread */
51
52 /* This function is called to enlarge the static sockettable in a thread.
53 * It keeps the mainthread dcc table enlarging with the main thread sockettable
54 * If this fails because the upper limit max_socks is reached, -1 is returned.
55 * If this was called from the main thread, it updates the socklist variable
56 *
57 * increase_socks_max() can be called by Tcl threads
58 */
59 int increase_socks_max()
60 {
61 struct threaddata *td = threaddata();
62 int osock = td->MAXSOCKS;
63
64 if (max_socks < 1)
65 max_socks = 1;
66
67 if (td->MAXSOCKS == max_socks) {
68 putlog(LOG_MISC, "*", _("Maximum socket limit reached. "
69 "Consider raising max-socks."));
70 return -1;
71 }
72
73 td->MAXSOCKS += 10;
74 if (td->MAXSOCKS > max_socks)
75 td->MAXSOCKS = max_socks;
76
77 if (td->socklist)
78 td->socklist = nrealloc(td->socklist, sizeof(sock_list) * td->MAXSOCKS);
79 else
80 td->socklist = nmalloc(sizeof(sock_list) * td->MAXSOCKS);
81 for (; osock < td->MAXSOCKS; osock++)
82 td->socklist[osock].flags = SOCK_UNUSED;
83
84 if (td->mainthread) {
85 max_dcc = td->MAXSOCKS - 10;
86 if (max_dcc < 1)
87 max_dcc = 1;
88 if (dcc)
89 dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
90 else
91 dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
92 socklist = td->socklist;
93 }
94
95 return 0;
96 }
97
98 int expmem_dccutil()
99 {
100 int tot, i;
101
102 tot = sizeof(struct dcc_t) * max_dcc;
103 tot += sizeof(sock_list) * threaddata()->MAXSOCKS;
104
105 for (i = 0; i < dcc_total; i++) {
106 if (dcc[i].type && dcc[i].type->expmem)
107 tot += dcc[i].type->expmem(dcc[i].u.other);
108 }
109 return tot;
110 }
111
112 int findidx(int z)
113 {
114 int j;
115
116 for (j = 0; j < dcc_total; j++)
117 if ((dcc[j].sock == z) && (dcc[j].type->flags & DCT_VALIDIDX))
118 return j;
119 return -1;
120 }
121
122 int findanyidx(register int z)
123 {
124 register int j;
125
126 for (j = 0; j < dcc_total; j++)
127 if (dcc[j].sock == z)
128 return j;
129 return -1;
130 }
131
132 /* Replace \n with \r\n */
133 char *add_cr(char *buf)
134 {
135 static char WBUF[1024];
136 char *p, *q;
137
138 for (p = buf, q = WBUF; *p; p++, q++) {
139 if (*p == '\n')
140 *q++ = '\r';
141 *q = *p;
142 }
143 *q = *p;
144 return WBUF;
145 }
146
147 extern void (*qserver) (int, char *, int);
148
149 void dprintf EGG_VARARGS_DEF(int, arg1)
150 {
151 char buf[1024];
152 char *format;
153 int idx, len;
154 va_list va;
155
156 idx = EGG_VARARGS_START(int, arg1, va);
157 format = va_arg(va, char *);
158
159 egg_vsnprintf(buf, 1023, format, va);
160 va_end(va);
161 /* We can not use the return value vsnprintf() to determine where
162 * to null terminate. The C99 standard specifies that vsnprintf()
163 * shall return the number of bytes that would be written if the
164 * buffer had been large enough, rather then -1.
165 */
166 /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
167 * sizeof(buf) bytes. But we're not doing that anyway.
168 */
169 buf[sizeof(buf) - 1] = 0;
170 len = strlen(buf);
171
172 if (idx < 0) {
173 tputs(-idx, buf, len);
174 } else if (idx > 0x7FF0) {
175 switch (idx) {
176 case DP_LOG:
177 putlog(LOG_MISC, "*", "%s", buf);
178 break;
179 case DP_STDOUT:
180 tputs(STDOUT, buf, len);
181 break;
182 case DP_STDERR:
183 tputs(STDERR, buf, len);
184 break;
185 case DP_SERVER:
186 case DP_HELP:
187 case DP_MODE:
188 case DP_MODE_NEXT:
189 case DP_SERVER_NEXT:
190 case DP_HELP_NEXT:
191 qserver(idx, buf, len);
192 break;
193 }
194 return;
195 } else {
196 if (len > 500) { /* Truncate to fit */
197 buf[500] = 0;
198 strcat(buf, "\n");
199 len = 501;
200 }
201 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
202 char *p = add_cr(buf);
203
204 tputs(dcc[idx].sock, p, strlen(p));
205 } else if (dcc[idx].type && dcc[idx].type->output)
206 dcc[idx].type->output(idx, buf, dcc[idx].u.other);
207 else
208 tputs(dcc[idx].sock, buf, len);
209 }
210 }
211
212 void chatout EGG_VARARGS_DEF(char *, arg1)
213 {
214 int i, len;
215 char *format;
216 char s[601];
217 va_list va;
218
219 format = EGG_VARARGS_START(char *, arg1, va);
220
221 egg_vsnprintf(s, 511, format, va);
222 va_end(va);
223 len = strlen(s);
224 if (len > 511)
225 len = 511;
226 s[len + 1] = 0;
227
228 for (i = 0; i < dcc_total; i++)
229 if (dcc[i].type == &DCC_CHAT)
230 if (dcc[i].u.chat->channel >= 0)
231 dprintf(i, "%s", s);
232
233 }
234
235 /* Print to all on this channel but one.
236 */
237 void chanout_but EGG_VARARGS_DEF(int, arg1)
238 {
239 int i, x, chan, len;
240 char *format;
241 char s[601];
242 va_list va;
243
244 x = EGG_VARARGS_START(int, arg1, va);
245 chan = va_arg(va, int);
246 format = va_arg(va, char *);
247
248 egg_vsnprintf(s, 511, format, va);
249 va_end(va);
250 len = strlen(s);
251 if (len > 511)
252 len = 511;
253 s[len + 1] = 0;
254
255 for (i = 0; i < dcc_total; i++)
256 if ((dcc[i].type == &DCC_CHAT) && (i != x))
257 if (dcc[i].u.chat->channel == chan)
258 dprintf(i, "%s", s);
259
260 }
261
262 void dcc_chatter(int idx)
263 {
264 int i, j;
265 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
266
267 get_user_flagrec(dcc[idx].user, &fr, NULL);
268 show_motd(idx);
269 i = dcc[idx].u.chat->channel;
270 dcc[idx].u.chat->channel = 234567;
271 j = dcc[idx].sock;
272 strcpy(dcc[idx].u.chat->con_chan, "***");
273 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
274 /* Still there? */
275 if ((idx >= dcc_total) || (dcc[idx].sock != j))
276 return; /* Nope */
277 /* Tcl script may have taken control */
278 if (dcc[idx].type == &DCC_CHAT) {
279 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
280 strcpy(dcc[idx].u.chat->con_chan, "*");
281 if (dcc[idx].u.chat->channel == 234567) {
282 /* If the chat channel has already been altered it's *highly*
283 * probably join/part messages have been broadcast everywhere,
284 * so dont bother sending them
285 */
286 if (i == -2)
287 i = 0;
288 dcc[idx].u.chat->channel = i;
289 if ((dcc[idx].u.chat->channel >= 0) &&
290 (dcc[idx].u.chat->channel < GLOBAL_CHANS))
291 botnet_send_join_idx(idx, -1);
292 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
293 geticon(idx), dcc[idx].sock, dcc[idx].host);
294 }
295 /* But *do* bother with sending it locally */
296 if (!dcc[idx].u.chat->channel) {
297 chanout_but(-1, 0, _("*** %s joined the party line.\n"), dcc[idx].nick);
298 } else if (dcc[idx].u.chat->channel > 0) {
299 chanout_but(-1, dcc[idx].u.chat->channel,
300 _("*** %s joined the channel.\n"), dcc[idx].nick);
301 }
302 }
303 }
304
305 /* Closes an open FD for transfer sockets. */
306 void killtransfer(int n)
307 {
308 int i, ok = 1;
309
310 if (dcc[n].type->flags & DCT_FILETRAN) {
311 if (dcc[n].u.xfer->f) {
312 fclose(dcc[n].u.xfer->f);
313 dcc[n].u.xfer->f = NULL;
314 }
315 if (dcc[n].u.xfer->filename && copy_to_tmp) {
316 for (i = 0; i < dcc_total; i++) {
317 if ((i != n) && (dcc[i].type->flags & DCT_FILETRAN) &&
318 (dcc[i].u.xfer->filename) &&
319 (!strcmp(dcc[i].u.xfer->filename, dcc[n].u.xfer->filename))) {
320 ok = 0;
321 break;
322 }
323 }
324 if (ok)
325 unlink(dcc[n].u.xfer->filename);
326 }
327 }
328 }
329
330 /* Mark an entry as lost and deconstruct it's contents. It will be securely
331 * removed from the dcc list in the main loop.
332 */
333 void lostdcc(int n)
334 {
335 /* Make sure it's a valid dcc index. */
336 if (n < 0 || n >= max_dcc)
337 return;
338
339 if (dcc[n].type && dcc[n].type->kill)
340 dcc[n].type->kill(n, dcc[n].u.other);
341 else if (dcc[n].u.other)
342 nfree(dcc[n].u.other);
343 egg_bzero(&dcc[n], sizeof(struct dcc_t));
344
345 dcc[n].sock = -1;
346 dcc[n].type = &DCC_LOST;
347 }
348
349 /* Remove entry from dcc list. Think twice before using this function,
350 * because it invalidates any variables that point to a specific dcc
351 * entry!
352 *
353 * Note: The entry will be deconstructed if it was not deconstructed
354 * already. This case should normally not occur.
355 */
356 void removedcc(int n)
357 {
358 if (dcc[n].type && dcc[n].type->kill)
359 dcc[n].type->kill(n, dcc[n].u.other);
360 else if (dcc[n].u.other)
361 nfree(dcc[n].u.other);
362 dcc_total--;
363 if (n < dcc_total)
364 memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
365 else
366 egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
367 }
368
369 /* Clean up sockets that were just left for dead.
370 */
371 void dcc_remove_lost(void)
372 {
373 int i;
374
375 for (i = 0; i < dcc_total; i++) {
376 if (dcc[i].type == &DCC_LOST) {
377 dcc[i].type = NULL;
378 dcc[i].sock = -1;
379 removedcc(i);
380 i--;
381 }
382 }
383 }
384
385 /* Show list of current dcc's to a dcc-chatter
386 * positive value: idx given -- negative value: sock given
387 */
388 void tell_dcc(int zidx)
389 {
390 int i, j, nicklen = 0;
391 char other[160];
392 char format[81];
393
394 /* calculate max nicklen */
395 for (i = 0; i < dcc_total; i++) {
396 if (strlen(dcc[i].nick) > nicklen)
397 nicklen = strlen(dcc[i].nick);
398 }
399 if (nicklen < 9)
400 nicklen = 9;
401
402 j = 60 - nicklen;
403 if (j < 15)
404 j = 15;
405 if (j > 40)
406 j = 40;
407
408 egg_snprintf(format, sizeof format, "%%-3s %%-%u.%us %%-6s %%-%u.%us %%s\n",
409 j, j, nicklen, nicklen);
410 dprintf(zidx, format, "IDX", "ADDR", "+ PORT", "NICK", "TYPE INFO");
411 dprintf(zidx, format, "---",
412 "------------------------------------------------------", "------",
413 "--------------------------------", "----- ---------");
414 egg_snprintf(format, sizeof format, "%%-3d %%-%u.%us %%c%%5d %%-%u.%us %%s\n",
415 j, j, nicklen, nicklen);
416
417 /* Show server */
418 for (i = 0; i < dcc_total; i++) {
419 if (dcc[i].type && dcc[i].type->display)
420 dcc[i].type->display(i, other);
421 else {
422 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
423 break;
424 }
425 dprintf(zidx, format, dcc[i].sock, iptostr(&dcc[i].sockname.addr.sa),
426 #ifdef TLS
427 dcc[i].ssl ? '+' : ' ', dcc[i].port, dcc[i].nick, other);
428 #else
429 ' ', dcc[i].port, dcc[i].nick, other);
430 #endif
431 }
432 }
433
434 /* Mark someone on dcc chat as no longer away
435 */
436 void not_away(int idx)
437 {
438 if (dcc[idx].u.chat->away == NULL) {
439 dprintf(idx, "You weren't away!\n");
440 return;
441 }
442 if (dcc[idx].u.chat->channel >= 0) {
443 chanout_but(-1, dcc[idx].u.chat->channel,
444 _("*** %s is no longer away.\n"), dcc[idx].nick);
445 if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
446 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
447 }
448 }
449 dprintf(idx, _("You're not away any more.\n"));
450 nfree(dcc[idx].u.chat->away);
451 dcc[idx].u.chat->away = NULL;
452 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
453 }
454
455 void set_away(int idx, char *s)
456 {
457 if (s == NULL) {
458 not_away(idx);
459 return;
460 }
461 if (!s[0]) {
462 not_away(idx);
463 return;
464 }
465 if (dcc[idx].u.chat->away != NULL)
466 nfree(dcc[idx].u.chat->away);
467 dcc[idx].u.chat->away = nmalloc(strlen(s) + 1);
468 strcpy(dcc[idx].u.chat->away, s);
469 if (dcc[idx].u.chat->channel >= 0) {
470 chanout_but(-1, dcc[idx].u.chat->channel,
471 _("*** %s is now away: %s\n"), dcc[idx].nick, s);
472 if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
473 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
474 }
475 }
476 dprintf(idx, _("You are now away.\n"));
477 check_tcl_away(botnetnick, dcc[idx].sock, s);
478 }
479
480 /* This helps the memory debugging
481 */
482 void *_get_data_ptr(int size, char *file, int line)
483 {
484 char *p;
485 #ifdef DEBUG_MEM
486 char x[1024];
487
488 p = strrchr(file, '/');
489 egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
490 p = n_malloc(size, x, line);
491 #else
492 p = nmalloc(size);
493 #endif
494 egg_bzero(p, size);
495 return p;
496 }
497
498 /* Make a password, 10-15 random letters and digits
499 */
500 void makepass(char *s)
501 {
502 int i;
503
504 i = 10 + randint(6);
505 make_rand_str(s, i);
506 }
507
508 void flush_lines(int idx, struct chat_info *ci)
509 {
510 int c = ci->line_count;
511 struct msgq *p = ci->buffer, *o;
512
513 while (p && c < (ci->max_line)) {
514 ci->current_lines--;
515 tputs(dcc[idx].sock, p->msg, p->len);
516 nfree(p->msg);
517 o = p->next;
518 nfree(p);
519 p = o;
520 c++;
521 }
522 if (p != NULL) {
523 if (dcc[idx].status & STAT_TELNET)
524 tputs(dcc[idx].sock, _("[More]: "), 8);
525 else
526 tputs(dcc[idx].sock, _("[More]\n"), 7);
527 }
528 ci->buffer = p;
529 ci->line_count = 0;
530 }
531
532 int new_dcc(struct dcc_table *type, int xtra_size)
533 {
534 int i = dcc_total;
535
536 if (dcc_total == max_dcc && increase_socks_max())
537 return -1;
538 dcc_total++;
539 egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
540
541 dcc[i].type = type;
542 if (xtra_size) {
543 dcc[i].u.other = nmalloc(xtra_size);
544 egg_bzero(dcc[i].u.other, xtra_size);
545 }
546 return i;
547 }
548
549 /* Changes the given dcc entry to another type.
550 */
551 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
552 {
553 /* Free old structure. */
554 if (dcc[i].type && dcc[i].type->kill)
555 dcc[i].type->kill(i, dcc[i].u.other);
556 else if (dcc[i].u.other) {
557 nfree(dcc[i].u.other);
558 dcc[i].u.other = NULL;
559 }
560
561 dcc[i].type = type;
562 if (xtra_size) {
563 dcc[i].u.other = nmalloc(xtra_size);
564 egg_bzero(dcc[i].u.other, xtra_size);
565 }
566 }
567
568 int detect_dcc_flood(time_t *timer, struct chat_info *chat, int idx)
569 {
570 time_t t;
571
572 if (!dcc_flood_thr)
573 return 0;
574 t = now;
575 if (*timer != t) {
576 *timer = t;
577 chat->msgs_per_sec = 0;
578 } else {
579 chat->msgs_per_sec++;
580 if (chat->msgs_per_sec > dcc_flood_thr) {
581 /* FLOOD */
582 dprintf(idx, _("*** FLOOD: Goodbye.\n"));
583 /* Evil assumption here that flags&DCT_CHAT implies chat type */
584 if ((dcc[idx].type->flags & DCT_CHAT) && chat && (chat->channel >= 0)) {
585 char x[1024];
586
587 egg_snprintf(x, sizeof x, _("%s has been forcibly removed for "
588 "flooding.\n"), dcc[idx].nick);
589 chanout_but(idx, chat->channel, "*** %s", x);
590 if (chat->channel < GLOBAL_CHANS)
591 botnet_send_part_idx(idx, x);
592 }
593 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
594 if ((dcc[idx].sock != STDOUT) || backgrd) {
595 killsock(dcc[idx].sock);
596 lostdcc(idx);
597 } else {
598 dprintf(DP_STDOUT, _("\n### SIMULATION RESET ###\n\n"));
599 dcc_chatter(idx);
600 }
601 return 1; /* <- flood */
602 }
603 }
604 return 0;
605 }
606
607 /* Handle someone being booted from dcc chat.
608 */
609 void do_boot(int idx, char *by, char *reason)
610 {
611 int files = (dcc[idx].type != &DCC_CHAT);
612
613 dprintf(idx, _("-=- poof -=-\n"));
614 if (files)
615 dprintf(idx, _("You've been booted from the file section by %s%s%s\n"),
616 by, reason[0] ? ": " : ".", reason);
617 else
618 dprintf(idx, _("You've been booted from the bot by %s%s%s\n"),
619 by, reason[0] ? ": " : ".", reason);
620 /* If it's a partyliner (chatterer :) */
621 /* Horrible assumption that DCT_CHAT using structure uses same format
622 * as DCC_CHAT */
623 if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
624 char x[1024];
625
626 egg_snprintf(x, sizeof x, _("%s booted %s from the party line%s%s"), by,
627 dcc[idx].nick, reason[0] ? ": " : "", reason);
628 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
629 if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
630 botnet_send_part_idx(idx, x);
631 }
632 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
633 if ((dcc[idx].sock != STDOUT) || backgrd) {
634 killsock(dcc[idx].sock);
635 lostdcc(idx);
636 /* Entry must remain in the table so it can be logged by the caller */
637 } else {
638 dprintf(DP_STDOUT, _("\n### SIMULATION RESET\n\n"));
639 dcc_chatter(idx);
640 }
641 return;
642 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23