]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
More tweaks from Eric, when an oper is invisible, attempting to /notice or /privmsg...
[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
56                 return CMD_LOCALONLY;
57         }
58 };
59
60 /** Thrown by m_randquote
61  */
62 class RandquoteException : public ModuleException
63 {
64  private:
65         const std::string err;
66  public:
67         RandquoteException(const std::string &message) : err(message) { }
68
69         ~RandquoteException() throw () { }
70
71         virtual const char* GetReason()
72         {
73                 return err.c_str();
74         }
75 };
76
77 class ModuleRandQuote : public Module
78 {
79  private:
80         cmd_randquote* mycommand;
81         ConfigReader *conf;
82  public:
83         ModuleRandQuote(InspIRCd* Me)
84                 : Module::Module(Me)
85         {
86                 
87                 conf = new ConfigReader(ServerInstance);
88                 // Sort the Randomizer thingie..
89                 srand(time(NULL));
90
91                 q_file = conf->ReadValue("randquote","file",0);
92                 prefix = conf->ReadValue("randquote","prefix",0);
93                 suffix = conf->ReadValue("randquote","suffix",0);
94
95                 mycommand = NULL;
96
97                 if (q_file == "")
98                 {
99                         RandquoteException e("m_randquote: Quotefile not specified - Please check your config.");
100                         throw(e);
101                 }
102
103                 quotes = new FileReader(ServerInstance, q_file);
104                 if(!quotes->Exists())
105                 {
106                         RandquoteException e("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
107                         throw(e);
108                 }
109                 else
110                 {
111                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
112                         mycommand = new cmd_randquote(ServerInstance);
113                         ServerInstance->AddCommand(mycommand);
114                 }
115         }
116
117         void Implements(char* List)
118         {
119                 List[I_OnUserConnect] = 1;
120         }
121         
122         virtual ~ModuleRandQuote()
123         {
124                 DELETE(conf);
125                 DELETE(quotes);
126         }
127         
128         virtual Version GetVersion()
129         {
130                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
131         }
132         
133         virtual void OnUserConnect(userrec* user)
134         {
135                 if (mycommand)
136                         mycommand->Handle(NULL, 0, user);
137         }
138 };
139
140
141 class ModuleRandQuoteFactory : public ModuleFactory
142 {
143  public:
144         ModuleRandQuoteFactory()
145         {
146         }
147         
148         ~ModuleRandQuoteFactory()
149         {
150         }
151         
152         virtual Module * CreateModule(InspIRCd* Me)
153         {
154                 return new ModuleRandQuote(Me);
155         }
156         
157 };
158
159
160 extern "C" void * init_module( void )
161 {
162         return new ModuleRandQuoteFactory;
163 }