]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ojoin.cpp
6e9e3681200304f4ee14384765727e9999931261
[user/henk/code/inspircd.git] / src / modules / m_ojoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /*
15  * Written for InspIRCd-1.2 by Taros on the Tel'Laerad M&D Team
16  * <http://tellaerad.net>
17  */
18
19 #include "inspircd.h"
20
21 /* $ModConfig: <ojoin prefix="!" notice="yes" op="yes">
22  *  Specify the prefix that +Y will grant here, it should be unused.
23  *  Leave prefix empty if you do not wish +Y to grant a prefix
24  *  If notice is set to on, upon ojoin, the server will notice
25  *  the channel saying that the oper is joining on network business
26  *  If op is set to on, it will give them +o along with +Y */
27 /* $ModDesc: Provides the /ojoin command, which joins a user to a channel on network business, and gives them +Y, which makes them immune to kick / deop and so on. */
28 /* $ModAuthor: Taros */
29 /* $ModAuthorMail: taros34@hotmail.com */
30
31 /* A note: This will not protect against kicks from services,
32  * ulines, or operoverride. */
33
34 #define NETWORK_VALUE 9000000
35
36 char NPrefix;
37 bool notice;
38 bool op;
39
40 /** Handle /OJOIN
41  */
42 class CommandOjoin : public Command
43 {
44  public:
45         bool active;
46         CommandOjoin(Module* parent) : Command(parent,"OJOIN", 1)
47         {
48                 flags_needed = 'o'; Penalty = 0; syntax = "<channel>";
49                 active = false;
50                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
51         }
52
53         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
54         {
55                 // Make sure the channel name is allowable.
56                 if (!ServerInstance->IsChannel(parameters[0].c_str(), ServerInstance->Config->Limits.ChanMax))
57                 {
58                         user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Invalid characters in channel name or name too long");
59                         return CMD_FAILURE;
60                 }
61
62                 active = true;
63                 Channel* channel = Channel::JoinUser(user, parameters[0].c_str(), false, "", false);
64                 active = false;
65
66                 if (channel)
67                 {
68                         ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used OJOIN to join "+channel->name);
69
70                         if (notice)
71                         {
72                                 channel = ServerInstance->FindChan(parameters[0]);
73                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "NOTICE %s :%s joined on official network business.",
74                                         parameters[0].c_str(), user->nick.c_str());
75                                 ServerInstance->PI->SendChannelNotice(channel, 0, std::string(user->nick) + " joined on official network business.");
76                         }
77                 }
78                 else
79                 {
80                         ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used OJOIN in "+parameters[0]);
81                         // they're already in the channel
82                         std::vector<std::string> modes;
83                         modes.push_back(parameters[0]);
84                         modes.push_back("+Y");
85                         modes.push_back(user->nick);
86                         ServerInstance->SendGlobalMode(modes, ServerInstance->FakeClient);
87                 }
88                 return CMD_SUCCESS;
89         }
90 };
91
92 /** channel mode +Y
93  */
94 class NetworkPrefix : public ModeHandler
95 {
96  public:
97         NetworkPrefix(Module* parent) : ModeHandler(parent, "official-join", 'Y', PARAM_ALWAYS, MODETYPE_CHANNEL)
98         {
99                 list = true;
100                 prefix = NPrefix;
101                 levelrequired = 0xFFFFFFFF;
102                 m_paramtype = TR_NICK;
103         }
104
105         void RemoveMode(Channel* channel, irc::modestacker* stack)
106         {
107                 const UserMembList* cl = channel->GetUsers();
108                 std::vector<std::string> mode_junk;
109                 mode_junk.push_back(channel->name);
110                 irc::modestacker modestack(false);
111                 std::deque<std::string> stackresult;
112
113                 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
114                 {
115                         if (i->second->hasMode('Y'))
116                         {
117                                 if (stack)
118                                         stack->Push(this->GetModeChar(), i->first->nick);
119                                 else
120                                         modestack.Push(this->GetModeChar(), i->first->nick);
121                         }
122                 }
123
124                 if (stack)
125                         return;
126
127                 while (modestack.GetStackedLine(stackresult))
128                 {
129                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
130                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
131                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
132                 }
133         }
134
135         User* FindAndVerify(std::string &parameter, Channel* channel)
136         {
137                 User* theuser = ServerInstance->FindNick(parameter);
138                 if ((!theuser) || (!channel->HasUser(theuser)))
139                 {
140                         parameter.clear();
141                         return NULL;
142                 }
143                 return theuser;
144         }
145
146         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
147         {
148                 Membership* m = channel->GetUser(theuser);
149                 if (m && adding)
150                 {
151                         if (!m->hasMode('Y'))
152                         {
153                                 parameter = theuser->nick;
154                                 return MODEACTION_ALLOW;
155                         }
156                 }
157                 else if (m && !adding)
158                 {
159                         if (m->hasMode('Y'))
160                         {
161                                 parameter = theuser->nick;
162                                 return MODEACTION_ALLOW;
163                         }
164                 }
165                 return MODEACTION_DENY;
166         }
167
168         unsigned int GetPrefixRank()
169         {
170                 return NETWORK_VALUE;
171         }
172
173         void RemoveMode(User* user, irc::modestacker* stack)
174         {
175         }
176
177         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
178         {
179                 User* theuser = FindAndVerify(parameter, channel);
180
181                 if (!theuser)
182                         return MODEACTION_DENY;
183
184                  // source is a server, or ulined, we'll let them +-Y the user.
185                 if (source == ServerInstance->FakeClient ||
186                         ((source == theuser) && (!adding)) ||
187                         (ServerInstance->ULine(source->nick.c_str())) ||
188                         (ServerInstance->ULine(source->server)) ||
189                         (!IS_LOCAL(source))
190                         )
191                 {
192                         return HandleChange(source, theuser, adding, channel, parameter);
193                 }
194                 else
195                 {
196                         // bzzzt, wrong answer!
197                         source->WriteNumeric(482, "%s %s :Only servers may change this mode.", source->nick.c_str(), channel->name.c_str());
198                         return MODEACTION_DENY;
199                 }
200         }
201
202 };
203
204 class ModuleOjoin : public Module
205 {
206         NetworkPrefix* np;
207         CommandOjoin mycommand;
208
209  public:
210
211         ModuleOjoin()
212                 : np(NULL), mycommand(this)
213         {
214         }
215
216         void init()
217         {
218                 /* Load config stuff */
219                 OnRehash(NULL);
220
221                 /* Initialise module variables */
222                 np = new NetworkPrefix(this);
223
224                 ServerInstance->Modules->AddService(*np);
225                 ServerInstance->Modules->AddService(mycommand);
226
227                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserPreKick, I_OnRehash };
228                 ServerInstance->Modules->Attach(eventlist, this, 3);
229         }
230
231         ModResult OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
232         {
233                 if (mycommand.active)
234                 {
235                         privs += 'Y';
236                         if (op)
237                                 privs += 'o';
238                         return MOD_RES_ALLOW;
239                 }
240
241                 return MOD_RES_PASSTHRU;
242         }
243
244         void OnRehash(User* user)
245         {
246                 ConfigReader Conf;
247
248                 if (!np)
249                 {
250                         // This is done on module load only
251                         std::string npre = Conf.ReadValue("ojoin", "prefix", 0);
252                         NPrefix = npre.empty() ? 0 : npre[0];
253
254                         if (NPrefix && ServerInstance->Modes->FindPrefix(NPrefix))
255                                 throw ModuleException("Looks like the +Y prefix you picked for m_ojoin is already in use. Pick another.");
256                 }
257
258                 notice = Conf.ReadFlag("ojoin", "notice", "yes", 0);
259                 op = Conf.ReadFlag("ojoin", "op", "yes", 0);
260         }
261
262         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
263         {
264                 if ((ServerInstance->ULine(source->nick.c_str())) || ServerInstance->ULine(source->server))
265                         return MOD_RES_PASSTHRU;
266
267                 // Don't do anything if they're not +Y
268                 if (!memb->hasMode('Y'))
269                         return MOD_RES_PASSTHRU;
270
271                 // Let them do whatever they want to themselves.
272                 if (source == memb->user)
273                         return MOD_RES_PASSTHRU;
274
275                 source->WriteNumeric(484, source->nick+" "+memb->chan->name+" :Can't kick "+memb->user->nick+" as they're on official network business.");
276                 return MOD_RES_DENY;
277         }
278
279         ~ModuleOjoin()
280         {
281                 delete np;
282         }
283
284         void Prioritize()
285         {
286                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_FIRST);
287         }
288
289         Version GetVersion()
290         {
291                 return Version("Network Buisness Join", VF_VENDOR);
292         }
293 };
294
295 MODULE_INIT(ModuleOjoin)
296