]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Remove VF_SERVICEPROVIDER, prevent heap allocation of ConfigReader
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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
16 static FileReader *quotes = NULL;
17
18 std::string q_file;
19 std::string prefix;
20 std::string suffix;
21
22 /* $ModDesc: Provides random Quotes on Connect. */
23
24 /** Handle /RANDQUOTE
25  */
26 class CommandRandquote : public Command
27 {
28  public:
29         CommandRandquote(Module* Creator) : Command(Creator,"RANDQUOTE", 0)
30         {
31         }
32
33         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
34         {
35                 std::string str;
36                 int fsize;
37
38                 if (q_file.empty() || quotes->Exists())
39                 {
40                         fsize = quotes->FileSize();
41                         str = quotes->GetLine(rand() % fsize);
42                         user->WriteServ("NOTICE %s :%s%s%s",user->nick.c_str(),prefix.c_str(),str.c_str(),suffix.c_str());
43                 }
44                 else
45                 {
46                         user->WriteServ("NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick.c_str());
47                         return CMD_FAILURE;
48                 }
49
50                 return CMD_SUCCESS;
51         }
52 };
53
54 class ModuleRandQuote : public Module
55 {
56  private:
57         CommandRandquote cmd;
58  public:
59         ModuleRandQuote()
60                 : cmd(this)
61         {
62                 ConfigReader conf;
63                 // Sort the Randomizer thingie..
64                 srand(ServerInstance->Time());
65
66                 q_file = conf.ReadValue("randquote","file",0);
67                 prefix = conf.ReadValue("randquote","prefix",0);
68                 suffix = conf.ReadValue("randquote","suffix",0);
69
70                 if (q_file.empty())
71                 {
72                         throw ModuleException("m_randquote: Quotefile not specified - Please check your config.");
73                 }
74
75                 quotes = new FileReader(q_file);
76                 if(!quotes->Exists())
77                 {
78                         throw ModuleException("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
79                 }
80                 else
81                 {
82                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
83                         ServerInstance->AddCommand(&cmd);
84                 }
85                 Implementation eventlist[] = { I_OnUserConnect };
86                 ServerInstance->Modules->Attach(eventlist, this, 1);
87         }
88
89
90         virtual ~ModuleRandQuote()
91         {
92                 delete quotes;
93         }
94
95         virtual Version GetVersion()
96         {
97                 return Version("Provides random Quotes on Connect.",VF_VENDOR);
98         }
99
100         virtual void OnUserConnect(User* user)
101         {
102                 cmd.Handle(std::vector<std::string>(), user);
103         }
104 };
105
106 MODULE_INIT(ModuleRandQuote)