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.1KB

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 os
  16. import sqlite3
  17. from collections import namedtuple
  18. class Keystore:
  19. __TABLE_LAYOUT__ = '''
  20. CREATE TABLE IF NOT EXISTS keystore (jid TEXT, privatekey TEXT, fingerprint TEXT, trust INTEGER, timestamp INTEGER, comment TEXT, UNIQUE(privatekey)); CREATE UNIQUE INDEX IF NOT EXISTS jid_fingerprint ON keystore (jid, fingerprint);
  21. '''
  22. def __init__(self, db):
  23. self._db = sqlite3.connect(db, isolation_level=None)
  24. self._db.row_factory = lambda cur,row : namedtuple("Row", [col[0] for col in cur.description])(*row)
  25. self._db.execute("PRAGMA synchronous=FULL;")
  26. self._db.executescript(self.__TABLE_LAYOUT__)
  27. def load(self, item = {'fingerprint IS NOT NULL; --': None}):
  28. sql = "SELECT * FROM keystore WHERE " + " AND ".join(["%s = '%s'" % (str(key), str(value)) for key,value in item.items()])
  29. if next(iter(item.values())): return self._db.execute(sql).fetchone() # return fetchone() if `item` arg is set
  30. return self._db.execute(sql).fetchall() or () # else return fetchall() or empty iterator
  31. def save(self, item):
  32. sql = "REPLACE INTO keystore(%s) VALUES(%s)" % (",".join(item.keys()), ",".join(["'%s'" % x for x in item.values()]) )
  33. return self._db.execute(sql)
  34. def forgot(self, item):
  35. sql = "DELETE FROM keystore WHERE " + " AND ".join(["%s='%s'" % (str(key),str(value)) for key,value in item.items()])
  36. return self._db.execute(sql)
  37. def close(self):
  38. self._db.close()