]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Change 974 numeric to 490 to avoid collision with Insp's failed to load module error
[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 <stdio.h>
20 #include <stdlib.h>
21 #include <fstream>
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25 #include "helperfuncs.h"
26
27 Server *Srv;
28 FileReader *quotes = NULL;
29
30 std::string q_file = "";
31 std::string prefix = "";
32 std::string suffix = "";
33
34 /* $ModDesc: Provides random Quotes on Connect. */
35
36 class cmd_randquote : public command_t
37 {
38  public:
39         cmd_randquote () : command_t("RANDQUOTE", 0, 0)
40         {
41                 this->source = "m_randquote.so";
42         }
43
44         void Handle (char** parameters, int pcntl, userrec *user)
45         {
46                 std::string str;
47                 int fsize;
48                 char buf[MAXBUF];
49                 if (q_file == "" || quotes->Exists())
50                 {
51                         fsize = quotes->FileSize();
52                         str = quotes->GetLine(rand() % fsize);
53                         sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
54                         Srv->SendServ(user->fd, buf);
55                 }
56                 else
57                 {
58                         sprintf(buf, "NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
59                         Srv->SendServ(user->fd, buf);
60                 }
61                 return;
62         }
63 };
64
65
66 class ModuleRandQuote : public Module
67 {
68  private:
69         cmd_randquote* mycommand;
70         ConfigReader *conf;
71  public:
72         ModuleRandQuote(Server* Me)
73                 : Module::Module(Me)
74         {
75                 Srv = Me;
76                 conf = new ConfigReader;
77                 // Sort the Randomizer thingie..
78                 srand(time(NULL));
79
80                 q_file = conf->ReadValue("randquote","file",0);
81                 prefix = conf->ReadValue("randquote","prefix",0);
82                 suffix = conf->ReadValue("randquote","suffix",0);
83
84                 mycommand = NULL;
85
86                 if (q_file == "")
87                 {
88                         log(DEFAULT,"m_randquote: Quotefile not specified - Please check your config.");
89                         return;
90                 }
91
92                 quotes = new FileReader(q_file);
93                 if(!quotes->Exists())
94                 {
95                         log(DEFAULT,"m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
96                         return;
97                 }
98                 else
99                 {
100                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
101                         mycommand = new cmd_randquote();
102                         Srv->AddCommand(mycommand);
103                 }
104         }
105
106         void Implements(char* List)
107         {
108                 List[I_OnUserConnect] = 1;
109         }
110         
111         virtual ~ModuleRandQuote()
112         {
113                 delete conf;
114                 delete quotes;
115         }
116         
117         virtual Version GetVersion()
118         {
119                 return Version(1,0,0,1,VF_VENDOR);
120         }
121         
122         virtual void OnUserConnect(userrec* user)
123         {
124                 if (mycommand)
125                         mycommand->Handle(NULL, 0, user);
126         }
127 };
128
129
130 class ModuleRandQuoteFactory : public ModuleFactory
131 {
132  public:
133         ModuleRandQuoteFactory()
134         {
135         }
136         
137         ~ModuleRandQuoteFactory()
138         {
139         }
140         
141         virtual Module * CreateModule(Server* Me)
142         {
143                 return new ModuleRandQuote(Me);
144         }
145         
146 };
147
148
149 extern "C" void * init_module( void )
150 {
151         return new ModuleRandQuoteFactory;
152 }