m_randquote.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015
00016 static FileReader *quotes = NULL;
00017
00018 std::string q_file;
00019 std::string prefix;
00020 std::string suffix;
00021
00022
00023
00026 class CommandRandquote : public Command
00027 {
00028 public:
00029 CommandRandquote (InspIRCd* Instance) : Command(Instance,"RANDQUOTE", 0, 0)
00030 {
00031 this->source = "m_randquote.so";
00032 }
00033
00034 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
00035 {
00036 std::string str;
00037 int fsize;
00038
00039 if (q_file.empty() || quotes->Exists())
00040 {
00041 fsize = quotes->FileSize();
00042 str = quotes->GetLine(rand() % fsize);
00043 user->WriteServ("NOTICE %s :%s%s%s",user->nick.c_str(),prefix.c_str(),str.c_str(),suffix.c_str());
00044 }
00045 else
00046 {
00047 user->WriteServ("NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick.c_str());
00048 return CMD_FAILURE;
00049 }
00050
00051 return CMD_LOCALONLY;
00052 }
00053 };
00054
00055 class ModuleRandQuote : public Module
00056 {
00057 private:
00058 CommandRandquote* mycommand;
00059 ConfigReader *conf;
00060 public:
00061 ModuleRandQuote(InspIRCd* Me)
00062 : Module(Me)
00063 {
00064
00065 conf = new ConfigReader(ServerInstance);
00066
00067 srand(ServerInstance->Time());
00068
00069 q_file = conf->ReadValue("randquote","file",0);
00070 prefix = conf->ReadValue("randquote","prefix",0);
00071 suffix = conf->ReadValue("randquote","suffix",0);
00072
00073 mycommand = NULL;
00074
00075 if (q_file.empty())
00076 {
00077 throw ModuleException("m_randquote: Quotefile not specified - Please check your config.");
00078 }
00079
00080 quotes = new FileReader(ServerInstance, q_file);
00081 if(!quotes->Exists())
00082 {
00083 throw ModuleException("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
00084 }
00085 else
00086 {
00087
00088 mycommand = new CommandRandquote(ServerInstance);
00089 ServerInstance->AddCommand(mycommand);
00090 }
00091 Implementation eventlist[] = { I_OnUserConnect };
00092 ServerInstance->Modules->Attach(eventlist, this, 1);
00093 }
00094
00095
00096 virtual ~ModuleRandQuote()
00097 {
00098 delete conf;
00099 delete quotes;
00100 }
00101
00102 virtual Version GetVersion()
00103 {
00104 return Version("$Id: m_randquote.cpp 10783 2008-11-01 23:02:23Z w00t $",VF_VENDOR,API_VERSION);
00105 }
00106
00107 virtual void OnUserConnect(User* user)
00108 {
00109 if (mycommand)
00110 mycommand->Handle(std::vector<std::string>(), user);
00111 }
00112 };
00113
00114 MODULE_INIT(ModuleRandQuote)