/[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.41 - (show annotations) (download) (as text)
Mon Jan 14 02:23:27 2002 UTC (17 years, 9 months ago) by ite
Branch: MAIN
Changes since 1.40: +1 -2 lines
File MIME type: text/x-chdr
* Merged bits of tothwolf cleaunps from his alpha patch.
* Removed some unneeded externs in some files.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23