From 46bb274636a4949b98e98d785a738d650878f64d Mon Sep 17 00:00:00 2001 From: a-Sansara Date: Sat, 29 Sep 2012 20:38:04 +0200 Subject: [PATCH] bugfix upload --- impra/cli.py | 91 ++++++++++++++++++++++++++++++++++------------ impra/core.py | 95 +++++++++++++++++++++++++------------------------ impra/imap.py | 10 +++--- impra/util.py | 23 +++++++++--- imprastorage.py | 7 +++- setup_build.py | 13 ++++--- 6 files changed, 155 insertions(+), 84 deletions(-) diff --git a/impra/cli.py b/impra/cli.py index f97f81f..f3dca28 100644 --- a/impra/cli.py +++ b/impra/cli.py @@ -3,7 +3,7 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # software : ImpraStorage # -# version : 0.4 # +# version : 0.6 # # date : 2012 # # licence : GPLv3.0 # # author : a-Sansara # @@ -41,7 +41,7 @@ LINE_SEP_LEN = 120 LINE_SEP_CHAR = '―' if not Clz.isUnix : LINE_SEP_CHAR = '-' APP_TITLE = 'ImpraStorage' -APP_VERSION = '0.5' +APP_VERSION = '0.6' APP_AUTHOR = 'a-Sansara' APP_COPY = 'pluie.org' APP_LICENSE = 'GNU GPLv3' @@ -118,13 +118,14 @@ class Cli: gpConf.add_option('-V', '--view' , help='view configuration' , action='store' ) gpConf.add_option('-L', '--load' , help='load configuration' , action='store' ) gpConf.add_option('-S', '--save' , help='save configuration' , action='store' ) + gpConf.add_option('-C', '--check' , help='check configuration' , action='store' ) gpConf.add_option('-H', '--set-host' , help='set imap host server' , action='store', metavar='HOST ') gpConf.add_option('-U', '--set-user' , help='set imap user login' , action='store', metavar='USER ') 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('-B', '--set-boxn' , help='set boxName on imap server (default:[%default])' , action='store', metavar='BOXNAME ') - gpConf.add_option('-K', '--gen-keys' , help='generate new key' , action='store_true', default=False) + 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) parser.add_option_group(gpConf) @@ -154,7 +155,7 @@ class Cli: # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~ conf CMD ~~ if a[0] == 'conf' : - + self.print_header() if o.load is not None or o.view is not None or o.save is not None : if o.view is not None : @@ -170,22 +171,28 @@ class Cli: if o.load : self.load_profile(o) - elif o.view : self.ini.print(o.active_profile) + elif o.view : + if o.view == 'all' : + sections = self.ini.getSections() + if len(sections) > 0: + Clz.print(' '+','.join(sections), Clz.fgB3) + else : Clz.print(' no profiles', Clz.fgB1) + else: self.ini.print(o.view) elif o.save : - if not o.set_host and not o.set_user and not o.set_pass and not o.set_port and not o.set_boxn and not o.set_name and not o.gen_keys: + if not o.set_host and not o.set_user and not o.set_pass and not o.set_port and not o.set_boxname and not o.set_name and not o.gen_key: parser.error(' no options specified') else : if o.set_port and not util.represents_int(o.set_port): parser.error(' port must be a number') sys.exit(1) - if o.set_boxn: self.ini.set('box' , o.set_boxn,o.active_profile+'.imap') + if o.set_boxname: self.ini.set('box' , o.set_boxname,o.active_profile+'.imap') if o.set_host: self.ini.set('host', o.set_host,o.active_profile+'.imap') if o.set_user: self.ini.set('user', o.set_user,o.active_profile+'.imap') if o.set_pass: self.ini.set('pass', o.set_pass,o.active_profile+'.imap') if o.set_port: self.ini.set('port', o.set_port,o.active_profile+'.imap') if o.set_name: self.ini.set('name', o.set_name,o.active_profile+'.infos') - if o.gen_keys: + if o.gen_key: kg = crypt.KeyGen(256) self.ini.set('key' ,kg.key,o.active_profile+'.keys') self.ini.set('mark',kg.mark,o.active_profile+'.keys') @@ -194,6 +201,9 @@ class Cli: self.ini.set('profile', o.active_profile) self.ini.write() self.ini.print(o.active_profile) + elif o.check : + self.check_profile(o.check, True) + else : self.print_usage('') @@ -205,9 +215,10 @@ class Cli: if self.check_profile(o.active_profile): + self.print_header() conf = core.ImpraConf(self.ini,o.active_profile) impst = None - try: + try: impst = core.ImpraStorage(conf) except crypt.BadKeyException as e : print() @@ -273,6 +284,7 @@ class Cli: elif a[0] == 'add': if not len(a)>1 : self.error_cmd('`'+a[0]+' need at least one argument',parser) + vfile = a[1] if util.file_exists(vfile) : if not len(a)>2 : @@ -291,8 +303,8 @@ class Cli: elif a[0] == 'get': if not len(a)>1 : self.error_cmd('`'+a[0]+' need one argument',parser) + vid = a[1] - ids = [] for sid in vid.split(',') : seq = sid.split('-') @@ -373,7 +385,14 @@ class Cli: elif a[0] == 'remove': - if not len(a)>1 : self.error_cmd('`'+a[0]+' need one argument',parser) + if not len(a)>1 : self.error_cmd('`'+a[0]+' need one argument',parser) + if not util.represents_int(a[1]): + print('\n ',end='') + Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) + Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) + Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) + print() + sys.exit(1) vid = a[1] key = impst.index.getById(vid) if key !=None : @@ -385,7 +404,7 @@ class Cli: else: print('\n ',end='') Clz.print(' == `' , Clz.bg1+Clz.fgB7, False) - Clz.print(str(o.remove) , Clz.bg1+Clz.fgB3, False) + Clz.print(a[1] , Clz.bg1+Clz.fgB3, False) Clz.print('` is not a valid id == ', Clz.bg1+Clz.fgB7) print() @@ -398,9 +417,38 @@ class Cli: parser.error(msg) sys.exit(1) - def check_profile(self,profile): + def check_profile(self,profile, activeCheck=False): """""" - return 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') + 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') + if activeCheck : + if c : + Clz.print(' '+profile+' is ok', Clz.fgB2) + Clz.print(' testing...', Clz.fgB3) + conf = core.ImpraConf(self.ini,profile) + impst = None + try: + impst = core.ImpraStorage(conf) + Clz.print(' done...', Clz.fgB2) + except crypt.BadKeyException as e : + pass + else : + Clz.print(' '+profile+' incomplete', Clz.fgB1) + Clz.print(' need :', Clz.fgB1) + if not self.ini.hasSection(profile+'.keys'): + Clz.print(' key (conf -S "'+profile+'" -K)', Clz.fgB1) + if not self.ini.has('host',profile+'.imap'): + Clz.print(' imap host (conf -S "'+profile+'" -H hostName)', Clz.fgB1) + if not self.ini.has('user',profile+'.imap'): + Clz.print(' imap user (conf -S "'+profile+'" -U userName)', Clz.fgB1) + if not self.ini.has('pass',profile+'.imap'): + Clz.print(' imap password (conf -S "'+profile+'" -X password)', Clz.fgB1) + if not self.ini.has('port',profile+'.imap'): + Clz.print(' imap port (conf -S "'+profile+'" -P port)', Clz.fgB1) + if not self.ini.has('name',profile+'.infos'): + if c : + Clz.print(' think to add your userName :',Clz.bgB3) + Clz.print(' userName (conf -S "'+profile+'" -N yourName)', Clz.fgB3) + return c def print_header(self): printLineSep(LINE_SEP_CHAR,LINE_SEP_LEN) @@ -507,10 +555,7 @@ class Cli: Clz.print('profile', Clz.fgb1, False) Clz.print('} ', Clz.fgB1, False) Clz.print('[', Clz.fgB3, False) - Clz.print(' -K ', Clz.fgB3, False) - Clz.print('{', Clz.fgB1, False) - Clz.print('length', Clz.fgB1, False) - Clz.print('}', Clz.fgB1, False) + Clz.print(' -K', Clz.fgB3, False) Clz.print(', -H ', Clz.fgB3, False) Clz.print('{', Clz.fgB1, False) Clz.print('host', Clz.fgB1, False) @@ -588,7 +633,7 @@ class Cli: Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) Clz.print(', --view'.ljust(18,' ') , Clz.fgB3, False) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1) - Clz.print(' '*50+'view the specified profile (or current)' , Clz.fgB7) + Clz.print(' '*50+'view the specified profile (or * for all available)' , Clz.fgB7) Clz.print(' '*4+'-S ' , Clz.fgB3, False) Clz.print('PROFILE'.ljust(10,' ') , Clz.fgB1, False) @@ -605,10 +650,10 @@ class Cli: Clz.print(' '*50+'set imprastorage username' , Clz.fgB7) Clz.print(' '*4+'-K ' , Clz.fgB3, False) - Clz.print('LENGTH'.ljust(10,' ') , Clz.fgB1, False) + Clz.print(''.ljust(10,' ') , Clz.fgB1, False) Clz.print(', --gen-key'.ljust(18,' ') , Clz.fgB3, False) - Clz.print('LENGTH'.ljust(10,' ') , Clz.fgB1) - Clz.print(' '*50+'generate a new key of optionnal LENGTH length (default:256)', Clz.fgB7) + Clz.print(''.ljust(10,' ') , Clz.fgB1) + Clz.print(' '*50+'generate a new key' , Clz.fgB7) Clz.print(' '*4+'-H ' , Clz.fgB3, False) Clz.print('HOST'.ljust(10,' ') , Clz.fgB1, False) diff --git a/impra/core.py b/impra/core.py index 854210e..6c73c9c 100644 --- a/impra/core.py +++ b/impra/core.py @@ -3,7 +3,7 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # software : ImpraStorage # -# version : 0.4 # +# version : 0.6 # # date : 2012 # # licence : GPLv3.0 # # author : a-Sansara # @@ -87,7 +87,8 @@ class FSplitter : def _split(self, fromPath, hlst): """""" - rt = RuTime(eval(__CALLER__())) + from impra.util import DEBUG_NOTICE, DEBUG, DEBUG_LEVEL, DEBUG_INFO + rt = RuTime(eval(__CALLER__()),DEBUG_INFO) f = open(fromPath, 'rb+') m = mmap(f.fileno(), 0) p = 0 @@ -577,11 +578,11 @@ class ImpraStorage: rt = RuTime(eval(__CALLER__()),DEBUG_INFO) if self.idx != None : self.ih.delete(self.idx, True) - 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) - self.srv.expunge() - sleep(2) + #~ 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) + #~ self.srv.expunge() + #~ sleep(2) encData = self.index.encrypt() msgIndex = self.mb.buildIndex(encData) if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(msgIndex.as_string()) @@ -613,7 +614,7 @@ class ImpraStorage: """""" lloc = [bytes(str(data[0]),'utf-8') for mid, data in enumerate(sendIds)] lsrv = self.ih.searchBySubject(subject,True) - return [ i for i in set(lloc).difference(set(lsrv))] + return [ int(i) for i in set(lloc).difference(set(lsrv))] def addFile(self, path, label, catg=''): """""" @@ -622,11 +623,10 @@ class ImpraStorage: rt = RuTime(eval(__CALLER__('"%s","%s","%s"' % (path[:13]+'...',label,catg))),DEBUG_INFO) _, ext = splitext(path) - - try: + try: size = getsize(path) - if size > 0 : - md5 = hash_md5_file(path) + if size > 0 : + md5 = hash_sha256(path) print() Clz.print(' file : ' , Clz.fgn7, False) Clz.print(path , Clz.fgN1) @@ -659,40 +659,42 @@ class ImpraStorage: 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 : - 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) - sendIds.append((mid[1],row)) - else: - print('\n-- error occured when sending part : %s\n-- retrying' % row[0]) - mid = self.ih.send(msg.as_string(), self.rootBox) - if mid is not None : - print('part %s sent as msg %s' % (row[0],mid[1])) + # dont remove + status, resp = self.ih.fetch(mid[1],'(UID BODYSTRUCTURE)', True) + if status == self.ih.OK: sendIds.append((mid[1],row)) - else: - print('\n-- can\'t send part %s\n-- cancelling ' % row[0]) - cancel = True - break + 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]) print() if not cancel : self.index.add(hlst['head'][3],label,hlst['head'][1],ext,ownerHash,catg,md5,bFlag,size) - done = self.saveIndex() - self.conf.set('nid', str(self.index.id),'index') diff = self.checkSendIds(sendIds,hlst['head'][2]) + for mid in diff : + if mid > 0: + print(mid) + #self.ih.delete(str(mid), True, False) + print('toto') + self.conf.set('nid', str(self.index.id),'index') if len(diff) > 0 : Clz.print(' error when sending, missing parts :', Clz.fgB1) - print(diff) - for mid, row in sendIds : - 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, False) - mid = self.ih.send(msg.as_string(), self.rootBox) + for mid in diff : + # 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 : + 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) - + done = self.saveIndex() # clean for mid, row in sendIds : @@ -714,6 +716,7 @@ class ImpraStorage: print() except Exception as e : + print('Erroreuh') print(e) rt.stop() return done @@ -739,7 +742,7 @@ class ImpraStorage: self.ih.delete(mid, True, False) Clz.print('\n expunge, waiting pls...\n', Clz.fgB1) self.ih.srv.expunge() - sleep(2) + sleep(0.5) self.index.rem(key) done = self.saveIndex() rt.stop() @@ -761,11 +764,11 @@ class ImpraStorage: rt = RuTime(eval(__CALLER__('"[%i] %s"' % (row[ImpraIndex.UID],row[ImpraIndex.LABEL]))),DEBUG_INFO) ck = ConfigKey(row[ImpraIndex.HASH]) hlst = ck.getHashList(key,row[ImpraIndex.PARTS],True) - ids = self._getIdsBySubject(hlst['head'][2]) + ids = self.ih.searchBySubject(hlst['head'][2],True) if len(ids) >= row[ImpraIndex.PARTS]: for mid in ids : - self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX) + self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX, True) if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(hlst['head']) for v in hlst['data']: @@ -781,13 +784,13 @@ class ImpraStorage: else : print() - Clz.print(' == `' , Clz.bg3+Clz.fgB4, False) - Clz.print(row[ImpraIndex.LABEL] , Clz.bg3+Clz.fgB1, False) - Clz.print('` invalid count parts ' , Clz.bg3+Clz.fgB4) - Clz.print(str(len(ids)) , Clz.bg3+Clz.fgB1) - Clz.print('/' , Clz.bg3+Clz.fgB4) - Clz.print(str(len(row[ImpraIndex.PARTS])) , Clz.bg3+Clz.fgB1) - Clz.print(' == ' , Clz.bg3+Clz.fgB4) + Clz.print(' == `' , Clz.BG3+Clz.fgB1, False) + Clz.print(row[ImpraIndex.LABEL] , Clz.BG3+Clz.fgB4, False) + Clz.print('` invalid count parts ' , Clz.BG3+Clz.fgB1, False) + Clz.print(str(len(ids)) , Clz.BG3+Clz.fgB4, False) + Clz.print('/' , Clz.BG3+Clz.fgB1, False) + Clz.print(str(row[ImpraIndex.PARTS]) , Clz.BG3+Clz.fgB4, False) + Clz.print(' == ' , Clz.BG3+Clz.fgB1) print() rt.stop() diff --git a/impra/imap.py b/impra/imap.py index 51e7186..8123f25 100755 --- a/impra/imap.py +++ b/impra/imap.py @@ -3,7 +3,7 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # software : ImpraStorage # -# version : 0.4 # +# version : 0.6 # # date : 2012 # # licence : GPLv3.0 # # author : a-Sansara # @@ -334,23 +334,23 @@ class ImapHelper: else : status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') Clz.print(' flag msg ' , Clz.fgn7, False) - Clz.print(str(mid) , Clz.fgB1, False) + Clz.print(str(int(mid)), Clz.fgB1, False) Clz.print(' as deleted', Clz.fgn7) if expunge : Clz.print('\n expunge, waiting server...\n', Clz.fgB1) self.srv.expunge() - sleep(2) + sleep(0.5) rt.stop() return status == self.OK - def downloadAttachment(self, msg, toDir='./'): + def downloadAttachment(self, msg, toDir='./', byUid=False): """""" from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_INFO rt = RuTime(eval(__CALLER__('%i' % int(msg))),DEBUG_INFO) if not isinstance(msg, Message) : - msg = self.email(msg) + msg = self.email(msg,byUid) for part in msg.walk(): filename = part.get_filename() if filename != None and DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(filename) diff --git a/impra/util.py b/impra/util.py index 8810c55..5410143 100755 --- a/impra/util.py +++ b/impra/util.py @@ -3,7 +3,7 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # software : ImpraStorage # -# version : 0.4 # +# version : 0.6 # # date : 2012 # # licence : GPLv3.0 # # author : a-Sansara # @@ -187,7 +187,7 @@ def __CALLER__(args=''): """ global DEBUG_LEVEL, DEBUG, DEBUG_WARN val = "self.__class__.__name__+'.%s' % stack()[1][3]+'("+quote_escape(args)+") " - if DEBUG and DEBUG_LEVEL<=DEBUG_WARN : val += "l:'+str(stack()[1][2])" + if DEBUG and DEBUG_LEVEL<=DEBUG_WARN : val += "l:'+str(stack()[1][2]) " else: val += "'" return val @@ -300,6 +300,15 @@ class IniFile: with open(path, mode='w', encoding='utf-8') as o: o.write(content) + def getSections(self): + """""" + l = {} + for s in self.dic: + section = s.split('.') + if len(section)> 1 and not section[0] in l : + l[section[0]] = 1 + return [k for i,k in enumerate(l)] + def toString(self,section='*'): """""" content = '' @@ -317,7 +326,7 @@ class IniFile: else : main += k+' = '+self.dic[s][k]+'\n' return main + content - def print(self,section='*'): + def print(self,section='*', withoutSectionName=False): """""" a = '' for s in self.dic: @@ -326,7 +335,9 @@ class IniFile: #~ if section=='*': content += '\n['+s+']\n' #~ else : content += '\n['+s[len(section)+1:]+']\n' print() - Clz.print('['+s+']', Clz.fgB3) + if not withoutSectionName : + Clz.print('['+s+']', Clz.fgB3) + else: Clz.print('['+s.split('.')[1]+']', Clz.fgB3) for k in sorted(self.dic[s]): k = k.rstrip(' ') if s!='main' : @@ -378,6 +389,10 @@ class StrIterator: self.i += 1 return self.d[self.i-1] + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ~~ class Coloriz ~~ + class Coloriz: _MARKER = '!§' diff --git a/imprastorage.py b/imprastorage.py index 408e6c4..6435ecd 100755 --- a/imprastorage.py +++ b/imprastorage.py @@ -3,7 +3,7 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # software : ImpraStorage # -# version : 0.4 # +# version : 0.6 # # date : 2012 # # licence : GPLv3.0 # # author : a-Sansara # @@ -31,6 +31,11 @@ from impra.util import IniFile, RuTime, get_file_path from impra.cli import Cli import sys, os +# TODO +# - check encrypt marker on crypt file +# - readjust cli commands +# - write help in colors + if __name__ == '__main__': Cli(get_file_path(__file__ )) diff --git a/setup_build.py b/setup_build.py index 0819e74..5a28d8c 100644 --- a/setup_build.py +++ b/setup_build.py @@ -24,14 +24,17 @@ if 'bdist_msi' in sys.argv: exe = Executable( script="imprastorage.py", base=None, - targetName="ImpraStorage.exe" + targetName="ImpraStorage.exe", + targetDir="lib", + shortcutName="ImpraStorage", ) setup( name="ImpraStorage.exe", - version="0.5", - author="Me", - description="Copyright 2012", + version="0.6", + author="a-Sansara", + description="ImpraStorage provided a private imap access to store large files. License GNU GPLv3 Copyright 2012 pluie.org", executables=[exe], + include-files=('./launcher.bat','./launcher.bat') scripts=[ 'install.py' ] @@ -40,6 +43,6 @@ else : setup( name = "ImpraStorage", version = "0.5", - description = "ImpraStorage an imap private access storage", + description = "mpraStorage provided a private imap access to store large files", options = {"build_exe": build_exe_options}, executables = [Executable("imprastorage.py", base=base)])