/[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.35 - (hide annotations) (download) (as text)
Wed Jun 23 21:12:57 2004 UTC (14 years, 11 months ago) by stdarg
Branch: MAIN
Changes since 1.34: +4 -3 lines
File MIME type: text/x-chdr
* Basic command line parameter access for scripts/modules (not finished)
* Remove special case for stdin/stdio from socket.c

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23