/[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.28 - (show annotations) (download) (as text)
Sat Mar 10 06:36:20 2001 UTC (18 years, 7 months ago) by guppy
Branch: MAIN
Changes since 1.27: +56 -24 lines
File MIME type: text/x-chdr
Wingman's utf8 fix ... please test this alot people :P~

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23