]> jfr.im git - solanum.git/blame - extensions/helpops.c
helpops: fix umode handling
[solanum.git] / extensions / helpops.c
CommitLineData
4d21f1e8
AC
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
eeabf33a
EM
17static const char helpops_desc[] = "The helpops system as used by freenode";
18
4d21f1e8
AC
19static rb_dlink_list helper_list = { NULL, NULL, 0 };
20static void h_hdl_stats_request(hook_data_int *hdata);
21static void h_hdl_new_remote_user(struct Client *client_p);
22static void h_hdl_client_exit(hook_data_client_exit *hdata);
23static void h_hdl_umode_changed(hook_data_umode_changed *hdata);
24static void h_hdl_whois(hook_data_client *hdata);
4d3f8ead 25static void recurse_client_exit(struct Client *client_p);
b1c32af9
AC
26static void helper_add(struct Client *client_p);
27static void helper_delete(struct Client *client_p);
3c7d6fcc
EM
28static void mo_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
29static void me_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
30static void do_dehelper(struct Client *source_p, struct Client *target_p);
4d21f1e8
AC
31
32mapi_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
6c639159 42#define UMODECHAR_HELPOPS 'H'
4d21f1e8 43
161ac1c8 44struct Message dehelper_msgtab = {
7baa37a9 45 "DEHELPER", 0, 0, 0, 0,
161ac1c8
AC
46 {mg_unreg, mg_not_oper, mg_not_oper, mg_ignore, {me_dehelper, 2}, {mo_dehelper, 2}}
47};
48
49mapi_clist_av1 helpops_clist[] = { &dehelper_msgtab, NULL };
50
3c7d6fcc
EM
51static void
52mo_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
161ac1c8
AC
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");
3c7d6fcc 59 return;
161ac1c8
AC
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]);
3c7d6fcc 65 return;
161ac1c8
AC
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));
161ac1c8
AC
73}
74
3c7d6fcc
EM
75static void
76me_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
161ac1c8
AC
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]);
3c7d6fcc 82 return;
161ac1c8
AC
83 }
84 if(!MyClient(target_p))
3c7d6fcc 85 return;
161ac1c8
AC
86
87 do_dehelper(source_p, target_p);
161ac1c8
AC
88}
89
3c7d6fcc
EM
90static void
91do_dehelper(struct Client *source_p, struct Client *target_p)
161ac1c8
AC
92{
93 const char *fakeparv[4];
94
6c639159 95 if(!(target_p->umodes & user_modes[UMODECHAR_HELPOPS]))
3c7d6fcc 96 return;
161ac1c8
AC
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);
161ac1c8
AC
106}
107
4d21f1e8
AC
108static int
109_modinit(void)
110{
6c639159 111 user_modes[UMODECHAR_HELPOPS] = find_umode_slot();
4d21f1e8
AC
112 construct_umodebuf();
113
114 return 0;
115}
116
117static void
118_moddeinit(void)
119{
6c639159 120 user_modes[UMODECHAR_HELPOPS] = 0;
4d21f1e8
AC
121 construct_umodebuf();
122}
123
124static void
125h_hdl_stats_request(hook_data_int *hdata)
126{
127 struct Client *target_p;
128 rb_dlink_node *helper_ptr;
129 unsigned int count = 0;
130
131 if (hdata->arg2 != 'p')
132 return;
133
134 RB_DLINK_FOREACH (helper_ptr, helper_list.head)
135 {
136 target_p = helper_ptr->data;
137
4d21f1e8
AC
138 if(target_p->user->away)
139 continue;
140
141 count++;
142
143 sendto_one_numeric(hdata->client, RPL_STATSDEBUG,
144 "p :%s (%s@%s)",
145 target_p->name, target_p->username,
146 target_p->host);
147 }
148
149 sendto_one_numeric(hdata->client, RPL_STATSDEBUG,
150 "p :%u staff members", count);
151
152 hdata->result = 1;
153}
154
b1c32af9
AC
155static void
156helper_add(struct Client *client_p)
157{
158 if (rb_dlinkFind(client_p, &helper_list) != NULL)
159 return;
160
161 rb_dlinkAddAlloc(client_p, &helper_list);
162}
163
164static void
165helper_delete(struct Client *client_p)
166{
167 rb_dlinkFindDestroy(client_p, &helper_list);
168}
169
4d21f1e8
AC
170static void
171h_hdl_new_remote_user(struct Client *client_p)
172{
6c639159 173 if (client_p->umodes & user_modes[UMODECHAR_HELPOPS])
b1c32af9 174 helper_add(client_p);
4d21f1e8
AC
175}
176
177static void
4d3f8ead 178recurse_client_exit(struct Client *client_p)
4d21f1e8 179{
4d3f8ead 180 if (IsPerson(client_p))
8093dc5f 181 {
6c639159 182 if (client_p->umodes & user_modes[UMODECHAR_HELPOPS])
b1c32af9 183 helper_delete(client_p);
8093dc5f 184 }
4d3f8ead 185 else if (IsServer(client_p))
8093dc5f
AC
186 {
187 rb_dlink_node *nptr;
188
4d3f8ead
AC
189 RB_DLINK_FOREACH(nptr, client_p->serv->users.head)
190 recurse_client_exit(nptr->data);
8093dc5f 191
4d3f8ead
AC
192 RB_DLINK_FOREACH(nptr, client_p->serv->servers.head)
193 recurse_client_exit(nptr->data);
8093dc5f 194 }
4d21f1e8
AC
195}
196
4d3f8ead
AC
197static void
198h_hdl_client_exit(hook_data_client_exit *hdata)
199{
200 recurse_client_exit(hdata->target);
201}
202
4d21f1e8
AC
203static void
204h_hdl_umode_changed(hook_data_umode_changed *hdata)
205{
206 struct Client *source_p = hdata->client;
207
208 /* didn't change +H umode, we don't need to do anything */
6c639159 209 if (!((hdata->oldumodes ^ source_p->umodes) & user_modes[UMODECHAR_HELPOPS]))
4d21f1e8
AC
210 return;
211
6c639159 212 if (source_p->umodes & user_modes[UMODECHAR_HELPOPS])
4d21f1e8
AC
213 {
214 if (MyClient(source_p) && !HasPrivilege(source_p, "usermode:helpops"))
215 {
6c639159 216 source_p->umodes &= ~user_modes[UMODECHAR_HELPOPS];
4d21f1e8
AC
217 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "usermode:helpops");
218 return;
219 }
220
b1c32af9 221 helper_add(source_p);
4d21f1e8 222 }
6c639159 223 else if (!(source_p->umodes & user_modes[UMODECHAR_HELPOPS]))
b1c32af9 224 helper_delete(source_p);
4d21f1e8
AC
225}
226
227static void
228h_hdl_whois(hook_data_client *hdata)
229{
230 struct Client *source_p = hdata->client;
231 struct Client *target_p = hdata->target;
232
6c639159 233 if ((target_p->umodes & user_modes[UMODECHAR_HELPOPS]) && EmptyString(target_p->user->away))
4d21f1e8
AC
234 {
235 sendto_one_numeric(source_p, RPL_WHOISHELPOP, form_str(RPL_WHOISHELPOP), target_p->name);
236 }
237}
238
a278a4fc 239DECLARE_MODULE_AV2(helpops, _modinit, _moddeinit, helpops_clist, NULL, helpops_hfnlist, NULL, NULL, helpops_desc);