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