]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
In the grand tradition of huge fucking commits:
[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
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 cmd_randquote : public Command
27 {
28  public:
29         cmd_randquote (InspIRCd* Instance) : Command(Instance,"RANDQUOTE", 0, 0)
30         {
31                 this->source = "m_randquote.so";
32         }
33
34         CmdResult Handle (const char** parameters, int pcntl, 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 /** Thrown by m_randquote
56  */
57 class RandquoteException : public ModuleException
58 {
59  private:
60         const std::string err;
61  public:
62         RandquoteException(const std::string &message) : err(message) { }
63
64         ~RandquoteException() throw () { }
65
66         virtual const char* GetReason()
67         {
68                 return err.c_str();
69         }
70 };
71
72 class ModuleRandQuote : public Module
73 {
74  private:
75         cmd_randquote* mycommand;
76         ConfigReader *conf;
77  public:
78         ModuleRandQuote(InspIRCd* Me)
79                 : Module(Me)
80         {
81                 
82                 conf = new ConfigReader(ServerInstance);
83                 // Sort the Randomizer thingie..
84                 srand(time(NULL));
85
86                 q_file = conf->ReadValue("randquote","file",0);
87                 prefix = conf->ReadValue("randquote","prefix",0);
88                 suffix = conf->ReadValue("randquote","suffix",0);
89
90                 mycommand = NULL;
91
92                 if (q_file.empty())
93                 {
94                         RandquoteException e("m_randquote: Quotefile not specified - Please check your config.");
95                         throw(e);
96                 }
97
98                 quotes = new FileReader(ServerInstance, q_file);
99                 if(!quotes->Exists())
100                 {
101                         RandquoteException e("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
102                         throw(e);
103                 }
104                 else
105                 {
106                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
107                         mycommand = new cmd_randquote(ServerInstance);
108                         ServerInstance->AddCommand(mycommand);
109                 }
110         }
111
112         void Implements(char* List)
113         {
114                 List[I_OnUserConnect] = 1;
115         }
116         
117         virtual ~ModuleRandQuote()
118         {
119                 DELETE(conf);
120                 DELETE(quotes);
121         }
122         
123         virtual Version GetVersion()
124         {
125                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
126         }
127         
128         virtual void OnUserConnect(User* user)
129         {
130                 if (mycommand)
131                         mycommand->Handle(NULL, 0, user);
132         }
133 };
134
135 MODULE_INIT(ModuleRandQuote)