]> jfr.im git - solanum.git/blob - extensions/m_shedding.c
m_shedding: user shedding module based on oftc-hybrid
[solanum.git] / extensions / m_shedding.c
1 /*
2 * Solanum: a slightly advanced ircd
3 * shedding.c: Enables/disables user shedding.
4 *
5 * Based on oftc-hybrid's m_shedding.c
6 *
7 * Copyright (C) 2021 David Schultz <me@zpld.me>
8 * Copyright (C) 2002 by the past and present ircd coders, and others.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 *
26 */
27
28 #include "stdinc.h"
29 #include "modules.h"
30 #include "hook.h"
31 #include "client.h"
32 #include "ircd.h"
33 #include "send.h"
34 #include "s_conf.h"
35 #include "s_serv.h"
36 #include "s_newconf.h"
37 #include "messages.h"
38 #include "numeric.h"
39
40 #define SHED_RATE_MIN 5
41
42 static struct ev_entry *user_shedding_ev = NULL;
43
44 static const char shed_desc[] = "Enables/disables user shedding.";
45
46 static void mo_shedding(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
47 static void me_shedding(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
48 static void do_user_shedding(void *unused);
49
50 static struct Message shedding_msgtab = {
51 "SHEDDING", 0, 0, 0, 0,
52 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_shedding, 2}, {mo_shedding, 3}}
53 };
54
55 mapi_clist_av1 shedding_clist[] = { &shedding_msgtab, NULL };
56
57 static void
58 moddeinit(void)
59 {
60 rb_event_delete(user_shedding_ev);
61 }
62
63 DECLARE_MODULE_AV2(shed, NULL, moddeinit, shedding_clist, NULL, NULL, NULL, NULL, shed_desc);
64
65 static void
66 set_shedding_state(struct Client *source_p, const char *chr, const char *reason)
67 {
68 int rate;
69 if (irccmp(chr, "OFF") == 0)
70 {
71 // disable shedding
72 sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, "%s disabled user shedding", get_oper_name(source_p));
73 rb_event_delete(user_shedding_ev);
74 user_shedding_ev = NULL;
75 return;
76 }
77
78 rate = atoi(chr);
79
80 if(rate < SHED_RATE_MIN)
81 {
82 sendto_one_notice(source_p, "Shedding rate must be at least %d", SHED_RATE_MIN);
83 return;
84 }
85
86 sendto_realops_snomask(SNO_GENERAL, L_ALL | L_NETWIDE, "%s enabled user shedding (interval: %d seconds, reason: %s)",
87 get_oper_name(source_p), rate, reason);
88
89 rb_event_delete(user_shedding_ev);
90 user_shedding_ev = NULL;
91 user_shedding_ev = rb_event_add("user shedding event", do_user_shedding, NULL, rate);
92 }
93
94 /*
95 * mo_shedding
96 *
97 * inputs - pointer to server
98 * - pointer to client
99 * - parameter count
100 * - parameter list
101 * output -
102 * side effects - user shedding is enabled or disabled
103 *
104 * SHEDDING <server> OFF - disable shedding
105 * SHEDDING <server> <approx_seconds_per_userdrop> :<reason>
106 * (parv[#] 1 2 3)
107 *
108 */
109 static void
110 mo_shedding(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
111 {
112 if (!HasPrivilege(source_p, "oper:shedding"))
113 {
114 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "SHEDDING");
115 return;
116 }
117
118 /* I can think of a thousand ways this could go wrong... */
119 if (strchr(parv[1], '*') != NULL)
120 {
121 sendto_one_notice(source_p, "Wildcards are not permitted for shedding targets");
122 return;
123 }
124
125 if (parc != 4 && !(parc == 3 && irccmp(parv[2], "OFF") == 0))
126 {
127 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
128 me.name, source_p->name, "SHEDDING");
129 return;
130 }
131
132 if (irccmp(parv[1], me.name) != 0) {
133 /* it's not for us, pass it around */
134 if (irccmp(parv[2], "OFF") == 0)
135 sendto_match_servs(source_p, parv[1],
136 CAP_ENCAP, NOCAPS,
137 "ENCAP %s SHEDDING OFF", parv[1]);
138 else
139 sendto_match_servs(source_p, parv[1],
140 CAP_ENCAP, NOCAPS,
141 "ENCAP %s SHEDDING %s :%s",
142 parv[1], parv[2], parv[3]);
143 return;
144 }
145
146 set_shedding_state(source_p, parv[2], parv[3]);
147 }
148
149 static void
150 me_shedding(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
151 {
152 if(!IsPerson(source_p))
153 return;
154
155 set_shedding_state(source_p, parv[1], parv[2]);
156 }
157
158
159 static void
160 do_user_shedding(void *unused)
161 {
162 rb_dlink_node *ptr;
163 struct Client *client_p;
164
165 RB_DLINK_FOREACH_PREV(ptr, lclient_list.tail)
166 {
167 client_p = ptr->data;
168
169 if (!IsClient(client_p)) /* It could be servers */
170 continue;
171 if (IsExemptKline(client_p))
172 continue;
173 exit_client(client_p, client_p, &me, "Server closed connection");
174 break;
175 }
176 }