]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Re-added required parts of connection.h
[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 = NULL;
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 (quotes)
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         return;
49 }
50
51
52
53
54 class ModuleRandQuote : public Module
55 {
56  private:
57
58         ConfigReader *conf;
59  public:
60         ModuleRandQuote()
61         {
62                 Srv = new Server;
63                 conf = new ConfigReader;
64                 // Sort the Randomizer thingie..
65                 srand(time(NULL));
66
67                 q_file = conf->ReadValue("randquote","file",0);
68                 prefix = conf->ReadValue("randquote","prefix",0);
69                 suffix = conf->ReadValue("randquote","suffix",0);
70
71                 if (q_file == "") {
72                         log(DEFAULT,"m_randquote: Quotefile not specified - Please check your config.");
73                         return;
74                 }
75
76                 quotes = new FileReader(q_file);
77                 if(!quotes->Exists())
78                 {
79                         log(DEFAULT,"m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
80                         return;
81                 }
82                 /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
83                 else Srv->AddCommand("QUOTE",handle_randquote,0,0,"m_randquote.so");
84         }
85         
86         virtual ~ModuleRandQuote()
87         {
88                 delete Srv;
89                 delete conf;
90                 delete quotes;
91         }
92         
93         virtual Version GetVersion()
94         {
95                 return Version(1,0,0,1,VF_VENDOR);
96         }
97         
98         virtual void OnUserConnect(userrec* user)
99         {
100                 // Make a fake pointer to be passed to handle_randquote()
101                 // Dont try this at home kiddies :D
102                 char *rar = "RAR";
103                 handle_randquote(&rar, 0, user);
104         }
105 };
106
107
108 class ModuleRandQuoteFactory : public ModuleFactory
109 {
110  public:
111         ModuleRandQuoteFactory()
112         {
113         }
114         
115         ~ModuleRandQuoteFactory()
116         {
117         }
118         
119         virtual Module * CreateModule()
120         {
121                 return new ModuleRandQuote;
122         }
123         
124 };
125
126
127 extern "C" void * init_module( void )
128 {
129         return new ModuleRandQuoteFactory;
130 }