twitcode #3: new mail in mbox
Once upon a time, people’s interactions with computers (those few people who got to interact with computers directly) was mostly through a teletype: a combination of a keyboard where they could type instructions to the computer and a printer where it gave the responses back to them. This model is tenaciously clung to by a handful of still-active projects such as gdb
, but the bulk of its use nowadays is from command shells (bash
, zsh
, cmd.exe
) because command-response interaction is much easier to specify, record, automate, examine, modify, and perform remotely in a teletype-style than a GUI-style.
Email was born in the teletype era. This meant that a notification of new mail (for those who wanted to be notified asynchronously, instead of checking their mailbox periodically) had to be a message printed out on the teletype. On BSD systems, the program which did this was biff
. Nowadays, not only do few people maintain a mailbox local to the computer they’re on, but also spewing out a line of text has long ago stopped being a desired method of getting notifications from the computer. So biff
is essentially dead.
Now, it just happened that I am working on a remote system, and sometimes I want to asynchronously get notifications of certain events on it, and other times I know I’m messing around with it in an unstable state and don’t want to be distracted by the notifications my messing around will cause. The system can deliver its notifications in various ways, one of which is email. Hence, for the first time in decades, I have a use for biff
! In my remote shell to this system, I can turn biff
on and off as I desire to/not-to get notifications.
Except it turns out biff
is broken on this system, which isn’t that surprising, as nobody uses it. I could try to debug this, but I figure it’s got to be faster and easier to write my own ‘new’ biff which checks the mbox once a minute instead of getting notified through comsat
(this is what xbiff
, the old version of biff
for the X11 GUI, did).
And it is indeed very easy to create a biff
in Python. So easy that it looks like I have another candidate for a ≤140-character program. So, weighing in at 136 characters, I present:
import time,os s=lambda i:os.stat(os.getenv("MAIL"))[i] o=s(8) while 1: t=s(8) if t>o and t>s(7):print"New mail!";o=t time.sleep(60)
I’m disappointed I couldn’t get it down to 140 while keeping the traditional message “You have new mail.\r\n”, but I’ve run out of ideas to squeeze it any shorter. If anybody would like a real biff
, here’s my un-golfed version (public domain).
Update: Well, there’s nothing like publishing to sharpen the mind, apparently. Here’s it done in 142 chars with the ‘right’ message:
import time,os s=lambda i:os.stat(os.getenv("MAIL"))[i] o=s(8) while 1: if s(8)>max(o,s(7)):print"You have new mail.";o=s(8) time.sleep(60)
Close…
Update 2: A friend informed me that Python’s “chained comparisons” which treat “a<b<c<d” as meaning “a<b and b<c and c<d” aren’t restricted to the forms a<b<c and a>b>c, but work for any combination of comparison operators, so t>o and t>s(7)
can be shortened to o<t>s(7)
, giving us a 137-character solution:
import time,os s=lambda i:os.stat(os.getenv("MAIL"))[i] o=s(8) while 1: if o<s(8)>s(7):print"You have new mail.";o=s(8) time.sleep(60)
Woot!