]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
/LOADMODULE and /UNLOADMODULE all successfully working!
[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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <fstream>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23
24 Server *Srv;
25 FileReader *quotes;
26
27 std::string q_file;
28 std::string prefix;
29 std::string suffix;
30
31
32
33 /* $ModDesc: Provides random Quotes on Connect. */
34
35 void handle_randquote(char** parameters, int pcntl, userrec *user)
36 {
37         std::string str;
38         int fsize;
39         char buf[MAXBUF];
40
41         fsize = quotes->FileSize();
42         str = quotes->GetLine(rand() % fsize);
43
44         sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
45         Srv->SendServ(user->fd, buf);
46         return;
47 }
48
49
50
51
52 class ModuleRandQuote : public Module
53 {
54  private:
55
56         ConfigReader *conf;
57  public:
58         ModuleRandQuote()
59         {
60                 Srv = new Server;
61                 conf = new ConfigReader;
62                 // Sort the Randomizer thingie..
63                 srand(time(NULL));
64
65
66                 q_file = conf->ReadValue("randquote","file",0);
67                 prefix = conf->ReadValue("randquote","prefix",0);
68                 suffix = conf->ReadValue("randquote","suffix",0);
69
70                 if (q_file == "") {
71                         printf("m_randquote: Quotefile not specified.. Please check your config.\n\n");
72                         exit(0);
73                 }
74
75
76                 quotes = new FileReader(q_file);
77                 if(!quotes->Exists())
78                 {
79                         printf("m_randquote: QuoteFile not Found!! Please check your config.\n\n");
80                         exit(0);
81                 }
82                 /* Hidden Command -- Mode clients assume /quote sends raw data to an IRCd >:D */
83                 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 }