/[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.19 - (show annotations) (download) (as text)
Thu Mar 23 23:17:55 2000 UTC (19 years, 7 months ago) by fabian
Branch: MAIN
Changes since 1.18: +8 -9 lines
File MIME type: text/x-chdr
conf_make_fix6 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.18 2000/03/22 00:42:57 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 if ((len = egg_vsnprintf(SBUF, 1023, format, va)) < 0)
112 SBUF[len = 1023] = 0;
113 va_end(va);
114 if (idx < 0) {
115 tputs(-idx, SBUF, len);
116 } else if (idx > 0x7FF0) {
117 switch (idx) {
118 case DP_LOG:
119 putlog(LOG_MISC, "*", "%s", SBUF);
120 break;
121 case DP_STDOUT:
122 tputs(STDOUT, SBUF, len);
123 break;
124 case DP_STDERR:
125 tputs(STDERR, SBUF, 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, SBUF, len);
134 break;
135 }
136 return;
137 } else {
138 if (len > 500) { /* Truncate to fit */
139 SBUF[500] = 0;
140 strcat(SBUF, "\n");
141 len = 501;
142 }
143 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
144 char *p = add_cr(SBUF);
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, SBUF, dcc[idx].u.other);
149 } else
150 tputs(dcc[idx].sock, SBUF, len);
151 }
152 }
153
154 void chatout EGG_VARARGS_DEF(char *, arg1)
155 {
156 int i;
157 char *format;
158 char s[601];
159 va_list va;
160
161 format = EGG_VARARGS_START(char *, arg1, va);
162 if (egg_vsnprintf(s, 511, format, va) < 0)
163 s[511] = 0;
164 for (i = 0; i < dcc_total; i++)
165 if (dcc[i].type == &DCC_CHAT)
166 if (dcc[i].u.chat->channel >= 0)
167 dprintf(i, "%s", s);
168 va_end(va);
169 }
170
171 /* Print to all on this channel but one.
172 */
173 void chanout_but EGG_VARARGS_DEF(int, arg1)
174 {
175 int i, x, chan;
176 char *format;
177 char s[601];
178 va_list va;
179
180 x = EGG_VARARGS_START(int, arg1, va);
181 chan = va_arg(va, int);
182 format = va_arg(va, char *);
183 if (egg_vsnprintf(s, 511, format, va) < 0)
184 s[511] = 0;
185 for (i = 0; i < dcc_total; i++)
186 if ((dcc[i].type == &DCC_CHAT) && (i != x))
187 if (dcc[i].u.chat->channel == chan)
188 dprintf(i, "%s", s);
189 va_end(va);
190 }
191
192 void dcc_chatter(int idx)
193 {
194 int i, j;
195 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
196
197 get_user_flagrec(dcc[idx].user, &fr, NULL);
198 dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
199 show_motd(idx);
200 dprintf(idx, "Commands start with '.' (like '.quit' or '.help')\n");
201 dprintf(idx, "Everything else goes out to the party line.\n\n");
202 i = dcc[idx].u.chat->channel;
203 dcc[idx].u.chat->channel = 234567;
204 j = dcc[idx].sock;
205 strcpy(dcc[idx].u.chat->con_chan, "***");
206 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
207 /* Still there? */
208 if ((idx >= dcc_total) || (dcc[idx].sock != j))
209 return; /* Nope */
210 /* Tcl script may have taken control */
211 if (dcc[idx].type == &DCC_CHAT) {
212 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
213 strcpy(dcc[idx].u.chat->con_chan, "*");
214 if (dcc[idx].u.chat->channel == 234567) {
215 /* If the chat channel has already been altered it's *highly*
216 * probably join/part messages have been broadcast everywhere,
217 * so dont bother sending them
218 */
219 if (i == -2)
220 i = 0;
221 dcc[idx].u.chat->channel = i;
222 if (dcc[idx].u.chat->channel >= 0) {
223 Context;
224 if (dcc[idx].u.chat->channel < 100000) {
225 botnet_send_join_idx(idx, -1);
226 }
227 }
228 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
229 geticon(idx), dcc[idx].sock, dcc[idx].host);
230 }
231 /* But *do* bother with sending it locally */
232 if (dcc[idx].u.chat->channel == 0) {
233 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
234 } else if (dcc[idx].u.chat->channel > 0) {
235 chanout_but(-1, dcc[idx].u.chat->channel,
236 "*** %s joined the channel.\n", dcc[idx].nick);
237 }
238 }
239 }
240
241 /* Mark an entry as lost and deconstruct it's contents. It will be securely
242 * removed from the dcc list in the main loop.
243 */
244 void lostdcc(int n)
245 {
246 if (dcc[n].type && dcc[n].type->kill)
247 dcc[n].type->kill(n, dcc[n].u.other);
248 else if (dcc[n].u.other)
249 nfree(dcc[n].u.other);
250 egg_bzero(&dcc[n], sizeof(struct dcc_t));
251
252 dcc[n].sock = (-1);
253 dcc[n].type = &DCC_LOST;
254 }
255
256 /* Remove entry from dcc list. Think twice before using this function,
257 * because it invalidates any variables that point to a specific dcc
258 * entry!
259 *
260 * Note: The entry will be deconstructed if it was not deconstructed
261 * already. This case should normally not occur.
262 */
263 void removedcc(int n)
264 {
265 if (dcc[n].type && dcc[n].type->kill)
266 dcc[n].type->kill(n, dcc[n].u.other);
267 else if (dcc[n].u.other)
268 nfree(dcc[n].u.other);
269 dcc_total--;
270 if (n < dcc_total)
271 egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
272 else
273 egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
274 }
275
276 /* Clean up sockets that were just left for dead.
277 */
278 void dcc_remove_lost(void)
279 {
280 int i;
281
282 Context;
283 for (i = 0; i < dcc_total; i++) {
284 if (dcc[i].type == &DCC_LOST) {
285 dcc[i].type = NULL;
286 dcc[i].sock = (-1);
287 removedcc(i);
288 i--;
289 }
290 }
291 }
292
293 /* Show list of current dcc's to a dcc-chatter
294 * positive value: idx given -- negative value: sock given
295 */
296 void tell_dcc(int zidx)
297 {
298 int i, j, k;
299 char other[160];
300
301 Context;
302 spaces[HANDLEN - 9] = 0;
303 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
304 ,spaces);
305 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
306 ,spaces);
307 spaces[HANDLEN - 9] = ' ';
308 /* Show server */
309 for (i = 0; i < dcc_total; i++) {
310 j = strlen(dcc[i].host);
311 if (j > 17)
312 j -= 17;
313 else
314 j = 0;
315 if (dcc[i].type && dcc[i].type->display)
316 dcc[i].type->display(i, other);
317 else {
318 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
319 break;
320 }
321 k = HANDLEN - strlen(dcc[i].nick);
322 spaces[k] = 0;
323 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
324 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
325 spaces[k] = ' ';
326 }
327 }
328
329 /* Mark someone on dcc chat as no longer away
330 */
331 void not_away(int idx)
332 {
333 Context;
334 if (dcc[idx].u.chat->away == NULL) {
335 dprintf(idx, "You weren't away!\n");
336 return;
337 }
338 if (dcc[idx].u.chat->channel >= 0) {
339 chanout_but(-1, dcc[idx].u.chat->channel,
340 "*** %s is no longer away.\n", dcc[idx].nick);
341 Context;
342 if (dcc[idx].u.chat->channel < 100000) {
343 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
344 }
345 }
346 dprintf(idx, "You're not away any more.\n");
347 nfree(dcc[idx].u.chat->away);
348 dcc[idx].u.chat->away = NULL;
349 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
350 }
351
352 void set_away(int idx, char *s)
353 {
354 if (s == NULL) {
355 not_away(idx);
356 return;
357 }
358 if (!s[0]) {
359 not_away(idx);
360 return;
361 }
362 if (dcc[idx].u.chat->away != NULL)
363 nfree(dcc[idx].u.chat->away);
364 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
365 strcpy(dcc[idx].u.chat->away, s);
366 if (dcc[idx].u.chat->channel >= 0) {
367 chanout_but(-1, dcc[idx].u.chat->channel,
368 "*** %s is now away: %s\n", dcc[idx].nick, s);
369 Context;
370 if (dcc[idx].u.chat->channel < 100000) {
371 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
372 }
373 }
374 dprintf(idx, "You are now away.\n");
375 check_tcl_away(botnetnick, dcc[idx].sock, s);
376 }
377
378 /* This helps the memory debugging
379 */
380 void *_get_data_ptr(int size, char *file, int line)
381 {
382 char *p;
383 #ifdef DEBUG_MEM
384 char x[1024];
385
386 simple_sprintf(x, "dccutil.c:%s", file);
387 p = n_malloc(size, x, line);
388 #else
389 p = nmalloc(size);
390 #endif
391 egg_bzero(p, size);
392 return p;
393 }
394
395 /* Make a password, 10-15 random letters and digits
396 */
397 void makepass(char *s)
398 {
399 int i;
400
401 i = 10 + (random() % 6);
402 make_rand_str(s, i);
403 }
404
405 void flush_lines(int idx, struct chat_info *ci)
406 {
407 int c = ci->line_count;
408 struct msgq *p = ci->buffer, *o;
409
410 while (p && c < (ci->max_line)) {
411 ci->current_lines--;
412 tputs(dcc[idx].sock, p->msg, p->len);
413 nfree(p->msg);
414 o = p->next;
415 nfree(p);
416 p = o;
417 c++;
418 }
419 if (p != NULL) {
420 if (dcc[idx].status & STAT_TELNET)
421 tputs(dcc[idx].sock, "[More]: ", 8);
422 else
423 tputs(dcc[idx].sock, "[More]\n", 7);
424 }
425 ci->buffer = p;
426 ci->line_count = 0;
427 }
428
429 int new_dcc(struct dcc_table *type, int xtra_size)
430 {
431 int i = dcc_total;
432
433 if (dcc_total == max_dcc)
434 return -1;
435 dcc_total++;
436 egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
437
438 dcc[i].type = type;
439 if (xtra_size) {
440 dcc[i].u.other = nmalloc(xtra_size);
441 egg_bzero(dcc[i].u.other, xtra_size);
442 }
443 return i;
444 }
445
446 /* Changes the given dcc entry to another type */
447 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
448 {
449 /* Free old structure */
450 if (dcc[i].type && dcc[i].type->kill)
451 dcc[i].type->kill(i, dcc[i].u.other);
452 else if (dcc[i].u.other) {
453 nfree(dcc[i].u.other);
454 dcc[i].u.other = NULL;
455 }
456
457 dcc[i].type = type;
458 if (xtra_size) {
459 dcc[i].u.other = nmalloc(xtra_size);
460 egg_bzero(dcc[i].u.other, xtra_size);
461 }
462 }
463
464 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
465 {
466 time_t t;
467
468 if (dcc_flood_thr == 0)
469 return 0;
470 t = now;
471 if (*timer != t) {
472 *timer = t;
473 chat->msgs_per_sec = 0;
474 } else {
475 chat->msgs_per_sec++;
476 if (chat->msgs_per_sec > dcc_flood_thr) {
477 /* FLOOD */
478 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
479 /* Evil assumption here that flags&DCT_CHAT implies chat type */
480 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
481 (chat->channel >= 0)) {
482 char x[1024];
483
484 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
485 chanout_but(idx, chat->channel, "*** %s", x);
486 if (chat->channel < 100000)
487 botnet_send_part_idx(idx, x);
488 }
489 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
490 if ((dcc[idx].sock != STDOUT) || backgrd) {
491 killsock(dcc[idx].sock);
492 lostdcc(idx);
493 } else {
494 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
495 dcc_chatter(idx);
496 }
497 return 1; /* <- flood */
498 }
499 }
500 return 0;
501 }
502
503 /* Handle someone being booted from dcc chat.
504 */
505 void do_boot(int idx, char *by, char *reason)
506 {
507 int files = (dcc[idx].type != &DCC_CHAT);
508
509 dprintf(idx, DCC_BOOTED1);
510 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
511 /* If it's a partyliner (chatterer :) */
512 /* Horrible assumption that DCT_CHAT using structure uses same format
513 * as DCC_CHAT */
514 if ((dcc[idx].type->flags & DCT_CHAT) &&
515 (dcc[idx].u.chat->channel >= 0)) {
516 char x[1024];
517
518 simple_sprintf(x, "%s booted %s from the party line%s%s",
519 by, dcc[idx].nick, reason[0] ? ": " : "", reason);
520 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
521 if (dcc[idx].u.chat->channel < 100000)
522 botnet_send_part_idx(idx, x);
523 }
524 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
525 if ((dcc[idx].sock != STDOUT) || backgrd) {
526 killsock(dcc[idx].sock);
527 lostdcc(idx);
528 /* Entry must remain in the table so it can be logged by the caller */
529 } else {
530 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
531 dcc_chatter(idx);
532 }
533 return;
534 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23