]> jfr.im git - solanum.git/blob - extensions/helpops.c
chmode: Get elevated access for op-only queries
[solanum.git] / extensions / helpops.c
1 /*
2 * Helpops system.
3 * -- kaniini
4 */
5
6 #include "stdinc.h"
7 #include "modules.h"
8 #include "client.h"
9 #include "hook.h"
10 #include "ircd.h"
11 #include "send.h"
12 #include "s_conf.h"
13 #include "s_user.h"
14 #include "s_newconf.h"
15 #include "numeric.h"
16
17 static const char helpops_desc[] = "The helpops system as used by freenode";
18
19 static rb_dlink_list helper_list = { NULL, NULL, 0 };
20 static void h_hdl_stats_request(hook_data_int *hdata);
21 static void h_hdl_new_remote_user(struct Client *client_p);
22 static void h_hdl_client_exit(hook_data_client_exit *hdata);
23 static void h_hdl_umode_changed(hook_data_umode_changed *hdata);
24 static void h_hdl_whois(hook_data_client *hdata);
25 static void recurse_client_exit(struct Client *client_p);
26 static void helper_add(struct Client *client_p);
27 static void helper_delete(struct Client *client_p);
28 static void mo_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
29 static void me_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
30 static void do_dehelper(struct Client *source_p, struct Client *target_p);
31
32 mapi_hfn_list_av1 helpops_hfnlist[] = {
33 { "doing_stats", (hookfn) h_hdl_stats_request },
34 { "new_remote_user", (hookfn) h_hdl_new_remote_user },
35 { "client_exit", (hookfn) h_hdl_client_exit },
36 { "umode_changed", (hookfn) h_hdl_umode_changed },
37 { "doing_whois", (hookfn) h_hdl_whois },
38 { "doing_whois_global", (hookfn) h_hdl_whois },
39 { NULL, NULL }
40 };
41
42 #define UMODECHAR_HELPOPS 'h'
43
44 struct Message dehelper_msgtab = {
45 "DEHELPER", 0, 0, 0, 0,
46 {mg_unreg, mg_not_oper, mg_not_oper, mg_ignore, {me_dehelper, 2}, {mo_dehelper, 2}}
47 };
48
49 mapi_clist_av1 helpops_clist[] = { &dehelper_msgtab, NULL };
50
51 static void
52 mo_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
53 {
54 struct Client *target_p;
55
56 if (!HasPrivilege(source_p, "oper:dehelper"))
57 {
58 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "dehelper");
59 return;
60 }
61
62 if(!(target_p = find_named_person(parv[1])))
63 {
64 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), parv[1]);
65 return;
66 }
67
68 if(MyClient(target_p))
69 do_dehelper(source_p, target_p);
70 else
71 sendto_one(target_p, ":%s ENCAP %s DEHELPER %s",
72 use_id(source_p), target_p->servptr->name, use_id(target_p));
73 }
74
75 static void
76 me_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
77 {
78 struct Client *target_p = find_person(parv[1]);
79 if(!target_p)
80 {
81 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), parv[1]);
82 return;
83 }
84 if(!MyClient(target_p))
85 return;
86
87 do_dehelper(source_p, target_p);
88 }
89
90 static void
91 do_dehelper(struct Client *source_p, struct Client *target_p)
92 {
93 const char *fakeparv[4];
94 static const char minus_helpops[3] = {'-', UMODECHAR_HELPOPS, '\0'};
95
96 if(!(target_p->umodes & user_modes[UMODECHAR_HELPOPS]))
97 return;
98
99 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using DEHELPER on %s",
100 source_p->name, target_p->name);
101 sendto_one_notice(target_p, ":*** %s is using DEHELPER on you", source_p->name);
102
103 fakeparv[0] = fakeparv[1] = target_p->name;
104 fakeparv[2] = minus_helpops;
105 fakeparv[3] = NULL;
106 user_mode(target_p, target_p, 3, fakeparv);
107 }
108
109 static int
110 _modinit(void)
111 {
112 rb_dlink_node *ptr;
113
114 user_modes[UMODECHAR_HELPOPS] = find_umode_slot();
115 construct_umodebuf();
116
117 RB_DLINK_FOREACH (ptr, global_client_list.head)
118 {
119 struct Client *client_p = ptr->data;
120 if (IsPerson(client_p) && (client_p->umodes & user_modes[UMODECHAR_HELPOPS]))
121 helper_add(client_p);
122 }
123
124 return 0;
125 }
126
127 static void
128 _moddeinit(void)
129 {
130 rb_dlink_node *n, *tn;
131
132 user_modes[UMODECHAR_HELPOPS] = 0;
133 construct_umodebuf();
134
135 RB_DLINK_FOREACH_SAFE(n, tn, helper_list.head)
136 rb_dlinkDestroy(n, &helper_list);
137 }
138
139 static void
140 h_hdl_stats_request(hook_data_int *hdata)
141 {
142 struct Client *target_p;
143 rb_dlink_node *helper_ptr;
144 unsigned int count = 0;
145
146 if (hdata->arg2 != 'p')
147 return;
148
149 RB_DLINK_FOREACH (helper_ptr, helper_list.head)
150 {
151 target_p = helper_ptr->data;
152
153 if(target_p->user->away)
154 continue;
155
156 count++;
157
158 sendto_one_numeric(hdata->client, RPL_STATSDEBUG,
159 "p :%s (%s@%s)",
160 target_p->name, target_p->username,
161 target_p->host);
162 }
163
164 sendto_one_numeric(hdata->client, RPL_STATSDEBUG,
165 "p :%u staff members", count);
166
167 hdata->result = 1;
168 }
169
170 static void
171 helper_add(struct Client *client_p)
172 {
173 if (rb_dlinkFind(client_p, &helper_list) != NULL)
174 return;
175
176 rb_dlinkAddAlloc(client_p, &helper_list);
177 }
178
179 static void
180 helper_delete(struct Client *client_p)
181 {
182 rb_dlinkFindDestroy(client_p, &helper_list);
183 }
184
185 static void
186 h_hdl_new_remote_user(struct Client *client_p)
187 {
188 if (client_p->umodes & user_modes[UMODECHAR_HELPOPS])
189 helper_add(client_p);
190 }
191
192 static void
193 recurse_client_exit(struct Client *client_p)
194 {
195 if (IsPerson(client_p))
196 {
197 if (client_p->umodes & user_modes[UMODECHAR_HELPOPS])
198 helper_delete(client_p);
199 }
200 else if (IsServer(client_p))
201 {
202 rb_dlink_node *nptr;
203
204 RB_DLINK_FOREACH(nptr, client_p->serv->users.head)
205 recurse_client_exit(nptr->data);
206
207 RB_DLINK_FOREACH(nptr, client_p->serv->servers.head)
208 recurse_client_exit(nptr->data);
209 }
210 }
211
212 static void
213 h_hdl_client_exit(hook_data_client_exit *hdata)
214 {
215 recurse_client_exit(hdata->target);
216 }
217
218 static void
219 h_hdl_umode_changed(hook_data_umode_changed *hdata)
220 {
221 struct Client *source_p = hdata->client;
222
223 /* didn't change +h umode, we don't need to do anything */
224 bool changed = (hdata->oldumodes ^ source_p->umodes) & user_modes[UMODECHAR_HELPOPS];
225
226 if (source_p->umodes & user_modes[UMODECHAR_HELPOPS])
227 {
228 if (MyClient(source_p) && !HasPrivilege(source_p, "usermode:helpops"))
229 {
230 source_p->umodes &= ~user_modes[UMODECHAR_HELPOPS];
231 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "usermode:helpops");
232 /* they didn't ask for +h so we must be removing it */
233 if (!changed)
234 helper_delete(source_p);
235 return;
236 }
237
238 if (changed)
239 helper_add(source_p);
240 }
241 else if (changed)
242 {
243 helper_delete(source_p);
244 }
245 }
246
247 static void
248 h_hdl_whois(hook_data_client *hdata)
249 {
250 struct Client *source_p = hdata->client;
251 struct Client *target_p = hdata->target;
252
253 if ((target_p->umodes & user_modes[UMODECHAR_HELPOPS]) && EmptyString(target_p->user->away))
254 {
255 sendto_one_numeric(source_p, RPL_WHOISHELPOP, form_str(RPL_WHOISHELPOP), target_p->name);
256 }
257 }
258
259 DECLARE_MODULE_AV2(helpops, _modinit, _moddeinit, helpops_clist, NULL, helpops_hfnlist, NULL, NULL, helpops_desc);