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