-
Notifications
You must be signed in to change notification settings - Fork 0
/
quassel.py
153 lines (126 loc) · 5.54 KB
/
quassel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from datetime import datetime
from time import mktime
from sqlalchemy import \
MetaData, Table, Column, \
Integer, DateTime, String, Text, Boolean, Sequence, \
ForeignKey, \
PrimaryKeyConstraint, UniqueConstraint, \
create_engine, \
desc
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.schema import Index
from sqlalchemy.types import TypeDecorator
class IntegerDateTime(TypeDecorator):
"""
Used for working with epoch timestamps.
Converts datetimes into epoch on the way in.
Converts epoch timestamps to datetimes on the way out.
"""
impl = Integer()
def process_bind_param(self, value, dialect):
return mktime(value.timetuple())
def process_result_value(self, value, dialect):
return datetime.fromtimestamp(value)
QuasselDateTime = DateTime()
QuasselDateTime = QuasselDateTime.with_variant(IntegerDateTime(), "sqlite")
class Base(object):
def _filter_properties(self):
# this function decides which properties should be exposed through repr
# todo: don"t show methods
properties = self.__dict__.keys()
for prop in properties:
if not prop.startswith("_"):
yield (prop, getattr(self, prop))
return
def __repr__(self):
prop_tuples = self._filter_properties()
prop_string_tuples = ("{0}={1}".format(*prop) for prop in prop_tuples)
prop_output_string = ", ".join(prop_string_tuples)
cls_name = self.__class__.__name__
return "{0}({1})".format(cls_name, prop_output_string)
def to_dict(self):
prop_tuples = self._filter_properties()
d = dict(prop_tuples)
for k,v in d.items():
if hasattr(v, "__table__") and hasattr(v, "to_dict"):
d[k] = v.to_dict()
return d
def to_json(self):
from json import dumps
return dumps(self.to_dict())
Model = declarative_base(cls=Base)
class Message(Model):
__tablename__ = "backlog"
id = Column("messageid", Integer, Sequence("backlog_messageid_seq"), nullable=False, primary_key=True)
time = Column(QuasselDateTime, nullable=False)
bufferid = Column(Integer, ForeignKey("buffer.bufferid", ondelete="CASCADE"), nullable=False)
type = Column(Integer, nullable=False)
flags = Column(Integer, nullable=False)
senderid = Column(Integer, ForeignKey("sender.senderid", ondelete="SET NULL"), nullable=False)
message = Column(Text)
buffer = relationship("Buffer", backref="messages")
sender = relationship("Sender", backref="messages")
__tablemeta__ = [
Index("backlog_bufferid_idx", "bufferid"),
Index("backlog_buffer_time_idx", "bufferid", "time"),
]
class Sender(Model):
__tablename__ = "sender"
id = Column("senderid", Integer, Sequence("sender_senderid_seq"), nullable=False, primary_key=True)
name = Column("sender", String(128), nullable=False, unique=True)
def pretty_name(self):
return self.name.split("!")[0]
class Buffer(Model):
__tablename__ = "buffer"
id = Column("bufferid", Integer, Sequence("buffer_bufferid_seq"), nullable=False, primary_key=True)
userid = Column(Integer, ForeignKey("quasseluser.userid", ondelete="CASCADE"), nullable=False, unique=True)
groupid = Column(Integer)
networkid = Column(Integer, ForeignKey("network.networkid", ondelete="CASCADE"), nullable=False, unique=True)
name = Column("buffername", String(128), nullable=False)
cname = Column("buffercname", String(128), nullable=False, unique=True)
type = Column("buffertype", Integer, nullable=False, default=0)
lastseenmsgid = Column(Integer, nullable=False, default=0)
markerlinemsgid = Column(Integer, nullable=False, default=0)
key = Column(String(128))
joined = Column(Boolean, nullable=False, default=False)
user = relationship("QuasselUser", backref="buffers")
network = relationship("Network", backref="buffers")
__tablemeta__ = [
Index("buffer_idx", "userid", "networkid", "buffername"),
Index("buffer_cname_idx", "userid", "networkid", "buffercname"),
Index("buffer_user_idx", "userid"),
]
class QuasselUser(Model):
__tablename__ = "quasseluser"
userid = Column(Integer, Sequence("quasseluser_userid_seq"), nullable=False, primary_key=True)
username = Column(String(64), nullable=False, unique=True)
password = Column(String(40), nullable=False) # CHAR(40)
class Network(Model):
__tablename__ = "network"
id = Column("networkid", Integer, Sequence("network_networkid_seq"), nullable=False, primary_key=True)
userid = Column(Integer, ForeignKey("quasseluser.userid", ondelete="CASCADE"), nullable=False, unique=True)
name = Column("networkname", String(32), nullable=False, unique=True)
identityid = Column(Integer, ForeignKey("identity.identityid", ondelete="SET NULL"))
encodingcodec = Column(String(32), nullable=False, default="ISO-8859-15")
decodingcodec = Column(String(32), nullable=False, default="ISO-8859-15")
servercodec = Column(String(32))
userandomserver = Column(Boolean, nullable=False, default=False)
perform = Column(Text)
useautoidentify = Column(Boolean, nullable=False, default=False)
saslaccount = Column(String(128))
saslpassword = Column(String(128))
useautoreconnect = Column(Boolean, nullable=False, default=True)
autoreconnectinterval = Column(Integer, nullable=False, default=0)
autoreconnectretries = Column(Integer, nullable=False, default=0)
unlimitedconnectretries = Column(Boolean, nullable=False, default=False)
rejoinchannels = Column(Boolean, nullable=False, default=False)
connected = Column(Boolean, nullable=False, default=False)
usermode = Column(String(32))
awaymessage = Column(String(256))
attachperform = Column(Text)
detachperform = Column(Text)
def quassel_session(uri):
engine = create_engine(uri, echo=False)
session = sessionmaker(engine)()
return session