~vcs-imports/amigu/svn-trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# -*- coding: utf-8 -*-

import os, re, random, time, shutil
import commands
import glob
from amigu import _
import binascii
from xml.dom import minidom
from amigu.util.folder import *
from amigu.apps.base import application


__DIR_PST2MBX__="/usr/bin"
__DIR_DBX2MBX__="/usr/bin"


class mailconfig:
    """Clase para las configuraciones de correo.
    Visit http://msdn2.microsoft.com/en-us/library/ms715237.aspx for more information about the values"""

    def __init__(self, config):
        """Constructor de la clase.
        Recibe la configuración obtenida del registro"""
        if isinstance(config, dict):
            self.c = config
        elif os.path.exists(config):
            self.c = self.readconfig_wm(config)
        else:
            raise Exception
        if not self.get_server():
            raise Exception
        #print self.c
        

    def get_type(self):
        """Devuelve el tipo de cuenta de correo"""
        if 'IMAP Server' in self.c.keys():
            t = "imap"
        elif 'POP3 Server' in self.c.keys():
            t = "pop3"
        elif 'HTTPMail Server' in self.c.keys():
            t = "HTTPMail"
        else:
            t = "Unknown"
        return t

    def get_user_name(self):
        """Devuelve el nombre del usuario"""
        if self.get_type() == "imap":
            if 'IMAP User Name' in self.c.keys():
                r = self.c['IMAP User Name']
            elif 'IMAP User' in self.c.keys():
                r = self.c['IMAP User']
        elif self.get_type() == "pop3":
            if 'POP3 User Name' in self.c.keys():
                r = self.c['POP3 User Name']
            elif 'POP3 User' in self.c.keys():
                r = self.c['POP3 User']
        elif self.get_type() == "HTTPmail":
            r = self.c['HTTPMail User Name']
        else:
            r = None
        return r

    def get_account_name(self):
        """Devuelve el nombre de la cuenta de correo"""
        return self.c['Account Name']

    def get_connection_type(self):
        """Devuelve el tipo de conexión con el servidor"""
        return self.c['Connection Type']

    def get_server(self):
        """Devuelve el servidor de correo entrante"""
        if self.get_type() == "imap":
            r = self.c['IMAP Server']
        elif self.get_type() == "pop3":
            r = self.c['POP3 Server']
        elif self.get_type() == "HTTPMail":
            r = self.c['HTTPMail Server']
        else:
            r = None
        return r

    def use_SSL(self):
        """Devuelve si la conexión es segura"""
        if self.get_type() == "imap" and 'IMAP Secure Connection' in self.c.keys():
            r = hex2dec(self.c['IMAP Secure Connection'])
        elif self.get_type() == "imap" and 'IMAP Use SSL' in self.c.keys():
            r = hex2dec(self.c['IMAP Use SSL'])
        elif self.get_type() == "pop3" and 'POP3 Secure Connection' in self.c.keys():
            r = hex2dec(self.c['POP3 Secure Connection'])
        elif self.get_type() == "pop3" and 'POP3 Use SSL' in self.c.keys():
            r = hex2dec(self.c['POP3 Use SSL'])
        else:
            r = None
        return r

    def get_port(self):
        """Devuelve el puerto del servidor de correo entrante"""
        if self.get_type() == "imap":
            if 'IMAP Port' in self.c.keys():
                r = hex2dec(self.c['IMAP Port'])
            elif self.use_SSL():
                r = 993
            else:
                r = 143
        elif self.get_type() == "pop3":
            if 'POP3 Port' in self.c.keys():
                r = hex2dec(self.c['POP3 Port'])
            elif self.use_SSL():
                r = 995
            else:
                r = 110
        else:
            r = None
        return r

    def get_timeout(self):
        """Devuelve el tiempo de espera"""
        try:
            if self.get_type() == "imap":
                r = self.c['IMAP Timeout']
            elif self.get_type() == "pop3":
                r = self.c['POP3 Timeout']
        except:
            r = 60
        return r

    def remove_expired(self):
        """Devuelve si se eliminan los correos con el paso del tiempo"""
        if 'Remove When Expired' in self.c.keys():
            return hex2dec(self.c['Remove When Expired']) and 'true' or 'false'

    def remove_deleted(self):
        """Devuelve si se eliminan los correos del servidor al ser borrados"""
        if 'Remove When Deleted' in self.c.keys():
            return hex2dec(self.c['Remove When Deleted']) and 'true' or 'false'

    def get_SMTP_display_name(self):
        """Devuelve el nombre del servidor de correo saliente"""
        if 'SMTP Display Name' in self.c.keys():
            return self.c['SMTP Display Name']
        elif 'Display Name' in self.c.keys():
            return self.c['Display Name']
        else:
            return ""

    def get_SMTP_port(self):
        """Devuelve el puerto del servidor de correo saliente"""
        if 'SMTP Port' in self.c.keys():
            return hex2dec(self.c['SMTP Port'])
        else:
            return 25

    def get_SMTP_server(self):
        """Devuelve el servidor de correo saliente"""
        return self.c['SMTP Server']

    def get_SMTP_email_address(self):
        """Devuelve la direccion de correo saliente"""
        if 'SMTP Email Address' in self.c.keys():
            return self.c['SMTP Email Address']
        elif 'Email' in self.c.keys():
            return self.c['Email']


    def get_SMTP_timeout(self):
        """Devuelve el tiempo de espera del servidor de correo saliente"""
        if 'SMTP Timeout' in self.c.keys():
            return hex2dec(self.c['SMTP Timeout'])
        else: 
            return 60

    def use_SMTP_SSL(self):
        """Devuelve si la conexión con el servidor de correo saliente es segura"""
        if 'SMTP Secure Connection' in self.c.keys():
            return hex2dec(self.c['SMTP Secure Connection'])
        elif 'SMTP Use SSL' in self.c.keys():
            return hex2dec(self.c['SMTP Use SSL'])

    def leave_mail(self):
        """Devuelve si los mensajes deben conservarse en el servidor"""
        r = 'false'
        if 'Leave Mail On Server' in self.c.keys():
            r = hex2dec(self.c['Leave Mail On Server']) and 'true' or 'false'
        return r
        
    def readconfig_wm(self, config):
        c = {}
        xmldoc = minidom.parse(config)
        for node in xmldoc.firstChild.childNodes:
            if node.nodeType == node.ELEMENT_NODE:
                c[node.tagName.replace('_',' ')] = node.firstChild.data
        return c
        
    
            
