/[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.38 - (show annotations) (download) (as text)
Thu Oct 11 11:34:19 2001 UTC (18 years ago) by tothwolf
Branch: MAIN
Changes since 1.37: +3 -3 lines
File MIME type: text/x-chdr
* Use calloc() instead of malloc_memset macro

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.37 2001/10/10 10:44:04 tothwolf Exp $
10 */
11 /*
12 * Copyright (C) 1997 Robey Pointer
13 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
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 extern Tcl_Interp *interp;
43
44 char motdfile[121] = "text/motd"; /* File where the motd is stored */
45 int connect_timeout = 15; /* How long to wait before a telnet
46 connection times out */
47
48 int reserved_port_min = 0;
49 int reserved_port_max = 0;
50
51 void init_dcc_max()
52 {
53 int osock = MAXSOCKS;
54
55 if (max_dcc < 1)
56 max_dcc = 1;
57 if (dcc)
58 dcc = realloc(dcc, sizeof(struct dcc_t) * max_dcc);
59 else
60 dcc = malloc(sizeof(struct dcc_t) * max_dcc);
61
62 MAXSOCKS = max_dcc + 10;
63 if (socklist)
64 socklist = (sock_list *) realloc((void *) socklist,
65 sizeof(sock_list) * MAXSOCKS);
66 else
67 socklist = (sock_list *) malloc(sizeof(sock_list) * MAXSOCKS);
68 for (; osock < MAXSOCKS; osock++)
69 socklist[osock].flags = SOCK_UNUSED;
70 }
71
72
73 /* Replace \n with \r\n */
74 char *add_cr(char *buf)
75 {
76 static char WBUF[1024];
77 char *p, *q;
78
79 for (p = buf, q = WBUF; *p; p++, q++) {
80 if (*p == '\n')
81 *q++ = '\r';
82 *q = *p;
83 }
84 *q = *p;
85 return WBUF;
86 }
87
88 extern void (*qserver) (int, char *, int);
89
90 void dprintf EGG_VARARGS_DEF(int, arg1)
91 {
92 static char buf[1024];
93 char *format;
94 int idx, len;
95 va_list va;
96
97 idx = EGG_VARARGS_START(int, arg1, va);
98 format = va_arg(va, char *);
99 egg_vsnprintf(buf, 1023, format, va);
100 va_end(va);
101 /* We can not use the return value vsnprintf() to determine where
102 * to null terminate. The C99 standard specifies that vsnprintf()
103 * shall return the number of bytes that would be written if the
104 * buffer had been large enough, rather then -1.
105 */
106 /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
107 * sizeof(buf) bytes. But we're not doing that anyway.
108 */
109 buf[sizeof(buf)-1] = 0;
110
111 #if (TCL_MAJOR_VERSION >= 8 && TCL_MINOR_VERSION >= 1) || (TCL_MAJOR_VERSION >= 9)
112 str_nutf8tounicode(buf, sizeof buf);
113 #endif
114 len = strlen(buf);
115
116 if (idx < 0) {
117 tputs(-idx, buf, len);
118 } else if (idx > 0x7FF0) {
119 switch (idx) {
120 case DP_LOG:
121 putlog(LOG_MISC, "*", "%s", buf);
122 break;
123 case DP_STDOUT:
124 tputs(STDOUT, buf, len);
125 break;
126 case DP_STDERR:
127 tputs(STDERR, buf, len);
128 break;
129 case DP_SERVER:
130 case DP_HELP:
131 case DP_MODE:
132 case DP_MODE_NEXT:
133 case DP_SERVER_NEXT:
134 case DP_HELP_NEXT:
135 qserver(idx, buf, len);
136 break;
137 }
138 return;
139 } else {
140 if (len > 500) { /* Truncate to fit */
141 buf[500] = 0;
142 strcat(buf, "\n");
143 len = 501;
144 }
145 if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
146 char *p = add_cr(buf);
147
148 tputs(dcc[idx].sock, p, strlen(p));
149 } else if (dcc[idx].type && dcc[idx].type->output) {
150 dcc[idx].type->output(idx, buf, dcc[idx].u.other);
151 } else
152 tputs(dcc[idx].sock, buf, len);
153 }
154 }
155
156 void chatout EGG_VARARGS_DEF(char *, arg1)
157 {
158 int i, len;
159 char *format;
160 char s[601];
161 va_list va;
162
163 format = EGG_VARARGS_START(char *, arg1, va);
164 egg_vsnprintf(s, 511, format, va);
165 va_end(va);
166 len = strlen(s);
167 if (len > 511)
168 len = 511;
169 s[len + 1] = 0;
170
171 for (i = 0; i < dcc_total; i++)
172 if (dcc[i].type == &DCC_CHAT)
173 if (dcc[i].u.chat->channel >= 0)
174 dprintf(i, "%s", s);
175
176 }
177
178 /* Print to all on this channel but one.
179 */
180 void chanout_but EGG_VARARGS_DEF(int, arg1)
181 {
182 int i, x, chan, len;
183 char *format;
184 char s[601];
185 va_list va;
186
187 x = EGG_VARARGS_START(int, arg1, va);
188 chan = va_arg(va, int);
189 format = va_arg(va, char *);
190 egg_vsnprintf(s, 511, format, va);
191 va_end(va);
192 len = strlen(s);
193 if (len > 511)
194 len = 511;
195 s[len + 1] = 0;
196
197 for (i = 0; i < dcc_total; i++)
198 if ((dcc[i].type == &DCC_CHAT) && (i != x))
199 if (dcc[i].u.chat->channel == chan)
200 dprintf(i, "%s", s);
201
202 }
203
204 void dcc_chatter(int idx)
205 {
206 int i, j;
207 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
208
209 get_user_flagrec(dcc[idx].user, &fr, NULL);
210 show_motd(idx);
211 i = dcc[idx].u.chat->channel;
212 dcc[idx].u.chat->channel = 234567;
213 j = dcc[idx].sock;
214 strcpy(dcc[idx].u.chat->con_chan, "***");
215 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
216 /* Still there? */
217 if ((idx >= dcc_total) || (dcc[idx].sock != j))
218 return; /* Nope */
219 /* Tcl script may have taken control */
220 if (dcc[idx].type == &DCC_CHAT) {
221 if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
222 strcpy(dcc[idx].u.chat->con_chan, "*");
223 if (dcc[idx].u.chat->channel == 234567) {
224 /* If the chat channel has already been altered it's *highly*
225 * probably join/part messages have been broadcast everywhere,
226 * so dont bother sending them
227 */
228 if (i == -2)
229 i = 0;
230 dcc[idx].u.chat->channel = i;
231 if (dcc[idx].u.chat->channel >= 0) {
232 if (dcc[idx].u.chat->channel < 100000) {
233 botnet_send_join_idx(idx, -1);
234 }
235 }
236 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
237 geticon(idx), dcc[idx].sock, dcc[idx].host);
238 }
239 /* But *do* bother with sending it locally */
240 if (!dcc[idx].u.chat->channel) {
241 chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
242 } else if (dcc[idx].u.chat->channel > 0) {
243 chanout_but(-1, dcc[idx].u.chat->channel,
244 "*** %s joined the channel.\n", dcc[idx].nick);
245 }
246 }
247 }
248
249 /* Mark an entry as lost and deconstruct it's contents. It will be securely
250 * removed from the dcc list in the main loop.
251 */
252 void lostdcc(int n)
253 {
254 if (dcc[n].type && dcc[n].type->kill)
255 dcc[n].type->kill(n, dcc[n].u.other);
256 else if (dcc[n].u.other)
257 free(dcc[n].u.other);
258 memset(&dcc[n], 0, sizeof(struct dcc_t));
259
260 dcc[n].sock = (-1);
261 dcc[n].type = &DCC_LOST;
262 }
263
264 /* Remove entry from dcc list. Think twice before using this function,
265 * because it invalidates any variables that point to a specific dcc
266 * entry!
267 *
268 * Note: The entry will be deconstructed if it was not deconstructed
269 * already. This case should normally not occur.
270 */
271 void removedcc(int n)
272 {
273 if (dcc[n].type && dcc[n].type->kill)
274 dcc[n].type->kill(n, dcc[n].u.other);
275 else if (dcc[n].u.other)
276 free(dcc[n].u.other);
277 dcc_total--;
278 if (n < dcc_total)
279 memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
280 else
281 memset(&dcc[n], 0, sizeof(struct dcc_t)); /* drummer */
282 }
283
284 /* Clean up sockets that were just left for dead.
285 */
286 void dcc_remove_lost(void)
287 {
288 int i;
289
290 for (i = 0; i < dcc_total; i++) {
291 if (dcc[i].type == &DCC_LOST) {
292 dcc[i].type = NULL;
293 dcc[i].sock = (-1);
294 removedcc(i);
295 i--;
296 }
297 }
298 }
299
300 /* Show list of current dcc's to a dcc-chatter
301 * positive value: idx given -- negative value: sock given
302 */
303 void tell_dcc(int zidx)
304 {
305 int i, j, k;
306 char other[160];
307
308 spaces[HANDLEN - 9] = 0;
309 dprintf(zidx, "SOCK PORT NICK %s HOST TYPE\n"
310 ,spaces);
311 dprintf(zidx, "---- ----- ---------%s -------------------------- ----\n"
312 ,spaces);
313 spaces[HANDLEN - 9] = ' ';
314 /* Show server */
315 for (i = 0; i < dcc_total; i++) {
316 j = strlen(dcc[i].host);
317 if (j > 26)
318 j -= 26;
319 else
320 j = 0;
321 if (dcc[i].type && dcc[i].type->display)
322 dcc[i].type->display(i, other);
323 else {
324 sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
325 break;
326 }
327 k = HANDLEN - strlen(dcc[i].nick);
328 spaces[k] = 0;
329 dprintf(zidx, "%-4d %5d %s%s %-26s %s\n", dcc[i].sock,
330 dcc[i].port, dcc[i].nick, spaces, dcc[i].host + j, other);
331 spaces[k] = ' ';
332 }
333 }
334
335 /* Mark someone on dcc chat as no longer away
336 */
337 void not_away(int idx)
338 {
339 if (dcc[idx].u.chat->away == NULL) {
340 dprintf(idx, "You weren't away!\n");
341 return;
342 }
343 if (dcc[idx].u.chat->channel >= 0) {
344 chanout_but(-1, dcc[idx].u.chat->channel,
345 "*** %s is no longer away.\n", dcc[idx].nick);
346 if (dcc[idx].u.chat->channel < 100000) {
347 botnet_send_away(-1, botnetnick, dcc[idx].sock, NULL, idx);
348 }
349 }
350 dprintf(idx, "You're not away any more.\n");
351 free_null(dcc[idx].u.chat->away);
352 check_tcl_away(botnetnick, dcc[idx].sock, NULL);
353 }
354
355 void set_away(int idx, char *s)
356 {
357 if (s == NULL) {
358 not_away(idx);
359 return;
360 }
361 if (!s[0]) {
362 not_away(idx);
363 return;
364 }
365 if (dcc[idx].u.chat->away != NULL)
366 free(dcc[idx].u.chat->away);
367 malloc_strcpy(dcc[idx].u.chat->away, s);
368 if (dcc[idx].u.chat->channel >= 0) {
369 chanout_but(-1, dcc[idx].u.chat->channel,
370 "*** %s is now away: %s\n", dcc[idx].nick, s);
371 if (dcc[idx].u.chat->channel < 100000) {
372 botnet_send_away(-1, botnetnick, dcc[idx].sock, s, idx);
373 }
374 }
375 dprintf(idx, "You are now away.\n");
376 check_tcl_away(botnetnick, dcc[idx].sock, s);
377 }
378
379 /* Make a password, 10-15 random letters and digits
380 */
381 void makepass(char *s)
382 {
383 int i;
384
385 i = 10 + (random() % 6);
386 make_rand_str(s, i);
387 }
388
389 void flush_lines(int idx, struct chat_info *ci)
390 {
391 int c = ci->line_count;
392 struct msgq *p = ci->buffer, *o;
393
394 while (p && c < (ci->max_line)) {
395 ci->current_lines--;
396 tputs(dcc[idx].sock, p->msg, p->len);
397 free(p->msg);
398 o = p->next;
399 free(p);
400 p = o;
401 c++;
402 }
403 if (p != NULL) {
404 if (dcc[idx].status & STAT_TELNET)
405 tputs(dcc[idx].sock, "[More]: ", 8);
406 else
407 tputs(dcc[idx].sock, "[More]\n", 7);
408 }
409 ci->buffer = p;
410 ci->line_count = 0;
411 }
412
413 int new_dcc(struct dcc_table *type, int xtra_size)
414 {
415 int i = dcc_total;
416
417 if (dcc_total == max_dcc)
418 return -1;
419 dcc_total++;
420 memset((char *) &dcc[i], 0, sizeof(struct dcc_t));
421
422 dcc[i].type = type;
423 if (xtra_size)
424 dcc[i].u.other = calloc(1, xtra_size);
425 return i;
426 }
427
428 /* Changes the given dcc entry to another type.
429 */
430 void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
431 {
432 /* Free old structure. */
433 if (dcc[i].type && dcc[i].type->kill)
434 dcc[i].type->kill(i, dcc[i].u.other);
435 else if (dcc[i].u.other)
436 free_null(dcc[i].u.other);
437
438 dcc[i].type = type;
439 if (xtra_size)
440 dcc[i].u.other = calloc(1, xtra_size);
441 }
442
443 int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
444 {
445 time_t t;
446
447 if (!dcc_flood_thr)
448 return 0;
449 t = now;
450 if (*timer != t) {
451 *timer = t;
452 chat->msgs_per_sec = 0;
453 } else {
454 chat->msgs_per_sec++;
455 if (chat->msgs_per_sec > dcc_flood_thr) {
456 /* FLOOD */
457 dprintf(idx, "*** FLOOD: %s.\n", _("Goodbye"));
458 /* Evil assumption here that flags&DCT_CHAT implies chat type */
459 if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
460 (chat->channel >= 0)) {
461 char x[1024];
462
463 egg_snprintf(x, sizeof x, _("%s has been forcibly removed for flooding.\n"), dcc[idx].nick);
464 chanout_but(idx, chat->channel, "*** %s", x);
465 if (chat->channel < 100000)
466 botnet_send_part_idx(idx, x);
467 }
468 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
469 if ((dcc[idx].sock != STDOUT) || backgrd) {
470 killsock(dcc[idx].sock);
471 lostdcc(idx);
472 } else {
473 dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
474 dcc_chatter(idx);
475 }
476 return 1; /* <- flood */
477 }
478 }
479 return 0;
480 }
481
482 /* Handle someone being booted from dcc chat.
483 */
484 void do_boot(int idx, char *by, char *reason)
485 {
486 int files = (dcc[idx].type != &DCC_CHAT);
487
488 dprintf(idx, _("-=- poof -=-\n"));
489 dprintf(idx, _("Youve been booted from the %s by %s%s%s\n"), files ? "file section" : "bot",
490 by, reason[0] ? ": " : ".", reason);
491 /* If it's a partyliner (chatterer :) */
492 /* Horrible assumption that DCT_CHAT using structure uses same format
493 * as DCC_CHAT */
494 if ((dcc[idx].type->flags & DCT_CHAT) &&
495 (dcc[idx].u.chat->channel >= 0)) {
496 char x[1024];
497
498 egg_snprintf(x, sizeof x, _("%s booted %s from the party line%s%s\n"), by, dcc[idx].nick,
499 reason[0] ? ": " : "", reason);
500 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
501 if (dcc[idx].u.chat->channel < 100000)
502 botnet_send_part_idx(idx, x);
503 }
504 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
505 if ((dcc[idx].sock != STDOUT) || backgrd) {
506 killsock(dcc[idx].sock);
507 lostdcc(idx);
508 /* Entry must remain in the table so it can be logged by the caller */
509 } else {
510 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
511 dcc_chatter(idx);
512 }
513 return;
514 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23