File: mail.c

package info (click to toggle)
ircii-pana 1%3A1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,048 kB
  • ctags: 14,567
  • sloc: ansic: 130,654; sql: 6,041; makefile: 4,313; cpp: 1,270; tcl: 1,230; sh: 638; java: 151
file content (290 lines) | stat: -rw-r--r-- 5,482 bytes parent folder | download | duplicates (2)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Mail check routines. Based on EPIC's mail check
 */


#include "irc.h"
static char cvsrevision[] = "$Id: mail.c,v 1.1.1.1 2003/04/11 01:09:07 dan Exp $";
CVS_REVISION(mail_c)
#include "struct.h"

#include "mail.h"
#include "lastlog.h"
#include "hook.h"
#include "vars.h"
#include "ircaux.h"
#include "output.h"
#include "window.h"
#include "status.h"
#include "misc.h"
#include "module.h"
#define MAIN_SOURCE
#include "modval.h"

#include <sys/stat.h>

#if	!defined(HAVE_QMAIL)
static	char	*mail_path = NULL;
#endif

#if	defined(HAVE_QMAIL)
/*
 * count_maildir_mail: counts total # of mails (in cur and new)
 */
int count_maildir_mail(void)
{
	int count = 0;
	DIR *dp;
	struct dirent *dir;
	char *mail_path, *m;
	
	m = m_sprintf("%s/cur", UNIX_MAIL);
	mail_path = expand_twiddle(m);
	new_free(&m);
        
	if ((dp = opendir(mail_path)))
	{
		while ((dir = readdir(dp)))
		{
			if (!dir->d_ino || (dir->d_name[0] == '.'))
				continue;
			count++;
		}
	
		closedir(dp);
        }
        
        new_free(&mail_path);
        m = m_sprintf("%s/new", UNIX_MAIL);
	mail_path = expand_twiddle(m);           
	new_free(&m);
                                
	if ((dp = opendir(mail_path)))
	{
		while ((dir = readdir(dp)))
		{
			if (!dir->d_ino || (dir->d_name[0] == '.'))
				continue;
			count++;
		}
	
		closedir(dp);
        }
        
        new_free(&mail_path);

        return count;
}
#endif

#ifndef UNIX_MAIL
#define UNIX_MAIL "/var/spool/mail"
#endif

#ifndef MAIL_DELIMITER
#define MAIL_DELIMITER "From "
#endif


#ifdef WANT_DLL
#define check_ext_mail global_table[CHECK_EXT_MAIL]
#define check_ext_mail_status global_table[CHECK_EXT_MAIL_STATUS]
#endif

/*
 * check_mail_status: returns 0 if mail status has not changed, 1 if mail
 * status has changed 
 */
#ifdef PUBLIC_ACCESS
int check_mail_status(void)
{
	return 0;
}
#else
int check_mail_status(void)
{
#if defined(HAVE_QMAIL)
	int count = 0;
	static int c = 0;
#else
static time_t old_stat = 0;
struct stat stat_buf;
#endif        
#ifdef WANT_DLL
	if (check_ext_mail)
		return (*check_ext_mail_status)();
#endif
	if (!get_int_var(MAIL_VAR))
	{
#if defined(HAVE_QMAIL)
		c = 0;
#else
		old_stat = 0;
#endif
		return (0);
	}
	
#if defined(HAVE_QMAIL)
	count = count_maildir_mail();
	if (count > c)
	{
		c = count;
		return c;
	}
	if (count < c)
	{
		int diff;
		diff = count - c;
		c = count;
		return diff;
	}
#else
	if (!mail_path)
	{
		char *tmp_mail_path;
		if ((tmp_mail_path = getenv("MAIL")) != NULL)
			mail_path = m_strdup(tmp_mail_path);
                else
#ifdef __EMX__
                        mail_path = m_sprintf("%s/mqueue", getenv("ETC"));
#else
                        mail_path = m_3dup(UNIX_MAIL, "/", username);
#endif
	}

	if (stat(mail_path, &stat_buf) == -1)
		return 0;

	if (stat_buf.st_ctime > old_stat)
	{
		old_stat = stat_buf.st_ctime;
		if (stat_buf.st_size)
			return 2;
	}
	if (stat_buf.st_size)
		return 1;
#endif		
	return 0;
}
#endif


/*
 * check_mail: This here thing counts up the number of pieces of mail and
 * returns it as static string.  If there are no mail messages, null is
 * returned. 
 */

char	*check_mail (void)
{
static 	int old_count = 0;
static	char ret_str[12];
static  int	i = 0;
#ifdef WANT_DLL
	if (check_ext_mail)
		return (char *)(*check_ext_mail)();
#endif
	switch (get_int_var(MAIL_VAR))
	{
		case 0:
			return NULL;
		case 1:
		{
			char this[] = "\\|/-";
#if defined(HAVE_QMAIL)
			int count = check_mail_status();
			if (count > 0)
			{
				set_display_target(NULL, LOG_CRAP);
				if (do_hook(MAIL_LIST, "%s %s", "Mail", "Yes"))
					put_it("%s", convert_output_format(fget_string_var(FORMAT_MAIL_FSET), "%s %s %s", update_clock(GET_TIME), "Mail", "Yes"));
				++i;
				reset_display_target();
				if (i == 4)
					i = 0;
			}
			sprintf(ret_str, "%c", this[i]);

			return ret_str;
#else
			switch(check_mail_status())
			{
				case 2:
					set_display_target(NULL, LOG_CRAP);
					if (do_hook(MAIL_LIST, "%s %s", "Mail", "Yes"))
						put_it("%s", convert_output_format(fget_string_var(FORMAT_MAIL_FSET), "%s %s %s", update_clock(GET_TIME), "Mail", "Yes"));
					reset_display_target();
					if (i == 4)
						i = 0;
					sprintf(ret_str, "%c", this[i++]);
				case 1:
					if (!*ret_str)
						return NULL;
					return ret_str;
				case 0:
					i = 0;
					return NULL;
			}
#endif
		}
		case 2:
		{
			register int count = 0;
#if defined(HAVE_QMAIL)
			count = count_maildir_mail();
			if (count == 0)
			{
				old_count = 0;
				return NULL;
			}
			if  (count > old_count)
			{
				set_display_target(NULL, LOG_CRAP);
				if (do_hook(MAIL_LIST, "%d %d", count - old_count, count))
					say("You have new email.");
				reset_display_target();
			}

			old_count = count;
			sprintf(ret_str, "%d", old_count);
			return ret_str;
#else
			FILE *mail;
			char buffer[255];
			switch(check_mail_status())
			{

				case 0:
					old_count = 0;
					return NULL;
				case 2:
				{
					if (!(mail = fopen(mail_path, "r")))
						return NULL;

					while (fgets(buffer, 254, mail))
						if (!strncmp(MAIL_DELIMITER, buffer, 5))
							count++;

					fclose(mail);
	
					if (count > old_count)
					{
						set_display_target(NULL, LOG_CRAP);
						if (do_hook(MAIL_LIST, "%d %d", count - old_count, count))
							say("You have new email.");
						reset_display_target();
					}
	
					old_count = count;
					sprintf(ret_str, "%d", old_count);
				}
				case 1:
					if (*ret_str)
						return ret_str;
			}
#endif
		}
	}
	return NULL;
}