]> jfr.im git - solanum.git/blob - extensions/helpops.c
Use const hook data where possible
[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 static int UMODE_HELPOPS = 0;
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 (!IsOperAdmin(source_p))
57 {
58 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "admin");
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
95 if(!(target_p->umodes & UMODE_HELPOPS))
96 return;
97
98 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using DEHELPER on %s",
99 source_p->name, target_p->name);
100 sendto_one_notice(target_p, ":*** %s is using DEHELPER on you", source_p->name);
101
102 fakeparv[0] = fakeparv[1] = target_p->name;
103 fakeparv[2] = "-H";
104 fakeparv[3] = NULL;
105 user_mode(target_p, target_p, 3, fakeparv);
106 }
107
108 static int
109 _modinit(void)
110 {
111 /* add the usermode to the available slot */
112 user_modes['H'] = UMODE_HELPOPS = find_umode_slot();
113 construct_umodebuf();
114
115 return 0;
116 }
117
118 static void
119 _moddeinit(void)
120 {
121 /* disable the umode and remove it from the available list */
122 user_modes['H'] = UMODE_HELPOPS = 0;
123 construct_umodebuf();
124 }
125
126 static void
127 h_hdl_stats_request(hook_data_int *hdata)
128 {
129 struct Client *target_p;
130 rb_dlink_node *helper_ptr;
131 unsigned int count = 0;
132
133 if (hdata->arg2 != 'p')
134 return;
135
136 RB_DLINK_FOREACH (helper_ptr, helper_list.head)
137 {
138 target_p = helper_ptr->data;
139
140 if(target_p->user->away)
141 continue;
142
143 count++;
144
145 sendto_one_numeric(hdata->client, RPL_STATSDEBUG,
146 "p :%s (%s@%s)",
147 target_p->name, target_p->username,
148 target_p->host);
149 }
150
151 sendto_one_numeric(hdata->client, RPL_STATSDEBUG,
152 "p :%u staff members", count);
153
154 hdata->result = 1;
155 }
156
157 static void
158 helper_add(struct Client *client_p)
159 {
160 if (rb_dlinkFind(client_p, &helper_list) != NULL)
161 return;
162
163 rb_dlinkAddAlloc(client_p, &helper_list);
164 }
165
166 static void
167 helper_delete(struct Client *client_p)
168 {
169 rb_dlinkFindDestroy(client_p, &helper_list);
170 }
171
172 static void
173 h_hdl_new_remote_user(struct Client *client_p)
174 {
175 if (client_p->umodes & UMODE_HELPOPS)
176 helper_add(client_p);
177 }
178
179 static void
180 recurse_client_exit(struct Client *client_p)
181 {
182 if (IsPerson(client_p))
183 {
184 if (client_p->umodes & UMODE_HELPOPS)
185 helper_delete(client_p);
186 }
187 else if (IsServer(client_p))
188 {
189 rb_dlink_node *nptr;
190
191 RB_DLINK_FOREACH(nptr, client_p->serv->users.head)
192 recurse_client_exit(nptr->data);
193
194 RB_DLINK_FOREACH(nptr, client_p->serv->servers.head)
195 recurse_client_exit(nptr->data);
196 }
197 }
198
199 static void
200 h_hdl_client_exit(hook_data_client_exit *hdata)
201 {
202 recurse_client_exit(hdata->target);
203 }
204
205 static void
206 h_hdl_umode_changed(hook_data_umode_changed *hdata)
207 {
208 struct Client *source_p = hdata->client;
209
210 /* didn't change +H umode, we don't need to do anything */
211 if (!((hdata->oldumodes ^ source_p->umodes) & UMODE_HELPOPS))
212 return;
213
214 if (source_p->umodes & UMODE_HELPOPS)
215 {
216 if (MyClient(source_p) && !HasPrivilege(source_p, "usermode:helpops"))
217 {
218 source_p->umodes &= ~UMODE_HELPOPS;
219 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "usermode:helpops");
220 return;
221 }
222
223 helper_add(source_p);
224 }
225 else if (!(source_p->umodes & UMODE_HELPOPS))
226 helper_delete(source_p);
227 }
228
229 static void
230 h_hdl_whois(hook_data_client *hdata)
231 {
232 struct Client *source_p = hdata->client;
233 struct Client *target_p = hdata->target;
234
235 if ((target_p->umodes & UMODE_HELPOPS) && EmptyString(target_p->user->away))
236 {
237 sendto_one_numeric(source_p, RPL_WHOISHELPOP, form_str(RPL_WHOISHELPOP), target_p->name);
238 }
239 }
240
241 DECLARE_MODULE_AV2(helpops, _modinit, _moddeinit, helpops_clist, NULL, helpops_hfnlist, NULL, NULL, helpops_desc);