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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
#include <view.h>
#include <controller.h>
#include <configparser.h>
#include <configcontainer.h>
#include <exceptions.h>
#include <downloadthread.h>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <sys/time.h>
#include <ctime>
#include <signal.h>
#include <nxml.h>
#include <sys/types.h>
#include <pwd.h>
#include <config.h>
using namespace noos;
static std::string lock_file = "lock.pid";
void ctrl_c_action(int /* unused */) {
stfl_reset();
::unlink(lock_file.c_str());
::exit(EXIT_FAILURE);
}
controller::controller() : v(0), rsscache(0), url_file("urls"), cache_file("cache.db"), config_file("config"), refresh_on_start(false) {
std::ostringstream cfgfile;
char * cfgdir;
if (!(cfgdir = ::getenv("HOME"))) {
struct passwd * spw = ::getpwuid(::getuid());
if (spw) {
cfgdir = spw->pw_dir;
} else {
std::cout << "Fatal error: couldn't determine home directory!" << std::endl;
std::cout << "Please set the HOME environment variable or add a valid user for UID " << ::getuid() << "!" << std::endl;
::exit(EXIT_FAILURE);
}
}
config_dir = cfgdir;
config_dir.append(NOOS_PATH_SEP);
config_dir.append(NOOS_CONFIG_SUBDIR);
mkdir(config_dir.c_str(),0700); // create configuration directory if it doesn't exist
url_file = config_dir + std::string(NOOS_PATH_SEP) + url_file;
cache_file = config_dir + std::string(NOOS_PATH_SEP) + cache_file;
config_file = config_dir + std::string(NOOS_PATH_SEP) + config_file;
lock_file = config_dir + std::string(NOOS_PATH_SEP) + lock_file;
reload_mutex = new mutex();
}
controller::~controller() {
if (rsscache)
delete rsscache;
delete reload_mutex;
}
void controller::set_view(view * vv) {
v = vv;
}
void controller::run(int argc, char * argv[]) {
int c;
::signal(SIGINT, ctrl_c_action);
::signal(SIGSEGV, ctrl_c_action);
bool do_import = false, do_export = false;
std::string importfile;
do {
if((c = ::getopt(argc,argv,"i:erhu:c:C:"))<0)
continue;
switch (c) {
case ':': /* fall-through */
case '?': /* missing option */
usage(argv[0]);
break;
case 'i':
if (do_export)
usage(argv[0]);
do_import = true;
importfile = optarg;
break;
case 'r':
refresh_on_start = true;
break;
case 'e':
if (do_import)
usage(argv[0]);
do_export = true;
break;
case 'h':
usage(argv[0]);
break;
case 'u':
url_file = optarg;
break;
case 'c':
cache_file = optarg;
break;
case 'C':
config_file = optarg;
break;
default:
std::cout << argv[0] << ": unknown option: -" << static_cast<char>(c) << std::endl;
usage(argv[0]);
break;
}
} while (c != -1);
urlcfg.load_config(url_file);
if (do_import) {
import_opml(importfile.c_str());
return;
}
if (urlcfg.get_urls().size() == 0) {
std::cout << "Error: no URLs configured. Please fill the file " << url_file << " with RSS feed URLs or import an OPML file." << std::endl << std::endl;
usage(argv[0]);
}
if (!do_export) {
std::cout << "Starting " << PROGRAM_NAME << " " << PROGRAM_VERSION << "..." << std::endl << std::endl;
pid_t pid;
if (!try_fs_lock(pid)) {
std::cout << "Error: an instance of " << PROGRAM_NAME << " is already running (PID: " << pid << ")" << std::endl;
return;
}
}
if (!do_export)
std::cout << "Loading configuration...";
std::cout.flush();
configparser cfgparser(config_file.c_str());
configcontainer cfg;
keymap keys;
cfg.register_commands(cfgparser);
cfgparser.register_handler("bind-key",&keys);
cfgparser.register_handler("unbind-key",&keys);
try {
cfgparser.parse();
} catch (const configexception& ex) {
std::cout << ex.what() << std::endl;
remove_fs_lock();
return;
}
if (!do_export)
std::cout << "done." << std::endl;
if (!do_export)
std::cout << "Loading articles from cache...";
std::cout.flush();
rsscache = new cache(cache_file,&cfg);
for (std::vector<std::string>::const_iterator it=urlcfg.get_urls().begin(); it != urlcfg.get_urls().end(); ++it) {
rss_feed feed(rsscache);
feed.set_rssurl(*it);
rsscache->internalize_rssfeed(feed);
feeds.push_back(feed);
}
// rsscache->cleanup_cache(feeds);
if (!do_export)
std::cout << "done." << std::endl;
if (do_export) {
export_opml();
remove_fs_lock();
return;
}
v->set_config_container(&cfg);
v->set_keymap(&keys);
v->set_feedlist(feeds);
v->run_feedlist();
std::cout << "Cleaning up cache...";
std::cout.flush();
rsscache->cleanup_cache(feeds);
/*
for (std::vector<rss_feed>::iterator it=feeds.begin(); it != feeds.end(); ++it) {
// rsscache->externalize_rssfeed(*it);
}
*/
std::cout << "done." << std::endl;
remove_fs_lock();
}
void controller::update_feedlist() {
v->set_feedlist(feeds);
}
bool controller::open_item(rss_item& item) {
bool show_next_unread = v->run_itemview(item);
item.set_unread(false); // XXX: see TODO list
return show_next_unread;
}
void controller::catchup_all() {
for (unsigned int i=0;i<feeds.size();++i) {
mark_all_read(i);
}
}
void controller::mark_all_read(unsigned int pos) {
if (pos < feeds.size()) {
rss_feed& feed = feeds[pos];
for (std::vector<rss_item>::iterator it = feed.items().begin(); it != feed.items().end(); ++it) {
it->set_unread(false);
}
}
}
void controller::open_feed(unsigned int pos) {
if (pos < feeds.size()) {
v->set_status("Opening feed...");
rss_feed& feed = feeds[pos];
v->set_status("");
if (feed.items().size() == 0) {
v->show_error("Error: feed contains no items!");
} else {
v->run_itemlist(pos);
// rsscache->externalize_rssfeed(feed); // save possibly changed unread flags
v->set_feedlist(feeds);
}
} else {
v->show_error("Error: invalid feed!");
}
}
void controller::reload(unsigned int pos, unsigned int max) {
if (pos < feeds.size()) {
rss_feed feed = feeds[pos];
std::string msg;
if (max > 0) {
msg.append("(");
std::ostringstream posstr;
posstr << (pos+1);
msg.append(posstr.str());
msg.append("/");
std::ostringstream maxstr;
maxstr << max;
msg.append(maxstr.str());
msg.append(") ");
}
msg.append("Loading ");
msg.append(feed.rssurl());
msg.append("...");
v->set_status(msg.c_str());
rss_parser parser(feed.rssurl().c_str(), rsscache);
feed = parser.parse();
/*
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
*/
rsscache->externalize_rssfeed(feed);
/*
gettimeofday(&tv2, NULL);
unsigned long long t1 = tv1.tv_sec*1000000 + tv1.tv_usec;
unsigned long long t2 = tv2.tv_sec*1000000 + tv2.tv_usec;
std::cerr << "time for externalizing: " << (t2-t1)/1000 << " ms" << std::endl;
*/
rsscache->internalize_rssfeed(feed);
feeds[pos] = feed;
v->set_status("");
v->set_feedlist(feeds);
} else {
v->show_error("Error: invalid feed!");
}
}
rss_feed& controller::get_feed(unsigned int pos) {
if (pos >= feeds.size()) {
// TODO: throw exception
}
return feeds[pos];
}
void controller::reload_all() {
for (unsigned int i=0;i<feeds.size();++i) {
this->reload(i,feeds.size());
}
}
void controller::start_reload_all_thread() {
if (reload_mutex->trylock()) {
thread * dlt = new downloadthread(this);
dlt->start();
}
}
void controller::usage(char * argv0) {
std::cout << PROGRAM_NAME << " " << PROGRAM_VERSION << std::endl;
std::cout << "usage: " << argv0 << " [-i <file>|-e] [-u <urlfile>] [-c <cachefile>] [-h]" << std::endl;
std::cout << "-e export OPML feed to stdout" << std::endl;
std::cout << "-r refresh feeds on start" << std::endl;
std::cout << "-i <file> import OPML file" << std::endl;
std::cout << "-u <urlfile> read RSS feed URLs from <urlfile>" << std::endl;
std::cout << "-c <cachefile> use <cachefile> as cache file" << std::endl;
std::cout << "-C <configfile> read configuration from <configfile>" << std::endl;
std::cout << "-h this help" << std::endl;
::exit(EXIT_FAILURE);
}
void controller::import_opml(const char * filename) {
nxml_t *data;
nxml_data_t * root, * body;
nxml_error_t ret;
ret = nxml_new (&data);
if (ret != NXML_OK) {
puts (nxml_strerror (ret)); // TODO
return;
}
ret = nxml_parse_file (data, const_cast<char *>(filename));
if (ret != NXML_OK) {
puts (nxml_strerror (ret)); // TODO
return;
}
nxml_root_element (data, &root);
if (root) {
body = nxmle_find_element(data, root, "body", NULL);
if (body) {
rec_find_rss_outlines(body);
urlcfg.write_config();
}
}
nxml_free(data);
std::cout << "Import of " << filename << " finished." << std::endl;
}
void controller::export_opml() {
std::cout << "<?xml version=\"1.0\"?>" << std::endl;
std::cout << "<opml version=\"1.0\">" << std::endl;
std::cout << "\t<head>" << std::endl << "\t\t<title>" PROGRAM_NAME " - Exported Feeds</title>" << std::endl << "\t</head>" << std::endl;
std::cout << "\t<body>" << std::endl;
for (std::vector<rss_feed>::iterator it=feeds.begin(); it != feeds.end(); ++it) {
std::cout << "\t\t<outline type=\"rss\" xmlUrl=\"" << it->rssurl() << "\" title=\"" << it->title() << "\" />" << std::endl;
}
std::cout << "\t</body>" << std::endl;
std::cout << "</opml>" << std::endl;
}
void controller::rec_find_rss_outlines(nxml_data_t * node) {
while (node) {
char * url = nxmle_find_attribute(node, "xmlUrl", NULL);
char * type = nxmle_find_attribute(node, "type", NULL);
if (node->type == NXML_TYPE_ELEMENT && strcmp(node->value,"outline")==0 && type && strcmp(type,"rss")==0 && url) {
bool found = false;
for (std::vector<std::string>::iterator it = urlcfg.get_urls().begin(); it != urlcfg.get_urls().end(); ++it) {
if (*it == url) {
found = true;
}
}
if (!found) {
urlcfg.get_urls().push_back(std::string(url));
}
}
rec_find_rss_outlines(node->children);
node = node->next;
}
}
void controller::remove_fs_lock() {
::unlink(lock_file.c_str());
}
bool controller::try_fs_lock(pid_t & pid) {
if (::access(lock_file.c_str(), F_OK)==0) {
std::fstream f(lock_file.c_str(), std::fstream::in);
f >> pid;
return false;
}
std::fstream f(lock_file.c_str(), std::fstream::out);
f << ::getpid();
f.close();
return true;
}
|