# end class mail

############################################################################

class mailreader(application):
    
    def initialize(self):
        self.name = _("Lector de correo")
        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []
        self.dest = folder(os.path.join(os.path.expanduser('~'),'.evolution','mail','local', _("Correo de ")+ self.name +'.sbd'))
        self.description = self.name +": %d"%len(self.mailconfigs)+ _("cuentas de correo")
        
    def get_configuration(self):
       pass

    def do(self):
        self.update_progress(5.0)
        self.import_accounts()
        self.import_mails()
        self.update_progress(80.0)
        self.import_contacts()
        self.update_progress(90.0)
        self.import_calendar()
        return 1

    def import_mails(self):
        self.dest = folder(os.path.join(os.path.expanduser('~'),'.evolution','mail','local', _("Correo de ")+ self.name +'.sbd'))
        for mb in self.mailboxes:
            self.convert_mailbox(mb)
            self.update_progress(delta=30.0/len(self.mailconfigs))
            
    def convert_mailbox(self, mb):
        pass

    def import_accounts(self):
        for a in self.mailconfigs:
            self.config_EVOLUTION(a)
            self.config_THUNDERBIRD(a)
            self.update_progress(delta=45.0/len(self.mailboxes))
        
    def import_calendar(self):
        self.config_EVOLUTION_calendar()
        
    def import_contacts(self):
        self.config_EVOLUTION_addressbook()

    def config_EVOLUTION(self, a):
        """Configura la cuenta dada en Evolution"""
        
        #a = self.mailconfig
        ssl = a.use_SSL() and 'always' or 'never'
        smtpssl = a.use_SMTP_SSL() and 'always' or 'never'
        server_type = a.get_type()
        if server_type == 'pop3':
            m = 'pop'
            draft = "mbox:" + os.path.expanduser('~') + "/.evolution/mail/local#Drafts"
            sent = "mbox:" + os.path.expanduser('~') + "/.evolution/mail/local#Sent"
            command = ''
        elif server_type == 'imap':
            m = 'imap'
            draft, sent = '', ''
            command = ";check_all;command=ssh%20-C%20-l%20%25u%20%25h%20exec%20/usr/sbin/imapd"
        elif server_type == "HTTPMail" or server_type == "Unknown":
            warning("Account type %s won't be added" % server_type)
            return 0
        # get previous accounts
        try:
            l = os.popen("gconftool --get /apps/evolution/mail/accounts")
            laccounts = str(l.read())
            l.close()
            if not laccounts:
                laccounts = "[]\n"
            elif laccounts.find(a.get_SMTP_email_address()) > 0:
                return 1
        except:
            error ("Evolution configuration is not readable")
        else:
            # generate new xml list
            e = "<?xml version=\"1.0\"?>\n" + \
            "<account name=\"%s\" uid=\"%s\" enabled=\"true\">" % (a.get_account_name(), str(time.time())) + \
            "<identity>" + \
            "<name>%s</name>" % a.get_SMTP_display_name() + \
            "<addr-spec>%s</addr-spec><signature uid=\"\"/>" % a.get_SMTP_email_address() + \
            "</identity>" + \
            "<source save-passwd=\"false\" keep-on-server=\"%s\" auto-check=\"true\" auto-check-timeout=\"10\">" % a.leave_mail() + \
            "<url>%s://%s@%s:%d/%s;use_ssl=%s</url>" % (m, a.get_user_name().replace('@','%40'), a.get_server(), a.get_port(), command, ssl) + \
            "</source>" + \
            "<transport save-passwd=\"false\">" + \
            "<url>smtp://%s;auth=PLAIN@%s:%d/;use_ssl=%s</url>" % (a.get_user_name().replace('@','%40'), a.get_SMTP_server(), a.get_SMTP_port(), smtpssl) + \
            "</transport>" + \
            "<drafts-folder>%s</drafts-folder>" % draft + \
            "<sent-folder>%s</sent-folder>" % sent + \
            "<auto-cc always=\"false\"><recipients></recipients></auto-cc><auto-bcc always=\"false\"><recipients></recipients></auto-bcc><receipt-policy policy=\"never\"/>" + \
            "<pgp encrypt-to-self=\"false\" always-trust=\"false\" always-sign=\"false\" no-imip-sign=\"false\"/><smime sign-default=\"false\" encrypt-default=\"false\" encrypt-to-self=\"false\"/>" + \
            "</account>\n"
            # concatenate elemennt to the list
            #print e
            if laccounts == "[]\n":
                #create list
                laccounts = "[" + e +"]"
            else:
                #add to list
                laccounts = laccounts[:-2] + ',' + e + "]"
            # set the modified list
            try:
                os.system("gconftool --set /apps/evolution/mail/accounts --type list --list-type string \"" + laccounts.replace("\"", "\\\"") +"\"")
                progress("Account %s added successfully" % a.get_account_name())
            except:
                self.errors += "Evolution configuration is not writable"
    
    def config_EVOLUTION_calendar(self):
        vcal = os.path.join(self.dest.path, _("Calendario"))
        old = None
        dates = []
        if os.path.exists(vcal):
            evo_cal = os.path.join(os.path.expanduser('~'),'.evolution','calendar','local','system','calendar.ics')
            folder(os.path.dirname(evo_cal))
            if os.path.exists(evo_cal):
                old = backup(evo_cal)
                if old:
                    new_cal = open(evo_cal, "w")
                    old_cal = open(old, 'r')
                    for l in old_cal.readlines():
                        if l.find('END:VCALENDAR') == -1:
                            new_cal.write(l)
                        if l.find('DTSTART') != -1:
                            dates.append(l.replace('DTSTART:', ''))
                    old_cal.close()
            orig = open(vcal,"r")
            events = False
            if not old:
                new_cal = open(evo_cal, "w")
                new_cal.write('BEGIN:VCALENDAR\n')
                new_cal.write('CALSCALE:GREGORIAN\n')
                new_cal.write('VERSION:2.0\n')
            buffer = ''
            for l in orig.readlines():
                buffer += l
                if l.find('BEGIN:VEVENT') != -1:
                    buffer = l
                    repeated = False
                elif l.find('END:VEVENT') != -1:
                    if not repeated:
                        new_cal.write(buffer)
                elif l.find('DTSTART') != -1:
                    repeated = l.replace('DTSTART:', '') in dates
                elif l.find('SUMMARY') != -1:
                    #repeated = l.replace('SUMMARY:', '') in dates
                    pass
                
            new_cal.write('END:VCALENDAR\n')
            orig.close()
            os.remove(vcal)
                
    def config_EVOLUTION_addressbook(self):
        """Lee los contactos alamcenados en Evolution"""
        vcard = os.path.join(self.dest.path, _("Contactos"))
        if os.path.exists(vcard):
            import bsddb
            adb=os.path.join(os.path.expanduser('~'),'.evolution','addressbook','local','system','addressbook.db')
            folder(os.path.dirname(adb))
            db = bsddb.hashopen(adb,'w')
            if not 'PAS-DB-VERSION\x00' in db.keys():
                db['PAS-DB-VERSION\x00'] = '0.2\x00'
            contacts = open(vcard, 'r')
            while 1:
                l = contacts.readline()
                if not l:
                    break
                if l.find('BEGIN:VCARD') != -1:
                    randomid = 'pas-id-' + str(random.random())[2:]
                    db[randomid+'\x00'] = 'BEGIN:VCARD\r\nUID:' + randomid + '\r\n'
                    while 1:
                        v = contacts.readline()
                        if v.find('END:VCARD') != -1:
                            db[randomid+'\x00'] += 'END:VCARD\x00'
                            break
                        else:
                            db[randomid+'\x00'] += v.replace('PERSONAL','HOME').replace('\n', '\r\n')
            db.sync()
            db.close()
            os.remove(vcard)
    
    def config_THUNDERBIRD(self, a):
        th = thunderbird()
        try:
            th.config_account(a)
        except:
            pass


