/[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.63 - (show annotations) (download) (as text)
Sun May 11 02:30:54 2003 UTC (16 years, 5 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.62: +1 -1 lines
File MIME type: text/x-chdr
FILE REMOVED
* Trying out the autoconf way of detecting tcl (test)

1 /* * chanprog.c --
2 *
3 * telling the current programmed settings
4 * initializing a lot of stuff and loading the tcl scripts
5 */
6 /*
7 * Copyright (C) 1997 Robey Pointer
8 * Copyright (C) 1999, 2000, 2001, 2002, 2003 Eggheads Development Team
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #ifndef lint
26 static const char rcsid[] = "$Id: chanprog.c,v 1.62 2003/02/26 01:51:14 wcc Exp $";
27 #endif
28
29 #include "main.h"
30 #include "core_config.h"
31 #if HAVE_GETRUSAGE
32 #include <sys/resource.h>
33 #if HAVE_SYS_RUSAGE_H
34 #include <sys/rusage.h>
35 #endif
36 #endif
37 #ifdef HAVE_UNAME
38 #include <sys/utsname.h>
39 #endif
40 #include "modules.h"
41 #include "logfile.h"
42 #include "misc.h"
43 #include "users.h" /* get_user_by_handle, readuserfile */
44 #include "dccutil.h" /* dprintf_eggdrop */
45 #include "userrec.h" /* count_users, clear_userlist */
46
47 extern struct userrec *userlist;
48 extern char ver[], firewall[], motdfile[], userfile[], helpdir[],
49 moddir[], notify_new[], owner[], configfile[];
50 extern time_t now, online_since;
51 extern int backgrd, term_z, con_chan, cache_hit, cache_miss, firewallport,
52 default_flags, conmask, protect_readonly, make_userfile,
53 ignore_time;
54
55 struct chanset_t *chanset = NULL; /* Channel list */
56 char admin[121] = ""; /* Admin info */
57 char origbotname[NICKLEN + 1];
58 char *botname; /* Primary botname */
59
60
61 /* Returns memberfields if the nick is in the member list.
62 */
63 memberlist *ismember(struct chanset_t *chan, char *nick)
64 {
65 register memberlist *x;
66
67 for (x = chan->channel.member; x && x->nick[0]; x = x->next)
68 if (!irccmp(x->nick, nick))
69 return x;
70 return NULL;
71 }
72
73 /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
74 */
75 struct chanset_t *findchan(const char *name)
76 {
77 register struct chanset_t *chan;
78
79 for (chan = chanset; chan; chan = chan->next)
80 if (!irccmp(chan->name, name))
81 return chan;
82 return NULL;
83 }
84
85 /* Find a chanset by display name (ie !channel)
86 */
87 struct chanset_t *findchan_by_dname(const char *name)
88 {
89 register struct chanset_t *chan;
90
91 for (chan = chanset; chan; chan = chan->next)
92 if (!irccmp(chan->dname, name))
93 return chan;
94 return NULL;
95 }
96
97 /*
98 * "caching" functions
99 */
100
101 /* Shortcut for get_user_by_host -- might have user record in one
102 * of the channel caches.
103 */
104 struct userrec *check_chanlist(const char *host)
105 {
106 char *nick, *uhost, buf[UHOSTLEN];
107 register memberlist *m;
108 register struct chanset_t *chan;
109
110 strlcpy(buf, host, sizeof buf);
111 nick = strtok(buf, "!");
112 uhost = strtok(NULL, "!");
113 for (chan = chanset; chan; chan = chan->next)
114 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
115 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
116 return m->user;
117 return NULL;
118 }
119
120 /* Shortcut for get_user_by_handle -- might have user record in channels
121 */
122 struct userrec *check_chanlist_hand(const char *hand)
123 {
124 register struct chanset_t *chan;
125 register memberlist *m;
126
127 for (chan = chanset; chan; chan = chan->next)
128 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
129 if (m->user && !strcasecmp(m->user->handle, hand))
130 return m->user;
131 return NULL;
132 }
133
134 /* Clear the user pointers in the chanlists.
135 *
136 * Necessary when a hostmask is added/removed, a user is added or a new
137 * userfile is loaded.
138 */
139 void clear_chanlist(void)
140 {
141 register memberlist *m;
142 register struct chanset_t *chan;
143
144 for (chan = chanset; chan; chan = chan->next)
145 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
146 m->user = NULL;
147 }
148
149 /* Clear the user pointer of a specific nick in the chanlists.
150 *
151 * Necessary when a hostmask is added/removed, a nick changes, etc.
152 * Does not completely invalidate the channel cache like clear_chanlist().
153 */
154 void clear_chanlist_member(const char *nick)
155 {
156 register memberlist *m;
157 register struct chanset_t *chan;
158
159 for (chan = chanset; chan; chan = chan->next)
160 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
161 if (!irccmp(m->nick, nick)) {
162 m->user = NULL;
163 break;
164 }
165 }
166
167 /* If this user@host is in a channel, set it (it was null)
168 */
169 void set_chanlist(const char *host, struct userrec *rec)
170 {
171 char *nick, *uhost, buf[UHOSTLEN];
172 register memberlist *m;
173 register struct chanset_t *chan;
174
175 strlcpy(buf, host, sizeof buf);
176 nick = strtok(buf, "!");
177 uhost = strtok(NULL, "!");
178 for (chan = chanset; chan; chan = chan->next)
179 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
180 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
181 m->user = rec;
182 }
183
184 /* Dump status info out to dcc
185 */
186 void tell_verbose_status(int idx)
187 {
188 char s[256], s1[121], s2[81];
189 char *vers_t, *uni_t;
190 int i;
191 time_t now2, hr, min;
192 #if HAVE_GETRUSAGE
193 struct rusage ru;
194 #else
195 # if HAVE_CLOCK
196 clock_t cl;
197 # endif
198 #endif
199 #ifdef HAVE_UNAME
200 struct utsname un;
201
202 if (!uname(&un) < 0) {
203 #endif
204 vers_t = " ";
205 uni_t = _("*unknown*");
206 #ifdef HAVE_UNAME
207 } else {
208 vers_t = un.release;
209 uni_t = un.sysname;
210 }
211 #endif
212
213 i = count_users(userlist);
214 dprintf(idx, _("I am %1$s, running %2$s: "), core_config.botname, ver);
215 dprintf(idx, P_("%d user\n", "%d users\n", i), i);
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 if (admin[0])
261 dprintf(idx, _("Admin: %s\n"), admin);
262 dprintf(idx, _("Config file: %s\n"), configfile);
263 dprintf(idx, _("OS: %1$s %2$s\n"), uni_t, vers_t);
264 }
265
266 /* Show all internal state variables
267 */
268 void tell_settings(int idx)
269 {
270 char s[1024];
271 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
272
273 dprintf(idx, _("My name: %s\n"), core_config.botname);
274 if (firewall[0])
275 dprintf(idx, _("Firewall: %1$s, port %2$d\n"), firewall, firewallport);
276 dprintf(idx, _("Userfile: %1$s Motd: %2$s\n"), userfile, motdfile);
277 dprintf(idx, _("Directories:\n"));
278 dprintf(idx, _(" Help : %s\n"), helpdir);
279 dprintf(idx, _(" Modules : %s\n"), moddir);
280 fr.global = default_flags;
281
282 build_flags(s, &fr, NULL);
283 dprintf(idx, "%s [%s], %s: %s\n", _("New users get flags"), s,
284 _("notify"), notify_new);
285 /* FIXME PLURAL: handle it properly */
286 if (owner[0])
287 dprintf(idx, _("Permanent owner(s): %s\n"), owner);
288 dprintf(idx, _("Ignores last %d mins\n"), ignore_time);
289 }
290
291 void reaffirm_owners()
292 {
293 char *p, *q, s[121];
294 struct userrec *u;
295
296 /* Make sure default owners are +n */
297 if (owner[0]) {
298 q = owner;
299 p = strchr(q, ',');
300 while (p) {
301 strlcpy(s, q, (p - q) + 1);
302 rmspace(s);
303 u = get_user_by_handle(userlist, s);
304 if (u)
305 u->flags = sanity_check(u->flags | USER_OWNER);
306 q = p + 1;
307 p = strchr(q, ',');
308 }
309 strcpy(s, q);
310 rmspace(s);
311 u = get_user_by_handle(userlist, s);
312 if (u)
313 u->flags = sanity_check(u->flags | USER_OWNER);
314 }
315 }
316
317 void chanprog()
318 {
319 admin[0] = 0;
320 helpdir[0] = 0;
321 conmask = 0;
322 call_hook(HOOK_REHASH);
323 if (!core_config.botname)
324 fatal("Please set the myname setting.\n", 0);
325 if (!userfile[0])
326 fatal(_("STARTING BOT IN USERFILE CREATION MODE.\n\
327 Telnet to the bot and enter 'NEW' as your nickname."), 0);
328 if (!readuserfile(userfile, &userlist)) {
329 if (!make_userfile) {
330 char tmp[178];
331
332 snprintf(tmp, sizeof tmp, _("USER FILE NOT FOUND! (try ./eggdrop -m %s to make one)\n"), configfile);
333 fatal(tmp, 0);
334 }
335 printf(_("\n\nSTARTING BOT IN USERFILE CREATION MODE.\n\
336 Telnet to the bot and enter 'NEW' as your nickname.\n"));
337 if (module_find("server", 0, 0))
338 printf(_("OR go to IRC and type: /msg %s hello\n"), core_config.botname);
339 printf(_("This will make the bot recognize you as the master.\n\n"));
340 } else if (make_userfile) {
341 make_userfile = 0;
342 printf(_("USERFILE ALREADY EXISTS (drop the -m)\n"));
343 }
344 if (helpdir[0])
345 if (helpdir[strlen(helpdir) - 1] != '/')
346 strcat(helpdir, "/");
347 reaffirm_owners();
348 }
349
350 /* Reload the user file from disk
351 */
352 void reload()
353 {
354 if (!file_readable(userfile)) {
355 putlog(LOG_MISC, "*", _("Cant reload user file!"));
356 return;
357 }
358
359 clear_userlist(userlist);
360 userlist = NULL;
361 if (!readuserfile(userfile, &userlist))
362 fatal(_("User file is missing!"), 0);
363 reaffirm_owners();
364 call_hook(HOOK_READ_USERFILE);
365 }
366
367 void rehash()
368 {
369 call_hook(HOOK_PRE_REHASH);
370 clear_userlist(userlist);
371 userlist = NULL;
372 chanprog();
373 }
374
375 /* Oddly enough, written by proton (Emech's coder)
376 */
377 int isowner(char *name)
378 {
379 char *pa, *pb, *owner;
380 char nl, pl;
381
382 owner = core_config.owner;
383 if (!owner || !*owner || !name || !*name) return(0);
384 nl = strlen(name);
385 pa = owner;
386 pb = owner;
387 while (1) {
388 while (1) {
389 if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
390 break;
391 pb++;
392 }
393 pl = (unsigned int) pb - (unsigned int) pa;
394 if (pl == nl && !strncasecmp(pa, name, nl))
395 return (1);
396 while (1) {
397 if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
398 break;
399 pb++;
400 }
401 if (*pb == 0)
402 return (0);
403 pa = pb;
404 }
405 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23