aboutsummaryrefslogtreecommitdiff
path: root/src/controller.cpp
blob: 2192793a2ff59305ea9496045c6b7b96e32602e3 (plain) (blame)
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
#define ENABLE_IMPLICIT_FILEPATH_CONVERSIONS

#include "controller.h"

#include <cassert>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <curl/curl.h>
#include <fstream>
#include <iostream>
#include <langinfo.h>
#include <libgen.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlsave.h>
#include <libxml/xmlversion.h>
#include <memory>
#include <pwd.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "cliargsparser.h"
#include "colormanager.h"
#include "config.h"
#include "configcontainer.h"
#include "configexception.h"
#include "configpaths.h"
#include "dbexception.h"
#include "exception.h"
#include "feedhqapi.h"
#include "feedhqurlreader.h"
#include "formaction.h"
#include "feedbinapi.h"
#include "feedbinurlreader.h"
#include "freshrssapi.h"
#include "freshrssurlreader.h"
#include "fileurlreader.h"
#include "inoreaderapi.h"
#include "inoreaderurlreader.h"
#include "itemrenderer.h"
#include "logger.h"
#include "minifluxapi.h"
#include "minifluxurlreader.h"
#include "newsblurapi.h"
#include "newsblururlreader.h"
#include "ocnewsapi.h"
#include "ocnewsurlreader.h"
#include "oldreaderapi.h"
#include "oldreaderurlreader.h"
#include "opml.h"
#include "opmlurlreader.h"
#include "regexmanager.h"
#include "remoteapi.h"
#include "rssfeed.h"
#include "rssparser.h"
#include "scopemeasure.h"
#include "stflpp.h"
#include "strprintf.h"
#include "ttrssapi.h"
#include "ttrssurlreader.h"
#include "utils.h"
#include "view.h"

