From 14424f3d3d9c237da59dacaf81f73bafce57ed94 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 10 Oct 2012 15:31:18 +0200 Subject: [PATCH] ensure yahoo imap compatibility --- impra/cli.py | 55 +++++++++---- impra/core.py | 212 +++++++++++++++++++++++++++----------------------- impra/imap.py | 27 ++++--- test.py | 116 --------------------------- 4 files changed, 172 insertions(+), 238 deletions(-) delete mode 100644 test.py diff --git a/impra/cli.py b/impra/cli.py index 7a93db4..9b9cb1b 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -711,12 +711,19 @@ class Cli: Clz.print('{', Clz.fgB1, False) Clz.print('name', Clz.fgB1, False) Clz.print('}', Clz.fgB1, False) - Clz.print(', -M ', Clz.fgB3, False) + Clz.print(', \\', Clz.fgB3) + + Clz.print(' '*37, Clz.fgb7, False) + Clz.print('-M ', Clz.fgB3, False) Clz.print('{', Clz.fgB1, False) - Clz.print('name', Clz.fgB1, False) + Clz.print('user', Clz.fgB1, False) Clz.print('} {', Clz.fgB1, False) Clz.print('pass', 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('}', Clz.fgB1, False) Clz.print(' ]', Clz.fgB3) def print_options(self): @@ -784,7 +791,7 @@ class Cli: Clz.print(' '*4+'-S ' , Clz.fgB3, False) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) - Clz.print(', --order'.ljust(18,' ') , Clz.fgB3, False) + Clz.print(', --save'.ljust(18,' ') , Clz.fgB3, False) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) Clz.print(' '*50+'save the specified profile' , Clz.fgB7) @@ -830,9 +837,20 @@ class Cli: Clz.print('BOXNAME'.ljust(10,' ') , Clz.fgB1, False) Clz.print(', --set-box'.ljust(18,' ') , Clz.fgB3, False) Clz.print('BOXNAME'.ljust(10,' ') , Clz.fgB1) - 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(', --set-multi'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('USER PASS'.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(', --remove-multi'.ljust(18,' ') , Clz.fgB3, False) + Clz.print('USER'.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'remove imap multi account' , Clz.fgB7) + print('\n') @@ -1060,10 +1078,11 @@ class Cli: Clz.print('-L ', Clz.fgB3, False) Clz.print('bobimap ', Clz.fgB1) - Clz.print(' '*8+'# view config current profile',Clz.fgn7) + Clz.print(' '*8+'# list all config profile',Clz.fgn7) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) Clz.print('conf ', Clz.fgB3, False) - Clz.print('-V', Clz.fgB3) + Clz.print('-V ', Clz.fgB3, False) + Clz.print('all ', Clz.fgB1) Clz.print(' '*8+'# view config profile bobgmail (current profile doesn\'t change)',Clz.fgn7) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) @@ -1071,19 +1090,29 @@ class Cli: Clz.print('-V ', Clz.fgB3, False) Clz.print('bobgmail ', Clz.fgB1) - Clz.print(' '*8+'# generate a new Key for current profile (carreful with this command if your account has no empty index - ',Clz.fgn7) - Clz.print(' '*8+'# all files will be unrecoverable without the appropriate key)',Clz.fgn7) - Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) - Clz.print('conf ', Clz.fgB3, False) - Clz.print('-S -K', Clz.fgB3) - Clz.print(' '*8+'# generate a new Key for profile bobgmail and set it as current profile (carreful with this command ',Clz.fgn7) Clz.print(' '*8+'# if your account has no empty index - all files will be unrecoverable without the appropriate key)',Clz.fgn7) Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) Clz.print('conf ', Clz.fgB3, False) Clz.print('-S ', Clz.fgB3, False) Clz.print('bobgmail ', Clz.fgB1, False) - Clz.print('-K', Clz.fgB3) + Clz.print('-K ', Clz.fgB3) + + Clz.print(' '*8+'# add multi account to profile bobgmail (accounts must be on same host)',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + 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(' '*8+'# remove multi account to profile bobgmail',Clz.fgn7) + Clz.print(' '*8+'imprastorage ', Clz.fgB7, False) + Clz.print('conf ', Clz.fgB3, False) + Clz.print('-S ', Clz.fgB3, False) + Clz.print('bobgmail ', Clz.fgB1, False) + Clz.print('-R ', Clz.fgB3, False) + Clz.print('bob23', Clz.fgB1) printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) print() diff --git a/impra/core.py b/impra/core.py index bd11423..eee1f43 100644 --- a/impra/core.py +++ b/impra/core.py @@ -285,7 +285,10 @@ class ImpraIndex: self.id = 1 else : self.dic = self.decrypt(encdata) - self.id = max([self.dic[k][self.UID] for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)])+1 + l = [self.dic[k][self.UID] for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] + if len(l) > 0 : + self.id = max(l)+1 + else: self.id = 1 for k in dicCategory : if k == "users" : for k1 in dicCategory[k]: @@ -425,18 +428,22 @@ class ImpraIndex: r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)] l = [(k,self.dic[k][self.UID]) for k in r] l2 = [k[1] for k in l] - mxid = max(l2) - import collections - l3 = [x for x, y in collections.Counter(l2).items() if y > 1] - d = [k[0] for k in l if any( k[1] == v for v in l3)] - for k in d: - mxid += 1 - print(self.dic[k]) - t = list(self.dic[k]) - t[self.UID] = mxid - print(t) - self.dic[k] = tuple(t) - self.id = mxid+1 + if len(l2)> 0 : + mxid = max(l2) + import collections + l3 = [x for x, y in collections.Counter(l2).items() if y > 1] + d = [k[0] for k in l if any( k[1] == v for v in l3)] + for k in d: + mxid += 1 + print(self.dic[k]) + t = list(self.dic[k]) + t[self.UID] = mxid + print(t) + self.dic[k] = tuple(t) + self.id = mxid+1 + else: + self.id = 1 + d = () rt.stop() return len(d)>0 @@ -711,8 +718,12 @@ class ImpraStorage: """""" from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO rt = RuTime(eval(__CALLER__()),DEBUG_INFO) - if self.idx != None : - self.ih.delete(self.idx, True) + try: + if self.idx != None : + self.ih.delete(self.idx, True) + except Exception as e : + print('error : ') + print(e) #~ if len(self.delids) > 0 : #~ for i in self.delids : self.ih.delete(i, True, False) #~ Clz.print('\n expunge, waiting server...\n', Clz.fgB1) @@ -784,106 +795,111 @@ class ImpraStorage: self.switchFileAccount() _, ext = splitext(path) - try: - size = getsize(path) - if size > 0 : - md5 = hash_sha256_file(path) - account = self.ih.conf.user - print() - Clz.print(' account : ' , Clz.fgn7, False) - Clz.print(account , Clz.fgB7) - Clz.print(' file : ' , Clz.fgn7, False) - Clz.print(path , Clz.fgN1) - Clz.print(' hash : ' , Clz.fgn7, False) - Clz.print(md5 , Clz.fgN2) - print() - if not self.index.get(md5) : + #~ try: + size = getsize(path) + if size > 0 : + md5 = hash_sha256_file(path) + account = self.ih.conf.user + print() + Clz.print(' account : ' , Clz.fgn7, False) + Clz.print(account , Clz.fgB7) + Clz.print(' file : ' , Clz.fgn7, False) + Clz.print(path , Clz.fgN1) + Clz.print(' hash : ' , Clz.fgn7, False) + Clz.print(md5 , Clz.fgN2) + print() + if not self.index.get(md5) : + + if catg=='' : catg = self.index.getAutoCatg(ext) + + bFlag = ImpraIndex.FILE_BINARY + if not is_binary(path): + bFlag = ImpraIndex.FILE_CRYPT + path = self.encryptTextFile(path) - if catg=='' : catg = self.index.getAutoCatg(ext) - - bFlag = ImpraIndex.FILE_BINARY - if not is_binary(path): - bFlag = ImpraIndex.FILE_CRYPT - path = self.encryptTextFile(path) - - hlst = self.fsplit.addFile(path,md5) - if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : - print(hlst['head']) - for v in hlst['data']: - print(v) - - usr = self.conf.get('name','infos') - ownerHash = self.mb.getHashName(usr) - self.index.addUser(usr,ownerHash) - - cancel = False - sendIds = [] - test = True - for row in hlst['data'] : - msg = self.mb.build(usr,'all',hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') - mid = self.ih.send(msg.as_string(), self.rootBox) - if mid is not None : - # dont remove - status, resp = self.ih.fetch(mid[1],'(UID BODYSTRUCTURE)', True) - if status == self.ih.OK: - sendIds.append((mid[1],row)) - print(' ',end='') - Clz.print('part ' , Clz.fgn7, False) - Clz.print(str(row[0]) , Clz.fgB2, False) - Clz.print(' sent as msg ', Clz.fgn7, False) - Clz.print(str(mid[1]) , Clz.fgB1) - else: - print('\n-- error occured when sending part : %s\n-- retrying' % row[0]) + hlst = self.fsplit.addFile(path,md5) + if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : + print(hlst['head']) + for v in hlst['data']: + print(v) + + usr = self.conf.get('name','infos') + ownerHash = self.mb.getHashName(usr) + self.index.addUser(usr,ownerHash) + + cancel = False + sendIds = [] + test = True + for row in hlst['data'] : + msg = self.mb.build(usr,'all',hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') + mid = self.ih.send(msg.as_string(), self.rootBox) + if mid is not None : + #sleep(0.5) + # dont remove + status, resp = self.ih.fetch(mid[1],'(UID BODYSTRUCTURE)', True) + if status == self.ih.OK: + sendIds.append((mid[1],row)) + print(' ',end='') + Clz.print('part ' , Clz.fgn7, False) + Clz.print(str(row[0]) , Clz.fgB2, False) + Clz.print(' sent as msg ', Clz.fgn7, False) + Clz.print(str(mid[1]) , Clz.fgB1) + else: + print('\n-- error occured when sending part : %s\n-- retrying' % row[0]) - if not cancel : + if not cancel : - diff = self.checkSendIds(sendIds,hlst['head'][2]) + diff = self.checkSendIds(sendIds,hlst['head'][2]) + #~ print('diff') + #~ for mid in diff : + #~ if mid > 0: + #~ print(mid) + #self.ih.delete(str(mid), True, False) + if len(diff) > 0 : for mid in diff : - if mid > 0: - print(mid) - #self.ih.delete(str(mid), True, False) - if len(diff) > 0 : - Clz.print(' error when sending, missing parts :', Clz.fgB1) - for mid in diff : + status, resp = self.ih.fetch(str(mid),'(UID BODYSTRUCTURE)', True) + if not status == self.ih.OK: + Clz.print(' error when sending, missing parts :', Clz.fgB1) # bugfix mid would be without +1 k = [ k for k in sendIds if len(k)>0 and int(k[0]) == int(mid+1)] if len(k) > 0 : + print(k) row = k[0][1] msg = self.mb.build(usr,'all',hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') Clz.print(' resending part ' , Clz.fgn7, False) Clz.print(str(row[0]) , Clz.fgN2) mid = self.ih.send(msg.as_string(), self.rootBox) - else : - print() - #Clz.print(' index intall files checked\n', Clz.fgB2) - self._setIndexImap() - self.index = self.getIndex(True) - 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() + else : + print() + #Clz.print(' index intall files checked\n', Clz.fgB2) + self._setIndexImap() + self.index = self.getIndex(True) + 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() - else : - print(' ',end='') - Clz.print(' == file already exist on server as ' , Clz.fgN7+Clz.bg1, False) - Clz.print(self.index.dic[md5][ImpraIndex.LABEL] , Clz.bg1+Clz.fgB3, False) - Clz.print(' [id:' , Clz.fgN7+Clz.bg1, False) - Clz.print(str(self.index.dic[md5][ImpraIndex.UID]) , Clz.bg1+Clz.fgB3, False) - Clz.print('] == ' , Clz.fgN7+Clz.bg1) - print() else : print(' ',end='') - Clz.print(' == files is empty or don\t exists == ' , Clz.bg1+Clz.fgN7) + Clz.print(' == file already exist on server as ' , Clz.fgN7+Clz.bg1, False) + Clz.print(self.index.dic[md5][ImpraIndex.LABEL] , Clz.bg1+Clz.fgB3, False) + Clz.print(' [id:' , Clz.fgN7+Clz.bg1, False) + Clz.print(str(self.index.dic[md5][ImpraIndex.UID]) , Clz.bg1+Clz.fgB3, False) + Clz.print('] == ' , Clz.fgN7+Clz.bg1) print() + else : + print(' ',end='') + Clz.print(' == files is empty or don\t exists == ' , Clz.bg1+Clz.fgN7) + print() - except Exception as e : - print('Erroreuh') - print(e) + #~ except Exception as e : + #~ print('Erroreuh') + #~ print(e) self._setIndexImap() rt.stop() return done diff --git a/impra/imap.py b/impra/imap.py index 6b2855c..db0ad54 100644 --- a/impra/imap.py +++ b/impra/imap.py @@ -164,17 +164,21 @@ class ImapHelper: BOX_BIN = '[Gmail]/Corbeille' """""" - def __init__(self, conf, box='INBOX'): + def __init__(self, conf, box='INBOX', boxBin=None): """""" - rt = RuTime(eval(__CALLER__('conf,"'+box+'"'))) + rt = RuTime(eval(__CALLER__('conf,"'+str(box)+'"'))) self.srv = IMAP4_SSL(conf.host,conf.port) self.conf = conf - self.srv.login(conf.user,conf.pwd) + status, resp = self.srv.login(conf.user,conf.pwd) self.rootBox = box - status, resp = self.srv.select(self.rootBox) - if status == self.KO : - self.createBox(self.rootBox) - self.srv.select(self.rootBox) + if boxBin is None : + if search('yahoo.com',conf.host) is not None : + self.BOX_BIN = 'Trash' + if box != None : + status, resp = self.srv.select(self.rootBox) + if status == self.KO : + self.createBox(self.rootBox) + self.srv.select(self.rootBox) rt.stop() def status(self,box='INBOX'): @@ -313,7 +317,7 @@ class ImapHelper: #~ print(type(mid)) #~ print(mid) #status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') - status, resp = self.srv.uid('store', mid, '+FLAGS', '\\Deleted' ) + status, resp = self.srv.uid('store', mid, '+FLAGS (\\Deleted)' ) print(' ',end='') Clz.print(' deleting msg ',Clz.fgN7+Clz.bg1, False) Clz.print(str(int(mid)) ,Clz.bg1+Clz.fgB3) @@ -331,9 +335,9 @@ class ImapHelper: status = None if int(mid) > 0 : if byUid: - status, resp = self.srv.uid( 'store', mid, '+FLAGS', '\\Deleted' ) + status, resp = self.srv.uid( 'store', mid, '+FLAGS (\\Deleted)' ) else : - status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') + status, resp = self.srv.store(mid, '+FLAGS', 'Deleted') Clz.print(' flag msg ' , Clz.fgn7, False) Clz.print(str(int(mid)), Clz.fgB1, False) Clz.print(' as deleted', Clz.fgn7) @@ -373,7 +377,8 @@ class ImapHelper: if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE: print(status) print(resp) - mid = str(resp[0],'utf-8')[11:-11].split(' ') + m = search(b']', resp[0]) + mid = str(resp[0],'utf-8')[11:-(len(resp[0])-m.start())].split(' ') rt.stop() return mid diff --git a/test.py b/test.py deleted file mode 100644 index f800124..0000000 --- a/test.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # -# # -# software : ImpraStorage # -# version : 0.4 # -# date : 2012 # -# licence : GPLv3.0 # -# author : a-Sansara # -# copyright : pluie.org # -# # -# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # -# -# This file is part of ImpraStorage. -# -# ImpraStorage is free software (free as in speech) : you can redistribute it -# and/or modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation, either version 3 of the License, -# or (at your option) any later version. -# -# ImpraStorage is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License -# along with ImpraStorage. If not, see . - -from impra.core import ImpraConf, ImpraStorage -from impra.util import IniFile, RuTime, get_file_path, PColor -from impra.cli import Cli -import sys, os - -if __name__ == '__main__': - #~ try : - #~ print('\033[1;30mGray like Ghost\033[1;m') - #~ print('\033[1;31mRed like Radish\033[1;m') - #~ print('\033[1;32mGreen like Grass\033[1;m') - #~ print('\033[1;33mYellow like Yolk\033[1;m') - #~ print('\033[1;34mBlue like Blood\033[1;m') - #~ print('\033[1;35mMagenta like Mimosa\033[1;m') - #~ print('\033[1;36mCyan like Caribbean\033[1;m') - #~ print('\033[1;37mWhite like Whipped Cream\033[1;m') - #~ print('\033[1;38mCrimson like Chianti\033[1;m') - #~ print('\033[1;41mHighlighted Red like Radish\033[1;m') - #~ print('\033[1;42mHighlighted Green like Grass\033[1;m') - #~ print('\033[1;43mHighlighted Brown like Bear\033[1;m') - #~ print('\033[1;44mHighlighted Blue like Blood\033[1;m') - #~ print('\033[1;45mHighlighted Magenta like Mimosa\033[1;m') - #~ print('\033[1;46mHighlighted Cyan like Caribbean\033[1;m') - #~ print('\033[1;47mHighlighted Gray like Ghost\033[1;m') - #~ print('\033[1;48mHighlighted Crimson like Chianti\033[1;m') - from re import finditer as regfinditer - data = ' ==> MaClasse.maMethod "mon param","other Param" ' - qp = (int(a.start()) for a in list(regfinditer('\(', data))) - print(data) - print([m.start() for m in regfinditer('"', data)]) - sp = [m.start() for m in regfinditer('\(', data)] - ep = [m.start() for m in regfinditer('\)', data)] - - Clz = PColor() - if len(sp) > 0 : - Clz.print(data[:sp[0]+1], Clz.fgb3, False) - Clz.print(data[sp[0]+1:ep[0]], Clz.fgn2, False) - Clz.print(data[ep[0]:], Clz.fgb3, False) - else: Clz.print(data, Clz.fgb2, False) - #~ clz.print(' -- ImpraStorage -- ',clz.BG4+clz.fgB3,False,True) - #~ clz.print(' [',clz.fgB0,False) - #~ clz.print('account',clz.fgB3,False) - #~ clz.print(':',clz.fgB0,False) - #~ clz.print('gplslot.001',clz.fgB4,False) - #~ clz.print('] ',clz.fgB0) - #~ print() - #~ clz.print('color0',clz.fgb0) - #~ clz.print('color1',clz.fgb1) - #~ clz.print('color2',clz.fgb2) - #~ clz.print('color3',clz.fgb3) - #~ clz.print('color4',clz.fgb4) - #~ clz.print('color5',clz.fgb5) - #~ clz.print('color6',clz.fgb6) - #~ clz.print('color7',clz.fgb7) - #~ print() - #~ clz.print('color0',clz.fgB0) - #~ clz.print('color1',clz.fgB1) - #~ clz.print('color2',clz.fgB2) - #~ clz.print('color3',clz.fgB3) - #~ clz.print('color4',clz.fgB4) - #~ clz.print('color5',clz.fgB5) - #~ clz.print('color6',clz.fgB6) - #~ clz.print('color7',clz.fgB7) - #~ print() - #~ clz.print('color0',clz.bg0+clz.fgb0) - #~ clz.print('color1',clz.bg1+clz.fgb0) - #~ clz.print('color2',clz.bg2+clz.fgb0) - #~ clz.print('color3',clz.bg3+clz.fgb0) - #~ clz.print('color4',clz.bg4+clz.fgb0) - #~ clz.print('color5',clz.bg5+clz.fgb0) - #~ clz.print('color6',clz.bg6+clz.fgb0) - #~ clz.print('color7',clz.bg7+clz.fgb0) - #~ print() - #~ clz.print('color0',clz.BG0+clz.fgB0) - #~ clz.print('color1',clz.BG1+clz.fgB0) - #~ clz.print('color2',clz.BG2+clz.fgB0) - #~ clz.print('color3',clz.BG3+clz.fgB0) - #~ clz.print('color4',clz.BG4+clz.fgB0) - #~ clz.print('color5',clz.BG5+clz.fgB0) - #~ clz.print('color6',clz.BG6+clz.fgB0) - #~ clz.print('color7',clz.BG7+clz.fgB0) - #~ print() - #Cli(get_file_path(__file__ )) - #os.system('echo python imprastorage data -l') - - #~ except Exception as e : - #~ print(e) - -#python -O -m compileall impra/*.py