cli search

This commit is contained in:
a-sansara 2012-09-17 17:34:08 +02:00
parent 54cd707836
commit c9460a5e49
6 changed files with 155 additions and 253 deletions

View File

@ -49,7 +49,7 @@ transfert process is transparent. Just vizualize locally the index of
stored files and simply select files to download or upload. stored files and simply select files to download or upload.
ImpraStorage automatically launch the parts to download, then merge parts ImpraStorage automatically launch the parts to download, then merge parts
in the appropriate way to rebuild the original file. Inversely, a file in the appropriate way to rebuild the original file. Inversely, a file
to upload is splitt -in several parts with addition of noise data), and to upload is split (in several parts with addition of noise data), and
ImpraStorage randomly upload each parts then update the index. ImpraStorage randomly upload each parts then update the index.
""" """
@ -250,9 +250,12 @@ you can remove index but all presents files on the box %s will be unrecoverable
if uid == None : uid = 'EMPTY' if uid == None : uid = 'EMPTY'
if date == None : date = '' if date == None : date = ''
if impst.index != None: if impst.index != None:
impst.index.print(True,'-'*120+'\n -- INDEX(`'+uid+'`) boxname :`'+impst.rootBox+'` '+date+'\n'+'-'*120) impst.index.print('-'*120+'\n -- INDEX(`'+uid+'`) boxname :`'+impst.rootBox+'` '+date+'\n'+'-'*120)
#encData = impst.index.impraEncrypt(impst.index.toString()) #encData = impst.index.impraEncrypt(impst.index.toString())
#~ dd = """coucou mon joli coeur :*:* je s""" #~ dd = """coucou mon joli coeur :*:* je sais que je te saoule avec ça mais bon putain tu va te planter ou merde"""
#~ dd = """01234567890123456789012345678901234567890123456789#"""
#~
#~
#~ kg = crypt.KeyGen(256) #~ kg = crypt.KeyGen(256)
#~ print('-- key --') #~ print('-- key --')
#~ print(kg.key) #~ print(kg.key)
@ -260,7 +263,7 @@ you can remove index but all presents files on the box %s will be unrecoverable
#~ print(kg.mark) #~ print(kg.mark)
#~ km = crypt.Kirmah(kg.key, kg.mark) #~ km = crypt.Kirmah(kg.key, kg.mark)
#~ encData = km.encrypt(dd,'.index',22) #~ encData = km.encrypt(dd,'.index',22)
#~ print('*'+encData+'*') #~ #print('*'+encData+'*')
#~ decData = km.decrypt(encData,'.index',22) #~ decData = km.decrypt(encData,'.index',22)
#~ print('*'+decData+'*') #~ print('*'+decData+'*')
@ -277,8 +280,11 @@ you can remove index but all presents files on the box %s will be unrecoverable
else: print(o.get_by_id+' a is not valid id') else: print(o.get_by_id+' a is not valid id')
elif o.search : elif o.search :
print(o.search) label = impst.index.searchByPattern(o.search)
if label==None:
print(' -- no match found for pattern `%s` --' % o.search)
else:
impst.index.print('-'*120+'\n -- SEARCH: `'+o.search+'` -- found '+str(len(label))+' results --\n'+'-'*120,label)
elif o.remove : elif o.remove :
print(o.remove) print(o.remove)

View File

