-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmail.c
113 lines (101 loc) · 3.4 KB
/
mail.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
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include "../util.h"
#include "mail.h"
#define ICONn COL1 "" COL0 /* last sync successful */
#define ICONe COL2 "" COL0 /* last sync failed */
#define ICONs COL3 "" COL0 /* syncing */
#define ICONz COL4 "" COL0 /* frozen */
#define NEWMAILDIR "/home/ashish/.local/share/mail/iiser/INBOX/new"
#define MAILSYNC (char *[]){ SCRIPT("mailsync.sh"), NULL }
#define NOTIFY(t, msg) uspawn((char *[]){ \
"notify-send", \
"-h", "string:x-canonical-private-synchronous:mailsync", \
"-t", t, \
"-e", \
"MailSync", msg, NULL })
static int newmails = 0;
static void
updatenewmails(void)
{
static time_t lastmtime = 0;
int prevnewmails;
DIR* dir;
struct dirent* entry;
struct stat buf;
if (stat(NEWMAILDIR, &buf) == -1) {
newmails = -1;
return;
}
if (buf.st_mtime == lastmtime)
return;
lastmtime = buf.st_mtime;
if (!(dir = opendir(NEWMAILDIR))) {
newmails = -1;
return;
}
prevnewmails = newmails;
newmails = 0;
while ((entry = readdir(dir)))
if (entry->d_type == DT_REG)
newmails++;
closedir(dir);
if (newmails > prevnewmails)
NOTIFY("2000", "You have got new mails!");
}
size_t
mailu(char *str, int sigval)
{
static int frozen;
/* update newmails except for routine resync and when MAILSYNC signals
* it's launch */
if (sigval != RTNE && sigval != -1)
updatenewmails();
if (newmails < 0) {
*str = '\0';
return 1;
}
if (frozen && sigval != -2)
return SPRINTF(str, ICONz "%d", newmails);
switch (sigval) {
/* routine update or `sigdsblocks 3` */
case STRT:
case RTNE:
case NONE:
uspawn(MAILSYNC);
/* MAILSYNC will signal -1, no need to update text now */
return 0;
/* toggle frozen */
case -2:
if (frozen) {
frozen = 0;
uspawn(MAILSYNC);
return 0;
} else {
frozen = 1;
return SPRINTF(str, ICONz "%d", newmails);
}
/* MAILSYNC started */
case -1:
return SPRINTF(str, ICONs "%d", newmails);
/* sync successful */
case 0:
return SPRINTF(str, ICONn "%d", newmails);
/* sync failed */
default:
return SPRINTF(str, ICONe "%d", newmails);
}
}
void
mailc(int button)
{
switch (button) {
case 1:
csigself(3, NONE);
break;
case 3:
csigself(3, -2);
break;
}
}