/[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.61 - (show annotations) (download) (as text)
Tue Feb 25 06:52:19 2003 UTC (16 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.60: +2 -26 lines
File MIME type: text/x-chdr
* Well, I think Tcl is finally removed from the core (except for detection/configuration).

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.60 2003/02/17 10:22:30 stdarg 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 "tcl.h" /* readtclprog */
46 #include "userrec.h" /* count_users, clear_userlist */
47
48 extern struct userrec *userlist;
49 extern char ver[], firewall[], motdfile[], userfile[], helpdir[],
50 moddir[], notify_new[], owner[], configfile[];
51 extern time_t now, online_since;
52 extern int backgrd, term_z, con_chan, cache_hit, cache_miss, firewallport,
53 default_flags, conmask, protect_readonly, make_userfile,
54 ignore_time;
55
56 struct chanset_t *chanset = NULL; /* Channel list */
57 char admin[121] = ""; /* Admin info */
58 char origbotname[NICKLEN + 1];
59 char *botname; /* Primary botname */
60
61
62 /* Returns memberfields if the nick is in the member list.
63 */
64 memberlist *ismember(struct chanset_t *chan, char *nick)
65 {
66 register memberlist *x;
67
68 for (x = chan->channel.member; x && x->nick[0]; x = x->next)
69 if (!irccmp(x->nick, nick))
70 return x;
71 return NULL;
72 }
73
74 /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
75 */
76 struct chanset_t *findchan(const char *name)
77 {
78 register struct chanset_t *chan;
79
80 for (chan = chanset; chan; chan = chan->next)
81 if (!irccmp(chan->name, name))
82 return chan;
83 return NULL;
84 }
85
86 /* Find a chanset by display name (ie !channel)
87 */
88 struct chanset_t *findchan_by_dname(const char *name)
89 {
90 register struct chanset_t *chan;
91
92 for (chan = chanset; chan; chan = chan->next)
93 if (!irccmp(chan->dname, name))
94 return chan;
95 return NULL;
96 }
97
98 /*
99 * "caching" functions
100 */
101
102 /* Shortcut for get_user_by_host -- might have user record in one
103 * of the channel caches.
104 */
105 struct userrec *check_chanlist(const char *host)
106 {
107 char *nick, *uhost, buf[UHOSTLEN];
108 register memberlist *m;
109 register struct chanset_t *chan;
110
111 strlcpy(buf, host, sizeof buf);
112 nick = strtok(buf, "!");
113 uhost = strtok(NULL, "!");
114 for (chan = chanset; chan; chan = chan->next)
115 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
116 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
117 return m->user;
118 return NULL;
119 }
120
121 /* Shortcut for get_user_by_handle -- might have user record in channels
122 */
123 struct userrec *check_chanlist_hand(const char *hand)
124 {
125 register struct chanset_t *chan;
126 register memberlist *m;
127
128 for (chan = chanset; chan; chan = chan->next)
129 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
130 if (m->user && !strcasecmp(m->user->handle, hand))
131 return m->user;
132 return NULL;
133 }
134
135 /* Clear the user pointers in the chanlists.
136 *
137 * Necessary when a hostmask is added/removed, a user is added or a new
138 * userfile is loaded.
139 */
140 void clear_chanlist(void)
141 {
142 register memberlist *m;
143 register struct chanset_t *chan;
144
145 for (chan = chanset; chan; chan = chan->next)
146 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
147 m->user = NULL;
148 }
149
150 /* Clear the user pointer of a specific nick in the chanlists.
151 *
152 * Necessary when a hostmask is added/removed, a nick changes, etc.
153 * Does not completely invalidate the channel cache like clear_chanlist().
154 */
155 void clear_chanlist_member(const char *nick)
156 {
157 register memberlist *m;
158 register struct chanset_t *chan;
159
160 for (chan = chanset; chan; chan = chan->next)
161 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
162 if (!irccmp(m->nick, nick)) {
163 m->user = NULL;
164 break;
165 }
166 }
167
168 /* If this user@host is in a channel, set it (it was null)
169 */
170 void set_chanlist(const char *host, struct userrec *rec)
171 {
172 char *nick, *uhost, buf[UHOSTLEN];
173 register memberlist *m;
174 register struct chanset_t *chan;
175
176 strlcpy(buf, host, sizeof buf);
177 nick = strtok(buf, "!");
178 uhost = strtok(NULL, "!");
179 for (chan = chanset; chan; chan = chan->next)
180 for (m = chan->channel.member; m && m->nick[0]; m = m->next)
181 if (!irccmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
182 m->user = rec;
183 }
184
185 /* Dump status info out to dcc
186 */
187 void tell_verbose_status(int idx)
188 {
189 char s[256], s1[121], s2[81];
190 char *vers_t, *uni_t;
191 int i;
192 time_t now2, hr, min;
193 #if HAVE_GETRUSAGE
194 struct rusage ru;
195 #else
196 # if HAVE_CLOCK
197 clock_t cl;
198 # endif
199 #endif
200 #ifdef HAVE_UNAME
201 struct utsname un;
202
203 if (!uname(&un) < 0) {
204 #endif
205 vers_t = " ";
206 uni_t = _("*unknown*");
207 #ifdef HAVE_UNAME
208 } else {
209 vers_t = un.release;
210 uni_t = un.sysname;
211 }
212 #endif
213
214 i = count_users(userlist);
215 dprintf(idx, _("I am %1$s, running %2$s: "), core_config.botname, ver);
216 dprintf(idx, P_("%d user\n", "%d users\n", i), i);
217 now2 = now - online_since;
218 s[0] = 0;
219
220 if (now2 > 86400) {
221 int days = (int) (now2 / 86400);
222
223 snprintf(s, sizeof(s), P_("%d day", "%d days", days), days);
224 strcat(s, ", ");
225 now2 -= days * 86400;
226 }
227 hr = (time_t) ((int) now2 / 3600);
228 now2 -= (hr * 3600);
229 min = (time_t) ((int) now2 / 60);
230 snprintf(&s[strlen(s)], sizeof(s) - strlen(s), "%02d:%02d", (int) hr,
231 (int) min);
232 s1[0] = 0;
233 if (backgrd)
234 strcpy(s1, _("background"));
235 else {
236 if (term_z)
237 strcpy(s1, _("terminal mode"));
238 else if (con_chan)
239 strcpy(s1, _("status mode"));
240 else
241 strcpy(s1, _("log dump mode"));
242 }
243 #if HAVE_GETRUSAGE
244 getrusage(RUSAGE_SELF, &ru);
245 hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
246 min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
247 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
248 #else
249 # if HAVE_CLOCK
250 cl = (clock() / CLOCKS_PER_SEC);
251 hr = (int) (cl / 60);
252 min = (int) (cl - (hr * 60));
253 sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
254 # else
255 sprintf(s2, "CPU ???");
256 # endif
257 #endif
258 dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", _("Online for"),
259 s, s1, s2, _("cache hit"),
260 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
261 if (admin[0])
262 dprintf(idx, _("Admin: %s\n"), admin);
263 dprintf(idx, _("Config file: %s\n"), configfile);
264 dprintf(idx, _("OS: %1$s %2$s\n"), uni_t, vers_t);
265 }
266
267 /* Show all internal state variables
268 */
269 void tell_settings(int idx)
270 {
271 char s[1024];
272 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
273
274 dprintf(idx, _("My name: %s\n"), core_config.botname);
275 if (firewall[0])
276 dprintf(idx, _("Firewall: %1$s, port %2$d\n"), firewall, firewallport);
277 dprintf(idx, _("Userfile: %1$s Motd: %2$s\n"), userfile, motdfile);
278 dprintf(idx, _("Directories:\n"));
279 dprintf(idx, _(" Help : %s\n"), helpdir);
280 dprintf(idx, _(" Modules : %s\n"), moddir);
281 fr.global = default_flags;
282
283 build_flags(s, &fr, NULL);
284 dprintf(idx, "%s [%s], %s: %s\n", _("New users get flags"), s,
285 _("notify"), notify_new);
286 /* FIXME PLURAL: handle it properly */
287 if (owner[0])
288 dprintf(idx, _("Permanent owner(s): %s\n"), owner);
289 dprintf(idx, _("Ignores last %d mins\n"), ignore_time);
290 }
291
292 void reaffirm_owners()
293 {
294 char *p, *q, s[121];
295 struct userrec *u;
296
297 /* Make sure default owners are +n */
298 if (owner[0]) {
299 q = owner;
300 p = strchr(q, ',');
301 while (p) {
302 strlcpy(s, q, (p - q) + 1);
303 rmspace(s);
304 u = get_user_by_handle(userlist, s);
305 if (u)
306 u->flags = sanity_check(u->flags | USER_OWNER);
307 q = p + 1;
308 p = strchr(q, ',');
309 }
310 strcpy(s, q);
311 rmspace(s);
312 u = get_user_by_handle(userlist, s);
313 if (u)
314 u->flags = sanity_check(u->flags | USER_OWNER);
315 }
316 }
317
318 void chanprog()
319 {
320 admin[0] = 0;
321 helpdir[0] = 0;
322 conmask = 0;
323 call_hook(HOOK_REHASH);
324 if (!core_config.botname)
325 fatal("Please set the myname setting.\n", 0);
326 if (!userfile[0])
327 fatal(_("STARTING BOT IN USERFILE CREATION MODE.\n\
328 Telnet to the bot and enter 'NEW' as your nickname."), 0);
329 if (!readuserfile(userfile, &userlist)) {
330 if (!make_userfile) {
331 char tmp[178];
332
333 snprintf(tmp, sizeof tmp, _("USER FILE NOT FOUND! (try ./eggdrop -m %s to make one)\n"), configfile);
334 fatal(tmp, 0);
335 }
336 printf(_("\n\nSTARTING BOT IN USERFILE CREATION MODE.\n\
337 Telnet to the bot and enter 'NEW' as your nickname.\n"));
338 if (module_find("server", 0, 0))
339 printf(_("OR go to IRC and type: /msg %s hello\n"), core_config.botname);
340 printf(_("This will make the bot recognize you as the master.\n\n"));
341 } else if (make_userfile) {
342 make_userfile = 0;
343 printf(_("USERFILE ALREADY EXISTS (drop the -m)\n"));
344 }
345 if (helpdir[0])
346 if (helpdir[strlen(helpdir) - 1] != '/')
347 strcat(helpdir, "/");
348 reaffirm_owners();
349 }
350
351 /* Reload the user file from disk
352 */
353 void reload()
354 {
355 if (!file_readable(userfile)) {
356 putlog(LOG_MISC, "*", _("Cant reload user file!"));
357 return;
358 }
359
360 clear_userlist(userlist);
361 userlist = NULL;
362 if (!readuserfile(userfile, &userlist))
363 fatal(_("User file is missing!"), 0);
364 reaffirm_owners();
365 call_hook(HOOK_READ_USERFILE);
366 }
367
368 void rehash()
369 {
370 call_hook(HOOK_PRE_REHASH);
371 clear_userlist(userlist);
372 userlist = NULL;
373 chanprog();
374 }
375
376 /* Oddly enough, written by proton (Emech's coder)
377 */
378 int isowner(char *name)
379 {
380 char *pa, *pb, *owner;
381 char nl, pl;
382
383 owner = core_config.owner;
384 if (!owner || !*owner || !name || !*name) return(0);
385 nl = strlen(name);
386 pa = owner;
387 pb = owner;
388 while (1) {
389 while (1) {
390 if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
391 break;
392 pb++;
393 }
394 pl = (unsigned int) pb - (unsigned int) pa;
395 if (pl == nl && !strncasecmp(pa, name, nl))
396 return (1);
397 while (1) {
398 if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
399 break;
400 pb++;
401 }
402 if (*pb == 0)
403 return (0);
404 pa = pb;
405 }
406 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23