@ -37,16 +37,17 @@ from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.utils import formatdate from email.utils import formatdate
from json import dump as jdump, load as jload, dumps as jdumps, loads as jloads
from math import ceil, floor from math import ceil, floor
from mmap import mmap from mmap import mmap
from os import remove, urandom, sep from os import remove, urandom, sep
from os.path import abspath, dirname, join, realpath, basename, getsize, splitext from os.path import abspath, dirname, join, realpath, basename, getsize, splitext
from re import split as regsplit from re import split as regsplit, match as regmatch, compile as regcompile, search as regsearch
from impra.imap import ImapHelper, ImapConfig from impra.imap import ImapHelper, ImapConfig
from impra.util import __CALLER__, RuTime, formatBytes, randomFrom, bstr, quote_escape, stack, run, file_exists, get_file_content from impra.util import __CALLER__, RuTime, formatBytes, randomFrom, bstr, quote_escape, stack, run, file_exists, get_file_content, DEBUG, DEBUG_ALL, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN
from impra.crypt import Kirmah, ConfigKey, Noiser, Randomiz, hash_sha256 from impra.crypt import Kirmah, ConfigKey, Noiser, Randomiz, hash_sha256
DEBUG = True
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -59,10 +60,9 @@ class FSplitter :
"""""" """"""
self.ck = ck self.ck = ck
self.wkdir = wkdir self.wkdir = wkdir
self.DIR_CACHE = self.wkdir+sep+'cache'+sep self.DIR_INBOX = join(self.wkdir,'inbox')+sep
self.DIR_INBOX = self.wkdir+sep+'inbox'+sep self.DIR_OUTBOX = join(self.wkdir,'outbox')+sep
self.DIR_OUTBOX = self.wkdir+sep+'outbox'+sep self.DIR_DEPLOY = join(self.wkdir,'deploy')+sep
self.DIR_DEPLOY = self.wkdir+sep+'deploy'+sep
def addFile(self, fromPath, label, fixCount = False): def addFile(self, fromPath, label, fixCount = False):
"""""" """"""
@ -70,8 +70,8 @@ class FSplitter :
fsize = getsize(fromPath) fsize = getsize(fromPath)
count = ceil(fsize/self.ck.psize) count = ceil(fsize/self.ck.psize)
minp, maxp = 52, 62 minp, maxp = 52, 62
if fsize < 4800000 : minp, maxp = 8, 12 if fsize < 4800000 : minp, maxp = 8, 16
elif fsize < 22200000 : minp, maxp = 12, 22 elif fsize < 22200000 : minp, maxp = 16, 22
elif fsize < 48000000 : minp, maxp = 22, 32 elif fsize < 48000000 : minp, maxp = 22, 32
elif fsize < 222000000 : minp, maxp = 32, 42 elif fsize < 222000000 : minp, maxp = 32, 42
if not fixCount : if not fixCount :
@ -91,7 +91,7 @@ class FSplitter :
m = mmap(f.fileno(), 0) m = mmap(f.fileno(), 0)
p = 0 p = 0
psize = ceil(getsize(fromPath)/hlst['head'][1]) psize = ceil(getsize(fromPath)/hlst['head'][1])
print(str(psize)) print(formatBytes(psize)+' '+str(len(hlst['data']))+' parts')
while m.tell() < m.size(): while m.tell() < m.size():
self._splitPart(m,p,psize,hlst['data'][p]) self._splitPart(m,p,psize,hlst['data'][p])
p += 1 p += 1
@ -217,30 +217,16 @@ class ImpraIndex:
`mark` : str `mark` : str
appropriate mark to check correct key appropriate mark to check correct key
`encdata` : str `encdata` : str
initial content of the index encrypted with rsa initial content of the index encrypted with Kirmah Algorythm
and representing a dic index as json string
""" """
self.km = Kirmah(key, mark) self.km = Kirmah(key, mark)
self.dic = {} self.dic = {}
self.id = id self.id = id
if encdata =='' : data = encdata if encdata =='' : self.dic = {}
else : data = self.decrypt(encdata) else : self.dic = self.decrypt(encdata)
data = data.replace(self.QUOTE_REPL, '\'')
ld = regsplit('\n?\r? ?'+self.SEP_CATEGORY+' ?\n\r??',data)
l = regsplit(self.SEP_ITEM,ld[0])
for row in l:
d = regsplit(self.SEP_TOKEN,row)
del d[7:]
# key : count, hash, ext, usr, cat
if len(d)>5 and d!='':
self.dic[d[1]] = d
if len(ld)>1:
l = regsplit(self.SEP_ITEM,ld[1].lstrip('\n\r'))
for row in l:
d = regsplit(' ?= ?',row,1)
if len(d)> 1 and len(d[0]) > 3 :
self.dic[d[0].lstrip('\n\r')] = d[1]
else:
for k in dicCategory : for k in dicCategory :
if not self.SEP_KEY_INTERN+k in self.dic:
self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k] self.dic[self.SEP_KEY_INTERN+k] = dicCategory[k]
def add(self,key, label, count, ext='', usr='', cat=''): def add(self,key, label, count, ext='', usr='', cat=''):
@ -253,6 +239,22 @@ class ImpraIndex:
else : else :
print(label+' already exist') print(label+' already exist')
def addUser(self, nameFrom, hashName):
""""""
if not self.SEP_KEY_INTERN+'users' in self.dic:
self.dic[self.SEP_KEY_INTERN+'users'] = {}
if not hashName in self.dic[self.SEP_KEY_INTERN+'users']:
self.dic[self.SEP_KEY_INTERN+'users'][hashName] = nameFrom
def getUser(self, hashName):
""""""
usrName = 'Anonymous'
if not str(self.SEP_KEY_INTERN+'users') in self.dic:
self.dic[self.SEP_KEY_INTERN+'users'] = {}
elif hashName in self.dic[self.SEP_KEY_INTERN+'users']:
usrName = self.dic[self.SEP_KEY_INTERN+'users'][hashName]
return usrName
def rem(self,label): def rem(self,label):
"""Remove the selected label from the index""" """Remove the selected label from the index"""
self.dic.pop(label, None) self.dic.pop(label, None)
@ -263,50 +265,63 @@ class ImpraIndex:
def searchById(self,sid): def searchById(self,sid):
"""Search the corresponding label in the index""" """Search the corresponding label in the index"""
rt = RuTime(eval(__CALLER__())) rt = RuTime(eval(__CALLER__(sid)))
l = None l = None
r = [v for i, v in enumerate(self.dic) if self.dic[v][6] == str(sid)] r = [v for i, v in enumerate(self.dic) if not v.startswith(self.SEP_KEY_INTERN) and self.dic[v][6] == int(sid)]
if len(r)>0: l = r[0] if len(r)==1: l = r[0]
rt.stop() rt.stop()
return l return l
def toString(self, withoutCatg=False, idFirst=False): def searchByPattern(self,pattern):
""""""
rt = RuTime(eval(__CALLER__(pattern)))
l = None
r = [ v for i,v in enumerate(self.dic) if not v.startswith(self.SEP_KEY_INTERN) and regsearch(pattern,self.dic[v][1]) is not None ]
l = [self.dic[k][6] for k in r]
rt.stop()
return l
def toString(self,matchIds):
"""Make a string representation of the index as it was store on the server""" """Make a string representation of the index as it was store on the server"""
data = cdata = '' data = ''
for k in sorted(self.dic): r = [k for i, k in enumerate(self.dic) if not k.startswith(self.SEP_KEY_INTERN)]
for k in r :
v = self.dic.get(k) v = self.dic.get(k)
k = k.lstrip('\n\r') k = k.lstrip('\n\r')
if k[0]==self.SEP_KEY_INTERN and len(k)>1: if not k[0]==self.SEP_KEY_INTERN and len(k)>1:
cdata += k+'='+v+self.SEP_ITEM if matchIds==None or v[6] in matchIds:
else :
if not idFirst :
for i in v:
data += str(i)+self.SEP_TOKEN
else :
data += str(v[6]).rjust(1+ceil(len(str(v[6]))/10),' ')+' ' data += str(v[6]).rjust(1+ceil(len(str(v[6]))/10),' ')+' '
data += str(v[0])[0:42]+'... ' data += str(v[0])[0:12]+'... '
data += str(v[1]).ljust(30,' ')+' ' data += str(v[1]).ljust(42,' ')+' '
for i in v[2:-1]: data += str(v[2]).rjust(2,'0')+' '
data += str(i)+' ' data += str(v[3]).ljust(5,' ')+' '
data = data.rstrip(self.SEP_TOKEN)+self.SEP_ITEM data += self.getUser(str(v[4])).ljust(15,' ')+' '
if not withoutCatg : data += str(v[5])+' '
data += self.SEP_CATEGORY+'\n'+cdata #~ elif len(k)>1:
#~ print(k,'=',v)
data = data+self.SEP_ITEM
return data; return data;
def encrypt(self): def encrypt(self):
"""""" """"""
return self.km.encrypt(self.toString().replace('\'', self.QUOTE_REPL),'.index',22) #~ print('encrypting index :')
jdata = jdumps(self.dic)
#~ print(jdata)
return self.km.encrypt(jdata,'.index',22)
def decrypt(self,data): def decrypt(self,data):
"""""" """"""
if data!='': data = self.km.decrypt(data,'.index',22) #~ print('decrypting index : ')
jdata = self.km.decrypt(data,'.index',22)
#~ print(jdata)
data = jloads(jdata)
return data return data
def print(self,withoutCatg=False, header=''): def print(self,header='', matchIds=None):
"""Print index content as formated bloc""" """Print index content as formated bloc"""
data = self.toString(withoutCatg,True).split(';') data = self.toString(matchIds).split(self.SEP_ITEM)
print(header) print(header)
print('id'+' '*2+'hash'+' '*43+'label'+' '*26+'part'+' '*2+'type'+' '*3+'from'+' '*11+'category') print('id'+' '*2+'hash'+' '*13+'label'+' '*40+'part'+' '*2+'type'+' '*2+'owner'+' '*12+'category')
print('-'*120) print('-'*120)
for row in data: for row in data:
if row.rstrip('\n') != '': print(row) if row.rstrip('\n') != '': print(row)
@ -366,25 +381,27 @@ class ImpraStorage:
def getIndex(self): def getIndex(self):
"""""" """"""
rt = RuTime(eval(__CALLER__())) from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_WARN, DEBUG_INFO
rt = RuTime(eval(__CALLER__()),DEBUG_INFO)
index = None index = None
encData = '' encData = ''
uid = self.conf.get('uid' ,'index') uid = self.conf.get('uid' ,'index')
date = self.conf.get('date','index') date = self.conf.get('date','index')
nid = self.conf.get('nid' ,'index') nid = self.conf.get('nid' ,'index')
if nid==None : nid = 0 if nid==None : nid = 0
if uid !=None : print(uid+' - '+date+' - ['+(str(nid))+']')
self._getIdIndex() self._getIdIndex()
if self.idx : if self.idx :
# getFromFile # getFromFile
if uid != None and int(self.idx) == int(uid) and file_exists(self.pathInd): if uid != None and int(self.idx) == int(uid) and file_exists(self.pathInd):
encData = get_file_content(self.pathInd) encData = get_file_content(self.pathInd)
print('cache') print(' index in cache')
else: else:
encData = self._getCryptIndex() encData = self._getCryptIndex()
with open(self.pathInd, mode='w', encoding='utf-8') as o: with open(self.pathInd, mode='w', encoding='utf-8') as o:
o.write(encData) o.write(encData)
index = ImpraIndex(self.conf.get('key','keys'),self.conf.get('mark','keys'), encData, {'catg':self.conf.get('types','catg')}, int(nid)) usrName = self.conf.get('name','infos')
usrHash = self.mb.getHashName(usrName)
index = ImpraIndex(self.conf.get('key','keys'),self.conf.get('mark','keys'), encData, {'catg':self.conf.get('types','catg'), 'users':{ ('%s' % self.mb.getHashName('all')) : 'all', ('%s' % usrHash) : usrName}}, int(nid))
rt.stop() rt.stop()
return index return index
@ -397,14 +414,14 @@ class ImpraStorage:
def saveIndex(self): def saveIndex(self):
"""""" """"""
global DEBUG from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO
rt = RuTime(eval(__CALLER__())) rt = RuTime(eval(__CALLER__()),DEBUG_INFO)
if self.idx != None : if self.idx != None :
self.ih.delete(self.idx, True) self.ih.delete(self.idx, True)
for i in self.delids : self.ih.delete(i, True) for i in self.delids : self.ih.delete(i, True)
encData = self.index.encrypt() encData = self.index.encrypt()
msgIndex = self.mb.buildIndex(encData) msgIndex = self.mb.buildIndex(encData)
if DEBUG: print(msgIndex.as_string()) if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(msgIndex.as_string())
ids = self.ih.send(msgIndex.as_string(), self.rootBox) ids = self.ih.send(msgIndex.as_string(), self.rootBox)
date = self.ih.headerField('date', ids[1], True) date = self.ih.headerField('date', ids[1], True)
self.conf.set('uid',ids[1],'index') self.conf.set('uid',ids[1],'index')
@ -417,8 +434,8 @@ class ImpraStorage:
def addFile(self, path, label, usr='all', catg=''): def addFile(self, path, label, usr='all', catg=''):
"""""" """"""
global DEBUG from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO
rt = RuTime(eval(__CALLER__('"%s","%s","%s"' % (path[:13]+'...',label,usr)))) rt = RuTime(eval(__CALLER__('"%s","%s","%s"' % (path[:13]+'...',label,usr))),DEBUG_INFO)
#~ hlst = self.fsplit.addFile(path,label) #~ hlst = self.fsplit.addFile(path,label)
#~ self.fsplit.deployFile(hlst,True) #~ self.fsplit.deployFile(hlst,True)
@ -426,11 +443,13 @@ class ImpraStorage:
try: try:
if self.index.search(label)==None : if self.index.search(label)==None :
hlst = self.fsplit.addFile(path,label) hlst = self.fsplit.addFile(path,label)
if DEBUG : if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE :
print(hlst['head']) print(hlst['head'])
for v in hlst['data']: for v in hlst['data']:
print(v) print(v)
nameFrom = self.conf.ini.get('name',self.conf.profile+'.infos') nameFrom = self.conf.get('name','infos')
self.index.addUser(nameFrom, self.mb.getHashName(nameFrom))
for row in hlst['data'] : for row in hlst['data'] :
msg = self.mb.build(nameFrom,usr,hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr') msg = self.mb.build(nameFrom,usr,hlst['head'][2],self.fsplit.DIR_OUTBOX+row[1]+'.ipr')
self.ih.send(msg.as_string(), self.rootBox) self.ih.send(msg.as_string(), self.rootBox)
@ -446,8 +465,9 @@ class ImpraStorage:
def getFile(self,label): def getFile(self,label):
"""""" """"""
global DEBUG from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_NOTICE, DEBUG_WARN, DEBUG_INFO
rt = RuTime(eval(__CALLER__('"%s"' % label)))
rt = RuTime(eval(__CALLER__('"%s"' % label)),DEBUG_INFO)
if label==None : if label==None :
print(str(label)+' unexist') print(str(label)+' unexist')
else : else :
@ -463,7 +483,7 @@ class ImpraStorage:
if to == self.mb.getHashName('all')+'@'+self.mb.DOMAIN_NAME or to == self.mb.getHashName(self.conf.ini.get('name',self.conf.profile+'.infos'))+'@'+self.mb.DOMAIN_NAME : if to == self.mb.getHashName('all')+'@'+self.mb.DOMAIN_NAME or to == self.mb.getHashName(self.conf.ini.get('name',self.conf.profile+'.infos'))+'@'+self.mb.DOMAIN_NAME :
for mid in ids : for mid in ids :
self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX) self.ih.downloadAttachment(mid,self.fsplit.DIR_INBOX)
if DEBUG : if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE :
print(hlst['head']) print(hlst['head'])
for v in hlst['data']: for v in hlst['data']:
print(v) print(v)

