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