]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_randquote.cpp
Release v2.0.22
[user/henk/code/inspircd.git] / src / modules / m_randquote.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2003, 2006 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2005 Craig McLure <craig@chatspike.net>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 /* $ModDesc: Provides random quotes on connect. */
25
26 #include "inspircd.h"
27
28 static FileReader *quotes = NULL;
29
30 std::string prefix;
31 std::string suffix;
32
33 /** Handle /RANDQUOTE
34  */
35 class CommandRandquote : public Command
36 {
37  public:
38         CommandRandquote(Module* Creator) : Command(Creator,"RANDQUOTE", 0)
39         {
40         }
41
42         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
43         {
44                 int fsize = quotes->FileSize();
45                 if (fsize)
46                 {
47                         std::string str = quotes->GetLine(ServerInstance->GenRandomInt(fsize));
48                         if (!str.empty())
49                                 user->WriteServ("NOTICE %s :%s%s%s",user->nick.c_str(),prefix.c_str(),str.c_str(),suffix.c_str());
50                 }
51
52                 return CMD_SUCCESS;
53         }
54 };
55
56 class ModuleRandQuote : public Module
57 {
58  private:
59         CommandRandquote cmd;
60  public:
61         ModuleRandQuote()
62                 : cmd(this)
63         {
64         }
65
66         void init()
67         {
68                 ConfigTag* conf = ServerInstance->Config->ConfValue("randquote");
69
70                 std::string q_file = conf->getString("file","quotes");
71                 prefix = conf->getString("prefix");
72                 suffix = conf->getString("suffix");
73
74                 quotes = new FileReader(q_file);
75                 if (!quotes->Exists())
76                 {
77                         throw ModuleException("m_randquote: QuoteFile not Found!! Please check your config - module will not function.");
78                 }
79                 ServerInstance->Modules->AddService(cmd);
80                 Implementation eventlist[] = { I_OnUserConnect };
81                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
82         }
83
84
85         virtual ~ModuleRandQuote()
86         {
87                 delete quotes;
88         }
89
90         virtual Version GetVersion()
91         {
92                 return Version("Provides random quotes on connect.",VF_VENDOR);
93         }
94
95         virtual void OnUserConnect(LocalUser* user)
96         {
97                 cmd.Handle(std::vector<std::string>(), user);
98         }
99 };
100
101 MODULE_INIT(ModuleRandQuote)