]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
f75d3f1aaf7b0eddc4c1a6f1a8f27225fc388faf
[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 void handle_randquote(char** parameters, int pcntl, userrec *user)
37 {
38         std::string str;
39         int fsize;
40         char buf[MAXBUF];
41         if (q_file == "" || quotes->Exists())
42         {
43                 fsize = quotes->FileSize();
44                 str = quotes->GetLine(rand() % fsize);
45                 sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
46                 Srv->SendServ(user->fd, buf);
47         }
48         else
49         {
50                 sprintf(buf, "NOTICE %s :Your administrator specified an invalid quotes file, please bug them about this.", user->nick);
51                 Srv->SendServ(user->fd, buf);
52         }
53         return;
54 }
55
56
57
58
59 class ModuleRandQuote : public Module
60 {
61  private:
62
63         ConfigReader *conf;
64  public:
65         ModuleRandQuote(Server* Me)
66                 : Module::Module(Me)
67         {
68                 Srv = Me;
69                 conf = new ConfigReader;
70                 // Sort the Randomizer thingie..
71                 srand(time(NULL));
72
73                 q_file = conf->ReadValue("randquote","file",0);
74                 prefix = conf->ReadValue("randquote","prefix",0);
75                 suffix = conf->ReadValue("randquote","suffix",0);
76
77                 if (q_file == "")
78                 {
79                         log(DEFAULT,"m_randquote: Quotefile not specified - Please check your config.");
80                         return;
81                 }
82
83                 quotes = new FileReader(q_file);
84                 if(!quotes->Exists())
85                 {
86                         log(DEFAULT,"m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
87                         return;
88                 }
89                 /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
90                 else Srv->AddCommand("QUOTE",handle_randquote,0,0,"m_randquote.so");
91         }
92         
93         virtual ~ModuleRandQuote()
94         {
95                 delete conf;
96                 delete quotes;
97         }
98         
99         virtual Version GetVersion()
100         {
101                 return Version(1,0,0,1,VF_VENDOR);
102         }
103         
104         virtual void OnUserConnect(userrec* user)
105         {
106                 // Make a fake pointer to be passed to handle_randquote()
107                 // Dont try this at home kiddies :D
108                 /* Or do things a slightly nicer way, and pass NULL */
109                 //char *rar = "RAR";
110                 handle_randquote(NULL, 0, user);
111         }
112 };
113
114
115 class ModuleRandQuoteFactory : public ModuleFactory
116 {
117  public:
118         ModuleRandQuoteFactory()
119         {
120         }
121         
122         ~ModuleRandQuoteFactory()
123         {
124         }
125         
126         virtual Module * CreateModule(Server* Me)
127         {
128                 return new ModuleRandQuote(Me);
129         }
130         
131 };
132
133
134 extern "C" void * init_module( void )
135 {
136         return new ModuleRandQuoteFactory;
137 }