mailstatus.c (6454B)
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 265 266 267 268 269 270 271 272 | #include <assert.h> #include <dirent.h> #include <errno.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/inotify.h> #include <sys/types.h> #include <time.h> #include <unistd.h> /* Maximum bytes that can be displayed. */ #define MAX_DISPLAY_LEN 25 const char *_maildir_inbox; char _maildir_inbox_new[FILENAME_MAX]; /* The display buffer */ static char _buffer[256]; static int _buffer_len = 0; /* Current mail info */ static int _unread_count = 0; static int _inotify = -1, _watch = -1, _watch2 = -1; static volatile sig_atomic_t _sigint = 0; /* Path of "fetching" indication file to watch */ static int _fetch = 0; static char _f_indicator[FILENAME_MAX]; static int _f_ellipsis = 0; static void sigint_handler(int param) { (void)param; _sigint = 1; fprintf(stdout, "caught SIGINT\n"); } #define FETCHMSG "Fetching mail " #define FETCHMSG_ELLIPSIS_RATE 300 #define FETCHMSG_ELLIPSIS "..." #define FETCHMSG_ELLIPSIS_LEN (sizeof(FETCHMSG_ELLIPSIS) - 1) static void update_status(void) { DIR *d; struct dirent *de; if (_fetch) { _buffer_len = snprintf(_buffer, sizeof(_buffer), FETCHMSG FETCHMSG_ELLIPSIS); /* Animate the ellipsis by truncating the string */ _buffer_len = _buffer_len - FETCHMSG_ELLIPSIS_LEN + _f_ellipsis; _buffer[_buffer_len] = '\0'; ++_f_ellipsis; _f_ellipsis %= (FETCHMSG_ELLIPSIS_LEN + 1); return; } d = opendir(_maildir_inbox_new); if (d == NULL) { fprintf(stderr, "update failed (%d): %s", errno, strerror(errno)); return; } _unread_count = 0; for (; (de = readdir(d)) != NULL;) { if (de->d_type == DT_REG) ++_unread_count; } closedir(d); if (_unread_count == 0) { _buffer_len = snprintf(_buffer, sizeof(_buffer), "No new mail."); } else { _buffer_len = snprintf(_buffer, sizeof(_buffer), "(%d) new mail.", _unread_count); } } static void print_status(void) { int p, pad; fputs(_buffer, stdout); pad = MAX_DISPLAY_LEN - _buffer_len; for (p = 0; p < pad; ++p) fputc(' ', stdout); fputs("\n", stdout); fflush(stdout); } /* sleep for milliseconds https://stackoverflow.com/a/1157217 */ static void msleep(long msec) { struct timespec ts; ts.tv_sec = msec / 1000; ts.tv_nsec = (msec % 1000) * 1000000; nanosleep(&ts, NULL); } int main(int argc, char **argv) { int rc = 0; char *runtime_dir; /* Get maildir from command line */ if (argc < 2) { fprintf(stderr, "usage: mailstatus <mailbox>\n"); rc = -1; goto exit; } _maildir_inbox = argv[1]; snprintf(_maildir_inbox_new, sizeof(_maildir_inbox_new), "%s/new", _maildir_inbox); //fprintf(stdout, "Watch directory: %s\n", _maildir_inbox_new); /* Get fetch indicator path */ runtime_dir = getenv("XDG_RUNTIME_DIR"); if (!runtime_dir && !*runtime_dir) { fprintf(stderr, "$XDG_RUNTIME_DIR is not set\n"); rc = -1; goto exit; } snprintf(_f_indicator, sizeof(_f_indicator), "%s/mailstatus-fetch", runtime_dir); _inotify = inotify_init1(IN_NONBLOCK); if (_inotify == -1) { fprintf(stderr, "failed to initialise inotify instance (%d): %s\n", errno, strerror(errno)); rc = -1; goto exit; } /* Register inotify watch on the mail directory */ _watch = inotify_add_watch(_inotify, _maildir_inbox_new, IN_CREATE | IN_DELETE | IN_MOVE); if (_watch == -1) { fprintf(stderr, "failed to add inotify watch (%d): %s\n", errno, strerror(errno)); rc = -1; goto exit; } /* Register inotify watch on the fetch indicator file */ _watch2 = inotify_add_watch(_inotify, runtime_dir, IN_CREATE | IN_DELETE); if (_watch2 == -1) { fprintf(stderr, "failed to add inotify watch (%d): %s\n", errno, strerror(errno)); rc = -1; goto exit; } struct sigaction sigact = { sigint_handler }; sigaction(SIGINT, &sigact, NULL); /* Initial check for fetch indicator file */ if (access(_f_indicator, F_OK) == 0) { _fetch = 1; } /* Print initial mail status */ update_status(); print_status(); for (;!_sigint;) { /* Check for inotify updates */ char buf[sizeof(struct inotify_event) + NAME_MAX + 1]; int n; n = read(_inotify, buf, sizeof(buf)); if (n == -1) { if (errno != EAGAIN) { fprintf(stderr, "read failed (%d): %s\n", errno, strerror(errno)); } } else if (n > 0) { char *p; const struct inotify_event *e; int count = 0; int fetch = -1; for (p = buf; p < buf + n; p += sizeof(struct inotify_event) + e->len) { e = (const struct inotify_event *)p; if ((e->wd == _watch) && (e->mask & (IN_CREATE | IN_DELETE | IN_MOVE))) { ++count; } else if ((e->wd == _watch2) && (strcmp(e->name, "mailstatus-fetch") == 0)) { if (e->mask & IN_CREATE) fetch = 1; else if (e->mask & IN_DELETE) fetch = 0; } } if (fetch != -1) { _fetch = fetch; _f_ellipsis = 0; } if (fetch == 0 || count > 0) { /* Update mail status */ update_status(); print_status(); } } if (_fetch) { msleep(FETCHMSG_ELLIPSIS_RATE); update_status(); print_status(); } else { msleep(500); } } exit: if (_inotify > -1) close(_inotify); return rc; } |