View File

@ -26,7 +26,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with ImpraStorage. If not, see <http://www.gnu.org/licenses/>. # along with ImpraStorage. If not, see <http://www.gnu.org/licenses/>.
from impra.util import RuTime, __CALLER__, stack from impra.util import RuTime, __CALLER__, stack, DEBUG
from base64 import urlsafe_b64encode, b64decode from base64 import urlsafe_b64encode, b64decode
from binascii import b2a_base64, a2b_base64 from binascii import b2a_base64, a2b_base64
from hashlib import sha256 from hashlib import sha256
@ -219,17 +219,19 @@ class Kirmah:
dataEnc = '' dataEnc = ''
psize = ceil(len(data)/cpart) psize = ceil(len(data)/cpart)
cp = 0 cp = 0
for row in hlst['data']: for row in hlst['data']:
#~ print(row)
#~ print('ns:%i - dataLength:%i - dataEncLength:%i - ne:%i' % (row[2],len(data), len(dataEnc), row[3]))
#~ print(data[cp*psize:cp*psize+psize])
dataEnc += self.ck.noiser.getNoise(row[2],True)+data[cp*psize:cp*psize+psize]+self.ck.noiser.getNoise(row[3],True) dataEnc += self.ck.noiser.getNoise(row[2],True)+data[cp*psize:cp*psize+psize]+self.ck.noiser.getNoise(row[3],True)
cp += 1 cp += 1
dataEnc = str(b2a_base64(bytes(dataEnc,'utf-8')),'utf-8') dataEnc = str(b2a_base64(bytes(dataEnc,'utf-8')),'utf-8')
rt.stop()
#~ dataEnc = self.subenc(odata)
with open('./.KirmahENC', mode='w') as o: with open('./.KirmahENC', mode='w') as o:
o.write(dataEnc) o.write(dataEnc)
rt.stop()
return dataEnc return dataEnc
def decrypt(self, data, label, cpart): def decrypt(self, data, label, cpart):
@ -244,24 +246,31 @@ class Kirmah:
rsz += row[2]+row[3] rsz += row[2]+row[3]
psize = ceil((len(data)-rsz)/cpart) psize = ceil((len(data)-rsz)/cpart)
for row in hlst['data']: for row in hlst['data']:
si = ni + row[2] si = ni + row[2]
ei = si + psize ei = si + psize
#~ print(row)
#~ print('si:%i - ei:%i - datalength:%i - dataDeclength:%i' % (si,ei,len(data),len(dataDec)))
if cp == cpart-1 : if cp == cpart-1 :
ei = -row[3] ei = -row[3]
if si > len(data)+ei : if not si > len(data)+ei : pass
### to delete #si = len(data)+ei
#~ print('si:%i - ei:%i' % (si,ei))
else :
#~ print('si: - ei:%i' % (len(data)+ei-si))
dataDec=dataDec[:len(data)+ei-si] dataDec=dataDec[:len(data)+ei-si]
break break
si = len(data)+ei #~ print(data[si:ei])
dataDec += data[si:ei] dataDec += data[si:ei]
ni = ei + row[3] ni = ei + row[3]
cp += 1 cp += 1
dataDec = self.subdec(dataDec) dataDec = self.subdec(dataDec)
#~ dataDec = self.subdec(data)
with open('./.KirmahDEC', mode='w') as o: with open('./.KirmahDEC', mode='w') as o:
o.write(dataDec) o.write(dataDec)
rt.stop() rt.stop()
#~ dataDec = self.subdec(data)
return dataDec return dataDec
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -298,18 +298,21 @@ class ImapHelper:
def deleteBin(self): def deleteBin(self):
"""""" """"""
from impra.util import DEBUG_NOTICE, DEBUG, DEBUG_LEVEL
rt = RuTime(eval(__CALLER__())) rt = RuTime(eval(__CALLER__()))
self.srv.select(self.BOX_BIN) self.srv.select(self.BOX_BIN)
ids = self.search('ALL',True) ids = self.search('ALL',True)
if len(ids) > 0 and ids[0]!='' and ids[0]!=None: if len(ids) > 0 and ids[0]!='' and ids[0]!=None:
#print(str(ids[0],'utf-8').split()) #print(str(ids[0],'utf-8').split())
for mid in ids: for mid in ids:
print('deleting msg '+str(mid))
#~ uid = bytes(mid) #~ uid = bytes(mid)
#~ print(type(mid)) #~ print(type(mid))
#~ print(mid) #~ print(mid)
#status, resp = self.srv.store(mid, '+FLAGS', '\\Deleted') #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' )
if DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE:
print('deleting msg '+str(mid))
print(status) print(status)
print(resp) print(resp)
self.srv.expunge() self.srv.expunge()
@ -331,12 +334,13 @@ class ImapHelper:
def downloadAttachment(self, msg, toDir='./'): def downloadAttachment(self, msg, toDir='./'):
"""""" """"""
rt = RuTime(eval(__CALLER__('%i' % int(msg)))) 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) : if not isinstance(msg, Message) :
msg = self.email(msg) msg = self.email(msg)
for part in msg.walk(): for part in msg.walk():
filename = part.get_filename() filename = part.get_filename()
if filename != None : print(filename) if filename != None and DEBUG and DEBUG_LEVEL <= DEBUG_NOTICE : print(filename)
if part.get_content_maintype() == 'multipart' or not filename : continue if part.get_content_maintype() == 'multipart' or not filename : continue
fp = open(join(toDir, filename), 'wb') fp = open(join(toDir, filename), 'wb')
#print(part.get_payload(decode=True)[::-1]) #print(part.get_payload(decode=True)[::-1])
@ -346,7 +350,8 @@ class ImapHelper:
def send(self, msg, box='INBOX'): def send(self, msg, box='INBOX'):
"""""" """"""
rt = RuTime(eval(__CALLER__())) from impra.util import DEBUG_INFO
rt = RuTime(eval(__CALLER__()),DEBUG_INFO)
mid = None mid = None
date = Time2Internaldate(time()) date = Time2Internaldate(time())
status, resp = self.srv.append(box, '\Draft', date, bytes(msg,'utf-8')) status, resp = self.srv.append(box, '\Draft', date, bytes(msg,'utf-8'))

