Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/source/mail.c
1069 views
1
/*
2
* Mail check routines. Based on EPIC's mail check
3
*/
4
5
6
#include "irc.h"
7
static char cvsrevision[] = "$Id: mail.c 3 2008-02-25 09:49:14Z keaston $";
8
CVS_REVISION(mail_c)
9
#include "struct.h"
10
11
#include "mail.h"
12
#include "lastlog.h"
13
#include "hook.h"
14
#include "vars.h"
15
#include "ircaux.h"
16
#include "output.h"
17
#include "window.h"
18
#include "status.h"
19
#include "misc.h"
20
#include "module.h"
21
#define MAIN_SOURCE
22
#include "modval.h"
23
24
#include <sys/stat.h>
25
26
#if !defined(HAVE_QMAIL)
27
static char *mail_path = NULL;
28
#endif
29
30
#if defined(HAVE_QMAIL)
31
/*
32
* count_maildir_mail: counts total # of mails (in cur and new)
33
*/
34
int count_maildir_mail(void)
35
{
36
int count = 0;
37
DIR *dp;
38
struct dirent *dir;
39
char *mail_path, *m;
40
41
m = m_sprintf("%s/cur", UNIX_MAIL);
42
mail_path = expand_twiddle(m);
43
new_free(&m);
44
45
if ((dp = opendir(mail_path)))
46
{
47
while ((dir = readdir(dp)))
48
{
49
if (!dir->d_ino || (dir->d_name[0] == '.'))
50
continue;
51
count++;
52
}
53
54
closedir(dp);
55
}
56
57
new_free(&mail_path);
58
m = m_sprintf("%s/new", UNIX_MAIL);
59
mail_path = expand_twiddle(m);
60
new_free(&m);
61
62
if ((dp = opendir(mail_path)))
63
{
64
while ((dir = readdir(dp)))
65
{
66
if (!dir->d_ino || (dir->d_name[0] == '.'))
67
continue;
68
count++;
69
}
70
71
closedir(dp);
72
}
73
74
new_free(&mail_path);
75
76
return count;
77
}
78
#endif
79
80
#ifndef UNIX_MAIL
81
#define UNIX_MAIL "/var/spool/mail"
82
#endif
83
84
#ifndef MAIL_DELIMITER
85
#define MAIL_DELIMITER "From "
86
#endif
87
88
89
#ifdef WANT_DLL
90
#define check_ext_mail global_table[CHECK_EXT_MAIL]
91
#define check_ext_mail_status global_table[CHECK_EXT_MAIL_STATUS]
92
#endif
93
94
/*
95
* check_mail_status: returns 0 if mail status has not changed, 1 if mail
96
* status has changed
97
*/
98
#ifdef PUBLIC_ACCESS
99
int check_mail_status(void)
100
{
101
return 0;
102
}
103
#else
104
int check_mail_status(void)
105
{
106
#if defined(HAVE_QMAIL)
107
int count = 0;
108
static int c = 0;
109
#else
110
static time_t old_stat = 0;
111
struct stat stat_buf;
112
#endif
113
#ifdef WANT_DLL
114
if (check_ext_mail)
115
return (*check_ext_mail_status)();
116
#endif
117
if (!get_int_var(MAIL_VAR))
118
{
119
#if defined(HAVE_QMAIL)
120
c = 0;
121
#else
122
old_stat = 0;
123
#endif
124
return (0);
125
}
126
127
#if defined(HAVE_QMAIL)
128
count = count_maildir_mail();
129
if (count > c)
130
{
131
c = count;
132
return c;
133
}
134
if (count < c)
135
{
136
int diff;
137
diff = count - c;
138
c = count;
139
return diff;
140
}
141
#else
142
if (!mail_path)
143
{
144
char *tmp_mail_path;
145
if ((tmp_mail_path = getenv("MAIL")) != NULL)
146
mail_path = m_strdup(tmp_mail_path);
147
else
148
#ifdef __EMX__
149
mail_path = m_sprintf("%s/mqueue", getenv("ETC"));
150
#else
151
mail_path = m_3dup(UNIX_MAIL, "/", username);
152
#endif
153
}
154
155
if (stat(mail_path, &stat_buf) == -1)
156
return 0;
157
158
if (stat_buf.st_ctime > old_stat)
159
{
160
old_stat = stat_buf.st_ctime;
161
if (stat_buf.st_size)
162
return 2;
163
}
164
if (stat_buf.st_size)
165
return 1;
166
#endif
167
return 0;
168
}
169
#endif
170
171
172
/*
173
* check_mail: This here thing counts up the number of pieces of mail and
174
* returns it as static string. If there are no mail messages, null is
175
* returned.
176
*/
177
178
char *check_mail (void)
179
{
180
static int old_count = 0;
181
static char ret_str[12];
182
static int i = 0;
183
#ifdef WANT_DLL
184
if (check_ext_mail)
185
return (char *)(*check_ext_mail)();
186
#endif
187
switch (get_int_var(MAIL_VAR))
188
{
189
case 0:
190
return NULL;
191
case 1:
192
{
193
char this[] = "\\|/-";
194
#if defined(HAVE_QMAIL)
195
int count = check_mail_status();
196
if (count > 0)
197
{
198
set_display_target(NULL, LOG_CRAP);
199
if (do_hook(MAIL_LIST, "%s %s", "Mail", "Yes"))
200
put_it("%s", convert_output_format(fget_string_var(FORMAT_MAIL_FSET), "%s %s %s", update_clock(GET_TIME), "Mail", "Yes"));
201
++i;
202
reset_display_target();
203
if (i == 4)
204
i = 0;
205
}
206
sprintf(ret_str, "%c", this[i]);
207
208
return ret_str;
209
#else
210
switch(check_mail_status())
211
{
212
case 2:
213
set_display_target(NULL, LOG_CRAP);
214
if (do_hook(MAIL_LIST, "%s %s", "Mail", "Yes"))
215
put_it("%s", convert_output_format(fget_string_var(FORMAT_MAIL_FSET), "%s %s %s", update_clock(GET_TIME), "Mail", "Yes"));
216
reset_display_target();
217
if (i == 4)
218
i = 0;
219
sprintf(ret_str, "%c", this[i++]);
220
case 1:
221
if (!*ret_str)
222
return NULL;
223
return ret_str;
224
case 0:
225
i = 0;
226
return NULL;
227
}
228
#endif
229
}
230
case 2:
231
{
232
register int count = 0;
233
#if defined(HAVE_QMAIL)
234
count = count_maildir_mail();
235
if (count == 0)
236
{
237
old_count = 0;
238
return NULL;
239
}
240
if (count > old_count)
241
{
242
set_display_target(NULL, LOG_CRAP);
243
if (do_hook(MAIL_LIST, "%d %d", count - old_count, count))
244
say("You have new email.");
245
reset_display_target();
246
}
247
248
old_count = count;
249
sprintf(ret_str, "%d", old_count);
250
return ret_str;
251
#else
252
FILE *mail;
253
char buffer[255];
254
switch(check_mail_status())
255
{
256
257
case 0:
258
old_count = 0;
259
return NULL;
260
case 2:
261
{
262
if (!(mail = fopen(mail_path, "r")))
263
return NULL;
264
265
while (fgets(buffer, 254, mail))
266
if (!strncmp(MAIL_DELIMITER, buffer, 5))
267
count++;
268
269
fclose(mail);
270
271
if (count > old_count)
272
{
273
set_display_target(NULL, LOG_CRAP);
274
if (do_hook(MAIL_LIST, "%d %d", count - old_count, count))
275
say("You have new email.");
276
reset_display_target();
277
}
278
279
old_count = count;
280
sprintf(ret_str, "%d", old_count);
281
}
282
case 1:
283
if (*ret_str)
284
return ret_str;
285
}
286
#endif
287
}
288
}
289
return NULL;
290
}
291
292