/[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.21 - (show annotations) (download) (as text)
Sun May 14 22:16:46 2000 UTC (19 years, 5 months ago) by fabian
Branch: MAIN
Changes since 1.20: +1 -4 lines
File MIME type: text/x-chdr
Wiktor's login2 patch

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.20 2000/04/05 19:55:12 fabian 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 int reserved_port = 0;
47
48
49 void init_dcc_max()
50 {
51 int osock = MAXSOCKS;
52
53 if (max_dcc < 1)
54 max_dcc = 1;
55 if (dcc)
56 dcc = nrealloc(dcc, sizeof(struct dcc_t) * max_dcc);
57 else
58 dcc = nmalloc(sizeof(struct dcc_t) * max_dcc);
59
60 MAXSOCKS = max_dcc + 10;
61 if (socklist)
62 socklist = (sock_list *) nrealloc((void *) socklist,
63 sizeof(sock_list) * MAXSOCKS);
64 else
65 socklist = (sock_list *) nmalloc(sizeof(sock_list) * MAXSOCKS);
66 for (; osock < MAXSOCKS; osock++)
67 socklist[osock].flags = SOCK_UNUSED;
68 }
69
70 int expmem_dccutil()
71 {
72 int tot, i;
73
74 Context;
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 Context;
221 if (dcc[idx].u.chat->channel < 100000) {
222 botnet_send_join_idx(idx, -1);
223 }
224 }
225 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
226 geticon(idx), dcc[idx].sock, dcc[idx].host);
227 }
228 /* But *do* bother with sending it locally */
229 if (dcc[idx].u.chat->channel == 0) {
230 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
231 } else if (dcc[idx].u.chat->channel > 0) {
232 chanout_but(-1, dcc[idx].u.chat->channel,
233 "*** %s joined the channel.\n", dcc[idx].nick);
234 }
235 }
236 }
237
238 /* Mark an entry as lost and deconstruct it's contents. It will be securely
239 * removed from the dcc list in the main loop.
240 */
241 void lostdcc(int n)
242 {
243 if (dcc[n].type && dcc[n].type->kill)
244 dcc[n].type->kill(n, dcc[n].u.other);
245 else if (dcc[n].u.other)
246 nfree(dcc[n].u.other);
247 egg_bzero(&dcc[n], sizeof(struct dcc_t));
248
249 dcc[n].sock = (-1);
250 dcc[n].type = &DCC_LOST;
251 }
252
253 /* Remove entry from dcc list. Think twice before using this function,
254 * because it invalidates any variables that point to a specific dcc
255 * entry!
256 *
257 * Note: The entry will be deconstructed if it was not deconstructed
258 * already. This case should normally not occur.
259 */
260 void removedcc(int n)
261 {
262 if (dcc[n].type && dcc[n].type->kill)
263 dcc[n].type->kill(n, dcc[n].u.other);
264 else if (dcc[n].u.other)
265 nfree(dcc[n].u.other);
266 dcc_total--;
267 if (n < dcc_total)
268 egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
269 else
270 egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
271 }
272
273 /* Clean up sockets that were just left for dead.
274 */
275 void dcc_remove_lost(void)
276 {
277 int i;
278
279 Context;
280 for (i = 0; i < dcc_total; i++) {
281 if (dcc[i].type == &DCC_LOST) {
282 dcc[i].type = NULL;
283 dcc[i].sock = (-1);
284 removedcc(i);
285 i--;
286 }
287 }
288 }
289
290 /* Show list of current dcc's to a dcc-chatter
291 * positive value: idx given -- negative value: sock given
292 */
293 void tell_dcc(int zidx)
294 {
295 int i, j, k;
296 char other[160];
297
298 Context;
299 spaces[HANDLEN - 9] = 0;
300 dprintf(zidx, "SOCK ADDR PORT NICK %s HOST TYPE\n"
301 ,spaces);
302 dprintf(zidx, "---- -------- ----- ---------%s ----------------- ----\n"
303 ,spaces);
304 spaces[HANDLEN - 9] = ' ';
305 /* Show server */
306 for (i = 0; i < dcc_total; i++) {
307 j = strlen(dcc[i].host);
308 if (j > 17)
309 j -= 17;
310 else
311 j = 0;
312 if (dcc[i].type && dcc[i].type->display)
313 dcc[i].type->display(i, other);
314 else {
315 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
316 break;
317 }
318 k = HANDLEN - strlen(dcc[i].nick);
319 spaces[k] = 0;
320 dprintf(zidx, "%-4d %08X %5d %s%s %-17s %s\n", dcc[i].sock, dcc[i].addr,
321 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
322 spaces[k] = ' ';
323 }
324 }
325
326 /* Mark someone on dcc chat as no longer away
327 */
328 void not_away(int idx)
329 {
330 Context;
331 if (dcc[idx].u.chat->away == NULL) {
332 dprintf(idx, "You weren't away!\n");
333 return;
334 }
335 if (dcc[idx].u.chat->channel >= 0) {
336 chanout_but(-1, dcc[idx].u.chat->channel,
337 "*** %s is no longer away.\n", dcc[idx].nick);
338 Context;
339 if (dcc[idx].u.chat->channel < 100000) {
340 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
341 }
342 }
343 dprintf(idx, "You're not away any more.\n");
344 nfree(dcc[idx].u.chat->away);
345 dcc[idx].u.chat->away = NULL;
346 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
347 }
348
349 void set_away(int idx, char *s)
350 {
351 if (s == NULL) {
352 not_away(idx);
353 return;
354 }
355 if (!s[0]) {
356 not_away(idx);
357 return;
358 }
359 if (dcc[idx].u.chat->away != NULL)
360 nfree(dcc[idx].u.chat->away);
361 dcc[idx].u.chat->away = (char *) nmalloc(strlen(s) + 1);
362 strcpy(dcc[idx].u.chat->away, s);
363 if (dcc[idx].u.chat->channel >= 0) {
364 chanout_but(-1, dcc[idx].u.chat->channel,
365 "*** %s is now away: %s\n", dcc[idx].nick, s);
366 Context;
367 if (dcc[idx].u.chat->channel < 100000) {
368 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
369 }
370 }
371 dprintf(idx, "You are now away.\n");
372 check_tcl_away(botnetnick, dcc[idx].sock, s);
373 }
374
375 /* This helps the memory debugging
376 */
377 void *_get_data_ptr(int size, char *file, int line)
378 {
379 char *p;
380 #ifdef DEBUG_MEM
381 char x[1024];
382
383 p = strrchr(file, '/');
384 simple_sprintf(x, "dccutil.c:%s", p ? p + 1 : file);
385 p = n_malloc(size, x, line);
386 #else
387 p = nmalloc(size);
388 #endif
389 egg_bzero(p, size);
390 return p;
391 }
392
393 /* Make a password, 10-15 random letters and digits
394 */
395 void makepass(char *s)
396 {
397 int i;
398
399 i = 10 + (random() % 6);
400 make_rand_str(s, i);
401 }
402
403 void flush_lines(int idx, struct chat_info *ci)
404 {
405 int c = ci->line_count;
406 struct msgq *p = ci->buffer, *o;
407
408 while (p && c < (ci->max_line)) {
409 ci->current_lines--;
410 tputs(dcc[idx].sock, p->msg, p->len);
411 nfree(p->msg);
412 o = p->next;
413 nfree(p);
414 p = o;
415 c++;
416 }
417 if (p != NULL) {
418 if (dcc[idx].status & STAT_TELNET)
419 tputs(dcc[idx].sock, "[More]: ", 8);
420 else
421 tputs(dcc[idx].sock, "[More]\n", 7);
422 }
423 ci->buffer = p;
424 ci->line_count = 0;
425 }
426
427 int new_dcc(struct dcc_table *type, int xtra_size)
428 {
429 int i = dcc_total;
430
431 if (dcc_total == max_dcc)
432 return -1;
433 dcc_total++;
434 egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
435
436 dcc[i].type = type;
437 if (xtra_size) {
438 dcc[i].u.other = nmalloc(xtra_size);
439 egg_bzero(dcc[i].u.other, xtra_size);
440 }
441 return i;
442 }
443
444 /* Changes the given dcc entry to another type */
445 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
446 {
447 /* Free old structure */
448 if (dcc[i].type && dcc[i].type->kill)
449 dcc[i].type->kill(i, dcc[i].u.other);
450 else if (dcc[i].u.other) {
451 nfree(dcc[i].u.other);
452 dcc[i].u.other = NULL;
453 }
454
455 dcc[i].type = type;
456 if (xtra_size) {
457 dcc[i].u.other = nmalloc(xtra_size);
458 egg_bzero(dcc[i].u.other, xtra_size);
459 }
460 }
461
462 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
463 {
464 time_t t;
465
466 if (dcc_flood_thr == 0)
467 return 0;
468 t = now;
469 if (*timer != t) {
470 *timer = t;
471 chat->msgs_per_sec = 0;
472 } else {
473 chat->msgs_per_sec++;
474 if (chat->msgs_per_sec > dcc_flood_thr) {
475 /* FLOOD */
476 dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
477 /* Evil assumption here that flags&DCT_CHAT implies chat type */
478 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
479 (chat->channel >= 0)) {
480 char x[1024];
481
482 simple_sprintf(x, DCC_FLOODBOOT, dcc[idx].nick);
483 chanout_but(idx, chat->channel, "*** %s", x);
484 if (chat->channel < 100000)
485 botnet_send_part_idx(idx, x);
486 }
487 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
488 if ((dcc[idx].sock != STDOUT) || backgrd) {
489 killsock(dcc[idx].sock);
490 lostdcc(idx);
491 } else {
492 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
493 dcc_chatter(idx);
494 }
495 return 1; /* <- flood */
496 }
497 }
498 return 0;
499 }
500
501 /* Handle someone being booted from dcc chat.
502 */
503 void do_boot(int idx, char *by, char *reason)
504 {
505 int files = (dcc[idx].type != &DCC_CHAT);
506
507 dprintf(idx, DCC_BOOTED1);
508 dprintf(idx, DCC_BOOTED2, DCC_BOOTED2_ARGS);
509 /* If it's a partyliner (chatterer :) */
510 /* Horrible assumption that DCT_CHAT using structure uses same format
511 * as DCC_CHAT */
512 if ((dcc[idx].type->flags & DCT_CHAT) &&
513 (dcc[idx].u.chat->channel >= 0)) {
514 char x[1024];
515
516 simple_sprintf(x, "%s booted %s from the party line%s%s",
517 by, dcc[idx].nick, reason[0] ? ": " : "", reason);
518 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
519 if (dcc[idx].u.chat->channel < 100000)
520 botnet_send_part_idx(idx, x);
521 }
522 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
523 if ((dcc[idx].sock != STDOUT) || backgrd) {
524 killsock(dcc[idx].sock);
525 lostdcc(idx);
526 /* Entry must remain in the table so it can be logged by the caller */
527 } else {
528 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
529 dcc_chatter(idx);
530 }
531 return;
532 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23