namespace newsboat {

void sighup_action(int /* sig */)
{
	LOG(Level::DEBUG, "caught SIGHUP");
	Stfl::reset();
	::exit(EXIT_FAILURE);
}

void ignore_signal(int sig)
{
	LOG(Level::WARN, "caught signal %d but ignored it", sig);
}

Controller::Controller(ConfigPaths& configpaths)
	: v(0)
	, urlcfg(0)
	, rsscache(0)
	, refresh_on_start(false)
	, api(0)
	, configpaths(configpaths)
	, queueManager(&cfg, configpaths.queue_file())
{
}

Controller::~Controller()
{
	delete rsscache;
	delete urlcfg;
	delete api;
}

void Controller::set_view(View* vv)
{
	v = vv;
}

int Controller::run(const CliArgsParser& args)
{
	::signal(SIGINT, View::ctrl_c_action);
	::signal(SIGPIPE, ignore_signal);
	::signal(SIGHUP, sighup_action);

	refresh_on_start = args.refresh_on_start();

	if (args.log_level().has_value()) {
		logger::set_loglevel(args.log_level().value());
	}

	if (args.log_file().has_value()) {
		logger::set_logfile(args.log_file().value());
	}

	if (!args.log_file().has_value() && args.log_level().has_value()) {
		const std::string date_time_string = utils::mt_strf_localtime("%Y-%m-%d_%H.%M.%S",
				std::time(nullptr));
		const std::string filename = "newsboat_" + date_time_string + ".log";
		const auto filepath = Filepath::from_locale_string(filename);
		logger::set_logfile(filepath);
	}

	if (!args.display_msg().empty()) {
		std::cerr << args.display_msg() << std::endl;
	}

	if (args.return_code().has_value()) {
		return args.return_code().value();
	}

	const auto migrated = configpaths.try_migrate_from_newsbeuter();
	if (migrated) {
		std::cerr << "\nPlease check the results and press Enter to "
			"continue.";
		std::cin.ignore();
	}

	if (!configpaths.create_dirs()) {
		return EXIT_FAILURE;
	}

	if (args.do_import()) {
		LOG(Level::INFO, "Importing OPML file from %s", args.importfile());
		return import_opml(args.importfile(), configpaths.url_file());
	}

	LOG(Level::INFO, "nl_langinfo(CODESET): %s", nl_langinfo(CODESET));

	if (!args.do_export()) {
		if (!args.silent())
			std::cout << strprintf::fmt(_("Starting %s %s..."),
					PROGRAM_NAME,
					utils::program_version())
				<< std::endl;
	}

	if (!args.silent()) {
		std::cout << _("Loading configuration...");
	}
	std::cout.flush();

	cfg.register_commands(cfgparser);
	colorman.register_commands(cfgparser);

	KeyMap keys(KM_NEWSBOAT);
	cfgparser.register_handler("bind-key", keys);
	cfgparser.register_handler("unbind-key", keys);
	cfgparser.register_handler("macro", keys);
	cfgparser.register_handler("run-on-startup", keys);

	cfgparser.register_handler("ignore-article", ign);
	cfgparser.register_handler("always-download", ign);
	cfgparser.register_handler("reset-unread-on-update", ign);

	cfgparser.register_handler("define-filter", filters);
	cfgparser.register_handler("highlight", rxman);
	cfgparser.register_handler("highlight-article", rxman);
	cfgparser.register_handler("highlight-feed", rxman);

	try {
		cfgparser.parse_file(Filepath::from_locale_string("/etc/" PACKAGE "/config"));
		cfgparser.parse_file(configpaths.config_file());
	} catch (const ConfigException& ex) {
		LOG(Level::ERROR,
			"an exception occurred while parsing the configuration "
			"file: %s",
			ex.what());
		std::cout << ex.what() << std::endl;
		return EXIT_FAILURE;
	}

	update_config();

	if (!args.silent()) {
		std::cout << _("done.") << std::endl;
	}

	// create cache object
	std::string cachefilepath = cfg.get_configvalue("cache-file");
	if (cachefilepath.length() > 0 && !args.cache_file().has_value()) {
		configpaths.set_cache_file(cachefilepath);
	}

	pid_t pid;
	std::string error;
	if (!fslock.try_lock(configpaths.lock_file(), pid, error)) {
		if (pid != 0) {
			std::cout << strprintf::fmt(
					_("Error: an instance of %s is "
						"already running (PID: %s)"),
					PROGRAM_NAME,
					std::to_string(pid))
				<< std::endl;
		} else {
			std::cout << _("Error: ") << error << std::endl;
		}
		return EXIT_FAILURE;
	}

	if (!args.silent()) {
		std::cout << _("Opening cache...");
		std::cout.flush();
	}
	try {
		rsscache = new Cache(configpaths.cache_file(), cfg);
	} catch (const DbException& e) {
		std::cerr << strprintf::fmt(
				_("Error: opening the cache file `%s' "
					"failed: %s"),
				configpaths.cache_file(),
				e.what())
			<< std::endl;
		return EXIT_FAILURE;
	} catch (const std::runtime_error& e) {
		std::cerr << strprintf::fmt(
				_("Error: opening the cache file `%s' "
					"failed: %s"),
				configpaths.cache_file(),
				e.what())
			<< std::endl;
		return EXIT_FAILURE;
	}

	if (!args.silent()) {
		std::cout << _("done.") << std::endl;
	}

	reloader = std::make_unique<Reloader>(this, rsscache, cfg);

	std::string type = cfg.get_configvalue("urls-source");
	if (type == "local") {
		urlcfg = new FileUrlReader(configpaths.url_file());
	} else if (type == "opml") {
		urlcfg = new OpmlUrlReader(cfg);
	} else if (type == "oldreader") {
		api = new OldReaderApi(cfg);
		urlcfg = new OldReaderUrlReader(
			&cfg, configpaths.url_file(), api);
	} else if (type == "ttrss") {
		api = new TtRssApi(cfg);
		urlcfg = new TtRssUrlReader(configpaths.url_file(), api);
	} else if (type == "newsblur") {
		const auto cookies = cfg.get_configvalue("cookie-cache");
		if (cookies.empty()) {
			std::cout << strprintf::fmt(
					_("ERROR: You must set `cookie-cache` to use "
						"NewsBlur.\n"));
			return EXIT_FAILURE;
		}

		std::ofstream check(cookies);
		if (!check.is_open()) {
			std::cout << strprintf::fmt(
					_("%s is inaccessible and can't be created\n"),
					cookies);
			return EXIT_FAILURE;
		}

		api = new NewsBlurApi(cfg);
		urlcfg = new NewsBlurUrlReader(configpaths.url_file(), api);
	} else if (type == "feedhq") {
		api = new FeedHqApi(cfg);
		urlcfg = new FeedHqUrlReader(&cfg, configpaths.url_file(), api);
	} else if (type == "feedbin") {
		const std::string user = cfg.get_configvalue("feedbin-login");
		const std::string pass = cfg.get_configvalue("feedbin-password");
		const std::string pass_file = cfg.get_configvalue("feedbin-passwordfile");
		const std::string pass_eval = cfg.get_configvalue("feedbin-passwordeval");
		const bool creds_set = !user.empty() &&
			(!pass.empty() || !pass_file.empty() || !pass_eval.empty());
		if (!creds_set) {
			std::cerr <<
				_("ERROR: You must set `feedbin-login` and one of `feedbin-password`, "
					"`feedbin-passwordfile` or `feedbin-passwordeval` to use "
					"Feedbin\n");
			return EXIT_FAILURE;
		}

		api = new FeedbinApi(cfg);
		urlcfg = new FeedbinUrlReader(configpaths.url_file(), api);
	} else if (type == "freshrss") {
		const auto freshrss_url = cfg.get_configvalue("freshrss-url");
		if (freshrss_url.empty()) {
			std::cerr <<
				_("ERROR: You must set `freshrss-url` to use FreshRSS\n");
			return EXIT_FAILURE;
		}

		const std::string user = cfg.get_configvalue("freshrss-login");
		const std::string pass = cfg.get_configvalue("freshrss-password");
		const std::string pass_file = cfg.get_configvalue("freshrss-passwordfile");
		const std::string pass_eval = cfg.get_configvalue("freshrss-passwordeval");
		const bool creds_set = !user.empty() &&
			(!pass.empty() || !pass_file.empty() || !pass_eval.empty());
		if (!creds_set) {
			std::cerr <<
				_("ERROR: You must set `freshrss-login` and one of `freshrss-password`, "
					"`freshrss-passwordfile` or `freshrss-passwordeval` to use "
					"FreshRSS\n");
			return EXIT_FAILURE;
		}

		api = new FreshRssApi(cfg);
		urlcfg = new FreshRssUrlReader(&cfg, configpaths.url_file(), api);
	} else if (type == "ocnews") {
		api = new OcNewsApi(cfg);
		urlcfg = new OcNewsUrlReader(configpaths.url_file(), api);
	} else if (type == "miniflux") {
		const auto miniflux_url = cfg.get_configvalue("miniflux-url");
		if (miniflux_url.empty()) {
			std::cerr <<
				_("ERROR: You must set `miniflux-url` to use Miniflux\n");
			return EXIT_FAILURE;
		}

		const std::string user = cfg.get_configvalue("miniflux-login");
		const std::string pass = cfg.get_configvalue("miniflux-password");
		const std::string pass_file = cfg.get_configvalue("miniflux-passwordfile");
		const std::string pass_eval = cfg.get_configvalue("miniflux-passwordeval");
		const std::string token = cfg.get_configvalue("miniflux-token");
		const std::string token_file = cfg.get_configvalue("miniflux-tokenfile");
		const std::string token_eval = cfg.get_configvalue("miniflux-tokeneval");
		const bool creds_set = !token.empty()
			|| !token_file.empty()
			|| !token_eval.empty()
			|| (!user.empty() && (!pass.empty() || !pass_file.empty() || !pass_eval.empty()));
		if (!creds_set) {
			std::cerr <<
				_("ERROR: You must provide an API token or a login/password pair to use Miniflux. Please set the appropriate miniflux-* settings\n");
			return EXIT_FAILURE;
		}

		api = new MinifluxApi(cfg);
		urlcfg = new MinifluxUrlReader(&cfg, configpaths.url_file(), api);
	} else if (type == "inoreader") {
		const auto all_set = !cfg.get_configvalue("inoreader-app-id").empty()
			&& !cfg.get_configvalue("inoreader-app-key").empty();
		if (!all_set) {
			std::cerr <<
				_("ERROR: You must set *both* `inoreader-app-id` and `inoreader-app-key` to use Inoreader.\n");
			return EXIT_FAILURE;
		}

		api = new InoreaderApi(cfg);
		urlcfg = new InoreaderUrlReader(&cfg, configpaths.url_file(), api);
	} else {
		std::cerr << strprintf::fmt(_("ERROR: Unknown urls-source `%s'"),
				type) << std::endl;
		return EXIT_FAILURE;
	}

	if (!args.do_export() && !args.silent()) {
		std::cout << strprintf::fmt(
				_("Loading URLs from %s..."), urlcfg->get_source());
		std::cout.flush();
	}
	if (api) {
		if (!api->authenticate()) {
			std::cerr << _("Authentication failed.") << std::endl;
			return EXIT_FAILURE;
		}
	}
	const auto error_message = urlcfg->reload();
	if (error_message.has_value()) {
		std::cout << error_message.value().message << std::endl << std::endl;
	} else if (!args.do_export() && !args.silent()) {
		std::cout << _("done.") << std::endl;
	}

	if (urlcfg->get_urls().size() == 0) {
		LOG(Level::ERROR, "no URLs configured.");
		std::string msg;
		if (type == "local") {
			msg = strprintf::fmt(
					_("Error: no URLs configured. Please fill the "
						"file %s with RSS feed URLs or import an "
						"OPML file."),
					configpaths.url_file());
		} else if (type == "opml") {
			msg = strprintf::fmt(
					_("It looks like the OPML feed you subscribed "
						"contains no feeds. Please fill it with "
						"feeds, and try again."));
		} else if (type == "oldreader") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any "
						"feeds in your The Old Reader account. "
						"Please do so, and try again."));
		} else if (type == "ttrss") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any "
						"feeds in your Tiny Tiny RSS account. Please "
						"do so, and try again."));
		} else if (type == "newsblur") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any "
						"feeds in your NewsBlur account. Please do "
						"so, and try again."));
		} else if (type == "inoreader") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any "
						"feeds in your Inoreader account. Please do "
						"so, and try again."));
		} else if (type == "miniflux") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any "
						"feeds in your Miniflux account. Please do "
						"so, and try again."));
		} else if (type == "feedbin") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any "
						"feeds in your Feedbin account. Please do "
						"so, and try again."));
		} else if (type == "ocnews") {
			msg = strprintf::fmt(
					_("It looks like you haven't configured any feeds in your "
						"Owncloud/Nextcloud account. Please do so, and try "
						"again."));
		} else {
			assert(0); // shouldn't happen
		}
		std::cout << msg << std::endl << std::endl;
		return EXIT_FAILURE;
	}

	if (args.do_vacuum()) {
		std::cout << _("Opening cache...");
		std::cout << _("done.") << std::endl;
		std::cout << _("Cleaning up cache thoroughly...");
		std::cout.flush();
		rsscache->do_vacuum();
		std::cout << _("done.") << std::endl;
		return EXIT_SUCCESS;
	}

	if (!args.do_export() && !args.silent()) {
		std::cout << _("Loading articles from cache...");
	}
	std::cout.flush();

	unsigned int i = 0;
	for (const auto& url : urlcfg->get_urls()) {
		try {
			bool ignore_disp =
				(cfg.get_configvalue("ignore-mode") ==
					"display");
			std::shared_ptr<RssFeed> feed =
				rsscache->internalize_rssfeed(
					url, ignore_disp ? &ign : nullptr);
			feed->set_tags(urlcfg->get_tags(url));
			feed->set_order(i);
			feedcontainer.add_feed(feed);
		} catch (const DbException& e) {
			std::cout << _("Error while loading feeds from "
					"database: ")
				<< e.what() << std::endl;
			return EXIT_FAILURE;
		} catch (const std::string& str) {
			std::cout << strprintf::fmt(
					_("Error while loading feed '%s': "
						"%s"),
					url,
					str)
				<< std::endl;
			return EXIT_FAILURE;
		}
		i++;
	}

	std::vector<std::string> tags = urlcfg->get_alltags();

	if (!args.do_export() && !args.silent()) {
		std::cout << _("done.") << std::endl;
	}

	if (args.do_cleanup()) {
		std::cout << _("Cleaning up cache...");
		std::cout.flush();
		rsscache->cleanup_cache(feedcontainer.get_all_feeds(), true);
		std::cout << _("done.") << std::endl;
		return EXIT_SUCCESS;
	}

	// if configured, we fill all query feeds with some data; no need to
	// sort it, it will be refilled when actually opening it.
	if (cfg.get_configvalue_as_bool("prepopulate-query-feeds")) {
		if (!args.do_export() && !args.silent()) {
			std::cout << _("Prepopulating query feeds...");
			std::cout.flush();
		}

		feedcontainer.populate_query_feeds();

		if (!args.do_export() && !args.silent()) {
			std::cout << _("done.") << std::endl;
		}
	}

	feedcontainer.sort_feeds(cfg.get_feed_sort_strategy());

	if (args.do_export()) {
		export_opml(args.export_as_opml2());
		return EXIT_SUCCESS;
	}

	if (args.readinfo_import_file().has_value()) {
		LOG(Level::INFO,
			"Importing read information file from %s",
			args.readinfo_import_file().value());
		std::cout << _("Importing list of read articles...");
		std::cout.flush();
		import_read_information(args.readinfo_import_file().value());
		std::cout << _("done.") << std::endl;
		return EXIT_SUCCESS;
	}

	if (args.readinfo_export_file().has_value()) {
		LOG(Level::INFO,
			"Exporting read information file to %s",
			args.readinfo_export_file().value());
		std::cout << _("Exporting list of read articles...");
		std::cout.flush();
		export_read_information(args.readinfo_export_file().value());
		std::cout << _("done.") << std::endl;
		return EXIT_SUCCESS;
	}

	// hand over the important objects to the View
	v->set_config_container(&cfg);
	v->set_keymap(&keys);
	v->set_tags(tags);
	v->set_cache(rsscache);

	const auto cmds_to_execute = args.cmds_to_execute();
	if (cmds_to_execute.size() >= 1) {
		execute_commands(cmds_to_execute);
		return EXIT_SUCCESS;
	}

	// if the user wants to refresh on startup via configuration file, then
	// do so, but only if -r hasn't been supplied.
	if (!refresh_on_start &&
		cfg.get_configvalue_as_bool("refresh-on-startup")) {
		refresh_on_start = true;
	}

	FormAction::load_histories(
		configpaths.search_history_file(), configpaths.cmdline_history_file());

	// run the View
	int ret = v->run();

	unsigned int history_limit =
		cfg.get_configvalue_as_int("history-limit");
	LOG(Level::DEBUG, "Controller::run: history-limit = %u", history_limit);
	FormAction::save_histories(configpaths.search_history_file(),
		configpaths.cmdline_history_file(),
		history_limit);

	if (!args.silent()) {
		std::cout << _("Cleaning up cache...");
		std::cout.flush();
	}
	try {
		const auto unreachable_feeds = rsscache->cleanup_cache(
				feedcontainer.get_all_feeds());
		if (!args.silent()) {
			std::cout << _("done.") << std::endl;
			if (!unreachable_feeds.empty()) {
				for (const auto& feed : unreachable_feeds) {
					LOG(Level::USERERROR, "Unreachable feed found: %s", feed);
				}

				// Workaround for missing overload of strprintf::fmt for size_type on macOS.
				std::uint64_t num_feeds = unreachable_feeds.size();
				std::cout << strprintf::fmt(_("%" PRIu64 " unreachable feeds found. See "
							"`cleanup-on-quit` in newsboat(1) for details."), num_feeds)
					<< std::endl;
			}
		}
	} catch (const DbException& e) {
		LOG(Level::USERERROR, "Cleaning up cache failed: %s", e.what());
		if (!args.silent()) {
			std::cout << _("failed: ") << e.what() << std::endl;
			ret = EXIT_FAILURE;
		}
	}

	return ret;
}

