source: trunk/debathena/debathena/evolution-wrapper/evolution.debathena @ 25749

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