/[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.5 - (show annotations) (download) (as text)
Thu Oct 21 19:23:01 1999 UTC (20 years, 1 month ago) by fabian
Branch: MAIN
Changes since 1.4: +6 -6 lines
File MIME type: text/x-chdr
resync with 1.3 tree, 939863113-940380771

1 /*
2 * dccutil.c -- handles:
3 * lots of little functions to send formatted text to varyinlg types
4 * of connections
5 * '.who', '.whom', and '.dccstat' code
6 * memory management for dcc structures
7 * timeout checking for dcc connections
8 * dprintf'ized, 28aug1995
9 */
10 /*
11 * This file is part of the eggdrop source code
12 * copyright (c) 1997 Robey Pointer
13 * and is distributed according to the GNU general public license.
14 * For full details, read the top of 'main.c' or the file called
15 * COPYING that was distributed with this code.
16 */
17
18 #include "main.h"
19 #include <sys/stat.h>
20 #include <errno.h>
21 #include "chan.h"
22 #include "modules.h"
23 #include "tandem.h"
24
25 extern struct dcc_t *dcc;
26 extern int dcc_total;
27 extern char botnetnick[];
28 extern char spaces[];
29 extern char version[];
30 extern time_t now;
31 extern int max_dcc;
32 extern int dcc_flood_thr;
33 extern int backgrd;
34
35 char motdfile[121] = "text/motd"; /* file where the motd is stored */
36 int connect_timeout = 15; /* how long to wait before a telnet
37 * connection times out */
38 int reserved_port = 0;
39
40 extern sock_list *socklist;
41 extern int MAXSOCKS;
42
43 void init_dcc_max()
44 {
45 int osock = MAXSOCKS;
46
47 if (max_dcc < 1)
48 max_dcc = 1;
49 if (dcc)
50 dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
51
52 else
53 dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
54
55 MAXSOCKS = max_dcc + 10;
56 if (socklist)
57 socklist = (sock_list *) nrealloc((void *) socklist,
58 sizeof(sock_list) * MAXSOCKS);
59 else
60 socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
61 for (; osock < MAXSOCKS; osock++)
62 socklist[osock].flags = SOCK_UNUSED;
63 }
64
65 int expmem_dccutil()
66 {
67 int tot, i;
68
69 context;
70 tot = sizeof(struct dcc_t) * max_dcc + sizeof(sock_list) * MAXSOCKS;
71
72 for (i = 0; i < dcc_total; i++) {
73 if (dcc[i].type && dcc[i].type->expmem)
74 tot += dcc[i].type->expmem(dcc[i].u.other);
75 }
76 return tot;
77 }
78
79 /* FIXME: should this be here ? */
80 static char SBUF[1024];
81
82 /* replace \n with \r\n */
83 char *add_cr(char *buf)
84 {
85 static char WBUF[1024];
86 char *p, *q;
87
88 for (p = buf, q = WBUF; *p; p++, q++) {
89 if (*p == '\n')
90 *q++ = '\r';
91 *q = *p;
92 }
93 *q = *p;
94 return WBUF;
95 }
96
97 extern void (*qserver) (int, char *, int);
98 void dprintf EGG_VARARGS_DEF(int, arg1)
99 {
100 char *format;
101 int idx, len;
102
103 va_list va;
104 idx = EGG_VARARGS_START(int, arg1, va);
105 format = va_arg(va, char *);
106
107 #ifdef HAVE_VSNPRINTF
108 if ((len = vsnprintf(SBUF, 1023, format, va)) < 0)
109 SBUF[len = 1023] = 0;
110 #else
111 len = vsprintf(SBUF, format, va);
112 #endif
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 qserver(idx, SBUF, len);
131 break;
132 }
133 return;
134 } else {
135 if (len > 500) { /* truncate to fit */
136 SBUF[500] = 0;
137 strcat(SBUF, "\n");
138 len = 501;
139 }
140 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
141 char *p = add_cr(SBUF);
142
143 tputs(dcc[idx].sock, p, strlen(p));
144 } else if (dcc[idx].type && dcc[idx].type->output) {
145 dcc[idx].type->output(idx, SBUF, dcc[idx].u.other);
146 } else
147 tputs(dcc[idx].sock, SBUF, len);
148 }
149 }
150
151 void chatout EGG_VARARGS_DEF(char *, arg1)
152 {
153 int i;
154 char *format;
155 char s[601];
156
157 va_list va;
158 format = EGG_VARARGS_START(char *, arg1, va);
159
160 #ifdef HAVE_VSNPRINTF
161 if (vsnprintf(s, 511, format, va) < 0)
162 s[511] = 0;
163 #else
164 vsprintf(s, format, va);
165 #endif
166 for (i = 0; i < dcc_total; i++)
167 if (dcc[i].type == &DCC_CHAT)
168 if (dcc[i].u.chat->channel >= 0)
169 dprintf(i, "%s", s);
170 va_end(va);
171 }
172
173 /* print to all on this channel but one */
174 void chanout_but EGG_VARARGS_DEF(int, arg1)
175 {
176 int i, x, chan;
177 char *format;
178 char s[601];
179
180 va_list va;
181 x = EGG_VARARGS_START(int, arg1, va);
182 chan = va_arg(va, int);
183 format = va_arg(va, char *);
184
185 #ifdef HAVE_VSNPRINTF
186 if (vsnprintf(s, 511, format, va) < 0)
187 s[511] = 0;
188 #else
189 vsprintf(s, format, va);
190 #endif
191 for (i = 0; i < dcc_total; i++)
192 if ((dcc[i].type == &DCC_CHAT) && (i != x))
193 if (dcc[i].u.chat->channel == chan)
194 dprintf(i, "%s", s);
195 va_end(va);
196 }
197
198 void dcc_chatter(int idx)
199 {
200 int i, j;
201 struct flag_record fr =
202 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
203
204 get_user_flagrec(dcc[idx].user, &fr, NULL);
205 dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
206 show_motd(idx);
207 dprintf(idx, "Commands start with '.' (like '.quit' or '.help')\n");
208 dprintf(idx, "Everything else goes out to the party line.\n\n");
209 i = dcc[idx].u.chat->channel;
210 dcc[idx].u.chat->channel = 234567;
211 j = dcc[idx].sock;
212 strcpy(dcc[idx].u.chat->con_chan, "***");
213 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
214 /* still there? */
215 if ((idx >= dcc_total) || (dcc[idx].sock != j))
216 return; /* nope */
217 /* tcl script may have taken control */
218 if (dcc[idx].type == &DCC_CHAT) {
219 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
220 strcpy(dcc[idx].u.chat->con_chan, "*");
221 if (dcc[idx].u.chat->channel == 234567) {
222 /* if the chat channel has already been altered it's *highly*
223 * probably join/part messages have been broadcast everywhere,
224 * so dont bother sending them */
225 if (i == -2)
226 i = 0;
227 dcc[idx].u.chat->channel = i;
228 if (dcc[idx].u.chat->channel >= 0) {
229 context;
230 if (dcc[idx].u.chat->channel < 100000) {
231 botnet_send_join_idx(idx, -1);
232 }
233 }
234 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
235 geticon(idx), dcc[idx].sock, dcc[idx].host);
236 }
237 /* but *do* bother with sending it locally */
238 if (dcc[idx].u.chat->channel == 0) {
239 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
240 } else if (dcc[idx].u.chat->channel > 0) {
241 chanout_but(-1, dcc[idx].u.chat->channel,
242 "*** %s joined the channel.\n", dcc[idx].nick);
243 }
244 }
245 }
246
247 /* remove entry from dcc list */
248 void lostdcc(int n)
249 {
250 if (dcc[n].type && dcc[n].type->kill)
251 dcc[n].type->kill(n, dcc[n].u.other);
252 else if (dcc[n].u.other)
253 nfree(dcc[n].u.other);
254 dcc_total--;
255 if (n < dcc_total)
256 my_memcpy((char *) &dcc[n], (char *) &dcc[dcc_total],
257 sizeof(struct dcc_t));
258 else
259 bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
260 }
261
262 /* show list of current dcc's to a dcc-chatter */
263 /* positive value: idx given -- negative value: sock given */
264 void tell_dcc(int zidx)
265 {
266 int i, j, k;
267 char other[160];
268
269 context;
270 spaces[HANDLEN - 9] = 0;
271 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
272 ,spaces);
273 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
274 ,spaces);
275 spaces[HANDLEN - 9] = ' ';
276 /* show server */
277 for (i = 0; i < dcc_total; i++) {
278 j = strlen(dcc[i].host);
279 if (j > 17)
280 j -= 17;
281 else
282 j = 0;
283 if (dcc[i].type && dcc[i].type->display)
284 dcc[i].type->display(i, other);
285 else {
286 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
287 break;
288 }
289 k = HANDLEN - strlen(dcc[i].nick);
290 spaces[k] = 0;
291 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
292 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
293 spaces[k] = ' ';
294 }
295 }
296
297 /* mark someone on dcc chat as no longer away */
298 void not_away(int idx)
299 {
300 context;
301 if (dcc[idx].u.chat->away == NULL) {
302 dprintf(idx, "You weren't away!\n");
303 return;
304 }
305 if (dcc[idx].u.chat->channel >= 0) {
306 chanout_but(-1, dcc[idx].u.chat->channel,
307 "*** %s is no longer away.\n", dcc[idx].nick);
308 context;
309 if (dcc[idx].u.chat->channel < 100000) {
310 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
311 }
312 }
313 dprintf(idx, "You're not away any more.\n");
314 nfree(dcc[idx].u.chat->away);
315 dcc[idx].u.chat->away = NULL;
316 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
317 }
318
319 void set_away(int idx, char *s)
320 {
321 if (s == NULL) {
322 not_away(idx);
323 return;
324 }
325 if (!s[0]) {
326 not_away(idx);
327 return;
328 }
329 if (dcc[idx].u.chat->away != NULL)
330 nfree(dcc[idx].u.chat->away);
331 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
332 strcpy(dcc[idx].u.chat->away, s);
333 if (dcc[idx].u.chat->channel >= 0) {
334 chanout_but(-1, dcc[idx].u.chat->channel,
335 "*** %s is now away: %s\n", dcc[idx].nick, s);
336 context;
337 if (dcc[idx].u.chat->channel < 100000) {
338 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
339 }
340 }
341 dprintf(idx, "You are now away.\n");
342 check_tcl_away(botnetnick, dcc[idx].sock, s);
343 }
344
345 /* this helps the memory debugging */
346 void *_get_data_ptr(int size, char *file, int line)
347 {
348 char *p;
349
350 #ifdef EBUG_MEM
351 char x[1024];
352
353 simple_sprintf(x, "dccutil.c:%s", file);
354 p = n_malloc(size, x, line);
355 #else
356 p = nmalloc(size);
357 #endif
358 bzero(p, size);
359 return p;
360 }
361
362 /* make a password, 10-15 random letters and digits */
363 void makepass(char *s)
364 {
365 int i;
366
367 i = 10 + (random() % 6);
368 make_rand_str(s, i);
369 }
370
371 void flush_lines(int idx, struct chat_info *ci)
372 {
373 int c = ci->line_count;
374 struct msgq *p = ci->buffer, *o;
375
376 while (p && c < (ci->max_line)) {
377 ci->current_lines--;
378 tputs(dcc[idx].sock, p->msg, p->len);
379 nfree(p->msg);
380 o = p->next;
381 nfree(p);
382 p = o;
383 c++;
384 }
385 if (p != NULL) {
386 if (dcc[idx].status & STAT_TELNET)
387 tputs(dcc[idx].sock, "[More]: ", 8);
388 else
389 tputs(dcc[idx].sock, "[More]\n", 7);
390 }
391 ci->buffer = p;
392 ci->line_count = 0;
393 }
394
395 int new_dcc(struct dcc_table *type, int xtra_size)
396 {
397 int i = dcc_total;
398
399 if (dcc_total == max_dcc)
400 return -1;
401 dcc_total++;
402 bzero((char *) &dcc[i], sizeof(struct dcc_t));
403
404 dcc[i].type = type;
405 if (xtra_size) {
406 dcc[i].u.other = nmalloc(xtra_size);
407 bzero(dcc[i].u.other, xtra_size);
408 }
409 return i;
410
411 }
412
413 /* Changes the given dcc entry to another type */
414 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
415 {
416 /* free old structure */
417 if (dcc[i].type && dcc[i].type->kill)
418 dcc[i].type->kill(i, dcc[i].u.other);
419 else if (dcc[i].u.other) {
420 nfree(dcc[i].u.other);
421 dcc[i].u.other = NULL;
422 }
423
424 dcc[i].type = type;
425 if (xtra_size) {
426 dcc[i].u.other = nmalloc(xtra_size);
427 bzero(dcc[i].u.other, xtra_size);
428 }
429 }
430
431
432 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
433 {
434 time_t t;
435
436 if (dcc_flood_thr == 0)
437 return 0;
438 t = now;
439 if (*timer != t) {
440 *timer = t;
441 chat->msgs_per_sec = 0;
442 } else {
443 chat->msgs_per_sec++;
444 if (chat->msgs_per_sec > dcc_flood_thr) {
445 /* FLOOD */
446 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
447 /* evil assumption here that flags&DCT_CHAT implies chat type */
448 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
449 (chat->channel >= 0)) {
450 char x[1024];
451
452 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
453 chanout_but(idx, chat->channel, "*** %s", x);
454 if (chat->channel < 100000)
455 botnet_send_part_idx(idx, x);
456 }
457 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
458 if ((dcc[idx].sock != STDOUT) || backgrd) {
459 killsock(dcc[idx].sock);
460 lostdcc(idx);
461 } else {
462 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
463 dcc_chatter(idx);
464 }
465 return 1; /* <- flood */
466 }
467 }
468 return 0;
469 }
470
471 /* handle someone being booted from dcc chat */
472 void do_boot(int idx, char *by, char *reason)
473 {
474 int files = (dcc[idx].type != &DCC_CHAT);
475
476 dprintf(idx, DCC_BOOTED1);
477 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
478 /* if it's a partyliner (chatterer :) */
479 /* horrible assumption that DCT_CHAT using structure uses same format
480 * as DCC_CHAT */
481 if ((dcc[idx].type->flags & DCT_CHAT) &&
482 (dcc[idx].u.chat->channel >= 0)) {
483 char x[1024];
484
485 simple_sprintf(x, "%s booted %s from the party line%s%s",
486 by, dcc[idx].nick, reason[0] ? ": " : "", reason);
487 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
488 if (dcc[idx].u.chat->channel < 100000)
489 botnet_send_part_idx(idx, x);
490 }
491 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
492 if ((dcc[idx].sock != STDOUT) || backgrd) {
493 killsock(dcc[idx].sock);
494 dcc[idx].sock = (long) dcc[idx].type;
495 dcc[idx].type = &DCC_LOST;
496 /* entry must remain in the table so it can be logged by the caller */
497 } else {
498 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
499 dcc_chatter(idx);
500 }
501 return;
502 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23