summaryrefslogtreecommitdiff
path: root/src/bookmark.py
blob: 5f1fe508efc835c697033503dcf9672b71fc5dfe (plain)
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
import os

from sleekxmpp.plugins.xep_0048 import *
from core import JID
from config import config

preferred = config.get('use_bookmarks_method', 'pep').lower()
if preferred not in ('pep', 'privatexml'):
    preferred = 'pep'
not_preferred = 'pep' if preferred is 'privatexml' else 'privatexml'
methods = ('local', preferred, not_preferred)


class Bookmark(object):

    possible_methods = methods

    def __init__(self, jid, name=None, autojoin=False, nick=None, password=None, method=None):
        self.jid = jid
        self.name = name or jid
        self.autojoin = autojoin
        if not nick:
            default = os.environ.get('USER') if os.environ.get('USER') else 'poezio'
            nick = config.get('default_nick', '') or default
        self.nick = nick
        self.password = password
        self._method = method

    @property
    def method(self):
        return self._method

    @method.setter
    def method(self, value):
        if value not in self.possible_methods:
            return
        self._method = value

    def __repr__(self):
        return '<%s>' % self.jid

    def stanza(self):
        """
        Generate a <conference/> stanza from the instance
        """
        el = Conference()
        el['name'] = self.name
        el['jid'] = self.jid
        el['autojoin'] = 'true' if self.autojoin else 'false'
        if self.nick:
            n = Nick().xml
            n.text = self.nick
            el.append(n)
        if self.password:
            p = Password().xml
            p.text = self.password
            el.append(p)
        return el

    def parse_from_element(el, method=None):
        """
        Generate a Bookmark object from a <conference/> element
        """
        jid = el.get('jid')
        name = el.get('name')
        autojoin = True if el.get('autojoin', False) == 'true' else False
        nick = None
        for n in el.iter('nick'):
            nick = nick.text
        password = None
        for p in el.iter('password'):
            password = p.text

        return Bookmark(jid, name, autojoin, nick, password, method)


bookmarks = []

def get_pep(xmpp):
    try:
        iq = xmpp.plugin['xep_0048'].get_bookmarks()
    except:
        return False
    for conf in iq.xml.iter('{storage:bookmarks}conference'):
        b = Bookmark.parse_from_element(conf, method='pep')
        if not get_by_jid(b.jid):
            bookmarks.append(b)
    return True

def get_privatexml(xmpp):
    try:
        iq = xmpp.plugin['xep_0048'].get_bookmarks_old()
    except:
        return False
    for conf in iq.xml.iter('{storage:bookmarks}conference'):
        b = Bookmark.parse_from_element(conf, method='privatexml')
        if not get_by_jid(b.jid):
            bookmarks.append(b)
    return True

def get_remote(xmpp):
    if xmpp.anon:
        return
    pep, privatexml = True, True
    for method in methods[1:]:
        if method == 'pep':
            pep = get_pep(xmpp)
        else:
            privatexml = get_privatexml(xmpp)
    if pep and not privatexml:
        config.set_and_save('use_bookmarks_method', 'pep')
    elif privatexml and not pep:
        config.set_and_save('use_bookmarks_method', 'privatexml')

def get_local():
    rooms = config.get('rooms', '')
    if not rooms:
        return
    rooms = rooms.split(':')
    for room in rooms:
        jid = JID(room)
        if jid.bare == '':
            continue
        if jid.resource != '':
            nick = jid.resource
        else:
            nick = None
        b = Bookmark(jid.bare, autojoin=True, nick=nick, method='local')
        if not get_by_jid(b.jid):
            bookmarks.append(b)