]>
git.datanom.net - securemail.git/blob - db.py
1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2018 Michael Rasmussen <mir@datanom.net>
5 # This file is part of SecureMail.
7 # SecureMail is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # SecureMail is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with SecureMail. If not, see <https://www.gnu.org/licenses/>.
21 sqlite_sql
= """create table account (
22 id int auto_increment,
23 token char(128) unique not null,
28 mysql_sql
= """create table account (
29 id int auto_increment,
30 token char(128) unique not null,
35 postgresql_sql
= """create table account (
37 token char(128) unique not null,
38 cipher bytea not null,
42 from config
import DBTYPE
, DBNAME
44 from config
import DBUID
48 from config
import DBPWD
50 DBPWD
= 'clV77B2ZJQxr'
52 from config
import DBHOST
56 from config
import DBPORT
60 elif DBTYPE
== 'postgresql':
62 from cryptonize
import Cryptonize
65 def __init__(self
, klass
):
69 def __call__(self
, *args
, **kwargs
):
70 if self
.instance
== None:
71 self
.instance
= self
.klass(*args
, **kwargs
)
78 def get_connection(self
):
82 self
.conn
= MySQLdb
.connect(host
=DBHOST
, port
=DBPORT
, user
=DBUID
, passwd
=DBPWD
, db
=DBNAME
)
83 elif DBTYPE
== 'postgresql':
85 self
.conn
= psycopg2
.connect(host
=DBHOST
, port
=DBPORT
, user
=DBUID
, password
=DBPWD
, dbname
=DBNAME
)
86 elif DBTYPE
== 'sqlite':
88 self
.conn
= apsw
.Connection('./{0}.db'.format(DBNAME
))
90 raise ValueError('{0}: Unsupported database'.format(DBTYPE
))
94 if self
.conn
is not None:
100 conn
= DB().get_connection()
101 cursor
= conn
.cursor()
102 cursor
.execute("select a.cipher from account a where token = '{0}'".format(key
))
103 row
= cursor
.fetchone()
108 msg
= base64
.b64decode(row
[0])
109 obj
= c
.create_EncryptedMessage(msg
)
115 def store_user(key
, cipher
):
116 if DBTYPE
== 'mysql':
117 from MySQLdb
import Error
as DBError
118 elif DBTYPE
== 'postgresql':
119 from psycopg2
import Error
as DBError
120 elif DBTYPE
== 'sqlite':
121 from apsw
import Error
as DBError
122 conn
= DB().get_connection()
123 cursor
= conn
.cursor()
124 raw
= base64
.b64encode(cipher
)
126 if DBTYPE
!= 'sqlite':
127 cursor
.execute("insert into account(token, cipher) values(%s, %s)", (key
, raw
))
130 cursor
.execute('begin')
131 cursor
.execute("insert into account(token, cipher) values(?, ?)", (key
, raw
))
132 cursor
.execute('commit')
135 if DBTYPE
!= 'sqlite':
138 cursor
.execute('rollback')
144 def create_database():
145 if DBTYPE
== 'mysql':
146 from MySQLdb
import Error
as DBError
147 elif DBTYPE
== 'postgresql':
148 from psycopg2
import Error
as DBError
149 elif DBTYPE
== 'sqlite':
150 from apsw
import Error
as DBError
151 conn
= DB().get_connection()
152 cursor
= conn
.cursor()
154 if DBTYPE
!= 'sqlite':
155 if DBTYPE
== 'mysql':
157 elif DBTYPE
== 'postgresql':
162 cursor
.execute('begin')
163 cursor
.execute(sqlite_sql
)
164 cursor
.execute('commit')
166 if DBTYPE
!= 'sqlite':
169 cursor
.execute('rollback')
175 from optparse
import OptionParser
177 usage
= "usage: %prog [options] arg"
178 parser
= OptionParser(usage
)
179 parser
.add_option("-c", "--create", action
="store_true", dest
="create",
180 help="Create tables in database using config.py", default
=False)
181 parser
.add_option("-v", "--verbose", action
="store_true", dest
="verbose",
182 help="Run in verbose mode", default
=False)
183 (options
, args
) = parser
.parse_args()
188 print("Creating empty database")
189 print("Database Engine: {0}".format(DBTYPE
))
190 if DBTYPE
!= 'sqlite':
191 print("Database Host: {0}".format(DBHOST
))
192 print("Database Port: {0}".format(DBPORT
))
194 print("Database File: ./{0}.db".format(DBNAME
))
195 DBInterface
.create_database()
196 print("Database created")
197 except Exception as e
:
198 print("Creating database failed!")
201 if __name__
== '__main__':
This page took 0.115989 seconds and 6 git commands to generate.