void Controller::update_feedlist()
{
	v->set_feedlist(feedcontainer.get_all_feeds());
}

void Controller::update_visible_feeds()
{
	v->update_visible_feeds(feedcontainer.get_all_feeds());
}

void Controller::mark_all_read(const std::string& feedurl)
{
	try {
		rsscache->mark_all_read(feedurl);
	} catch (const DbException& e) {
		v->get_statusline().show_error(strprintf::fmt(
				_("Error: couldn't mark all feeds read: %s"),
				e.what()));
		return;
	}

	if (feedurl.empty()) { // Mark all feeds as read
		if (api) {
			for (const auto& feed : feedcontainer.get_all_feeds()) {
				api->mark_all_read(feed->rssurl());
			}
		}
		feedcontainer.mark_all_feeds_read();
	} else { // Mark a specific feed as read
		const auto feed = feedcontainer.get_feed_by_url(feedurl);
		if (!feed) {
			return;
		}

		if (api) {
			api->mark_all_read(feed->rssurl());
		}

		feed->mark_all_items_read();
	}
}

void Controller::mark_article_read(const std::string& guid, bool read)
{
	if (api) {
		api->mark_article_read(guid, read);
	}
}

void Controller::mark_all_read(unsigned int pos)
{
	ScopeMeasure m("Controller::mark_all_read");
	const auto feed = feedcontainer.get_feed(pos);
	if (feed == nullptr) {
		return;
	}

	if (feed->is_query_feed()) {
		if (api) {
			std::vector<std::string> item_guids;
			for (const auto& item : feed->items()) {
				if (item->unread()) {
					item_guids.push_back(item->guid());
				}
			}
			api->mark_articles_read(item_guids);
		}
		rsscache->mark_all_read(*feed);
	} else {
		rsscache->mark_all_read(feed->rssurl());
		if (api) {
			api->mark_all_read(feed->rssurl());
		}
	}
	m.stopover(
		"after rsscache->mark_all_read, before iteration over "
		"items");

	feedcontainer.mark_all_feed_items_read(feed);
}

