-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbidled.c
122 lines (102 loc) · 2.56 KB
/
mbidled.c
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
#include <assert.h>
#include <ev.h>
#include <openssl/ssl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>
#include "channel.h"
#include "mbconfig.h"
#include "mbidled.h"
#include "version.h"
static char const USAGE[] = "Usage: %s -c MBSYNC_CONFIG [-e COMMAND] [-v]\n"
"Run command on mailbox change.\n"
"\n"
"Try man mbidled(1) for more information.\n";
char const *opt_cmd = "mbsync -c \"$MBIDLED_CONFIG\" \"$MBIDLED_CHANNEL:$MBIDLED_MAILBOX\"";
int opt_verbose = 0;
void
print_log(int priority, char const *message)
{
if (!opt_verbose && priority > LOG_NOTICE)
return;
(void)fprintf(stderr, "<%d>%s\n", priority, message);
}
int
main(int argc, char *argv[])
{
(void)SSL_library_init();
SSL_load_error_strings();
sigaction(
SIGPIPE,
&(struct sigaction const){
.sa_flags = SA_RESTART,
.sa_handler = SIG_IGN,
},
NULL
);
char const *opt_config = NULL;
for (int opt; 0 <= (opt = getopt(argc, argv, "c:e:d:D:vh"));) {
switch (opt) {
case 'c':
opt_config = optarg;
break;
case 'e':
opt_cmd = optarg;
break;
case 'v':
opt_verbose = 1;
fprintf(stderr, VERSION "\n");
break;
case 'h':
printf(USAGE, argv[0]);
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
char path_xdg[PATH_MAX], path_legacy[PATH_MAX];
if (!opt_config) {
char const *config_home = getenv("XDG_CONFIG_HOME");
char const *home = getenv("HOME");
ASSERT(home);
if (config_home)
ASSERT_SNPRINTF(path_xdg, "%s/isyncrc", config_home);
else
ASSERT_SNPRINTF(path_xdg, "%s/.config/isyncrc", home);
ASSERT_SNPRINTF(path_legacy, "%s/.mbsyncrc", home);
struct stat st;
int xdg = !lstat(path_xdg, &st);
int legacy = !lstat(path_legacy, &st);
if (!xdg && legacy) {
opt_config = path_legacy;
} else if (xdg && legacy) {
fprintf(stderr,
"Using configuration file %s instead of legacy %s.\n",
path_xdg,
path_legacy);
opt_config = path_xdg;
} else {
opt_config = path_xdg;
}
}
struct mbconfig mb_config;
struct mbconfig_parser mb_config_parser;
mb_config_parser.config = &mb_config;
if (mbconfig_parse(&mb_config_parser, opt_config)) {
fprintf(stderr,
"%s:%d:%d: %s\n",
opt_config,
mb_config_parser.lnum,
mb_config_parser.col,
mb_config_parser.error_msg);
fprintf(stderr, "Could not parse configuration file.\n");
return EXIT_FAILURE;
}
struct ev_loop *loop = EV_DEFAULT;
struct mbconfig_channel *mb_chan;
SLIST_FOREACH (mb_chan, &mb_config.channels, link)
channel_open(EV_A_ & mb_config, mb_chan);
ev_run(EV_A_ 0);
}