class outlook12(mailreader):
    
    def initialize(self):
        
        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []
        
        self.name = _("Outlook 2007")
        self.description = _("Datos y configuraciones de MS Office Outlook 2007")
        pst = os.path.join(self.user.folders["Local AppData"].path, 'Microsoft','Outlook', 'Outlook.pst')
        if os.path.exists(pst):
            self.mailboxes.append(pst)
        self.mailconfigs = self.get_configuration()
        if not self.mailconfigs:
            raise Exception

        for mb in self.mailboxes:
            self.size = os.path.getsize(mb)/1024
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")


    def get_configuration(self):
        configs = []
        for key in self.option:
            r = self.user.search_key(key)
            if not "Email" in r.keys():
                continue
            pst = None
            for k, v in r.iteritems():
                if v.startswith('hex'):
                    r[k]=hex2str(v)
                if k.find('pst') > 0:
                    pst = k
                if k == 'IMAP Store EID':
                    pst = r[k]
            if pst:
                i = pst.find(':')
                pst = self.user.check_path(pst)
                pst = pst.replace('\\','/')
                if os.path.exists(pst) and not pst in self.mailboxes:
                    self.mailboxes.append(pst)
            try:
                c = mailconfig(r)
            except:
                continue
            else:
                configs.append(c)
        return configs

    def convert_mailbox(self, mb):
        readpst = os.path.join(__DIR_PST2MBX__,'readpst')
        com = '%s -w %s -o %s' % (readpst, mb.replace(' ',"\ "), self.dest.path.replace(' ',"\ "))
        os.system(com)



