/[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.14 - (show annotations) (download) (as text)
Sat Jan 1 19:34:13 2000 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.13: +13 -6 lines
File MIME type: text/x-chdr
1.4 dccfix patch

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 * dprintf'ized, 28aug1995
10 *
11 * $Id: dccutil.c,v 1.13 1999/12/24 14:23:01 fabian Exp $
12 */
13 /*
14 * Copyright (C) 1997 Robey Pointer
15 * Copyright (C) 1999 Eggheads
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
32 #include "main.h"
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include "chan.h"
36 #include "modules.h"
37 #include "tandem.h"
38
39 extern struct dcc_t *dcc;
40 extern int dcc_total;
41 extern char botnetnick[];
42 extern char spaces[];
43 extern char version[];
44 extern time_t now;
45 extern int max_dcc;
46 extern int dcc_flood_thr;
47 extern int backgrd;
48
49 char motdfile[121] = "text/motd"; /* file where the motd is stored */
50 int connect_timeout = 15; /* how long to wait before a telnet
51 * connection times out */
52 int reserved_port = 0;
53
54 extern sock_list *socklist;
55 extern int MAXSOCKS;
56
57 void init_dcc_max()
58 {
59 int osock = MAXSOCKS;
60
61 if (max_dcc < 1)
62 max_dcc = 1;
63 if (dcc)
64 dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
65
66 else
67 dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
68
69 MAXSOCKS = max_dcc + 10;
70 if (socklist)
71 socklist = (sock_list *) nrealloc((void *) socklist,
72 sizeof(sock_list) * MAXSOCKS);
73 else
74 socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
75 for (; osock < MAXSOCKS; osock++)
76 socklist[osock].flags = SOCK_UNUSED;
77 }
78
79 int expmem_dccutil()
80 {
81 int tot, i;
82
83 Context;
84 tot = sizeof(struct dcc_t) * max_dcc + sizeof(sock_list) * MAXSOCKS;
85
86 for (i = 0; i < dcc_total; i++) {
87 if (dcc[i].type && dcc[i].type->expmem)
88 tot += dcc[i].type->expmem(dcc[i].u.other);
89 }
90 return tot;
91 }
92
93 static char SBUF[1024];
94
95 /* replace \n with \r\n */
96 char *add_cr(char *buf)
97 {
98 static char WBUF[1024];
99 char *p, *q;
100
101 for (p = buf, q = WBUF; *p; p++, q++) {
102 if (*p == '\n')
103 *q++ = '\r';
104 *q = *p;
105 }
106 *q = *p;
107 return WBUF;
108 }
109
110 extern void (*qserver) (int, char *, int);
111
112 void dprintf EGG_VARARGS_DEF(int, arg1)
113 {
114 char *format;
115 int idx, len;
116
117 va_list va;
118 idx = EGG_VARARGS_START(int, arg1, va);
119 format = va_arg(va, char *);
120
121 #ifdef HAVE_VSNPRINTF
122 if ((len = vsnprintf(SBUF, 1023, format, va)) < 0)
123 SBUF[len = 1023] = 0;
124 #else
125 len = vsprintf(SBUF, format, va);
126 #endif
127 va_end(va);
128 if (idx < 0) {
129 tputs(-idx, SBUF, len);
130 } else if (idx > 0x7FF0) {
131 switch (idx) {
132 case DP_LOG:
133 putlog(LOG_MISC, "*", "%s", SBUF);
134 break;
135 case DP_STDOUT:
136 tputs(STDOUT, SBUF, len);
137 break;
138 case DP_STDERR:
139 tputs(STDERR, SBUF, 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, SBUF, len);
148 break;
149 }
150 return;
151 } else {
152 if (len > 500) { /* truncate to fit */
153 SBUF[500] = 0;
154 strcat(SBUF, "\n");
155 len = 501;
156 }
157 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
158 char *p = add_cr(SBUF);
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, SBUF, dcc[idx].u.other);
163 } else
164 tputs(dcc[idx].sock, SBUF, len);
165 }
166 }
167
168 void chatout EGG_VARARGS_DEF(char *, arg1)
169 {
170 int i;
171 char *format;
172 char s[601];
173
174 va_list va;
175 format = EGG_VARARGS_START(char *, arg1, va);
176
177 #ifdef HAVE_VSNPRINTF
178 if (vsnprintf(s, 511, format, va) < 0)
179 s[511] = 0;
180 #else
181 vsprintf(s, format, va);
182 #endif
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 va_end(va);
188 }
189
190 /* print to all on this channel but one */
191 void chanout_but EGG_VARARGS_DEF(int, arg1)
192 {
193 int i, x, chan;
194 char *format;
195 char s[601];
196
197 va_list va;
198 x = EGG_VARARGS_START(int, arg1, va);
199 chan = va_arg(va, int);
200 format = va_arg(va, char *);
201
202 #ifdef HAVE_VSNPRINTF
203 if (vsnprintf(s, 511, format, va) < 0)
204 s[511] = 0;
205 #else
206 vsprintf(s, format, va);
207 #endif
208 for (i = 0; i < dcc_total; i++)
209 if ((dcc[i].type == &DCC_CHAT) && (i != x))
210 if (dcc[i].u.chat->channel == chan)
211 dprintf(i, "%s", s);
212 va_end(va);
213 }
214
215 void dcc_chatter(int idx)
216 {
217 int i, j;
218 struct flag_record fr =
219 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
220
221 get_user_flagrec(dcc[idx].user, &fr, NULL);
222 dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
223 show_motd(idx);
224 dprintf(idx, "Commands start with '.' (like '.quit' or '.help')\n");
225 dprintf(idx, "Everything else goes out to the party line.\n\n");
226 i = dcc[idx].u.chat->channel;
227 dcc[idx].u.chat->channel = 234567;
228 j = dcc[idx].sock;
229 strcpy(dcc[idx].u.chat->con_chan, "***");
230 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
231 /* still there? */
232 if ((idx >= dcc_total) || (dcc[idx].sock != j))
233 return; /* nope */
234 /* tcl script may have taken control */
235 if (dcc[idx].type == &DCC_CHAT) {
236 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
237 strcpy(dcc[idx].u.chat->con_chan, "*");
238 if (dcc[idx].u.chat->channel == 234567) {
239 /* if the chat channel has already been altered it's *highly*
240 * probably join/part messages have been broadcast everywhere,
241 * so dont bother sending them */
242 if (i == -2)
243 i = 0;
244 dcc[idx].u.chat->channel = i;
245 if (dcc[idx].u.chat->channel >= 0) {
246 Context;
247 if (dcc[idx].u.chat->channel < 100000) {
248 botnet_send_join_idx(idx, -1);
249 }
250 }
251 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
252 geticon(idx), dcc[idx].sock, dcc[idx].host);
253 }
254 /* but *do* bother with sending it locally */
255 if (dcc[idx].u.chat->channel == 0) {
256 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
257 } else if (dcc[idx].u.chat->channel > 0) {
258 chanout_but(-1, dcc[idx].u.chat->channel,
259 "*** %s joined the channel.\n", dcc[idx].nick);
260 }
261 }
262 }
263
264 /* Mark an entry as lost and deconstruct it's contents. It will be securely
265 * removed from the dcc list in the main loop.
266 */
267 void lostdcc(int n)
268 {
269 if (dcc[n].type && dcc[n].type->kill)
270 dcc[n].type->kill(n, dcc[n].u.other);
271 else if (dcc[n].u.other)
272 nfree(dcc[n].u.other);
273 bzero(&dcc[n], sizeof(struct dcc_t));
274
275 dcc[n].sock = (-1);
276 dcc[n].type = &DCC_LOST;
277 }
278
279 /* Remove entry from dcc list. Think twice before using this function,
280 * because it invalidates any variables that point to a specific dcc
281 * entry!
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 my_memcpy((char *) &dcc[n], (char *) &dcc[dcc_total],
292 sizeof(struct dcc_t));
293 else
294 bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
295 }
296
297 /* Clean up sockets that were just left for dead.
298 */
299 void dcc_remove_lost(void)
300 {
301 int i;
302
303 Context;
304 for (i = 0; i < dcc_total; i++) {
305 if (dcc[i].type == &DCC_LOST) {
306 dcc[i].type = NULL;
307 dcc[i].sock = (-1);
308 removedcc(i);
309 i--;
310 }
311 }
312 }
313
314 /* show list of current dcc's to a dcc-chatter */
315 /* positive value: idx given -- negative value: sock given */
316 void tell_dcc(int zidx)
317 {
318 int i, j, k;
319 char other[160];
320
321 Context;
322 spaces[HANDLEN - 9] = 0;
323 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
324 ,spaces);
325 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
326 ,spaces);
327 spaces[HANDLEN - 9] = ' ';
328 /* show server */
329 for (i = 0; i < dcc_total; i++) {
330 j = strlen(dcc[i].host);
331 if (j > 17)
332 j -= 17;
333 else
334 j = 0;
335 if (dcc[i].type && dcc[i].type->display)
336 dcc[i].type->display(i, other);
337 else {
338 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
339 break;
340 }
341 k = HANDLEN - strlen(dcc[i].nick);
342 spaces[k] = 0;
343 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
344 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
345 spaces[k] = ' ';
346 }
347 }
348
349 /* mark someone on dcc chat as no longer away */
350 void not_away(int idx)
351 {
352 Context;
353 if (dcc[idx].u.chat->away == NULL) {
354 dprintf(idx, "You weren't away!\n");
355 return;
356 }
357 if (dcc[idx].u.chat->channel >= 0) {
358 chanout_but(-1, dcc[idx].u.chat->channel,
359 "*** %s is no longer away.\n", dcc[idx].nick);
360 Context;
361 if (dcc[idx].u.chat->channel < 100000) {
362 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
363 }
364 }
365 dprintf(idx, "You're not away any more.\n");
366 nfree(dcc[idx].u.chat->away);
367 dcc[idx].u.chat->away = NULL;
368 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
369 }
370
371 void set_away(int idx, char *s)
372 {
373 if (s == NULL) {
374 not_away(idx);
375 return;
376 }
377 if (!s[0]) {
378 not_away(idx);
379 return;
380 }
381 if (dcc[idx].u.chat->away != NULL)
382 nfree(dcc[idx].u.chat->away);
383 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
384 strcpy(dcc[idx].u.chat->away, s);
385 if (dcc[idx].u.chat->channel >= 0) {
386 chanout_but(-1, dcc[idx].u.chat->channel,
387 "*** %s is now away: %s\n", dcc[idx].nick, s);
388 Context;
389 if (dcc[idx].u.chat->channel < 100000) {
390 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
391 }
392 }
393 dprintf(idx, "You are now away.\n");
394 check_tcl_away(botnetnick, dcc[idx].sock, s);
395 }
396
397 /* this helps the memory debugging */
398 void *_get_data_ptr(int size, char *file, int line)
399 {
400 char *p;
401
402 #ifdef DEBUG_MEM
403 char x[1024];
404
405 simple_sprintf(x, "dccutil.c:%s", file);
406 p = n_malloc(size, x, line);
407 #else
408 p = nmalloc(size);
409 #endif
410 bzero(p, size);
411 return p;
412 }
413
414 /* make a password, 10-15 random letters and digits */
415 void makepass(char *s)
416 {
417 int i;
418
419 i = 10 + (random() % 6);
420 make_rand_str(s, i);
421 }
422
423 void flush_lines(int idx, struct chat_info *ci)
424 {
425 int c = ci->line_count;
426 struct msgq *p = ci->buffer, *o;
427
428 while (p && c < (ci->max_line)) {
429 ci->current_lines--;
430 tputs(dcc[idx].sock, p->msg, p->len);
431 nfree(p->msg);
432 o = p->next;
433 nfree(p);
434 p = o;
435 c++;
436 }
437 if (p != NULL) {
438 if (dcc[idx].status & STAT_TELNET)
439 tputs(dcc[idx].sock, "[More]: ", 8);
440 else
441 tputs(dcc[idx].sock, "[More]\n", 7);
442 }
443 ci->buffer = p;
444 ci->line_count = 0;
445 }
446
447 int new_dcc(struct dcc_table *type, int xtra_size)
448 {
449 int i = dcc_total;
450
451 if (dcc_total == max_dcc)
452 return -1;
453 dcc_total++;
454 bzero((char *) &dcc[i], sizeof(struct dcc_t));
455
456 dcc[i].type = type;
457 if (xtra_size) {
458 dcc[i].u.other = nmalloc(xtra_size);
459 bzero(dcc[i].u.other, xtra_size);
460 }
461 return i;
462
463 }
464
465 /* Changes the given dcc entry to another type */
466 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
467 {
468 /* free old structure */
469 if (dcc[i].type && dcc[i].type->kill)
470 dcc[i].type->kill(i, dcc[i].u.other);
471 else if (dcc[i].u.other) {
472 nfree(dcc[i].u.other);
473 dcc[i].u.other = NULL;
474 }
475
476 dcc[i].type = type;
477 if (xtra_size) {
478 dcc[i].u.other = nmalloc(xtra_size);
479 bzero(dcc[i].u.other, xtra_size);
480 }
481 }
482
483 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
484 {
485 time_t t;
486
487 if (dcc_flood_thr == 0)
488 return 0;
489 t = now;
490 if (*timer != t) {
491 *timer = t;
492 chat->msgs_per_sec = 0;
493 } else {
494 chat->msgs_per_sec++;
495 if (chat->msgs_per_sec > dcc_flood_thr) {
496 /* FLOOD */
497 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
498 /* evil assumption here that flags&DCT_CHAT implies chat type */
499 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
500 (chat->channel >= 0)) {
501 char x[1024];
502
503 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
504 chanout_but(idx, chat->channel, "*** %s", x);
505 if (chat->channel < 100000)
506 botnet_send_part_idx(idx, x);
507 }
508 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
509 if ((dcc[idx].sock != STDOUT) || backgrd) {
510 killsock(dcc[idx].sock);
511 lostdcc(idx);
512 } else {
513 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
514 dcc_chatter(idx);
515 }
516 return 1; /* <- flood */
517 }
518 }
519 return 0;
520 }
521
522 /* handle someone being booted from dcc chat */
523 void do_boot(int idx, char *by, char *reason)
524 {
525 int files = (dcc[idx].type != &DCC_CHAT);
526
527 dprintf(idx, DCC_BOOTED1);
528 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
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 simple_sprintf(x, "%s booted %s from the party line%s%s",
537 by, dcc[idx].nick, 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