aboutsummaryrefslogblamecommitdiff
path: root/src/main.c
blob: 228604ee74f22704b77d7fb12bd0b4cf8b711bf2 (plain) (tree)






























































                                                                               
#include <assert.h>

#include "config.h"
#include "fetch.h"
#include "log.h"
#include "mail.h"
#include "parse.h"

int main(int argc, const char** argv) {
  if (argc < 2) {
    LOG("Error: Missing configuration file path argument\n");
    LOG("Usage: rssmail options.conf [other_options.conf, ...]");
    return EXIT_FAILURE;
  }

  // I am relying on this assumption in many places for the sake of convenience
  static_assert(sizeof(xmlChar) == sizeof(char),
                "xmlChar and char are not the same size!");

  // this initializes global libcurl and libxml resources
  init_fetch();
  init_parse();

  // initialize configuration objects
  smtp_config_t smtp_config;
  rss_config_t rss_config;
  init_config(&smtp_config, &rss_config);

  // read each of the configuration files whose paths were passed in as
  // command-line arguments
  for (int i = 1; i < argc; ++i) {
    if (read_config_file(argv[i], &smtp_config, &rss_config) != 0) {
      LOG_ERROR("Failed to read configuration file %s", argv[1]);
      goto done;
    }
  }

  // download all the RSS posts from the configured URI
  int count;
  post_item_t* posts = fetch_posts(&rss_config, &count);

  // if there are posts
  if (posts != NULL && count > 0) {
    send_posts_as_digest(&smtp_config, posts, count);
  }

  // cleanup
  for (int i = 0; i < count; ++i) {
    free_post_item(&posts[i]);
  }

  free(posts);

done:
  free_smtp_config(&smtp_config);
  free_rss_config(&rss_config);

  cleanup_parse();
  cleanup_fetch();

  EXIT_SUCCESS;
}