View File

@ -41,7 +41,13 @@ from inspect import stack
from subprocess import PIPE, Popen from subprocess import PIPE, Popen
from sys import stderr, executable as pyexec from sys import stderr, executable as pyexec
DEBUG_ALL = 0
DEBUG_WARN = 1
DEBUG_NOTICE = 2
DEBUG_INFO = 3
DEBUG = True
DEBUG_LEVEL = DEBUG_INFO
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ methods ~~ # ~~ methods ~~
@ -150,92 +156,42 @@ def __CALLER__(args=''):
:Returns: `str` :Returns: `str`
""" """
global DEBUG_LEVEL, DEBUG, DEBUG_WARN
#~ print(inspect.stack()[1][3]) #~ print(inspect.stack()[1][3])
#~ print(print(args)) #~ print(print(args))
#~ print('-----') #~ print('-----')
#~ print(inspect.stack()) #~ print(inspect.stack())
#~ print('---------------') #~ print('---------------')
val = "self.__class__.__name__+'.%s' % stack()[1][3]+'("+quote_escape(args)+") l:'+str(stack()[1][2])" val = "self.__class__.__name__+'.%s' % stack()[1][3]+'("+quote_escape(args)+") "
if DEBUG and DEBUG_LEVEL<=DEBUG_WARN : val += "l:'+str(stack()[1][2])"
else: val += "'"
return val return val
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class Noiser ~~
class Noiser:
""""""
KEY_LEN = 256
""""""
def __init__(self, key, part=0):
""""""
#~ if len(key)!=self.KEY_LEN :
#~ raise Exception('Invalid Pass length')
#~ else :
self.key = key
self.build(part)
def build(self, part):
""""""
if not part < self.KEY_LEN-1 : raise Exception('part exceed limit')
else :
self.part, v = part, 0
v = int(ceil((self.key[22]+v)/4.20583))
self.lns = int(ceil(v/2))-self.key[self.part]+self.key[7]
self.lne = int(v-self.lns-self.key[self.part+2]-self.key[44]/2.1934)
def getNoise(self, l):
""""""
return urandom(l)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class Randomiz ~~
class Randomiz:
""""""
def __init__(self,count,chl=None):
""""""
if chl ==None : self.lst = list(range(0,count))
else: self.lst = chl
self.count = len(self.lst)
def new(self,count=None, chl=None):
""""""
if count : self.count = count
self.__init__(self.count,chl)
def get(self,single=True):
""""""
pos = choice(self.lst)
if single: del self.lst[self.lst.index(pos)]
return pos
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class RuTime ~~ # ~~ class RuTime ~~
class RuTime: class RuTime:
"""Give basics time stats""" """Give basics time stats"""
def __init__(self,label): def __init__(self,label,lvl=DEBUG_NOTICE):
"""Initialize duration with appropriate label""" """Initialize duration with appropriate label"""
from impra.util import DEBUG, DEBUG_LEVEL, DEBUG_INFO
self.debug = DEBUG and DEBUG_LEVEL <= lvl
self.debugStart = self.debug and lvl < DEBUG_INFO
self.lvl = lvl
self.label = label self.label = label
self._start() self._start()
def _start(self): def _start(self):
from impra.core import DEBUG
if DEBUG :print(' ==> '+self.label) if self.debugStart :print(' ==> '+self.label)
self.sc = time() self.sc = time()
def stop(self): def stop(self):
"""Stop duration and print basics stats duration on console""" """Stop duration and print basics stats duration on console"""
from impra.core import DEBUG
self.ec = time() self.ec = time()
if DEBUG:self._stats() if self.debug: self._stats()
def _stats(self): def _stats(self):
print(' <== '+self.label+(' [%.9f s]' % (self.ec - self.sc))) print(' <== '+self.label+(' [%.9f s]' % (self.ec - self.sc)))
@ -340,68 +296,6 @@ class IniFile:
class BadKeysException(BaseException): class BadKeysException(BaseException):
pass pass
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class Rsa ~~
class Rsa:
""""""
def __init__(self, prvKey=None, pubKey=None, dpath='./', forceKeyGen=False):
""""""
self.cpath = join(dirname(dirname(realpath(__file__))),'desurveil','scripts')+sep
self.prvKey = prvKey
self.pubKey = pubKey
self.dpath = realpath(dpath)+sep
if prvKey == None or pubKey==None : self.key(forceKeyGen)
def key(self,force=False):
""""""
cmd = self.cpath+'desurveil key -a '+self.dpath+'.impra_id_rsa -l '+self.dpath+'.impra_id_rsa.pub'
try :
with open(self.dpath+'.impra_id_rsa','rt') as f: pass
if force:d = popen(cmd).read()
except IOError as e:
d = popen(pyexec+' '+cmd).read()
#print(pyexec+' '+cmd)
self.prvKey = get_file_content(self.dpath+'.impra_id_rsa')
self.pubKey = get_file_content(self.dpath+'.impra_id_rsa.pub')
#~ print('pubKey : \n'+self.pubKey)
#~ print('prvKey : \n'+self.prvKey)
def encrypt(self,data):
""""""
key = ''
#if self.pubKey != None : key = ' -CI "'+self.pubKey+'"'
if self.pubKey != None : key = ' -C "'+self.dpath+'.impra_id_rsa.pub"'
with open(self.dpath+'.tmpdecd', mode='w', encoding='utf-8') as o:
o.write(data)
cmd = self.cpath+'desurveil encrypt "'+self.dpath+'.tmpdecd'+'" '+key
#print(pyexec+' '+cmd)
rs = run(pyexec+' '+cmd)
if rs[0]==1:
print(rs)
raise BadKeysException('bad key to encrypt')
else :
encData = str(rs[1],'utf-8')
return encData
def decrypt(self,data):
""""""
key = ''
#if self.prvKey != None : key = ' -CI "'+self.prvKey+'"'
if self.prvKey != None : key = ' -C "'+self.dpath+'.impra_id_rsa"'
with open(self.dpath+'.tmpencd', mode='w', encoding='utf-8') as o:
o.write(data)
cmd = self.cpath+'desurveil decrypt "'+self.dpath+'.tmpencd'+'" '+key
#print(pyexec+' '+cmd)
rs = run(pyexec+' '+cmd)
if rs[0]==1:
print(rs)
raise BadKeysException('bad key to decrypt')
else :
decData = str(rs[1],'utf-8')
return decData
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class StrIterator ~~ # ~~ class StrIterator ~~

