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

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

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


Revision 1.13 - (show annotations) (download) (as text)
Mon Jan 17 16:14:45 2000 UTC (19 years, 10 months ago) by per
Branch: MAIN
CVS Tags: eggdrop104030RC2, eggdrop10403RC1, eggdrop10402RC1, eggdrop10404, eggdrop10403, eggdrop10402, HEAD
Changes since 1.12: +3 -8 lines
File MIME type: text/x-chdr
relayfix, extern_cleanup

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23