]> jfr.im git - irc/freenode/solanum.git/blob - modules/m_grant.c
63ae6c7d96198bbc9878fc5a89b95f10cfbaf64a
[irc/freenode/solanum.git] / modules / m_grant.c
1 /*
2 * Copyright (C) 2006 Jilles Tjoelker
3 * Copyright (C) 2006 Stephen Bennett <spb@gentoo.org>
4 */
5
6 #include "stdinc.h"
7 #include "modules.h"
8 #include "numeric.h"
9 #include "client.h"
10 #include "ircd.h"
11 #include "send.h"
12 #include "s_user.h"
13 #include "s_serv.h"
14 #include "s_conf.h"
15 #include "s_newconf.h"
16 #include "msgbuf.h"
17
18 static void mo_grant(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
19 static void me_grant(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
20
21 static int do_grant(struct Client *source_p, struct Client *target_p, const char *new_privset);
22
23 struct Message grant_msgtab = {
24 "GRANT", 0, 0, 0, 0,
25 { mg_ignore, mg_not_oper, mg_ignore, mg_ignore, {me_grant, 3}, {mo_grant, 3}}
26 };
27
28 mapi_clist_av1 grant_clist[] = { &grant_msgtab, NULL };
29
30 static const char grant_desc[] = "Allows operators to set or remove operator privileges on other users";
31
32 DECLARE_MODULE_AV2(grant, NULL, NULL, grant_clist, NULL, NULL, NULL, NULL, grant_desc);
33
34 static void
35 mo_grant(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
36 {
37 struct Client *target_p;
38
39 if(!HasPrivilege(source_p, "oper:grant"))
40 {
41 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "grant");
42 return;
43 }
44
45 target_p = find_named_person(parv[1]);
46 if (target_p == NULL)
47 {
48 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
49 form_str(ERR_NOSUCHNICK), parv[1]);
50 return;
51 }
52
53 if (MyClient(target_p))
54 {
55 do_grant(source_p, target_p, parv[2]);
56 }
57 else
58 {
59 sendto_one(target_p, ":%s ENCAP %s GRANT %s %s",
60 get_id(source_p, target_p), target_p->servptr->name,
61 get_id(target_p, target_p), parv[2]);
62 }
63 }
64
65 static void
66 me_grant(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
67 {
68 struct Client *target_p;
69
70 target_p = find_person(parv[1]);
71 if (target_p == NULL)
72 {
73 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
74 form_str(ERR_NOSUCHNICK), parv[1]);
75 return;
76 }
77
78 do_grant(source_p, target_p, parv[2]);
79 }
80
81
82 static int do_grant(struct Client *source_p, struct Client *target_p, const char *new_privset)
83 {
84 int dooper = 0, dodeoper = 0;
85 struct PrivilegeSet *privset = 0;
86
87 if (!strcasecmp(new_privset, "deoper"))
88 {
89 if (!IsOper(target_p))
90 {
91 sendto_one_notice(source_p, ":You can't deoper someone who isn't an oper.");
92 return 0;
93 }
94 dodeoper = 1;
95
96 sendto_one_notice(target_p, ":%s is deopering you.", source_p->name);
97 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is deopering %s.", get_oper_name(source_p), target_p->name);
98 }
99 else
100 {
101 if (!(privset = privilegeset_get(new_privset)))
102 {
103 sendto_one_notice(source_p, ":There is no privilege set named '%s'.", new_privset);
104 return 0;
105 }
106
107 if (privset == target_p->user->privset)
108 {
109 sendto_one_notice(source_p, ":%s already has privilege set %s.", target_p->name, target_p->user->privset->name);
110 return 0;
111 }
112 }
113
114 if (!dodeoper)
115 {
116 if (!IsOper(target_p))
117 {
118 sendto_one_notice(target_p, ":%s is opering you with privilege set %s", source_p->name, privset->name);
119 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is opering %s with privilege set %s", get_oper_name(source_p), target_p->name, privset->name);
120 dooper = 1;
121 }
122 else
123 {
124 sendto_one_notice(target_p, ":%s is changing your privilege set to %s", source_p->name, privset->name);
125 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is changing the privilege set of %s to %s", get_oper_name(source_p), target_p->name, privset->name);
126 }
127
128 if (!IsOper(target_p))
129 {
130 dooper = 1;
131 }
132 }
133
134 if (dodeoper)
135 {
136 const char *modeparv[4];
137 modeparv[0] = modeparv[1] = target_p->name;
138 modeparv[2] = "-o";
139 modeparv[3] = NULL;
140 user_mode(target_p, target_p, 3, modeparv);
141 }
142
143 if (dooper)
144 {
145 struct oper_conf oper = {0};
146 oper.name = "<grant>";
147 oper.privset = privset;
148
149 oper_up(target_p, &oper);
150 }
151 else if (privset != NULL)
152 {
153 privilegeset_ref(privset);
154 }
155
156 if (target_p->user->privset != NULL)
157 privilegeset_unref(target_p->user->privset);
158
159 target_p->user->privset = privset;
160
161 if (privset != NULL)
162 sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s OPER %s %s",
163 use_id(target_p), target_p->user->opername, privset->name);
164
165 const char *modeparv[4];
166 modeparv[0] = modeparv[1] = target_p->name;
167 modeparv[2] = "+";
168 modeparv[3] = NULL;
169 user_mode(target_p, target_p, 3, modeparv);
170
171 return 0;
172 }