/[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.9 - (show annotations) (download) (as text)
Sat Dec 11 18:58:03 1999 UTC (19 years, 10 months ago) by fabian
Branch: MAIN
Changes since 1.8: +7 -7 lines
File MIME type: text/x-chdr
tothwolf's remove-context-bloat patch

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 static char SBUF[1024];
80
81 /* replace \n with \r\n */
82 char *add_cr(char *buf)
83 {
84 static char WBUF[1024];
85 char *p, *q;
86
87 for (p = buf, q = WBUF; *p; p++, q++) {
88 if (*p == '\n')
89 *q++ = '\r';
90 *q = *p;
91 }
92 *q = *p;
93 return WBUF;
94 }
95
96 extern void (*qserver) (int, char *, int);
97
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 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
160 va_list va;
161 format = EGG_VARARGS_START(char *, arg1, va);
162
163 #ifdef HAVE_VSNPRINTF
164 if (vsnprintf(s, 511, format, va) < 0)
165 s[511] = 0;
166 #else
167 vsprintf(s, format, va);
168 #endif
169 for (i = 0; i < dcc_total; i++)
170 if (dcc[i].type == &DCC_CHAT)
171 if (dcc[i].u.chat->channel >= 0)
172 dprintf(i, "%s", s);
173 va_end(va);
174 }
175
176 /* print to all on this channel but one */
177 void chanout_but EGG_VARARGS_DEF(int, arg1)
178 {
179 int i, x, chan;
180 char *format;
181 char s[601];
182
183 va_list va;
184 x = EGG_VARARGS_START(int, arg1, va);
185 chan = va_arg(va, int);
186 format = va_arg(va, char *);
187
188 #ifdef HAVE_VSNPRINTF
189 if (vsnprintf(s, 511, format, va) < 0)
190 s[511] = 0;
191 #else
192 vsprintf(s, format, va);
193 #endif
194 for (i = 0; i < dcc_total; i++)
195 if ((dcc[i].type == &DCC_CHAT) && (i != x))
196 if (dcc[i].u.chat->channel == chan)
197 dprintf(i, "%s", s);
198 va_end(va);
199 }
200
201 void dcc_chatter(int idx)
202 {
203 int i, j;
204 struct flag_record fr =
205 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
206
207 get_user_flagrec(dcc[idx].user, &fr, NULL);
208 dprintf(idx, "Connected to %s, running %s\n", botnetnick, version);
209 show_motd(idx);
210 dprintf(idx, "Commands start with '.' (like '.quit' or '.help')\n");
211 dprintf(idx, "Everything else goes out to the party line.\n\n");
212 i = dcc[idx].u.chat->channel;
213 dcc[idx].u.chat->channel = 234567;
214 j = dcc[idx].sock;
215 strcpy(dcc[idx].u.chat->con_chan, "***");
216 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
217 /* still there? */
218 if ((idx >= dcc_total) || (dcc[idx].sock != j))
219 return; /* nope */
220 /* tcl script may have taken control */
221 if (dcc[idx].type == &DCC_CHAT) {
222 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
223 strcpy(dcc[idx].u.chat->con_chan, "*");
224 if (dcc[idx].u.chat->channel == 234567) {
225 /* if the chat channel has already been altered it's *highly*
226 * probably join/part messages have been broadcast everywhere,
227 * so dont bother sending them */
228 if (i == -2)
229 i = 0;
230 dcc[idx].u.chat->channel = i;
231 if (dcc[idx].u.chat->channel >= 0) {
232 Context;
233 if (dcc[idx].u.chat->channel < 100000) {
234 botnet_send_join_idx(idx, -1);
235 }
236 }
237 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
238 geticon(idx), dcc[idx].sock, dcc[idx].host);
239 }
240 /* but *do* bother with sending it locally */
241 if (dcc[idx].u.chat->channel == 0) {
242 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
243 } else if (dcc[idx].u.chat->channel > 0) {
244 chanout_but(-1, dcc[idx].u.chat->channel,
245 "*** %s joined the channel.\n", dcc[idx].nick);
246 }
247 }
248 }
249
250 /* remove entry from dcc list */
251 void lostdcc(int n)
252 {
253 if (dcc[n].type && dcc[n].type->kill)
254 dcc[n].type->kill(n, dcc[n].u.other);
255 else if (dcc[n].u.other)
256 nfree(dcc[n].u.other);
257 dcc_total--;
258 if (n < dcc_total)
259 my_memcpy((char *) &dcc[n], (char *) &dcc[dcc_total],
260 sizeof(struct dcc_t));
261 else
262 bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
263 }
264
265 /* show list of current dcc's to a dcc-chatter */
266 /* positive value: idx given -- negative value: sock given */
267 void tell_dcc(int zidx)
268 {
269 int i, j, k;
270 char other[160];
271
272 Context;
273 spaces[HANDLEN - 9] = 0;
274 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
275 ,spaces);
276 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
277 ,spaces);
278 spaces[HANDLEN - 9] = ' ';
279 /* show server */
280 for (i = 0; i < dcc_total; i++) {
281 j = strlen(dcc[i].host);
282 if (j > 17)
283 j -= 17;
284 else
285 j = 0;
286 if (dcc[i].type && dcc[i].type->display)
287 dcc[i].type->display(i, other);
288 else {
289 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
290 break;
291 }
292 k = HANDLEN - strlen(dcc[i].nick);
293 spaces[k] = 0;
294 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
295 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
296 spaces[k] = ' ';
297 }
298 }
299
300 /* mark someone on dcc chat as no longer away */
301 void not_away(int idx)
302 {
303 Context;
304 if (dcc[idx].u.chat->away == NULL) {
305 dprintf(idx, "You weren't away!\n");
306 return;
307 }
308 if (dcc[idx].u.chat->channel >= 0) {
309 chanout_but(-1, dcc[idx].u.chat->channel,
310 "*** %s is no longer away.\n", dcc[idx].nick);
311 Context;
312 if (dcc[idx].u.chat->channel < 100000) {
313 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
314 }
315 }
316 dprintf(idx, "You're not away any more.\n");
317 nfree(dcc[idx].u.chat->away);
318 dcc[idx].u.chat->away = NULL;
319 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
320 }
321
322 void set_away(int idx, char *s)
323 {
324 if (s == NULL) {
325 not_away(idx);
326 return;
327 }
328 if (!s[0]) {
329 not_away(idx);
330 return;
331 }
332 if (dcc[idx].u.chat->away != NULL)
333 nfree(dcc[idx].u.chat->away);
334 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
335 strcpy(dcc[idx].u.chat->away, s);
336 if (dcc[idx].u.chat->channel >= 0) {
337 chanout_but(-1, dcc[idx].u.chat->channel,
338 "*** %s is now away: %s\n", dcc[idx].nick, s);
339 Context;
340 if (dcc[idx].u.chat->channel < 100000) {
341 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
342 }
343 }
344 dprintf(idx, "You are now away.\n");
345 check_tcl_away(botnetnick, dcc[idx].sock, s);
346 }
347
348 /* this helps the memory debugging */
349 void *_get_data_ptr(int size, char *file, int line)
350 {
351 char *p;
352
353 #ifdef DEBUG_MEM
354 char x[1024];
355
356 simple_sprintf(x, "dccutil.c:%s", file);
357 p = n_malloc(size, x, line);
358 #else
359 p = nmalloc(size);
360 #endif
361 bzero(p, size);
362 return p;
363 }
364
365 /* make a password, 10-15 random letters and digits */
366 void makepass(char *s)
367 {
368 int i;
369
370 i = 10 + (random() % 6);
371 make_rand_str(s, i);
372 }
373
374 void flush_lines(int idx, struct chat_info *ci)
375 {
376 int c = ci->line_count;
377 struct msgq *p = ci->buffer, *o;
378
379 while (p && c < (ci->max_line)) {
380 ci->current_lines--;
381 tputs(dcc[idx].sock, p->msg, p->len);
382 nfree(p->msg);
383 o = p->next;
384 nfree(p);
385 p = o;
386 c++;
387 }
388 if (p != NULL) {
389 if (dcc[idx].status & STAT_TELNET)
390 tputs(dcc[idx].sock, "[More]: ", 8);
391 else
392 tputs(dcc[idx].sock, "[More]\n", 7);
393 }
394 ci->buffer = p;
395 ci->line_count = 0;
396 }
397
398 int new_dcc(struct dcc_table *type, int xtra_size)
399 {
400 int i = dcc_total;
401
402 if (dcc_total == max_dcc)
403 return -1;
404 dcc_total++;
405 bzero((char *) &dcc[i], sizeof(struct dcc_t));
406
407 dcc[i].type = type;
408 if (xtra_size) {
409 dcc[i].u.other = nmalloc(xtra_size);
410 bzero(dcc[i].u.other, xtra_size);
411 }
412 return i;
413
414 }
415
416 /* Changes the given dcc entry to another type */
417 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
418 {
419 /* free old structure */
420 if (dcc[i].type && dcc[i].type->kill)
421 dcc[i].type->kill(i, dcc[i].u.other);
422 else if (dcc[i].u.other) {
423 nfree(dcc[i].u.other);
424 dcc[i].u.other = NULL;
425 }
426
427 dcc[i].type = type;
428 if (xtra_size) {
429 dcc[i].u.other = nmalloc(xtra_size);
430 bzero(dcc[i].u.other, xtra_size);
431 }
432 }
433
434 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
435 {
436 time_t t;
437
438 if (dcc_flood_thr == 0)
439 return 0;
440 t = now;
441 if (*timer != t) {
442 *timer = t;
443 chat->msgs_per_sec = 0;
444 } else {
445 chat->msgs_per_sec++;
446 if (chat->msgs_per_sec > dcc_flood_thr) {
447 /* FLOOD */
448 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
449 /* evil assumption here that flags&DCT_CHAT implies chat type */
450 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
451 (chat->channel >= 0)) {
452 char x[1024];
453
454 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
455 chanout_but(idx, chat->channel, "*** %s", x);
456 if (chat->channel < 100000)
457 botnet_send_part_idx(idx, x);
458 }
459 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
460 if ((dcc[idx].sock != STDOUT) || backgrd) {
461 killsock(dcc[idx].sock);
462 lostdcc(idx);
463 } else {
464 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
465 dcc_chatter(idx);
466 }
467 return 1; /* <- flood */
468 }
469 }
470 return 0;
471 }
472
473 /* handle someone being booted from dcc chat */
474 void do_boot(int idx, char *by, char *reason)
475 {
476 int files = (dcc[idx].type != &DCC_CHAT);
477
478 dprintf(idx, DCC_BOOTED1);
479 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
480 /* if it's a partyliner (chatterer :) */
481 /* horrible assumption that DCT_CHAT using structure uses same format
482 * as DCC_CHAT */
483 if ((dcc[idx].type->flags & DCT_CHAT) &&
484 (dcc[idx].u.chat->channel >= 0)) {
485 char x[1024];
486
487 simple_sprintf(x, "%s booted %s from the party line%s%s",
488 by, dcc[idx].nick, reason[0] ? ": " : "", reason);
489 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
490 if (dcc[idx].u.chat->channel < 100000)
491 botnet_send_part_idx(idx, x);
492 }
493 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
494 if ((dcc[idx].sock != STDOUT) || backgrd) {
495 killsock(dcc[idx].sock);
496 dcc[idx].sock = (long) dcc[idx].type;
497 dcc[idx].type = &DCC_LOST;
498 /* entry must remain in the table so it can be logged by the caller */
499 } else {
500 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
501 dcc_chatter(idx);
502 }
503 return;
504 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23