/[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.34 - (show annotations) (download) (as text)
Mon Jun 21 20:35:11 2004 UTC (15 years, 3 months ago) by wingman
Branch: MAIN
Changes since 1.33: +2 -2 lines
File MIME type: text/x-chdr
* Renamed eggconfig.[hc] => config.[hc]
* Renamed eggdns.[hc] => dns.[hc]
* Renamed eggtimer.[hc] => timer.[hc]
* Renamed eggmod.[hc] => module.[hc]
* Renamed eggnet.[hc] => net.[hc]
* Renamed eggowner.[hc] => owner.[hc]
* Renamed eggident.[hc] => ident.[hc]

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.33 2004/06/20 13:33:48 wingman 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)"), core_config.help_path, help_count_entries(),
199 help_count_sections ());
200 partymember_printf(p, "");
201 check_bind_status(p, text);
202 return(0);
203 }
204
205 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
206 {
207 putlog(LOG_MISC, "*", _("Saving user file..."));
208 user_save(core_config.userfile);
209 putlog(LOG_MISC, "*", _("Saving config file..."));
210 core_config_save();
211 return(1);
212 }
213
214 static int party_newpass(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
215 {
216 if (!text || strlen(text) < 6) {
217 partymember_printf(p, _("Please use at least 6 characters."));
218 return(0);
219 }
220 user_set_pass(p->user, text);
221 partymember_printf(p, _("Changed password to '%s'."), text);
222 return(0);
223 }
224
225 static int intsorter(const void *left, const void *right)
226 {
227 return(*(int *)left - *(int *)right);
228 }
229
230 static int party_who(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
231 {
232 partymember_t *who;
233 int *pids, len, i, width = 0;
234
235 partymember_printf(p, _("Partyline members:"));
236 partymember_who(&pids, &len);
237 qsort(pids, len, sizeof(int), intsorter);
238 if (len > 0) {
239 i = pids[len-1];
240 if (!i) i++;
241 while (i != 0) {
242 i /= 10;
243 width++;
244 }
245 }
246 for (i = 0; i < len; i++) {
247 who = partymember_lookup_pid(pids[i]);
248 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
249 }
250 free(pids);
251 return(0);
252 }
253
254 static int party_whois(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
255 {
256 user_t *who;
257 flags_t flags;
258 char *item, *chan, *setting, *value, flagstr[64];
259 const char *next;
260 int i;
261
262 if (text && *text) who = user_lookup_by_handle(text);
263 else who = u;
264
265 if (!who) {
266 partymember_printf(p, "No such user.");
267 return(0);
268 }
269
270 next = core_config.whois_items;
271 while (next && *next) {
272 egg_get_arg(next, &next, &item);
273 if (!strcasecmp(item, "handle")) {
274 partymember_printf(p, "%s: '%s'", item, who->handle);
275 }
276 else if (!strcasecmp(item, "uid")) {
277 partymember_printf(p, "%s: '%d'", item, who->uid);
278 }
279 else if (!strcasecmp(item, "ircmasks")) {
280 partymember_printf(p, "%s:", item);
281 for (i = 0; i < who->nircmasks; i++) {
282 partymember_printf(p, " %d. '%s'", i+1, who->ircmasks[i]);
283 }
284 }
285 else {
286 if ((setting = strchr(item, '.'))) {
287 chan = item;
288 *setting = 0;
289 setting++;
290 }
291 else {
292 chan = NULL;
293 setting = item;
294 }
295 if (!strcasecmp(setting, "flags")) {
296 user_get_flags(who, chan, &flags);
297 flag_to_str(&flags, flagstr);
298 value = flagstr;
299 }
300 else {
301 user_get_setting(who, chan, setting, &value);
302 }
303
304 if (chan) partymember_printf(p, "%s.%s: %s", chan, setting, value);
305 else partymember_printf(p, "%s: %s", setting, value);
306 }
307 free(item);
308 }
309 return(0);
310 }
311
312 static int party_die(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
313 {
314 /* XXX: should we really enable hard shutdowns?
315 if (*text && 0 == strcmp(text, "force")) {
316 return core_shutdown(SHUTDOWN_HARD, nick, text);
317 } else
318 */
319 return core_shutdown(SHUTDOWN_GRACEFULL, nick, text);
320 }
321
322 static int party_plus_user(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
323 {
324 user_t *newuser;
325
326 if (!text || !*text) {
327 partymember_printf(p, _("Syntax: +user <handle>"));
328 return(0);
329 }
330 if (user_lookup_by_handle(text)) {
331 partymember_printf(p, _("User '%s' already exists!"));
332 return(0);
333 }
334 newuser = user_new(text);
335 if (newuser) partymember_printf(p, _("User '%s' created."), text);
336 else partymember_printf(p, _("Could not create user '%s'."), text);
337 return(0);
338 }
339
340 static int party_minus_user(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
341 {
342 user_t *who;
343
344 if (!text || !*text) {
345 partymember_printf(p, _("Syntax: -user <handle>"));
346 return(0);
347 }
348 who = user_lookup_by_handle(text);
349 if (!who) partymember_printf(p, _("User '%s' not found."));
350 else {
351 partymember_printf(p, _("Deleting user '%s'."), who->handle);
352 user_delete(who);
353 }
354 return(0);
355 }
356
357 static int party_plus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
358 {
359 user_t *who;
360 char *target, *newhost;
361
362 egg_get_args(text, NULL, &target, &newhost, NULL);
363 if (!target) {
364 partymember_printf(p, _("Syntax: +host [handle] <host>"));
365 return(0);
366 }
367 if (!newhost) {
368 newhost = target;
369 target = NULL;
370 }
371 if (target) {
372 who = user_lookup_by_handle(target);
373 if (!who) {
374 partymember_printf(p, _("User '%s' not found."), target);
375 goto done;
376 }
377 }
378 else {
379 who = u;
380 if (!who) {
381 partymember_printf(p, _("Only valid users can add hosts."));
382 goto done;
383 }
384 }
385 user_add_ircmask(who, newhost);
386 partymember_printf(p, _("Added '%1$s' to user '%2$s'."), newhost, who->handle);
387
388 done:
389 if (target) free(target);
390 free(newhost);
391
392 return(0);
393 }
394
395 static int party_minus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
396 {
397 user_t *who;
398 char *target, *host;
399
400 egg_get_args(text, NULL, &target, &host, NULL);
401 if (!target) {
402 partymember_printf(p, _("Syntax: -host [handle] <host>"));
403 return(0);
404 }
405 if (!host) {
406 host = target;
407 target = NULL;
408 }
409 if (target) {
410 who = user_lookup_by_handle(target);
411 if (!who) {
412 partymember_printf(p, _("User '%s' not found."), target);
413 goto done;
414 }
415 }
416 else {
417 who = u;
418 if (!who) {
419 partymember_printf(p, _("Only valid users can remove hosts."));
420 goto done;
421 }
422 }
423 if (user_del_ircmask(who, host)) {
424 partymember_printf(p, _("Mask '%1$s' not found for user '%2$s'."), host, who->handle);
425 }
426 else {
427 partymember_printf(p, _("Removed '%1$s' from user '%2$s'."), host, who->handle);
428 }
429
430 done:
431 if (target) free(target);
432 free(host);
433
434 return(0);
435 }
436
437 /* Syntax: chattr <user> [chan] <flags> */
438 static int party_chattr(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
439 {
440 const char *next;
441 char *who, *flags, *chan;
442 user_t *dest;
443 flags_t flagstruct;
444 char flagstr[64];
445 int n;
446
447 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
448 if (!chan || !*chan) {
449 if (who) free(who);
450 partymember_printf(p, _("Syntax: chattr <handle> [channel] <+/-flags>"));
451 return(0);
452 }
453 if (!flags || !*flags) {
454 flags = chan;
455 chan = NULL;
456 }
457 dest = user_lookup_by_handle(who);
458 if (dest) {
459 user_set_flags_str(dest, chan, flags);
460 user_get_flags(dest, chan, &flagstruct);
461 flag_to_str(&flagstruct, flagstr);
462 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
463 }
464 else partymember_printf(p, _("'%s' is not a valid user."), who);
465 free(who);
466 free(flags);
467 if (chan) free(chan);
468 return(0);
469 }
470
471 static int party_help(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
472 {
473 return help_print_party(p, text);
474 }
475
476 static int party_addlog(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
477 {
478 putlog(LOG_MISC, "*", "%s: %s", nick, text);
479
480 return BIND_RET_LOG;
481 }
482
483 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
484 {
485 const char **modules;
486 int nummods, ctr;
487
488 nummods = module_list(&modules);
489 partymember_printf(p, _("Loaded modules:"));
490 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
491 free(modules);
492
493 return BIND_RET_LOG;
494 }
495
496 static int party_loadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
497 {
498 if (!text || !*text) {
499 partymember_printf(p, _("Syntax: loadmod <module name>"));
500 return BIND_RET_BREAK;
501 }
502 switch (module_load(text)) {
503 case 0:
504 partymember_printf(p, _("Module '%s' loaded successfully."), text);
505 break;
506 case -1:
507 partymember_printf(p, _("Module '%s' is already loaded."), text);
508 break;
509 case -2:
510 partymember_printf(p, _("Module '%s' could not be loaded."), text);
511 break;
512 case -3:
513 partymember_printf(p, _("Module '%s' does not have a valid initialization function. Perhaps it is not an eggdrop module?"), text);
514 break;
515 }
516 return BIND_RET_LOG;
517 }
518
519 static int party_unloadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
520 {
521 if (!text || !*text) {
522 partymember_printf(p, _("Syntax: unloadmod <module name>"));
523 return BIND_RET_BREAK;
524 }
525 switch (module_unload(text, MODULE_USER)) {
526 case 0:
527 partymember_printf(p, _("Module '%s' unloaded successfully."), text);
528 break;
529 case -1:
530 partymember_printf(p, _("Module '%s' is not loaded."), text);
531 break;
532 case -2:
533 partymember_printf(p, _("Module '%s' has dependencies that are still loaded. You must unload them first."), text);
534 break;
535 case -3:
536 partymember_printf(p, _("Module '%s' refuses to be unloaded by you!"), text);
537 break;
538 }
539 return BIND_RET_LOG;
540 }
541
542 static int party_binds(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
543 {
544 bind_table_t *table;
545 bind_entry_t *entry;
546 char flags[64];
547
548 partymember_printf(p, "%-16s %-16s %-16s %-10s %-5s %s", _("TABLE"), _("SYNTAX"),
549 _("FUNCTION"), _("MASK"), _("FLAGS"), _("HITS"));
550 for (table = bind_table_list(); table; table = table->next) {
551 for (entry = table->entries; entry; entry = entry->next) {
552 flag_to_str(&entry->user_flags, flags);
553 partymember_printf(p, "%-16s %-16s %-16s %-10s %-5s %i", table->name, table->syntax,
554 entry->function_name, entry->mask, flags, entry->nhits);
555 }
556 }
557
558 return BIND_RET_LOG;
559 }
560
561 static int party_restart(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
562 {
563 core_restart (nick);
564
565 return BIND_RET_LOG;
566 }
567
568 static bind_list_t core_party_binds[] = {
569 {NULL, "join", party_join}, /* DDD */
570 {NULL, "whisper", party_whisper}, /* DDD */
571 {NULL, "newpass", party_newpass}, /* DDD */
572 {NULL, "help", party_help}, /* DDC */
573 {NULL, "part", party_part}, /* DDD */
574 {NULL, "quit", party_quit}, /* DDD */
575 {NULL, "who", party_who}, /* DDD */
576 {NULL, "whois", party_whois}, /* DDD */
577 {"n", "addlog", party_addlog}, /* DDD */
578 {"n", "get", party_get}, /* DDC */
579 {"n", "set", party_set}, /* DDC */
580 {"n", "unset", party_unset}, /* DDC */
581 {"n", "status", party_status}, /* DDC */
582 {"n", "save", party_save}, /* DDD */
583 {"n", "die", party_die}, /* DDD */
584 {"n", "restart", party_restart}, /* DDD */
585 {"n", "+user", party_plus_user}, /* DDC */
586 {"n", "-user", party_minus_user}, /* DDC */
587 {"n", "chattr", party_chattr}, /* DDC */
588 {"n", "modules", party_modules}, /* DDD */
589 {"n", "loadmod", party_loadmod}, /* DDD */
590 {"n", "unloadmod", party_unloadmod}, /* DDD */
591 {"n", "binds", party_binds}, /* DDD */
592 {"m", "+host", party_plus_host}, /* DDC */
593 {"m", "-host", party_minus_host}, /* DDC */
594 {0}
595 };
596
597 void core_party_init(void)
598 {
599 bind_add_list("party", core_party_binds);
600 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23