]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Possible Bugfix.. stop the IRCd from launching if the quotes file aint
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fstream>
4
5 #include "users.h"
6 #include "channels.h"
7 #include "modules.h"
8
9
10 /* $ModDesc: Provides random Quotes on Connect. */
11
12 class ModuleRandQuote : public Module
13 {
14  private:
15
16          Server *Srv;
17          ConfigReader *conf;
18          FileReader *quotes;
19
20          std::string q_file;
21          std::string prefix;
22          std::string suffix;
23          
24  public:
25         ModuleRandQuote()
26         {
27                 Srv = new Server;
28                 conf = new ConfigReader;
29
30                 if (conf->ReadValue("randquote","file",0) == '') {
31                         log(DEBUG,"m_randquote: startup: File Not Specified!, bailing!");
32                         return (ERROR);
33                 }
34
35                 q_file = conf->ReadValue("randquote","file",0);
36                 prefix = conf->ReadValue("randquote","prefix",0);
37                 suffix = conf->ReadValue("randquote","suffix",0);
38
39                 quotes = new FileReader(q_file);
40         }
41         
42         virtual ~ModuleRandQuote()
43         {
44                 delete Srv;
45                 delete conf;
46                 delete quotes;
47         }
48         
49         virtual Version GetVersion()
50         {
51                 return Version(1,0,0,0);
52         }
53         
54         virtual void OnUserConnect(userrec* user)
55         {
56                 std::string str;
57                 int fsize;
58                 char buf[MAXBUF];
59
60                 fsize = quotes->FileSize();
61                 srand(time(NULL));
62                 str = quotes->GetLine(rand() % fsize);
63                         
64                 sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
65                 Srv->SendServ(user->fd, buf);
66                 return;
67         }
68 };
69
70
71 class ModuleRandQuoteFactory : public ModuleFactory
72 {
73  public:
74         ModuleRandQuoteFactory()
75         {
76         }
77         
78         ~ModuleRandQuoteFactory()
79         {
80         }
81         
82         virtual Module * CreateModule()
83         {
84                 return new ModuleRandQuote;
85         }
86         
87 };
88
89
90 extern "C" void * init_module( void )
91 {
92         return new ModuleRandQuoteFactory;
93 }
94