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

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

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


Revision 1.37 - (show annotations) (download) (as text)
Mon Jun 28 20:44:38 2004 UTC (14 years, 10 months ago) by darko
Branch: MAIN
Changes since 1.36: +33 -4 lines
File MIME type: text/x-chdr
- Added .chpass <handle> <new_pass>
- Fixed .chhandle to advertize change if affected user is on partyline.

1 /* 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
20 #ifndef lint
21 static const char rcsid[] = "$Id: core_party.c,v 1.36 2004/06/23 21:26:06 darko Exp $";
22 #endif
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #ifdef HAVE_UNAME
34 # include <sys/utsname.h>
35 #endif
36
37 #include <eggdrop/eggdrop.h>
38
39 #include "core_config.h"
40 #include "core_binds.h"
41 #include "logfile.h"
42 #include "terminal.h" /* TERMINAL_NICK */
43 #include "main.h" /* SHUTDOWN_*, core_shutdown, core_restart */
44
45 /* from main.c */
46 extern char pid_file[];
47
48 static int party_join(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
49 {
50 if (!text || !*text) {
51 partymember_printf(p, _("Syntax: join <channel>"));
52 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 if (!text || !*text) chan = partychan_get_default(p);
63 else chan = partychan_lookup_name(text);
64 partychan_part(chan, p, "parting");
65 return(0);
66 }
67
68 static int party_quit(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
69 {
70 if (0 == strcmp (p->nick, TERMINAL_NICK)) {
71 partymember_printf (p, "You can't leave the partyline in terminal mode.");
72 return -1;
73 }
74
75 partymember_printf(p, "Goodbye!");
76 if (!text || !*text) partymember_delete(p, "Quit");
77 else partymember_delete(p, text);
78 return(0);
79 }
80
81 static int party_whisper(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
82 {
83 char *who;
84 const char *next;
85 partymember_t *dest;
86
87 egg_get_arg(text, &next, &who);
88 if (!who || !next || !*who || !*next) {
89 partymember_printf(p, _("Syntax: whisper <partylineuser> <msg>"));
90 goto done;
91 }
92
93 dest = partymember_lookup_nick(who);
94 if (!dest) {
95 partymember_printf(p, _("No such user '%s'."), who);
96 goto done;
97 }
98
99 partymember_msg(dest, p, next, -1);
100 done:
101 if (who) free(who);
102 return(0);
103 }
104
105 static void *lookup_setting(partymember_t *p, const char *path)
106 {
107 void *root;
108
109 root = config_get_root("eggdrop");
110 root = config_exists(root, path, 0, NULL);
111 if (!root) partymember_printf(p, _("That setting does not exist."));
112 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 partymember_printf(p, _("Syntax: get <path>"));
122 return(0);
123 }
124
125 root = lookup_setting(p, text);
126 if (!root) return(0);
127
128 config_get_str(&str, root, NULL);
129 if (str) partymember_printf(p, "Current value: '%s'", str);
130 else partymember_printf(p, _("Current value: null (unset)"));
131 return(0);
132 }
133
134 static int party_set(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
135 {
136 void *root;
137 char *path, *str;
138 const char *next;
139
140 egg_get_arg(text, &next, &path);
141 if (!path) {
142 partymember_printf(p, _("Syntax: set <path> [new value]"));
143 return(0);
144 }
145 if (!next) {
146 free(path);
147 return party_get(p, nick, u, cmd, text);
148 }
149
150 root = lookup_setting(p, path);
151 free(path);
152 if (!root) return(0);
153
154 config_get_str(&str, root, NULL);
155 partymember_printf(p, _("Old value: '%s'"), str);
156 config_set_str(next, root, NULL);
157 config_get_str(&str, root, NULL);
158 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 root = lookup_setting(p, text);
174 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 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 partymember_printf(p, _("I am %1$s, running Eggdrop %2$s."), core_config.botname, VERSION);
193 partymember_printf(p, _("Owner: %s"), core_config.owner);
194 if (core_config.admin) partymember_printf(p, _("Admin: %s"), core_config.admin);
195 #ifdef HAVE_UNAME
196 if (!uname(&un)) partymember_printf(p, _("OS: %1$s %2$s"), un.sysname, un.release);
197 #endif
198 partymember_printf(p, _("Help path: %s (%d entries, %d sections)"),
199 core_config.help_path, help_count_entries(),
200 help_count_sections());
201 partymember_printf(p, "");
202 check_bind_status(p, text);
203 return(0);
204 }
205
206 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
207 {
208 putlog(LOG_MISC, "*", _("Saving user file..."));
209 user_save(core_config.userfile);
210 putlog(LOG_MISC, "*", _("Saving config file..."));
211 core_config_save();
212 return(1);
213 }
214
215 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 partymember_printf(p, _("Please use at least 6 characters."));
219 return(0);
220 }
221 user_set_pass(p->user, text);
222 partymember_printf(p, _("Changed password to '%s'."), text);
223 return(0);
224 }
225
226 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 int *pids, len, i, width = 0;
235
236 partymember_printf(p, _("Partyline members:"));
237 partymember_who(&pids, &len);
238 qsort(pids, len, sizeof(int), intsorter);
239 if (len > 0) {
240 i = pids[len-1];
241 if (!i) i++;
242 while (i != 0) {
243 i /= 10;
244 width++;
245 }
246 }
247 for (i = 0; i < len; i++) {
248 who = partymember_lookup_pid(pids[i]);
249 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
250 }
251 free(pids);
252 return(0);
253 }
254
255 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 if ((setting = strchr(item, '.'))) {
288 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 static int party_die(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
314 {
315 /* 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 }
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 partymember_printf(p, _("Syntax: +user <handle>"));
329 return(0);
330 }
331 if (user_lookup_by_handle(text)) {
332 partymember_printf(p, _("User '%s' already exists!"));
333 return(0);
334 }
335 newuser = user_new(text);
336 if (newuser) partymember_printf(p, _("User '%s' created."), text);
337 else partymember_printf(p, _("Could not create user '%s'."), text);
338 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 partymember_printf(p, _("Syntax: -user <handle>"));
347 return(0);
348 }
349 who = user_lookup_by_handle(text);
350 if (!who) partymember_printf(p, _("User '%s' not found."));
351 else {
352 partymember_printf(p, _("Deleting user '%s'."), who->handle);
353 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 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 return(0);
394 }
395
396 static int party_minus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
397 {
398 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 return(0);
436 }
437
438 /* Syntax: chattr <user> [chan] <flags> */
439 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 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
449 if (!chan || !*chan) {
450 if (who) free(who);
451 partymember_printf(p, _("Syntax: chattr <handle> [channel] <+/-flags>"));
452 return(0);
453 }
454 if (!flags || !*flags) {
455 flags = chan;
456 chan = NULL;
457 }
458 dest = user_lookup_by_handle(who);
459 if (dest) {
460 user_set_flags_str(dest, chan, flags);
461 user_get_flags(dest, chan, &flagstruct);
462 flag_to_str(&flagstruct, flagstr);
463 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
464 }
465 else partymember_printf(p, _("'%s' is not a valid user."), who);
466 free(who);
467 free(flags);
468 if (chan) free(chan);
469 return(0);
470 }
471
472 static int party_help(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
473 {
474 return help_print_party(p, text);
475 }
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
481 return BIND_RET_LOG;
482 }
483
484 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
485 {
486 const char **modules;
487 int nummods, ctr;
488
489 nummods = module_list(&modules);
490 partymember_printf(p, _("Loaded modules:"));
491 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
492 free(modules);
493
494 return BIND_RET_LOG;
495 }
496
497 static int party_loadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
498 {
499 if (!text || !*text) {
500 partymember_printf(p, _("Syntax: loadmod <module name>"));
501 return BIND_RET_BREAK;
502 }
503 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 return BIND_RET_LOG;
518 }
519
520 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 return BIND_RET_BREAK;
525 }
526 switch (module_unload(text, MODULE_USER)) {
527 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 return BIND_RET_LOG;
541 }
542
543 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 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 /* Syntax: chhandle <old_handle> <new_handle> */
570 static int party_chhandle(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
571 {
572 char *old = NULL, *new = NULL;
573 user_t *dest;
574
575 egg_get_args(text, NULL, &old, &new, NULL);
576 if (!old || !new || !*old || !*new) {
577 partymember_printf(p, _("Syntax: chhandle <old_handle> <new_handle>"));
578 return 0;
579 }
580
581 dest = user_lookup_by_handle(old);
582 if (!dest) {
583 partymember_printf(p, _("Error: User '%s' does not exist."), old);
584 return 0;
585 }
586
587 if (user_lookup_by_handle(new)) {
588 partymember_printf(p, _("Error: User '%s' already exists."), new);
589 return 0;
590 }
591
592 if (user_change_handle(dest, old, new))
593 partymember_printf(p, _("Ok, changed."));
594 return 0;
595 }
596
597 static int party_chpass(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
598 {
599 char *user = NULL, *pass = NULL;
600 user_t *dest;
601
602 egg_get_args(text, NULL, &user, &pass, NULL);
603
604 if (!user || !pass || !*user || !*pass) {
605 partymember_printf(p, _("Syntax: chpass <handle> <pass>"));
606 return 0;
607 }
608
609 dest = user_lookup_by_handle(user);
610 if (!dest) {
611 partymember_printf(p, _("Error: User '%s' does not exist."), user);
612 return 0;
613 }
614
615 if (strlen(pass) < 6) {
616 partymember_printf(p, _("Error: Please use at least 6 characters."));
617 return 0;
618 }
619
620 user_set_pass(dest, pass);
621 partymember_printf(p, _("Password for %s is now '%s'."), user, pass);
622 return 0;
623 }
624
625 static bind_list_t core_party_binds[] = {
626 {NULL, "join", party_join}, /* DDD */
627 {NULL, "whisper", party_whisper}, /* DDD */
628 {NULL, "newpass", party_newpass}, /* DDD */
629 {NULL, "help", party_help}, /* DDC */
630 {NULL, "part", party_part}, /* DDD */
631 {NULL, "quit", party_quit}, /* DDD */
632 {NULL, "who", party_who}, /* DDD */
633 {NULL, "whois", party_whois}, /* DDD */
634 {"n", "addlog", party_addlog}, /* DDD */
635 {"n", "get", party_get}, /* DDC */
636 {"n", "set", party_set}, /* DDC */
637 {"n", "unset", party_unset}, /* DDC */
638 {"n", "status", party_status}, /* DDC */
639 {"n", "save", party_save}, /* DDD */
640 {"n", "die", party_die}, /* DDD */
641 {"n", "restart", party_restart}, /* DDD */
642 {"n", "+user", party_plus_user}, /* DDC */
643 {"n", "-user", party_minus_user}, /* DDC */
644 {"n", "chattr", party_chattr}, /* DDC */
645 {"n", "modules", party_modules}, /* DDD */
646 {"n", "loadmod", party_loadmod}, /* DDD */
647 {"n", "unloadmod", party_unloadmod}, /* DDD */
648 {"n", "binds", party_binds}, /* DDD */
649 {"m", "+host", party_plus_host}, /* DDC */
650 {"m", "-host", party_minus_host}, /* DDC */
651 {"t", "chhandle", party_chhandle}, /* DDD */
652 {"t", "chpass", party_chpass}, /* DDD */
653 {0}
654 };
655
656 void core_party_init(void)
657 {
658 bind_add_list("party", core_party_binds);
659 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23