void Controller::mark_all_read(const std::vector<std::string>& item_guids)
{
	ScopeMeasure m("Controller::mark_all_read");
	if (api) {
		api->mark_articles_read(item_guids);
	}
}

void Controller::replace_feed(RssFeed& oldfeed, RssFeed& newfeed, unsigned int pos,
	bool unattended)
{
	LOG(Level::DEBUG, "Controller::replace_feed: saving");
	rsscache->externalize_rssfeed(
		newfeed, ign.matches_resetunread(newfeed.rssurl()));
	LOG(Level::DEBUG,
		"Controller::replace_feed: after externalize_rssfeed");

	bool ignore_disp = (cfg.get_configvalue("ignore-mode") == "display");
	std::shared_ptr<RssFeed> feed = rsscache->internalize_rssfeed(
			oldfeed.rssurl(), ignore_disp ? &ign : nullptr);
	LOG(Level::DEBUG,
		"Controller::replace_feed: after internalize_rssfeed");

	feed->set_tags(urlcfg->get_tags(oldfeed.rssurl()));
	feed->set_order(oldfeed.get_order());
	feedcontainer.replace_feed(pos, feed);

	if (cfg.get_configvalue_as_bool("podcast-auto-enqueue")) {
		const auto result = queueManager.autoenqueue(*feed);
		switch (result.status) {
		case EnqueueStatus::QUEUED_SUCCESSFULLY:
		case EnqueueStatus::URL_QUEUED_ALREADY:
			// All is well, nothing to be done
			break;
		case EnqueueStatus::OUTPUT_FILENAME_USED_ALREADY:
			v->get_statusline().show_error(
				strprintf::fmt(_("Generated filename (%s) is used already."),
					result.extra_info));
			break;
		case EnqueueStatus::QUEUE_FILE_OPEN_ERROR:
			v->get_statusline().show_error(
				strprintf::fmt(_("Failed to open queue file: %s."), result.extra_info));
			break;
		}
	}

	for (const auto& item : feed->items()) {
		rsscache->update_rssitem_unread_and_enqueued(*item, feed->rssurl());
	}

	v->notify_itemlist_change(feed);
	if (!unattended) {
		v->set_feedlist(feedcontainer.get_all_feeds());
	}
}

