/[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.17 - (show annotations) (download) (as text)
Sun Jan 30 19:26:20 2000 UTC (19 years, 8 months ago) by fabian
Branch: MAIN
CVS Tags: eggdrop105020
Changes since 1.16: +47 -47 lines
File MIME type: text/x-chdr
cleanup3 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 * $Id: dccutil.c,v 1.16 2000/01/17 22:36:06 fabian Exp $
10 */
11 /*
12 * Copyright (C) 1997 Robey Pointer
13 * Copyright (C) 1999, 2000 Eggheads
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, max_dcc, dcc_flood_thr, backgrd, MAXSOCKS;
39 extern char botnetnick[], spaces[], 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 int reserved_port = 0;
47
48
49 void init_dcc_max()
50 {
51 int osock = MAXSOCKS;
52
53 if (max_dcc < 1)
54 max_dcc = 1;
55 if (dcc)
56 dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
57 else
58 dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
59
60 MAXSOCKS = max_dcc + 10;
61 if (socklist)
62 socklist = (sock_list *) nrealloc((void *) socklist,
63 sizeof(sock_list) * MAXSOCKS);
64 else
65 socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
66 for (; osock < MAXSOCKS; osock++)
67 socklist[osock].flags = SOCK_UNUSED;
68 }
69
70 int expmem_dccutil()
71 {
72 int tot, i;
73
74 Context;
75 tot = sizeof(struct dcc_t) * max_dcc + sizeof(sock_list) * MAXSOCKS;
76
77 for (i = 0; i < dcc_total; i++) {
78 if (dcc[i].type && dcc[i].type->expmem)
79 tot += dcc[i].type->expmem(dcc[i].u.other);
80 }
81 return tot;
82 }
83
84 static char SBUF[1024];
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 char *format;
106 int idx, len;
107 va_list va;
108
109 idx = EGG_VARARGS_START(int, arg1, va);
110 format = va_arg(va, char *);
111 #ifdef HAVE_VSNPRINTF
112 if ((len = vsnprintf(SBUF, 1023, format, va)) < 0)
113 SBUF[len = 1023] = 0;
114 #else
115 len = vsprintf(SBUF, format, va);
116 #endif
117 va_end(va);
118 if (idx < 0) {
119 tputs(-idx, SBUF, len);
120 } else if (idx > 0x7FF0) {
121 switch (idx) {
122 case DP_LOG:
123 putlog(LOG_MISC, "*", "%s", SBUF);
124 break;
125 case DP_STDOUT:
126 tputs(STDOUT, SBUF, len);
127 break;
128 case DP_STDERR:
129 tputs(STDERR, SBUF, len);
130 break;
131 case DP_SERVER:
132 case DP_HELP:
133 case DP_MODE:
134 case DP_MODE_NEXT:
135 case DP_SERVER_NEXT:
136 case DP_HELP_NEXT:
137 qserver(idx, SBUF, len);
138 break;
139 }
140 return;
141 } else {
142 if (len > 500) { /* Truncate to fit */
143 SBUF[500] = 0;
144 strcat(SBUF, "\n");
145 len = 501;
146 }
147 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
148 char *p = add_cr(SBUF);
149
150 tputs(dcc[idx].sock, p, strlen(p));
151 } else if (dcc[idx].type && dcc[idx].type->output) {
152 dcc[idx].type->output(idx, SBUF, dcc[idx].u.other);
153 } else
154 tputs(dcc[idx].sock, SBUF, len);
155 }
156 }
157
158 void chatout EGG_VARARGS_DEF(char *, arg1)
159 {
160 int i;
161 char *format;
162 char s[601];
163 va_list va;
164
165 format = EGG_VARARGS_START(char *, arg1, va);
166 #ifdef HAVE_VSNPRINTF
167 if (vsnprintf(s, 511, format, va) < 0)
168 s[511] = 0;
169 #else
170 vsprintf(s, format, va);
171 #endif
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 va_end(va);
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;
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 #ifdef HAVE_VSNPRINTF
192 if (vsnprintf(s, 511, format, va) < 0)
193 s[511] = 0;
194 #else
195 vsprintf(s, format, va);
196 #endif
197 for (i = 0; i < dcc_total; i++)
198 if ((dcc[i].type == &DCC_CHAT) && (i != x))
199 if (dcc[i].u.chat->channel == chan)
200 dprintf(i, "%s", s);
201 va_end(va);
202 }
203
204 void dcc_chatter(int idx)
205 {
206 int i, j;
207 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
208
209 get_user_flagrec(dcc[idx].user, &fr, NULL);
210 dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
211 show_motd(idx);
212 dprintf(idx, "Commands start with '.' (like '.quit' or '.help')\n");
213 dprintf(idx, "Everything else goes out to the party line.\n\n");
214 i = dcc[idx].u.chat->channel;
215 dcc[idx].u.chat->channel = 234567;
216 j = dcc[idx].sock;
217 strcpy(dcc[idx].u.chat->con_chan, "***");
218 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
219 /* Still there? */
220 if ((idx >= dcc_total) || (dcc[idx].sock != j))
221 return; /* Nope */
222 /* Tcl script may have taken control */
223 if (dcc[idx].type == &DCC_CHAT) {
224 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
225 strcpy(dcc[idx].u.chat->con_chan, "*");
226 if (dcc[idx].u.chat->channel == 234567) {
227 /* If the chat channel has already been altered it's *highly*
228 * probably join/part messages have been broadcast everywhere,
229 * so dont bother sending them
230 */
231 if (i == -2)
232 i = 0;
233 dcc[idx].u.chat->channel = i;
234 if (dcc[idx].u.chat->channel >= 0) {
235 Context;
236 if (dcc[idx].u.chat->channel < 100000) {
237 botnet_send_join_idx(idx, -1);
238 }
239 }
240 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
241 geticon(idx), dcc[idx].sock, dcc[idx].host);
242 }
243 /* But *do* bother with sending it locally */
244 if (dcc[idx].u.chat->channel == 0) {
245 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
246 } else if (dcc[idx].u.chat->channel > 0) {
247 chanout_but(-1, dcc[idx].u.chat->channel,
248 "*** %s joined the channel.\n", dcc[idx].nick);
249 }
250 }
251 }
252
253 /* Mark an entry as lost and deconstruct it's contents. It will be securely
254 * removed from the dcc list in the main loop.
255 */
256 void lostdcc(int n)
257 {
258 if (dcc[n].type && dcc[n].type->kill)
259 dcc[n].type->kill(n, dcc[n].u.other);
260 else if (dcc[n].u.other)
261 nfree(dcc[n].u.other);
262 bzero(&dcc[n], sizeof(struct dcc_t));
263
264 dcc[n].sock = (-1);
265 dcc[n].type = &DCC_LOST;
266 }
267
268 /* Remove entry from dcc list. Think twice before using this function,
269 * because it invalidates any variables that point to a specific dcc
270 * entry!
271 *
272 * Note: The entry will be deconstructed if it was not deconstructed
273 * already. This case should normally not occur.
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 */
309 void tell_dcc(int zidx)
310 {
311 int i, j, k;
312 char other[160];
313
314 Context;
315 spaces[HANDLEN - 9] = 0;
316 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
317 ,spaces);
318 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
319 ,spaces);
320 spaces[HANDLEN - 9] = ' ';
321 /* Show server */
322 for (i = 0; i < dcc_total; i++) {
323 j = strlen(dcc[i].host);
324 if (j > 17)
325 j -= 17;
326 else
327 j = 0;
328 if (dcc[i].type && dcc[i].type->display)
329 dcc[i].type->display(i, other);
330 else {
331 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
332 break;
333 }
334 k = HANDLEN - strlen(dcc[i].nick);
335 spaces[k] = 0;
336 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
337 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
338 spaces[k] = ' ';
339 }
340 }
341
342 /* Mark someone on dcc chat as no longer away
343 */
344 void not_away(int idx)
345 {
346 Context;
347 if (dcc[idx].u.chat->away == NULL) {
348 dprintf(idx, "You weren't away!\n");
349 return;
350 }
351 if (dcc[idx].u.chat->channel >= 0) {
352 chanout_but(-1, dcc[idx].u.chat->channel,
353 "*** %s is no longer away.\n", dcc[idx].nick);
354 Context;
355 if (dcc[idx].u.chat->channel < 100000) {
356 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
357 }
358 }
359 dprintf(idx, "You're not away any more.\n");
360 nfree(dcc[idx].u.chat->away);
361 dcc[idx].u.chat->away = NULL;
362 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
363 }
364
365 void set_away(int idx, char *s)
366 {
367 if (s == NULL) {
368 not_away(idx);
369 return;
370 }
371 if (!s[0]) {
372 not_away(idx);
373 return;
374 }
375 if (dcc[idx].u.chat->away != NULL)
376 nfree(dcc[idx].u.chat->away);
377 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
378 strcpy(dcc[idx].u.chat->away, s);
379 if (dcc[idx].u.chat->channel >= 0) {
380 chanout_but(-1, dcc[idx].u.chat->channel,
381 "*** %s is now away: %s\n", dcc[idx].nick, s);
382 Context;
383 if (dcc[idx].u.chat->channel < 100000) {
384 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
385 }
386 }
387 dprintf(idx, "You are now away.\n");
388 check_tcl_away(botnetnick, dcc[idx].sock, s);
389 }
390
391 /* This helps the memory debugging
392 */
393 void *_get_data_ptr(int size, char *file, int line)
394 {
395 char *p;
396 #ifdef DEBUG_MEM
397 char x[1024];
398
399 simple_sprintf(x, "dccutil.c:%s", file);
400 p = n_malloc(size, x, line);
401 #else
402 p = nmalloc(size);
403 #endif
404 bzero(p, size);
405 return p;
406 }
407
408 /* Make a password, 10-15 random letters and digits
409 */
410 void makepass(char *s)
411 {
412 int i;
413
414 i = 10 + (random() % 6);
415 make_rand_str(s, i);
416 }
417
418 void flush_lines(int idx, struct chat_info *ci)
419 {
420 int c = ci->line_count;
421 struct msgq *p = ci->buffer, *o;
422
423 while (p && c < (ci->max_line)) {
424 ci->current_lines--;
425 tputs(dcc[idx].sock, p->msg, p->len);
426 nfree(p->msg);
427 o = p->next;
428 nfree(p);
429 p = o;
430 c++;
431 }
432 if (p != NULL) {
433 if (dcc[idx].status & STAT_TELNET)
434 tputs(dcc[idx].sock, "[More]: ", 8);
435 else
436 tputs(dcc[idx].sock, "[More]\n", 7);
437 }
438 ci->buffer = p;
439 ci->line_count = 0;
440 }
441
442 int new_dcc(struct dcc_table *type, int xtra_size)
443 {
444 int i = dcc_total;
445
446 if (dcc_total == max_dcc)
447 return -1;
448 dcc_total++;
449 bzero((char *) &dcc[i], sizeof(struct dcc_t));
450
451 dcc[i].type = type;
452 if (xtra_size) {
453 dcc[i].u.other = nmalloc(xtra_size);
454 bzero(dcc[i].u.other, xtra_size);
455 }
456 return i;
457 }
458
459 /* Changes the given dcc entry to another type */
460 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
461 {
462 /* Free old structure */
463 if (dcc[i].type && dcc[i].type->kill)
464 dcc[i].type->kill(i, dcc[i].u.other);
465 else if (dcc[i].u.other) {
466 nfree(dcc[i].u.other);
467 dcc[i].u.other = NULL;
468 }
469
470 dcc[i].type = type;
471 if (xtra_size) {
472 dcc[i].u.other = nmalloc(xtra_size);
473 bzero(dcc[i].u.other, xtra_size);
474 }
475 }
476
477 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
478 {
479 time_t t;
480
481 if (dcc_flood_thr == 0)
482 return 0;
483 t = now;
484 if (*timer != t) {
485 *timer = t;
486 chat->msgs_per_sec = 0;
487 } else {
488 chat->msgs_per_sec++;
489 if (chat->msgs_per_sec > dcc_flood_thr) {
490 /* FLOOD */
491 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
492 /* Evil assumption here that flags&DCT_CHAT implies chat type */
493 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
494 (chat->channel >= 0)) {
495 char x[1024];
496
497 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
498 chanout_but(idx, chat->channel, "*** %s", x);
499 if (chat->channel < 100000)
500 botnet_send_part_idx(idx, x);
501 }
502 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
503 if ((dcc[idx].sock != STDOUT) || backgrd) {
504 killsock(dcc[idx].sock);
505 lostdcc(idx);
506 } else {
507 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
508 dcc_chatter(idx);
509 }
510 return 1; /* <- flood */
511 }
512 }
513 return 0;
514 }
515
516 /* Handle someone being booted from dcc chat.
517 */
518 void do_boot(int idx, char *by, char *reason)
519 {
520 int files = (dcc[idx].type != &DCC_CHAT);
521
522 dprintf(idx, DCC_BOOTED1);
523 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
524 /* If it's a partyliner (chatterer :) */
525 /* Horrible assumption that DCT_CHAT using structure uses same format
526 * as DCC_CHAT */
527 if ((dcc[idx].type->flags & DCT_CHAT) &&
528 (dcc[idx].u.chat->channel >= 0)) {
529 char x[1024];
530
531 simple_sprintf(x, "%s booted %s from the party line%s%s",
532 by, dcc[idx].nick, reason[0] ? ": " : "", reason);
533 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
534 if (dcc[idx].u.chat->channel < 100000)
535 botnet_send_part_idx(idx, x);
536 }
537 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
538 if ((dcc[idx].sock != STDOUT) || backgrd) {
539 killsock(dcc[idx].sock);
540 lostdcc(idx);
541 /* Entry must remain in the table so it can be logged by the caller */
542 } else {
543 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
544 dcc_chatter(idx);
545 }
546 return;
547 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23