/[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.10 - (show annotations) (download) (as text)
Tue Dec 21 17:35:09 1999 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.9: +26 -12 lines
File MIME type: text/x-chdr
Tothwolf's id-header

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$
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 /* remove entry from dcc list */
265 void lostdcc(int n)
266 {
267 if (dcc[n].type && dcc[n].type->kill)
268 dcc[n].type->kill(n, dcc[n].u.other);
269 else if (dcc[n].u.other)
270 nfree(dcc[n].u.other);
271 dcc_total--;
272 if (n < dcc_total)
273 my_memcpy((char *) &dcc[n], (char *) &dcc[dcc_total],
274 sizeof(struct dcc_t));
275 else
276 bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
277 }
278
279 /* show list of current dcc's to a dcc-chatter */
280 /* positive value: idx given -- negative value: sock given */
281 void tell_dcc(int zidx)
282 {
283 int i, j, k;
284 char other[160];
285
286 Context;
287 spaces[HANDLEN - 9] = 0;
288 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
289 ,spaces);
290 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
291 ,spaces);
292 spaces[HANDLEN - 9] = ' ';
293 /* show server */
294 for (i = 0; i < dcc_total; i++) {
295 j = strlen(dcc[i].host);
296 if (j > 17)
297 j -= 17;
298 else
299 j = 0;
300 if (dcc[i].type && dcc[i].type->display)
301 dcc[i].type->display(i, other);
302 else {
303 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
304 break;
305 }
306 k = HANDLEN - strlen(dcc[i].nick);
307 spaces[k] = 0;
308 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
309 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
310 spaces[k] = ' ';
311 }
312 }
313
314 /* mark someone on dcc chat as no longer away */
315 void not_away(int idx)
316 {
317 Context;
318 if (dcc[idx].u.chat->away == NULL) {
319 dprintf(idx, "You weren't away!\n");
320 return;
321 }
322 if (dcc[idx].u.chat->channel >= 0) {
323 chanout_but(-1, dcc[idx].u.chat->channel,
324 "*** %s is no longer away.\n", dcc[idx].nick);
325 Context;
326 if (dcc[idx].u.chat->channel < 100000) {
327 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
328 }
329 }
330 dprintf(idx, "You're not away any more.\n");
331 nfree(dcc[idx].u.chat->away);
332 dcc[idx].u.chat->away = NULL;
333 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
334 }
335
336 void set_away(int idx, char *s)
337 {
338 if (s == NULL) {
339 not_away(idx);
340 return;
341 }
342 if (!s[0]) {
343 not_away(idx);
344 return;
345 }
346 if (dcc[idx].u.chat->away != NULL)
347 nfree(dcc[idx].u.chat->away);
348 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
349 strcpy(dcc[idx].u.chat->away, s);
350 if (dcc[idx].u.chat->channel >= 0) {
351 chanout_but(-1, dcc[idx].u.chat->channel,
352 "*** %s is now away: %s\n", dcc[idx].nick, s);
353 Context;
354 if (dcc[idx].u.chat->channel < 100000) {
355 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
356 }
357 }
358 dprintf(idx, "You are now away.\n");
359 check_tcl_away(botnetnick, dcc[idx].sock, s);
360 }
361
362 /* this helps the memory debugging */
363 void *_get_data_ptr(int size, char *file, int line)
364 {
365 char *p;
366
367 #ifdef DEBUG_MEM
368 char x[1024];
369
370 simple_sprintf(x, "dccutil.c:%s", file);
371 p = n_malloc(size, x, line);
372 #else
373 p = nmalloc(size);
374 #endif
375 bzero(p, size);
376 return p;
377 }
378
379 /* make a password, 10-15 random letters and digits */
380 void makepass(char *s)
381 {
382 int i;
383
384 i = 10 + (random() % 6);
385 make_rand_str(s, i);
386 }
387
388 void flush_lines(int idx, struct chat_info *ci)
389 {
390 int c = ci->line_count;
391 struct msgq *p = ci->buffer, *o;
392
393 while (p && c < (ci->max_line)) {
394 ci->current_lines--;
395 tputs(dcc[idx].sock, p->msg, p->len);
396 nfree(p->msg);
397 o = p->next;
398 nfree(p);
399 p = o;
400 c++;
401 }
402 if (p != NULL) {
403 if (dcc[idx].status & STAT_TELNET)
404 tputs(dcc[idx].sock, "[More]: ", 8);
405 else
406 tputs(dcc[idx].sock, "[More]\n", 7);
407 }
408 ci->buffer = p;
409 ci->line_count = 0;
410 }
411
412 int new_dcc(struct dcc_table *type, int xtra_size)
413 {
414 int i = dcc_total;
415
416 if (dcc_total == max_dcc)
417 return -1;
418 dcc_total++;
419 bzero((char *) &dcc[i], sizeof(struct dcc_t));
420
421 dcc[i].type = type;
422 if (xtra_size) {
423 dcc[i].u.other = nmalloc(xtra_size);
424 bzero(dcc[i].u.other, xtra_size);
425 }
426 return i;
427
428 }
429
430 /* Changes the given dcc entry to another type */
431 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
432 {
433 /* free old structure */
434 if (dcc[i].type && dcc[i].type->kill)
435 dcc[i].type->kill(i, dcc[i].u.other);
436 else if (dcc[i].u.other) {
437 nfree(dcc[i].u.other);
438 dcc[i].u.other = NULL;
439 }
440
441 dcc[i].type = type;
442 if (xtra_size) {
443 dcc[i].u.other = nmalloc(xtra_size);
444 bzero(dcc[i].u.other, xtra_size);
445 }
446 }
447
448 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
449 {
450 time_t t;
451
452 if (dcc_flood_thr == 0)
453 return 0;
454 t = now;
455 if (*timer != t) {
456 *timer = t;
457 chat->msgs_per_sec = 0;
458 } else {
459 chat->msgs_per_sec++;
460 if (chat->msgs_per_sec > dcc_flood_thr) {
461 /* FLOOD */
462 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
463 /* evil assumption here that flags&DCT_CHAT implies chat type */
464 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
465 (chat->channel >= 0)) {
466 char x[1024];
467
468 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
469 chanout_but(idx, chat->channel, "*** %s", x);
470 if (chat->channel < 100000)
471 botnet_send_part_idx(idx, x);
472 }
473 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
474 if ((dcc[idx].sock != STDOUT) || backgrd) {
475 killsock(dcc[idx].sock);
476 lostdcc(idx);
477 } else {
478 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
479 dcc_chatter(idx);
480 }
481 return 1; /* <- flood */
482 }
483 }
484 return 0;
485 }
486
487 /* handle someone being booted from dcc chat */
488 void do_boot(int idx, char *by, char *reason)
489 {
490 int files = (dcc[idx].type != &DCC_CHAT);
491
492 dprintf(idx, DCC_BOOTED1);
493 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
494 /* if it's a partyliner (chatterer :) */
495 /* horrible assumption that DCT_CHAT using structure uses same format
496 * as DCC_CHAT */
497 if ((dcc[idx].type->flags & DCT_CHAT) &&
498 (dcc[idx].u.chat->channel >= 0)) {
499 char x[1024];
500
501 simple_sprintf(x, "%s booted %s from the party line%s%s",
502 by, dcc[idx].nick, reason[0] ? ": " : "", reason);
503 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
504 if (dcc[idx].u.chat->channel < 100000)
505 botnet_send_part_idx(idx, x);
506 }
507 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
508 if ((dcc[idx].sock != STDOUT) || backgrd) {
509 killsock(dcc[idx].sock);
510 dcc[idx].sock = (long) dcc[idx].type;
511 dcc[idx].type = &DCC_LOST;
512 /* entry must remain in the table so it can be logged by the caller */
513 } else {
514 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
515 dcc_chatter(idx);
516 }
517 return;
518 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23