]> jfr.im git - solanum.git/blob - extensions/helpops.c
Merge pull request #161 from awilfox/av2desc
[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 rb_dlink_list helper_list = { NULL, NULL, 0 };
18 static void h_hdl_stats_request(hook_data_int *hdata);
19 static void h_hdl_new_remote_user(struct Client *client_p);
20 static void h_hdl_client_exit(hook_data_client_exit *hdata);
21 static void h_hdl_umode_changed(hook_data_umode_changed *hdata);
22 static void h_hdl_whois(hook_data_client *hdata);
23 static int mo_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
24 static int me_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
25 static int do_dehelper(struct Client *source_p, struct Client *target_p);
26
27 mapi_hfn_list_av1 helpops_hfnlist[] = {
28 { "doing_stats", (hookfn) h_hdl_stats_request },
29 { "new_remote_user", (hookfn) h_hdl_new_remote_user },
30 { "client_exit", (hookfn) h_hdl_client_exit },
31 { "umode_changed", (hookfn) h_hdl_umode_changed },
32 { "doing_whois", (hookfn) h_hdl_whois },
33 { "doing_whois_global", (hookfn) h_hdl_whois },
34 { NULL, NULL }
35 };
36
37 static int UMODE_HELPOPS = 0;
38
39 struct Message dehelper_msgtab = {
40 "DEHELPER", 0, 0, 0, 0,
41 {mg_unreg, mg_not_oper, mg_not_oper, mg_ignore, {me_dehelper, 2}, {mo_dehelper, 2}}
42 };
43
44 mapi_clist_av1 helpops_clist[] = { &dehelper_msgtab, NULL };
45
46 static const char helpops_desc[] = "The helpops system as used by freenode";
47
48 static int mo_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
49 {
50 struct Client *target_p;
51
52 if (!IsOperAdmin(source_p))
53 {
54 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "admin");
55 return 0;
56 }
57
58 if(!(target_p = find_named_person(parv[1])))
59 {
60 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), parv[1]);
61 return 0;
62 }
63
64 if(MyClient(target_p))
65 do_dehelper(source_p, target_p);
66 else
67 sendto_one(target_p, ":%s ENCAP %s DEHELPER %s",
68 use_id(source_p), target_p->servptr->name, use_id(target_p));
69
70 return 0;
71 }
72
73 static int me_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
74 {
75 struct Client *target_p = find_person(parv[1]);
76 if(!target_p)
77 {
78 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), parv[1]);
79 return 0;
80 }
81 if(!MyClient(target_p))
82 return 0;
83
84 do_dehelper(source_p, target_p);
85 return 0;
86 }
87
88 static int do_dehelper(struct Client *source_p, struct Client *target_p)
89 {
90 const char *fakeparv[4];
91
92 if(!(target_p->umodes & UMODE_HELPOPS))
93 return 0;
94
95 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using DEHELPER on %s",
96 source_p->name, target_p->name);
97 sendto_one_notice(target_p, ":*** %s is using DEHELPER on you", source_p->name);
98
99 fakeparv[0] = fakeparv[1] = target_p->name;
100 fakeparv[2] = "-H";
101 fakeparv[3] = NULL;
102 user_mode(target_p, target_p, 3, fakeparv);
103 return 0;
104 }
105
106 static int
107 _modinit(void)
108 {
109 /* add the usermode to the available slot */
110 user_modes['H'] = UMODE_HELPOPS = find_umode_slot();
111 construct_umodebuf();
112
113 return 0;
114 }
115
116 static void
117 _moddeinit(void)
118 {
119 /* disable the umode and remove it from the available list */
120 user_modes['H'] = UMODE_HELPOPS = 0;
121 construct_umodebuf();
122 }
123
124 static void
125 h_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
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
155 static void
156 h_hdl_new_remote_user(struct Client *client_p)
157 {
158 if (client_p->umodes & UMODE_HELPOPS)
159 rb_dlinkAddAlloc(client_p, &helper_list);
160 }
161
162 static void
163 h_hdl_client_exit(hook_data_client_exit *hdata)
164 {
165 if (hdata->target->umodes & UMODE_HELPOPS)
166 rb_dlinkFindDestroy(hdata->target, &helper_list);
167 }
168
169 static void
170 h_hdl_umode_changed(hook_data_umode_changed *hdata)
171 {
172 struct Client *source_p = hdata->client;
173
174 /* didn't change +H umode, we don't need to do anything */
175 if (!((hdata->oldumodes ^ source_p->umodes) & UMODE_HELPOPS))
176 return;
177
178 if (source_p->umodes & UMODE_HELPOPS)
179 {
180 if (MyClient(source_p) && !HasPrivilege(source_p, "usermode:helpops"))
181 {
182 source_p->umodes &= ~UMODE_HELPOPS;
183 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "usermode:helpops");
184 return;
185 }
186
187 rb_dlinkAddAlloc(source_p, &helper_list);
188 }
189 else if (!(source_p->umodes & UMODE_HELPOPS))
190 rb_dlinkFindDestroy(source_p, &helper_list);
191 }
192
193 static void
194 h_hdl_whois(hook_data_client *hdata)
195 {
196 struct Client *source_p = hdata->client;
197 struct Client *target_p = hdata->target;
198
199 if ((target_p->umodes & UMODE_HELPOPS) && EmptyString(target_p->user->away))
200 {
201 sendto_one_numeric(source_p, RPL_WHOISHELPOP, form_str(RPL_WHOISHELPOP), target_p->name);
202 }
203 }
204
205 DECLARE_MODULE_AV2(helpops, _modinit, _moddeinit, helpops_clist, NULL, helpops_hfnlist, NULL, NULL, NULL);