5 # this script goes through all the config keys that deal with
6 # accounts and replaces accounts referenced by name to be referenced
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 = "";
17 next if ( /^$/ ); # skip empty lines
18 next if ( /^\#/ ); # skip comments
19 if ( /^\[/ ) { # group begin
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
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
40 foreach my $accountGroup (@accountGroups) {
43 $id = int(rand 0x7fFFffFF);
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) {
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|");
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 )
75 # extract the accountname
76 my (@parts) = split (/\[Folder-/,$folderGroup);
77 my $account = substr($parts[1], 0, -1);
78 if ($account =~ /^[^\/]*\
.directory\
//)
81 my (@dirparts) = split (/\.directory\//,$account);
82 $account = substr( $dirparts[0], 1 );
83 # this is no root folder
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/;
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";
103 # new label and id of this rootfolder
104 print "SystemLabel=$account\n";
105 print "Id=".$id."\n";
109 if ($nameToType{$accountDecoded} eq "imap") {
111 } elsif ($nameToType{$accountDecoded} eq "cachedimap") {
114 my $oldname = QFileEncode
( "$basedir/$subdir/\.$account\.directory" );
115 my $systemcall = "mv '$oldname' '$basedir/$subdir/\.".$id."\.directory'";
118 $oldname = QFileEncode
( "$basedir/$subdir/$account" );
119 $systemcall = "mv '$oldname' '$basedir/$subdir/".$id."'";
122 $oldname = QFileEncode
( "$basedir/$subdir/\.$account\.index" );
123 $systemcall = "mv '$oldname' '$basedir/$subdir/\.".$id."\.index'";
126 $oldname = QFileEncode
( "$basedir/$subdir/\.$account\.index.ids" );
127 $systemcall = "mv '$oldname' '$basedir/$subdir/\.".$id."\.index.ids'";
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";
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";
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
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}/;
214 ## emulate QFileDecode
220 $input =~ s/%40/\@/g;
221 $input =~ s/%25/%/g; # must be the last one
226 ## emulate QFileEncode
231 $input =~ s/%/%25/g; # must be the first one
233 $input =~ s/\@/%40/g;