class outlook11(mailreader):
    
    def initialize(self):

        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []

        self.name = _("Outlook XP-2002-2003")
        pst = glob.glob(os.path.join(self.user.folders["Local AppData"].path, 'Microsoft','Outlook', '?utlook.pst'))
        if pst and os.path.exists(pst[0]):
            self.mailboxes.append(pst[0])
        self.mailconfigs = self.get_configuration()
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")
        if not self.mailconfigs:
            raise Exception

        for mb in self.mailboxes:
            self.size = os.path.getsize(mb)/1024

    def get_configuration(self):
        configs = []
        for key in self.option:
            r = self.user.search_key(key)
            if not "Email" in r.keys():
                continue
            for k, v in r.iteritems():
                if v.startswith('hex'):
                    r[k]=hex2str(v)
            
            try:
                pst_file = "*Outlook*"+c.get_server()+'-'+self.option.split('\\')[-1][:-3]+'.pst'
                pst_file = glob.glob(os.path.join(self.user.folders["Local AppData"].path, 'Microsoft','Outlook', pst_file))[0]
                if os.path.exists(pst_file) and not pst_file in self.mailboxes:
                    self.mailboxes.append(pst_file)
            except:
                pass
            try:
                c = mailconfig(r)
            except:
                continue
            else:
                configs.append(c)
        return configs

    def convert_mailbox(self, mb):
        readpst = os.path.join(__DIR_PST2MBX__,'readpst')
        com = '%s -w %s -o %s' % (readpst, mb.replace(' ',"\ "), self.dest.path.replace(' ',"\ "))
        os.system(com)


class outlook9(mailreader):
    
    def initialize(self):

        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []

        self.name = _("Outlook 2000")
        self.description = _("Datos y configuraciones de MS Office Outlook 2000")
        pst = glob.glob(os.path.join(self.user.folders["Local AppData"].path, 'Microsoft','Outlook', '?utlook.pst'))

        if pst and os.path.exists(pst[0]):
            self.mailboxes.append(pst[0])
        self.mailconfigs = self.get_configuration()
        for mb in self.mailboxes:
            self.size = os.path.getsize(mb)/1024
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")
        if not self.mailconfigs:
            raise Exception

    def get_configuration(self):
        configs = []
        for key in self.option:
            r = self.user.search_key(key)
            for k, v in r.iteritems():
                if k.find('pst') > 0:
                    pst = self.user.check_path(k)
                    if os.path.exists(pst) and not pst in self.mailboxes:
                        self.mailboxes.append(pst)
            try:
                c = mailconfig(r)
            except:
                continue
            else:
                configs.append(c)
        return configs

    def convert_mailbox(self, mb):
        readpst = os.path.join(__DIR_PST2MBX__,'readpst')
        com = '%s -w %s -o %s' % (readpst, mb.replace(' ',"\ "), self.dest.path.replace(' ',"\ "))
        os.system(com)

        
class outlook_express(mailreader):
    
    def initialize(self):
        
        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []

        self.name = _("Outlook Express")
        self.mailconfigs = self.get_configuration()
        for mb in self.mailboxes:
            self.size += mb.get_size()
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")
        if not self.mailconfigs:
            raise Exception

    def get_configuration(self):
        configs = []
        for key in self.option:
            r = self.user.search_key(key)
            
            mb = glob.glob(os.path.join(self.user.folders["Local AppData"].path, 'Identities','{*}','Microsoft','Outlook Express'))
            if mb:
                self.mailboxes.append(folder(mb[0]))
            try:
                c = mailconfig(r)
            except:
                continue
            else:
                configs.append(c)
        return configs

    def convert_mailbox(self, mb):
        readdbx = os.path.join(__DIR_DBX2MBX__,'readoe')
        output = self.dest.path
        com = '%s -i %s -o %s' % (readdbx, mb.path.replace(' ',"\ "), output.replace(' ','\ '))
        os.system(com)
        for e in os.listdir(output):
            if os.path.splitext(e)[-1] == '.dbx':
                shutil.move(os.path.join(output, e), os.path.join(output, e).replace('.dbx',''))

    def import_contacts(self):
        pass
        
    def import_calendar(self):
        pass
        

class windowsmail(mailreader):
    
    def initialize(self):

        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []
        self.name = _("Windows Mail")
      
        self.mailconfigs = self.get_configuration()
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")
        for mb in self.mailboxes:
            self.size += mb.get_size()
        if not self.mailconfigs:
            raise Exception


    def get_configuration(self):
        configs = []
        for key in self.option:
            try:
                c = mailconfig(key)
            except:
                continue
            else:
                configs.append(c)
            mb = folder(os.path.dirname(key), False)
            if mb.path:
                self.mailboxes.append(mb)
        return configs

    def convert_mailbox(self, mb):
        eml2mbox(mb.path, os.path.join(self.dest.path, mb.path.split('/')[-1]))
        
    def import_calendar(self):
        pass
        
    def import_contacts(self):
        pass

class windowslivemail(windowsmail):
    
    def initialize(self):

        self.size = 0
        self.mailconfigs = []
        self.mailboxes = []

        self.name = _("Windows Live Mail")
        self.mailconfigs = self.get_configuration()
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")
        for mb in self.mailboxes:
            self.size += mb.get_size()
        if not self.mailconfigs:
            raise Exception

