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
|
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <utime.h>
#include "defs.h"
#include "mail.h"
bool is_From_line( char* line ) {
if( !strncmp( line, "From", 4 ) )
return true;
return false;
}
bool is_blank_line( char* line ) {
while( *line ) {
if( *line != ' ' && *line != '\n' && *line != '\t' && *line != '\r' )
return false;
line++;
}
return true;
}
char* cfg_file() {
static char buff[1024];
sprintf(buff, "%s/.glbiffrc", getenv("HOME"));
return buff;
}
/*
* returns number of messages found in folder
*/
int count_messages( char* filename ) {
struct stat st;
if( stat( filename, &st ) ) {
fprintf( stderr, "Unable to stat %s\n", filename );
return -1;
}
FILE* fl = fopen( filename, "r" );
if( !fl ) {
fprintf( stderr, "Unable to open %s\n", filename );
return -1;
}
char line[1024];
int messages = 0;
bool looking_for_header = true;
bool in_header = false;
bool last_line_blank = true;
fgets( line, 1024, fl );
while( !feof( fl ) ) {
if( looking_for_header && is_From_line( line ) && last_line_blank ) {
in_header = true;
looking_for_header = false;
messages++;
} else if( in_header && is_blank_line( line ) ) {
in_header = false;
looking_for_header = true;
}
last_line_blank = is_blank_line( line );
fgets( line, 1024, fl );
}
fclose(fl);
// here just restore the modify and access times to what they were
struct utimbuf buf;
buf.actime = st.st_atime;
buf.modtime = st.st_mtime;
utime( filename, &buf );
return messages;
}
/*
* returns the file size of the mailbox
*/
ulong check_mailbox( char *filename, bool& anymail, bool& unreadmail ) {
struct stat st;
off_t size;
if( stat( filename, &st ) ) {
#ifdef DEBUG
printf("!!!--Couldn't stat file %s .\n",filename);
#endif
anymail = false;
unreadmail = false;
size = 0;
} else {
size = st.st_size;
anymail = (size>0)? true: false;
unreadmail = (st.st_mtime > st.st_atime && size>0) ? true: false;
#ifdef DEBUG
printf( "unreadmail = %c\n", (unreadmail)?'y':'n' );
#endif
}
return size;
}
/*
* returns total number of messages
*/
int check_all_mailboxes( bool& anymail, bool& unreadmail, bool& newmail ) {
static ulong oldsize = 0;
ulong newsize = 0;
int messages = 0;
bool a, u;
bool usingmail = false;
anymail= false;
unreadmail= false;
newmail= false;
FILE* fl = fopen(cfg_file(),"r");
if( !fl ) {
printf("error opening file %s. Using $MAIL.\n",\
cfg_file());
usingmail = true;
}
#ifdef DEBUG
printf("read configuration file %s.\n",cfg_file());
#endif
char buff[1024];
if ( usingmail == false ) { /* HACK: Using .glbiffrc */
fgets( buff, 1024, fl );
while( !feof( fl ) ) {
if( buff[strlen(buff)-1] == '\n' )
buff[strlen(buff)-1] = 0; // kill off the trailing '\n'
// allow some different forms of mailbox spec. lines
char* mbox_name=buff+1;
if( *mbox_name==' ' || *mbox_name=='\t' )
mbox_name++;
#ifdef DEBUG
printf("checking mailbox: %s\n", mbox_name );
#endif
ulong size = check_mailbox( mbox_name, a, u );
anymail = (a)? true: anymail;
unreadmail = (u)? true: unreadmail;
newsize += size;
if( size && (buff[0]=='y' || buff[0]=='Y') )
messages += count_messages( mbox_name );
fgets( buff, 1024, fl ); // get the next mailbox name
}
}
else /* HACK: Using $MAIL environment variable */
{
char* mbox_name = getenv("MAIL");
if ( mbox_name = NULL ) {
printf("Error opening $MAIL. Aborting\n");
return -1;
}
ulong size = check_mailbox ( mbox_name, a, u );
anymail = (a)? true: anymail;
unreadmail = (u)? true: unreadmail;
newsize += size;
if( size )
messages += count_messages( mbox_name);
}
if( newsize > oldsize && unreadmail )
newmail = true;
oldsize = newsize;
#ifdef DEBUG
printf( "Messages in mailboxes : %d (new = %c)\n", messages, (unreadmail)?'y':'n' );
#endif
if (usingmail == false)
fclose(fl);
return messages;
}
|