]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Conversions
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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
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 (InspIRCd* Instance) : Command(Instance,"RANDQUOTE", 0, 0)
30         {
31                 this->source = "m_randquote.so";
32         }
33
34         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
35         {
36                 std::string str;
37                 int fsize;
38
39                 if (q_file.empty() || quotes->Exists())
40                 {
41                         fsize = quotes->FileSize();
42                         str = quotes->GetLine(rand() % fsize);
43                         user->WriteServ("NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
44                 }
45                 else
46                 {
47                         user->WriteServ("NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
48                         return CMD_FAILURE;
49                 }
50
51                 return CMD_LOCALONLY;
52         }
53 };
54
55 class ModuleRandQuote : public Module
56 {
57  private:
58         CommandRandquote* mycommand;
59         ConfigReader *conf;
60  public:
61         ModuleRandQuote(InspIRCd* Me)
62                 : Module(Me)
63         {
64                 
65                 conf = new ConfigReader(ServerInstance);
66                 // Sort the Randomizer thingie..
67                 srand(time(NULL));
68
69                 q_file = conf->ReadValue("randquote","file",0);
70                 prefix = conf->ReadValue("randquote","prefix",0);
71                 suffix = conf->ReadValue("randquote","suffix",0);
72
73                 mycommand = NULL;
74
75                 if (q_file.empty())
76                 {
77                         throw ModuleException("m_randquote: Quotefile not specified - Please check your config.");
78                 }
79
80                 quotes = new FileReader(ServerInstance, q_file);
81                 if(!quotes->Exists())
82                 {
83                         throw ModuleException("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
84                 }
85                 else
86                 {
87                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
88                         mycommand = new CommandRandquote(ServerInstance);
89                         ServerInstance->AddCommand(mycommand);
90                 }
91                 Implementation eventlist[] = { I_OnUserConnect };
92                 ServerInstance->Modules->Attach(eventlist, this, 1);
93         }
94
95         
96         virtual ~ModuleRandQuote()
97         {
98                 delete conf;
99                 delete quotes;
100         }
101         
102         virtual Version GetVersion()
103         {
104                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
105         }
106         
107         virtual void OnUserConnect(User* user)
108         {
109                 if (mycommand)
110                         mycommand->Handle(std::vector<std::string>(), user);
111         }
112 };
113
114 MODULE_INIT(ModuleRandQuote)