]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Just to mess with om's head, remove helperfuncs.h from everywhere
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22
23 #include "inspircd.h"
24
25
26 static FileReader *quotes = NULL;
27
28 std::string q_file = "";
29 std::string prefix = "";
30 std::string suffix = "";
31
32 /* $ModDesc: Provides random Quotes on Connect. */
33
34 class cmd_randquote : public command_t
35 {
36  public:
37  cmd_randquote (InspIRCd* Instance) : command_t(Instance,"RANDQUOTE", 0, 0)
38         {
39                 this->source = "m_randquote.so";
40         }
41
42         void Handle (const char** parameters, int pcntl, userrec *user)
43         {
44                 std::string str;
45                 int fsize;
46                 char buf[MAXBUF];
47                 if (q_file == "" || quotes->Exists())
48                 {
49                         fsize = quotes->FileSize();
50                         str = quotes->GetLine(rand() % fsize);
51                         sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
52                         user->WriteServ(std::string(buf));
53                 }
54                 else
55                 {
56                         sprintf(buf, "NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
57                         user->WriteServ(std::string(buf));
58                 }
59                 return;
60         }
61 };
62
63 class RandquoteException : public ModuleException
64 {
65  private:
66         std::string err;
67  public:
68         RandquoteException(std::string message) : err(message) { }
69
70         virtual const char* GetReason()
71         {
72                 return (char*)err.c_str();
73         }
74 };
75
76 class ModuleRandQuote : public Module
77 {
78  private:
79         cmd_randquote* mycommand;
80         ConfigReader *conf;
81  public:
82         ModuleRandQuote(InspIRCd* Me)
83                 : Module::Module(Me)
84         {
85                 
86                 conf = new ConfigReader(ServerInstance);
87                 // Sort the Randomizer thingie..
88                 srand(time(NULL));
89
90                 q_file = conf->ReadValue("randquote","file",0);
91                 prefix = conf->ReadValue("randquote","prefix",0);
92                 suffix = conf->ReadValue("randquote","suffix",0);
93
94                 mycommand = NULL;
95
96                 if (q_file == "")
97                 {
98                         RandquoteException e("m_randquote: Quotefile not specified - Please check your config.");
99                         throw(e);
100                 }
101
102                 quotes = new FileReader(ServerInstance, q_file);
103                 if(!quotes->Exists())
104                 {
105                         RandquoteException e("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
106                         throw(e);
107                 }
108                 else
109                 {
110                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
111                         mycommand = new cmd_randquote(ServerInstance);
112                         ServerInstance->AddCommand(mycommand);
113                 }
114         }
115
116         void Implements(char* List)
117         {
118                 List[I_OnUserConnect] = 1;
119         }
120         
121         virtual ~ModuleRandQuote()
122         {
123                 DELETE(conf);
124                 DELETE(quotes);
125         }
126         
127         virtual Version GetVersion()
128         {
129                 return Version(1,0,0,1,VF_VENDOR);
130         }
131         
132         virtual void OnUserConnect(userrec* user)
133         {
134                 if (mycommand)
135                         mycommand->Handle(NULL, 0, user);
136         }
137 };
138
139
140 class ModuleRandQuoteFactory : public ModuleFactory
141 {
142  public:
143         ModuleRandQuoteFactory()
144         {
145         }
146         
147         ~ModuleRandQuoteFactory()
148         {
149         }
150         
151         virtual Module * CreateModule(InspIRCd* Me)
152         {
153                 return new ModuleRandQuote(Me);
154         }
155         
156 };
157
158
159 extern "C" void * init_module( void )
160 {
161         return new ModuleRandQuoteFactory;
162 }