]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
2ad7831ce90f0f5aba9e3a1b6d32c9b362b92f39
[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 "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 static FileReader *quotes = NULL;
20
21 std::string q_file;
22 std::string prefix;
23 std::string suffix;
24
25 /* $ModDesc: Provides random Quotes on Connect. */
26
27 /** Handle /RANDQUOTE
28  */
29 class cmd_randquote : public command_t
30 {
31  public:
32         cmd_randquote (InspIRCd* Instance) : command_t(Instance,"RANDQUOTE", 0, 0)
33         {
34                 this->source = "m_randquote.so";
35         }
36
37         CmdResult Handle (const char** parameters, int pcntl, userrec *user)
38         {
39                 std::string str;
40                 int fsize;
41
42                 if (q_file.empty() || quotes->Exists())
43                 {
44                         fsize = quotes->FileSize();
45                         str = quotes->GetLine(rand() % fsize);
46                         user->WriteServ("NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
47                 }
48                 else
49                 {
50                         user->WriteServ("NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
51                         return CMD_FAILURE;
52                 }
53
54                 return CMD_LOCALONLY;
55         }
56 };
57
58 /** Thrown by m_randquote
59  */
60 class RandquoteException : public ModuleException
61 {
62  private:
63         const std::string err;
64  public:
65         RandquoteException(const std::string &message) : err(message) { }
66
67         ~RandquoteException() throw () { }
68
69         virtual const char* GetReason()
70         {
71                 return err.c_str();
72         }
73 };
74
75 class ModuleRandQuote : public Module
76 {
77  private:
78         cmd_randquote* mycommand;
79         ConfigReader *conf;
80  public:
81         ModuleRandQuote(InspIRCd* Me)
82                 : Module(Me)
83         {
84                 
85                 conf = new ConfigReader(ServerInstance);
86                 // Sort the Randomizer thingie..
87                 srand(time(NULL));
88
89                 q_file = conf->ReadValue("randquote","file",0);
90                 prefix = conf->ReadValue("randquote","prefix",0);
91                 suffix = conf->ReadValue("randquote","suffix",0);
92
93                 mycommand = NULL;
94
95                 if (q_file.empty())
96                 {
97                         RandquoteException e("m_randquote: Quotefile not specified - Please check your config.");
98                         throw(e);
99                 }
100
101                 quotes = new FileReader(ServerInstance, q_file);
102                 if(!quotes->Exists())
103                 {
104                         RandquoteException e("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
105                         throw(e);
106                 }
107                 else
108                 {
109                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
110                         mycommand = new cmd_randquote(ServerInstance);
111                         ServerInstance->AddCommand(mycommand);
112                 }
113         }
114
115         void Implements(char* List)
116         {
117                 List[I_OnUserConnect] = 1;
118         }
119         
120         virtual ~ModuleRandQuote()
121         {
122                 DELETE(conf);
123                 DELETE(quotes);
124         }
125         
126         virtual Version GetVersion()
127         {
128                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
129         }
130         
131         virtual void OnUserConnect(userrec* user)
132         {
133                 if (mycommand)
134                         mycommand->Handle(NULL, 0, user);
135         }
136 };
137
138 MODULE_INIT(ModuleRandQuote)