/[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.11 - (show annotations) (download) (as text)
Wed Dec 22 12:11:02 1999 UTC (19 years, 10 months ago) by fabian
Branch: MAIN
Changes since 1.10: +13 -4 lines
File MIME type: text/x-chdr
resync with 1.4.2, 1999/12/22

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23