]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
7354420a975041e6c8e0cb0ac62b03b387d0ee22
[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 /** Handle /RANDQUOTE
35  */
36 class cmd_randquote : public command_t
37 {
38  public:
39         cmd_randquote (InspIRCd* Instance) : command_t(Instance,"RANDQUOTE", 0, 0)
40         {
41                 this->source = "m_randquote.so";
42         }
43
44         CmdResult Handle (const char** parameters, int pcntl, userrec *user)
45         {
46                 std::string str;
47                 int fsize;
48
49                 if (q_file == "" || quotes->Exists())
50                 {
51                         fsize = quotes->FileSize();
52                         str = quotes->GetLine(rand() % fsize);
53                         user->WriteServ("NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
54                 }
55                 else
56                 {
57                         user->WriteServ("NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
58                         return CMD_FAILURE;
59                 }
60                 return CMD_SUCCESS;
61         }
62 };
63
64 /** Thrown by m_randquote
65  */
66 class RandquoteException : public ModuleException
67 {
68  private:
69         const std::string err;
70  public:
71         RandquoteException(const std::string &message) : err(message) { }
72
73         virtual const char* GetReason()
74         {
75                 return err.c_str();
76         }
77 };
78
79 class ModuleRandQuote : public Module
80 {
81  private:
82         cmd_randquote* mycommand;
83         ConfigReader *conf;
84  public:
85         ModuleRandQuote(InspIRCd* Me)
86                 : Module::Module(Me)
87         {
88                 
89                 conf = new ConfigReader(ServerInstance);
90                 // Sort the Randomizer thingie..
91                 srand(time(NULL));
92
93                 q_file = conf->ReadValue("randquote","file",0);
94                 prefix = conf->ReadValue("randquote","prefix",0);
95                 suffix = conf->ReadValue("randquote","suffix",0);
96
97                 mycommand = NULL;
98
99                 if (q_file == "")
100                 {
101                         RandquoteException e("m_randquote: Quotefile not specified - Please check your config.");
102                         throw(e);
103                 }
104
105                 quotes = new FileReader(ServerInstance, q_file);
106                 if(!quotes->Exists())
107                 {
108                         RandquoteException e("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
109                         throw(e);
110                 }
111                 else
112                 {
113                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
114                         mycommand = new cmd_randquote(ServerInstance);
115                         ServerInstance->AddCommand(mycommand);
116                 }
117         }
118
119         void Implements(char* List)
120         {
121                 List[I_OnUserConnect] = 1;
122         }
123         
124         virtual ~ModuleRandQuote()
125         {
126                 DELETE(conf);
127                 DELETE(quotes);
128         }
129         
130         virtual Version GetVersion()
131         {
132                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
133         }
134         
135         virtual void OnUserConnect(userrec* user)
136         {
137                 if (mycommand)
138                         mycommand->Handle(NULL, 0, user);
139         }
140 };
141
142
143 class ModuleRandQuoteFactory : public ModuleFactory
144 {
145  public:
146         ModuleRandQuoteFactory()
147         {
148         }
149         
150         ~ModuleRandQuoteFactory()
151         {
152         }
153         
154         virtual Module * CreateModule(InspIRCd* Me)
155         {
156                 return new ModuleRandQuote(Me);
157         }
158         
159 };
160
161
162 extern "C" void * init_module( void )
163 {
164         return new ModuleRandQuoteFactory;
165 }