class winthunderbird(mailreader):
    
    def initialize(self):
        self.name = _("Mozilla Thunderbird")
        self.mailboxes = []
        self.size = 0
        self.mailconfigs = self.get_configuration()
        self.description = self.name +": %d "%len(self.mailconfigs)+ _("cuentas de correo")
        for mb in self.mailboxes:
            self.size += mb.get_size()
        
        if not self.mailconfigs:
            raise Exception

    def get_configuration(self):
        prefs = self.user.get_THUNDERBIRD_prefs()
        p = open(prefs, 'r')
        content = p.readlines()
        p.close()
        id = 0
        c = {}
        configs = []
        for ac in self.option:
            pid = re.compile('.*'+self.option+'\.identities\".+\"(?P<id>\w+)\".+')
            pserver = re.compile('.*'+self.option+'\.server\".+\"(?P<server>\w+)\".+')
            
            for l in content:
                m = pid.match(l)
                if m:
                    id = m.group('id')
                    psmtp = re.compile('.*'+id+'\.smtpServer\".+\"(?P<smtp>\w+)\".+')
                else:
                    m = pserver.match(l)
                    if m:
                        server = m.group('server')
                    elif id:
                        m = psmtp.match(l)
                        if m:
                            smtp = m.group('smtp')
            pid = re.compile('.*mail\.identity\.'+id+'\.(?P<key>.+)\".+\s(?P<value>(\".+\")|\d+)\).+')
            pserver = re.compile('.*mail\.server\.'+server+'\.(?P<key>.+)\".+\s(?P<value>(\".+\")|\w+)\).+')
            psmtp = re.compile('.*mail\.smtpserver\.'+smtp+'\.(?P<key>.+)\".+\s(?P<value>(\".+\")|\w+)\).+')
            for l in content:
                m = pid.match(l)
                if m:
                    c[m.group('key')]=m.group('value').replace('\"','')
                else:
                    m = pserver.match(l)
                    if m:
                        c[m.group('key')]=m.group('value').replace('\"','')
                    else:
                        m = psmtp.match(l)
                        if m:
                            c['smtp'+m.group('key')]=m.group('value').replace('\"','')
            configs.append(mailconfig(self.check_config(c)))
        return configs
        
    def check_config(self, c):

        c[c['type'].upper()+' Server'] = c['hostname']
        c[c['type'].upper()+' User Name'] = c['userName']
        if 'port' in c.keys(): c[c['type'].upper()+' Port']=eval(c['port'])
        if 'socketType' in c.keys(): 
            c['Connection Type'] = str(c['socketType'])
            if c['socketType'] == '3':
                c[c['type'].upper()+' Use SSL'] = '1'
        c['Account Name'] = c['name']
        c['SMTP Display Name'] = c['fullName']
        c['SMTP Email Address'] = c['smtpusername']
        c['SMTP Server'] = c['smtphostname']
        if 'smtpport' in c.keys(): c['SMTP Port']=eval(c['smtpport'])
        if 'smtptry_ssl' in c.keys(): c['SMTP Use SSL'] = '1'
        c['Email Address'] = c['useremail']
        if 'leave_on_server' in c.keys():
            c['Leave Mail On Server'] = '1'
        if 'empty_trash_on_exit' in c.keys():
            c['Remove When Deleted'] = '1'
        
        #print c
        #print c['directory-rel'].replace("[ProfD]",os.path.dirname(self.user.get_THUNDERBIRD_prefs())+'/')
        self.mailboxes.append(folder(c['directory-rel'].replace("[ProfD]",os.path.dirname(self.user.get_THUNDERBIRD_prefs())+'/')))
        return c
        
    def import_mails(self):
        self.dest = folder(os.path.join(os.path.expanduser('~'),'.evolution','mail','local', _("Correo de ")+ self.name +'.sbd'))
        self.mb_dir.copy(self.dest.path, exclude=['.dat','.msf'])
        

########################################################################
########################################################################

def hex2dec(hexa):
    """Convierte valores hexadecimales del registro en valores enteros"""
    if not isinstance(hexa, int):
        hexa = hexa.replace('dword:','0x')
        return int(hexa, 16)
    else:
        return int(hexa)


def hex2str(hexa):
    """Convierte valores hexadecimales del registro en cadenas de caracteres"""
    hexa = hexa.replace('hex:','')
    hexa = hexa.replace(',00','')
    hexa = hexa.replace(',','')
    hexa = hexa.replace(' ','')
    return binascii.unhexlify(hexa)
      
def eml2mbox(emlpath, mbxpath):
    i = 0
    pattern = re.compile('(?P<date>Date:)\s+(?P<day>\w{3})\s+(?P<number>\d{1,2})\s+(?P<month>\w{3})\s+(?P<year>\d{4})\s+(?P<hour>[\d:]{5,8}).+')
    try:
        d = open(mbxpath, 'w')
    except: 
        return 0
    for e in os.listdir(emlpath):
        if os.path.isdir(os.path.join(emlpath, e)):
            new = folder(mbxpath+'.sbd')
            eml2mbox(os.path.join(emlpath, e), os.path.join(new.path, e))
        elif os.path.splitext(e)[-1] == '.eml':
            try:
                m = open(os.path.join(emlpath, e),'r')
                content = m.readlines()
                m.close()
            except:
                continue
            else:
                for l in content:
                    r = pattern.match(l.replace(',',''))
                    if r:
                        d.write("From - %s %s  %s %s %s\n" % (r.group('day'),
                                                            r.group('month'),
                                                            r.group('number'),
                                                            r.group('hour'),
                                                            r.group('year')))
                        for l in content:
                            d.write(l.replace('\r', ''))
                        d.write('\n\n')
                        i += 1
                        break
    d.close()
    print "%d archivos procesados" % i

       

