module.cpp

Go to the documentation of this file.
00001 /* Modular support
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  */
00009 
00010 #include "services.h"
00011 #include "modules.h"
00012 #include "language.h"
00013 #include "account.h"
00014 
00015 #ifdef GETTEXT_FOUND
00016 # include <libintl.h>
00017 #endif
00018 
00019 Module::Module(const Anope::string &modname, const Anope::string &, ModType modtype) : name(modname), type(modtype)
00020 {
00021         this->handle = NULL;
00022         this->permanent = false;
00023         this->created = Anope::CurTime;
00024         this->SetVersion(Anope::Version());
00025 
00026         if (ModuleManager::FindModule(this->name))
00027                 throw CoreException("Module already exists!");
00028         
00029         if (Anope::NoThird && modtype == THIRD)
00030                 throw ModuleException("Third party modules may not be loaded");
00031 
00032         ModuleManager::Modules.push_back(this);
00033 
00034 #if GETTEXT_FOUND
00035         for (unsigned i = 0; i < Language::Languages.size(); ++i)
00036                 if (Anope::IsFile(Anope::LocaleDir + "/" + Language::Languages[i] + "/LC_MESSAGES/" + modname + ".mo"))
00037                 {
00038                         if (!bindtextdomain(this->name.c_str(), Anope::LocaleDir.c_str()))
00039                                 Log() << "Error calling bindtextdomain, " << Anope::LastError();
00040                         else
00041                                 Language::Domains.push_back(modname);
00042                         break;
00043                 }
00044 #endif
00045 }
00046 
00047 Module::~Module()
00048 {
00049         /* Detach all event hooks for this module */
00050         ModuleManager::DetachAll(this);
00051         /* Clear any active callbacks this module has */
00052         ModuleManager::ClearCallBacks(this);
00053         IdentifyRequest::ModuleUnload(this);
00054 
00055         std::list<Module *>::iterator it = std::find(ModuleManager::Modules.begin(), ModuleManager::Modules.end(), this);
00056         if (it != ModuleManager::Modules.end())
00057                 ModuleManager::Modules.erase(it);
00058 
00059 #if GETTEXT_FOUND
00060         std::vector<Anope::string>::iterator dit = std::find(Language::Domains.begin(), Language::Domains.end(), this->name);
00061         if (dit != Language::Domains.end())
00062                 Language::Domains.erase(dit);
00063 #endif
00064 }
00065 
00066 void Module::SetPermanent(bool state)
00067 {
00068         this->permanent = state;
00069 }
00070 
00071 bool Module::GetPermanent() const
00072 {
00073         return this->permanent;
00074 }
00075 
00076 void Module::SetVersion(const Anope::string &nversion)
00077 {
00078         this->version = nversion;
00079 }
00080 
00081 void Module::SetAuthor(const Anope::string &nauthor)
00082 {
00083         this->author = nauthor;
00084 }
00085 
00086 ModuleVersion::ModuleVersion(int maj, int min, int pa) : version_major(maj), version_minor(min), version_patch(pa)
00087 {
00088 }
00089 
00090 int ModuleVersion::GetMajor() const
00091 {
00092         return this->version_major;
00093 }
00094 
00095 int ModuleVersion::GetMinor() const
00096 {
00097         return this->version_minor;
00098 }
00099 
00100 int ModuleVersion::GetPatch() const
00101 {
00102         return this->version_patch;
00103 }
00104 
00105 CallBack::CallBack(Module *mod, long time_from_now, time_t now, bool repeating) : Timer(time_from_now, now, repeating),  m(mod)
00106 {
00107 }
00108 
00109 CallBack::~CallBack()
00110 {
00111         std::list<CallBack *>::iterator it = std::find(m->callbacks.begin(), m->callbacks.end(), this);
00112         if (it != m->callbacks.end())
00113                 m->callbacks.erase(it);
00114 }
00115