int Controller::import_opml(const Filepath& opmlFile,
	const Filepath& urlFile)
{
	FileUrlReader urlReader(urlFile);
	const auto error_message = urlReader.reload(); // Load existing URLs
	if (error_message.has_value()) {
		std::cout << strprintf::fmt(
				_("Error importing OPML to urls file %s: %s"),
				urlFile, error_message.value().message)
			<< std::endl;
		return EXIT_FAILURE;
	}

	const auto import_error = opml::import(opmlFile, urlReader);
	if (import_error.has_value()) {
		std::cout << strprintf::fmt(
				_("An error occurred while parsing %s: %s"),
				opmlFile,
				import_error.value())
			<< std::endl;
		return EXIT_FAILURE;
	} else {
		std::cout << strprintf::fmt(
				_("Import of %s finished."), opmlFile)
			<< std::endl;
		return EXIT_SUCCESS;
	}
}

void Controller::export_opml(bool version2)
{
	xmlDocPtr root = opml::generate(feedcontainer, version2);

	xmlSaveCtxtPtr savectx = xmlSaveToFd(1, nullptr, 1);
	xmlSaveDoc(savectx, root);
	xmlSaveClose(savectx);

	xmlFreeDoc(root);
}

std::vector<std::shared_ptr<RssItem>> Controller::search_for_items(
		const std::string& query,
		std::shared_ptr<RssFeed> feed)
{
	std::vector<std::shared_ptr<RssItem>> items;
	if (feed && (feed->is_query_feed() || feed->is_search_feed())) {
		std::unordered_set<std::string> guids;
		for (const auto& item : feed->items()) {
			if (!item->deleted()) {
				guids.insert(item->guid());
			}
		}
		guids = rsscache->search_in_items(query, guids);
		for (const auto& item : feed->items()) {
			if (guids.find(item->guid()) != guids.end()) {
				items.push_back(item);
			}
		}
	} else {
		items = rsscache->search_for_items(
				query, (feed != nullptr ? feed->rssurl() : ""), ign);
		for (const auto& item : items) {
			item->set_feedptr(
				feedcontainer.get_feed_by_url(item->feedurl()));
		}
	}
	return items;
}

