Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
python-nbxmpp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
8
Issues
8
List
Boards
Labels
Service Desk
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gajim
python-nbxmpp
Commits
53bfd2b1
Commit
53bfd2b1
authored
Oct 12, 2020
by
Philipp Hörist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Blocking: Refactor module
parent
aceef5cd
Pipeline
#6565
passed with stages
in 1 minute and 5 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
112 additions
and
45 deletions
+112
-45
nbxmpp/modules/blocking.py
nbxmpp/modules/blocking.py
+105
-43
nbxmpp/structs.py
nbxmpp/structs.py
+7
-2
No files found.
nbxmpp/modules/blocking.py
View file @
53bfd2b1
...
...
@@ -17,13 +17,14 @@
from
nbxmpp.namespaces
import
Namespace
from
nbxmpp.protocol
import
Iq
from
nbxmpp.protocol
import
isResultNode
from
nbxmpp.structs
import
BlockingListResult
from
nbxmpp.structs
import
CommonResult
from
nbxmpp.util
import
call_on_response
from
nbxmpp.util
import
callback
from
nbxmpp.util
import
raise_error
from
nbxmpp.protocol
import
JID
from
nbxmpp.modules.base
import
BaseModule
from
nbxmpp.modules.util
import
raise_if_error
from
nbxmpp.modules.util
import
finalize
from
nbxmpp.task
import
iq_request_task
from
nbxmpp.errors
import
MalformedStanzaError
from
nbxmpp.structs
import
BlockingPush
from
nbxmpp.structs
import
StanzaHandler
class
Blocking
(
BaseModule
):
...
...
@@ -31,54 +32,115 @@ class Blocking(BaseModule):
BaseModule
.
__init__
(
self
,
client
)
self
.
_client
=
client
self
.
handlers
=
[]
self
.
handlers
=
[
StanzaHandler
(
name
=
'iq'
,
priority
=
15
,
callback
=
self
.
_process_blocking_push
,
typ
=
'set'
,
ns
=
Namespace
.
BLOCKING
),
]
@
call_on_response
(
'_blocking_list_received'
)
def
get_blocking_list
(
self
):
iq
=
Iq
(
'get'
,
Namespace
.
BLOCKING
)
iq
.
setQuery
(
'blocklist'
)
return
iq
@
iq_request_task
def
request_blocking_list
(
self
):
_task
=
yield
@
callback
def
_blocking_list_received
(
self
,
stanza
):
blocked
=
[]
if
not
isResultNode
(
stanza
):
return
raise_error
(
self
.
_log
.
info
,
stanza
)
result
=
yield
_make_blocking_list_request
()
raise_if_error
(
result
)
blocklist
=
stanza
.
getTag
(
'blocklist'
,
namespace
=
Namespace
.
BLOCKING
)
blocklist
=
result
.
getTag
(
'blocklist'
,
namespace
=
Namespace
.
BLOCKING
)
if
blocklist
is
None
:
r
eturn
raise_error
(
self
.
_log
.
warning
,
stanza
,
'stanza-malformed'
)
r
aise
MalformedStanzaError
(
'blocklist node missing'
,
result
)
blocked
=
[]
for
item
in
blocklist
.
getTags
(
'item'
):
blocked
.
append
(
item
.
getAttr
(
'jid'
))
self
.
_log
.
info
(
'Received blocking list: %s'
,
blocked
)
return
BlockingListResult
(
blocking_list
=
blocked
)
yield
blocked
@
call_on_response
(
'_default_response'
)
@
iq_request_task
def
block
(
self
,
jids
,
report
=
None
):
task
=
yield
self
.
_log
.
info
(
'Block: %s'
,
jids
)
iq
=
Iq
(
'set'
,
Namespace
.
BLOCKING
)
query
=
iq
.
setQuery
(
name
=
'block'
)
for
jid
in
jids
:
item
=
query
.
addChild
(
name
=
'item'
,
attrs
=
{
'jid'
:
jid
})
if
report
in
(
'spam'
,
'abuse'
):
action
=
item
.
addChild
(
name
=
'report'
,
namespace
=
Namespace
.
REPORTING
)
action
.
setTag
(
report
)
return
iq
@
call_on_response
(
'_default_response'
)
result
=
yield
_make_block_request
(
jids
,
report
)
yield
finalize
(
task
,
result
)
@
iq_request_task
def
unblock
(
self
,
jids
):
task
=
yield
self
.
_log
.
info
(
'Unblock: %s'
,
jids
)
iq
=
Iq
(
'set'
,
Namespace
.
BLOCKING
)
query
=
iq
.
setQuery
(
name
=
'unblock'
)
for
jid
in
jids
:
query
.
addChild
(
name
=
'item'
,
attrs
=
{
'jid'
:
jid
})
return
iq
@
callback
def
_default_response
(
self
,
stanza
):
if
not
isResultNode
(
stanza
):
return
raise_error
(
self
.
_log
.
info
,
stanza
)
return
CommonResult
(
jid
=
stanza
.
getFrom
())
result
=
yield
_make_unblock_request
(
jids
)
yield
finalize
(
task
,
result
)
@
staticmethod
def
_process_blocking_push
(
client
,
stanza
,
properties
):
unblock
=
stanza
.
getTag
(
'unblock'
,
namespace
=
Namespace
.
BLOCKING
)
if
unblock
is
not
None
:
properties
.
blocking
=
_parse_push
(
unblock
)
return
block
=
stanza
.
getTag
(
'block'
,
namespace
=
Namespace
.
BLOCKING
)
if
block
is
not
None
:
properties
.
blocking
=
_parse_push
(
block
)
reply
=
stanza
.
buildSimpleReply
(
'result'
)
client
.
send_stanza
(
reply
)
def
_make_blocking_list_request
():
iq
=
Iq
(
'get'
,
Namespace
.
BLOCKING
)
iq
.
setQuery
(
'blocklist'
)
return
iq
def
_make_block_request
(
jids
,
report
):
iq
=
Iq
(
'set'
,
Namespace
.
BLOCKING
)
query
=
iq
.
setQuery
(
name
=
'block'
)
for
jid
in
jids
:
item
=
query
.
addChild
(
name
=
'item'
,
attrs
=
{
'jid'
:
jid
})
if
report
in
(
'spam'
,
'abuse'
):
action
=
item
.
addChild
(
name
=
'report'
,
namespace
=
Namespace
.
REPORTING
)
action
.
setTag
(
report
)
return
iq
def
_make_unblock_request
(
jids
):
iq
=
Iq
(
'set'
,
Namespace
.
BLOCKING
)
query
=
iq
.
setQuery
(
name
=
'unblock'
)
for
jid
in
jids
:
query
.
addChild
(
name
=
'item'
,
attrs
=
{
'jid'
:
jid
})
return
iq
def
_parse_push
(
node
):
items
=
node
.
getTags
(
'item'
)
if
not
items
:
return
BlockingPush
(
block
=
[],
unblock
=
[],
unblock_all
=
True
)
jids
=
[]
for
item
in
items
:
jid
=
item
.
getAttr
(
'jid'
)
if
not
jid
:
continue
try
:
jid
=
JID
.
from_string
(
jid
)
except
Exception
:
continue
jids
.
append
(
jid
)
block
,
unblock
=
[],
[]
if
node
.
getName
()
==
'block'
:
block
=
jids
else
:
unblock
=
jids
return
BlockingPush
(
block
=
block
,
unblock
=
unblock
,
unblock_all
=
False
)
nbxmpp/structs.py
View file @
53bfd2b1
...
...
@@ -75,6 +75,8 @@ PubSubEventData.__new__.__defaults__ = (None, None, None, False, False, False)
MoodData
=
namedtuple
(
'MoodData'
,
'mood text'
)
BlockingPush
=
namedtuple
(
'BlockingPush'
,
'block unblock unblock_all'
)
ActivityData
=
namedtuple
(
'ActivityData'
,
'activity subactivity text'
)
LocationData
=
namedtuple
(
'LocationData'
,
LOCATION_DATA
)
...
...
@@ -83,8 +85,6 @@ LocationData.__new__.__defaults__ = (None,) * len(LocationData._fields)
BookmarkData
=
namedtuple
(
'BookmarkData'
,
'jid name nick autojoin password'
)
BookmarkData
.
__new__
.
__defaults__
=
(
None
,
None
,
None
,
None
)
BlockingListResult
=
namedtuple
(
'BlockingListResult'
,
'blocking_list'
)
PGPPublicKey
=
namedtuple
(
'PGPPublicKey'
,
'jid key date'
)
PGPKeyMetadata
=
namedtuple
(
'PGPKeyMetadata'
,
'jid fingerprint date'
)
...
...
@@ -844,6 +844,7 @@ class IqProperties:
self
.
payload
=
None
self
.
http_auth
=
None
self
.
ibb
=
None
self
.
blocking
=
None
@
property
def
is_http_auth
(
self
):
...
...
@@ -853,6 +854,10 @@ class IqProperties:
def
is_ibb
(
self
):
return
self
.
ibb
is
not
None
@
property
def
is_blocking
(
self
):
return
self
.
blocking
is
not
None
class
PresenceProperties
:
def
__init__
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment