Debian Codegraph @

modules/exttarget/chanacs.c

raw
1 /*
2 * Copyright (c) 2011 William Pitcock <[email protected]>
3 *
4 * Rights to this code are as documented in doc/LICENSE.
5 *
6 * $chanacs:#channel gives flags to users in /cs flags #channel if
7 * the user has flags in the $chanacs:#channel
8 */
9
10 #include "atheme.h"
11 #include "exttarget.h"
12
13 DECLARE_MODULE_V1
14 (
15 "exttarget/chanacs", false, _modinit, _moddeinit,
16 PACKAGE_STRING,
17 VENDOR_STRING
18 );
19
20 static mowgli_patricia_t **exttarget_tree = NULL;
21
22 typedef struct {
23 myentity_t parent;
24 stringref channel;
25 int checking;
26 } chanacs_exttarget_t;
27
28 static chanacs_t *chanacs_ext_match_user(chanacs_t *ca, user_t *u)
29 {
30 chanacs_exttarget_t *ent;
31 mychan_t *mc;
32 unsigned int flags;
33
34 ent = (chanacs_exttarget_t *) ca->entity;
35
36 if (ent->checking > 4) /* arbitrary recursion limit? */
37 return NULL;
38
39 if (!(mc = mychan_find(ent->channel)))
40 return NULL;
41
42 ent->checking++;
43 flags = chanacs_user_flags(mc, u);
44 ent->checking--;
45
46 if (flags & CA_AKICK)
47 return NULL;
48
49 if (flags)
50 return ca;
51
52 return NULL;
53 }
54
55 static chanacs_t *chanacs_ext_match_entity(chanacs_t *ca, myentity_t *mt)
56 {
57 if (ca->entity == mt)
58 return ca;
59
60 return NULL;
61 }
62
63 static bool chanacs_ext_can_register_channel(myentity_t *mt)
64 {
65 return false;
66 }
67
68 static bool chanacs_allow_foundership(myentity_t *mt)
69 {
70 return false;
71 }
72
73 static entity_chanacs_validation_vtable_t chanacs_ext_validate = {
74 .match_entity = chanacs_ext_match_entity,
75 .match_user = chanacs_ext_match_user,
76 .can_register_channel = chanacs_ext_can_register_channel,
77 .allow_foundership = chanacs_allow_foundership,
78 };
79
80 static mowgli_heap_t *chanacs_ext_heap = NULL;
81 static mowgli_patricia_t *chanacs_exttarget_tree = NULL;
82
83 static void chanacs_ext_delete(chanacs_exttarget_t *e)
84 {
85 return_if_fail(e != NULL);
86
87 mowgli_patricia_delete(chanacs_exttarget_tree, e->channel);
88 strshare_unref(e->channel);
89 strshare_unref(entity(e)->name);
90
91 mowgli_heap_free(chanacs_ext_heap, e);
92 }
93
94 static myentity_t *chanacs_validate_f(const char *param)
95 {
96 char *name;
97 chanacs_exttarget_t *ext;
98 size_t namelen;
99
100 if (param == NULL)
101 return NULL;
102
103 if (*param == '\0')
104 return NULL;
105
106 /* if we already have an object, return it from our tree. */
107 if ((ext = mowgli_patricia_retrieve(chanacs_exttarget_tree, param)) != NULL)
108 return entity(ext);
109
110 ext = mowgli_heap_alloc(chanacs_ext_heap);
111 ext->channel = strshare_get(param);
112 ext->checking = 0;
113
114 /* name the entity... $chanacs:param */
115 #define NAMEPREFIX "$chanacs:"
116 namelen = sizeof NAMEPREFIX + strlen(param);
117
118 name = smalloc(namelen);
119 memcpy(name, NAMEPREFIX, sizeof NAMEPREFIX - 1);
120 memcpy(name + sizeof NAMEPREFIX - 1, param, namelen - sizeof NAMEPREFIX + 1);
121
122 entity(ext)->name = strshare_get(name);
123 free(name);
124 #undef NAMEPREFIX
125
126 /* hook up the entity's validation table. */
127 entity(ext)->chanacs_validate = &chanacs_ext_validate;
128 entity(ext)->type = ENT_EXTTARGET;
129
130 /* initialize the object. */
131 object_init(object(ext), entity(ext)->name, (destructor_t) chanacs_ext_delete);
132
133 /* add the object to the exttarget tree */
134 mowgli_patricia_add(chanacs_exttarget_tree, ext->channel, ext);
135
136 /* return the object as initially unowned by sinking the reference count. */
137 return object_sink_ref(ext);
138 }
139
140 void _modinit(module_t *m)
141 {
142 MODULE_TRY_REQUEST_SYMBOL(m, exttarget_tree, "exttarget/main", "exttarget_tree");
143
144 mowgli_patricia_add(*exttarget_tree, "chanacs", chanacs_validate_f);
145
146 /* since we are dealing with channel names, we use irccasecanon. */
147 chanacs_exttarget_tree = mowgli_patricia_create(irccasecanon);
148 chanacs_ext_heap = mowgli_heap_create(sizeof(chanacs_exttarget_t), 32, BH_LAZY);
149 }
150
151 void _moddeinit(module_unload_intent_t intent)
152 {
153 mowgli_heap_destroy(chanacs_ext_heap);
154 mowgli_patricia_delete(*exttarget_tree, "chanacs");
155 mowgli_patricia_destroy(chanacs_exttarget_tree, NULL, NULL);
156 }