]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Combined The file Modules and Config file existance checkers into one function
[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
31                 q_file = conf->ReadValue("randquote","file",0);
32                 prefix = conf->ReadValue("randquote","prefix",0);
33                 suffix = conf->ReadValue("randquote","suffix",0);
34
35                 if (q_file == "") {
36                         printf("m_randquote: Quotefile not specified.. Please check your config.\n\n");
37                         exit(0);
38                 }
39
40
41                 quotes = new FileReader(q_file);
42                 if(!quotes->Exists())
43                 {
44                         printf("m_randquote: QuoteFile not Found!! Please check your config.\n\n");
45                         exit(0);
46                 }
47         }
48         
49         virtual ~ModuleRandQuote()
50         {
51                 delete Srv;
52                 delete conf;
53                 delete quotes;
54         }
55         
56         virtual Version GetVersion()
57         {
58                 return Version(1,0,0,0);
59         }
60         
61         virtual void OnUserConnect(userrec* user)
62         {
63                 std::string str;
64                 int fsize;
65                 char buf[MAXBUF];
66
67                 fsize = quotes->FileSize();
68                 srand(time(NULL));
69                 str = quotes->GetLine(rand() % fsize);
70                         
71                 sprintf(buf,"NOTICE %s :%s%s%s",user->nick,prefix.c_str(),str.c_str(),suffix.c_str());
72                 Srv->SendServ(user->fd, buf);
73                 return;
74         }
75 };
76
77
78 class ModuleRandQuoteFactory : public ModuleFactory
79 {
80  public:
81         ModuleRandQuoteFactory()
82         {
83         }
84         
85         ~ModuleRandQuoteFactory()
86         {
87         }
88         
89         virtual Module * CreateModule()
90         {
91                 return new ModuleRandQuote;
92         }
93         
94 };
95
96
97 extern "C" void * init_module( void )
98 {
99         return new ModuleRandQuoteFactory;
100 }
101