File: mail.c

package info (click to toggle)
ircii-pana 75-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,448 kB
  • ctags: 7,556
  • sloc: ansic: 82,667; makefile: 989; tcl: 153; sh: 124
file content (214 lines) | stat: -rw-r--r-- 3,895 bytes parent folder | download
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
/*
 * Mail check routines. Based on EPIC's mail check
 */


#include "irc.h"
#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 <sys/stat.h>

static	char	*mail_path = NULL;

/*
 * check_mail_status: returns 0 if mail status has not changed, 1 if mail
 * status has changed 
 */
int check_mail_status(void)
{
#if defined(HAVE_QMAIL)
	int count = 0;
	DIR *dp;
	struct dirent *dir;
	static int c = 0;
#else
static time_t old_stat = 0;
struct stat stat_buf;
#endif        
#ifdef PUBLIC_ACCESS
	return 0;
#endif
	if (!get_int_var(MAIL_VAR))
	{
#if defined(HAVE_QMAIL)
		c = 0;
#else
		old_stat = 0;
#endif
		return (0);
	}
	
#if defined(HAVE_QMAIL)
	if (!mail_path)
	{
		char *m;
		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);
	}
	if (count > c)
	{
		c = count;
		return c;
	}
	else if (count <= c)
		return -count;
#else
	if (!mail_path)
	{
		char *tmp_mail_path;
		if ((tmp_mail_path = getenv("MAIL")) != NULL)
			mail_path = m_strdup(tmp_mail_path);
		else
			mail_path = m_3dup(UNIX_MAIL, "/", username);
	}

	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;
}

/*
 * 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;
	switch (get_int_var(MAIL_VAR))
	{
		case 0:
			return NULL;
		case 1:
		{
			char this[] = "\\|/-";
#if defined(HAVE_QMAIL)
			int count;
			if ((count = check_mail_status()) > 0)
			{
				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"));
				if (i == 4)
					i = 0;
				sprintf(ret_str, "%c", this[i++]);
			}
			else if (count == 0)
				i = 0;
			return *ret_str ? ret_str : NULL;
#else
			switch(check_mail_status())
			{
				case 2:
					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"));
					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 = check_mail_status();
			if (count == 0)
			{
				old_count = 0;
				return NULL;
			}
			if  (count > 0)
			{
				if (count > old_count)
				{
					if (do_hook(MAIL_LIST, "%d %d", count - old_count, count))
						say("You have new email.");
				}

				old_count = count;
				sprintf(ret_str, "%d", old_count);
				return ret_str;
			}
			else
			{
				if (*ret_str)
					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("From ", buffer, 5))
							count++;

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