]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Add extra /map info (connection uptime, and lag time) to /MAP for opers. Adds feature...
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17
18 #include "inspircd.h"
19
20
21 static FileReader *quotes = NULL;
22
23 std::string q_file = "";
24 std::string prefix = "";
25 std::string suffix = "";
26
27 /* $ModDesc: Provides random Quotes on Connect. */
28
29 /** Handle /RANDQUOTE
30  */
31 class cmd_randquote : public command_t
32 {
33  public:
34         cmd_randquote (InspIRCd* Instance) : command_t(Instance,"RANDQUOTE", 0, 0)
35         {
36                 this->source = "m_randquote.so";
37         }
38
39         CmdResult Handle (const char** parameters, int pcntl, userrec *user)
40         {
41                 std::string str;
42                 int fsize;
43
44                 if (q_file == "" || quotes->Exists())
45                 {
46                         fsize = quotes->FileSize();
47                         str = quotes->GetLine(rand() % fsize);
48                         user->WriteServ("NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
49                 }
50                 else
51                 {
52                         user->WriteServ("NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
53                         return CMD_FAILURE;
54                 }
55                 return CMD_SUCCESS;
56         }
57 };
58
59 /** Thrown by m_randquote
60  */
61 class RandquoteException : public ModuleException
62 {
63  private:
64         const std::string err;
65  public:
66         RandquoteException(const std::string &message) : err(message) { }
67
68         ~RandquoteException() throw () { }
69
70         virtual const char* GetReason()
71         {
72                 return 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,1,0,1,VF_VENDOR,API_VERSION);
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 }