[Nym3-commit] r463 - trunk/nymbaron/Client
laurent at conuropsis.org
laurent at conuropsis.org
Wed Mar 8 21:57:07 CET 2006
Author: laurent
Date: 2006-03-08 21:57:04 +0100 (Wed, 08 Mar 2006)
New Revision: 463
Modified:
trunk/nymbaron/Client/Account.py
Log:
One exciting indentation-only commit
Modified: trunk/nymbaron/Client/Account.py
===================================================================
--- trunk/nymbaron/Client/Account.py 2006-03-08 20:44:06 UTC (rev 462)
+++ trunk/nymbaron/Client/Account.py 2006-03-08 20:57:04 UTC (rev 463)
@@ -57,24 +57,24 @@
(ie a tuple (dict, seq)."""
#Add to the hash
if h.has_key(mid):
- #TODO what should be done here?
- raise Exception("Bug mid not unique")
+ #TODO what should be done here?
+ raise Exception("Bug mid not unique")
h[mid] = (xnymseq, flag, syn)
#Add to the list
#if sorted it is quick to insert from the end
if sorted:
- i = len(l)
- while i > 0:
- if l[i-1][0] <= xnymseq:
- l[i:i] = [mid]
- return
- i = i - 1
- l[0:0] = [mid]
+ i = len(l)
+ while i > 0:
+ if l[i-1][0] <= xnymseq:
+ l[i:i] = [mid]
+ return
+ i = i - 1
+ l[0:0] = [mid]
#else we use a binary search
else:
- #look for the first mid whose seq number is greater than xnymseq
- i = binsearch(l, lambda x: (h[x][0] > xnymseq))
- l[i:i] = [mid]
+ #look for the first mid whose seq number is greater than xnymseq
+ i = binsearch(l, lambda x: (h[x][0] > xnymseq))
+ l[i:i] = [mid]
class NoSuchAccount(Exception): pass
"""Exception thrown when a user identified by her nym can't be found"""
@@ -85,524 +85,524 @@
class TagMap:
"""Contains mapping between idTag and nickname"""
def __init__(self, filename):
- self.lock = None
- self.id2nick = None
- self.nick2id = None
- self.filename = filename
+ self.lock = None
+ self.id2nick = None
+ self.nick2id = None
+ self.filename = filename
def __del__(self):
- if self.lock: self._release()
+ if self.lock: self._release()
def _lock(self):
"""Lock the tagmap file"""
self.lock = mixminion.Common.Lockfile(self.filename + '.lck')
- nbtries = 5
- while True:
- try:
- self.lock.acquire(blocking = 0)
- break
- except:
- import time
- time.sleep(2)
- nbtries = nbtries - 1
- if nbtries == 0: raise Exception("Unable to acquire lock")
+ nbtries = 5
+ while True:
+ try:
+ self.lock.acquire(blocking = 0)
+ break
+ except:
+ import time
+ time.sleep(2)
+ nbtries = nbtries - 1
+ if nbtries == 0: raise Exception("Unable to acquire lock")
def _release(self):
self.lock.release()
- self.lock = None
+ self.lock = None
def _tagfile(self):
return self.filename
def _load(self):
- tag = self._tagfile()
- try:
- f = open(tag, 'r')
- self.id2nick = pickle.load(f)
- f.close()
- except IOError:
- self.id2nick = {}
- self.nick2id = {}
- for i in self.id2nick.keys():
- self.nick2id[self.id2nick[i]] = i
+ tag = self._tagfile()
+ try:
+ f = open(tag, 'r')
+ self.id2nick = pickle.load(f)
+ f.close()
+ except IOError:
+ self.id2nick = {}
+ self.nick2id = {}
+ for i in self.id2nick.keys():
+ self.nick2id[self.id2nick[i]] = i
def _loadifneeded(self):
- if self.id2nick == None:
- self._lock()
- self._load()
- self._release()
+ if self.id2nick == None:
+ self._lock()
+ self._load()
+ self._release()
def _save(self):
if self.id2nick == None: return
- tagmap = self._tagfile()
- f = open(tagmap, 'w')
- pickle.dump(self.id2nick, f)
- f.close()
+ tagmap = self._tagfile()
+ f = open(tagmap, 'w')
+ pickle.dump(self.id2nick, f)
+ f.close()
def nickFromId(self, idTag):
self._loadifneeded()
- try:
- return self.id2nick[idTag]
- except KeyError:
- raise NoSuchAccount()
+ try:
+ return self.id2nick[idTag]
+ except KeyError:
+ raise NoSuchAccount()
def idFromNick(self, nick):
self._loadifneeded()
- try:
- return self.nick2id[nick]
- except KeyError:
- raise NoSuchAccount()
+ try:
+ return self.nick2id[nick]
+ except KeyError:
+ raise NoSuchAccount()
def getnewId(self, nick):
- self._lock()
- self._load()
- if self.nick2id.has_key(nick):
- self._release()
- raise AlreadySuchAccount()
- while True:
- tag = Mail.genMid(8)
- rtag = Mail.mid2filename(tag).lower()
- if not self.id2nick.has_key(rtag): break
- self.nick2id[nick] = rtag
- self.id2nick[rtag] = nick
- self._save()
- self._release()
- return rtag
+ self._lock()
+ self._load()
+ if self.nick2id.has_key(nick):
+ self._release()
+ raise AlreadySuchAccount()
+ while True:
+ tag = Mail.genMid(8)
+ rtag = Mail.mid2filename(tag).lower()
+ if not self.id2nick.has_key(rtag): break
+ self.nick2id[nick] = rtag
+ self.id2nick[rtag] = nick
+ self._save()
+ self._release()
+ return rtag
def getNicks(self):
- self._loadifneeded()
- return self.nick2id.keys()
+ self._loadifneeded()
+ return self.nick2id.keys()
def remove(self, idTag):
- self._loadifneeded()
- self._lock()
- if self.id2nick.has_key(idTag):
- nick = self.id2nick[idTag]
- del self.id2nick[idTag]
- del self.nick2id[nick]
- self._save()
- self._release()
-
+ self._loadifneeded()
+ self._lock()
+ if self.id2nick.has_key(idTag):
+ nick = self.id2nick[idTag]
+ del self.id2nick[idTag]
+ del self.nick2id[nick]
+ self._save()
+ self._release()
+
class Account:
"""Hold account data. Specifically, this means:
- - idTag is the identity string used to generate SURBs for this
- account. It is therefore unique to this account. Additionnaly
- it is used to name the subdirectory holding this account's data
- - 'handshake' tells how far we are in the account creation handshake,
- it is one of 'initiated', 'got_challenge', 'answered_challenge' or
- 'completed'.
- - misc parameters that needs further documentation.
- - data is the hash that contains all of this
- - datafile() is the file where we pickle from and to the data hash
- - synbox is the synopsis box, stored in synboxfile()
- It was a hashtable that associates to a XNymSeq a t-uple
- (mid, flag_is_present_on server, synopsis)
- it may be represented by a string : the encrypted pickled hashtable
- (status = 'encrypted') or a hashtable (status = decrypted).
- When saved, it is put in the encrypted form.
- Due to the meaning of the mid the previous structure is deprecated and
- replaced by a tuple (dic, seq) where
- - dic is a hashtable mid -> (xnymseq, flag_is_present_on_server,
- synopsis). In the new format xnymseq is an int whereas it was a str in
- the old format
- - seq is a list of dic.keys(), sorted by ascending xnymseq
- - mbox is the couple of a hash mid to message and a list of the keys of
- the hash to order these keys, stored in mboxfile()
- - a journal which is a hash of seqno -> encrypted (message, time sent) and is pickled from/to journalfile()
- - the keys used by the account. The actual keys are stored in the
- Keyring, we only store the handles in the account. An account can
- have at most two identity keys at the same time, one active and one
- pending acknowledgment. The active key is named idKey (string), and
- the pending one is name pendingKey (string * seqno) and holds the
- sequence number of the message whose ack by the server would turn
- into the main idKey. The number of encryption keys is not limited,
- they are stored in the encKeys list, last generated first."""
+ - idTag is the identity string used to generate SURBs for this
+ account. It is therefore unique to this account. Additionnaly
+ it is used to name the subdirectory holding this account's data
+ - 'handshake' tells how far we are in the account creation handshake,
+ it is one of 'initiated', 'got_challenge', 'answered_challenge' or
+ 'completed'.
+ - misc parameters that needs further documentation.
+ - data is the hash that contains all of this
+ - datafile() is the file where we pickle from and to the data hash
+ - synbox is the synopsis box, stored in synboxfile()
+ It was a hashtable that associates to a XNymSeq a t-uple
+ (mid, flag_is_present_on server, synopsis)
+ it may be represented by a string : the encrypted pickled hashtable
+ (status = 'encrypted') or a hashtable (status = decrypted).
+ When saved, it is put in the encrypted form.
+ Due to the meaning of the mid the previous structure is deprecated and
+ replaced by a tuple (dic, seq) where
+ - dic is a hashtable mid -> (xnymseq, flag_is_present_on_server,
+ synopsis). In the new format xnymseq is an int whereas it was a str in
+ the old format
+ - seq is a list of dic.keys(), sorted by ascending xnymseq
+ - mbox is the couple of a hash mid to message and a list of the keys of
+ the hash to order these keys, stored in mboxfile()
+ - a journal which is a hash of seqno -> encrypted (message, time sent) and is pickled from/to journalfile()
+ - the keys used by the account. The actual keys are stored in the
+ Keyring, we only store the handles in the account. An account can
+ have at most two identity keys at the same time, one active and one
+ pending acknowledgment. The active key is named idKey (string), and
+ the pending one is name pendingKey (string * seqno) and holds the
+ sequence number of the message whose ack by the server would turn
+ into the main idKey. The number of encryption keys is not limited,
+ they are stored in the encKeys list, last generated first."""
def __init__(self, config, nickname, create = False):
- """Load from an existing account, or create a new, or fail"""
- # Some data related to the account are loaded only on demand,
- # this includes the journal, the mailbox, the synbox.
- self.succeeded = True
- self.config = config
- # If home directory does not exist, create it:
- try:
- os.stat(config.path)
- except OSError:
- os.makedirs(config.path)
- self.tagmap = TagMap(config.path + os.sep + 'tagmap')
- if create:
- self.data = {}
- self.succeeded = False
- try:
- self.idTag = self.tagmap.getnewId(nickname)
- except AlreadySuchAccount:
- self.succeeded = True #don't erase the existing account
- self.data_status = 'unloaded'
- self.journal_status = 'unloaded'
- self.mbox_status = 'unloaded'
- self.synbox_status = 'unloaded'
- raise
- self._lock()
- # If the nick already existed, we're out of this because of
- # a thrown AlreadySuchAccount.
- # Create the dir that holds this account data
- self.base_path = config.path + os.sep + self.idTag
- os.mkdir(self.base_path)
- self.data_status = 'dirty'
- self.journal = {}
- self.journal_status = 'dirty'
- self.mbox = {}, []
- self.mbox_status = 'dirty'
- self.synbox = {}, []
- self.synbox_status = 'dirty'
- self.synbox_enc_status = 'decrypted'
- self['encKeys'] = []
- self.idKey = None
- self.admKey = None
- self.pendingKey = None
- else:
- self.data_status = 'unloaded'
- self.journal_status = 'unloaded'
- self.mbox_status = 'unloaded'
- self.synbox_status = 'unloaded'
- self.idTag = self.tagmap.idFromNick(nickname)
- self._lock()
- self.base_path = config.path + os.sep + self.idTag
- self._load_data()
- self.journal = None
- self.mbox = None
- self.synbox = None
- self.synbox_enc_status = None
+ """Load from an existing account, or create a new, or fail"""
+ # Some data related to the account are loaded only on demand,
+ # this includes the journal, the mailbox, the synbox.
+ self.succeeded = True
+ self.config = config
+ # If home directory does not exist, create it:
+ try:
+ os.stat(config.path)
+ except OSError:
+ os.makedirs(config.path)
+ self.tagmap = TagMap(config.path + os.sep + 'tagmap')
+ if create:
+ self.data = {}
+ self.succeeded = False
+ try:
+ self.idTag = self.tagmap.getnewId(nickname)
+ except AlreadySuchAccount:
+ self.succeeded = True #don't erase the existing account
+ self.data_status = 'unloaded'
+ self.journal_status = 'unloaded'
+ self.mbox_status = 'unloaded'
+ self.synbox_status = 'unloaded'
+ raise
+ self._lock()
+ # If the nick already existed, we're out of this because of
+ # a thrown AlreadySuchAccount.
+ # Create the dir that holds this account data
+ self.base_path = config.path + os.sep + self.idTag
+ os.mkdir(self.base_path)
+ self.data_status = 'dirty'
+ self.journal = {}
+ self.journal_status = 'dirty'
+ self.mbox = {}, []
+ self.mbox_status = 'dirty'
+ self.synbox = {}, []
+ self.synbox_status = 'dirty'
+ self.synbox_enc_status = 'decrypted'
+ self['encKeys'] = []
+ self.idKey = None
+ self.admKey = None
+ self.pendingKey = None
+ else:
+ self.data_status = 'unloaded'
+ self.journal_status = 'unloaded'
+ self.mbox_status = 'unloaded'
+ self.synbox_status = 'unloaded'
+ self.idTag = self.tagmap.idFromNick(nickname)
+ self._lock()
+ self.base_path = config.path + os.sep + self.idTag
+ self._load_data()
+ self.journal = None
+ self.mbox = None
+ self.synbox = None
+ self.synbox_enc_status = None
def creationSuccess(self):
- """Tells the system that this account is successfully initialized. Call
- this method once the account is successfully setup."""
- self.succeeded = True
+ """Tells the system that this account is successfully initialized. Call
+ this method once the account is successfully setup."""
+ self.succeeded = True
def generateSurbs(self, n):
"""Generate surbs"""
- # TODO : this should be done via ClientAPI.
- if n == 0:
- return ""
- surbdir = tempfile.mkdtemp()
- surbspath = surbdir + os.sep + "surbs"
- mixcall = "mixminion generate-surb -t %s -b -n %d -o %s --identity='%s'"
- mixcall = mixcall % (self['returnaddress'], n, surbspath, self.idTag)
- os.system(mixcall)
- f = open(surbspath, "r")
- data = f.read()
- f.close()
- os.unlink(surbspath)
- os.rmdir(surbdir)
- return data
+ # TODO : this should be done via ClientAPI.
+ if n == 0:
+ return ""
+ surbdir = tempfile.mkdtemp()
+ surbspath = surbdir + os.sep + "surbs"
+ mixcall = "mixminion generate-surb -t %s -b -n %d -o %s --identity='%s'"
+ mixcall = mixcall % (self['returnaddress'], n, surbspath, self.idTag)
+ os.system(mixcall)
+ f = open(surbspath, "r")
+ data = f.read()
+ f.close()
+ os.unlink(surbspath)
+ os.rmdir(surbdir)
+ return data
def __getitem__(self, key):
- """Gets user account field as in a hashtable"""
- if self.data_status == 'unloaded': self._load_data()
- return self.data[key]
+ """Gets user account field as in a hashtable"""
+ if self.data_status == 'unloaded': self._load_data()
+ return self.data[key]
def __setitem__(self, key, value):
- """Gets user account field as in a hashtable"""
- self.data[key] = value
- self.data_status = 'dirty'
+ """Gets user account field as in a hashtable"""
+ self.data[key] = value
+ self.data_status = 'dirty'
def send(self, body):
- # TODO : this should be done via ClientAPI.
- (f, msgpath) = tempfile.mkstemp()
- os.close(f)
- f = open(msgpath, "w")
- f.write(body)
- f.close()
- mixcall = "mixminion send -t %s -i %s" % \
- (self['servername'], msgpath)
- if self.config.online:
- os.system(mixcall)
- if self.config.DEBUG:
- print "Raw control message left in " + msgpath
- else:
- os.unlink(msgpath)
+ # TODO : this should be done via ClientAPI.
+ (f, msgpath) = tempfile.mkstemp()
+ os.close(f)
+ f = open(msgpath, "w")
+ f.write(body)
+ f.close()
+ mixcall = "mixminion send -t %s -i %s" % \
+ (self['servername'], msgpath)
+ if self.config.online:
+ os.system(mixcall)
+ if self.config.DEBUG:
+ print "Raw control message left in " + msgpath
+ else:
+ os.unlink(msgpath)
def mboxfile(self):
- """Gets the path of the mailbox"""
- return self.base_path + os.sep + 'mbox'
+ """Gets the path of the mailbox"""
+ return self.base_path + os.sep + 'mbox'
def datafile(self):
- """Gets the path of the datafile"""
- return self.base_path + os.sep + 'data'
+ """Gets the path of the datafile"""
+ return self.base_path + os.sep + 'data'
def synboxfile(self):
- """Gets the path of the synbox"""
- return self.base_path + os.sep + 'synbox'
+ """Gets the path of the synbox"""
+ return self.base_path + os.sep + 'synbox'
def journalfile(self):
- """Gets the path of the journal"""
- return self.base_path + os.sep + 'journal'
+ """Gets the path of the journal"""
+ return self.base_path + os.sep + 'journal'
def _lock(self):
- """Locks the user. For well behaved functions."""
- self.lock = mixminion.Common.Lockfile(self.config.path + os.sep + \
- self.idTag + '.lck')
- nbtries = 5
- while True:
- try:
- self.lock.acquire(blocking = 0)
- break
- except:
- import time
- time.sleep(2)
- nbtries = nbtries - 1
- if nbtries == 0: raise Exception("Unable to acquire lock")
+ """Locks the user. For well behaved functions."""
+ self.lock = mixminion.Common.Lockfile(self.config.path + os.sep + \
+ self.idTag + '.lck')
+ nbtries = 5
+ while True:
+ try:
+ self.lock.acquire(blocking = 0)
+ break
+ except:
+ import time
+ time.sleep(2)
+ nbtries = nbtries - 1
+ if nbtries == 0: raise Exception("Unable to acquire lock")
def _release(self):
- """Releases the lock"""
- if hasattr(self, 'lock'):
- self.lock.release()
- self.lock = None
+ """Releases the lock"""
+ if hasattr(self, 'lock'):
+ self.lock.release()
+ self.lock = None
def homeDir(self):
- """Gets the home directory of this account"""
+ """Gets the home directory of this account"""
return self.base_path
def sendControl(self, l, idKey, desc):
- """Sends a list of command messages to the nymserver"""
+ """Sends a list of command messages to the nymserver"""
msg = Message.buildMessage(l)
- seqno = self.get_seqno()
- #TODO debugging cruft
- print "send message: %s" % binascii.hexlify(seqno)
+ seqno = self.get_seqno()
+ #TODO debugging cruft
+ print "send message: %s" % binascii.hexlify(seqno)
hdr = self.buildHeader(msg, seqno, idKey)
- self.record(seqno, hdr + msg, desc)
- self.send(hdr + msg)
+ self.record(seqno, hdr + msg, desc)
+ self.send(hdr + msg)
def buildHeader(self, msg, seqno, idKey):
- """Generates a header for a message using the identification key"""
+ """Generates a header for a message using the identification key"""
h = Message.Header()
sig = _cr.pk_sign(_cr.sha1(chr(len(self['username'])) +
- self['username'] + seqno + msg), idKey)
+ self['username'] + seqno + msg), idKey)
h.fromData(self.data['username'], seqno, sig)
return str(h)
def get_admPubKey(self):
- pubring = Keyring.Keyring(self.config.pubring_path, create = False)
- pubring.decrypt("nymbaron")
- return pubring.get_key(self['admKey'])
+ pubring = Keyring.Keyring(self.config.pubring_path, create = False)
+ pubring.decrypt("nymbaron")
+ return pubring.get_key(self['admKey'])
def record(self, seqno, msg, desc):
- """Store a control message in the journal"""
- if self.journal_status == 'unloaded': self._load_journal()
- clear = pickle.dumps((msg, int(time.time()), desc))
- key = self.get_admPubKey()
- self.journal[seqno] = Crypto.nym_encrypt(clear, key)
- self.journal_status = 'dirty'
+ """Store a control message in the journal"""
+ if self.journal_status == 'unloaded': self._load_journal()
+ clear = pickle.dumps((msg, int(time.time()), desc))
+ key = self.get_admPubKey()
+ self.journal[seqno] = Crypto.nym_encrypt(clear, key)
+ self.journal_status = 'dirty'
def acknowledge(self, seqno_list, verbose = True):
- if seqno_list == None or seqno_list == []: return
- if self.journal_status == 'unloaded': self._load_journal()
- for el in seqno_list:
- if self.journal.has_key(el):
- del self.journal[el]
- #TODO debugging cruft
- if verbose:
- print "acknowledged message : %s" % binascii.hexlify(el)
- self.journal_status = 'dirty'
+ if seqno_list == None or seqno_list == []: return
+ if self.journal_status == 'unloaded': self._load_journal()
+ for el in seqno_list:
+ if self.journal.has_key(el):
+ del self.journal[el]
+ #TODO debugging cruft
+ if verbose:
+ print "acknowledged message : %s" % binascii.hexlify(el)
+ self.journal_status = 'dirty'
def get_seqno(self):
- """Return a previously unused sequence number"""
- if self.journal_status == 'unloaded': self._load_journal()
- ret = Mail.genMid(SEQNO_LEN)
- while self.journal.has_key(ret):
- ret = Mail.genMid(SEQNO_LEN)
- return ret
+ """Return a previously unused sequence number"""
+ if self.journal_status == 'unloaded': self._load_journal()
+ ret = Mail.genMid(SEQNO_LEN)
+ while self.journal.has_key(ret):
+ ret = Mail.genMid(SEQNO_LEN)
+ return ret
def _save_data(self):
- """Flushes the data to the disk"""
- if not self.data_status == 'dirty': return
- data = self.datafile()
- f = open(data, 'w')
- pickle.dump(self.data, f)
- f.close()
- self.data_status = 'ok'
+ """Flushes the data to the disk"""
+ if not self.data_status == 'dirty': return
+ data = self.datafile()
+ f = open(data, 'w')
+ pickle.dump(self.data, f)
+ f.close()
+ self.data_status = 'ok'
def _save_mbox(self):
- """Flushes the mbox to the disk"""
- if not self.mbox_status == 'dirty': return
- mbox = self.mboxfile()
- f = open(mbox, 'w')
- pickle.dump(self.mbox, f)
- f.close()
- self.mbox_status = 'ok'
+ """Flushes the mbox to the disk"""
+ if not self.mbox_status == 'dirty': return
+ mbox = self.mboxfile()
+ f = open(mbox, 'w')
+ pickle.dump(self.mbox, f)
+ f.close()
+ self.mbox_status = 'ok'
def _save_synbox(self):
- """Flushes the synbox to the disk"""
- if not self.synbox_status == 'dirty': return
- synbox = self.synboxfile()
- f = open(synbox, 'w')
- if self.synbox_enc_status == 'decrypted':
- key = self.get_admPubKey()
- s = Crypto.nym_encrypt(pickle.dumps(self.synbox), key)
- else:
- assert self.synbox_enc_status == 'encrypted'
- s = self.synbox
- f.write(s)
- f.close()
- self.synbox_status = 'ok'
+ """Flushes the synbox to the disk"""
+ if not self.synbox_status == 'dirty': return
+ synbox = self.synboxfile()
+ f = open(synbox, 'w')
+ if self.synbox_enc_status == 'decrypted':
+ key = self.get_admPubKey()
+ s = Crypto.nym_encrypt(pickle.dumps(self.synbox), key)
+ else:
+ assert self.synbox_enc_status == 'encrypted'
+ s = self.synbox
+ f.write(s)
+ f.close()
+ self.synbox_status = 'ok'
def _save_journal(self):
- """Flushes the journal to the disk"""
- if not self.journal_status == 'dirty': return
- journal = self.journalfile()
- f = open(journal, 'w')
- pickle.dump(self.journal, f)
- f.close()
- self.journal_status = 'ok'
+ """Flushes the journal to the disk"""
+ if not self.journal_status == 'dirty': return
+ journal = self.journalfile()
+ f = open(journal, 'w')
+ pickle.dump(self.journal, f)
+ f.close()
+ self.journal_status = 'ok'
def __del__(self):
- """Flushes the user account to the disk"""
- if self.succeeded:
- self._save_synbox()
- self._save_mbox()
- self._save_journal()
- self._save_data()
- else:
- self.tagmap.remove(self.idTag)
- for f in os.listdir(self.base_path):
- os.unlink(self.base_path + '/' + f)
- os.rmdir(self.base_path)
- self._release()
+ """Flushes the user account to the disk"""
+ if self.succeeded:
+ self._save_synbox()
+ self._save_mbox()
+ self._save_journal()
+ self._save_data()
+ else:
+ self.tagmap.remove(self.idTag)
+ for f in os.listdir(self.base_path):
+ os.unlink(self.base_path + '/' + f)
+ os.rmdir(self.base_path)
+ self._release()
def _load_mbox(self):
- """Loads the mailbox from the disk"""
- if not self.mbox_status == 'unloaded': return
- mbox = self.mboxfile()
- try:
- f = open(mbox, 'r')
- self.mbox = pickle.load(f)
- f.close()
- except IOError:
- self.mbox = {}
- self.mbox_status = 'ok'
+ """Loads the mailbox from the disk"""
+ if not self.mbox_status == 'unloaded': return
+ mbox = self.mboxfile()
+ try:
+ f = open(mbox, 'r')
+ self.mbox = pickle.load(f)
+ f.close()
+ except IOError:
+ self.mbox = {}
+ self.mbox_status = 'ok'
def _load_data(self):
- """Loads the datafile from the disk"""
- if not self.data_status == 'unloaded': return
- datafile = self.datafile()
- try:
- f = open(datafile, 'r')
- self.data = pickle.load(f)
- f.close()
- except IOError:
- self.data = {}
- self.data_status = 'ok'
+ """Loads the datafile from the disk"""
+ if not self.data_status == 'unloaded': return
+ datafile = self.datafile()
+ try:
+ f = open(datafile, 'r')
+ self.data = pickle.load(f)
+ f.close()
+ except IOError:
+ self.data = {}
+ self.data_status = 'ok'
def _load_synbox(self):
- """Loads the synbox from the disk"""
- if not self.synbox_status == 'unloaded': return
- synbox = self.synboxfile()
- try:
- f = open(synbox, 'r')
- self.synbox = f.read()
- self.synbox_enc_status = 'encrypted'
- f.close()
- except IOError:
- self.synbox = {}, []
- self.synbox_enc_status = 'decrypted'
- self.synbox_status = 'ok'
+ """Loads the synbox from the disk"""
+ if not self.synbox_status == 'unloaded': return
+ synbox = self.synboxfile()
+ try:
+ f = open(synbox, 'r')
+ self.synbox = f.read()
+ self.synbox_enc_status = 'encrypted'
+ f.close()
+ except IOError:
+ self.synbox = {}, []
+ self.synbox_enc_status = 'decrypted'
+ self.synbox_status = 'ok'
def _load_journal(self):
- """Loads the journal from the disk"""
- if not self.journal_status == 'unloaded': return
- journal = self.journalfile()
- try:
- f = open(journal, 'r')
- self.journal = pickle.load(f)
- f.close()
- except IOError:
- self.journal = {}
- self.journal_status = 'ok'
+ """Loads the journal from the disk"""
+ if not self.journal_status == 'unloaded': return
+ journal = self.journalfile()
+ try:
+ f = open(journal, 'r')
+ self.journal = pickle.load(f)
+ f.close()
+ except IOError:
+ self.journal = {}
+ self.journal_status = 'ok'
def _decrypt_synbox(self, secring):
- """After a call to this method synbox is loaded and decrypted.
- secring contains the private admKey"""
- self._load_synbox()
- if self.synbox_enc_status == 'decrypted': return
- assert self.synbox_enc_status == 'encrypted'
- key = secring.get_key(self['admKey'])
- self.synbox = pickle.loads(Crypto.nym_decrypt(self.synbox, key))
- self.synbox_enc_status = 'decrypted'
- #TODO the following code converts a synbox in the old format (dictionary
- #to a synbox in the new format. Remove once we can assume the old format
- #isn't used anymore.
- if type(self.synbox) == dict:
- synbox = {}, []
- xnymseqs = self.synbox.keys()
- xnymseqs = map(int, xnymseqs)
- xnymseqs.sort()
- for xnymseq in xnymseqs:
- (mid, flag, syn) = self.synbox[str(xnymseq)]
- add_syn_to_synbox(synbox[0], synbox[1], xnymseq,
- mid, flag, syn, True)
- self.synbox = synbox
- self.synbox_status = 'dirty'
-
+ """After a call to this method synbox is loaded and decrypted.
+ secring contains the private admKey"""
+ self._load_synbox()
+ if self.synbox_enc_status == 'decrypted': return
+ assert self.synbox_enc_status == 'encrypted'
+ key = secring.get_key(self['admKey'])
+ self.synbox = pickle.loads(Crypto.nym_decrypt(self.synbox, key))
+ self.synbox_enc_status = 'decrypted'
+ #TODO the following code converts a synbox in the old format (dictionary
+ #to a synbox in the new format. Remove once we can assume the old format
+ #isn't used anymore.
+ if type(self.synbox) == dict:
+ synbox = {}, []
+ xnymseqs = self.synbox.keys()
+ xnymseqs = map(int, xnymseqs)
+ xnymseqs.sort()
+ for xnymseq in xnymseqs:
+ (mid, flag, syn) = self.synbox[str(xnymseq)]
+ add_syn_to_synbox(synbox[0], synbox[1], xnymseq,
+ mid, flag, syn, True)
+ self.synbox = synbox
+ self.synbox_status = 'dirty'
+
def get_synbox(self, secring):
- """return a copy of the synbox, trying to decrypt it if it is encrypted
- """
- self._decrypt_synbox(secring)
- return copy.deepcopy(self.synbox)
+ """return a copy of the synbox, trying to decrypt it if it is encrypted
+ """
+ self._decrypt_synbox(secring)
+ return copy.deepcopy(self.synbox)
def get_mbox(self):
- """return a copy of the mbox"""
- self._load_mbox()
- return copy.deepcopy(self.mbox)
+ """return a copy of the mbox"""
+ self._load_mbox()
+ return copy.deepcopy(self.mbox)
def get_journal(self):
- """return a copy of the journal"""
- self._load_journal()
- return copy.deepcopy(self.journal)
+ """return a copy of the journal"""
+ self._load_journal()
+ return copy.deepcopy(self.journal)
def add_syn(self, secring, xnymseq, mid, flag, syn):
- """add a syn to the synbox. The secring is used only if the synbox is
- encrypted."""
- self._decrypt_synbox(secring)
- add_syn_to_synbox(self.synbox[0], self.synbox[1], xnymseq, mid, flag,
- syn)
- self.synbox_status = "dirty"
+ """add a syn to the synbox. The secring is used only if the synbox is
+ encrypted."""
+ self._decrypt_synbox(secring)
+ add_syn_to_synbox(self.synbox[0], self.synbox[1], xnymseq, mid, flag,
+ syn)
+ self.synbox_status = "dirty"
def delete_syn(self, secring, mid):
- self._decrypt_synbox(secring)
- h, l = self.synbox
- if h.has_key(mid):
- i = binsearch(l, lambda x: (x[0] >= h[mid][0]))
- while h[l[i]][0] == h[mid][0]:
- if l[i] == mid:
- del(self.synbox[1][i])
- del(self.synbox[0][mid])
- self.synbox_status = "dirty"
- return
- raise Error("Bug : mid in the synbox hash but not sequence")
- #TODO cruft -> output a warning here
- print "mid %s not in the synbox, doing nothing" % binascii.hexlify(mid)
-
+ self._decrypt_synbox(secring)
+ h, l = self.synbox
+ if h.has_key(mid):
+ i = binsearch(l, lambda x: (x[0] >= h[mid][0]))
+ while h[l[i]][0] == h[mid][0]:
+ if l[i] == mid:
+ del(self.synbox[1][i])
+ del(self.synbox[0][mid])
+ self.synbox_status = "dirty"
+ return
+ raise Error("Bug : mid in the synbox hash but not sequence")
+ #TODO cruft -> output a warning here
+ print "mid %s not in the synbox, doing nothing" % binascii.hexlify(mid)
+
def add_msg(self, mid, msg):
- self._load_mbox()
- #this ensures there is no more than one occurence of a mid in
- #the list
- if self.mbox[0].has_key(mid):
- self.mbox[1].remove(mid)
- self.mbox[0][mid] = msg
- self.mbox[1].append(mid)
- self.mbox_status = "dirty"
+ self._load_mbox()
+ #this ensures there is no more than one occurence of a mid in
+ #the list
+ if self.mbox[0].has_key(mid):
+ self.mbox[1].remove(mid)
+ self.mbox[0][mid] = msg
+ self.mbox[1].append(mid)
+ self.mbox_status = "dirty"
def delete_msg(self, mid):
- self._load_mbox()
- if self.mbox[0].has_key(mid):
- del(self.mbox[0][mid])
- self.mbox[1].remove(mid)
- self.mbox_status = "dirty"
+ self._load_mbox()
+ if self.mbox[0].has_key(mid):
+ del(self.mbox[0][mid])
+ self.mbox[1].remove(mid)
+ self.mbox_status = "dirty"
def add_enckey(self, key):
- self['encKeys'].insert(0, key)
+ self['encKeys'].insert(0, key)
def removeAccount(self):
- self.succeeded = False
+ self.succeeded = False
More information about the Nym3-commit
mailing list