/[cvs]/eggdrop1.9/src/chanprog.c
ViewVC logotype

Contents of /eggdrop1.9/src/chanprog.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.44 - (show annotations) (download) (as text)
Wed Feb 13 16:44:58 2002 UTC (17 years, 6 months ago) by ite
Branch: MAIN
Changes since 1.43: +11 -12 lines
File MIME type: text/x-chdr
gettext plurals are now supported with the P_() macro

1 /*
2 * chanprog.c -- handles:
3 * telling the current programmed settings
4 * initializing a lot of stuff and loading the tcl scripts
5 *
6 * $Id: chanprog.c,v 1.43 2002/02/07 22:19:04 wcc Exp $
7 */
8 /*
9 * Copyright (C) 1997 Robey Pointer
10 * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #include "main.h"
28 #if HAVE_GETRUSAGE
29 #include <sys/resource.h>
30 #if HAVE_SYS_RUSAGE_H
31 #include <sys/rusage.h>
32 #endif
33 #endif
34 #ifdef HAVE_UNAME
35 #include <sys/utsname.h>
36 #endif
37 #include "modules.h"
38 #include "logfile.h"
39 #include "misc.h"
40
41 extern struct userrec *userlist;
42 extern Tcl_Interp *interp;
43 extern char ver[], botnetnick[], firewall[],
44 motdfile[], userfile[], helpdir[], tempdir[],
45 moddir[], notify_new[], owner[], configfile[];
46 extern time_t now, online_since;
47 extern int backgrd, term_z, con_chan, cache_hit, cache_miss,
48 firewallport, default_flags, conmask,
49 protect_readonly, make_userfile, noshare,
50 ignore_time;
51
52 struct chanset_t *chanset = NULL; /* Channel list */
53 char admin[121] = ""; /* Admin info */
54 char origbotname[NICKLEN + 1];
55 char botname[NICKLEN + 1]; /* Primary botname */
56
57
58 /* Returns memberfields if the nick is in the member list.
59 */
60 memberlist *ismember(struct chanset_t *chan, char *nick)
61 {
62 register memberlist *x;
63
64 for (x = chan->channel.member; x && x->nick[0]; x = x->next)
65 if (!irccmp(x->nick, nick))
66 return x;
67 return NULL;
68 }
69
70 /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
71 */
72 struct chanset_t *findchan(const char *name)
73 {
74 register struct chanset_t *chan;
75
76 for (chan = chanset; chan; chan = chan->next)
77 if (!irccmp(chan->name, name))
78 return chan;
79 return NULL;
80 }
81
82 /* Find a chanset by display name (ie !channel)
83 */
84 struct chanset_t *findchan_by_dname(const char *name)
85 {
86 register struct chanset_t *chan;
87
88 for (chan = chanset; chan; chan = chan->next)
89 if (!irccmp(chan->dname, name))
90 return chan;
91 return NULL;
92 }
93
94 /*
95 * "caching" functions
96 */
97
98 /* Shortcut for get_user_by_host -- might have user record in one
99 * of the channel caches.
100 */
101 struct userrec *check_chanlist(const char *host)
102 {
103 char *nick, *uhost, buf[UHOSTLEN];
104 register memberlist *m;
105 register struct chanset_t *chan;
106
107 strncpyz(buf, host, sizeof buf);
108 nick = strtok(buf, "!");
109 uhost = strtok(NULL, "!");
110 for (chan = chanset; chan; chan = chan->next)
111 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
112 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
113 return m->user;
114 return NULL;
115 }
116
117 /* Shortcut for get_user_by_handle -- might have user record in channels
118 */
119 struct userrec *check_chanlist_hand(const char *hand)
120 {
121 register struct chanset_t *chan;
122 register memberlist *m;
123
124 for (chan = chanset; chan; chan = chan->next)
125 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
126 if (m->user && !strcasecmp(m->user->handle, hand))
127 return m->user;
128 return NULL;
129 }
130
131 /* Clear the user pointers in the chanlists.
132 *
133 * Necessary when a hostmask is added/removed, a user is added or a new
134 * userfile is loaded.
135 */
136 void clear_chanlist(void)
137 {
138 register memberlist *m;
139 register struct chanset_t *chan;
140
141 for (chan = chanset; chan; chan = chan->next)
142 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
143 m->user = NULL;
144 }
145
146 /* Clear the user pointer of a specific nick in the chanlists.
147 *
148 * Necessary when a hostmask is added/removed, a nick changes, etc.
149 * Does not completely invalidate the channel cache like clear_chanlist().
150 */
151 void clear_chanlist_member(const char *nick)
152 {
153 register memberlist *m;
154 register struct chanset_t *chan;
155
156 for (chan = chanset; chan; chan = chan->next)
157 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
158 if (!irccmp(m->nick, nick)) {
159 m->user = NULL;
160 break;
161 }
162 }
163
164 /* If this user@host is in a channel, set it (it was null)
165 */
166 void set_chanlist(const char *host, struct userrec *rec)
167 {
168 char *nick, *uhost, buf[UHOSTLEN];
169 register memberlist *m;
170 register struct chanset_t *chan;
171
172 strncpyz(buf, host, sizeof buf);
173 nick = strtok(buf, "!");
174 uhost = strtok(NULL, "!");
175 for (chan = chanset; chan; chan = chan->next)
176 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
177 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
178 m->user = rec;
179 }
180
181 /* Dump status info out to dcc
182 */
183 void tell_verbose_status(int idx)
184 {
185 char s[256], s1[121], s2[81];
186 char *vers_t, *uni_t;
187 int i;
188 time_t now2, hr, min;
189 #if HAVE_GETRUSAGE
190 struct rusage ru;
191 #else
192 # if HAVE_CLOCK
193 clock_t cl;
194 # endif
195 #endif
196 #ifdef HAVE_UNAME
197 struct utsname un;
198
199 if (!uname(&un) < 0) {
200 #endif
201 vers_t = " ";
202 uni_t = _("*unknown*");
203 #ifdef HAVE_UNAME
204 } else {
205 vers_t = un.release;
206 uni_t = un.sysname;
207 }
208 #endif
209
210 i = count_users(userlist);
211 dprintf(idx, _("I am %1$s, running %2$s: "), botnetnick, ver);
212 dprintf(idx, P_("%d user\n", "%d users\n", i), i);
213 dprintf(idx, _("Running on %1$s %2$s\n"), uni_t, vers_t);
214 if (admin[0])
215 dprintf(idx, _("Admin: %s\n"), admin);
216 now2 = now - online_since;
217 s[0] = 0;
218
219 if (now2 > 86400) {
220 int days = (int) (now2 / 86400);
221
222 snprintf(s, sizeof(s), P_("%d day", "%d days", days), days);
223 strcat(s, ", ");
224 now2 -= days * 86400;
225 }
226 hr = (time_t) ((int) now2 / 3600);
227 now2 -= (hr * 3600);
228 min = (time_t) ((int) now2 / 60);
229 snprintf(&s[strlen(s)], sizeof(s) - strlen(s), "%02d:%02d", (int) hr,
230 (int) min);
231 s1[0] = 0;
232 if (backgrd)
233 strcpy(s1, _("background"));
234 else {
235 if (term_z)
236 strcpy(s1, _("terminal mode"));
237 else if (con_chan)
238 strcpy(s1, _("status mode"));
239 else
240 strcpy(s1, _("log dump mode"));
241 }
242 #if HAVE_GETRUSAGE
243 getrusage(RUSAGE_SELF, &ru);
244 hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
245 min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
246 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
247 #else
248 # if HAVE_CLOCK
249 cl = (clock() / CLOCKS_PER_SEC);
250 hr = (int) (cl / 60);
251 min = (int) (cl - (hr * 60));
252 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
253 # else
254 sprintf(s2, "CPU ???");
255 # endif
256 #endif
257 dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", _("Online for"),
258 s, s1, s2, _("cache hit"),
259 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
260
261 /* info library */
262 dprintf(idx, "%s %s\n", _("Tcl library:"),
263 ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
264 interp->result : _("*unknown*"));
265
266 /* info tclversion/patchlevel */
267 dprintf(idx, "%s %s (%s %s)\n", _("Tcl version:"),
268 ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
269 interp->result : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
270 interp->result : _("*unknown*"), _("header version"),
271 TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL : _("*unknown*"));
272
273 #if HAVE_TCL_THREADS
274 dprintf(idx, _("Tcl is threaded\n"));
275 #endif
276
277 }
278
279 /* Show all internal state variables
280 */
281 void tell_settings(int idx)
282 {
283 char s[1024];
284 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
285
286 dprintf(idx, _("Botnet Nickname: %s\n"), botnetnick);
287 if (firewall[0])
288 dprintf(idx, _("Firewall: %1$s, port %2$d\n"), firewall, firewallport);
289 dprintf(idx, _("Userfile: %1$s Motd: %2$s\n"), userfile, motdfile);
290 dprintf(idx, _("Directories:\n"));
291 dprintf(idx, _(" Help : %s\n"), helpdir);
292 dprintf(idx, _(" Temp : %s\n"), tempdir);
293 #ifndef STATIC
294 dprintf(idx, _(" Modules : %s\n"), moddir);
295 #endif
296 fr.global = default_flags;
297
298 build_flags(s, &fr, NULL);
299 dprintf(idx, "%s [%s], %s: %s\n", _("New users get flags"), s,
300 _("notify"), notify_new);
301 /* FIXME PLURAL: handle it properly */
302 if (owner[0])
303 dprintf(idx, _("Permanent owner(s): %s\n"), owner);
304 dprintf(idx, _("Ignores last %d mins\n"), ignore_time);
305 }
306
307 void reaffirm_owners()
308 {
309 char *p, *q, s[121];
310 struct userrec *u;
311
312 /* Make sure default owners are +n */
313 if (owner[0]) {
314 q = owner;
315 p = strchr(q, ',');
316 while (p) {
317 strncpyz(s, q, p - q);
318 rmspace(s);
319 u = get_user_by_handle(userlist, s);
320 if (u)
321 u->flags = sanity_check(u->flags | USER_OWNER);
322 q = p + 1;
323 p = strchr(q, ',');
324 }
325 strcpy(s, q);
326 rmspace(s);
327 u = get_user_by_handle(userlist, s);
328 if (u)
329 u->flags = sanity_check(u->flags | USER_OWNER);
330 }
331 }
332
333 void chanprog()
334 {
335 admin[0] = 0;
336 helpdir[0] = 0;
337 tempdir[0] = 0;
338 conmask = 0;
339 /* Turn off read-only variables (make them write-able) for rehash */
340 protect_readonly = 0;
341 /* Now read it */
342 if (!readtclprog(configfile))
343 fatal(_("CONFIG FILE NOT LOADED (NOT FOUND, OR ERROR)"), 0);
344 /* We should be safe now */
345 call_hook(HOOK_REHASH);
346 protect_readonly = 1;
347 if (!botnetnick[0]) {
348 strncpyz(botnetnick, origbotname, HANDLEN + 1);
349 }
350 if (!botnetnick[0])
351 fatal("I don't have a botnet nick!!\n", 0);
352 if (!userfile[0])
353 fatal(_("STARTING BOT IN USERFILE CREATION MODE.\n\
354 Telnet to the bot and enter 'NEW' as your nickname."), 0);
355 if (!readuserfile(userfile, &userlist)) {
356 if (!make_userfile) {
357 char tmp[178];
358
359 snprintf(tmp, sizeof tmp, _("USER FILE NOT FOUND! (try ./eggdrop -m %s to make one)\n"), configfile);
360 fatal(tmp, 0);
361 }
362 printf(_("\n\nSTARTING BOT IN USERFILE CREATION MODE.\n\
363 Telnet to the bot and enter 'NEW' as your nickname.\n"));
364 if (module_find("server", 0, 0))
365 printf(_("OR go to IRC and type: /msg %s hello\n"), origbotname);
366 printf(_("This will make the bot recognize you as the master.\n\n"));
367 } else if (make_userfile) {
368 make_userfile = 0;
369 printf(_("USERFILE ALREADY EXISTS (drop the -m)\n"));
370 }
371 if (helpdir[0])
372 if (helpdir[strlen(helpdir) - 1] != '/')
373 strcat(helpdir, "/");
374 if (tempdir[0])
375 if (tempdir[strlen(tempdir) - 1] != '/')
376 strcat(tempdir, "/");
377 /* Test tempdir: it's vital */
378 {
379 FILE *f;
380 char s[161], rands[8];
381
382 /* Possible file race condition solved by using a random string
383 * and the process id in the filename.
384 * FIXME: This race is only partitially fixed. We could still be
385 * overwriting an existing file / following a malicious
386 * link.
387 */
388 make_rand_str(rands, 7); /* create random string */
389 sprintf(s, "%s.test-%u-%s", tempdir, getpid(), rands);
390 f = fopen(s, "w");
391 if (f == NULL)
392 fatal(_("CANT WRITE TO TEMP DIR"), 0);
393 fclose(f);
394 unlink(s);
395 }
396 reaffirm_owners();
397 }
398
399 /* Reload the user file from disk
400 */
401 void reload()
402 {
403 FILE *f;
404
405 f = fopen(userfile, "r");
406 if (f == NULL) {
407 putlog(LOG_MISC, "*", _("Cant reload user file!"));
408 return;
409 }
410 fclose(f);
411 noshare = 1;
412 clear_userlist(userlist);
413 noshare = 0;
414 userlist = NULL;
415 if (!readuserfile(userfile, &userlist))
416 fatal(_("User file is missing!"), 0);
417 reaffirm_owners();
418 call_hook(HOOK_READ_USERFILE);
419 }
420
421 void rehash()
422 {
423 call_hook(HOOK_PRE_REHASH);
424 noshare = 1;
425 clear_userlist(userlist);
426 noshare = 0;
427 userlist = NULL;
428 chanprog();
429 }
430
431 /* Oddly enough, written by proton (Emech's coder)
432 */
433 int isowner(char *name)
434 {
435 char *pa, *pb;
436 char nl, pl;
437
438 if (!owner || !*owner)
439 return (0);
440 if (!name || !*name)
441 return (0);
442 nl = strlen(name);
443 pa = owner;
444 pb = owner;
445 while (1) {
446 while (1) {
447 if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
448 break;
449 pb++;
450 }
451 pl = (unsigned int) pb - (unsigned int) pa;
452 if (pl == nl && !strncasecmp(pa, name, nl))
453 return (1);
454 while (1) {
455 if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
456 break;
457 pb++;
458 }
459 if (*pb == 0)
460 return (0);
461 pa = pb;
462 }
463 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23