diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..228604e --- /dev/null +++ b/src/main.c | |||
@@ -0,0 +1,63 @@ | |||
1 | #include <assert.h> | ||
2 | |||
3 | #include "config.h" | ||
4 | #include "fetch.h" | ||
5 | #include "log.h" | ||
6 | #include "mail.h" | ||
7 | #include "parse.h" | ||
8 | |||
9 | int main(int argc, const char** argv) { | ||
10 | if (argc < 2) { | ||
11 | LOG("Error: Missing configuration file path argument\n"); | ||
12 | LOG("Usage: rssmail options.conf [other_options.conf, ...]"); | ||
13 | return EXIT_FAILURE; | ||
14 | } | ||
15 | |||
16 | // I am relying on this assumption in many places for the sake of convenience | ||
17 | static_assert(sizeof(xmlChar) == sizeof(char), | ||
18 | "xmlChar and char are not the same size!"); | ||
19 | |||
20 | // this initializes global libcurl and libxml resources | ||
21 | init_fetch(); | ||
22 | init_parse(); | ||
23 | |||
24 | // initialize configuration objects | ||
25 | smtp_config_t smtp_config; | ||
26 | rss_config_t rss_config; | ||
27 | init_config(&smtp_config, &rss_config); | ||
28 | |||
29 | // read each of the configuration files whose paths were passed in as | ||
30 | // command-line arguments | ||
31 | for (int i = 1; i < argc; ++i) { | ||
32 | if (read_config_file(argv[i], &smtp_config, &rss_config) != 0) { | ||
33 | LOG_ERROR("Failed to read configuration file %s", argv[1]); | ||
34 | goto done; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | // download all the RSS posts from the configured URI | ||
39 | int count; | ||
40 | post_item_t* posts = fetch_posts(&rss_config, &count); | ||
41 | |||
42 | // if there are posts | ||
43 | if (posts != NULL && count > 0) { | ||
44 | send_posts_as_digest(&smtp_config, posts, count); | ||
45 | } | ||
46 | |||
47 | // cleanup | ||
48 | for (int i = 0; i < count; ++i) { | ||
49 | free_post_item(&posts[i]); | ||
50 | } | ||
51 | |||
52 | free(posts); | ||
53 | |||
54 | done: | ||
55 | free_smtp_config(&smtp_config); | ||
56 | free_rss_config(&rss_config); | ||
57 | |||
58 | cleanup_parse(); | ||
59 | cleanup_fetch(); | ||
60 | |||
61 | EXIT_SUCCESS; | ||
62 | } | ||
63 | |||