improve multi account, bugfix remove .ipr

This commit is contained in:
a-Sansara 2012-10-11 04:28:36 +02:00
parent d961a92b99
commit 97027e7376
4 changed files with 87 additions and 69 deletions

View File

@ -124,8 +124,8 @@ class Cli:
gpConf.add_option('-X', '--set-pass' , help='set imap user password' , action='store', metavar='PASS ') 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('-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('-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('-M', '--set-multi' , help='set multi account' , action='store', metavar='PROFILE ')
gpConf.add_option('-R', '--remove-multi' , help='remove multi account' , action='store', metavar='ACCOUNT ') 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('-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) 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') if o.set_name : self.ini.set('name' , o.set_name,o.active_profile+'.infos')
m = self.ini.get('multi',o.active_profile+'.imap') m = self.ini.get('multi',o.active_profile+'.imap')
if m is None : m = {} if m is None : m = []
else: m = eval(m) else : m = m.split(',')
if o.set_multi : m[o.set_multi[0]] = o.set_multi[1] m = [x for x in m if x]
elif o.remove_multi and m is not None and o.remove_multi in m : m.pop(o.remove_multi,None) if o.set_multi :
self.ini.set('multi', core.jdumps(m),o.active_profile+'.imap') 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: if o.gen_key:
kg = crypt.KeyGen(256) kg = crypt.KeyGen(256)
@ -582,9 +589,12 @@ class Cli:
def exit(self, code): def exit(self, code):
if Clz.isUnix : sys.exit(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): 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 activeCheck :
if c : if c :
Clz.print(' '+profile+' is ok', Clz.fgB2) Clz.print(' '+profile+' is ok', Clz.fgB2)
@ -778,13 +788,11 @@ class Cli:
Clz.print(' '*40, Clz.fgb7, False) Clz.print(' '*40, Clz.fgb7, False)
Clz.print('-M ', Clz.fgB3, False) Clz.print('-M ', Clz.fgB3, False)
Clz.print('{', Clz.fgB1, 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('pass', Clz.fgB1, False)
Clz.print('}', Clz.fgB1, False) Clz.print('}', Clz.fgB1, False)
Clz.print(' -R ', Clz.fgB3, False) Clz.print(' -R ', Clz.fgB3, False)
Clz.print('{', Clz.fgB1, 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.fgB1, False)
Clz.print(' ]', Clz.fgB3) Clz.print(' ]', Clz.fgB3)
@ -904,15 +912,15 @@ class Cli:
Clz.print(' '*50+'set imap boxname (default:__impra__)' , Clz.fgB7) Clz.print(' '*50+'set imap boxname (default:__impra__)' , Clz.fgB7)
Clz.print(' '*4+'-M ' , Clz.fgB3, False) 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(', --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(' '*50+'add imap multi account' , Clz.fgB7)
Clz.print(' '*4+'-R ' , Clz.fgB3, False) 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(', --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) Clz.print(' '*50+'remove imap multi account' , Clz.fgB7)
mprint('\n') mprint('\n')
@ -1185,7 +1193,7 @@ class Cli:
Clz.print('-S ', Clz.fgB3, False) Clz.print('-S ', Clz.fgB3, False)
Clz.print('bobgmail ', Clz.fgB1, False) Clz.print('bobgmail ', Clz.fgB1, False)
Clz.print('-M ', Clz.fgB3, 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+'# remove multi account to profile bobgmail',Clz.fgn7)
Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False)
@ -1193,7 +1201,7 @@ class Cli:
Clz.print('-S ', Clz.fgB3, False) Clz.print('-S ', Clz.fgB3, False)
Clz.print('bobgmail ', Clz.fgB1, False) Clz.print('bobgmail ', Clz.fgB1, False)
Clz.print('-R ', Clz.fgB3, False) Clz.print('-R ', Clz.fgB3, False)
Clz.print('bob23', Clz.fgB1) Clz.print('bobimap', Clz.fgB1)
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
mprint() mprint()

View File

@ -402,7 +402,7 @@ class ImpraIndex:
def fixAccount(self,account): 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)] r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)]
for k in r: for k in r:
t = list(self.dic[k]) t = list(self.dic[k])
@ -415,24 +415,24 @@ class ImpraIndex:
def getLightestAccount(self,l): 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)] r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)]
t = {} t = {}
for k in r: for k in r:
if not self.dic[k][self.ACCOUNT] in t: t[self.dic[k][self.ACCOUNT]] = self.dic[k][self.SIZE] 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]) else: t[self.dic[k][self.ACCOUNT]] += int(self.dic[k][self.SIZE])
account = None profile = None
r = [] r = []
for a in l: for a in l:
if not a in t : if not a in t :
account = a profile = a
break break
else : r.append((t[a],a)) else : r.append((t[a],a))
if account is None : if profile is None :
d = sorted(r, reverse=False, key=lambda lst:lst[0]) d = sorted(r, reverse=False, key=lambda lst:lst[0])
account = d[0][1] profile = d[0][1]
rt.stop() rt.stop()
return account return profile
def fixDuplicateIds(self): def fixDuplicateIds(self):
"""Get corresponding keys of duplicate ids in the index """Get corresponding keys of duplicate ids in the index
@ -563,7 +563,7 @@ class ImpraIndex:
a = '' a = ''
tsize = 0 tsize = 0
psize = 0 psize = 0
#~ acc = {} #~ acc = {} acc = {}
for v,k in d : for v,k in d :
if matchIds==None or v[self.UID] in matchIds: if matchIds==None or v[self.UID] in matchIds:
a = '' 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.PARTS]).rjust(2 ,'0') +' '*2 , Clz.fgN1, False)
Clz.print(str(v[self.EXT][:5]).ljust(7,' ') , Clz.fgn3, 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(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)
#~ Clz.print(str(v[self.CATG]).ljust(30 ,' ') , Clz.fgN3, False) Clz.print(str(v[self.CATG]).ljust(30 ,' ') , Clz.fgN3, False)
#~ if len(v)-1==self.ACCOUNT: if len(v)-1==self.ACCOUNT:
#~ Clz.print(str(v[self.ACCOUNT]) +' '*2 , Clz.fgN3) Clz.print(str(v[self.ACCOUNT]) +' '*2 , Clz.fgN3)
#~ if v[self.ACCOUNT] in acc : if v[self.ACCOUNT] in acc :
#~ acc[v[self.ACCOUNT]] += int(v[self.SIZE]) acc[v[self.ACCOUNT]] += int(v[self.SIZE])
#~ else : acc[v[self.ACCOUNT]] = int(v[self.SIZE]) else : acc[v[self.ACCOUNT]] = int(v[self.SIZE])
#~ else: mprint() else: mprint()
psize += int(v[self.SIZE]) psize += int(v[self.SIZE])
tsize += int(v[self.SIZE]) tsize += int(v[self.SIZE])
if len(d)==0: if len(d)==0:
@ -598,16 +599,17 @@ class ImpraIndex:
Clz.print(formatBytes(int(tsize)), Clz.fgB2, False) Clz.print(formatBytes(int(tsize)), Clz.fgB2, False)
mprint() mprint()
printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN)
#~ Clz.print(' '*4+'[', Clz.fgB7, False)
#~ sep = '' Clz.print(' '*4+'[', Clz.fgB7, False)
#~ for k in acc: sep = ''
#~ if k!= '': for k in acc:
#~ Clz.print(sep+k,Clz.fgB3,False) if k!= '':
#~ Clz.print(':',Clz.fgB7,False) Clz.print(sep+k,Clz.fgB3,False)
#~ Clz.print(formatBytes(acc[k]),Clz.fgB2,False) Clz.print(':',Clz.fgB7,False)
#~ if sep=='':sep = ',' Clz.print(formatBytes(acc[k]),Clz.fgB2,False)
#~ Clz.print(']', Clz.fgB7, False) if sep=='':sep = ','
#~ mprint() Clz.print(']', Clz.fgB7, False)
mprint()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -757,6 +759,7 @@ class ImpraStorage:
#~ self.srv.expunge() #~ self.srv.expunge()
#~ sleep(2) #~ sleep(2)
self.index.fixDuplicateIds() self.index.fixDuplicateIds()
#~ self.index.fixAccount('gmx')
encData = self.index.encrypt() encData = self.index.encrypt()
msgIndex = self.mb.buildIndex(encData) msgIndex = self.mb.buildIndex(encData)
if DEBUG.level <= DEBUG.NOTICE : mprint(msgIndex.as_string()) if DEBUG.level <= DEBUG.NOTICE : mprint(msgIndex.as_string())
@ -794,44 +797,48 @@ class ImpraStorage:
lsrv = self.ih.searchBySubject(subject,True) lsrv = self.ih.searchBySubject(subject,True)
return [ int(i) for i in set(lloc).difference(set(lsrv))] 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__())) rt = RuTime(eval(__CALLER__('%s' % str(profile))))
al = self.conf.get('multi','imap') pl = self.conf.get('multi','imap')
if al is not None : if pl is not None :
al = eval(al) pl = pl.split(',')
if len(al) > 0: if len(pl) > 0:
if not self.conf.get('user','imap') in al: if not self.conf.profile in pl:
al[self.conf.get('user','imap')] = self.conf.get('pass','imap') pl.append(self.conf.profile)
iconf = self.ih.conf iconf = self.ih.conf
if iconf.user != account : account = self.conf.get('user','imap',profile)
if True or iconf.user != account :
# reinit # reinit
iconf.user = None iconf.user = None
try : try :
if account is None : account = self.index.getLightestAccount(al) if profile is None : profile = self.index.getLightestAccount(pl)
if account in al : if profile in pl :
iconf.user = account iconf.user = self.conf.get('user','imap',profile)
iconf.pwd = al[account] 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) self.ih = ImapHelper(iconf,self.rootBox)
except Exception as e: except Exception as e:
mprint('Error : ') mprint('Error : ')
mprint(e) 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() rt.stop()
if profile is None: profile = self.conf.profile
return profile
def addFile(self, path, label, catg=''): def addFile(self, path, label, catg=''):
"""""" """"""
done = False done = False
rt = RuTime(eval(__CALLER__()),DEBUG.INFO) rt = RuTime(eval(__CALLER__()),DEBUG.INFO)
self.switchFileAccount() account = self.switchFileAccount()
_, ext = splitext(path) _, ext = splitext(path)
#~ try: #~ try:
size = getsize(path) size = getsize(path)
if size > 0 : if size > 0 :
md5 = hash_sha256_file(path) md5 = hash_sha256_file(path)
account = self.ih.conf.user
mprint() mprint()
Clz.print(' account : ' , Clz.fgn7, False) Clz.print(' account : ' , Clz.fgn7, False)
Clz.print(account , Clz.fgB7) 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) self.index.add(hlst['head'][3],label,hlst['head'][1],ext,ownerHash,catg,md5,bFlag,size,account)
done = self.saveIndex() done = self.saveIndex()
else : # clean
# clean for mid, row in sendIds :
for mid, row in sendIds : if cancel : self.ih.delete(mid, True)
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')
if file_exists(self.fsplit.DIR_OUTBOX+row[1]+'.ipr') : remove(self.fsplit.DIR_OUTBOX+row[1]+'.ipr') self.clean()
self.clean()
else : else :
mprint(' ',end='') mprint(' ',end='')

