/[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.25 - (show annotations) (download) (as text)
Tue Jan 20 22:47:56 2004 UTC (15 years, 9 months ago) by stdarg
Branch: MAIN
Changes since 1.24: +72 -3 lines
File MIME type: text/x-chdr
* Added +/-host

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.24 2004/01/11 12:16:08 wcc 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 extern char pid_file[];
44
45 static int party_join(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
46 {
47 if (!text || !*text) {
48 partymember_printf(p, _("Syntax: join <channel>"));
49 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 if (!text || !*text) chan = partychan_get_default(p);
60 else chan = partychan_lookup_name(text);
61 partychan_part(chan, p, "parting");
62 return(0);
63 }
64
65 static int party_quit(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
66 {
67 partymember_printf(p, "Goodbye!");
68 if (!text || !*text) partymember_delete(p, "Quit");
69 else partymember_delete(p, text);
70 return(0);
71 }
72
73 static int party_whisper(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
74 {
75 char *who;
76 const char *next;
77 partymember_t *dest;
78
79 egg_get_arg(text, &next, &who);
80 if (!who || !next || !*who || !*next) {
81 partymember_printf(p, _("Syntax: whisper <partylineuser> <msg>"));
82 goto done;
83 }
84
85 dest = partymember_lookup_nick(who);
86 if (!dest) {
87 partymember_printf(p, _("No such user '%s'."), who);
88 goto done;
89 }
90
91 partymember_msg(dest, p, next, -1);
92 done:
93 if (who) free(who);
94 return(0);
95 }
96
97 static void *lookup_setting(partymember_t *p, const char *path)
98 {
99 void *root;
100
101 root = config_get_root("eggdrop");
102 root = config_exists(root, path, 0, NULL);
103 if (!root) partymember_printf(p, _("That setting does not exist."));
104 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 partymember_printf(p, _("Syntax: get <path>"));
114 return(0);
115 }
116
117 root = lookup_setting(p, text);
118 if (!root) return(0);
119
120 config_get_str(&str, root, NULL);
121 if (str) partymember_printf(p, "Current value: '%s'", str);
122 else partymember_printf(p, _("Current value: null (unset)"));
123 return(0);
124 }
125
126 static int party_set(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
127 {
128 void *root;
129 char *path, *str;
130 const char *next;
131
132 egg_get_arg(text, &next, &path);
133 if (!path) {
134 partymember_printf(p, _("Syntax: set <path> [new value]"));
135 return(0);
136 }
137 if (!next) {
138 free(path);
139 return party_get(p, nick, u, cmd, text);
140 }
141
142 root = lookup_setting(p, path);
143 free(path);
144 if (!root) return(0);
145
146 config_get_str(&str, root, NULL);
147 partymember_printf(p, _("Old value: '%s'"), str);
148 config_set_str(next, root, NULL);
149 config_get_str(&str, root, NULL);
150 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 root = lookup_setting(p, text);
166 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 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 partymember_printf(p, _("I am %1$s, running Eggdrop %2$s."), core_config.botname, VERSION);
185 partymember_printf(p, _("Owner: %s"), core_config.owner);
186 if (core_config.admin) partymember_printf(p, _("Admin: %s"), core_config.admin);
187 #ifdef HAVE_UNAME
188 if (!uname(&un)) partymember_printf(p, _("OS: %1$s %2$s"), un.sysname, un.release);
189 #endif
190 partymember_printf(p, _("Help path: %s (%d help entries loaded)"), core_config.help_path, help_count());
191 partymember_printf(p, "");
192 check_bind_status(p, text);
193 return(0);
194 }
195
196 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
197 {
198 putlog(LOG_MISC, "*", _("Saving user file..."));
199 user_save(core_config.userfile);
200 putlog(LOG_MISC, "*", _("Saving config file..."));
201 core_config_save();
202 return(1);
203 }
204
205 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 partymember_printf(p, _("Please use at least 6 characters."));
209 return(0);
210 }
211 user_set_pass(p->user, text);
212 partymember_printf(p, _("Changed password to '%s'."), text);
213 return(0);
214 }
215
216 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 int *pids, len, i, width = 0;
225
226 partymember_printf(p, _("Partyline members:"));
227 partymember_who(&pids, &len);
228 qsort(pids, len, sizeof(int), intsorter);
229 if (len > 0) {
230 i = pids[len-1];
231 if (!i) i++;
232 while (i != 0) {
233 i /= 10;
234 width++;
235 }
236 }
237 for (i = 0; i < len; i++) {
238 who = partymember_lookup_pid(pids[i]);
239 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
240 }
241 free(pids);
242 return(0);
243 }
244
245 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 if ((setting = strchr(item, '.'))) {
278 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 static int party_die(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
304 {
305 putlog(LOG_MISC, "*", _("Saving user file..."));
306 user_save(core_config.userfile);
307 if (text && *text) putlog(LOG_MISC, "*", _("Bot shutting down: %s"), text);
308 else putlog(LOG_MISC, "*", _("Bot shutting down."));
309 flushlogs();
310 unlink(pid_file);
311 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 partymember_printf(p, _("Syntax: +user <handle>"));
320 return(0);
321 }
322 if (user_lookup_by_handle(text)) {
323 partymember_printf(p, _("User '%s' already exists!"));
324 return(0);
325 }
326 newuser = user_new(text);
327 if (newuser) partymember_printf(p, _("User '%s' created."), text);
328 else partymember_printf(p, _("Could not create user '%s'."), text);
329 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 partymember_printf(p, _("Syntax: -user <handle>"));
338 return(0);
339 }
340 who = user_lookup_by_handle(text);
341 if (!who) partymember_printf(p, _("User '%s' not found."));
342 else {
343 partymember_printf(p, _("Deleting user '%s'."), who->handle);
344 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 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 return(0);
385 }
386
387 static int party_minus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
388 {
389 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 return(0);
427 }
428
429 /* Syntax: chattr <user> [chan] <flags> */
430 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 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
440 if (!chan || !*chan) {
441 if (who) free(who);
442 partymember_printf(p, _("Syntax: chattr <handle> [channel] <+/-flags>"));
443 return(0);
444 }
445 if (!flags || !*flags) {
446 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 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
455 }
456 else partymember_printf(p, _("'%s' is not a valid user."), who);
457 free(who);
458 free(flags);
459 if (chan) free(chan);
460 return(0);
461 }
462
463 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
464 {
465 const char **modules;
466 int nummods, ctr;
467
468 nummods = module_list(&modules);
469 partymember_printf(p, _("Loaded modules:"));
470 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
471 free(modules);
472 return(0);
473 }
474
475 static int party_help(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
476 {
477 help_t *h;
478
479 if (!text || !*text) {
480 partymember_printf(p, _("Syntax: help <section>"));
481 return(0);
482 }
483
484 h = help_lookup_by_name(text);
485 if (!h) {
486 partymember_printf(p, _("No help found for '%s'."), text);
487 return(0);
488 }
489
490 help_print_party(p, h);
491 return(0);
492 }
493
494 static bind_list_t core_party_binds[] = {
495 {NULL, "join", party_join},
496 {NULL, "whisper", party_whisper},
497 {NULL, "newpass", party_newpass},
498 {NULL, "help", party_help},
499 {NULL, "part", party_part},
500 {NULL, "quit", party_quit},
501 {NULL, "who", party_who},
502 {NULL, "whois", party_whois},
503 {"n", "get", party_get},
504 {"n", "set", party_set},
505 {"n", "unset", party_unset},
506 {"n", "status", party_status},
507 {"n", "save", party_save},
508 {"n", "die", party_die},
509 {"n", "+user", party_plus_user},
510 {"n", "-user", party_minus_user},
511 {"n", "chattr", party_chattr},
512 {"n", "modules", party_modules},
513 {"m", "+host", party_plus_host},
514 {"m", "-host", party_minus_host},
515 {0}
516 };
517
518 void core_party_init()
519 {
520 bind_add_list("party", core_party_binds);
521 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23