aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: cf0e8356374b86a9e82917f194f16a200931abd7 (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
#include "config.h"

#ifdef __STDC_ALLOC_LIB__
#define __STDC_WANT_LIB_EXT2__ 1
#endif

#include <ctype.h>
#include <post.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "log.h"

#define SMTP_FROM "from_address", "sender", "from"
#define SMTP_TO "to_address", "to"
#define SMTP_SUBJECT "subject", "subj", "sub"
#define SMTP_USERNAME "username", "user"
#define SMTP_PASSWORD "password", "pass", "passwd"
#define SMTP_HOST "server_host", "host"
#define SMTP_CERTPATH "certificate_path", "cert", "cert_path"
#define SMTP_PORT "server_port", "port"
#define SMTP_RECIPIENT "recipient", "rcpt"

#define RSS_URI "feed_uri", "feed_url", "feed", "uri", "url"
#define RSS_USERAGENT "useragent", "agent"
#define RSS_MAXAGE "max_age", "age"

#define COMMENT_TOKEN '#'

#define CFG_STR(line, fld, ...)                                \
  {                                                            \
    char* tokens[] = {__VA_ARGS__};                            \
    for (int i = 0; i < sizeof(tokens) / sizeof(char*); ++i) { \
      if (strstr(line, tokens[i]) == (line) &&                 \
          isspace(line[strlen(tokens[i])])) {                  \
        if ((fld) != NULL) {                                   \
          free((fld));                                         \
        }                                                      \
        (fld) = copy_line_value(line);                         \
        LOG_DEBUG("%s = %s", tokens[i], fld);                  \
        break;                                                 \
      }                                                        \
    }                                                          \
  }

#define CFG_STRAPPEND(line, fld, n, ...)                       \
  {                                                            \
    char* tokens[] = {__VA_ARGS__};                            \
    for (int i = 0; i < sizeof(tokens) / sizeof(char*); ++i) { \
      if (strstr(line, tokens[i]) == (line) &&                 \
          isspace(line[strlen(tokens[i])])) {                  \
        (fld) = realloc(fld, ++(n) * sizeof(char*));           \
        (fld)[(n)-1] = copy_line_value(line);                  \
        LOG_DEBUG("%s (%d) = %s", tokens[i], n, (fld)[(n)-1]); \
        break;                                                 \
      }                                                        \
    }                                                          \
  }

#define CFG_INT(line, fld, ...)                                \
  {                                                            \
    char* tokens[] = {__VA_ARGS__};                            \
    for (int i = 0; i < sizeof(tokens) / sizeof(char*); ++i) { \
      if (strstr(line, tokens[i]) == (line) &&                 \
          isspace(line[strlen(tokens[i])])) {                  \
        char* strval = copy_line_value(line);                  \
        if (strval != NULL) {                                  \
          (fld) = (int)strtol(strval, NULL, 10);               \
          LOG_DEBUG("%s = %d", tokens[i], fld);                \
          free(strval);                                        \
        }                                                      \
        break;                                                 \
      }                                                        \
    }                                                          \
  }

static char* copy_line_value(char* line) {
  // find first whitespace character
  char* value = strchr(line, ' ');
  if (value == NULL) {
    return NULL;
  }

  // advanced until no-longer whitespace
  while (isspace(value[0])) {
    ++value;
  }

  // return a copy from that position
  return strndup(value, strnlen(value, RSSMAIL_POST_MAX_FIELD_LENGTH) - 1);
}

// NOLINTNEXTLINE
static void parse_line(char* line, smtp_config_t* smtp, rss_config_t* rss) {
  // check to see if the line starts with the comment char; if so, skip it
  if (strchr(line, COMMENT_TOKEN) == line) {
    return;
  }

  CFG_INT(line, smtp->port, SMTP_PORT);
  CFG_STR(line, smtp->from, SMTP_FROM);
  CFG_STR(line, smtp->to, SMTP_TO);
  CFG_STR(line, smtp->subject, SMTP_SUBJECT);
  CFG_STR(line, smtp->username, SMTP_USERNAME);
  CFG_STR(line, smtp->password, SMTP_PASSWORD);
  CFG_STR(line, smtp->host, SMTP_HOST);
  CFG_STR(line, smtp->cert_path, SMTP_CERTPATH);
  CFG_STRAPPEND(line, smtp->recipients, smtp->recipient_count, SMTP_RECIPIENT);
  CFG_STRAPPEND(line, rss->uris, rss->uri_count, RSS_URI);
  CFG_STR(line, rss->useragent, RSS_USERAGENT);
  CFG_INT(line, rss->max_age, RSS_MAXAGE);
}

int read_config_file(const char* path, smtp_config_t* s, rss_config_t* r) {
  FILE* file;
  if ((file = fopen(path, "r")) == NULL) {
    perror("Failed to open configuration file");
    return RSSMAIL_CONFIGURE_FAILURE;
  }

  char* line = NULL;
  size_t len = 0;
  while (getline(&line, &len, file) > 0) {
    if (line != NULL) {
      parse_line(line, s, r);
    }
  }

  free(line);

  fclose(file);

  return RSSMAIL_CONFIGURE_SUCCESS;
}

void init_config(smtp_config_t* s, rss_config_t* r) {
  s->recipient_count = 0;
  s->port = 0;
  s->from = NULL;
  s->to = NULL;
  s->subject = NULL;
  s->username = NULL;
  s->password = NULL;
  s->host = NULL;
  s->cert_path = NULL;
  s->recipients = NULL;

  r->uris = NULL;
  r->uri_count = 0;
  r->useragent = NULL;
}

void free_smtp_config(smtp_config_t* s) {
  free(s->from);
  free(s->to);
  free(s->username);
  free(s->password);
  free(s->host);
  free(s->cert_path);
  free(s->subject);

  for (int i = 0; i < s->recipient_count; ++i) {
    free(s->recipients[i]);
  }

  free(s->recipients);
}

void free_rss_config(rss_config_t* r) {
  free(r->useragent);

  for (int i = 0; i < r->uri_count; ++i) {
    free(r->uris[i]);
  }

  free(r->uris);
}