]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
New 'Implements' system
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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         virtual ~ModuleRandQuote()
107         {
108                 delete conf;
109                 delete quotes;
110         }
111         
112         virtual Version GetVersion()
113         {
114                 return Version(1,0,0,1,VF_VENDOR);
115         }
116         
117         virtual void OnUserConnect(userrec* user)
118         {
119                 if (mycommand)
120                         mycommand->Handle(NULL, 0, user);
121         }
122 };
123
124
125 class ModuleRandQuoteFactory : public ModuleFactory
126 {
127  public:
128         ModuleRandQuoteFactory()
129         {
130         }
131         
132         ~ModuleRandQuoteFactory()
133         {
134         }
135         
136         virtual Module * CreateModule(Server* Me)
137         {
138                 return new ModuleRandQuote(Me);
139         }
140         
141 };
142
143
144 extern "C" void * init_module( void )
145 {
146         return new ModuleRandQuoteFactory;
147 }