EnqueueResult Controller::enqueue_url(RssItem& item, RssFeed& feed)
{
	return queueManager.enqueue_url(item, feed);
}

void Controller::reload_urls_file()
{
	const auto error_message = urlcfg->reload();
	if (error_message.has_value()) {
		v->get_statusline().show_message(error_message.value().message);
		return;
	}

	std::vector<std::shared_ptr<RssFeed>> new_feeds;
	unsigned int i = 0;

	for (const auto& url : urlcfg->get_urls()) {
		const auto feed = feedcontainer.get_feed_by_url(url);
		if (feed) {
			feed->set_tags(urlcfg->get_tags(url));
			feed->set_order(i);
			new_feeds.push_back(feed);
		} else {
			try {
				bool ignore_disp =
					(cfg.get_configvalue("ignore-mode") ==
						"display");
				std::shared_ptr<RssFeed> new_feed =
					rsscache->internalize_rssfeed(url,
						ignore_disp ? &ign : nullptr);
				new_feed->set_tags(urlcfg->get_tags(url));
				new_feed->set_order(i);
				new_feeds.push_back(new_feed);
			} catch (const DbException& e) {
				LOG(Level::ERROR,
					"Controller::reload_urls_file: caught "
					"exception: %s",
					e.what());
				throw;
			}
		}
		i++;
	}

	v->set_tags(urlcfg->get_alltags());

	feedcontainer.set_feeds(new_feeds);
	feedcontainer.sort_feeds(cfg.get_feed_sort_strategy());
	update_feedlist();
}

