]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_reloadmodule.cpp
Handle module reloading in core_reloadmodule entirely
[user/henk/code/inspircd.git] / src / coremods / core_reloadmodule.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22
23 class CommandReloadmodule : public Command
24 {
25  public:
26         /** Constructor for reloadmodule.
27          */
28         CommandReloadmodule ( Module* parent) : Command( parent, "RELOADMODULE",1) { flags_needed = 'o'; syntax = "<modulename>"; }
29         /** Handle command.
30          * @param parameters The parameters to the command
31          * @param user The user issuing the command
32          * @return A value from CmdResult to indicate command success or failure.
33          */
34         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
35 };
36
37 class ReloadAction : public HandlerBase0<void>
38 {
39         Module* const mod;
40         const std::string uuid;
41         const std::string passedname;
42
43  public:
44         ReloadAction(Module* m, const std::string& uid, const std::string& passedmodname)
45                 : mod(m)
46                 , uuid(uid)
47                 , passedname(passedmodname)
48         {
49         }
50
51         void Call()
52         {
53                 DLLManager* dll = mod->ModuleDLLManager;
54                 std::string name = mod->ModuleSourceFile;
55                 ServerInstance->Modules->DoSafeUnload(mod);
56                 ServerInstance->GlobalCulls.Apply();
57                 delete dll;
58                 bool result = ServerInstance->Modules->Load(name);
59
60                 ServerInstance->SNO->WriteGlobalSno('a', "RELOAD MODULE: %s %ssuccessfully reloaded", passedname.c_str(), result ? "" : "un");
61                 User* user = ServerInstance->FindUUID(uuid);
62                 if (user)
63                         user->WriteNumeric(RPL_LOADEDMODULE, "%s :Module %ssuccessfully reloaded.", passedname.c_str(), result ? "" : "un");
64
65                 ServerInstance->GlobalCulls.AddItem(this);
66         }
67 };
68
69 CmdResult CommandReloadmodule::Handle (const std::vector<std::string>& parameters, User *user)
70 {
71         Module* m = ServerInstance->Modules->Find(parameters[0]);
72         if (m == creator)
73         {
74                 user->WriteNumeric(RPL_LOADEDMODULE, "%s :You cannot reload core_reloadmodule.so (unload and load it)",
75                         parameters[0].c_str());
76                 return CMD_FAILURE;
77         }
78
79         if (creator->dying)
80                 return CMD_FAILURE;
81
82         if ((m) && (ServerInstance->Modules.CanUnload(m)))
83         {
84                 ServerInstance->AtomicActions.AddAction(new ReloadAction(m, user->uuid, parameters[0]));
85                 return CMD_SUCCESS;
86         }
87         else
88         {
89                 user->WriteNumeric(RPL_LOADEDMODULE, "%s :Could not find module by that name", parameters[0].c_str());
90                 return CMD_FAILURE;
91         }
92 }
93
94 COMMAND_INIT(CommandReloadmodule)