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