void Controller::edit_urls_file()
{
	const char* editor;

	editor = getenv("VISUAL");
	if (!editor) {
		editor = getenv("EDITOR");
	}
	if (!editor) {
		editor = "vi";
	}

	std::string cmdline = strprintf::fmt("%s \"%s\"",
			editor,
			utils::replace_all(configpaths.url_file().to_locale_string(), "\"", "\\\""));

	v->push_empty_formaction();
	Stfl::reset();
	utils::run_interactively(cmdline, "Controller::edit_urls_file");
	v->drop_queued_input();

	v->pop_current_formaction();

	reload_urls_file();
}

int Controller::execute_commands(const std::vector<std::string>& cmds)
{
	if (v->formaction_stack_size() > 0) {
		v->pop_current_formaction();
	}
	for (const auto& cmd : cmds) {
		LOG(Level::DEBUG,
			"Controller::execute_commands: executing `%s'",
			cmd);
		if (cmd == "reload") {
			reloader->reload_all(true);
		} else if (cmd == "print-unread") {
			std::cout << strprintf::fmt(_("%u unread articles"),
					feedcontainer.unread_item_count())
				<< std::endl;
		} else {
			std::cerr
					<< strprintf::fmt(_("%s: %s: unknown command"),
							"newsboat",
							cmd)
						<< std::endl;
			return EXIT_FAILURE;
		}
	}
	return EXIT_SUCCESS;
}

