OTR plugin for Gajim 1.0+
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keystore.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
  2. # Copyright (C) 2019 Pavel R. <pd at narayana dot im>
  3. # This file is part of Gajim OTR Plugin.
  4. #
  5. # Gajim OTR Plugin is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published
  7. # by the Free Software Foundation; version 3 only.
  8. #
  9. # This software is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You can always obtain full license text at <http://www.gnu.org/licenses/>.
  15. import sqlite3
  16. from collections import namedtuple
  17. class Keystore:
  18. SCHEMA = '''
  19. PRAGMA synchronous=FULL;
  20. CREATE TABLE IF NOT EXISTS keystore (jid TEXT, privatekey TEXT, fingerprint TEXT, trust INTEGER, timestamp INTEGER, comment TEXT, UNIQUE(privatekey));
  21. CREATE UNIQUE INDEX IF NOT EXISTS jid_fingerprint ON keystore (jid, fingerprint);
  22. '''
  23. def __init__(self, db):
  24. self._db = sqlite3.connect(db, isolation_level=None)
  25. self._db.row_factory = lambda cur,row : namedtuple("Row", [col[0] for col in cur.description])(*row)
  26. self._db.executescript(self.SCHEMA)
  27. def __del__(self):
  28. self._db.close()
  29. # fetch all entries with known fingerprints: `load()` or specific entry `load(jid = jid)`
  30. def load(self, **args):
  31. sql = "SELECT * FROM keystore WHERE %s" % (not args and "fingerprint IS NOT NULL" or " AND ".join(["{0}='{1}'".format(*arg) for arg in args.items()]))
  32. return (args) and self._db.execute(sql).fetchone() or self._db.execute(sql).fetchall()
  33. # save entry to database: save(jid=jid, fingerprint=fingerprint)
  34. def save(self, **args):
  35. sql = "REPLACE INTO keystore({0}) VALUES({1})".format(",".join(args.keys()), ",".join(["'%s'"%s for s in args.values()]))
  36. self._db.execute(sql)
  37. # delete entry from database: `delete(jid=jid) `
  38. def delete(self, **args):
  39. sql = "DELETE FROM keystore WHERE %s" % " AND ".join(["{0}='{1}'".format(*a) for a in args.items()])
  40. self._db.execute(sql)