Exchange transition helper for nnimap users

#!/usr/bin/perl -Tw
#-
# Copyright (c) 2014 Universitetet i Oslo
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Original author: Dag-Erling Smørgrav <d.e.smorgrav@usit.uio.no>
#

#
# This script updates your .newsrc.eld after the Cyrus -> Exchange
# transition by replacing . with / in folder names and renaming the
# sent, spam and drafts folders.
#
# Remember to update nnimap-address and gnus-outgoing-message-group in
# your .gnus.el:
#
#    (setq gnus-select-method
#          '(nntp "nntp.uio.no"))
#    (setq gnus-secondary-select-methods
#          '((nnimap "uio"
#                    (nnimap-address "mail.uio.no")
#                    (nnimap-server-port 993)
#                    (nnimap-stream ssl)
#                    (nnimap-authenticator login))))
#    (setq gnus-outgoing-message-group "nnimap+uio:Sent Items")
#
# Remove the second and third lines if you don't read news.
#
# Usage:
#
#    % nnimap-exchange-helper <server>
#
# where <server> is the name of your nnimap method ("uio" in the
# example above).
#

use strict;
use warnings;
use open qw(:locale);
use utf8;

use User::pwent;

our $SERVER;

sub translate_folders($) {
    my ($ifn) = @_;

    open(my $ifh, '<', $ifn)
        or die("$ifn: $!\n");
    my $ofn = "$ifn.$$";
    open(my $ofh, '>', $ofn)
        or die("$ofn: $!\n");
    while (<$ifh>) {
        s@("nnimap\+\Q$SERVER\E:)INBOX\.Sent(")@${1}Sent Items${2}@g;
        s@("nnimap\+\Q$SERVER\E:)INBOX\.spam(")@${1}spam${2}@g;
        s@("nnimap\+\Q$SERVER\E:)INBOX\.Drafts(")@${1}Drafts${2}@g;
        while (s@("nnimap\+\Q$SERVER\E:)INBOX([^."]*)\.@${1}INBOX${2}/@g) {}
        print($ofh $_);
    }
    close($ifh);
    close($ofh);
    print("Wrote $ofn\n");
}

MAIN:{
    @ARGV == 1
        or die("usage: nnimap-exchange-helper <server>\n");
    $ARGV[0] =~ m/^([a-z][0-9a-z-]*)$/
        or die("invalid server\n");
    $SERVER = $1;
    my $user = getpwuid($<)
        or die("Who are you?\n");
    my $home = $user->dir
        or die("Where do you live?\n");
    -d $home
        or die("Where do you live?\n");
    -f "$home/.newsrc.eld"
        or die("Where is your .newsrc.eld?\n");

    translate_folders("$home/.newsrc.eld");

    print("Remember to change your gnus-outgoing-message-group " .
          "to \"nnimap+$SERVER:Sent\ Items\"\n");
}