/[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.48 - (show annotations) (download) (as text)
Sat Apr 27 18:34:09 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.47: +12 -15 lines
File MIME type: text/x-chdr
* Use init_dcc_max instead of resizing the dcc array ourselves

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23