Filepath Controller::write_temporary_item(RssItem& item)
{
	char filename[_POSIX_PATH_MAX];
	char* tmpdir = getenv("TMPDIR");
	if (tmpdir != nullptr) {
		snprintf(filename,
			sizeof(filename),
			"%s/newsboat-article.XXXXXX",
			tmpdir);
	} else {
		snprintf(filename,
			sizeof(filename),
			"/tmp/newsboat-article.XXXXXX");
	}
	int fd = mkstemp(filename);
	if (fd != -1) {
		write_item(item, filename);
		close(fd);
		return std::string(filename);
	} else {
		return "";
	}
}

void Controller::write_item(RssItem& item, const Filepath& filename)
{
	const std::string save_path = cfg.get_configvalue("save-path");
	Filepath spath = save_path.back() == '/' ? save_path : save_path + "/";

	Filepath path;
	if (filename.starts_with("/")) {
		path.push(filename);
	} else if (filename.starts_with("~")) {
		path = utils::resolve_tilde(filename);
	} else {
		path = spath.join(filename);
	}

	std::fstream f(path, std::fstream::out);
	if (!f.is_open()) {
		throw Exception(errno);
	}

	write_item(item, f);
}

void Controller::write_item(RssItem& item, std::ostream& ostr)
{
	ostr << item_renderer::to_plain_text(cfg, item) << std::endl;
}

void Controller::import_read_information(const Filepath& readinfofile)
{
	std::vector<std::string> guids;

	std::ifstream f(readinfofile);
	std::string line;
	getline(f, line);
	if (!f.is_open()) {
		return;
	}
	while (f.is_open() && !f.eof()) {
		guids.push_back(line);
		getline(f, line);
	}
	rsscache->mark_items_read_by_guid(guids);
}

void Controller::export_read_information(const Filepath& readinfofile)
{
	std::vector<std::string> guids = rsscache->get_read_item_guids();

	std::fstream f(readinfofile, std::fstream::out);
	if (f.is_open()) {
		for (const auto& guid : guids) {
			f << guid << std::endl;
		}
	}
}

void Controller::update_config()
{
	v->apply_colors_to_all_formactions();

	if (cfg.get_configvalue("error-log").length() > 0) {
		try {
			const auto filepath = Filepath::from_locale_string(cfg.get_configvalue("error-log"));
			logger::set_user_error_logfile(filepath);
		} catch (const Exception& e) {
			const std::string msg =
				strprintf::fmt("Couldn't open %s: %s",
					cfg.get_configvalue("error-log"),
					e.what());
			v->get_statusline().show_error(msg);
			std::cerr << msg << std::endl;
		}
	}
}

void Controller::load_configfile(const Filepath& filename)
{
	if (cfgparser.parse_file(filename)) {
		update_config();
	} else {
		v->get_statusline().show_error(strprintf::fmt(
				_("Error: couldn't open configuration file `%s'!"),
				filename));
	}
}

void Controller::dump_config(const Filepath& filename) const
{
	std::vector<std::string> configlines;
	cfg.dump_config(configlines);
	if (v) {
		v->get_keymap()->dump_config(configlines);
	}
	ign.dump_config(configlines);
	filters.dump_config(configlines);
	colorman.dump_config(configlines);
	rxman.dump_config(configlines);
	std::fstream f(filename, std::fstream::out);
	if (f.is_open()) {
		for (const auto& line : configlines) {
			f << line << std::endl;
		}
	}
}

void Controller::update_flags(std::shared_ptr<RssItem> item)
{
	if (api) {
		api->update_article_flags(
			item->oldflags(), item->flags(), item->guid());
	}
	item->update_flags();
}

} // namespace newsboat