-#include <stdio.h>
-#include <stdlib.h>
-#include <fstream>
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2010 InspIRCd Development Team
+ * See: http://wiki.inspircd.org/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
+#include "inspircd.h"
+static FileReader *quotes = NULL;
+
+std::string prefix;
+std::string suffix;
/* $ModDesc: Provides random Quotes on Connect. */
-class ModuleRandQuote : public Module
+/** Handle /RANDQUOTE
+ */
+class CommandRandquote : public Command
{
- private:
-
- Server *Srv;
- ConfigReader *conf;
- FileReader *quotes;
-
- std::string q_file;
- std::string prefix;
- std::string suffix;
-
public:
- ModuleRandQuote()
+ CommandRandquote(Module* Creator) : Command(Creator,"RANDQUOTE", 0)
{
- Srv = new Server;
- conf = new ConfigReader;
-
- q_file = conf->ReadValue("randquote","file",0);
- prefix = conf->ReadValue("randquote","prefix",0);
- suffix = conf->ReadValue("randquote","suffix",0);
-
- quotes = new FileReader(q_file);
}
-
- virtual ~ModuleRandQuote()
- {
- delete Srv;
- delete conf;
- delete quotes;
- }
-
- virtual Version GetVersion()
- {
- return Version(1,0,0,0);
- }
-
- virtual void OnUserConnect(userrec* user)
+
+ CmdResult Handle (const std::vector<std::string>& parameters, User *user)
{
std::string str;
int fsize;
- char buf[MAXBUF];
fsize = quotes->FileSize();
- srand(time(NULL));
- str = quotes->GetLine(rand() % fsize);
-
- sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
- Srv->SendServ(user->fd, buf);
- return;
+ str = quotes->GetLine(ServerInstance->GenRandomInt(fsize));
+ user->WriteServ("NOTICE %s :%s%s%s",user->nick.c_str(),prefix.c_str(),str.c_str(),suffix.c_str());
+
+ return CMD_SUCCESS;
}
};
-
-class ModuleRandQuoteFactory : public ModuleFactory
+class ModuleRandQuote : public Module
{
+ private:
+ CommandRandquote cmd;
public:
- ModuleRandQuoteFactory()
+ ModuleRandQuote()
+ : cmd(this)
{
}
-
- ~ModuleRandQuoteFactory()
+
+ void init()
{
+ ConfigTag* conf = ServerInstance->Config->ConfValue("randquote");
+
+ std::string q_file = conf->getString("file","quotes");
+ prefix = conf->getString("prefix");
+ suffix = conf->getString("suffix");
+
+ quotes = new FileReader(q_file);
+ if (!quotes->Exists())
+ {
+ throw ModuleException("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
+ }
+ ServerInstance->AddCommand(&cmd);
+ Implementation eventlist[] = { I_OnUserConnect };
+ ServerInstance->Modules->Attach(eventlist, this, 1);
}
-
- virtual Module * CreateModule()
+
+
+ virtual ~ModuleRandQuote()
{
- return new ModuleRandQuote;
+ delete quotes;
}
-
-};
+ virtual Version GetVersion()
+ {
+ return Version("Provides random Quotes on Connect.",VF_VENDOR);
+ }
-extern "C" void * init_module( void )
-{
- return new ModuleRandQuoteFactory;
-}
+ virtual void OnUserConnect(LocalUser* user)
+ {
+ cmd.Handle(std::vector<std::string>(), user);
+ }
+};
+MODULE_INIT(ModuleRandQuote)