forked from CyberShadow/DFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewsgroups.d
More file actions
282 lines (234 loc) · 6.44 KB
/
newsgroups.d
File metadata and controls
282 lines (234 loc) · 6.44 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* 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 newsgroups;
import std.string;
import std.conv;
import ae.utils.array : queuePop;
import ae.utils.container : HashSet;
import ae.utils.json;
import ae.net.nntp.client;
import ae.net.nntp.listener;
import ae.sys.timing;
import common;
import rfc850;
import database;
/// Poll the server periodically for new messages
class NntpListenerSource : NewsSource
{
this(string server)
{
super("NNTP-Listener");
this.server = server;
client = new NntpListener(log);
client.handleMessage = &onMessage;
}
override void start()
{
}
override void stop()
{
if (connected)
client.disconnect();
stopped = true;
}
/// Call this to start polling the server.
/// startTime is the timestamp (as returned by the
/// server DATE command) for the first poll cutoff time.
void startListening(string startTime=null)
{
if (!stopped)
{
client.connect(server);
connected = true;
client.startPolling(startTime);
}
}
private:
string server;
bool connected, stopped;
NntpListener client;
void onMessage(string[] lines, string num, string id)
{
announcePost(new Rfc850Post(lines.join("\n"), id));
}
}
/// Download articles not present in the database.
class NntpDownloader : NewsSource
{
enum Mode { newOnly, full, fullPurge }
NntpClient client;
this(string server, Mode mode)
{
super("NNTP-Downloader");
this.server = server;
this.mode = mode;
initialize();
}
override void start()
{
running = true;
log("Starting, mode is " ~ text(mode));
client.connect(server, &onConnect);
}
override void stop()
{
if (running)
{
running = false;
stopping = true;
log("Shutting down");
client.disconnect();
}
}
void delegate(string startTime) handleFinished;
private:
string server;
Mode mode;
bool running, stopping;
string startTime;
void onConnect()
{
if (stopping) return;
log("Listing groups...");
client.getDate((string date) { startTime = date; });
client.listGroups(&onGroups);
}
void onGroups(GroupInfo[] groups)
{
log(format("Got %d groups.", groups.length));
foreach (group; groups)
getGroup(group); // Own function for closure
client.handleIdle = &onIdle;
}
void getGroup(GroupInfo group)
{
// Get maximum article numbers before fetching messages -
// a cross-posted message might change a queued group's
// "maximum article number in database".
// The listGroup commands will be queued all together
// before any getMessage commands.
int maxNum = 0;
foreach (int num; query("SELECT MAX(`ArtNum`) FROM `Groups` WHERE `Group` = ?").iterate(group.name))
maxNum = num;
void onListGroup(string[] messages)
{
if (stopping) return;
log(format("%d messages in group %s.", messages.length, group.name));
HashSet!int serverMessages;
foreach (i, m; messages)
serverMessages.add(to!int(m));
HashSet!int localMessages;
foreach (int num; query("SELECT `ArtNum` FROM `Groups` WHERE `Group` = ?").iterate(group.name))
localMessages.add(num);
// Construct set of posts to download
HashSet!int messagesToDownload = serverMessages.dup;
foreach (num; localMessages)
if (num in messagesToDownload)
messagesToDownload.remove(num);
// Remove posts present in the database
if (messagesToDownload.length)
{
client.selectGroup(group.name);
foreach (num; messagesToDownload.keys.sort)
client.getMessage(to!string(num), &onMessage);
}
if (mode == Mode.fullPurge)
{
HashSet!int messagesToDelete = localMessages.dup;
foreach (num; serverMessages)
if (num in messagesToDelete)
messagesToDelete.remove(num);
enum PRETEND = false;
void logAndDelete(string TABLE, string WHERE, T...)(T args)
{
auto selectSql = "SELECT * FROM `" ~ TABLE ~ "` " ~ WHERE;
auto deleteSql = "DELETE FROM `" ~ TABLE ~ "` " ~ WHERE;
log(" " ~ deleteSql);
auto select = query(selectSql);
select.bindAll!T(args);
while (select.step())
log(" " ~ toJson(select.getAssoc()));
static if (!PRETEND)
query(deleteSql).exec(args);
}
foreach (num; messagesToDelete)
{
log((PRETEND ? "Would delete" : "Deleting") ~ " message: " ~ text(num));
mixin(DB_TRANSACTION);
string id;
foreach (string msgId; query("SELECT `ID` FROM `Groups` WHERE `Group` = ? AND `ArtNum` = ?").iterate(group.name, num))
id = msgId;
logAndDelete!(`Groups`, "WHERE `Group` = ? AND `ArtNum` = ?")(group.name, num);
if (id)
{
logAndDelete!(`Posts` , "WHERE `ID` = ?")(id);
logAndDelete!(`Threads`, "WHERE `ID` = ?")(id);
}
}
}
}
log(format("Listing group: %s", group.name));
if (mode == Mode.newOnly)
{
log(format("Highest article number in database: %d", maxNum));
if (group.high > maxNum)
{
// news.digitalmars.com doesn't support LISTGROUP ranges, use XOVER
client.listGroupXover(group.name, maxNum+1, &onListGroup);
}
}
else
client.listGroup(group.name, &onListGroup);
}
void onIdle()
{
log("All done!");
running = false;
client.handleIdle = null;
client.disconnect();
assert(startTime);
if (handleFinished)
handleFinished(startTime);
}
void onMessage(string[] lines, string num, string id)
{
log(format("Got message %s (%s)", num, id));
announcePost(new Rfc850Post(lines.join("\n"), id));
}
void onDisconnect(string reason, DisconnectType type)
{
if (running)
onError("Unexpected NntpDownloader disconnect: " ~ reason);
}
void onError(string msg)
{
log(msg);
setTimeout({ log("Retrying..."); restart(); }, TickDuration.from!"seconds"(10));
}
void initialize()
{
startTime = null;
client = new NntpClient(log);
client.handleDisconnect = &onDisconnect;
}
void restart()
{
if (stopping)
return;
initialize();
start();
}
}