View File

@ -27,44 +27,12 @@
# along with ImpraStorage. If not, see <http://www.gnu.org/licenses/>. # along with ImpraStorage. If not, see <http://www.gnu.org/licenses/>.
from impra.core import ImpraConf, ImpraStorage from impra.core import ImpraConf, ImpraStorage
from impra.util import IniFile, Rsa, RuTime, get_file_path from impra.util import IniFile, RuTime, get_file_path
from impra.cli import Cli from impra.cli import Cli
if __name__ == '__main__': if __name__ == '__main__':
Cli(get_file_path(__file__ )) Cli(get_file_path(__file__ ))
#~ rt = RuTime(__name__+'()')
#~ conf = ImpraConf(IniFile('./impra.ini'))
#~ rsa = Rsa(conf.ini.get('prvKey',conf.profile+'.keys'),conf.ini.get('pubKey',conf.profile+'.keys'))
#~ impst = ImpraStorage(rsa, conf)
#~
#~ print('\n -- INDEX DATA -- ')
#~ impst.index.print()
#~ print('-- LIST BOX --')
#~ lb = impst.ih.listBox('/')
#~ print(lb)
#~
#~ #print('-- DELETE BIN --')
#~ #impst.ih.deleteBin()
#~
#~ filePath = '/media/Data/dev/big_toph3.jpg'
#~
#~ lab = 'Meuf\'bonne aussi4'
#~
#~ print('\n -- ADD FILE -- ')
#~ impst.addFile(filePath,lab,conf.ini.get('name',conf.profile+'.infos'),'images')
#~
#~ print('\n -- GET FILE -- ')
#~ impst.getFile(lab)
#~
#~ print('\n -- INDEX DATA -- ')
#~ impst.index.print()
#~
#~ print('\n -- CLEAN -- ')
#~ impst.clean()
#~
#~ rt.stop()
#python -O -m compileall impra/*.py #python -O -m compileall impra/*.py