/[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.27 - (show annotations) (download) (as text)
Sat Jan 27 06:26:40 2001 UTC (18 years, 8 months ago) by tothwolf
Branch: MAIN
Changes since 1.26: +1 -16 lines
File MIME type: text/x-chdr
*** empty log message ***

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.26 2001/01/26 21:18:22 guppy 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
47 int reserved_port_min = 0;
48 int reserved_port_max = 0;
49
50 void init_dcc_max()
51 {
52 int osock = MAXSOCKS;
53
54 if (max_dcc < 1)
55 max_dcc = 1;
56 if (dcc)
57 dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
58 else
59 dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
60
61 MAXSOCKS = max_dcc + 10;
62 if (socklist)
63 socklist = (sock_list *) nrealloc((void *) socklist,
64 sizeof(sock_list) * MAXSOCKS);
65 else
66 socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
67 for (; osock < MAXSOCKS; osock++)
68 socklist[osock].flags = SOCK_UNUSED;
69 }
70
71 int expmem_dccutil()
72 {
73 int tot, i;
74
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 show_motd(idx);
199 i = dcc[idx].u.chat->channel;
200 dcc[idx].u.chat->channel = 234567;
201 j = dcc[idx].sock;
202 strcpy(dcc[idx].u.chat->con_chan, "***");
203 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
204 /* Still there? */
205 if ((idx >= dcc_total) || (dcc[idx].sock != j))
206 return; /* Nope */
207 /* Tcl script may have taken control */
208 if (dcc[idx].type == &DCC_CHAT) {
209 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
210 strcpy(dcc[idx].u.chat->con_chan, "*");
211 if (dcc[idx].u.chat->channel == 234567) {
212 /* If the chat channel has already been altered it's *highly*
213 * probably join/part messages have been broadcast everywhere,
214 * so dont bother sending them
215 */
216 if (i == -2)
217 i = 0;
218 dcc[idx].u.chat->channel = i;
219 if (dcc[idx].u.chat->channel >= 0) {
220 if (dcc[idx].u.chat->channel < 100000) {
221 botnet_send_join_idx(idx, -1);
222 }
223 }
224 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
225 geticon(idx), dcc[idx].sock, dcc[idx].host);
226 }
227 /* But *do* bother with sending it locally */
228 if (!dcc[idx].u.chat->channel) {
229 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
230 } else if (dcc[idx].u.chat->channel > 0) {
231 chanout_but(-1, dcc[idx].u.chat->channel,
232 "*** %s joined the channel.\n", dcc[idx].nick);
233 }
234 }
235 }
236
237 /* Mark an entry as lost and deconstruct it's contents. It will be securely
238 * removed from the dcc list in the main loop.
239 */
240 void lostdcc(int n)
241 {
242 if (dcc[n].type && dcc[n].type->kill)
243 dcc[n].type->kill(n, dcc[n].u.other);
244 else if (dcc[n].u.other)
245 nfree(dcc[n].u.other);
246 egg_bzero(&dcc[n], sizeof(struct dcc_t));
247
248 dcc[n].sock = (-1);
249 dcc[n].type = &DCC_LOST;
250 }
251
252 /* Remove entry from dcc list. Think twice before using this function,
253 * because it invalidates any variables that point to a specific dcc
254 * entry!
255 *
256 * Note: The entry will be deconstructed if it was not deconstructed
257 * already. This case should normally not occur.
258 */
259 void removedcc(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 dcc_total--;
266 if (n < dcc_total)
267 egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
268 else
269 egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
270 }
271
272 /* Clean up sockets that were just left for dead.
273 */
274 void dcc_remove_lost(void)
275 {
276 int i;
277
278 for (i = 0; i < dcc_total; i++) {
279 if (dcc[i].type == &DCC_LOST) {
280 dcc[i].type = NULL;
281 dcc[i].sock = (-1);
282 removedcc(i);
283 i--;
284 }
285 }
286 }
287
288 /* Show list of current dcc's to a dcc-chatter
289 * positive value: idx given -- negative value: sock given
290 */
291 void tell_dcc(int zidx)
292 {
293 int i, j, k;
294 char other[160];
295
296 spaces[HANDLEN - 9] = 0;
297 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
298 ,spaces);
299 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
300 ,spaces);
301 spaces[HANDLEN - 9] = ' ';
302 /* Show server */
303 for (i = 0; i < dcc_total; i++) {
304 j = strlen(dcc[i].host);
305 if (j > 17)
306 j -= 17;
307 else
308 j = 0;
309 if (dcc[i].type && dcc[i].type->display)
310 dcc[i].type->display(i, other);
311 else {
312 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
313 break;
314 }
315 k = HANDLEN - strlen(dcc[i].nick);
316 spaces[k] = 0;
317 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
318 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
319 spaces[k] = ' ';
320 }
321 }
322
323 /* Mark someone on dcc chat as no longer away
324 */
325 void not_away(int idx)
326 {
327 if (dcc[idx].u.chat->away == NULL) {
328 dprintf(idx, "You weren't away!\n");
329 return;
330 }
331 if (dcc[idx].u.chat->channel >= 0) {
332 chanout_but(-1, dcc[idx].u.chat->channel,
333 "*** %s is no longer away.\n", dcc[idx].nick);
334 if (dcc[idx].u.chat->channel < 100000) {
335 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
336 }
337 }
338 dprintf(idx, "You're not away any more.\n");
339 nfree(dcc[idx].u.chat->away);
340 dcc[idx].u.chat->away = NULL;
341 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
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 nfree(dcc[idx].u.chat->away);
356 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
357 strcpy(dcc[idx].u.chat->away, s);
358 if (dcc[idx].u.chat->channel >= 0) {
359 chanout_but(-1, dcc[idx].u.chat->channel,
360 "*** %s is now away: %s\n", dcc[idx].nick, s);
361 if (dcc[idx].u.chat->channel < 100000) {
362 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
363 }
364 }
365 dprintf(idx, "You are now away.\n");
366 check_tcl_away(botnetnick, dcc[idx].sock, s);
367 }
368
369 /* This helps the memory debugging
370 */
371 void *_get_data_ptr(int size, char *file, int line)
372 {
373 char *p;
374 #ifdef DEBUG_MEM
375 char x[1024];
376
377 p = strrchr(file, '/');
378 egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
379 p = n_malloc(size, x, line);
380 #else
381 p = nmalloc(size);
382 #endif
383 egg_bzero(p, size);
384 return p;
385 }
386
387 /* Make a password, 10-15 random letters and digits
388 */
389 void makepass(char *s)
390 {
391 int i;
392
393 i = 10 + (random() % 6);
394 make_rand_str(s, i);
395 }
396
397 void flush_lines(int idx, struct chat_info *ci)
398 {
399 int c = ci->line_count;
400 struct msgq *p = ci->buffer, *o;
401
402 while (p && c < (ci->max_line)) {
403 ci->current_lines--;
404 tputs(dcc[idx].sock, p->msg, p->len);
405 nfree(p->msg);
406 o = p->next;
407 nfree(p);
408 p = o;
409 c++;
410 }
411 if (p != NULL) {
412 if (dcc[idx].status & STAT_TELNET)
413 tputs(dcc[idx].sock, "[More]: ", 8);
414 else
415 tputs(dcc[idx].sock, "[More]\n", 7);
416 }
417 ci->buffer = p;
418 ci->line_count = 0;
419 }
420
421 int new_dcc(struct dcc_table *type, int xtra_size)
422 {
423 int i = dcc_total;
424
425 if (dcc_total == max_dcc)
426 return -1;
427 dcc_total++;
428 egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
429
430 dcc[i].type = type;
431 if (xtra_size) {
432 dcc[i].u.other = nmalloc(xtra_size);
433 egg_bzero(dcc[i].u.other, xtra_size);
434 }
435 return i;
436 }
437
438 /* Changes the given dcc entry to another type.
439 */
440 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
441 {
442 /* Free old structure. */
443 if (dcc[i].type && dcc[i].type->kill)
444 dcc[i].type->kill(i, dcc[i].u.other);
445 else if (dcc[i].u.other) {
446 nfree(dcc[i].u.other);
447 dcc[i].u.other = NULL;
448 }
449
450 dcc[i].type = type;
451 if (xtra_size) {
452 dcc[i].u.other = nmalloc(xtra_size);
453 egg_bzero(dcc[i].u.other, xtra_size);
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)
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 egg_snprintf(x, sizeof 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 */
498 void do_boot(int idx, char *by, char *reason)
499 {
500 int files = (dcc[idx].type != &DCC_CHAT);
501
502 dprintf(idx, DCC_BOOTED1);
503 dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot",
504 by, reason[0] ? ": " : ".", reason);
505 /* If it's a partyliner (chatterer :) */
506 /* Horrible assumption that DCT_CHAT using structure uses same format
507 * as DCC_CHAT */
508 if ((dcc[idx].type->flags & DCT_CHAT) &&
509 (dcc[idx].u.chat->channel >= 0)) {
510 char x[1024];
511
512 egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick,
513 reason[0] ? ": " : "", reason);
514 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
515 if (dcc[idx].u.chat->channel < 100000)
516 botnet_send_part_idx(idx, x);
517 }
518 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
519 if ((dcc[idx].sock != STDOUT) || backgrd) {
520 killsock(dcc[idx].sock);
521 lostdcc(idx);
522 /* Entry must remain in the table so it can be logged by the caller */
523 } else {
524 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
525 dcc_chatter(idx);
526 }
527 return;
528 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23