Debian Codegraph @

modules/exttarget/channel.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
7 #include "atheme.h"
8 #include "exttarget.h"
9
10 DECLARE_MODULE_V1
11 (
12 "exttarget/channel", false, _modinit, _moddeinit,
13 PACKAGE_STRING,
14 VENDOR_STRING
15 );
16
17 static mowgli_patricia_t **exttarget_tree = NULL;
18
19 typedef struct {
20 myentity_t parent;
21 stringref channel;
22 } channel_exttarget_t;
23
24 static chanacs_t *channel_ext_match_user(chanacs_t *ca, user_t *u)
25 {
26 channel_exttarget_t *ent;
27 mowgli_node_t *n;
28
29 ent = (channel_exttarget_t *) ca->entity;
30 MOWGLI_LIST_FOREACH(n, u->channels.head)
31 {
32 chanuser_t *cu = n->data;
33
34 if (!irccasecmp(cu->chan->name, ent->channel))
35 return ca;
36 }
37
38 return NULL;
39 }
40
41 static chanacs_t *channel_ext_match_entity(chanacs_t *ca, myentity_t *mt)
42 {
43 if (ca->entity == mt)
44 return ca;
45
46 return NULL;
47 }
48
49 static bool channel_ext_can_register_channel(myentity_t *mt)
50 {
51 return false;
52 }
53
54 static bool channel_ext_allow_foundership(myentity_t *mt)
55 {
56 return false;
57 }
58
59 static entity_chanacs_validation_vtable_t channel_ext_validate = {
60 .match_entity = channel_ext_match_entity,
61 .match_user = channel_ext_match_user,
62 .can_register_channel = channel_ext_can_register_channel,
63 .allow_foundership = channel_ext_allow_foundership,
64 };
65
66 static mowgli_heap_t *channel_ext_heap = NULL;
67 static mowgli_patricia_t *channel_exttarget_tree = NULL;
68
69 static void channel_ext_delete(channel_exttarget_t *e)
70 {
71 return_if_fail(e != NULL);
72
73 mowgli_patricia_delete(channel_exttarget_tree, e->channel);
74 strshare_unref(e->channel);
75 strshare_unref(entity(e)->name);
76
77 mowgli_heap_free(channel_ext_heap, e);
78 }
79
80 static myentity_t *channel_validate_f(const char *param)
81 {
82 char *name;
83 channel_exttarget_t *ext;
84 size_t namelen;
85
86 if (param == NULL)
87 return NULL;
88
89 if (*param == '\0')
90 return NULL;
91
92 /* if we already have an object, return it from our tree. */
93 if ((ext = mowgli_patricia_retrieve(channel_exttarget_tree, param)) != NULL)
94 return entity(ext);
95
96 ext = mowgli_heap_alloc(channel_ext_heap);
97 ext->channel = strshare_get(param);
98
99 /* name the entity... $channel:param */
100 #define NAMEPREFIX "$channel:"
101 namelen = sizeof NAMEPREFIX + strlen(param);
102
103 name = smalloc(namelen);
104 memcpy(name, NAMEPREFIX, sizeof NAMEPREFIX - 1);
105 memcpy(name + sizeof NAMEPREFIX - 1, param, namelen - sizeof NAMEPREFIX + 1);
106
107 entity(ext)->name = strshare_get(name);
108 free(name);
109 #undef NAMEPREFIX
110
111 /* hook up the entity's validation table. */
112 entity(ext)->chanacs_validate = &channel_ext_validate;
113 entity(ext)->type = ENT_EXTTARGET;
114
115 /* initialize the object. */
116 object_init(object(ext), entity(ext)->name, (destructor_t) channel_ext_delete);
117
118 /* add the object to the exttarget tree. */
119 mowgli_patricia_add(channel_exttarget_tree, ext->channel, ext);
120
121 /* return the object as initially unowned by sinking the reference count. */
122 return object_sink_ref(ext);
123 }
124
125 void _modinit(module_t *m)
126 {
127 MODULE_TRY_REQUEST_SYMBOL(m, exttarget_tree, "exttarget/main", "exttarget_tree");
128
129 mowgli_patricia_add(*exttarget_tree, "channel", channel_validate_f);
130
131 /* since we are dealing with channel names, we use irccasecanon. */
132 channel_exttarget_tree = mowgli_patricia_create(irccasecanon);
133 channel_ext_heap = mowgli_heap_create(sizeof(channel_exttarget_t), 32, BH_LAZY);
134 }
135
136 void _moddeinit(module_unload_intent_t intent)
137 {
138 mowgli_heap_destroy(channel_ext_heap);
139 mowgli_patricia_delete(*exttarget_tree, "channel");
140 mowgli_patricia_destroy(channel_exttarget_tree, NULL, NULL);
141 }