class thunderbird:
    """Clase para el gestro de correo Thunderbird"""

    def __init__(self):
        """Constructor de la clase"""
        thunderbird = folder(os.path.join(os.path.expanduser('~'),'.mozilla-thunderbird'))
        self.profile = self.get_thunderbird_profile(thunderbird.path)
        self.errors = []
        if not self.profile and thunderbird.path:
            try:
                prof = open(os.path.join(thunderbird.path,'profiles.ini'), 'w')
                prof.write("[General]\n")
                prof.write("StartWithLastProfile=1\n\n")
                prof.write("[Profile0]\n")
                prof.write("Name=migrated\n")
                prof.write("IsRelative=1\n")
                prof.write("Path=m1gra73d.default\n")
                prof.close()
                self.profile = thunderbird.create_subfolder("m1gra73d.default")
            except:
                self.error("Unable to create Mozilla Thunderbird profile")
                return 0
        self.config_file  = os.path.join(self.get_thunderbird_profile(thunderbird.path), 'prefs.js')

    def error (self, e):
        """Almacena los errores en tiempo de ejecución"""
        self.errors.append(e)

    def get_thunderbird_profile(self, thunderbird = '~/.mozilla-thunderbird'):
        """Devuelve el perfil actual de Thunderbird. En caso de no existir crea uno nuevo"""
        profiles_ini = os.path.join(thunderbird,'profiles.ini')
        if os.path.exists(profiles_ini):
            try:
                prof = open(profiles_ini, 'r')
            except:
                self.error ('Unable to read Thunderbird profiles')
            else:
                relative = 0
                for e in prof.read().split('\n'):
                    if re.search("IsRelative=1",e):
                        relative = 1
                    if re.search("Path",e):
                        profile = e.split('=')[1]
                prof.close()
                if relative:
                    path_profile = os.path.join(thunderbird, profile)
                else:
                    path_profile = profile
                if path_profile[-1]=='\r':
                    return path_profile[:-1]
                else:
                    return path_profile

    def config_account(self, account):
        """Configura la cuenta leida del registro"""
        n = str(random.randint(100,999))
        #check if the mail config is valid
        a = account
        server_type = a.get_type()
        if server_type == "HTTPMail" or server_type == "Unknown":
            warning("Acoount type %s won't be added" % server_type)
            return 0
        elif os.path.exists(self.config_file):
            bak = backup (self.config_file)
            try:
                p = open(self.config_file, 'w')
                b = open(bak, 'r')
            except:
                p.close()
                b.close()
                self.error('Unable to modify %s' % self.config_file)
                return 0
            else:
                added_account, added_smtpserver = False, False
                for l in b.readlines():
                    if re.search('mail.accountmanager.accounts',l): #add the new account
                        l = l[:-4] + ',account' + n + l[-4:]
                        added_account = True
                    if re.search('mail.smtpservers', l): #add the new smtp
                        l = l[:-4] + ',smtp' + n + l[-4:]
                        added_smtpserver = True
                    p.write(l)
                if not added_account:
                    p.write("user_pref(\"mail.accountmanager.accounts\", \"account%s\");\n" % n)
                if not added_smtpserver:
                    p.write("user_pref(\"mail.smtpservers\", \"smtp%s\");\n" % n)
                b.close()
        else:
            try:
                p = open(self.config_file, 'w')
            except:
                self.error('Unable to create %s' % self.config_file)
                return 0
            else:
                p.write("# Mozilla User Preferences\n\n")
                p.write("user_pref(\"mail.accountmanager.accounts\", \"account%s\");\n" % n)
                p.write("user_pref(\"mail.smtpservers\", \"smtp%s\");\n" % n)
                p.write("user_pref(\"mail.root.none\", \"%s/Mail\");\n" % self.profile)
                p.write("user_pref(\"mail.root.none-rel\", \"[ProfD]Mail\");\n")
                if server_type == "imap":# only for IMAP accounts
                    p.write("user_pref(\"mail.root.imap\", \"%s/ImapMail\");\n" % self.profile)
                    p.write("user_pref(\"mail.root.imap-rel\", \"[ProfD] ImapMail\");\n")
                    #p.write("user_pref(\"mail.server.server%s.directory\", \"%s/ImapMail/%s\");\n"% (n, self.profile, a.get_server()))
                elif server_type == "pop3": # only for POP3 accounts
                    p.write("user_pref(\"mail.root.pop3\", \"%s/Mail\");\n" % self.profile)
                    p.write("user_pref(\"mail.root.pop3-rel\", \"[ProfD]Mail\");\n")
                    #p.write("user_pref(\"mail.server.server%s.leave_on_server\", %s);\n" % (n, a.leave_mail()))
                    #p.write("user_pref(\"mail.server.server%s.directory\", \"%s/Mail/%s\");\n" % (n, self.profile, a.get_server()))
                    #p.write ("user_pref(\"mail.server.server%s.delete_by_age_from_server\", %s);\n" % (n, a.remove_deleted()))
                    #p.write("user_pref(\"mail.server.server%s.delete_mail_left_on_server\", %s);\n" % (n, a.remove_expired()))
        # for both types
        p.write("user_pref(\"mail.account.account%s.identities\", \"id%s\");\n" % (n, n))
        p.write("user_pref(\"mail.account.account%s.server\", \"server%s\");\n" % (n, n))
        # identity configuration
        p.write("user_pref(\"mail.identity.id%s.fullName\", \"%s\");\n" % (n, a.get_SMTP_display_name()))
        p.write("user_pref(\"mail.identity.id%s.useremail\", \"%s\");\n" % (n, a.get_SMTP_email_address()))
        p.write("user_pref(\"mail.identity.id%s.smtpServer\", \"smtp%s\");\n" % (n, n))
        p.write("user_pref(\"mail.identity.id%s.valid\", true);\n" % n )
        # server configuration
        p.write("user_pref(\"mail.server.server%s.login_at_startup\", true);\n" % n )
        p.write("user_pref(\"mail.server.server%s.name\", \"%s\");\n" % (n, a.get_account_name()))
        p.write("user_pref(\"mail.server.server%s.hostname\", \"%s\");\n"% (n, a.get_server()))
        p.write("user_pref(\"mail.server.server%s.type\", \"%s\");\n" % (n, server_type))
        p.write("user_pref(\"mail.server.server%s.userName\", \"%s\");\n" % (n, a.get_user_name()))
        if a.use_SSL():
            p.write("user_pref(\"mail.server.server%s.socketType\", 3);\n"% n)
            p.write("user_pref(\"mail.server.server%s.port\", %d);\n" % (n, a.get_port()))
        # SMTP configuration
        p.write("user_pref(\"mail.smtpserver.smtp%s.hostname\", \"%s\");\n" % (n, a.get_SMTP_server()))
        p.write("user_pref(\"mail.smtpserver.smtp%s.username\", \"%s\");\n" % (n, a.get_user_name()))
        if a.use_SMTP_SSL():
            p.write("user_pref(\"mail.smtpserver.smtp%s.try_ssl\", 3);\n" % n)
            p.write("user_pref(\"mail.smtpserver.smtp%s.port\", %d);\n" % (n, a.get_SMTP_port()))
        p.close()

    def import_windows_settings(self, AppData):
        """Importa la configuración de Thunderbird en Windows. SOBREESCRIBE LA INFORMACIÓN EXISTENTE"""
        winprofile = folder(self.get_thunderbird_profile(os.path.join(AppData, 'Thunderbird')))
        if winprofile:
            # copy all files from the winprofile
            winprofile.copy(self.profile)
            # modify prefs.js
            try:
                wp = open (os.path.join(winprofile.path, 'prefs.js'), 'r')
                p = open (self.config_file, 'w')
                for l in wp.readlines():
                    if l.find(':\\') == -1:
                        p.write(l)
            except:
                self.error("Failed to copy Thunderbird profile")
            wp.close()
            p.close()
        return 0

    def is_installed_on_Windows(self, AppData):
        """Devuelve si Firefox está instalado en Windows"""
        return self.get_thunderbird_profile(os.path.join(AppData.path, 'Thunderbird'))

    def generate_impab(self, l):
        try:
            backup(os.path.join(self.profile,'impab.mab'))
            f = open(os.path.join(self.profile,'impab.mab'),'w')
        except:
            self.error( "Fallo al crear contactos de Thunderbird")
        else:
            f.write("// <!-- <mdb:mork:z v=\"1.4\"/> -->\n< <(a=c)> // (f=iso-8859-1)\n  (B8=Custom3)(B9=Custom4)(BA=Notes)(BB=LastModifiedDate)(BC=RecordKey)\n  (BD=AddrCharSet)(BE=LastRecordKey)(BF=ns:addrbk:db:table:kind:pab)\n  (C0=ListName)(C1=ListNickName)(C2=ListDescription)\n  (C3=ListTotalAddresses)(C4=LowercaseListName)\n  (C5=ns:addrbk:db:table:kind:deleted)\n  (80=ns:addrbk:db:row:scope:card:all)\n  (81=ns:addrbk:db:row:scope:list:all)\n  (82=ns:addrbk:db:row:scope:data:all)(83=FirstName)(84=LastName)\n  (85=PhoneticFirstName)(86=PhoneticLastName)(87=DisplayName)\n  (88=NickName)(89=PrimaryEmail)(8A=LowercasePrimaryEmail)\n  (8B=SecondEmail)(8C=DefaultEmail)(8D=CardType)(8E=PreferMailFormat)\n  (8F=PopularityIndex)(90=WorkPhone)(91=HomePhone)(92=FaxNumber)\n  (93=PagerNumber)(94=CellularNumber)(95=WorkPhoneType)(96=HomePhoneType)\n  (97=FaxNumberType)(98=PagerNumberType)(99=CellularNumberType)\n  (9A=HomeAddress)(9B=HomeAddress2)(9C=HomeCity)(9D=HomeState)\n  (9E=HomeZipCode)(9F=HomeCountry)(A0=WorkAddress)(A1=WorkAddress2)\n  (A2=WorkCity)(A3=WorkState)(A4=WorkZipCode)(A5=WorkCountry)\n  (A6=JobTitle)(A7=Department)(A8=Company)(A9=_AimScreenName)\n  (AA=AnniversaryYear)(AB=AnniversaryMonth)(AC=AnniversaryDay)\n  (AD=SpouseName)(AE=FamilyName)(AF=DefaultAddress)(B0=Category)\n  (B1=WebPage1)(B2=WebPage2)(B3=BirthYear)(B4=BirthMonth)(B5=BirthDay)\n  (B6=Custom1)(B7=Custom2)>\n\n")

            f.write("<(80=0)>\n{1:^80 {(k^BF:c)(s=9)}\n  [1:^82(^BE=0)]}\n\n")

            f.write("@$${1{@\n\n<")
            i, j = 1, 1
            for a in l:
                f.write("(%d=%d)(%d=%s)"%(80 + 2*(len(l) - j + 1) , len(l) - j + 1 , 80 + i, a ))
                i += 2
                j += 1
            f.write(">\n{-1:^80 {(k^BF:c)(s=9)} \n  [1:^82(^BE=%d)]"%(j-1))
            i, j = 1, 1
            for a in l:
                f.write("\n  [-%d(^89^%d)(^8A^%d)(^BC=%d)]"%(j, 80+i, 80+i, j))
                i += 2
                j += 1
            f.write("}\n@$$}1}@")
            f.close()
            self.add_impab()
            return 1

    def add_impab(self):
        p = None
        if os.path.exists(self.config_file):
            bak = backup (self.config_file)
            try:
                p = open(self.config_file, 'a')
            except:
                p.close()
                self.error('Unable to modify %s' % self.config_file)
                return 0

        else:
            try:
                p = open(self.config_file, 'w')
            except:
                self.error('Unable to create %s' % self.config_file)
                return 0
            else:
                p.write("# Mozilla User Preferences\n\n")
                p.write("user_pref(\"mail.accountmanager.accounts\", \"\");\n")
                p.write("user_pref(\"mail.smtpservers\", \"\");\n")
                p.write("user_pref(\"mail.root.none\", \"%s/Mail\");\n"%self.profile)
                p.write("user_pref(\"mail.root.none-rel\", \"[ProfD]Mail\");\n")
        if p:
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.description\", \"contactos_Outlook\");\n")
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.dirType\", 2);\n")
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.filename\", \"impab.mab\");\n")
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.isOffline\", false);\n")
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.protocolVersion\", \"2\");\n")
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.replication.lastChangeNumber\", 0);\n")
            p.write("user_pref(\"ldap_2.servers.contactosOutlook.position\", 1);\n")
            p.close()

