Category: perl

Judicial Candidate Selection Algorithm

I had always hoped that Supreme Court Justices where choosen in the following manner:
@candidates      = find_all_legal_professionals();
@qualified       = sort_by_qualifications(@candidates);
@partisans       = filter_by_political_necessity(@qualified);
$final_candidate = most_confirmable(@partisans);
The nomination of Harriet Miers appeared to follow this algorithm:
$final_candidate = find_lawyer_in_radius("10 feet");
Without commenting on the actual qualifications of Ms. Miers, I would suggest that the 2nd algorihtm is somewhat less likely to find a quality candidate, much less a confirmable one.

Technorati tags for this post:

Extracting Meeting Invites from my Mailbox

In previous posts I've bemoaned the lack of integration between Mozilla's Thunderbird and external programs -- especially Mozilla Sunbird. I realize that there is a new project (Lightning) to integrate the two, but my meeting invites keep pouring in at work and I can't wait.

My temporary solution to this problem uses the following perl script. Its job is to copy any MIME attachments of type "text/calendar" out of my inbox and into a folder on disk. Then I can use Sunbird's Import feature to read them in. My procedure is this:

  1. When I receive a meeting invite in Thunderbird, I copy the message into a specific folder - I call it meetings
  2. Then I run my script like this: ./extract_ics.pl -m ~/Mail/meetings -d ~/calendar -v
  3. In Sunbird, I import the ~/calendar/[whatever].ics file created in step 2.
  4. Finally, I go back and delete the contents of ~/calendar and the meetings folder
Here's my script:

#!/usr/bin/perl -w
use strict;
use Email::Folder;
use Email::MIME;
use Time::Local;
use Date::Parse;
use Getopt::Std;

######################################################################
#                                                                    #
# Find text/calendar MIME attachments in mailboxes and save them     #
# into a local folder for import into Sunbird                        #
#                                                                    #
######################################################################

# parse cmd line options
my $verbose  = 0;
my %opts     = ();
getopts('vm:d:', \%opts);
if ( ! $opts{'d'} or ! $opts{'m'} ) { usage(); }
if ( $opts{'v'} ) { $verbose = 1; }
my $maildir = $opts{'m'};
my $savedir = $opts{'d'};

# is -m a single mailbox or a maildir ?
if ( -f $maildir ) { parse_box($maildir); }
else {
        # find all the mailboxes in our maildir
        opendir(DIR, "$maildir") or die "Couldn't open directory $maildir";
        my @boxes = grep { -f "$maildir/$_" && ! m/\.msf$/ && ! m/^\./ } sort readdir(DIR);
        closedir(DIR);

        foreach my $b ( @boxes ) { parse_box("$maildir/$b"); }
}
exit 0;


sub usage {
        print qq(Usage: $0 -m  -d  [-v]\n);
        exit 1;
}

# find all text/calendar MIME attachments in a mailbox
sub parse_box {
        my $box = shift;

        print "Reading mailbox $box\n" if $verbose;

        my $folder = Email::Folder->new($box);
        # foreach my $m ( $folder->messages ) {
        while ( my $m = $folder->next_message ) {
                # $m is an Email::Simple class, Email::MIME requires a string
                my $parsed = Email::MIME->new($m->as_string);
                my @parts = $parsed->parts;

                foreach my $p ( @parts ) {
                        if ( $p->content_type =~ m|text/calendar| ) {
                                write_ics($p, $m->header("Subject"), $m->header("Date"));
                        }
                }
        }
}

# write attachments to a local folder
sub write_ics {
        my ($p, $subject, $date) = @_;

        # sanitize the subject line so I can use it as a filename
        $subject =~ s/\s+/_/g; # no spaces
        $subject =~ s|/|_|g;   # no slashes
        $subject =~ s|_+|_|g;  # remove duplicate underscores

        my $filename = "${savedir}/${subject}.ics";
        if ( -f $filename ) {
                print "WARNING: overwriting $filename\n" if $verbose;
        }

        if ( ! open(ICS, ">$filename") ) {
                print qq(WARNING: failed to open $filename for writing\n);
                return;
        }

        print "Writing $filename\n" if $verbose;
        print ICS $p->body, "\n";
        close ICS;

        # set date of the file to date of email Date header
        my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($date);
        my $newtime = timelocal(0, $mm, $hh, $day, $month, $year);
        utime $newtime, $newtime, $filename;
}
Update: Make sure you "compact" your Mozilla folder before you run the script. There are likely to be "deleted" messages in it.

Technorati tags for this post:

Thunderbird, you stupid monolithic app!

Gee Whiz, Thunderbird. If you won't let me modify an incoming message (see previous post), can I at least modify a message on disk? I'd like to use the standard Perl CPAN email modules to modify or at least read your folders.

Can I do that?

Please?

Well, sort of.

I can of course use read-only tools like Mail::MboxParser. And I can even use other tools to modify a message. However, Thunderbird doesn't seem to re-read the mailbox. Shouldn't it react in some way when one of its folders is externally modified?

Drat. I curse all you monolithic, gui-centric applications! Why won't you play nice with other apps? Why don't you expect your user to be a Perl-mad hacker?

Technorati tags for this post:

Advanced Thunderbird Filtering Extension?

I've recently switched from using Mutt to Thunderbird at work. The only feature I really and truly miss is the ability to run external programs as part of the message filtering process - just like good old procmail used to let me do (or Perl's Mail::Audit). Thunderbird lets me sort messages into folders, but I need to do things like:
  • Modify the body of a message
  • Add a header
  • Run another program based on the contents of the message
  • Bounce the message to another address

Does anybody have an extension for Thunderbird to let me run Perl or Python programs on an incoming message? No? Am I going to have to write this myself?

Technorati tags for this post:

Photo Album Software

I rather liked to looks of http://marginalhacks.com/Hacks/album/. However, it wasn't generating medium-sized images consitently.

So I rolled my own. Turns out a scant 250 lines of perl is enough to build a static photo album that meets my needs. See my album for an example.

Thanks for the inspiration Dave!

Technorati tags for this post:

Photo Galleries Suck

I've been using that PHP gallery program to do web-based photo galleries. But now I've got a sweet mod_perl server with a unified authentication system. And gallery has its own auth system. What a waste to install PHP just to get a bunch of stuff I don't need.

After some research, I think I'll just use good old album, a perl script which creates static html photo albums. All I need to do is write a simple upload script and I'll have everthing I need!

Link to story

Technorati tags for this post:

< Future 10 | Past 10 >