* updated test case, code cleanup, documentation fixes
[modest] / scripts / modest-parse-presets.pl
1 #! /usr/bin/perl -w
2
3 # Copyright (c) 2006, Nokia Corporation
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 #   notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 #   notice, this list of conditions and the following disclaimer in the
14 #   documentation and/or other materials provided with the distribution.
15 # * Neither the name of the Nokia Corporation nor the names of its
16 #   contributors may be used to endorse or promote products derived from
17 #   this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31
32 #
33 # Perl script to converts an Excel spreadsheet (arg 1)
34 # with provider information in a GKeyFile readable format
35 # for use in modest (see modest-presets.[ch] for details]
36  
37 # The input is assumed to be MS-style UTF16-LE, and
38 # output will be UTF-8. 
39 ##
40 # Program requires Spreadsheet::ParseExcel, which does all
41 # the interesting stuff
42
43 # input columns are like this:
44 # 0: MCC (0 For Global Email)
45 # 1: Mailbox Name
46 # 2: EmailAddress
47 # 3: OutgoingMailServer
48 # 4: Secure smtp: (0 = no\, 1=yes)
49 # 5: IncomingMailServer
50 # 6: SendMessage (0=Immediately; 1=During next conn.)
51 # 7: SendCopyToSelf (0=No; 1=Yes)
52 # 8: MailboxType (0=POP3; 1=IMAP4)
53 # 9: Security (0=Off; 1=On(143/110); 2=On(993/995);)
54 #10: APOPSecureLogin (0=Off; 1=On)
55 #11: RetrieveAttachment (0=No; 1=Yes)
56 #12: RetrieveHeaders (0=All; 1-99=User defined)
57
58 # some of these are properties of user-settings; however,
59 # we are only interested in server settings, so some
60 # of the data (6,7,11,12) will be ignored for our output.
61
62 use strict;
63 use Spreadsheet::ParseExcel;
64 #use Unicode::String qw(utf8 utf16le);
65 #Unicode::String->stringify_as('utf8');
66
67 die "usage: xls2cvs <file.xls>\n" unless @ARGV == 1;
68
69 my $file = $ARGV[0];
70 die "'$file' is not a readable file\n" unless -r $file;
71   
72
73 my $xl   = new Spreadsheet::ParseExcel;
74 my $data = $xl->Parse($file) or die "could not parse $file: $!";
75
76 my $sheet = $data->{Worksheet}[0];
77
78 my $now = `date`;
79 chomp $now;
80
81 print "# generated on $now from " . $data->{File} . "\n";
82 print "# keys and their meaning:\n";
83 print "# [MailboxName]: name of the provider (eg. Wanadoo, Gmail)\n" .
84       "# MCC: Mobile Country Code (Netherlands=204, France=208, ...\n" . 
85       "#                           globals like GMail don't have one)\n" .
86       "# OutgoingMailServer: name of the smtp server (eg. smtp.foo.fi)\n" . 
87       "# SecureSMTP: 'true' if there's secure SMTP\n" .
88       "# IncomingMailServer\n" .
89       "# MailboxType: 'pop' or 'imap'\n" .
90       "# SMTPSecurity: 'true' if SMTP is secure\n" .
91       "# APOPSecureLogin: 'true' if APOP is supported\n\n";
92
93 # ignore the first row
94 for (my $r = $sheet->{MinRow} + 1 ; defined $sheet->{MaxRow} && $r <= $sheet->{MaxRow}; ++$r) {
95
96     my $cell;
97
98     # legend:
99     # 0: MCC (mobile country code, or 0 for Global)
100     # 1: MailboxName
101     # 2: EmailAddress
102     # 3: OutgoingMailServer
103     # 4: SecureSmtp
104     # 5: IncomingMailServer
105     # 6: SendMessage
106     # 7:
107     
108     next unless ($sheet->{Cells}[$r][0] && $sheet->{Cells}[$r][0]->Value =~ /\d+/);
109
110     # name -> required, unique
111     $cell = $sheet->{Cells}[$r][1];
112     next unless ($cell);
113     print "[" . $cell->Value . "]\n";
114
115     # MCC -> TODO: convert to normal country code
116     $cell = $sheet->{Cells}[$r][0];
117     if ($cell->Value > 0) {
118         print "MCC=" . $cell->Value . "\n";
119     }
120
121     # address -> required, unique
122     #$cell = $sheet->{Cells}[$r][2];
123     #print "EmailAddress=" . $cell->Value . "\n";
124
125     # OutgoingMailServer
126     $cell = $sheet->{Cells}[$r][3];
127     print "OutgoingMailServer=" . $cell->Value . "\n" if ($cell);
128
129     # SecureSmtp?
130     $cell = $sheet->{Cells}[$r][4];
131     print "SecureSmtp=true\n"  if ($cell && $cell->Value == 1);
132     
133     # IncomingMailServer
134     $cell = $sheet->{Cells}[$r][5];
135     if ($cell) {
136         print "IncomingMailServer=" . $cell->Value;
137         
138         my $type = $sheet->{Cells}[$r][8]->Value;
139         my $sec =  $sheet->{Cells}[$r][9]->Value;
140         
141         if ($sec == 2) {
142             if ($type == 0) { print ":995";}
143             if ($type == 1) { print ":993";}
144         }
145         print "\n";
146         print "IncomingSecurity=$sec\n";
147     }
148     
149     # MailboxType
150     $cell = $sheet->{Cells}[$r][8];
151     if ($cell) {
152         print "MailboxType=";
153         if ($cell->Value == '0') {print "pop"} else {print "imap"};
154         print "\n";
155     }
156
157     $cell = $sheet->{Cells}[$r][10];
158     print "APOPSecureLogin=true\n"  if ($cell && $cell->Value == 1);
159 }
160
161 # the end