/[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.26 - (hide annotations) (download) (as text)
Sun Feb 15 05:36:56 2004 UTC (15 years, 3 months ago) by stdarg
Branch: MAIN
Changes since 1.25: +65 -10 lines
File MIME type: text/x-chdr
* .loadmod and .unloadmod

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23