4 ## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
5 ## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
7 ## This file is part of Gajim.
9 ## Gajim is free software; you can redistribute it and/or modify
10 ## it under the terms of the GNU General Public License as published
11 ## by the Free Software Foundation; version 3 only.
13 ## Gajim is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
18 ## You should have received a copy of the GNU General Public License
19 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
22 from common
.configpaths
import gajimpaths
25 from common
import crypto
26 from common
import exceptions
31 secrets_filename
= gajimpaths
['SECRETS_FILE']
35 def __init__(self
, filename
):
36 self
.filename
= filename
42 raise exceptions
.Cancelled
45 f
= open(secrets_filename
, 'w')
49 def retained_secrets(self
, account
, bare_jid
):
51 return self
.srs
[account
][bare_jid
]
55 # retained secrets are stored as a tuple of the secret and whether the user
57 def save_new_srs(self
, account
, jid
, secret
, verified
):
58 if not account
in self
.srs
:
59 self
.srs
[account
] = {}
61 if not jid
in self
.srs
[account
]:
62 self
.srs
[account
][jid
] = []
64 self
.srs
[account
][jid
].append((secret
, verified
))
68 def find_srs(self
, account
, jid
, srs
):
69 our_secrets
= self
.srs
[account
][jid
]
70 return [(x
, y
) for x
, y
in our_secrets
if x
== srs
][0]
72 # has the user verified this retained secret?
73 def srs_verified(self
, account
, jid
, srs
):
74 return self
.find_srs(account
, jid
, srs
)[1]
76 def replace_srs(self
, account
, jid
, old_secret
, new_secret
, verified
):
77 our_secrets
= self
.srs
[account
][jid
]
79 idx
= our_secrets
.index(self
.find_srs(account
, jid
, old_secret
))
81 our_secrets
[idx
] = (new_secret
, verified
)
85 # the public key associated with 'account'
86 def my_pubkey(self
, account
):
88 pk
= self
.privkeys
[account
]
90 pk
= Crypto
.PublicKey
.RSA
.generate(384, crypto
.random_bytes
)
92 self
.privkeys
[account
] = pk
97 def load_secrets(filename
):
98 f
= open(filename
, 'r')
101 secrets
= pickle
.load(f
)
102 except (KeyError, EOFError):
104 secrets
= Secrets(filename
)
114 if os
.path
.exists(secrets_filename
):
115 secrets_cache
= load_secrets(secrets_filename
)
117 secrets_cache
= Secrets(secrets_filename
)