#!/usr/bin/env perl use strict; my ($root, $ok, $start, $trash, $drafts, $sent, $spam, $bayes_file); # This function takes 2 arguments. First is the question (without trailing # space or ask sign). Second is the default value. User will be shown the # question and asked for a response. If no answer is supplied, the default # one will be returned. sub ask { my $question = shift @_; my $default = shift @_; print "$question [$default]: "; my $response = ; chop $response; if ($response eq ""){ return $default; } return $response; } # Takes only 1 argument and replaces leading tilde (~) with users home # directory. sub replace_tilde { $_ = shift @_; s/^~/$ENV{HOME}/g; return $_; } sub is_valid_maildir { my $dir = shift @_; return -d $dir && -d "$dir/cur" && -d "$dir/new" && -d "$dir/tmp"; } sub make_maildir { my $dir = shift @_; if (-d $dir){ system ("rm -rf $dir"); } if (-d $dir){ print "\nCouldn't create $dir...\n\n"; die; } mkdir "$dir", 0700; mkdir "$dir/new", 0700; mkdir "$dir/cur", 0700; mkdir "$dir/tmp", 0700; } sub print_valid { my $dir = shift @_; print "\nThe directory inbox seems to be a valid maildir.\n"; print "Using inbox as a $dir directory.\n\n"; } sub print_dir_exists { my $dir = shift @_; print "\nThere is an $dir directory under the specified directory,\n"; print "but it is not a valid maildir. Maybe another program is using\n"; print "this directory to store your messages, but using different\n"; print "format. You will be asked for a box. If $dir is your\n"; print "answer, it will be destroyed.\n\n" } sub print_file_exists { my $file = shift @_; print "\nThere is a file $file under the specified directory.\n"; print "You can't use it as a $file because elmo won't be able to\n"; print "move messages there. You will be asked for a box. If $file\n"; print "is your answer, it will be removed.\n\n"; } sub print_only_name { print "\nPlease specify only a name of the box under the specified\n"; print "directory. If it doesn't exist, it will be created for\n"; print "you. This name must be relative and cannot contain / sign.\n"; } # STEP 1 # Set up root directory of mailbox. do { $root = ask ("Where do you want to store your mailboxes", "~/mail"); $root = replace_tilde ($root); if (-d $root){ $ok = 1; } elsif (! -e $root){ mkdir $root, 0700; $ok = 1; } else { print "\nYou have specified an existing file to be a location\n"; print "of your mailboxes collection. I'm affraid it's impossible.\n"; print "Elmo stores your mailboxes in a single directory.\n\n"; print "Please specify a directory name.\n\n" } } while (! $ok); # STEP 2 # Start box. if (is_valid_maildir ("$root/inbox")){ print_valid ("inbox"); $start = "inbox"; } elsif (-d "$root/inbox"){ print_dir_exists ("inbox"); $start = ask ("What is your start box", "inbox"); make_maildir ("$root/$start"); } elsif (-e "$root/inbox"){ print_file_exists ("inbox"); $start = ask ("What is your start box", "inbox"); unlink("$root/$start"); make_maildir ("$root/$start"); } else { $start = ask ("What is your start box", "inbox"); while (index ($start, "/") != -1){ print_only_name(); $start = ask ("What is your start box", "inbox"); } make_maildir ("$root/$start"); } # STEP 3 # Trash. if (is_valid_maildir ("$root/trash")){ print_valid ("trash"); $trash = "trash"; } elsif (-d "$root/trash"){ print_dir_exists ("trash"); $trash = ask ("Where do you want to move deleted messages", "trash"); make_maildir ("$root/$trash"); } elsif (-e "$root/trash"){ print_file_exists ("trash"); $trash = ask ("Where do you want to move deleted messages", "trash"); unlink("$root/$trash"); make_maildir ("$root/$trash"); } else { $trash = ask ("Where do you want to move deleted messages", "trash"); while (index ($start, "/") != -1){ print_only_name(); $trash = ask ("Where do you want to move deleted messages", "trash"); } make_maildir ("$root/$trash"); } # STEP 4 # Drafts. if (is_valid_maildir ("$root/drafts")){ print_valid ("drafts"); $drafts = "drafts"; } elsif (-d "$root/drafts"){ print_dir_exists ("drafts"); $drafts = ask ("Where do you want to store your unfinished messages", "drafts"); make_maildir ("$root/$drafts"); } elsif (-e "$root/drafts"){ print_file_exists ("drafts"); $drafts = ask ("Where do you want to store your unfinished messages", "drafts"); unlink("$root/$drafts"); make_maildir ("$root/$drafts"); } else { $drafts = ask ("Where do you want to store your unfinished messages", "drafts"); while (index ($start, "/") != -1){ print_only_name(); $drafts = ask ("Where do you want to store your unfinished messages", "drafts"); } make_maildir ("$root/$drafts"); } # STEP 5 # Sent. if (is_valid_maildir ("$root/sent")){ print_valid ("sent"); $sent = "sent"; } elsif (-d "$root/sent"){ print_dir_exists ("sent"); $sent = ask ("Where do you want to store your sent messages", "sent"); make_maildir ("$root/$sent"); } elsif (-e "$root/sent"){ print_file_exists ("sent"); $sent = ask ("Where do you want to store your sent messages", "sent"); unlink("$root/$sent"); make_maildir ("$root/$sent"); } else { $sent = ask ("Where do you want to store your sent messages", "sent"); while (index ($start, "/") != -1){ print_only_name(); $sent = ask ("Where do you want to store your sent messages", "sent"); } make_maildir ("$root/$sent"); } # STEP 6 # Spam. if (is_valid_maildir ("$root/spam")){ print_valid ("spam"); $spam = "spam"; } elsif (-d "$root/spam"){ print_dir_exists ("spam"); $spam = ask ("Where do you want to store your spam messages", "spam"); make_maildir ("$root/$spam"); } elsif (-e "$root/spam"){ print_file_exists ("spam"); $spam = ask ("Where do you want to store your spam messages", "spam"); unlink("$root/$spam"); make_maildir ("$root/$spam"); } else { $spam = ask ("Where do you want to store your spam messages", "spam"); while (index ($start, "/") != -1){ print_only_name(); $sent = ask ("Where do you want to store your sent messages", "sent"); } make_maildir ("$root/$spam"); } # STEP 7 # SMTP my $my_name = ask ("What is your name", "$ENV{USER}"); my $email = ask ("What is your email", ""); my $smtp_server = ask ("What is your smtp server", "$ENV{HOSTNAME}"); # STEP 8 # POP3 my $pop_server = ask ("What is your pop3 server", "$ENV{HOSTNAME}"); my $username = ask ("What is your user name", "$ENV{USER}"); my $password = ask ("What is your password", ""); # STEP 9 # SPAM PROTECTION my $protect = ask ("Should I protect your mail against spam", "yes"); if ($protect == "yes"){ $bayes_file = "$root/.bayes_file"; } # STEP 10 # ADRESSBOOK my $adressbook = ask ("Where do you want store your adressbook", "$root/.adressbook"); mkdir ("$ENV{HOME}/.elmo"); my $pop_info_dir = '~/.elmo/pop'; if (-e "$ENV{HOME}/.elmorc"){ print "\nFound existing .elmorc in your home directory. The existing\n"; print "file will be renamed to .elmorc.0.\n\n"; rename "$ENV{HOME}/.elmorc", "$ENV{HOME}/.elmorc.0"; } open (CONFIG, ">$ENV{HOME}/.elmorc"); print CONFIG "set mailbox {\n name: 'my box'\n root: '$root'\n"; print CONFIG " start: '$start'\n drafts: '$drafts'\n trash: '$trash'\n spam: '$spam'\n protect: '$protect'\n}\n\n"; print CONFIG "set bayes_file '$bayes_file'\n\n"; print CONFIG "set addressbook '$adressbook'\n\n"; print CONFIG "set smtp_acc {\n name: '$smtp_server'\n server: '$smtp_server'\n"; print CONFIG " my_name: '$my_name'\n email: '$email'\n}\n\n"; print CONFIG "set pop_acc {\n name: '$pop_server'\n server: '$pop_server'\n"; print CONFIG " username: '$username'\n password: '$password'\n}\n\n"; print CONFIG "set pop_info_dir $pop_info_dir\n\n"; # KEY BINDINGS print CONFIG "#KEY BINDINGS\n"; print CONFIG "key folder \Cn folder_bar_next\n"; print CONFIG "key folder \Cp folder_bar_prev\n"; print CONFIG "key mail r sender_open_reply\n"; print CONFIG "key mail f sender_open_fwd \n"; print CONFIG "key folder help_open\n"; print CONFIG "key help q help_close\n"; print CONFIG "key select \Cg box_selection_leave\n\n"; # HOOKS print CONFIG "#HOOKS\n"; print CONFIG "hook folder_toggle_flag folder_bar_next\n"; print CONFIG "hook fetch_del_mail fetch_next\n"; print CONFIG "hook fetch_get_mail fetch_next\n"; print CONFIG "hook mybox_create_box box_selection_re_read\n\n"; close (CONFIG); chmod (0600, "$ENV{HOME}/.elmorc") or die "\nCouldn't chmod $ENV{HOME}/.elmorc: $!\n";