From 97027e7376f9876661298106aa229d0301a2843d Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Thu, 11 Oct 2012 04:28:36 +0200 Subject: [PATCH] improve multi account, bugfix remove .ipr --- impra/cli.py | 44 ++++++++++++--------- impra/core.py | 104 ++++++++++++++++++++++++++------------------------ impra/imap.py | 6 ++- impra/util.py | 2 +- 4 files changed, 87 insertions(+), 69 deletions(-) diff --git a/impra/cli.py b/impra/cli.py index ad4dad2..6c4115a 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -124,8 +124,8 @@ class Cli: gpConf.add_option('-X', '--set-pass' , help='set imap user password' , action='store', metavar='PASS ') gpConf.add_option('-P', '--set-port' , help='set imap port' , action='store', metavar='PORT ') gpConf.add_option('-N', '--set-name' , help='set user name' , action='store', metavar='NAME ') - gpConf.add_option('-M', '--set-multi' , help='set multi account' , action='store', metavar='ACCOUNT PASSWORD', nargs=2) - gpConf.add_option('-R', '--remove-multi' , help='remove multi account' , action='store', metavar='ACCOUNT ') + gpConf.add_option('-M', '--set-multi' , help='set multi account' , action='store', metavar='PROFILE ') + gpConf.add_option('-R', '--remove-multi' , help='remove multi account' , action='store', metavar='PROFILE ') gpConf.add_option('-B', '--set-boxname' , help='set boxName on imap server (default:[%default])' , action='store', metavar='BOXNAME ') gpConf.add_option('-K', '--gen-key' , help='generate new key' , action='store_true', default=False) @@ -215,11 +215,18 @@ class Cli: if o.set_name : self.ini.set('name' , o.set_name,o.active_profile+'.infos') m = self.ini.get('multi',o.active_profile+'.imap') - if m is None : m = {} - else: m = eval(m) - if o.set_multi : m[o.set_multi[0]] = o.set_multi[1] - elif o.remove_multi and m is not None and o.remove_multi in m : m.pop(o.remove_multi,None) - self.ini.set('multi', core.jdumps(m),o.active_profile+'.imap') + if m is None : m = [] + else : m = m.split(',') + m = [x for x in m if x] + if o.set_multi : + if self.check_imap(o.set_multi): + if not o.set_multi in m :m.append(o.set_multi) + else: + mprint() + Clz.print('imap profile '+o.set_multi+' not found', Clz.fgB1) + mprint() + elif o.remove_multi and o.remove_multi in m : m.remove(o.remove_multi) + self.ini.set('multi', ','.join(m), o.active_profile+'.imap') if o.gen_key: kg = crypt.KeyGen(256) @@ -582,9 +589,12 @@ class Cli: def exit(self, code): if Clz.isUnix : sys.exit(code) + def check_imap(self,profile): + return self.ini.has('host',profile+'.imap') and self.ini.has('user',profile+'.imap') and self.ini.has('pass',profile+'.imap') and self.ini.has('port',profile+'.imap') + def check_profile(self,profile, activeCheck=False): """""" - c = self.ini.hasSection(profile+'.keys') and self.ini.has('host',profile+'.imap') and self.ini.has('user',profile+'.imap') and self.ini.has('pass',profile+'.imap') and self.ini.has('port',profile+'.imap') and self.ini.has('name',profile+'.infos') + c = self.ini.hasSection(profile+'.keys') and self.check_imap(profile) and self.ini.has('name',profile+'.infos') if activeCheck : if c : Clz.print(' '+profile+' is ok', Clz.fgB2) @@ -778,13 +788,11 @@ class Cli: Clz.print(' '*40, Clz.fgb7, False) Clz.print('-M ', Clz.fgB3, False) Clz.print('{', Clz.fgB1, False) - Clz.print('user', Clz.fgB1, False) - Clz.print('} {', Clz.fgB1, False) - Clz.print('pass', Clz.fgB1, False) + Clz.print('profile', Clz.fgB1, False) Clz.print('}', Clz.fgB1, False) Clz.print(' -R ', Clz.fgB3, False) Clz.print('{', Clz.fgB1, False) - Clz.print('user', Clz.fgB1, False) + Clz.print('profile', Clz.fgB1, False) Clz.print('}', Clz.fgB1, False) Clz.print(' ]', Clz.fgB3) @@ -904,15 +912,15 @@ class Cli: Clz.print(' '*50+'set imap boxname (default:__impra__)' , Clz.fgB7) Clz.print(' '*4+'-M ' , Clz.fgB3, False) - Clz.print('USER PASS'.ljust(10,' ') , Clz.fgB1, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) Clz.print(', --set-multi'.ljust(18,' ') , Clz.fgB3, False) - Clz.print('USER PASS'.ljust(10,' ') , Clz.fgB1) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) Clz.print(' '*50+'add imap multi account' , Clz.fgB7) Clz.print(' '*4+'-R ' , Clz.fgB3, False) - Clz.print('USER'.ljust(10,' ') , Clz.fgB1, False) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) Clz.print(', --remove-multi'.ljust(18,' ') , Clz.fgB3, False) - Clz.print('USER'.ljust(10,' ') , Clz.fgB1) + Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) Clz.print(' '*50+'remove imap multi account' , Clz.fgB7) mprint('\n') @@ -1185,7 +1193,7 @@ class Cli: Clz.print('-S ', Clz.fgB3, False) Clz.print('bobgmail ', Clz.fgB1, False) Clz.print('-M ', Clz.fgB3, False) - Clz.print('bob23 passbob23', Clz.fgB1) + Clz.print('bobimap', Clz.fgB1) Clz.print(' '*8+'# remove multi account to profile bobgmail',Clz.fgn7) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) @@ -1193,7 +1201,7 @@ class Cli: Clz.print('-S ', Clz.fgB3, False) Clz.print('bobgmail ', Clz.fgB1, False) Clz.print('-R ', Clz.fgB3, False) - Clz.print('bob23', Clz.fgB1) + Clz.print('bobimap', Clz.fgB1) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) mprint() diff --git a/impra/core.py b/impra/core.py index 7d99598..72517c3 100644 --- a/impra/core.py +++ b/impra/core.py @@ -402,7 +402,7 @@ class ImpraIndex: def fixAccount(self,account): """""" - rt = RuTime(eval(__CALLER__())) + rt = RuTime(eval(__CALLER__('%s' % account))) r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] for k in r: t = list(self.dic[k]) @@ -415,24 +415,24 @@ class ImpraIndex: def getLightestAccount(self,l): """""" - rt = RuTime(eval(__CALLER__())) + rt = RuTime(eval(__CALLER__('%s' % str(l)))) r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] t = {} for k in r: if not self.dic[k][self.ACCOUNT] in t: t[self.dic[k][self.ACCOUNT]] = self.dic[k][self.SIZE] else: t[self.dic[k][self.ACCOUNT]] += int(self.dic[k][self.SIZE]) - account = None + profile = None r = [] for a in l: if not a in t : - account = a + profile = a break else : r.append((t[a],a)) - if account is None : + if profile is None : d = sorted(r, reverse=False, key=lambda lst:lst[0]) - account = d[0][1] + profile = d[0][1] rt.stop() - return account + return profile def fixDuplicateIds(self): """Get corresponding keys of duplicate ids in the index @@ -563,7 +563,7 @@ class ImpraIndex: a = '' tsize = 0 psize = 0 - #~ acc = {} #~ acc = {} + acc = {} for v,k in d : if matchIds==None or v[self.UID] in matchIds: a = '' @@ -575,14 +575,15 @@ class ImpraIndex: Clz.print(str(v[self.PARTS]).rjust(2 ,'0') +' '*2 , Clz.fgN1, False) Clz.print(str(v[self.EXT][:5]).ljust(7,' ') , Clz.fgn3, False) Clz.print(self.getUser(str(v[self.USER])).ljust(15 ,' ') , Clz.fgn7, False) - Clz.print(str(v[self.CATG]).ljust(30 ,' ') , Clz.fgN3) - #~ Clz.print(str(v[self.CATG]).ljust(30 ,' ') , Clz.fgN3, False) - #~ if len(v)-1==self.ACCOUNT: - #~ Clz.print(str(v[self.ACCOUNT]) +' '*2 , Clz.fgN3) - #~ if v[self.ACCOUNT] in acc : - #~ acc[v[self.ACCOUNT]] += int(v[self.SIZE]) - #~ else : acc[v[self.ACCOUNT]] = int(v[self.SIZE]) - #~ else: mprint() + #~ Clz.print(str(v[self.CATG]).ljust(30 ,' ') , Clz.fgN3) + Clz.print(str(v[self.CATG]).ljust(30 ,' ') , Clz.fgN3, False) + if len(v)-1==self.ACCOUNT: + Clz.print(str(v[self.ACCOUNT]) +' '*2 , Clz.fgN3) + if v[self.ACCOUNT] in acc : + acc[v[self.ACCOUNT]] += int(v[self.SIZE]) + else : acc[v[self.ACCOUNT]] = int(v[self.SIZE]) + else: mprint() + psize += int(v[self.SIZE]) tsize += int(v[self.SIZE]) if len(d)==0: @@ -598,16 +599,17 @@ class ImpraIndex: Clz.print(formatBytes(int(tsize)), Clz.fgB2, False) mprint() printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) - #~ Clz.print(' '*4+'[', Clz.fgB7, False) - #~ sep = '' - #~ for k in acc: - #~ if k!= '': - #~ Clz.print(sep+k,Clz.fgB3,False) - #~ Clz.print(':',Clz.fgB7,False) - #~ Clz.print(formatBytes(acc[k]),Clz.fgB2,False) - #~ if sep=='':sep = ',' - #~ Clz.print(']', Clz.fgB7, False) - #~ mprint() + + Clz.print(' '*4+'[', Clz.fgB7, False) + sep = '' + for k in acc: + if k!= '': + Clz.print(sep+k,Clz.fgB3,False) + Clz.print(':',Clz.fgB7,False) + Clz.print(formatBytes(acc[k]),Clz.fgB2,False) + if sep=='':sep = ',' + Clz.print(']', Clz.fgB7, False) + mprint() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -757,6 +759,7 @@ class ImpraStorage: #~ self.srv.expunge() #~ sleep(2) self.index.fixDuplicateIds() + #~ self.index.fixAccount('gmx') encData = self.index.encrypt() msgIndex = self.mb.buildIndex(encData) if DEBUG.level <= DEBUG.NOTICE : mprint(msgIndex.as_string()) @@ -794,44 +797,48 @@ class ImpraStorage: lsrv = self.ih.searchBySubject(subject,True) return [ int(i) for i in set(lloc).difference(set(lsrv))] - def switchFileAccount(self,account=None): + def switchFileAccount(self,profile=None): """""" - rt = RuTime(eval(__CALLER__())) - al = self.conf.get('multi','imap') - if al is not None : - al = eval(al) - if len(al) > 0: - if not self.conf.get('user','imap') in al: - al[self.conf.get('user','imap')] = self.conf.get('pass','imap') - iconf = self.ih.conf - if iconf.user != account : + rt = RuTime(eval(__CALLER__('%s' % str(profile)))) + pl = self.conf.get('multi','imap') + if pl is not None : + pl = pl.split(',') + if len(pl) > 0: + if not self.conf.profile in pl: + pl.append(self.conf.profile) + iconf = self.ih.conf + account = self.conf.get('user','imap',profile) + if True or iconf.user != account : # reinit iconf.user = None try : - if account is None : account = self.index.getLightestAccount(al) - if account in al : - iconf.user = account - iconf.pwd = al[account] + if profile is None : profile = self.index.getLightestAccount(pl) + if profile in pl : + iconf.user = self.conf.get('user','imap',profile) + iconf.pwd = self.conf.get('pass','imap',profile) + iconf.host = self.conf.get('host','imap',profile) + iconf.port = self.conf.get('port','imap',profile) self.ih = ImapHelper(iconf,self.rootBox) except Exception as e: mprint('Error : ') mprint(e) - mprint('check your connection or your imap config for account '+iconf.user+' : '+iconf.password) + mprint('check your connection or your imap config for profile '+profile) rt.stop() + if profile is None: profile = self.conf.profile + return profile def addFile(self, path, label, catg=''): """""" done = False rt = RuTime(eval(__CALLER__()),DEBUG.INFO) - self.switchFileAccount() + account = self.switchFileAccount() _, ext = splitext(path) #~ try: size = getsize(path) if size > 0 : md5 = hash_sha256_file(path) - account = self.ih.conf.user mprint() Clz.print(' account : ' , Clz.fgn7, False) Clz.print(account , Clz.fgB7) @@ -910,12 +917,11 @@ class ImpraStorage: self.index.add(hlst['head'][3],label,hlst['head'][1],ext,ownerHash,catg,md5,bFlag,size,account) done = self.saveIndex() - else : - # clean - for mid, row in sendIds : - if cancel : self.ih.delete(mid, True) - if file_exists(self.fsplit.DIR_OUTBOX+row[1]+'.ipr') : remove(self.fsplit.DIR_OUTBOX+row[1]+'.ipr') - self.clean() + # clean + for mid, row in sendIds : + if cancel : self.ih.delete(mid, True) + if file_exists(self.fsplit.DIR_OUTBOX+row[1]+'.ipr') : remove(self.fsplit.DIR_OUTBOX+row[1]+'.ipr') + self.clean() else : mprint(' ',end='') diff --git a/impra/imap.py b/impra/imap.py index b6a583f..dcca8ba 100644 --- a/impra/imap.py +++ b/impra/imap.py @@ -169,6 +169,10 @@ class ImapHelper: rt = RuTime(eval(__CALLER__('conf,"'+str(box)+'"'))) self.srv = IMAP4_SSL(conf.host,conf.port) self.conf = conf + print(conf.host) + print(conf.port) + print(conf.user) + print(conf.pwd) status, resp = self.srv.login(conf.user,conf.pwd) if DEBUG.level <= DEBUG.ALL : mprint(status) @@ -176,7 +180,7 @@ class ImapHelper: if status == self.OK: self.rootBox = box if boxBin is None : - if search('yahoo.com',conf.host) is not None : + if search('google.com',conf.host) is None: self.BOX_BIN = 'Trash' if box != None : status, resp = self.srv.select(self.rootBox) diff --git a/impra/util.py b/impra/util.py index 170c44a..d7a76ed 100644 --- a/impra/util.py +++ b/impra/util.py @@ -327,7 +327,7 @@ class IniFile: """""" content = '' main = '' - for s in self.dic: + for s in sorted(self.dic): if section=='*' or section+'.'==s[:len(section)+1]: if s!='main': #~ if section=='*': content += '\n['+s+']\n'