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
//! Application transforming rss feeds into email by directly pushing the entries into IMP folders.
//! This application is an adaption of the rss2imap Python script to Rust.
//!
//! #### How to use ?
//!
//! The simplest way to understand what to do is just to run `rrss2imap --help`
//!
//! It should output something like
//!
//! FLAGS:
//! -h, --help Prints help information
//! -V, --version Prints version information
//!
//! SUBCOMMANDS:
//! add Adds a new feed given its url
//! delete Delete the given feed
//! email Changes email address used in feed file to be the given one
//! export Export subscriptions as opml file
//! help Prints this message or the help of the given subcommand(s)
//! import import the given opml file into subscriptions
//! list List all feeds configured
//! new Creates a new feedfile with the given email address
//! reset Reset feedfile (in other words, remove everything)
//! run Run feed parsing and transformation
//!
//! Which give you a glimpse of what will happen
//!
//! Each of these commands also provide some help, when run with the same `--help` flag.
//!
//! The important operations to memorize are obviously
//!
//! #### `rrss2imap new`
//!
//! Creates a new `config.json` file. At init time, the config file will only contains `settings` element
//! with the email address set. You **have** to set
//!
//! * the used imap server
//! ** with user login and password
//! ** and security settings (secure should contain `{"Yes": secure port}` for imap/s
//! or `{"No": unsecure port}` for simple imap)
//! * the default config
//! ** folder will be the full path to an imap folder where entries will fall in
//! ** email will be the recipient email address (which may not be yours for easier filtering)
//! ** Base64 image inlining
//! * feeds is the list of all rss feeds that can be added
//!
//! #### `rrss2imap add`
//!
//! This command will add a new feed to your config. You can directly set here the email recipient as well as the folder
//! (but not the base64 image inlining parameter)
//!
//! #### `rrss2imap run`
//!
//! THis is the main command. It will
//!
//! 1. get all rss/atom feed contents
//! 2. List all new entries in these feeds
//! 3. Transform these entries into valid email messages
//! 4. Push these mail messages directly on IMAP server
//!
//! #### `rrss2imap list`
//!
//! Displays a list of the rss feeds. Here is an example
//!
//! ```
//! 0 : http://tontof.net/?rss (to: Nicolas Delsaux <nicolas.delsaux@gmx.fr> (default)) RSS/rrss2imap (default)
//! 1 : https://www.brothers-brick.com/feed/ (to: Nicolas Delsaux <nicolas.delsaux@gmx.fr> (default)) RSS/rrss2imap (default)
//! 2 : https://nicolas-delsaux.hd.free.fr/rss-bridge/?action=display&bridge=LesJoiesDuCode&format=AtomFormat (to: Nicolas Delsaux <nicolas.delsaux@gmx.fr> (default)) RSS/rrss2imap (default)
//! ```
//!
//! Please notice that each entry has an associated number, which is the one to enter when running `rrss2imap delete <NUMBER>`
//!
//! #### `config.json` format
//!
//! A typical feedfile will look like this
//!
//! ```json
//! {
//! "settings": {
//! "email": {
//! "server": "the imap server of your mail provider",
//! "user": "your imap user name",
//! "password": "your imap user password",
//! "secure": {
//! "Yes": 993 // Set to "Yes": port for imaps or "No": port for unsecure imap
//! }
//! },
//! // This config is to be used for all feeds
//! "config": {
//! // This is the email address written in each mail sent. It can be different from the email user
//! "email": "Nicolas Delsaux <nicolas.delsaux@gmx.fr>",
//! // This is the imap folder in which mails will be written
//! "folder": "RSS/rrss2imap"
//! // Setting this to true will force rrss2imap to transform all images into
//! // base64. This prevents images from beind downloaded (and is really cool when reading feeds from a smartphone)
//! // But largely increase each mail size (which can be quite bothering)
//! "inline_image_as_data": true
//! }
//! },
//! "feeds": [
//! {
//! "url": "http://tontof.net/?rss",
//! // This last updated is updated for each entry and should be enough to have rss items correctly read
//! "last_updated": "2019-05-04T16:53:15",
//! "config": {
//! // each config element can be overwritten at the feed level
//! }
//! },
//! ```
//!
extern crate structopt;
extern crate log;
extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate flexi_logger;
extern crate treexml;
extern crate chrono;
extern crate tera;
extern crate lazy_static;
extern crate human_panic;
extern crate kuchiki;
extern crate imap;
extern crate native_tls;
extern crate base64;
extern crate atom_syndication;
extern crate requests;
extern crate rss;
extern crate xhtmlchardet;
extern crate url;
extern crate tree_magic;
extern crate emailmessage;
extern crate openssl_probe;
use Logger;
use PathBuf;
use StructOpt;
///
/// rrss2imap is a script used to transform rss feed entries into mail messages that are directly dropped
/// into your mailbox by the grace of imap protocol
///
/// Main function simply load the RRSS2IMAP struct from the command-line arguments