]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ircv3_servertime.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_ircv3_servertime.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2018 Attila Molnar <attilamolnar@hush.com>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/ircv3.h"
23 #include "modules/ircv3_servertime.h"
24 #include "modules/server.h"
25
26 class ServerTimeTag
27         : public IRCv3::ServerTime::Manager
28         , public IRCv3::CapTag<ServerTimeTag>
29         , public ServerProtocol::MessageEventListener
30 {
31         time_t lasttime;
32         long lasttimens;
33         std::string lasttimestring;
34
35         void RefreshTimeString()
36         {
37                 const time_t currtime = ServerInstance->Time();
38                 const long currtimens = ServerInstance->Time_ns();
39                 if (currtime != lasttime || currtimens != lasttimens)
40                 {
41                         lasttime = currtime;
42                         lasttimens = currtimens;
43
44                         // Cache the string so it's not recreated every time a message is sent.
45                         lasttimestring = IRCv3::ServerTime::FormatTime(currtime, (currtimens ? currtimens / 1000000 : 0));
46                 }
47         }
48
49  public:
50         using ServerProtocol::MessageEventListener::OnBuildMessage;
51
52         ServerTimeTag(Module* mod)
53                 : IRCv3::ServerTime::Manager(mod)
54                 , IRCv3::CapTag<ServerTimeTag>(mod, "server-time", "time")
55                 , ServerProtocol::MessageEventListener(mod)
56                 , lasttime(0)
57                 , lasttimens(0)
58         {
59                 tagprov = this;
60         }
61
62         const std::string* GetValue(const ClientProtocol::Message& msg)
63         {
64                 // Client protocol.
65                 RefreshTimeString();
66                 return &lasttimestring;
67         }
68
69         void OnBuildMessage(User* source, const char* command, ClientProtocol::TagMap& tags) CXX11_OVERRIDE
70         {
71                 // Server protocol.
72                 RefreshTimeString();
73                 tags.insert(std::make_pair(tagname, ClientProtocol::MessageTagData(this, lasttimestring)));
74         }
75
76 };
77
78 class ModuleIRCv3ServerTime : public Module
79 {
80         ServerTimeTag tag;
81
82  public:
83         ModuleIRCv3ServerTime()
84                 : tag(this)
85         {
86         }
87
88         Version GetVersion() CXX11_OVERRIDE
89         {
90                 return Version("Provides the IRCv3 server-time client capability.", VF_VENDOR);
91         }
92 };
93
94 MODULE_INIT(ModuleIRCv3ServerTime)