forked from CyberShadow/DFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircsink.d
114 lines (97 loc) · 3.2 KB
/
ircsink.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Copyright (C) 2011, 2012 Vladimir Panteleev <vladimir@thecybershadow.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
module ircsink;
import std.string;
import std.datetime;
import std.file;
import ae.net.asockets;
import ae.net.irc.client;
import ae.sys.timing;
import ae.sys.shutdown;
import ae.utils.text;
import common;
alias core.time.TickDuration TickDuration;
//const FORMAT = "PRIVMSG %s :\x01ACTION %s\x01";
const FORMAT = "PRIVMSG %s :\x01ACTION \x0314%s\x01";
final class IrcSink : NewsSink
{
string server, nick, channel, channel2;
this()
{
// Note to hackers: unless you want to work on IRC code, don't create
// this configuration file to get DFeed to work - use e.g. dfeed_web instead.
auto configLines = readText("data/irc.txt").splitLines();
server = configLines[0];
nick = configLines[1];
channel = configLines[2];
channel2 = configLines[3];
conn = new IrcClient();
conn.encoder = conn.decoder = &nullStringTransform;
conn.exactNickname = true;
conn.log = createLogger("IRC");
conn.handleConnect = &onConnect;
conn.handleDisconnect = &onDisconnect;
connect();
addShutdownHandler({ stopping = true; if (connecting || connected) conn.disconnect("DFeed shutting down"); });
}
protected:
override void handlePost(Post post)
{
if (post.time < Clock.currTime() - dur!"days"(1))
return; // ignore posts older than a day old (e.g. StackOverflow question activity bumps the questions)
bool important = post.isImportant();
if (important || haveUnimportantListeners())
{
post.formatForIRC((string summary) {
if (connected)
{
summary = summary.newlinesToSpaces();
conn.sendRaw(format(FORMAT, channel2, summary));
if (important)
conn.sendRaw(format(FORMAT, channel, summary));
}
});
}
}
private:
IrcClient conn;
bool connecting, connected, stopping;
void connect()
{
conn.connect(nick, "https://github.com/CyberShadow/DFeed", server);
connecting = true;
}
void onConnect(IrcClient sender)
{
connecting = false;
conn.join(channel);
conn.join(channel2);
connected = true;
}
void onDisconnect(IrcClient sender, string reason, DisconnectType type)
{
connecting = connected = false;
if (type != DisconnectType.Requested && !stopping)
setTimeout(&connect, TickDuration.from!"seconds"(10));
}
/// This function exists for the sole reason of avoiding creation of
/// shortened URLs (thus, needlessly polluting bit.ly) when no one
/// will be there to see them.
bool haveUnimportantListeners()
{
return (channel2 in conn.channels) && conn.channels[channel2].users.length > 1;
}
}