/[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.45 - (show annotations) (download) (as text)
Thu Feb 14 00:55:11 2002 UTC (17 years, 6 months ago) by ite
Branch: MAIN
Changes since 1.44: +1 -3 lines
File MIME type: text/x-chdr
* Got rid of STATIC and the related code. We don't need to care if we have a static build these days.
* Let's tell libtdl about the module's search path with lt_dladdsearchdir()

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.44 2002/02/13 16:44:58 ite 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 dprintf(idx, _(" Modules : %s\n"), moddir);
294 fr.global = default_flags;
295
296 build_flags(s, &fr, NULL);
297 dprintf(idx, "%s [%s], %s: %s\n", _("New users get flags"), s,
298 _("notify"), notify_new);
299 /* FIXME PLURAL: handle it properly */
300 if (owner[0])
301 dprintf(idx, _("Permanent owner(s): %s\n"), owner);
302 dprintf(idx, _("Ignores last %d mins\n"), ignore_time);
303 }
304
305 void reaffirm_owners()
306 {
307 char *p, *q, s[121];
308 struct userrec *u;
309
310 /* Make sure default owners are +n */
311 if (owner[0]) {
312 q = owner;
313 p = strchr(q, ',');
314 while (p) {
315 strncpyz(s, q, p - q);
316 rmspace(s);
317 u = get_user_by_handle(userlist, s);
318 if (u)
319 u->flags = sanity_check(u->flags | USER_OWNER);
320 q = p + 1;
321 p = strchr(q, ',');
322 }
323 strcpy(s, q);
324 rmspace(s);
325 u = get_user_by_handle(userlist, s);
326 if (u)
327 u->flags = sanity_check(u->flags | USER_OWNER);
328 }
329 }
330
331 void chanprog()
332 {
333 admin[0] = 0;
334 helpdir[0] = 0;
335 tempdir[0] = 0;
336 conmask = 0;
337 /* Turn off read-only variables (make them write-able) for rehash */
338 protect_readonly = 0;
339 /* Now read it */
340 if (!readtclprog(configfile))
341 fatal(_("CONFIG FILE NOT LOADED (NOT FOUND, OR ERROR)"), 0);
342 /* We should be safe now */
343 call_hook(HOOK_REHASH);
344 protect_readonly = 1;
345 if (!botnetnick[0]) {
346 strncpyz(botnetnick, origbotname, HANDLEN + 1);
347 }
348 if (!botnetnick[0])
349 fatal("I don't have a botnet nick!!\n", 0);
350 if (!userfile[0])
351 fatal(_("STARTING BOT IN USERFILE CREATION MODE.\n\
352 Telnet to the bot and enter 'NEW' as your nickname."), 0);
353 if (!readuserfile(userfile, &userlist)) {
354 if (!make_userfile) {
355 char tmp[178];
356
357 snprintf(tmp, sizeof tmp, _("USER FILE NOT FOUND! (try ./eggdrop -m %s to make one)\n"), configfile);
358 fatal(tmp, 0);
359 }
360 printf(_("\n\nSTARTING BOT IN USERFILE CREATION MODE.\n\
361 Telnet to the bot and enter 'NEW' as your nickname.\n"));
362 if (module_find("server", 0, 0))
363 printf(_("OR go to IRC and type: /msg %s hello\n"), origbotname);
364 printf(_("This will make the bot recognize you as the master.\n\n"));
365 } else if (make_userfile) {
366 make_userfile = 0;
367 printf(_("USERFILE ALREADY EXISTS (drop the -m)\n"));
368 }
369 if (helpdir[0])
370 if (helpdir[strlen(helpdir) - 1] != '/')
371 strcat(helpdir, "/");
372 if (tempdir[0])
373 if (tempdir[strlen(tempdir) - 1] != '/')
374 strcat(tempdir, "/");
375 /* Test tempdir: it's vital */
376 {
377 FILE *f;
378 char s[161], rands[8];
379
380 /* Possible file race condition solved by using a random string
381 * and the process id in the filename.
382 * FIXME: This race is only partitially fixed. We could still be
383 * overwriting an existing file / following a malicious
384 * link.
385 */
386 make_rand_str(rands, 7); /* create random string */
387 sprintf(s, "%s.test-%u-%s", tempdir, getpid(), rands);
388 f = fopen(s, "w");
389 if (f == NULL)
390 fatal(_("CANT WRITE TO TEMP DIR"), 0);
391 fclose(f);
392 unlink(s);
393 }
394 reaffirm_owners();
395 }
396
397 /* Reload the user file from disk
398 */
399 void reload()
400 {
401 FILE *f;
402
403 f = fopen(userfile, "r");
404 if (f == NULL) {
405 putlog(LOG_MISC, "*", _("Cant reload user file!"));
406 return;
407 }
408 fclose(f);
409 noshare = 1;
410 clear_userlist(userlist);
411 noshare = 0;
412 userlist = NULL;
413 if (!readuserfile(userfile, &userlist))
414 fatal(_("User file is missing!"), 0);
415 reaffirm_owners();
416 call_hook(HOOK_READ_USERFILE);
417 }
418
419 void rehash()
420 {
421 call_hook(HOOK_PRE_REHASH);
422 noshare = 1;
423 clear_userlist(userlist);
424 noshare = 0;
425 userlist = NULL;
426 chanprog();
427 }
428
429 /* Oddly enough, written by proton (Emech's coder)
430 */
431 int isowner(char *name)
432 {
433 char *pa, *pb;
434 char nl, pl;
435
436 if (!owner || !*owner)
437 return (0);
438 if (!name || !*name)
439 return (0);
440 nl = strlen(name);
441 pa = owner;
442 pb = owner;
443 while (1) {
444 while (1) {
445 if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
446 break;
447 pb++;
448 }
449 pl = (unsigned int) pb - (unsigned int) pa;
450 if (pl == nl && !strncasecmp(pa, name, nl))
451 return (1);
452 while (1) {
453 if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
454 break;
455 pb++;
456 }
457 if (*pb == 0)
458 return (0);
459 pa = pb;
460 }
461 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23