SVN_SILENT made messages (.desktop file)
[kdepim.git] / kmail / kconf_update / kmail-3.3-use-ID-for-accounts.pl
blob21daf743fce32ad35033b8c19405d11c2ea9626e
1 #!/usr/bin/perl -w
3 use strict;
5 # this script goes through all the config keys that deal with
6 # accounts and replaces accounts referenced by name to be referenced
7 # by IDs.
8 # It also renames the toplevel-folder and the sent/trash/drafts folder
9 # accordingly and renames all references
10 # last but not least we move the lokal folder-cache of (d)imap folders
12 # read the whole config file
13 my $currentGroup = "";
14 my %configFile;
15 while ( <> ) {
16 chomp;
17 next if ( /^$/ ); # skip empty lines
18 next if ( /^\#/ ); # skip comments
19 if ( /^\[/ ) { # group begin
20 $currentGroup = $_;
21 next;
22 } elsif ( $currentGroup =~ /^\[Account/ and /^Id/ ) {
23 # We need to prevent this script from running twice, since it
24 # would change IDs of accounts then.
25 # Presence of a id key in an Account section is the
26 # best indicator
27 exit;
28 } elsif ( $currentGroup ne "" ) { # normal entry
29 my ($key,$value) = split /=/;
30 $configFile{$currentGroup}{$key}=$value;
34 # filter out account groups
35 my @accountGroups = grep { /^\[Account \d+\]/ } keys %configFile;
37 # create IDs for each account
38 my %nameToID;
39 my %nameToType;
40 foreach my $accountGroup (@accountGroups) {
41 my $id;
42 do {
43 $id = int(rand 0x7fFFffFF);
44 } while ($id <= 0);
45 my $name = $configFile{$accountGroup}{'Name'};
46 # remember id and type
47 $nameToID{$name} = $id;
48 $nameToType{$name} = $configFile{$accountGroup}{'Type'};
49 # create the id entries
50 print "${accountGroup}\nId=$id\n";
53 foreach my $accountGroup (@accountGroups) {
54 # rename the trash
55 my $trash = $configFile{$accountGroup}{'trash'};
56 if (&replaceID($trash)) {
57 print "# DELETE ".$accountGroup."trash\n";
58 print "${accountGroup}\ntrash=".&replaceID($trash)."\n";
62 # we need the directory where the imap cache is stored
63 open(CMD, "kde4-config --localprefix|");
64 my $basedir = <CMD>;
65 chomp( $basedir );
66 $basedir = $basedir."share/apps/kmail";
68 # Now, go through all [Folder-*] groups that belong to (d)imap folders
69 # and replace the account name with the id
70 my @folderGroups = grep { /^\[Folder-.*\]/ } keys %configFile;
72 foreach my $folderGroup ( @folderGroups )
74 my $isRootFolder = 1;
75 # extract the accountname
76 my (@parts) = split (/\[Folder-/,$folderGroup);
77 my $account = substr($parts[1], 0, -1);
78 if ($account =~ /^[^\/]*\.directory\//)
80 # .account.directory
81 my (@dirparts) = split (/\.directory\//,$account);
82 $account = substr( $dirparts[0], 1 );
83 # this is no root folder
84 $isRootFolder = 0;
86 # delete the old group and write the new entry
87 my $accountDecoded = QFileDecode( $account );
88 if ( exists( $nameToID{$accountDecoded} ) )
90 my $id = $nameToID{$accountDecoded};
91 print "# DELETEGROUP $folderGroup\n";
92 my $folderGroupNew = $folderGroup;
93 my $pattern = quotemeta( $account );
94 $folderGroupNew =~ s/$pattern/$id/;
95 # new account section
96 print "$folderGroupNew\n";
97 # print all original keys
98 my %groupData = %{$configFile{$folderGroup}};
99 foreach my $key ( keys %groupData ) {
100 print "$key=" . $groupData{$key} . "\n";
102 if ($isRootFolder) {
103 # new label and id of this rootfolder
104 print "SystemLabel=$account\n";
105 print "Id=".$id."\n";
107 # move the directory
108 my $subdir;
109 if ($nameToType{$accountDecoded} eq "imap") {
110 $subdir = "imap";
111 } elsif ($nameToType{$accountDecoded} eq "cachedimap") {
112 $subdir = "dimap";
114 my $oldname = QFileEncode( "$basedir/$subdir/\.$account\.directory" );
115 my $systemcall = "mv '$oldname' '$basedir/$subdir/\.".$id."\.directory'";
116 system($systemcall);
118 $oldname = QFileEncode( "$basedir/$subdir/$account" );
119 $systemcall = "mv '$oldname' '$basedir/$subdir/".$id."'";
120 system($systemcall);
122 $oldname = QFileEncode( "$basedir/$subdir/\.$account\.index" );
123 $systemcall = "mv '$oldname' '$basedir/$subdir/\.".$id."\.index'";
124 system($systemcall);
126 $oldname = QFileEncode( "$basedir/$subdir/\.$account\.index.ids" );
127 $systemcall = "mv '$oldname' '$basedir/$subdir/\.".$id."\.index.ids'";
128 system($systemcall);
133 # go through all identities and replace the sent-mail and drafts folder
134 my @identities = grep { /^\[Identity #\d\]/ } keys %configFile;
136 foreach my $identity (@identities)
138 my $drafts = $configFile{$identity}{'Drafts'};
139 my $sent = $configFile{$identity}{'Fcc'};
140 # extract the account name
141 if (&replaceID($drafts))
143 print "# DELETE ".$identity."Drafts\n";
144 print "${identity}\nDrafts=".&replaceID($drafts)."\n";
146 if (&replaceID($sent))
148 print "# DELETE ".$identity."Fcc\n";
149 print "${identity}\nFcc=".&replaceID($sent)."\n";
153 # go through all filters and replace the target
154 my @filterGroups = grep { /^\[Filter \#\d+\]/ } keys %configFile;
155 foreach my $filterGroup (@filterGroups)
157 my $numActions = +$configFile{$filterGroup}{'actions'};
158 # go through all actions in search for "set identity":
159 for ( my $i = 0 ; $i < $numActions ; ++$i )
161 my $actionName = "action-name-$i";
162 my $actionArgs = "action-args-$i";
163 if ( $configFile{$filterGroup}{$actionName} eq "transfer" &&
164 &replaceID($configFile{$filterGroup}{$actionArgs}) )
166 print "# DELETE $filterGroup$actionArgs\n";
167 print "$filterGroup\n$actionArgs=".
168 &replaceID($configFile{$filterGroup}{$actionArgs})."\n";
173 # previous fcc
174 my $pfcc = $configFile{'[Composer]'}{'previous-fcc'};
175 if (&replaceID($pfcc)) {
176 print "# DELETE [Composer]previous-fcc\n";
177 print "[Composer]\nprevious-fcc=".&replaceID($pfcc)."\n";
180 # GroupwareFolder
181 my $groupware = $configFile{'[Groupware]'}{'GroupwareFolder'};
182 if (&replaceID($groupware)) {
183 print "# DELETE [Groupware]GroupwareFolder\n";
184 print "[Groupware]\nGroupwareFolder=".&replaceID($groupware)."\n";
187 # and finally the startupFolder
188 my $startup = $configFile{'[General]'}{'startupFolder'};
189 if (&replaceID($startup)) {
190 print "# DELETE [General]startupFolder\n";
191 print "[General]\nstartupFolder=".&replaceID($startup)."\n";
194 ## Returns input string with replaced account name
195 ## If there is nothing to replace it returns undef
196 sub replaceID
198 my ($input) = @_;
200 if ($input && $input =~ /\.directory/)
202 my (@dirparts) = split (/\.directory\//,$input);
203 my $account = substr( $dirparts[0], 1 );
204 my $accountDecoded = QFileDecode( $account );
205 if ( exists( $nameToID{$accountDecoded} ) )
207 my $pattern = quotemeta( $account );
208 $input =~ s/$pattern/$nameToID{$accountDecoded}/;
209 return $input;
214 ## emulate QFileDecode
215 sub QFileDecode
217 my ($input) = @_;
219 $input =~ s/%20/ /g;
220 $input =~ s/%40/\@/g;
221 $input =~ s/%25/%/g; # must be the last one
223 return $input;
226 ## emulate QFileEncode
227 sub QFileEncode
229 my ($input) = @_;
231 $input =~ s/%/%25/g; # must be the first one
232 $input =~ s/ /%20/g;
233 $input =~ s/\@/%40/g;
235 return $input;