View File

@ -169,6 +169,10 @@ class ImapHelper:
rt = RuTime(eval(__CALLER__('conf,"'+str(box)+'"'))) rt = RuTime(eval(__CALLER__('conf,"'+str(box)+'"')))
self.srv = IMAP4_SSL(conf.host,conf.port) self.srv = IMAP4_SSL(conf.host,conf.port)
self.conf = conf self.conf = conf
print(conf.host)
print(conf.port)
print(conf.user)
print(conf.pwd)
status, resp = self.srv.login(conf.user,conf.pwd) status, resp = self.srv.login(conf.user,conf.pwd)
if DEBUG.level <= DEBUG.ALL : if DEBUG.level <= DEBUG.ALL :
mprint(status) mprint(status)
@ -176,7 +180,7 @@ class ImapHelper:
if status == self.OK: if status == self.OK:
self.rootBox = box self.rootBox = box
if boxBin is None : 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' self.BOX_BIN = 'Trash'
if box != None : if box != None :
status, resp = self.srv.select(self.rootBox) status, resp = self.srv.select(self.rootBox)

View File

@ -327,7 +327,7 @@ class IniFile:
"""""" """"""
content = '' content = ''
main = '' main = ''
for s in self.dic: for s in sorted(self.dic):
if section=='*' or section+'.'==s[:len(section)+1]: if section=='*' or section+'.'==s[:len(section)+1]:
if s!='main': if s!='main':
#~ if section=='*': content += '\n['+s+']\n' #~ if section=='*': content += '\n['+s+']\n'