Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gajim
gajim
Commits
2ca0ca38
Commit
2ca0ca38
authored
Jul 01, 2018
by
Philipp Hörist
Browse files
Refactor Pubsub/Bookmarks/UserAvatar into own modules
parent
858e472e
Changes
16
Hide whitespace changes
Inline
Side-by-side
gajim/common/connection.py
View file @
2ca0ca38
...
...
@@ -73,6 +73,9 @@
from
gajim.common.modules.http_auth
import
HTTPAuth
from
gajim.common.modules.vcard_temp
import
VCardTemp
from
gajim.common.modules.vcard_avatars
import
VCardAvatars
from
gajim.common.modules.pubsub
import
PubSub
from
gajim.common.modules.bookmarks
import
Bookmarks
from
gajim.common.modules.user_avatar
import
UserAvatar
from
gajim.common.connection_handlers
import
*
from
gajim.common.contacts
import
GC_Contact
from
gajim.gtkgui_helpers
import
get_action
...
...
@@ -113,7 +116,6 @@ def __init__(self, name):
self
.
old_show
=
''
self
.
priority
=
app
.
get_priority
(
name
,
'offline'
)
self
.
time_to_reconnect
=
None
self
.
bookmarks
=
[]
self
.
blocked_list
=
[]
self
.
blocked_contacts
=
[]
...
...
@@ -491,18 +493,6 @@ def _on_new_account(self, con=None, con_type=None):
def
account_changed
(
self
,
new_name
):
self
.
name
=
new_name
def
get_bookmarks
(
self
):
"""
To be implemented by derived classes
"""
raise
NotImplementedError
def
store_bookmarks
(
self
):
"""
To be implemented by derived classes
"""
raise
NotImplementedError
def
get_metacontacts
(
self
):
"""
To be implemented by derived classes
...
...
@@ -670,6 +660,9 @@ def __init__(self, name):
self
.
register_module
(
'HTTPAuth'
,
HTTPAuth
,
self
)
self
.
register_module
(
'VCardTemp'
,
VCardTemp
,
self
)
self
.
register_module
(
'VCardAvatars'
,
VCardAvatars
,
self
)
self
.
register_module
(
'PubSub'
,
PubSub
,
self
)
self
.
register_module
(
'Bookmarks'
,
Bookmarks
,
self
)
self
.
register_module
(
'UserAvatar'
,
UserAvatar
,
self
)
app
.
ged
.
register_event_handler
(
'privacy-list-received'
,
ged
.
CORE
,
self
.
_nec_privacy_list_received
)
...
...
@@ -1774,8 +1767,8 @@ def _continue_invisible(self, con, iq_obj, msg, signed, initial):
# ask our VCard
self
.
get_module
(
'VCardTemp'
).
request_vcard
()
# Get bookmarks
from private namespace
self
.
get_bookmarks
()
# Get bookmarks
self
.
get_
module
(
'Bookmarks'
).
get_
bookmarks
()
# Get annotations
self
.
get_module
(
'Annotations'
).
get_annotations
()
...
...
@@ -1916,8 +1909,7 @@ def _nec_agent_info_received(self, obj):
self
.
pubsub_publish_options_supported
=
True
else
:
# Remove stored bookmarks accessible to everyone.
self
.
send_pb_purge
(
our_jid
,
'storage:bookmarks'
)
self
.
send_pb_delete
(
our_jid
,
'storage:bookmarks'
)
self
.
get_module
(
'Bookmarks'
).
purge_pubsub_bookmarks
()
if
obj
.
fjid
==
hostname
:
if
nbxmpp
.
NS_SECLABEL
in
obj
.
features
:
...
...
@@ -2255,99 +2247,6 @@ def bookmarks_available(self):
return
True
return
False
def
_request_bookmarks_xml
(
self
):
if
not
app
.
account_is_connected
(
self
.
name
):
return
iq
=
nbxmpp
.
Iq
(
typ
=
'get'
)
iq2
=
iq
.
addChild
(
name
=
'query'
,
namespace
=
nbxmpp
.
NS_PRIVATE
)
iq2
.
addChild
(
name
=
'storage'
,
namespace
=
'storage:bookmarks'
)
self
.
connection
.
send
(
iq
)
app
.
log
(
'bookmarks'
).
info
(
'Request Bookmarks (PrivateStorage)'
)
def
_check_bookmarks_received
(
self
):
if
not
self
.
bookmarks
:
self
.
_request_bookmarks_xml
()
def
get_bookmarks
(
self
,
storage_type
=
None
):
"""
Get Bookmarks from storage or PubSub if supported as described in XEP
0048
storage_type can be set to xml to force request to xml storage
"""
if
not
app
.
account_is_connected
(
self
.
name
):
return
if
storage_type
!=
'xml'
:
if
self
.
pep_supported
and
self
.
pubsub_publish_options_supported
:
self
.
send_pb_retrieve
(
''
,
'storage:bookmarks'
)
app
.
log
(
'bookmarks'
).
info
(
'Request Bookmarks (PubSub)'
)
# some server (ejabberd) are so slow to answer that we
# request via XML if we don't get answer in the next 30 seconds
app
.
idlequeue
.
set_alarm
(
self
.
_check_bookmarks_received
,
30
)
return
self
.
_request_bookmarks_xml
()
def
get_bookmarks_storage_node
(
self
):
NS_GAJIM_BM
=
'xmpp:gajim.org/bookmarks'
storage_node
=
nbxmpp
.
Node
(
tag
=
'storage'
,
attrs
=
{
'xmlns'
:
'storage:bookmarks'
})
for
bm
in
self
.
bookmarks
:
conf_node
=
storage_node
.
addChild
(
name
=
"conference"
)
conf_node
.
setAttr
(
'jid'
,
bm
[
'jid'
])
conf_node
.
setAttr
(
'autojoin'
,
bm
[
'autojoin'
])
conf_node
.
setAttr
(
'name'
,
bm
[
'name'
])
conf_node
.
setTag
(
'minimize'
,
namespace
=
NS_GAJIM_BM
).
setData
(
bm
[
'minimize'
])
# Only add optional elements if not empty
# Note: need to handle both None and '' as empty
# thus shouldn't use "is not None"
if
bm
.
get
(
'nick'
,
None
):
conf_node
.
setTagData
(
'nick'
,
bm
[
'nick'
])
if
bm
.
get
(
'password'
,
None
):
conf_node
.
setTagData
(
'password'
,
bm
[
'password'
])
if
bm
.
get
(
'print_status'
,
None
):
conf_node
.
setTag
(
'print_status'
,
namespace
=
NS_GAJIM_BM
).
setData
(
bm
[
'print_status'
])
return
storage_node
@
staticmethod
def
get_bookmark_publish_options
():
options
=
nbxmpp
.
Node
(
nbxmpp
.
NS_DATA
+
' x'
,
attrs
=
{
'type'
:
'submit'
})
f
=
options
.
addChild
(
'field'
,
attrs
=
{
'var'
:
'FORM_TYPE'
,
'type'
:
'hidden'
})
f
.
setTagData
(
'value'
,
nbxmpp
.
NS_PUBSUB_PUBLISH_OPTIONS
)
f
=
options
.
addChild
(
'field'
,
attrs
=
{
'var'
:
'pubsub#access_model'
})
f
.
setTagData
(
'value'
,
'whitelist'
)
return
options
def
store_bookmarks
(
self
,
storage_type
=
None
):
"""
Send bookmarks to the storage namespace or PubSub if supported
storage_type can be set to 'pubsub' or 'xml' so store in only one method
else it will be stored on both
"""
if
not
app
.
account_is_connected
(
self
.
name
):
return
storage_node
=
self
.
get_bookmarks_storage_node
()
if
storage_type
!=
'xml'
:
if
self
.
pep_supported
and
self
.
pubsub_publish_options_supported
:
self
.
send_pb_publish
(
''
,
'storage:bookmarks'
,
storage_node
,
'current'
,
options
=
self
.
get_bookmark_publish_options
())
app
.
log
(
'bookmarks'
).
info
(
'Bookmarks published (PubSub)'
)
if
storage_type
!=
'pubsub'
:
iq
=
nbxmpp
.
Iq
(
'set'
,
nbxmpp
.
NS_PRIVATE
,
payload
=
storage_node
)
self
.
connection
.
send
(
iq
)
app
.
log
(
'bookmarks'
).
info
(
'Bookmarks published (PrivateStorage)'
)
def
get_roster_delimiter
(
self
):
"""
Get roster group delimiter from storage as described in XEP 0083
...
...
@@ -2614,11 +2513,7 @@ def destroy_gc_room(self, room_jid, reason = '', jid = ''):
destroy
.
setAttr
(
'jid'
,
jid
)
self
.
connection
.
send
(
iq
)
i
=
0
for
bm
in
self
.
bookmarks
:
if
bm
[
'jid'
]
==
room_jid
:
del
self
.
bookmarks
[
i
]
break
i
+=
1
self
.
get_module
(
'Bookmarks'
).
bookmarks
.
pop
(
jid
,
None
)
self
.
store_bookmarks
()
def
send_gc_status
(
self
,
nick
,
jid
,
show
,
status
,
auto
=
False
):
...
...
gajim/common/connection_handlers.py
View file @
2ca0ca38
...
...
@@ -50,7 +50,6 @@
from
gajim.common
import
configpaths
from
gajim.common.caps_cache
import
muc_caps_cache
from
gajim.common.commands
import
ConnectionCommands
from
gajim.common.pubsub
import
ConnectionPubSub
from
gajim.common.protocol.caps
import
ConnectionCaps
from
gajim.common.protocol.bytestream
import
ConnectionSocks5Bytestream
from
gajim.common.protocol.bytestream
import
ConnectionIBBytestream
...
...
@@ -339,15 +338,15 @@ def send_activity(self, activity, subactivity=None, message=None):
if
message
:
i
=
item
.
addChild
(
'text'
)
i
.
addData
(
message
)
self
.
_p
ub
s
ub
_connection
.
send_pb_publish
(
''
,
nbxmpp
.
NS_ACTIVITY
,
item
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_publish
(
''
,
nbxmpp
.
NS_ACTIVITY
,
item
,
'0'
)
def
retract_activity
(
self
):
if
not
self
.
pep_supported
:
return
self
.
send_activity
(
None
)
# not all client support new XEP, so we still retract
self
.
_p
ub
s
ub
_connection
.
send_pb_retract
(
''
,
nbxmpp
.
NS_ACTIVITY
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_retract
(
''
,
nbxmpp
.
NS_ACTIVITY
,
'0'
)
def
send_mood
(
self
,
mood
,
message
=
None
):
if
self
.
connected
==
1
:
...
...
@@ -363,14 +362,14 @@ def send_mood(self, mood, message=None):
if
message
:
i
=
item
.
addChild
(
'text'
)
i
.
addData
(
message
)
self
.
_p
ub
s
ub
_connection
.
send_pb_publish
(
''
,
nbxmpp
.
NS_MOOD
,
item
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_publish
(
''
,
nbxmpp
.
NS_MOOD
,
item
,
'0'
)
def
retract_mood
(
self
):
if
not
self
.
pep_supported
:
return
self
.
send_mood
(
None
)
# not all client support new XEP, so we still retract
self
.
_p
ub
s
ub
_connection
.
send_pb_retract
(
''
,
nbxmpp
.
NS_MOOD
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_retract
(
''
,
nbxmpp
.
NS_MOOD
,
'0'
)
def
send_tune
(
self
,
artist
=
''
,
title
=
''
,
source
=
''
,
track
=
0
,
length
=
0
,
items
=
None
):
...
...
@@ -399,14 +398,14 @@ def send_tune(self, artist='', title='', source='', track=0, length=0,
i
.
addData
(
length
)
if
items
:
item
.
addChild
(
payload
=
items
)
self
.
_p
ub
s
ub
_connection
.
send_pb_publish
(
''
,
nbxmpp
.
NS_TUNE
,
item
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_publish
(
''
,
nbxmpp
.
NS_TUNE
,
item
,
'0'
)
def
retract_tune
(
self
):
if
not
self
.
pep_supported
:
return
self
.
send_tune
(
None
)
# not all client support new XEP, so we still retract
self
.
_p
ub
s
ub
_connection
.
send_pb_retract
(
''
,
nbxmpp
.
NS_TUNE
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_retract
(
''
,
nbxmpp
.
NS_TUNE
,
'0'
)
def
send_nickname
(
self
,
nick
):
if
self
.
connected
==
1
:
...
...
@@ -418,13 +417,13 @@ def send_nickname(self, nick):
return
item
=
nbxmpp
.
Node
(
'nick'
,
{
'xmlns'
:
nbxmpp
.
NS_NICK
})
item
.
addData
(
nick
)
self
.
_p
ub
s
ub
_connection
.
send_pb_publish
(
''
,
nbxmpp
.
NS_NICK
,
item
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_publish
(
''
,
nbxmpp
.
NS_NICK
,
item
,
'0'
)
def
retract_nickname
(
self
):
if
not
self
.
pep_supported
:
return
self
.
_p
ub
s
ub
_connection
.
send_pb_retract
(
''
,
nbxmpp
.
NS_NICK
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_retract
(
''
,
nbxmpp
.
NS_NICK
,
'0'
)
def
send_location
(
self
,
info
):
if
self
.
connected
==
1
:
...
...
@@ -439,14 +438,14 @@ def send_location(self, info):
if
info
.
get
(
field
,
None
):
i
=
item
.
addChild
(
field
)
i
.
addData
(
info
[
field
])
self
.
_p
ub
s
ub
_connection
.
send_pb_publish
(
''
,
nbxmpp
.
NS_LOCATION
,
item
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_publish
(
''
,
nbxmpp
.
NS_LOCATION
,
item
,
'0'
)
def
retract_location
(
self
):
if
not
self
.
pep_supported
:
return
self
.
send_location
({})
# not all client support new XEP, so we still retract
self
.
_p
ub
s
ub
_connection
.
send_pb_retract
(
''
,
nbxmpp
.
NS_LOCATION
,
'0'
)
self
.
get_module
(
'P
ub
S
ub
'
)
.
send_pb_retract
(
''
,
nbxmpp
.
NS_LOCATION
,
'0'
)
# basic connection handlers used here and in zeroconf
class
ConnectionHandlersBase
:
...
...
@@ -915,7 +914,7 @@ def make_new_session(self, jid, thread_id=None, type_='chat', cls=None):
class
ConnectionHandlers
(
ConnectionArchive313
,
ConnectionSocks5Bytestream
,
ConnectionDisco
,
ConnectionCommands
,
ConnectionPubSub
,
ConnectionPEP
,
ConnectionCaps
,
ConnectionCommands
,
ConnectionPEP
,
ConnectionCaps
,
ConnectionHandlersBase
,
ConnectionJingle
,
ConnectionIBBytestream
,
ConnectionHTTPUpload
):
def
__init__
(
self
):
...
...
@@ -923,7 +922,6 @@ def __init__(self):
ConnectionSocks5Bytestream
.
__init__
(
self
)
ConnectionIBBytestream
.
__init__
(
self
)
ConnectionCommands
.
__init__
(
self
)
ConnectionPubSub
.
__init__
(
self
)
ConnectionPEP
.
__init__
(
self
,
account
=
self
.
name
,
dispatcher
=
self
,
pubsub_connection
=
self
)
ConnectionHTTPUpload
.
__init__
(
self
)
...
...
@@ -949,8 +947,6 @@ def __init__(self):
self
.
privacy_default_list
=
None
app
.
nec
.
register_incoming_event
(
PrivateStorageBookmarksReceivedEvent
)
app
.
nec
.
register_incoming_event
(
BookmarksReceivedEvent
)
app
.
nec
.
register_incoming_event
(
StreamConflictReceivedEvent
)
app
.
nec
.
register_incoming_event
(
StreamOtherHostReceivedEvent
)
app
.
nec
.
register_incoming_event
(
MessageReceivedEvent
)
...
...
@@ -961,8 +957,6 @@ def __init__(self):
app
.
ged
.
register_event_handler
(
'roster-set-received'
,
ged
.
CORE
,
self
.
_nec_roster_set_received
)
app
.
ged
.
register_event_handler
(
'private-storage-bookmarks-received'
,
ged
.
CORE
,
self
.
_nec_private_storate_bookmarks_received
)
app
.
ged
.
register_event_handler
(
'roster-received'
,
ged
.
CORE
,
self
.
_nec_roster_received
)
app
.
ged
.
register_event_handler
(
'iq-error-received'
,
ged
.
CORE
,
...
...
@@ -988,12 +982,9 @@ def cleanup(self):
ConnectionHandlersBase
.
cleanup
(
self
)
ConnectionCaps
.
cleanup
(
self
)
ConnectionArchive313
.
cleanup
(
self
)
ConnectionPubSub
.
cleanup
(
self
)
ConnectionHTTPUpload
.
cleanup
(
self
)
app
.
ged
.
remove_event_handler
(
'roster-set-received'
,
ged
.
CORE
,
self
.
_nec_roster_set_received
)
app
.
ged
.
remove_event_handler
(
'private-storage-bookmarks-received'
,
ged
.
CORE
,
self
.
_nec_private_storate_bookmarks_received
)
app
.
ged
.
remove_event_handler
(
'roster-received'
,
ged
.
CORE
,
self
.
_nec_roster_received
)
app
.
ged
.
remove_event_handler
(
'iq-error-received'
,
ged
.
CORE
,
...
...
@@ -1144,28 +1135,6 @@ def _nec_iq_error_received(self, obj):
conn
=
self
,
stanza
=
obj
.
stanza
))
return
True
def
_nec_private_storate_bookmarks_received
(
self
,
obj
):
if
obj
.
conn
.
name
!=
self
.
name
:
return
app
.
log
(
'bookmarks'
).
info
(
'Received Bookmarks (PrivateStorage)'
)
resend_to_pubsub
=
False
bm_jids
=
[
b
[
'jid'
]
for
b
in
self
.
bookmarks
]
for
bm
in
obj
.
bookmarks
:
if
bm
[
'jid'
]
not
in
bm_jids
:
self
.
bookmarks
.
append
(
bm
)
# We got a bookmark that was not in pubsub
resend_to_pubsub
=
True
if
resend_to_pubsub
:
self
.
store_bookmarks
(
'pubsub'
)
def
_PrivateCB
(
self
,
con
,
iq_obj
):
"""
Private Data (XEP 048 and 049)
"""
log
.
debug
(
'PrivateCB'
)
app
.
nec
.
push_incoming_event
(
PrivateStorageReceivedEvent
(
None
,
conn
=
self
,
stanza
=
iq_obj
))
def
_SecLabelCB
(
self
,
con
,
iq_obj
):
"""
Security Label callback, used for catalogues.
...
...
@@ -1540,8 +1509,8 @@ def _send_first_presence(self, signed=''):
# ask our VCard
self
.
get_module
(
'VCardTemp'
).
request_vcard
()
# Get bookmarks
from private namespace
self
.
get_bookmarks
()
# Get bookmarks
self
.
get_
module
(
'Bookmarks'
).
get_
bookmarks
()
# Get annotations from private namespace
self
.
get_module
(
'Annotations'
).
get_annotations
()
...
...
@@ -1645,7 +1614,6 @@ def _register_handlers(self, con, con_type):
nbxmpp
.
NS_MUC_OWNER
)
con
.
RegisterHandler
(
'iq'
,
self
.
_MucAdminCB
,
'result'
,
nbxmpp
.
NS_MUC_ADMIN
)
con
.
RegisterHandler
(
'iq'
,
self
.
_PrivateCB
,
'result'
,
nbxmpp
.
NS_PRIVATE
)
con
.
RegisterHandler
(
'iq'
,
self
.
_SecLabelCB
,
'result'
,
nbxmpp
.
NS_SECLABEL_CATALOG
)
con
.
RegisterHandler
(
'iq'
,
self
.
_CommandExecuteCB
,
'set'
,
...
...
@@ -1657,8 +1625,6 @@ def _register_handlers(self, con, con_type):
con
.
RegisterHandler
(
'iq'
,
self
.
_PrivacySetCB
,
'set'
,
nbxmpp
.
NS_PRIVACY
)
con
.
RegisterHandler
(
'iq'
,
self
.
_ArchiveCB
,
ns
=
nbxmpp
.
NS_MAM_1
)
con
.
RegisterHandler
(
'iq'
,
self
.
_ArchiveCB
,
ns
=
nbxmpp
.
NS_MAM_2
)
con
.
RegisterHandler
(
'iq'
,
self
.
_PubSubCB
,
'result'
)
con
.
RegisterHandler
(
'iq'
,
self
.
_PubSubErrorCB
,
'error'
)
con
.
RegisterHandler
(
'iq'
,
self
.
_JingleCB
,
'result'
)
con
.
RegisterHandler
(
'iq'
,
self
.
_JingleCB
,
'error'
)
con
.
RegisterHandler
(
'iq'
,
self
.
_JingleCB
,
'set'
,
nbxmpp
.
NS_JINGLE
)
...
...
gajim/common/connection_handlers_events.py
View file @
2ca0ca38
...
...
@@ -309,118 +309,6 @@ def generate(self):
self
.
users_dict
[
jid
][
'reason'
]
=
reason
return
True
class
PrivateStorageReceivedEvent
(
nec
.
NetworkIncomingEvent
):
name
=
'private-storage-received'
base_network_events
=
[]
def
generate
(
self
):
query
=
self
.
stanza
.
getTag
(
'query'
)
self
.
storage_node
=
query
.
getTag
(
'storage'
)
if
self
.
storage_node
:
self
.
namespace
=
self
.
storage_node
.
getNamespace
()
return
True
class
BookmarksHelper
:
def
parse_bookmarks
(
self
):
self
.
bookmarks
=
[]
NS_GAJIM_BM
=
'xmpp:gajim.org/bookmarks'
confs
=
self
.
storage_node
.
getTags
(
'conference'
)
for
conf
in
confs
:
autojoin_val
=
conf
.
getAttr
(
'autojoin'
)
if
not
autojoin_val
:
# not there (it's optional)
autojoin_val
=
False
minimize_val
=
conf
.
getTag
(
'minimize'
,
namespace
=
NS_GAJIM_BM
)
if
not
minimize_val
:
# not there, try old Gajim behaviour
minimize_val
=
conf
.
getAttr
(
'minimize'
)
if
not
minimize_val
:
# not there (it's optional)
minimize_val
=
False
else
:
minimize_val
=
minimize_val
.
getData
()
print_status
=
conf
.
getTag
(
'print_status'
,
namespace
=
NS_GAJIM_BM
)
if
not
print_status
:
# not there, try old Gajim behaviour
print_status
=
conf
.
getTagData
(
'print_status'
)
if
not
print_status
:
# not there, try old Gajim behaviour
print_status
=
conf
.
getTagData
(
'show_status'
)
else
:
print_status
=
print_status
.
getData
()
try
:
jid
=
helpers
.
parse_jid
(
conf
.
getAttr
(
'jid'
))
except
helpers
.
InvalidFormat
:
log
.
warning
(
'Invalid JID: %s, ignoring it'
%
conf
.
getAttr
(
'jid'
))
continue
bm
=
{
'name'
:
conf
.
getAttr
(
'name'
),
'jid'
:
jid
,
'autojoin'
:
autojoin_val
,
'minimize'
:
minimize_val
,
'password'
:
conf
.
getTagData
(
'password'
),
'nick'
:
conf
.
getTagData
(
'nick'
),
'print_status'
:
print_status
}
bm_jids
=
[
b
[
'jid'
]
for
b
in
self
.
bookmarks
]
if
bm
[
'jid'
]
not
in
bm_jids
:
self
.
bookmarks
.
append
(
bm
)
class
PrivateStorageBookmarksReceivedEvent
(
nec
.
NetworkIncomingEvent
,
BookmarksHelper
):
name
=
'private-storage-bookmarks-received'
base_network_events
=
[
'private-storage-received'
]
def
generate
(
self
):
self
.
conn
=
self
.
base_event
.
conn
self
.
storage_node
=
self
.
base_event
.
storage_node
if
self
.
base_event
.
namespace
!=
nbxmpp
.
NS_BOOKMARKS
:
return
self
.
parse_bookmarks
()
return
True
class
BookmarksReceivedEvent
(
nec
.
NetworkIncomingEvent
):
name
=
'bookmarks-received'
base_network_events
=
[
'private-storage-bookmarks-received'
,
'pubsub-bookmarks-received'
]
def
generate
(
self
):
self
.
conn
=
self
.
base_event
.
conn
self
.
bookmarks
=
self
.
base_event
.
bookmarks
return
True
class
PubsubReceivedEvent
(
nec
.
NetworkIncomingEvent
):
name
=
'pubsub-received'
base_network_events
=
[]
def
generate
(
self
):
self
.
jid
=
self
.
stanza
.
getFrom
()
self
.
pubsub_node
=
self
.
stanza
.
getTag
(
'pubsub'
)
if
not
self
.
pubsub_node
:
return
self
.
items_node
=
self
.
pubsub_node
.
getTag
(
'items'
)
if
not
self
.
items_node
:
return
return
True
class
PubsubBookmarksReceivedEvent
(
nec
.
NetworkIncomingEvent
,
BookmarksHelper
):
name
=
'pubsub-bookmarks-received'
base_network_events
=
[
'pubsub-received'
]
def
generate
(
self
):
self
.
conn
=
self
.
base_event
.
conn
self
.
item_node
=
self
.
base_event
.
items_node
.
getTag
(
'item'
)
if
not
self
.
item_node
:
return
children
=
self
.
item_node
.
getChildren
()
if
not
children
:
return
self
.
storage_node
=
children
[
0
]
ns
=
self
.
storage_node
.
getNamespace
()
if
ns
!=
nbxmpp
.
NS_BOOKMARKS
:
return
self
.
parse_bookmarks
()
return
True
class
IqErrorReceivedEvent
(
nec
.
NetworkIncomingEvent
,
HelperEvent
):
name
=
'iq-error-received'
base_network_events
=
[]
...
...
gajim/common/const.py
View file @
2ca0ca38
...
...
@@ -119,6 +119,11 @@ class RequestAvatar(IntEnum):
ROOM
=
1
USER
=
2
@
unique
class
BookmarkStorageType
(
IntEnum
):
PRIVATE
=
0
PUBSUB
=
1
SSLError
=
{
2
:
_
(
"Unable to get issuer certificate"
),
3
:
_
(
"Unable to get certificate CRL"
),
...
...
gajim/common/modules/bookmarks.py
0 → 100644
View file @
2ca0ca38
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
# XEP-0048: Bookmarks
import
logging
import
nbxmpp
from
gajim.common
import
app
from
gajim.common
import
helpers
from
gajim.common.const
import
BookmarkStorageType
from
gajim.common.nec
import
NetworkIncomingEvent
log
=
logging
.
getLogger
(
'gajim.c.m.bookmarks'
)
class
Bookmarks
:
def
__init__
(
self
,
con
):
self
.
_con
=
con
self
.
_account
=
con
.
name
self
.
bookmarks
=
{}
self
.
handlers
=
[]
def
_pubsub_support
(
self
):
return
(
self
.
_con
.
pep_supported
and
self
.
_con
.
pubsub_publish_options_supported
)
def
get_bookmarks
(
self
,
storage_type
=
None
):
if
not
app
.
account_is_connected
(
self
.
_account
):
return
if
storage_type
in
(
None
,
BookmarkStorageType
.
PUBSUB
):
if
self
.
_pubsub_support
():
self
.
_request_pubsub_bookmarks
()
else
:
# Fallback, request private storage
self
.
_request_private_bookmarks
()
else
:
log
.
info
(
'Request Bookmarks (PrivateStorage)'
)
self
.
_request_private_bookmarks
()
def
_request_pubsub_bookmarks
(
self
):
log
.
info
(
'Request Bookmarks (PubSub)'
)
self
.
_con
.
get_module
(
'PubSub'
).
send_pb_retrieve
(
''
,
'storage:bookmarks'
,
cb
=
self
.
_pubsub_bookmarks_received
)
def
_pubsub_bookmarks_received
(
self
,
conn
,
stanza
):
if
not
nbxmpp
.
isResultNode
(
stanza
):
log
.
info
(
'No pubsub bookmarks: %s'
,
stanza
.
getError
())
# Fallback, request private storage
self
.
_request_private_bookmarks
()
return
log
.
info
(
'Received Bookmarks (PubSub)'
)
self
.
_parse_bookmarks
(
stanza
)
self
.
_request_private_bookmarks
()
def
_request_private_bookmarks
(
self
):
if
not
app
.
account_is_connected
(
self
.
_account
):
return
iq
=
nbxmpp
.
Iq
(
typ
=
'get'
)
query
=
iq
.
addChild
(
name
=
'query'
,
namespace
=
nbxmpp
.
NS_PRIVATE
)
query
.
addChild
(
name
=
'storage'
,
namespace
=
'storage:bookmarks'
)
log
.
info
(
'Request Bookmarks (PrivateStorage)'
)
self
.
_con
.
connection
.
SendAndCallForResponse
(
iq
,
self
.
_private_bookmarks_received
)
def
_private_bookmarks_received
(
self
,
stanza
):
if
not
nbxmpp
.
isResultNode
(
stanza
):
log
.
info
(
'No private bookmarks: %s'
,
stanza
.
getError
())
else
:
log
.
info
(
'Received Bookmarks (PrivateStorage)'
)
merged
=
self
.
_parse_bookmarks
(
stanza
,
check_merge
=
True
)
if
merged
:
log
.
info
(
'Merge PrivateStorage with PubSub'
)
self
.
store_bookmarks
(
BookmarkStorageType
.
PUBSUB
)
self
.
auto_join_bookmarks
()
app
.
nec
.
push_incoming_event
(
BookmarksReceivedEvent
(
None
,
account
=
self
.
_account
))