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

Annotation of /eggdrop1.9/src/core_party.c

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


Revision 1.32 - (hide annotations) (download) (as text)
Sat Jun 19 18:07:01 2004 UTC (14 years, 10 months ago) by wingman
Branch: MAIN
Changes since 1.31: +28 -8 lines
File MIME type: text/x-chdr
* Added new (debugging) command: .binds
* Added "framework" for init/shutdown/restart. We should really try to avoid
  use of exit system call. More cleanups to come later.

1 wcc 1.15 /* core_party.c: core partyline commands
2     *
3     * Copyright (C) 2003, 2004 Eggheads Development Team
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19 wcc 1.14
20 wcc 1.15 #ifndef lint
21 wingman 1.32 static const char rcsid[] = "$Id: core_party.c,v 1.31 2004/06/19 17:19:25 wingman Exp $";
22 wcc 1.15 #endif
23 wcc 1.14
24     #ifdef HAVE_CONFIG_H
25     # include <config.h>
26     #endif
27    
28 wcc 1.15 #include <ctype.h>
29     #include <unistd.h>
30 wcc 1.17 #include <string.h>
31     #include <stdlib.h>
32 wcc 1.15
33 wcc 1.14 #ifdef HAVE_UNAME
34     # include <sys/utsname.h>
35     #endif
36    
37 wcc 1.24 #include <eggdrop/eggdrop.h>
38    
39     #include "core_config.h"
40     #include "core_binds.h"
41     #include "logfile.h"
42 wingman 1.32 #include "main.h" /* SHUTDOWN_*, core_shutdown, core_restart */
43 wcc 1.24
44 wingman 1.27 /* from main.c */
45 wcc 1.14 extern char pid_file[];
46 stdarg 1.1
47 stdarg 1.4 static int party_join(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
48     {
49 wcc 1.12 if (!text || !*text) {
50 wcc 1.16 partymember_printf(p, _("Syntax: join <channel>"));
51 stdarg 1.4 return(0);
52     }
53     partychan_join_name(text, p);
54     return(0);
55     }
56    
57     static int party_part(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
58     {
59     partychan_t *chan;
60    
61 wcc 1.12 if (!text || !*text) chan = partychan_get_default(p);
62 stdarg 1.4 else chan = partychan_lookup_name(text);
63     partychan_part(chan, p, "parting");
64     return(0);
65     }
66    
67 stdarg 1.3 static int party_quit(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
68 stdarg 1.1 {
69 wingman 1.28 if (partyline_terminal_mode && 0 == strcmp (p->nick, PARTY_TERMINAL_NICK)) {
70 wingman 1.27 partymember_printf (p, "You can't leave the partyline in terminal mode.");
71     return -1;
72     }
73    
74 wcc 1.18 partymember_printf(p, "Goodbye!");
75     if (!text || !*text) partymember_delete(p, "Quit");
76 wcc 1.12 else partymember_delete(p, text);
77 stdarg 1.1 return(0);
78     }
79    
80 wcc 1.12 static int party_whisper(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
81 stdarg 1.1 {
82 stdarg 1.13 char *who;
83     const char *next;
84     partymember_t *dest;
85    
86 stdarg 1.21 egg_get_arg(text, &next, &who);
87 stdarg 1.13 if (!who || !next || !*who || !*next) {
88 wcc 1.16 partymember_printf(p, _("Syntax: whisper <partylineuser> <msg>"));
89 stdarg 1.13 goto done;
90     }
91    
92     dest = partymember_lookup_nick(who);
93     if (!dest) {
94 wcc 1.16 partymember_printf(p, _("No such user '%s'."), who);
95 stdarg 1.13 goto done;
96     }
97    
98     partymember_msg(dest, p, next, -1);
99     done:
100     if (who) free(who);
101 stdarg 1.1 return(0);
102     }
103    
104 stdarg 1.22 static void *lookup_setting(partymember_t *p, const char *path)
105 stdarg 1.11 {
106     void *root;
107    
108     root = config_get_root("eggdrop");
109     root = config_exists(root, path, 0, NULL);
110 stdarg 1.22 if (!root) partymember_printf(p, _("That setting does not exist."));
111 stdarg 1.11 return(root);
112     }
113    
114     static int party_get(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
115     {
116     void *root;
117     char *str = NULL;
118    
119     if (!text || !*text) {
120 wcc 1.16 partymember_printf(p, _("Syntax: get <path>"));
121 stdarg 1.11 return(0);
122     }
123    
124 stdarg 1.22 root = lookup_setting(p, text);
125 stdarg 1.11 if (!root) return(0);
126    
127     config_get_str(&str, root, NULL);
128 wcc 1.24 if (str) partymember_printf(p, "Current value: '%s'", str);
129     else partymember_printf(p, _("Current value: null (unset)"));
130 stdarg 1.11 return(0);
131     }
132    
133 stdarg 1.6 static int party_set(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
134 stdarg 1.2 {
135 stdarg 1.11 void *root;
136     char *path, *str;
137     const char *next;
138    
139 stdarg 1.21 egg_get_arg(text, &next, &path);
140 stdarg 1.22 if (!path) {
141     partymember_printf(p, _("Syntax: set <path> [new value]"));
142 stdarg 1.11 return(0);
143     }
144 stdarg 1.22 if (!next) {
145     free(path);
146     return party_get(p, nick, u, cmd, text);
147     }
148 wcc 1.14
149 stdarg 1.22 root = lookup_setting(p, path);
150 stdarg 1.11 free(path);
151     if (!root) return(0);
152    
153     config_get_str(&str, root, NULL);
154 wcc 1.16 partymember_printf(p, _("Old value: '%s'"), str);
155 stdarg 1.11 config_set_str(next, root, NULL);
156     config_get_str(&str, root, NULL);
157 stdarg 1.20 if (str) partymember_printf(p, _("New value: '%s'"), str);
158     else partymember_printf(p, _("New value: null (unset)"));
159     return(0);
160     }
161    
162     static int party_unset(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
163     {
164     void *root;
165     char *str;
166    
167     if (!text || !*text) {
168     partymember_printf(p, _("Syntax: unset <path>"));
169     return(0);
170     }
171    
172 stdarg 1.22 root = lookup_setting(p, text);
173 stdarg 1.20 if (!root) return(0);
174    
175     config_get_str(&str, root, NULL);
176     if (str) partymember_printf(p, _("Old value: '%s'"), str);
177     else partymember_printf(p, _("Old value: null (unset)"));
178     config_set_str(NULL, root, NULL);
179     config_get_str(&str, root, NULL);
180     if (str) partymember_printf(p, _("New value: '%s'"), str);
181     else partymember_printf(p, _("New value: null (unset)"));
182 wcc 1.14 return(0);
183     }
184    
185     static int party_status(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
186     {
187     #ifdef HAVE_UNAME
188     struct utsname un;
189     #endif
190    
191 stdarg 1.20 partymember_printf(p, _("I am %1$s, running Eggdrop %2$s."), core_config.botname, VERSION);
192 wcc 1.16 partymember_printf(p, _("Owner: %s"), core_config.owner);
193     if (core_config.admin) partymember_printf(p, _("Admin: %s"), core_config.admin);
194 wcc 1.14 #ifdef HAVE_UNAME
195 stdarg 1.20 if (!uname(&un)) partymember_printf(p, _("OS: %1$s %2$s"), un.sysname, un.release);
196 wcc 1.14 #endif
197 wingman 1.30 partymember_printf(p, _("Help path: %s (%d entries, %d sections)"), core_config.help_path, help_count_entries(),
198     help_count_sections ());
199 stdarg 1.20 partymember_printf(p, "");
200     check_bind_status(p, text);
201 stdarg 1.2 return(0);
202     }
203    
204 stdarg 1.6 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
205 stdarg 1.2 {
206 wcc 1.16 putlog(LOG_MISC, "*", _("Saving user file..."));
207 stdarg 1.5 user_save(core_config.userfile);
208 wcc 1.16 putlog(LOG_MISC, "*", _("Saving config file..."));
209 stdarg 1.2 core_config_save();
210     return(1);
211     }
212    
213 stdarg 1.5 static int party_newpass(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
214     {
215     if (!text || strlen(text) < 6) {
216 wcc 1.16 partymember_printf(p, _("Please use at least 6 characters."));
217 stdarg 1.5 return(0);
218     }
219     user_set_pass(p->user, text);
220 wcc 1.16 partymember_printf(p, _("Changed password to '%s'."), text);
221 stdarg 1.5 return(0);
222     }
223    
224 stdarg 1.7 static int intsorter(const void *left, const void *right)
225     {
226     return(*(int *)left - *(int *)right);
227     }
228    
229     static int party_who(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
230     {
231     partymember_t *who;
232 stdarg 1.20 int *pids, len, i, width = 0;
233 stdarg 1.7
234 wcc 1.16 partymember_printf(p, _("Partyline members:"));
235 stdarg 1.7 partymember_who(&pids, &len);
236     qsort(pids, len, sizeof(int), intsorter);
237 stdarg 1.20 if (len > 0) {
238     i = pids[len-1];
239     if (!i) i++;
240     while (i != 0) {
241     i /= 10;
242     width++;
243     }
244     }
245 stdarg 1.7 for (i = 0; i < len; i++) {
246     who = partymember_lookup_pid(pids[i]);
247 stdarg 1.20 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
248 stdarg 1.7 }
249     free(pids);
250     return(0);
251     }
252    
253 stdarg 1.23 static int party_whois(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
254     {
255     user_t *who;
256     flags_t flags;
257     char *item, *chan, *setting, *value, flagstr[64];
258     const char *next;
259     int i;
260    
261     if (text && *text) who = user_lookup_by_handle(text);
262     else who = u;
263    
264     if (!who) {
265     partymember_printf(p, "No such user.");
266     return(0);
267     }
268    
269     next = core_config.whois_items;
270     while (next && *next) {
271     egg_get_arg(next, &next, &item);
272     if (!strcasecmp(item, "handle")) {
273     partymember_printf(p, "%s: '%s'", item, who->handle);
274     }
275     else if (!strcasecmp(item, "uid")) {
276     partymember_printf(p, "%s: '%d'", item, who->uid);
277     }
278     else if (!strcasecmp(item, "ircmasks")) {
279     partymember_printf(p, "%s:", item);
280     for (i = 0; i < who->nircmasks; i++) {
281     partymember_printf(p, " %d. '%s'", i+1, who->ircmasks[i]);
282     }
283     }
284     else {
285 stdarg 1.25 if ((setting = strchr(item, '.'))) {
286 stdarg 1.23 chan = item;
287     *setting = 0;
288     setting++;
289     }
290     else {
291     chan = NULL;
292     setting = item;
293     }
294     if (!strcasecmp(setting, "flags")) {
295     user_get_flags(who, chan, &flags);
296     flag_to_str(&flags, flagstr);
297     value = flagstr;
298     }
299     else {
300     user_get_setting(who, chan, setting, &value);
301     }
302    
303     if (chan) partymember_printf(p, "%s.%s: %s", chan, setting, value);
304     else partymember_printf(p, "%s: %s", setting, value);
305     }
306     free(item);
307     }
308     return(0);
309     }
310    
311 stdarg 1.7 static int party_die(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
312     {
313 wingman 1.32 /* XXX: should we really enable hard shutdowns?
314     if (*text && 0 == strcmp(text, "force")) {
315     return core_shutdown(SHUTDOWN_HARD, nick, text);
316     } else
317     */
318     return core_shutdown(SHUTDOWN_GRACEFULL, nick, text);
319 stdarg 1.7 }
320    
321     static int party_plus_user(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
322     {
323     user_t *newuser;
324    
325     if (!text || !*text) {
326 wcc 1.16 partymember_printf(p, _("Syntax: +user <handle>"));
327 stdarg 1.7 return(0);
328     }
329     if (user_lookup_by_handle(text)) {
330 wcc 1.16 partymember_printf(p, _("User '%s' already exists!"));
331 stdarg 1.7 return(0);
332     }
333     newuser = user_new(text);
334 wcc 1.16 if (newuser) partymember_printf(p, _("User '%s' created."), text);
335     else partymember_printf(p, _("Could not create user '%s'."), text);
336 stdarg 1.7 return(0);
337     }
338    
339     static int party_minus_user(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
340     {
341     user_t *who;
342    
343     if (!text || !*text) {
344 wcc 1.16 partymember_printf(p, _("Syntax: -user <handle>"));
345 stdarg 1.7 return(0);
346     }
347     who = user_lookup_by_handle(text);
348 wcc 1.16 if (!who) partymember_printf(p, _("User '%s' not found."));
349 stdarg 1.7 else {
350 wcc 1.16 partymember_printf(p, _("Deleting user '%s'."), who->handle);
351 stdarg 1.7 user_delete(who);
352     }
353     return(0);
354     }
355    
356     static int party_plus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
357     {
358 stdarg 1.25 user_t *who;
359     char *target, *newhost;
360    
361     egg_get_args(text, NULL, &target, &newhost, NULL);
362     if (!target) {
363     partymember_printf(p, _("Syntax: +host [handle] <host>"));
364     return(0);
365     }
366     if (!newhost) {
367     newhost = target;
368     target = NULL;
369     }
370     if (target) {
371     who = user_lookup_by_handle(target);
372     if (!who) {
373     partymember_printf(p, _("User '%s' not found."), target);
374     goto done;
375     }
376     }
377     else {
378     who = u;
379     if (!who) {
380     partymember_printf(p, _("Only valid users can add hosts."));
381     goto done;
382     }
383     }
384     user_add_ircmask(who, newhost);
385     partymember_printf(p, _("Added '%1$s' to user '%2$s'."), newhost, who->handle);
386    
387     done:
388     if (target) free(target);
389     free(newhost);
390    
391 stdarg 1.7 return(0);
392     }
393    
394 stdarg 1.9 static int party_minus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
395     {
396 stdarg 1.25 user_t *who;
397     char *target, *host;
398    
399     egg_get_args(text, NULL, &target, &host, NULL);
400     if (!target) {
401     partymember_printf(p, _("Syntax: -host [handle] <host>"));
402     return(0);
403     }
404     if (!host) {
405     host = target;
406     target = NULL;
407     }
408     if (target) {
409     who = user_lookup_by_handle(target);
410     if (!who) {
411     partymember_printf(p, _("User '%s' not found."), target);
412     goto done;
413     }
414     }
415     else {
416     who = u;
417     if (!who) {
418     partymember_printf(p, _("Only valid users can remove hosts."));
419     goto done;
420     }
421     }
422     if (user_del_ircmask(who, host)) {
423     partymember_printf(p, _("Mask '%1$s' not found for user '%2$s'."), host, who->handle);
424     }
425     else {
426     partymember_printf(p, _("Removed '%1$s' from user '%2$s'."), host, who->handle);
427     }
428    
429     done:
430     if (target) free(target);
431     free(host);
432    
433 stdarg 1.9 return(0);
434     }
435    
436 stdarg 1.21 /* Syntax: chattr <user> [chan] <flags> */
437 stdarg 1.9 static int party_chattr(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
438     {
439     const char *next;
440     char *who, *flags, *chan;
441     user_t *dest;
442     flags_t flagstruct;
443     char flagstr[64];
444     int n;
445    
446 stdarg 1.21 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
447 wcc 1.14 if (!chan || !*chan) {
448 stdarg 1.10 if (who) free(who);
449 wcc 1.24 partymember_printf(p, _("Syntax: chattr <handle> [channel] <+/-flags>"));
450 stdarg 1.9 return(0);
451     }
452 wcc 1.14 if (!flags || !*flags) {
453 stdarg 1.9 flags = chan;
454     chan = NULL;
455     }
456     dest = user_lookup_by_handle(who);
457     if (dest) {
458 wingman 1.30 user_set_flags_str(dest, chan, flags);
459 stdarg 1.9 user_get_flags(dest, chan, &flagstruct);
460     flag_to_str(&flagstruct, flagstr);
461 wcc 1.16 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
462 stdarg 1.9 }
463 wcc 1.24 else partymember_printf(p, _("'%s' is not a valid user."), who);
464 stdarg 1.9 free(who);
465     free(flags);
466     if (chan) free(chan);
467     return(0);
468     }
469    
470 stdarg 1.26 static int party_help(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
471     {
472 wingman 1.30 return help_print_party(p, text);
473 stdarg 1.26 }
474    
475     static int party_addlog(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
476     {
477     putlog(LOG_MISC, "*", "%s: %s", nick, text);
478 wingman 1.30
479     return BIND_RET_LOG;
480 stdarg 1.26 }
481    
482 wcc 1.12 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
483     {
484 stdarg 1.19 const char **modules;
485 wcc 1.12 int nummods, ctr;
486    
487 stdarg 1.19 nummods = module_list(&modules);
488 wcc 1.16 partymember_printf(p, _("Loaded modules:"));
489 wcc 1.12 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
490     free(modules);
491 wingman 1.30
492     return BIND_RET_LOG;
493 wcc 1.12 }
494    
495 stdarg 1.26 static int party_loadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
496 wcc 1.24 {
497     if (!text || !*text) {
498 stdarg 1.26 partymember_printf(p, _("Syntax: loadmod <module name>"));
499 wingman 1.30 return BIND_RET_BREAK;
500 wcc 1.24 }
501 stdarg 1.26 switch (module_load(text)) {
502     case 0:
503     partymember_printf(p, _("Module '%s' loaded successfully."), text);
504     break;
505     case -1:
506     partymember_printf(p, _("Module '%s' is already loaded."), text);
507     break;
508     case -2:
509     partymember_printf(p, _("Module '%s' could not be loaded."), text);
510     break;
511     case -3:
512     partymember_printf(p, _("Module '%s' does not have a valid initialization function. Perhaps it is not an eggdrop module?"), text);
513     break;
514     }
515 wingman 1.30 return BIND_RET_LOG;
516 stdarg 1.26 }
517 wcc 1.24
518 stdarg 1.26 static int party_unloadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
519     {
520     if (!text || !*text) {
521     partymember_printf(p, _("Syntax: unloadmod <module name>"));
522 wingman 1.30 return BIND_RET_BREAK;
523 wcc 1.24 }
524 stdarg 1.26 switch (module_unload(text, EGGMOD_USER)) {
525     case 0:
526     partymember_printf(p, _("Module '%s' unloaded successfully."), text);
527     break;
528     case -1:
529     partymember_printf(p, _("Module '%s' is not loaded."), text);
530     break;
531     case -2:
532     partymember_printf(p, _("Module '%s' has dependencies that are still loaded. You must unload them first."), text);
533     break;
534     case -3:
535     partymember_printf(p, _("Module '%s' refuses to be unloaded by you!"), text);
536     break;
537     }
538 wingman 1.30 return BIND_RET_LOG;
539 wcc 1.24 }
540    
541 wingman 1.32 static int party_binds(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
542     {
543     bind_table_t *table;
544     bind_entry_t *entry;
545     char flags[64];
546    
547     partymember_printf(p, "%-16s %-16s %-16s %-10s %-5s %s", _("TABLE"), _("SYNTAX"),
548     _("FUNCTION"), _("MASK"), _("FLAGS"), _("HITS"));
549     for (table = bind_table_list(); table; table = table->next) {
550     for (entry = table->entries; entry; entry = entry->next) {
551     flag_to_str(&entry->user_flags, flags);
552     partymember_printf(p, "%-16s %-16s %-16s %-10s %-5s %i", table->name, table->syntax,
553     entry->function_name, entry->mask, flags, entry->nhits);
554     }
555     }
556    
557     return BIND_RET_LOG;
558     }
559    
560 stdarg 1.1 static bind_list_t core_party_binds[] = {
561 wingman 1.29 {NULL, "join", party_join}, /* DDD */
562     {NULL, "whisper", party_whisper}, /* DDD */
563     {NULL, "newpass", party_newpass}, /* DDD */
564 wingman 1.30 {NULL, "help", party_help}, /* DDC */
565 wingman 1.29 {NULL, "part", party_part}, /* DDD */
566     {NULL, "quit", party_quit}, /* DDD */
567     {NULL, "who", party_who}, /* DDD */
568     {NULL, "whois", party_whois}, /* DDD */
569     {"n", "addlog", party_addlog}, /* DDD */
570 wingman 1.30 {"n", "get", party_get}, /* DDC */
571     {"n", "set", party_set}, /* DDC */
572     {"n", "unset", party_unset}, /* DDC */
573 wingman 1.31 {"n", "status", party_status}, /* DDC */
574 wingman 1.29 {"n", "save", party_save}, /* DDD */
575     {"n", "die", party_die}, /* DDD */
576 wingman 1.30 {"n", "+user", party_plus_user}, /* DDC */
577     {"n", "-user", party_minus_user}, /* DDC */
578     {"n", "chattr", party_chattr}, /* DDC */
579 wingman 1.29 {"n", "modules", party_modules}, /* DDD */
580     {"n", "loadmod", party_loadmod}, /* DDD */
581     {"n", "unloadmod", party_unloadmod}, /* DDD */
582 wingman 1.32 {"n", "binds", party_binds}, /* DDD */
583 wingman 1.30 {"m", "+host", party_plus_host}, /* DDC */
584     {"m", "-host", party_minus_host}, /* DDC */
585 stdarg 1.1 {0}
586     };
587    
588 wingman 1.27 void core_party_init(void)
589 stdarg 1.1 {
590     bind_add_list("party", core_party_binds);
591     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23