]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
64c967ae81edb691105bf52190bb8e2a0ebe906a
[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 static Server *Srv;
28 static 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 class RandquoteException : public ModuleException
66 {
67  private:
68         std::string err;
69  public:
70         RandquoteException(std::string message) : err(message) { }
71
72         virtual const char* GetReason()
73         {
74                 return (char*)err.c_str();
75         }
76 };
77
78 class ModuleRandQuote : public Module
79 {
80  private:
81         cmd_randquote* mycommand;
82         ConfigReader *conf;
83  public:
84         ModuleRandQuote(Server* Me)
85                 : Module::Module(Me)
86         {
87                 Srv = Me;
88                 conf = new ConfigReader;
89                 // Sort the Randomizer thingie..
90                 srand(time(NULL));
91
92                 q_file = conf->ReadValue("randquote","file",0);
93                 prefix = conf->ReadValue("randquote","prefix",0);
94                 suffix = conf->ReadValue("randquote","suffix",0);
95
96                 mycommand = NULL;
97
98                 if (q_file == "")
99                 {
100                         RandquoteException e("m_randquote: Quotefile not specified - Please check your config.");
101                         throw(e);
102                 }
103
104                 quotes = new FileReader(q_file);
105                 if(!quotes->Exists())
106                 {
107                         RandquoteException e("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
108                         throw(e);
109                 }
110                 else
111                 {
112                         /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
113                         mycommand = new cmd_randquote();
114                         Srv->AddCommand(mycommand);
115                 }
116         }
117
118         void Implements(char* List)
119         {
120                 List[I_OnUserConnect] = 1;
121         }
122         
123         virtual ~ModuleRandQuote()
124         {
125                 DELETE(conf);
126                 DELETE(quotes);
127         }
128         
129         virtual Version GetVersion()
130         {
131                 return Version(1,0,0,1,VF_VENDOR);
132         }
133         
134         virtual void OnUserConnect(userrec* user)
135         {
136                 if (mycommand)
137                         mycommand->Handle(NULL, 0, user);
138         }
139 };
140
141
142 class ModuleRandQuoteFactory : public ModuleFactory
143 {
144  public:
145         ModuleRandQuoteFactory()
146         {
147         }
148         
149         ~ModuleRandQuoteFactory()
150         {
151         }
152         
153         virtual Module * CreateModule(Server* Me)
154         {
155                 return new ModuleRandQuote(Me);
156         }
157         
158 };
159
160
161 extern "C" void * init_module( void )
162 {
163         return new ModuleRandQuoteFactory;
164 }