/[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.51 - (show annotations) (download) (as text)
Sun May 5 16:40:38 2002 UTC (17 years, 5 months ago) by tothwolf
Branch: MAIN
Changes since 1.50: +10 -7 lines
File MIME type: text/x-chdr
Grafted GPL header onto newer files.
Storing rcsid in binary files now.

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