1 /*
2 * Copyright (c) 2005 William Pitcock, et al.
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the CService OWNER functions.
6 *
7 */
8
9 #include "atheme.h"
10 #include "chanserv.h"
11
12 DECLARE_MODULE_V1
13 (
14 "chanserv/protect", false, _modinit, _moddeinit,
15 PACKAGE_STRING,
16 VENDOR_STRING
17 );
18
19 static void cs_cmd_protect(sourceinfo_t *si, int parc, char *parv[]);
20 static void cs_cmd_deprotect(sourceinfo_t *si, int parc, char *parv[]);
21
22 command_t cs_protect = { "PROTECT", N_("Gives the channel protection flag to a user."),
23 AC_NONE, 2, cs_cmd_protect, { .path = "cservice/protect" } };
24 command_t cs_deprotect = { "DEPROTECT", N_("Removes channel protection flag from a user."),
25 AC_NONE, 2, cs_cmd_deprotect, { .path = "cservice/protect" } };
26
_modinit(module_t * m)27 void _modinit(module_t *m)
28 {
29 if (ircd != NULL && !ircd->uses_protect)
30 {
31 slog(LG_INFO, "Module %s requires protect support, refusing to load.", m->name);
32 m->mflags = MODTYPE_FAIL;
33 return;
34 }
35
36 service_named_bind_command("chanserv", &cs_protect);
37 service_named_bind_command("chanserv", &cs_deprotect);
38 }
39
_moddeinit(module_unload_intent_t intent)40 void _moddeinit(module_unload_intent_t intent)
41 {
42 service_named_unbind_command("chanserv", &cs_protect);
43 service_named_unbind_command("chanserv", &cs_deprotect);
44 }
45
46 static mowgli_list_t protect_actions;
47
cmd_protect(sourceinfo_t * si,bool protecting,int parc,char * parv[])48 static void cmd_protect(sourceinfo_t *si, bool protecting, int parc, char *parv[])
49 {
50 char *chan = parv[0];
51 char *nick = parv[1];
52 mychan_t *mc;
53 user_t *tu;
54 chanuser_t *cu;
55 char *nicks;
56 bool protect;
57 mowgli_node_t *n;
58
59 if (ircd->uses_protect == false)
60 {
61 command_fail(si, fault_noprivs, _("The IRCd software you are running does not support this feature."));
62 return;
63 }
64
65 mc = mychan_find(chan);
66 if (!mc)
67 {
68 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), chan);
69 return;
70 }
71
72 if (!chanacs_source_has_flag(mc, si, CA_USEPROTECT))
73 {
74 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
75 return;
76 }
77
78 if (metadata_find(mc, "private:close:closer"))
79 {
80 command_fail(si, fault_noprivs, _("\2%s\2 is closed."), chan);
81 return;
82 }
83
84 nicks = (!nick ? strdup(si->su->nick) : strdup(nick));
85 prefix_action_set_all(&protect_actions, protecting, nicks);
86 free(nicks);
87
88 MOWGLI_LIST_FOREACH(n, protect_actions.head)
89 {
90 struct prefix_action *act = n->data;
91 nick = act->nick;
92 protect = act->en;
93
94 /* figure out who we're going to protect */
95 if (!(tu = user_find_named(nick)))
96 {
97 command_fail(si, fault_nosuch_target, _("\2%s\2 is not online."), nick);
98 continue;
99 }
100
101 if (is_internal_client(tu))
102 continue;
103
104 /* SECURE check; we can skip this if deprotecting or sender == target, because we already verified */
105 if (protect && (si->su != tu) && (mc->flags & MC_SECURE) && !chanacs_user_has_flag(mc, tu, CA_OP) && !chanacs_user_has_flag(mc, tu, CA_AUTOOP))
106 {
107 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
108 command_fail(si, fault_noprivs, _("\2%s\2 has the SECURE option enabled, and \2%s\2 does not have appropriate access."), mc->name, tu->nick);
109 continue;
110 }
111
112 cu = chanuser_find(mc->chan, tu);
113 if (!cu)
114 {
115 command_fail(si, fault_nosuch_target, _("\2%s\2 is not on \2%s\2."), tu->nick, mc->name);
116 continue;
117 }
118
119 modestack_mode_param(chansvs.nick, mc->chan, protect ? MTYPE_ADD : MTYPE_DEL, ircd->protect_mchar[1], CLIENT_NAME(tu));
120 if (protect)
121 cu->modes |= CSTATUS_PROTECT;
122 else
123 cu->modes &= ~CSTATUS_PROTECT;
124
125 if (si->c == NULL && tu != si->su)
126 change_notify(chansvs.nick, tu, "You have been %sset as protected on %s by %s", protect ? "" : "un", mc->name, get_source_name(si));
127
128 logcommand(si, CMDLOG_DO, "%sPROTECT: \2%s!%s@%s\2 on \2%s\2", protect ? "" : "DE", tu->nick, tu->user, tu->vhost, mc->name);
129 if (si->su == NULL || !chanuser_find(mc->chan, si->su))
130 command_success_nodata(si, _("\2%s\2 has been %sset as protected on \2%s\2."), tu->nick, protect ? "" : "un", mc->name);
131 }
132
133 prefix_action_clear(&protect_actions);
134 }
135
cs_cmd_protect(sourceinfo_t * si,int parc,char * parv[])136 static void cs_cmd_protect(sourceinfo_t *si, int parc, char *parv[])
137 {
138 if (!parv[0])
139 {
140 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "PROTECT");
141 command_fail(si, fault_needmoreparams, _("Syntax: PROTECT <#channel> [nickname]"));
142 return;
143 }
144
145 cmd_protect(si, true, parc, parv);
146 }
147
cs_cmd_deprotect(sourceinfo_t * si,int parc,char * parv[])148 static void cs_cmd_deprotect(sourceinfo_t *si, int parc, char *parv[])
149 {
150 if (!parv[0])
151 {
152 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DEPROTECT");
153 command_fail(si, fault_needmoreparams, _("Syntax: DEPROTECT <#channel> [nickname]"));
154 return;
155 }
156
157 cmd_protect(si, false, parc, parv);
158 }
159
160 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
161 * vim:ts=8
162 * vim:sw=8
163 * vim:noexpandtab
164 */
165