ZooT: I was inspired by your pop3 server idea and your tenacity of building this thing

Therefor I had my own idea of building this in awk. Please remember this too is in it's infancy since I started working on this only yesterday.
I hope it gives you some (good) ideas for your bash version.
Any user in /etc/passwd can make use of this service, as long as there is an md5 encrypted password for that user in his homedir named
.pop3auth(create with for example:
echo -n "mypassword" | md5sum > ~/.pop3auth)
Emails are to be stored in that same users homedir, in a folder called
.pop3spool and should be in standard mbox-format. (complete header, single empty line, then body.
I had a few trial runs with Evolution as a reader and it seemed to work quite well for me.
Code:
#!/usr/bin/gawk -f
function dbg(str) { printf("[%s] = %s\n", strftime("%T"), str); }
## send string to client
function send(disp, str) {
printf("%s\r\n", str) |& pop3d;
if (disp) printf("[%s] > %s\n", strftime("%T"), str);
}
## receive string from client
function recv() {
if ((n=(pop3d |& getline)) < 1) {
printf("[%s] = Connection lost (%d)\n", strftime("%T"), n);
return(n);
}
gsub(/\r$/, "", $0);
if ($1 == "PASS") printf("[%s] < PASS ********\n", strftime("%T"));
else printf("[%s] < %s\n", strftime("%T"), $0);
return(n);
}
function QUIT() {
delete(_file);
delete(_size);
_user = _home = _auth = "";
_nrfiles = _nrbytes = _last = 0;
send(1,"+OK POP3 server signing off");
# exit 0;
}
function USER(uname) {
delete(_file);
delete(_size);
_user = _home = _auth = "";
_nrfiles = _nrbytes = _last = 0;
while ( (getline <passwd) > 0) {
if ( ($0 !~ /^ *(#|;)/) && (split($0, arr, ":") > 6) ) {
if ( (uname == arr[1]) && !system("test -d \""arr[6]"\"") ) {
_user = arr[1];
_home = arr[6];
break;
}
}
}
close(passwd);
if (_user) send(1,"+OK User accepted");
else send(1,"-ERR Invalid user");
}
function PASS(pass) {
_auth = "";
if (_user && _home) {
fname = _home"/"authfile;
md5 = "echo -n \""pass"\" | md5sum";
md5 | getline;
md5sum = $1;
close(md5);
if ((getline < fname) > 0) {
if (md5sum == $1) {
_auth = 1;
system("mkdir -p -m 700 \""_home"/"spooldir"\"");
}
}
close(fname);
}
if (_auth) send(1,"+OK Authentication successful");
else send(1,"-ERR Authentication failed");
STAT(1);
}
function STAT(quiet) {
if (_auth) {
delete(_file);
delete(_size);
_nrfiles = _nrbytes = _last = 0;
cmd = "ls -l \""_home"/"spooldir"/\"*\".mbox\" 2>/dev/null";
while (cmd | getline) {
if (split($0, arr) > 4) {
_nrfiles++;
_nrbytes += arr[5];
_size[_nrfiles] = arr[5];
_file[_nrfiles] = $NF;
}
}
close(cmd);
if (!quiet) send(1,sprintf("+OK %d %d", _nrfiles, _nrbytes));
} else if (!quiet) send(1,"-ERR Please log in first");
}
function LIST(msg) {
if (_auth) {
if (msg) {
if (_file[msg] && _size[msg]) send(1,sprintf("+OK %d %d", msg, _size[msg]));
else send(1,"-ERR No such message");
} else {
send(1,sprintf("+OK %d messages, %d octets", _nrfiles, _nrbytes));
for (i=1; i<=_nrfiles; i++)
send(0,sprintf("%d %d", i, _size[i]));
send(0,".");
}
} else send(1,"-ERR Please log in first");
}
function TOP(msg, head) {
if (_auth) {
if (_file[msg] && _size[msg]) {
send(1,"+OK");
while (getline < _file[msg]) {
if ($0 == "") break;
send(0,($0==".")?"..":$0);
}
if (head > 0) {
send(0,"");
while ((getline < _file[msg]) && (head-- > 0))
send(0,($0==".")?"..":$0);
}
close(_file[msg]);
send(0,".");
} else send(1,"-ERR No such message");
} else send(1,"-ERR Please log in first");
}
function RETR(msg) {
if (_auth) {
if (_file[msg] && _size[msg]) {
_last = msg;
send(1,sprintf("+OK %d octets", _size[msg]));
while (getline < _file[msg])
send(0,($0==".")?"..":$0);
close(_file[msg]);
send(0,".");
} else send(1,"-ERR No such message");
} else send(1,"-ERR Please log in first");
}
function DELE(msg) {
if (_auth) {
if (_file[msg] && _size[msg]) {
_size[msg] = 0;
send(1,sprintf("+OK Message %d marked for deletion",msg));
} else send(1,"-ERR No such message");
} else send(1,"-ERR Please log in first");
}
function NOOP() {
if (_auth) send(1,"+OK");
else send(1,"-ERR Please log in first");
}
function LAST() {
if (_auth) send(1,sprintf("+OK %d", _last));
else send(1,"-ERR Please log in first");
}
function RSET() {
if (_auth) {
_last = 0;
STAT(1);
send(1,"+OK");
}
else send(1,"-ERR Please log in first");
}
function UIDL() {
if (_auth) {
send(1,"-ERR Not implemented");
} else send(1,"-ERR Please log in first");
}
BEGIN {
passwd = "/etc/passwd";
spooldir = ".pop3spool";
authfile = ".pop3auth";
pop3d = "/inet/tcp/110/0/0";
while (1) {
send(1,"+OK POP3 server ready");
while (recv() > 0) {
if ($0 ~ /^QUIT$/) QUIT();
else if ($0 ~ /^USER .+/) USER($2);
else if ($0 ~ /^PASS .+/) PASS($2);
else if ($0 ~ /^STAT$/) STAT();
else if ($0 ~ /^LIST/) LIST($2);
else if ($0 ~ /^TOP [0-9]+ [0-9]+$/) TOP($2, $3);
else if ($0 ~ /^RETR [0-9]+$/) RETR($2);
else if ($0 ~ /^DELE [0-9]+$/) DELE($2);
else if ($0 ~ /^NOOP$/) NOOP();
else if ($0 ~ /^LAST$/) LAST();
else if ($0 ~ /^RSET$/) RSET();
else if ($0 ~ /^UIDL$/) UIDL();
else send(1,sprintf("-ERR %s command not implemented", $1));
}
close(pop3d);
}
}