/[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.27 - (show annotations) (download) (as text)
Tue Jun 15 11:24:46 2004 UTC (15 years, 4 months ago) by wingman
Branch: MAIN
Changes since 1.26: +9 -2 lines
File MIME type: text/x-chdr
* Assertion outputs shouldn't be done via putlog
* stdin/stdout can now be used with sockbufs (just create a new sockbuf
  with socket fileno(stdout))
* Added terminal mode (--terminal/-t). Though not the best solution yet
  since telnetparty needs to be loaded but it's much easier now to
  debug/test things now.
* Added two new binds:

	init: executed after everything (core,modules,scripts) is loaded
		and right before main loop.

	shutdown: executed before shutting down the bot (currently never
		called since we'll never leave our main loop and there
		are places like party_die which calls exit() manually).

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.26 2004/02/15 05:36:56 stdarg 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
43 /* from main.c */
44 extern int term_z;
45 extern char pid_file[];
46
47 static int party_join(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
48 {
49 if (!text || !*text) {
50 partymember_printf(p, _("Syntax: join <channel>"));
51 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 if (!text || !*text) chan = partychan_get_default(p);
62 else chan = partychan_lookup_name(text);
63 partychan_part(chan, p, "parting");
64 return(0);
65 }
66
67 static int party_quit(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
68 {
69 if (term_z && 0 == strcmp (p->nick, PARTY_TERMINAL_NICK)) {
70 partymember_printf (p, "You can't leave the partyline in terminal mode.");
71 return -1;
72 }
73
74 partymember_printf(p, "Goodbye!");
75 if (!text || !*text) partymember_delete(p, "Quit");
76 else partymember_delete(p, text);
77 return(0);
78 }
79
80 static int party_whisper(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
81 {
82 char *who;
83 const char *next;
84 partymember_t *dest;
85
86 egg_get_arg(text, &next, &who);
87 if (!who || !next || !*who || !*next) {
88 partymember_printf(p, _("Syntax: whisper <partylineuser> <msg>"));
89 goto done;
90 }
91
92 dest = partymember_lookup_nick(who);
93 if (!dest) {
94 partymember_printf(p, _("No such user '%s'."), who);
95 goto done;
96 }
97
98 partymember_msg(dest, p, next, -1);
99 done:
100 if (who) free(who);
101 return(0);
102 }
103
104 static void *lookup_setting(partymember_t *p, const char *path)
105 {
106 void *root;
107
108 root = config_get_root("eggdrop");
109 root = config_exists(root, path, 0, NULL);
110 if (!root) partymember_printf(p, _("That setting does not exist."));
111 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 partymember_printf(p, _("Syntax: get <path>"));
121 return(0);
122 }
123
124 root = lookup_setting(p, text);
125 if (!root) return(0);
126
127 config_get_str(&str, root, NULL);
128 if (str) partymember_printf(p, "Current value: '%s'", str);
129 else partymember_printf(p, _("Current value: null (unset)"));
130 return(0);
131 }
132
133 static int party_set(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
134 {
135 void *root;
136 char *path, *str;
137 const char *next;
138
139 egg_get_arg(text, &next, &path);
140 if (!path) {
141 partymember_printf(p, _("Syntax: set <path> [new value]"));
142 return(0);
143 }
144 if (!next) {
145 free(path);
146 return party_get(p, nick, u, cmd, text);
147 }
148
149 root = lookup_setting(p, path);
150 free(path);
151 if (!root) return(0);
152
153 config_get_str(&str, root, NULL);
154 partymember_printf(p, _("Old value: '%s'"), str);
155 config_set_str(next, root, NULL);
156 config_get_str(&str, root, NULL);
157 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 root = lookup_setting(p, text);
173 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 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 partymember_printf(p, _("I am %1$s, running Eggdrop %2$s."), core_config.botname, VERSION);
192 partymember_printf(p, _("Owner: %s"), core_config.owner);
193 if (core_config.admin) partymember_printf(p, _("Admin: %s"), core_config.admin);
194 #ifdef HAVE_UNAME
195 if (!uname(&un)) partymember_printf(p, _("OS: %1$s %2$s"), un.sysname, un.release);
196 #endif
197 partymember_printf(p, _("Help path: %s (%d help entries loaded)"), core_config.help_path, help_count());
198 partymember_printf(p, "");
199 check_bind_status(p, text);
200 return(0);
201 }
202
203 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
204 {
205 putlog(LOG_MISC, "*", _("Saving user file..."));
206 user_save(core_config.userfile);
207 putlog(LOG_MISC, "*", _("Saving config file..."));
208 core_config_save();
209 return(1);
210 }
211
212 static int party_newpass(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
213 {
214 if (!text || strlen(text) < 6) {
215 partymember_printf(p, _("Please use at least 6 characters."));
216 return(0);
217 }
218 user_set_pass(p->user, text);
219 partymember_printf(p, _("Changed password to '%s'."), text);
220 return(0);
221 }
222
223 static int intsorter(const void *left, const void *right)
224 {
225 return(*(int *)left - *(int *)right);
226 }
227
228 static int party_who(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
229 {
230 partymember_t *who;
231 int *pids, len, i, width = 0;
232
233 partymember_printf(p, _("Partyline members:"));
234 partymember_who(&pids, &len);
235 qsort(pids, len, sizeof(int), intsorter);
236 if (len > 0) {
237 i = pids[len-1];
238 if (!i) i++;
239 while (i != 0) {
240 i /= 10;
241 width++;
242 }
243 }
244 for (i = 0; i < len; i++) {
245 who = partymember_lookup_pid(pids[i]);
246 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
247 }
248 free(pids);
249 return(0);
250 }
251
252 static int party_whois(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
253 {
254 user_t *who;
255 flags_t flags;
256 char *item, *chan, *setting, *value, flagstr[64];
257 const char *next;
258 int i;
259
260 if (text && *text) who = user_lookup_by_handle(text);
261 else who = u;
262
263 if (!who) {
264 partymember_printf(p, "No such user.");
265 return(0);
266 }
267
268 next = core_config.whois_items;
269 while (next && *next) {
270 egg_get_arg(next, &next, &item);
271 if (!strcasecmp(item, "handle")) {
272 partymember_printf(p, "%s: '%s'", item, who->handle);
273 }
274 else if (!strcasecmp(item, "uid")) {
275 partymember_printf(p, "%s: '%d'", item, who->uid);
276 }
277 else if (!strcasecmp(item, "ircmasks")) {
278 partymember_printf(p, "%s:", item);
279 for (i = 0; i < who->nircmasks; i++) {
280 partymember_printf(p, " %d. '%s'", i+1, who->ircmasks[i]);
281 }
282 }
283 else {
284 if ((setting = strchr(item, '.'))) {
285 chan = item;
286 *setting = 0;
287 setting++;
288 }
289 else {
290 chan = NULL;
291 setting = item;
292 }
293 if (!strcasecmp(setting, "flags")) {
294 user_get_flags(who, chan, &flags);
295 flag_to_str(&flags, flagstr);
296 value = flagstr;
297 }
298 else {
299 user_get_setting(who, chan, setting, &value);
300 }
301
302 if (chan) partymember_printf(p, "%s.%s: %s", chan, setting, value);
303 else partymember_printf(p, "%s: %s", setting, value);
304 }
305 free(item);
306 }
307 return(0);
308 }
309
310 static int party_die(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
311 {
312 putlog(LOG_MISC, "*", _("Saving user file..."));
313 user_save(core_config.userfile);
314 if (text && *text) putlog(LOG_MISC, "*", _("Bot shutting down: %s"), text);
315 else putlog(LOG_MISC, "*", _("Bot shutting down."));
316 flushlogs();
317 unlink(pid_file);
318 exit(0);
319 }
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 partymember_printf(p, _("Syntax: +user <handle>"));
327 return(0);
328 }
329 if (user_lookup_by_handle(text)) {
330 partymember_printf(p, _("User '%s' already exists!"));
331 return(0);
332 }
333 newuser = user_new(text);
334 if (newuser) partymember_printf(p, _("User '%s' created."), text);
335 else partymember_printf(p, _("Could not create user '%s'."), text);
336 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 partymember_printf(p, _("Syntax: -user <handle>"));
345 return(0);
346 }
347 who = user_lookup_by_handle(text);
348 if (!who) partymember_printf(p, _("User '%s' not found."));
349 else {
350 partymember_printf(p, _("Deleting user '%s'."), who->handle);
351 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 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 return(0);
392 }
393
394 static int party_minus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
395 {
396 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 return(0);
434 }
435
436 /* Syntax: chattr <user> [chan] <flags> */
437 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 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
447 if (!chan || !*chan) {
448 if (who) free(who);
449 partymember_printf(p, _("Syntax: chattr <handle> [channel] <+/-flags>"));
450 return(0);
451 }
452 if (!flags || !*flags) {
453 flags = chan;
454 chan = NULL;
455 }
456 dest = user_lookup_by_handle(who);
457 if (dest) {
458 user_set_flag_str(dest, chan, flags);
459 user_get_flags(dest, chan, &flagstruct);
460 flag_to_str(&flagstruct, flagstr);
461 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
462 }
463 else partymember_printf(p, _("'%s' is not a valid user."), who);
464 free(who);
465 free(flags);
466 if (chan) free(chan);
467 return(0);
468 }
469
470 static int party_help(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
471 {
472 help_t *h;
473
474 if (!text || !*text) {
475 partymember_printf(p, _("Syntax: help <section>"));
476 return(0);
477 }
478
479 h = help_lookup_by_name(text);
480 if (!h) {
481 partymember_printf(p, _("No help found for '%s'."), text);
482 return(0);
483 }
484
485 help_print_party(p, h);
486 return(0);
487 }
488
489 static int party_addlog(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
490 {
491 putlog(LOG_MISC, "*", "%s: %s", nick, text);
492 return(0);
493 }
494
495 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
496 {
497 const char **modules;
498 int nummods, ctr;
499
500 nummods = module_list(&modules);
501 partymember_printf(p, _("Loaded modules:"));
502 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
503 free(modules);
504 return(0);
505 }
506
507 static int party_loadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
508 {
509 if (!text || !*text) {
510 partymember_printf(p, _("Syntax: loadmod <module name>"));
511 return(0);
512 }
513 switch (module_load(text)) {
514 case 0:
515 partymember_printf(p, _("Module '%s' loaded successfully."), text);
516 break;
517 case -1:
518 partymember_printf(p, _("Module '%s' is already loaded."), text);
519 break;
520 case -2:
521 partymember_printf(p, _("Module '%s' could not be loaded."), text);
522 break;
523 case -3:
524 partymember_printf(p, _("Module '%s' does not have a valid initialization function. Perhaps it is not an eggdrop module?"), text);
525 break;
526 }
527 return(0);
528 }
529
530 static int party_unloadmod(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
531 {
532 if (!text || !*text) {
533 partymember_printf(p, _("Syntax: unloadmod <module name>"));
534 return(0);
535 }
536 switch (module_unload(text, EGGMOD_USER)) {
537 case 0:
538 partymember_printf(p, _("Module '%s' unloaded successfully."), text);
539 break;
540 case -1:
541 partymember_printf(p, _("Module '%s' is not loaded."), text);
542 break;
543 case -2:
544 partymember_printf(p, _("Module '%s' has dependencies that are still loaded. You must unload them first."), text);
545 break;
546 case -3:
547 partymember_printf(p, _("Module '%s' refuses to be unloaded by you!"), text);
548 break;
549 }
550 return(0);
551 }
552
553 static bind_list_t core_party_binds[] = {
554 {NULL, "join", party_join},
555 {NULL, "whisper", party_whisper},
556 {NULL, "newpass", party_newpass},
557 {NULL, "help", party_help},
558 {NULL, "part", party_part},
559 {NULL, "quit", party_quit},
560 {NULL, "who", party_who},
561 {NULL, "whois", party_whois},
562 {"n", "addlog", party_addlog},
563 {"n", "get", party_get},
564 {"n", "set", party_set},
565 {"n", "unset", party_unset},
566 {"n", "status", party_status},
567 {"n", "save", party_save},
568 {"n", "die", party_die},
569 {"n", "+user", party_plus_user},
570 {"n", "-user", party_minus_user},
571 {"n", "chattr", party_chattr},
572 {"n", "modules", party_modules},
573 {"n", "loadmod", party_loadmod},
574 {"n", "unloadmod", party_unloadmod},
575 {"m", "+host", party_plus_host},
576 {"m", "-host", party_minus_host},
577 {0}
578 };
579
580 void core_party_init(void)
581 {
582 bind_add_list("party", core_party_binds);
583 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23