if (word(2 $loadinfo()) != [pf]) { load -pf $word(1 $loadinfo()); return; };
#
# Mail script.  lifted from anduril by wd.
# This script is in the public domain
#
# $Id: mail,v 1.5 2005/06/25 18:08:12 jnelson Exp $
#
# This module is intended as a replacement for the built-in functionality of
# epic's mail code.  currently it only handles mbox-style mail boxes.  It
# allows the user to add and remove mboxes as desired, and change the
# checking time.
#
# You can /set mail_watch_time to change the time interval for scanning for
# new mail this script honors /set mail to completely enable/disable checking.
# You can use the /mbox command to list details about currently added
# mailboxes.  You can also use it to add or delete folders from the list.
#

load addset;
@mail.folders = getenv(MAIL);

addset mail_watch_time int {
	^timer -del mailcheck;
	if (*0 > 0) {
	    ^timer -refnum mailcheck -rep -1 $0 mail.check;
        };
};
set mail_watch_time 60;

# # # #
alias mbox (cmd, args) {
    if (cmd != [] && cmd != [list] && numwords($args) != 1) {
	echo [mail] usage: /mbox [add|del|list] [folder];
	return;
    } else if (cmd == []) {
	@:cmd = [list];
    };

    @:folder = twiddle($args);

    switch ($cmd) {
	(add) {
	    if (findw($folder $mail.folders) != -1) {
		echo [mail] folder $args is already being monitored;
		return;
	    };

	    @:stat = stat($folder);
	    if (stat == []) {
		echo [mail] stat\($folder\) failed.  either the file is not accesible or the system does not support stat(2).;
		return;
	    };
	    @push(mail.folders $folder);
	    @push(mail.times $word(11 $stat));

	    echo [mail] now monitoring $folder for new mail;
	}
	(del) {
	    if (findw($folder $mail.folders) == -1) {
		echo [mail] folder $folder is not being monitored;
		return;
	    };

	    @mail.folders = remw($folder $mail.folders);
	    echo [mail] no longer monitoring $folder for new mail;
	}
	(list) {
	    @:mtot = 0;
	    @:cols = word(0 $geom());
	    echo $pad(${cols - 40} \" \" folder) $pad(20 \" \" last modified) $pad(8 \" \" size) $pad(8 \" \" messages);
	    for xx in ($mail.folders) {
		@:stat = stat($xx);
		@:out = pad(${cols - 40} \" \" $xx);
		if (stat != []) {
		    @:out #= [ $pad(20 \" \" $strftime($word(11 $stat) %d/%m/%Y %H:%M:%S))];
		    @:out #= [ $pad(8 \" \" $mail.size_fmt($word(7 $stat)))];
		} else {
		    @:out #= [ $pad(20 \" \" unknown) $pad(8 \" \" unknown)];
		};
		@:mcnt = 0;
		@:fd = open($xx R);
		if (fd > -1) {
		    while (!eof($fd)) {
			if (word(1 $read($fd)) == [From]) {
			    @:mcnt++;
			};
		    };
		};
		@close($fd);
		@:out #= [ $pad(8 \" \" $mcnt)];
		@:mtot += mcnt;
		echo $out;
	    };
	}
	(*) {
	    echo [mail] usage: /mbox [add|del|list] [folder];
	}
    };
};

alias mail.check {
    if (numwords($mail.folders) == 0 || MAIL == 0 || MAIL == []) {
	return;
    };

    for (@:i = 0,i < numwords($mail.folders),@:i++) {
	@:folder = word($i $mail.folders);
	@:stat = stat($folder);
	if (stat == []) {
	    continue;
	}
	if (word(11 $stat) > word($i $mail.times)) {
	    echo [mail] new mail in $folder \($strftime($word(11 $stat) %d/%m/%Y %H:%M:%S)\);
	    @mail.times = chngw($i $word(11 $stat) $mail.times);
	};
    };
};

alias mail.size_fmt (n) {
    stack push set floating_point_math;
    ^set floating_point_math on;
    if (!n) {
	@function_return = [0b];
    } elsif (n < 1024) {
	@function_return = [${n}b];
    } elsif (n < 1048576) {
	@function_return = [$trunc(2 ${n / 1024})kb];
    } elsif (n < 1073741824) {
	@function_return = [$trunc(2 ${n / 1048576})mb];
    } elsif (n < 1099511627776) {
	@function_return = [$trunc(2 ${n / 1073741824})gb];
    } else {
	@function_return = [$trunc(2 ${n / 1099511627776})tb];
    };
    stack pop set floating_point_math;
};

for xx in ($mail.folders) {
    @:stat = stat($xx);
    if (stat == []) {
	@mail.folders = remw($xx $mail.folders);
    } else {
	@push(mail.times $word(11 $stat));
    };
};

# vi:set ts=8 sts=4 sw=4 tw=79 syntax=off ai smartindent:
