/[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.56 - (show annotations) (download) (as text)
Thu Oct 10 04:41:59 2002 UTC (17 years ago) by stdarg
Branch: MAIN
Changes since 1.55: +1 -15 lines
File MIME type: text/x-chdr
* Moved bind tables to libeggdrop

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23