[22998] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | # Evolution wrapper script for Athena |
---|
| 4 | |
---|
| 5 | # Performs the following Evolution customizations: |
---|
| 6 | # * Ensures $HOME/.evolution is private when created. |
---|
| 7 | # * Performs Hesiod lookup of user inbox. |
---|
| 8 | # * Pre-configures Evolution for MIT mail on first invocation. |
---|
| 9 | |
---|
| 10 | use strict; |
---|
| 11 | use Gnome2::GConf; |
---|
[23955] | 12 | use File::Basename; |
---|
| 13 | use File::Path; |
---|
[22998] | 14 | |
---|
[23955] | 15 | sub edit_state_file(); |
---|
| 16 | sub create_initial_state_file(); |
---|
| 17 | |
---|
[22998] | 18 | # Ensure $HOME/.evolution is private when created. |
---|
| 19 | my $homedir = $ENV{"HOME"}; |
---|
| 20 | if ($homedir && ! -e "$homedir/.evolution") { |
---|
| 21 | if (mkdir("$homedir/.evolution")) { |
---|
| 22 | system("fs sa $homedir/.evolution system:anyuser none 2>/dev/null"); |
---|
| 23 | } |
---|
| 24 | } |
---|
| 25 | |
---|
[24087] | 26 | # Pick server to use |
---|
[23955] | 27 | my $user = $ENV{"ATHENA_USER"} || $ENV{"USER"} || getpwuid($<); |
---|
[24087] | 28 | my $server = "$user.mail.mit.edu"; |
---|
[22998] | 29 | |
---|
[23955] | 30 | # Determine the authentication method to use for the IMAP server. |
---|
[24087] | 31 | # For a Cyrus PO server account we use GSSAPI. |
---|
[23955] | 32 | # For an Exchange account (or anything else), use password for now. |
---|
| 33 | # (Ideally some day we will use GSSAPI everywhere). |
---|
| 34 | my $auth = ""; |
---|
[24087] | 35 | my ($real_server) = gethostbyname($server); |
---|
| 36 | if ($real_server =~ /po\d+\.mail\.mit\.edu/i) { |
---|
| 37 | $auth = ";auth=GSSAPI"; |
---|
[23955] | 38 | } |
---|
| 39 | |
---|
| 40 | # Regular expression for the server names we recognize when updating |
---|
| 41 | # the IMAP account setting. We also convert Athena 9.4-style Hesiod |
---|
| 42 | # specifications (although it should be unusual to see one due to |
---|
| 43 | # debathena-gconf2-config). |
---|
[24087] | 44 | my $serverRE = "($user" . '\.mail\.mit\.edu)|(po\d+\.mit\.edu)|((imap\.)?exchange\.mit\.edu)|(_hesiod)'; |
---|
[23955] | 45 | my $old_server = ""; |
---|
| 46 | |
---|
[22998] | 47 | my $client = Gnome2::GConf::Client->get_default; |
---|
| 48 | my $accounts = $client->get_list("/apps/evolution/mail/accounts"); |
---|
| 49 | |
---|
[23955] | 50 | if ($accounts && @$accounts) { |
---|
| 51 | # Update the server with the value from Hesiod. |
---|
[22998] | 52 | my $change = 0; |
---|
| 53 | foreach (@$accounts) { |
---|
| 54 | my $old = $_; |
---|
| 55 | if (/name="MIT mail"/) { |
---|
[23955] | 56 | # Remember the old server name. |
---|
| 57 | if (m%imap://$user(?:;[^\@]*)?\@([^/]+)/%i) { |
---|
| 58 | $old_server = $1; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | # Update the server name (and corresponding auth method). |
---|
| 62 | s%(imap://$user)(\;[^\@]*)?\@($serverRE)%$1$auth\@$server%i; |
---|
| 63 | |
---|
| 64 | # Make sure we always use SSL. |
---|
| 65 | unless (m%imap://[^/]*/[^<]*;use_ssl=always[^<]*%i) { |
---|
| 66 | # First clear any other SSL setting. |
---|
| 67 | s%(imap://[^/]*/[^<]*);use_ssl=[^;<]*([^<]*)%$1$2%i; |
---|
| 68 | s%(imap://[^/]*/)([^<]*)%$1;use_ssl=always$2%i; |
---|
| 69 | } |
---|
[22998] | 70 | } |
---|
| 71 | $change = 1 if ($_ ne $old); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | if ($change) { |
---|
[23955] | 75 | # We need to update the account settings. |
---|
[22998] | 76 | $client->set_list("/apps/evolution/mail/accounts", "string", |
---|
| 77 | $accounts); |
---|
[23955] | 78 | if ($old_server ne $server) { |
---|
| 79 | # Edit the folder state file -- the URI of the saved |
---|
| 80 | # selected folder may point at the old server. |
---|
| 81 | edit_state_file(); |
---|
| 82 | } |
---|
[22998] | 83 | } |
---|
| 84 | } else { |
---|
| 85 | # Pre-configuration. |
---|
| 86 | $client->set_string("/apps/evolution/calendar/display/timezone", |
---|
| 87 | "America/New_York"); |
---|
[23092] | 88 | |
---|
[22998] | 89 | ($_, $_, $_, $_, $_, $_, my $gecos) = getpwuid($<); |
---|
| 90 | my @fields = split(",", $gecos); |
---|
| 91 | my $name = $fields[0]; |
---|
| 92 | my $home = $ENV{"HOME"}; |
---|
| 93 | my $local = "mbox:$home/.evolution/mail/local"; |
---|
| 94 | my $a = '<?xml version="1.0"?>' . "\n"; |
---|
| 95 | $a .= '<account name="MIT mail" uid="mitinitial" enabled="true">'; |
---|
| 96 | $a .= '<identity>'; |
---|
| 97 | $a .= "<name>$name</name>"; |
---|
| 98 | $a .= "<addr-spec>$user\@mit.edu</addr-spec>"; |
---|
| 99 | $a .= '</identity>'; |
---|
| 100 | $a .= '<source save-passwd="false">'; |
---|
[23955] | 101 | $a .= "<url>imap://$user$auth\@$server/;use_ssl=always</url>"; |
---|
[22998] | 102 | $a .= '</source>'; |
---|
| 103 | $a .= '<transport save-passwd="false">'; |
---|
| 104 | $a .= "<url>smtp://$user;auth=GSSAPI\@outgoing.mit.edu/;use_ssl=always"; |
---|
| 105 | $a .= '</url>'; |
---|
| 106 | $a .= '</transport>'; |
---|
| 107 | $a .= "<drafts-folder>$local#Drafts</drafts-folder>"; |
---|
| 108 | $a .= "<sent-folder>$local#Sent</sent-folder>"; |
---|
| 109 | $a .= '</account>' . "\n"; |
---|
| 110 | $accounts = [ $a ]; |
---|
| 111 | $client->set_list("/apps/evolution/mail/accounts", "string", $accounts); |
---|
| 112 | $client->set_string("/apps/evolution/mail/default_account", "mitinitial"); |
---|
[23955] | 113 | create_initial_state_file(); |
---|
[22998] | 114 | } |
---|
| 115 | |
---|
| 116 | exec("/usr/bin/evolution.debathena-orig", @ARGV); |
---|
[23955] | 117 | |
---|
| 118 | # Edit the existing folder tree state file, to update any URI referring to |
---|
| 119 | # the old server. |
---|
| 120 | sub edit_state_file() { |
---|
| 121 | return unless $old_server; |
---|
| 122 | my $file = "$homedir/.evolution/mail/config/folder-tree-expand-state.xml"; |
---|
| 123 | open(OLD, "<$file") or return; |
---|
| 124 | my $newfile = $file . ".debathena-new"; |
---|
| 125 | unless (open(NEW, ">$newfile")) { |
---|
| 126 | warn "Cannot open $newfile: $!\n"; |
---|
| 127 | close OLD; |
---|
| 128 | return; |
---|
| 129 | } |
---|
| 130 | my $changed = 0; |
---|
| 131 | # Loop to copy and edit the file, replacing the old IMAP server |
---|
| 132 | # name with the new. |
---|
| 133 | while (<OLD>) { |
---|
| 134 | my $old = $_; |
---|
| 135 | s%(imap://$user)(;[^\@]*)?\@($old_server)%$1\@$server%i; |
---|
| 136 | print NEW; |
---|
| 137 | $changed = 1 if ($_ ne $old); |
---|
| 138 | } |
---|
| 139 | close NEW; |
---|
| 140 | close OLD; |
---|
| 141 | # If we have changed the file, move the new one into place. |
---|
| 142 | # Otherwise, just delete the copy. |
---|
| 143 | if ($changed) { |
---|
| 144 | unless (rename($newfile, $file)) { |
---|
| 145 | warn "Cannot rename $newfile to $file: $!\n"; |
---|
| 146 | # Nuke the old file to be safe. |
---|
| 147 | unlink $file; |
---|
| 148 | } |
---|
| 149 | } else { |
---|
| 150 | unlink $newfile; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | # Create the initial folder tree state file, to expand and select the |
---|
| 155 | # MIT INBOX. |
---|
| 156 | sub create_initial_state_file() { |
---|
| 157 | my $file = "$homedir/.evolution/mail/config/folder-tree-expand-state.xml"; |
---|
| 158 | |
---|
| 159 | mkpath(dirname($file), 0, 0700); |
---|
| 160 | unless (open(OUT, ">$file")) { |
---|
| 161 | warn "Cannot open $file: $!\n"; |
---|
| 162 | return; |
---|
| 163 | } |
---|
| 164 | print OUT "<?xml version=\"1.0\"?>\n"; |
---|
| 165 | print OUT "<tree-state>\n"; |
---|
| 166 | print OUT " <node name=\"local\" expand=\"true\"/>\n"; |
---|
| 167 | print OUT " <node name=\"vfolder\" expand=\"false\"/>\n"; |
---|
| 168 | print OUT " <node name=\"mitinitial\" expand=\"true\">\n"; |
---|
| 169 | print OUT " <node name=\"INBOX\" expand=\"true\"/>\n"; |
---|
| 170 | print OUT " </node>\n"; |
---|
| 171 | print OUT " <selected uri=\"imap://$user\@$server/INBOX\"/>\n"; |
---|
| 172 | print OUT "</tree-state>\n"; |
---|
| 173 | close OUT; |
---|
| 174 | } |
---|