# end class thunderbird

############################################################################


class kmail:
    """Clase para el gestor de correo Kmail integrado en Kontact"""

    def __init__(self):
        """Constructor de la clase"""
        self.errors = []
        self.config_file = os.path.join(os.path.expanduser('~'),'.kde', 'share', 'config', 'kmailrc')
        if os.path.exists(self.config_file):
            try:
                b = open (self.config_file, 'r')
                for l in b.readlines():
                    if re.search('accounts=',l): # increase account
                        self.iaccount = int(l[9:]) + 1
                    if re.search('transports=', l): # increase smtp
                        self.itransport = int(l[11:]) + 1
                b.close()
            except:
                b.close()
                self.error("Unable to read %s" % self.config_file)
        else:
            self.iaccount, self.itransport = 0, 0
            try:
                b = open (self.config_file, 'r')
                b.write("[Composer]\n")
                b.write("default-transport=%s\n" % a.get_SMTP_display_name())
                b.write("\n[General]\n")
                b.write("accounts=1\n")
                b.write("checkmail-startup=true\n")
                b.write("first-start=false\n")
                b.write("transports=1\n")
                b.close()
            except:
                b.close()
                self.error("Unable to create %s" % self.config_file)
        self.identities = os.path.join(os.path.expanduser('~'), '.kde', 'share', 'config', 'emailidentities')
        self.maxid = 0
        if os.path.exists(self.identities):# configuration file already exists
            try:
                b = open(self.identities, 'r')
                for l in b.readlines():
                    if re.search('Identity #',l): # increase account
                        maxid = (maxid <= int(l[11:-2])) and int(l[11:-2]) + 1 or maxid
                b.close()
            except:
                b.close()
                self.error('Unable to access %s' % self.identities)


    def error(self, e):
        """Almacena los errores en tiempo de ejecución"""
        self.errors.append(e)

    def config_account(self, account):
        """Configura la cuenta dada en Kmail"""
        #check if the mail config is valid
        a = mailconfig(account)
        server_type = a.get_type()
        if server_type == "HTTPMail" or server_type == "Unknown":
            warning("Acoount type %s won't be added" % server_type)
            return 0
        self.itransport = self.itransport + 1
        self.iaccount = self.iaccount + 1
        bak = backup (self.config_file)
        if bak:
            try:
                p = open(self.config_file, 'w')
                # config account
                p.write("\n[Account %i]\n" % self.iaccount)
                p.write("Folder=inbox\n")
                p.write("Id=%s\n" % str(time.time()))
                p.write("Name=%s\n" % a.get_account_name())
                if server_type=='imap':
                    p.write("auth=*\n")
                    p.write("Type=%s\n" % server_type)
                if server_type=='pop3':
                    p.write("auth=USER\n")
                    p.write("Type=pop\n")
                p.write("host=%s\n" % a.get_server())
                p.write("leave-on-server=%s\n" % a.leave_mail())
                p.write("login=%s\n" % a.get_user_name())
                p.write("port=%s\n" % a.get_port())
                p.write("use-ssl=%s\n" % a.use_SSL())
                p.write("use-tls=false\n")
                #config transport (SMTP)
                p.write("\n[Transport %i]\n" % self.itransport)
                p.write("auth=false\n")
                p.write("authtype=PLAIN\n")
                p.write("encryption=SSL\n")
                p.write("host=%s\n" % a.get_SMTP_server())
                p.write("id=%s\n" % str(time.time()))
                p.write("name=%s\n" % a.get_SMTP_server())
                p.write("port=%s\n" % a.get_SMTP_port())
                p.write("type=smtp\n")
                p.write("user=\n")
                p.close()
                #config identity
                self.add_identity(account)
            except:
                p.close()
                self.itransport = self.itransport - 1
                self.iaccount = self.iaccount - 1
                self.error('Failed to modify %s' % self.config_file)
                return 0

    def add_identity(self, a):
        """Añade la identidad"""
        bak = backup(self.identities)
        if bak:
            try:
                p = open(self.identities, 'a')
                p.write("\n[Identity #%i]\n" % self.maxid)
                p.write("Email Address=%s\n" % a.get_SMTP_email_address())
                p.write("Name=%s\n" % a.get_SMTP_display_name())
                p.write("Transport=%s\n" % a.get_SMTP_server())
                p.write("uoid=%s\n" % str(time.time()*100))
                p.close()
            except:
                p.close()
                self.error("Failed to add identity")
                restore_backup(bak)

# end class kmail

############################################################################


if __name__ == "__main__":
    from amigu.computer.info import pc
    from amigu.computer.users.mswin import winuser
    com = pc()
    com.check_all_partitions()
    print "Analizando usuarios de Windows"
    for user_path, ops in com.get_win_users().iteritems():
        u = winuser(user_path, com, ops)
        try:
            print u.get_OUTLOOK_accounts()
        except:
            pass
        u.clean()