| | 7 | PRS_OFFLINE = 1 |
| | 8 | PRS_CHAT = 2 |
| | 9 | PRS_AVAILABLE = 3 |
| | 10 | PRS_AWAY = 4 |
| | 11 | PRS_XA = 5 |
| | 12 | PRS_DND = 6 |
| | 13 | |
| | 14 | def db_connect(config): |
| | 15 | if config.DB_MODULE == 'sqlite': |
| | 16 | from pysqlite2 import dbapi2 as sqlite |
| | 17 | return sqlite.connect(config.SQLITE_DB) |
| | 18 | else: |
| | 19 | raise Exception("Incorrect DB module") |
| | 20 | |
| | 21 | class Users: |
| | 22 | def __init__(self, db): |
| | 23 | self.db = db |
| | 24 | self.users_list = {} |
| | 25 | self.read_users() |
| | 26 | |
| | 27 | def read_users(self): |
| | 28 | cursor = self.db.cursor() |
| | 29 | cursor.execute("SELECT jid FROM randomchat WHERE 1") |
| | 30 | user = cursor.fetchone() |
| | 31 | while user: |
| | 32 | self.users_list[user[0]] = PRS_OFFLINE |
| | 33 | user = cursor.fetchone() |
| | 34 | cursor.close() |
| | 35 | |
| | 36 | def has_user(self, jid): |
| | 37 | return jid in self.users_list |
| | 38 | |
| | 39 | def add_user(self, jid): |
| | 40 | self.db.execute("INSERT INTO randomchat (jid) VALUES (?)", (jid,)) |
| | 41 | self.db.commit() |
| | 42 | |
| | 43 | def remove_user(self, jid): |
| | 44 | self.db.execute("DELETE FROM randomchat WHERE jid=?", (jid,)) |
| | 45 | self.db.commit() |
| | 46 | |
| | 47 | def get_chat_partner(jid): |
| | 48 | pass |
| | 49 | |
| | 50 | |
| | 63 | self.con.RegisterHandler('presence', self.subscribe_handler, 'subscribe') |
| | 64 | self.con.RegisterHandler('presence', self.subscribed_handler, 'subscribed') |
| | 65 | self.con.RegisterHandler('presence', self.unsubscribe_handler, 'unsubscribe') |
| | 66 | self.con.RegisterHandler('presence', self.unsubscribed_handler, 'unsubscribed') |
| | 67 | self.con.RegisterHandler('message', self.message_handler) |
| | 73 | |
| | 74 | def subscribe_handler(self, con, prs): |
| | 75 | user = prs.getFrom().getStripped() |
| | 76 | if not self.users.has_user(user): |
| | 77 | self.con.send(xmpp.Presence(to = prs.getFrom(), frm = self.jid, typ = 'subscribe')) |
| | 78 | self.con.send(xmpp.Presence(to = prs.getFrom(), frm = self.jid, typ = 'subscribed')) |
| | 79 | |
| | 80 | def subscribed_handler(self, con, prs): |
| | 81 | user = prs.getFrom().getStripped() |
| | 82 | if not self.users.has_user(user): |
| | 83 | self.users.add_user(user) |
| | 84 | |
| | 85 | def unsubscribe_handler(self, con, prs): |
| | 86 | self.con.send(xmpp.Presence(to = prs.getFrom(), frm = self.jid, typ = 'unsubscribe')) |
| | 87 | |
| | 88 | def unsubscribed_handler(self, con, prs): |
| | 89 | user = prs.getFrom().getStripped() |
| | 90 | self.users.remove_user(user) |
| | 91 | self.con.send(xmpp.Presence(to = prs.getFrom(), frm = self.jid, typ = 'unsubscribed')) |
| | 92 | |
| 26 | | rc = RandomChat(config.JID, config.PASS, config.SERVER, config.PORT) |
| | 96 | def message_handler(self, con, msg): |
| | 97 | self.init_chat(msg.getFrom()) |
| | 98 | |
| | 99 | def init_chat(self, jid): |
| | 100 | other_jid = self.users.get_chat_partner(jid) |
| | 101 | |
| | 102 | |
| | 103 | db = db_connect(config) |
| | 104 | users = Users(db) |
| | 105 | rc = RandomChat(users, config.JID, config.PASS, config.SERVER, config.PORT) |