File: maildir.c

package info (click to toggle)
asmail 2.1-9
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,352 kB
  • sloc: ansic: 3,050; sh: 183; makefile: 18
file content (88 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download | duplicates (7)
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>

#include "globals.h"
#include "maildir.h"

int count_dir( const char * dirname ) {
	DIR * d = NULL;
	struct dirent * f = NULL;
	int ctotal = 0;

	if ( (d = opendir(dirname)) == NULL ) {
		return(-1);
	}

	while ( (f = readdir(d)) ) {
		if ( f->d_name[0] != '.' )
			++ctotal;
	}

	closedir(d);

	return(ctotal);
}

void maildir_handle( struct mbox_struct * mb ) {
	/* Since we never really intend to quit this function,
	 * all "global" stuff may go on the stack here */
	char dir_cur[MAX_INPUT_LENGTH+1];
	char dir_new[MAX_INPUT_LENGTH+1];
	int result;

	if ( ! strlen(mb->file) ) {
		printf("asmail: maildir_handle: no mailbox directory specified.\n");
		mb->status = STAT_FAIL;
		signal_update();
		pthread_exit(NULL);
	}
	if ( strlen(mb->file) > (MAX_INPUT_LENGTH-4)) {
		printf("asmail: maildir_handle: mailbox directory name is too long.\n");
		mb->status = STAT_FAIL;
		signal_update();
		pthread_exit(NULL);
	}
	strcpy(dir_cur, mb->file);
	strcat(dir_cur, "/cur");
	strcpy(dir_new, mb->file);
	strcat(dir_new, "/new");
	while (1) {
		mb->status |= STAT_RUN;
		signal_update();
		result = count_dir(dir_cur);
		if ( result < 0 ) {
			mb->status = STAT_FAIL;
			signal_update();
		} else {
			if ( result )
				mb->mail = MAIL_OLD;
			else
				mb->mail = MAIL_NONE;
			mb->ctotal = result;
		}
		result = count_dir(dir_new);
		if ( result < 0 ) {
			mb->status = STAT_FAIL;
			signal_update();
		} else {
			if ( result ) {
				if ( mb->cnew != result ) {
					pthread_mutex_lock(&mb->mutex);
					mb->flags |= FLAG_ARRIVED;
					pthread_mutex_unlock(&mb->mutex);
				}
				mb->mail = MAIL_NEW;
			}
			mb->cnew = result;
			if ( mb->status == STAT_RUN ) 
				mb->status = STAT_IDLE;
		}
		signal_update();
		sleep_check(mb->update);
	}
}