/[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.65 - (show annotations) (download) (as text)
Wed Aug 6 18:01:00 2003 UTC (15 years, 10 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.64: +1 -1 lines
File MIME type: text/x-chdr
FILE REMOVED
*** empty log message ***

1 /*
2 * dccutil.c --
3 *
4 * 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 */
10 /*
11 * Copyright (C) 1997 Robey Pointer
12 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Eggheads Development Team
13 *
14 * 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 *
19 * 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 *
24 * 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 */
28
29 #ifndef lint
30 static const char rcsid[] = "$Id: dccutil.c,v 1.64 2003/08/02 05:59:53 tothwolf Exp $";
31 #endif
32
33 #include <sys/stat.h>
34 #include "main.h"
35 #include <errno.h>
36 #include "chan.h"
37 #include "modules.h"
38 #include "logfile.h"
39 #include "misc.h"
40 #include "cmdt.h" /* cmd_t */
41 #include "net.h" /* tputs, killsock */
42 #include "dccutil.h" /* prototypes */
43 #include "users.h" /* get_user_by_handle */
44
45 extern struct dcc_t *dcc;
46 extern struct userrec *userlist;
47
48 extern int dcc_total, max_dcc, dcc_flood_thr, backgrd, MAXSOCKS;
49 extern char spaces[], version[];
50
51 extern time_t now;
52
53 extern sock_list *socklist;
54
55 #ifndef MAKING_MODS
56 extern struct dcc_table DCC_CHAT, DCC_LOST;
57 #endif /* MAKING_MODS */
58
59 char myname[HANDLEN + 1] = "";
60 char motdfile[121] = "text/motd"; /* File where the motd is stored */
61 int connect_timeout = 15; /* How long to wait before a telnet
62 connection times out */
63
64 int reserved_port_min = 0;
65 int reserved_port_max = 0;
66
67 void init_dcc_max(int newmax)
68 {
69 int osock = MAXSOCKS;
70
71 if (newmax < 1) newmax = 1;
72 dcc = realloc(dcc, sizeof(struct dcc_t) * newmax);
73 if (newmax > max_dcc) memset(dcc+max_dcc, 0, (newmax-max_dcc) * sizeof(*dcc));
74
75 MAXSOCKS = newmax + 10;
76 socklist = (sock_list *)realloc(socklist, sizeof(sock_list) * MAXSOCKS);
77 if (newmax > max_dcc) memset(socklist+max_dcc+10, 0, (newmax-max_dcc) * sizeof(*socklist));
78
79 for (; osock < MAXSOCKS; osock++)
80 socklist[osock].flags = SOCK_UNUSED;
81
82 max_dcc = newmax;
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 len = vsnprintf(buf, 1023, format, va);
113 va_end(va);
114 if (len < 0 || len >= sizeof(buf)) len = sizeof(buf)-1;
115 buf[len] = 0;
116
117 if (idx < 0) {
118 tputs(-idx, buf, len);
119 } else if (idx > 0x7FF0) {
120 switch (idx) {
121 case DP_LOG:
122 putlog(LOG_MISC, "*", "%s", buf);
123 break;
124 case DP_STDOUT:
125 tputs(STDOUT, buf, len);
126 break;
127 case DP_STDERR:
128 tputs(STDERR, buf, len);
129 break;
130 case DP_SERVER:
131 case DP_HELP:
132 case DP_MODE:
133 case DP_MODE_NEXT:
134 case DP_SERVER_NEXT:
135 case DP_HELP_NEXT:
136 qserver(idx, buf, len);
137 break;
138 }
139 return;
140 } else {
141 if (len > 500) { /* Truncate to fit */
142 buf[500] = 0;
143 strcat(buf, "\n");
144 len = 501;
145 }
146 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
147 char *p = add_cr(buf);
148
149 tputs(dcc[idx].sock, p, strlen(p));
150 } else if (dcc[idx].type && dcc[idx].type->output) {
151 dcc[idx].type->output(idx, buf, dcc[idx].u.other);
152 } else
153 tputs(dcc[idx].sock, buf, len);
154 }
155 }
156
157 void chatout EGG_VARARGS_DEF(char *, arg1)
158 {
159 int i, len;
160 char *format;
161 char s[601];
162 va_list va;
163
164 format = EGG_VARARGS_START(char *, arg1, va);
165 vsnprintf(s, 511, format, va);
166 va_end(va);
167 len = strlen(s);
168 if (len > 511)
169 len = 511;
170 s[len + 1] = 0;
171
172 for (i = 0; i < dcc_total; i++)
173 if (dcc[i].type == &DCC_CHAT)
174 if (dcc[i].u.chat->channel >= 0)
175 dprintf(i, "%s", s);
176
177 }
178
179 /* Print to all on this channel but one.
180 */
181 void chanout_but EGG_VARARGS_DEF(int, arg1)
182 {
183 int i, x, chan, len;
184 char *format;
185 char s[601];
186 va_list va;
187
188 x = EGG_VARARGS_START(int, arg1, va);
189 chan = va_arg(va, int);
190 format = va_arg(va, char *);
191 vsnprintf(s, 511, format, va);
192 va_end(va);
193 len = strlen(s);
194 if (len > 511)
195 len = 511;
196 s[len + 1] = 0;
197
198 for (i = 0; i < dcc_total; i++)
199 if ((dcc[i].type == &DCC_CHAT) && (i != x))
200 if (dcc[i].u.chat->channel == chan)
201 dprintf(i, "%s", s);
202
203 }
204
205 void dcc_chatter(int idx)
206 {
207 int i, j;
208 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
209
210 get_user_flagrec(dcc[idx].user, &fr, NULL);
211 show_motd(idx);
212 i = dcc[idx].u.chat->channel;
213 dcc[idx].u.chat->channel = 234567;
214 j = dcc[idx].sock;
215 strcpy(dcc[idx].u.chat->con_chan, "***");
216 /* Still there? */
217 if ((idx >= dcc_total) || (dcc[idx].sock != j))
218 return; /* Nope */
219 /* Tcl script may have taken control */
220 if (dcc[idx].type == &DCC_CHAT) {
221 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
222 strcpy(dcc[idx].u.chat->con_chan, "*");
223 if (dcc[idx].u.chat->channel == 234567) {
224 /* If the chat channel has already been altered it's *highly*
225 * probably join/part messages have been broadcast everywhere,
226 * so dont bother sending them
227 */
228 if (i == -2)
229 i = 0;
230 dcc[idx].u.chat->channel = i;
231 }
232 /* But *do* bother with sending it locally */
233 if (!dcc[idx].u.chat->channel) {
234 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
235 } else if (dcc[idx].u.chat->channel > 0) {
236 chanout_but(-1, dcc[idx].u.chat->channel,
237 "*** %s joined the channel.\n", dcc[idx].nick);
238 }
239 }
240 }
241
242 /* Closes an open FD for transfer sockets. */
243 void killtransfer(int n)
244 {
245 int i, ok = 1;
246
247 if (dcc[n].type->flags & DCT_FILETRAN) {
248 if (dcc[n].u.xfer->f) {
249 fclose(dcc[n].u.xfer->f);
250 dcc[n].u.xfer->f = NULL;
251 }
252 }
253 }
254
255 /* Mark an entry as lost and deconstruct it's contents. It will be securely
256 * removed from the dcc list in the main loop.
257 */
258 void lostdcc(int n)
259 {
260 /* Make sure it's a valid dcc index. */
261 if (n < 0 || n >= max_dcc) return;
262
263 if (dcc[n].type && dcc[n].type->kill)
264 dcc[n].type->kill(n, dcc[n].u.other);
265 else if (dcc[n].u.other)
266 free(dcc[n].u.other);
267 memset(&dcc[n], 0, sizeof(struct dcc_t));
268
269 dcc[n].sock = (-1);
270 dcc[n].type = &DCC_LOST;
271 }
272
273 /* Remove entry from dcc list. Think twice before using this function,
274 * because it invalidates any variables that point to a specific dcc
275 * entry!
276 *
277 * Note: The entry will be deconstructed if it was not deconstructed
278 * already. This case should normally not occur.
279 */
280 static void removedcc(int n)
281 {
282 if (dcc[n].type && dcc[n].type->kill)
283 dcc[n].type->kill(n, dcc[n].u.other);
284 else if (dcc[n].u.other)
285 free(dcc[n].u.other);
286
287 /* if we are removing the last entry let's decrease dcc_total */
288 if(n + 1 == dcc_total) dcc_total--;
289
290 memset(&dcc[n], 0, sizeof(struct dcc_t)); /* drummer */
291 }
292
293 /* Clean up sockets that were just left for dead.
294 */
295 void dcc_remove_lost(void)
296 {
297 int i;
298
299
300 for (i = dcc_total; i > 0; --i) {
301 if (dcc[i].type == &DCC_LOST) {
302 dcc[i].type = NULL;
303 removedcc(i);
304 }
305 }
306 }
307
308 /* Show list of current dcc's to a dcc-chatter
309 * positive value: idx given -- negative value: sock given
310 */
311 void tell_dcc(int zidx)
312 {
313 int i, j, k;
314 char other[160];
315
316 spaces[HANDLEN - 9] = 0;
317 dprintf(zidx, "SOCK PORT NICK %s HOST TYPE\n"
318 ,spaces);
319 dprintf(zidx, "---- ----- ---------%s -------------------------- ----\n"
320 ,spaces);
321 spaces[HANDLEN - 9] = ' ';
322 /* Show server */
323 for (i = 0; i < dcc_total; i++) {
324 if (!dcc[i].type) continue;
325 j = strlen(dcc[i].host);
326 if (j > 26)
327 j -= 26;
328 else
329 j = 0;
330 if (dcc[i].type && dcc[i].type->display)
331 dcc[i].type->display(i, other);
332 else {
333 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
334 break;
335 }
336 k = HANDLEN - strlen(dcc[i].nick);
337 spaces[k] = 0;
338 dprintf(zidx, "%-4d %5d %s%s %-26s %s\n", i,
339 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
340 spaces[k] = ' ';
341 }
342 }
343
344 /* Mark someone on dcc chat as no longer away
345 */
346 void not_away(int idx)
347 {
348 if (dcc[idx].u.chat->away == NULL) {
349 dprintf(idx, "You weren't away!\n");
350 return;
351 }
352 if (dcc[idx].u.chat->channel >= 0) {
353 chanout_but(-1, dcc[idx].u.chat->channel,
354 "*** %s is no longer away.\n", dcc[idx].nick);
355 }
356 dprintf(idx, "You're not away any more.\n");
357 free_null(dcc[idx].u.chat->away);
358 }
359
360 void set_away(int idx, char *s)
361 {
362 if (s == NULL) {
363 not_away(idx);
364 return;
365 }
366 if (!s[0]) {
367 not_away(idx);
368 return;
369 }
370 if (dcc[idx].u.chat->away != NULL)
371 free(dcc[idx].u.chat->away);
372 dcc[idx].u.chat->away = strdup(s);
373 if (dcc[idx].u.chat->channel >= 0) {
374 chanout_but(-1, dcc[idx].u.chat->channel,
375 "*** %s is now away: %s\n", dcc[idx].nick, s);
376 }
377 dprintf(idx, "You are now away.\n");
378 }
379
380 /* Make a password, 10-15 random letters and digits
381 */
382 void makepass(char *s)
383 {
384 int i;
385
386 i = 10 + (random() % 6);
387 make_rand_str(s, i);
388 }
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 free(p->msg);
399 o = p->next;
400 free(p);
401 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 for (i = 0; i < dcc_total; i++)
419 if (!dcc[i].type) break;
420
421 if (i == max_dcc)
422 return -1;
423
424 if (i == dcc_total)
425 dcc_total++;
426 memset(&dcc[i], 0, sizeof(*dcc));
427
428 dcc[i].type = type;
429 if (xtra_size)
430 dcc[i].u.other = calloc(1, xtra_size);
431 return i;
432 }
433
434 /* Changes the given dcc entry to another type.
435 */
436 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
437 {
438 /* Free old structure. */
439 if (dcc[i].type && dcc[i].type->kill)
440 dcc[i].type->kill(i, dcc[i].u.other);
441 else if (dcc[i].u.other)
442 free_null(dcc[i].u.other);
443
444 dcc[i].type = type;
445 if (xtra_size)
446 dcc[i].u.other = calloc(1, xtra_size);
447 }
448
449 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
450 {
451 time_t t;
452
453 if (!dcc_flood_thr)
454 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 dprintf(idx, "*** FLOOD: %s.\n", _("Goodbye"));
464 /* Evil assumption here that flags&DCT_CHAT implies chat type */
465 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
466 (chat->channel >= 0)) {
467 char x[1024];
468
469 snprintf(x, sizeof x, _("%s has been forcibly removed for flooding.\n"), dcc[idx].nick);
470 chanout_but(idx, chat->channel, "*** %s", x);
471 }
472 if ((dcc[idx].sock != STDOUT) || backgrd) {
473 killsock(dcc[idx].sock);
474 lostdcc(idx);
475 } else {
476 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
477 dcc_chatter(idx);
478 }
479 return 1; /* <- flood */
480 }
481 }
482 return 0;
483 }
484
485 /* Handle someone being booted from dcc chat.
486 */
487 void do_boot(int idx, char *by, char *reason)
488 {
489 int files = (dcc[idx].type != &DCC_CHAT);
490
491 dprintf(idx, _("-=- poof -=-\n"));
492 dprintf(idx, _("Youve been booted from the %s by %s%s%s\n"), files ? "file section" : "bot",
493 by, reason[0] ? ": " : ".", reason);
494 /* If it's a partyliner (chatterer :) */
495 /* Horrible assumption that DCT_CHAT using structure uses same format
496 * as DCC_CHAT */
497 if ((dcc[idx].type->flags & DCT_CHAT) &&
498 (dcc[idx].u.chat->channel >= 0)) {
499 char x[1024];
500
501 snprintf(x, sizeof x, _("%s booted %s from the party line%s%s\n"), by, dcc[idx].nick,
502 reason[0] ? ": " : "", reason);
503 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
504 }
505 if ((dcc[idx].sock != STDOUT) || backgrd) {
506 killsock(dcc[idx].sock);
507 lostdcc(idx);
508 /* Entry must remain in the table so it can be logged by the caller */
509 } else {
510 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
511 dcc_chatter(idx);
512 }
513 return;
514 }
515
516 /* FIXME: botnet dependencies */
517 int add_note(char *to, char *from, char *msg, int idx, int echo)
518 {
519 int status, i, iaway, sock;
520 char ss[81], ssf[81];
521 struct userrec *u;
522
523 if (strlen(msg) > 450)
524 msg[450] = 0; /* Notes have a limit */
525 /* note length + PRIVMSG header + nickname + date must be <512 */
526 /* Might be form "sock:nick" */
527 splitc(ssf, from, ':');
528 rmspace(ssf);
529 splitc(ss, to, ':');
530 rmspace(ss);
531 if (!ss[0])
532 sock = (-1);
533 else
534 sock = atoi(ss);
535 /* Don't process if there's a note binding for it */
536 if (idx != (-2)) { /* Notes from bots don't trigger it */
537 }
538 if (!(u = get_user_by_handle(userlist, to))) {
539 if (idx >= 0)
540 dprintf(idx, _("I dont know anyone by that name.\n"));
541 return NOTE_ERROR;
542 }
543 if (is_bot(u)) {
544 if (idx >= 0)
545 dprintf(idx, _("Thats a bot. You cant leave notes for a bot.\n"));
546 return NOTE_ERROR;
547 }
548 if (match_noterej(u, from)) {
549 if (idx >= 0)
550 dprintf(idx, "%s %s\n", u->handle, "rejected your note.");
551 return NOTE_REJECT;
552 }
553 status = NOTE_STORED;
554 iaway = 0;
555 /* Online right now? */
556 for (i = 0; i < dcc_total; i++) {
557 if (dcc[i].type && (dcc[i].type->flags & DCT_GETNOTES) &&
558 ((sock == (-1)) || (sock == dcc[i].sock)) &&
559 (!strcasecmp(dcc[i].nick, to))) {
560 int aok = 1;
561
562 if (dcc[i].type == &DCC_CHAT)
563 if ((dcc[i].u.chat->away != NULL) &&
564 (idx != (-2))) {
565 /* Only check away if it's not from a bot */
566 aok = 0;
567 if (idx >= 0)
568 dprintf(idx, "%s %s: %s\n", dcc[i].nick, _("is away"),
569 dcc[i].u.chat->away);
570 if (!iaway)
571 iaway = i;
572 status = NOTE_AWAY;
573 }
574 if (aok) {
575 char *p, *fr = from;
576 int l = 0;
577 char work[1024];
578
579 while ((*msg == '<') || (*msg == '>')) {
580 p = newsplit(&msg);
581 if (*p == '<')
582 l += simple_sprintf(work + l, "via %s, ", p + 1);
583 else if (*from == '@')
584 fr = p + 1;
585 }
586 if (idx == -2 || (!strcasecmp(from, myname)))
587 dprintf(i, "*** [%s] %s%s\n", fr, l ? work : "", msg);
588 else
589 dprintf(i, "%cNote [%s]: %s%s\n", 7, fr, l ? work : "", msg);
590 if ((idx >= 0) && (echo))
591 dprintf(idx, "-> %s: %s\n", to, msg);
592 return NOTE_OK;
593 }
594 }
595 }
596 if (idx == (-2))
597 return NOTE_OK; /* Error msg from a tandembot: don't store */
598 status = storenote(from, to, msg, idx, NULL, 0);
599 if (status < 0) status = NOTE_ERROR;
600 else if (status == NOTE_AWAY) {
601 /* User is away in all sessions -- just notify the user that a
602 * message arrived and was stored. (only oldest session is notified.)
603 */
604 dprintf(iaway, "*** %s.\n", _("Note arrived for you"));
605 }
606 return(status);
607 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23