Debian Codegraph @

modules/gameserv/lottery.c

raw
1 /*
2 * Copyright (c) 2005-2006 William Pitcock <[email protected]> et al
3 * Rights to this code are documented in doc/LICENSE.
4 *
5 * Choose a random user on the channel.
6 */
7
8 #include "atheme.h"
9 #include "gameserv_common.h"
10
11 DECLARE_MODULE_V1
12 (
13 "gameserv/lottery", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void command_lottery(sourceinfo_t *si, int parc, char *parv[]);
19
20 command_t cmd_lottery = { "LOTTERY", N_("Choose a random user on a channel."), AC_NONE, 2, command_lottery, { .path = "gameserv/lottery" } };
21
22 void _modinit(module_t * m)
23 {
24 service_named_bind_command("gameserv", &cmd_lottery);
25
26 service_named_bind_command("chanserv", &cmd_lottery);
27 }
28
29 void _moddeinit(module_unload_intent_t intent)
30 {
31 service_named_unbind_command("gameserv", &cmd_lottery);
32
33 service_named_unbind_command("chanserv", &cmd_lottery);
34 }
35
36 static user_t *pick_a_sucker(channel_t *c)
37 {
38 int slot = arc4random() % MOWGLI_LIST_LENGTH(&c->members);
39 chanuser_t *cu;
40
41 cu = (chanuser_t *) mowgli_node_nth_data(&c->members, slot);
42 while (cu != NULL && is_internal_client(cu->user))
43 {
44 slot = arc4random() % MOWGLI_LIST_LENGTH(&c->members);
45 cu = (chanuser_t *) mowgli_node_nth_data(&c->members, slot);
46 }
47
48 return cu != NULL ? cu->user : NULL;
49 }
50
51 static void command_lottery(sourceinfo_t *si, int parc, char *parv[])
52 {
53 mychan_t *mc;
54 user_t *u;
55
56 if (!gs_do_parameters(si, &parc, &parv, &mc))
57 return;
58
59 if (si->c == NULL)
60 {
61 command_fail(si, fault_nosuch_target, _("This command may only be used on a channel."));
62 return;
63 }
64
65 u = pick_a_sucker(si->c);
66 return_if_fail(u != NULL);
67
68 gs_command_report(si, "%s", u->nick);
69 }
70
71 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
72 * vim:ts=8
73 * vim:sw=8
74 * vim:noexpandtab
75 */