annotate MoinMoin/events/jabbernotify.py @ 6133:a6283e189869 tip

fixup: remove nonexisting passlib.utils._blowfish this was removed by the passlib 1.7.1 upgrade.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Jun 2017 18:10:19 +0200
parents 500f68d3e2fd
children
rev   line source
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
3 MoinMoin - jabber notification plugin for event system
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5 This code sends notifications using a separate daemon.
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
6
2110
1ede132a9d38 Suggested fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2109
diff changeset
7 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
1ede132a9d38 Suggested fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2109
diff changeset
8 @license: GNU GPL, see COPYING for details.
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
9 """
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
10
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
11 import xmlrpclib
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
12
3637
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
13 from MoinMoin import log
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
14 logging = log.getLogger(__name__)
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
15
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
16 from MoinMoin.Page import Page
6027
8618232296b5 optimized notification of superusers on user account creation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5280
diff changeset
17 from MoinMoin.user import User, superusers
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
18 from MoinMoin.action.AttachFile import getAttachUrl
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
19
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
20 import MoinMoin.events.notification as notification
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
21 import MoinMoin.events as ev
2086
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2074
diff changeset
22
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2074
diff changeset
23
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
24 def handle(event):
2344
d680aee762a7 Fix notification about user creation - command needs a list of jids.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
25 """An event handler"""
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2344
diff changeset
26
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
27 cfg = event.request.cfg
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
28
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
29 # Check for desired event type and if notification bot is configured
2093
917bb59fdbb8 Catch Exception as well, because xmlrpclib lets other exceptions propagate.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2091
diff changeset
30 if not cfg.jabber_enabled:
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
31 return
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
32
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3456
diff changeset
33 if isinstance(event, (ev.PageChangedEvent, ev.TrivialPageChangedEvent)):
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3456
diff changeset
34 return handle_page_changed(event)
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3456
diff changeset
35 elif isinstance(event, (ev.JabberIDSetEvent, ev.JabberIDUnsetEvent)):
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
36 return handle_jid_changed(event)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
37 elif isinstance(event, ev.FileAttachedEvent):
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
38 return handle_file_attached(event)
5279
2aa43685e17b AttachFile: added remove_attachment(), made nuke_page use it (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4665
diff changeset
39 # TODO (needs also corresponding changes in xmppbot + testing)
2aa43685e17b AttachFile: added remove_attachment(), made nuke_page use it (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4665
diff changeset
40 #elif isinstance(event, ev.FileRemovedEvent):
2aa43685e17b AttachFile: added remove_attachment(), made nuke_page use it (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4665
diff changeset
41 # return handle_file_removed(event)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
42 elif isinstance(event, ev.PageDeletedEvent):
2109
b39379ab47d3 Send notifications when page is deleted + some small fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
43 return handle_page_deleted(event)
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
44 elif isinstance(event, ev.PageRenamedEvent):
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
45 return handle_page_renamed(event)
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
46 elif isinstance(event, ev.UserCreatedEvent):
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
47 return handle_user_created(event)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
48
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
49
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
50 def handle_jid_changed(event):
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
51 """ Handles events sent when user's JID changes """
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
52
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
53 request = event.request
2160
7c8ee12f8c1d Fix an error with missing bot_host attribute. Use more descriptive names.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2154
diff changeset
54 server = request.cfg.notification_server
3873
e5a9570d3001 secrets configuration refactored - see the snippet for an example usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3638
diff changeset
55 secret = request.cfg.secrets['jabberbot']
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
56 try:
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
57 if isinstance(event, ev.JabberIDSetEvent):
3873
e5a9570d3001 secrets configuration refactored - see the snippet for an example usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3638
diff changeset
58 server.addJIDToRoster(secret, event.jid)
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
59 else:
3873
e5a9570d3001 secrets configuration refactored - see the snippet for an example usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3638
diff changeset
60 server.removeJIDFromRoster(secret, event.jid)
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
61 except xmlrpclib.Error, err:
3637
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
62 logging.error("XML RPC error: %s" % str(err))
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
63 except Exception, err:
3637
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
64 logging.error("Low-level communication error: %s" % str(err))
2095
258d6e648eca Add a JabberIDUnsetEvent; make some private symbols internal in MoinMoin.events
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2093
diff changeset
65
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
66
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
67 def handle_file_attached(event):
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
68 """Handles event sent when a file is attached to a page"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
69
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
70 names = set()
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
71 request = event.request
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
72 page = Page(request, event.pagename)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
73 subscribers = page.getSubscribers(request, return_users=1)
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
74 notification.filter_subscriber_list(event, subscribers, True)
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
75 recipients = []
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
76
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
77 for lang in subscribers:
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
78 recipients.extend(subscribers[lang])
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
79
4663
68628fe3b091 fix wrong links in attachment notifications
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3638
diff changeset
80 attachlink = request.getQualifiedURL(getAttachUrl(event.pagename, event.filename, request))
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3211
diff changeset
81 pagelink = request.getQualifiedURL(page.url(request, {}))
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
82
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
83 for lang in subscribers.keys():
3143
16ae95df840a i18n.getText: fixed some callers that had still 'formatted=' kw args
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2948
diff changeset
84 _ = lambda text: request.getText(text, lang=lang)
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
85 data = notification.attachment_added(request, _, event.pagename, event.filename, event.size)
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
86 links = [{'url': attachlink, 'description': _("Attachment link")},
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
87 {'url': pagelink, 'description': _("Page link")}]
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
88
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
89 jids = [usr.jid for usr in subscribers[lang]]
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
90 data['url_list'] = links
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
91 data['action'] = "file_attached"
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
92
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
93 if send_notification(request, jids, data):
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
94 names.update(recipients)
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
95
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
96 return notification.Success(names)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
97
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
98
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3456
diff changeset
99 def handle_page_changed(event):
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
100 """ Handles events related to page changes """
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
101 request = event.request
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
102 page = event.page
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
103
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3456
diff changeset
104 subscribers = page.getSubscribers(request, return_users=1)
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
105 notification.filter_subscriber_list(event, subscribers, True)
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
106 return page_change("page_changed", request, page, subscribers, \
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
107 revisions=page.getRevList(), comment=event.comment)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
108
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
109
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
110 def handle_page_deleted(event):
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
111 """Handles event sent when a page is deleted"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
112
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
113 request = event.request
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
114 page = event.page
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
115
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
116 subscribers = page.getSubscribers(request, return_users=1)
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
117 notification.filter_subscriber_list(event, subscribers, True)
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
118 return page_change("page_deleted", request, page, subscribers)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
119
5280
d25574af97eb event package: fix PEP8, c&p errors, trivial code changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5279
diff changeset
120
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
121 def handle_page_renamed(event):
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
122 """Handles event sent when a page is renamed"""
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
123
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
124 request = event.request
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
125 page = event.page
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
126 old_name = event.old_page.page_name
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
127
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
128 subscribers = page.getSubscribers(request, return_users=1)
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
129
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
130 # Change request's page so that we filter subscribers of the OLD page
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
131 request.page = event.old_page
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
132 notification.filter_subscriber_list(event, subscribers, True)
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
133 request.page = page
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
134 return page_change("page_renamed", request, page, subscribers, old_name=old_name)
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
135
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
136
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
137 def handle_user_created(event):
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
138 """Handles an event sent when a new user is being created"""
3636
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
139 request = event.request
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
140 sitename = request.cfg.sitename
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2505
diff changeset
141 event_name = event.name
2344
d680aee762a7 Fix notification about user creation - command needs a list of jids.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
142 email = event.user.email or u"NOT SET"
2354
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2345
diff changeset
143 username = event.user.name
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
144
6027
8618232296b5 optimized notification of superusers on user account creation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5280
diff changeset
145 for usr in superusers(request):
8618232296b5 optimized notification of superusers on user account creation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5280
diff changeset
146 if usr.jid and event_name in usr.jabber_subscribed_events:
3638
715c0a9e4840 jabber/email new user notification: use english language if user did not specify language
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3637
diff changeset
147 _ = lambda text: request.getText(text, lang=usr.language or 'en')
3636
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
148 msg = notification.user_created_message(request, _, sitename, username, email)
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
149 data = {'action': "user_created", 'subject': msg['subject'], 'text': msg['text'],
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
150 'url_list': []}
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
151 send_notification(request, [usr.jid], data)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
152
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
153
2505
0a90c1bad60f Again, some fixes suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2494
diff changeset
154 def page_change(change_type, request, page, subscribers, **kwargs):
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
155 """Sends notification about page being changed in some way"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
156
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
157 # send notifications to all subscribers
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
158 if subscribers:
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
159 recipients = set()
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
160
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
161 for lang in subscribers:
3636
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
162 _ = lambda text: request.getText(text, lang=lang)
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
163 jids = [u.jid for u in subscribers[lang] if u.jid]
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
164 names = [u.name for u in subscribers[lang] if u.jid]
2505
0a90c1bad60f Again, some fixes suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2494
diff changeset
165 msg = notification.page_change_message(change_type, request, page, lang, **kwargs)
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3211
diff changeset
166 page_url = request.getQualifiedURL(page.url(request))
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
167 url = {'url': page_url, 'description': _("Changed page")}
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
168 data = {'action': change_type, 'subject': _('Page changed'),
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
169 'url_list': [url], 'text': msg['text'], 'diff': msg.get('diff', ''),
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
170 'comment': msg.get('comment', ''), 'editor': msg['editor'],
2697
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
171 'old_name': msg.get('old_name', ''), 'page_name': msg.get('page_name', ''),
80cf17344f98 Reverts from forms should work, but I didn't do much testing.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2688
diff changeset
172 'revision': msg.get('revision', '')}
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
173
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
174 result = send_notification(request, jids, data)
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
175
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
176 if result:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
177 recipients.update(names)
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
178
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
179 if recipients:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
180 return notification.Success(recipients)
2109
b39379ab47d3 Send notifications when page is deleted + some small fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
181
5280
d25574af97eb event package: fix PEP8, c&p errors, trivial code changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5279
diff changeset
182
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
183 def send_notification(request, jids, notification):
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
184 """ Send notifications for a single language.
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
185
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
186 @param jids: an iterable of Jabber IDs to send the message to
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2505
diff changeset
187 @param message: message text
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2505
diff changeset
188 @param subject: subject of the message, makes little sense for chats
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2505
diff changeset
189 @param url_list: a list of dicts containing URLs and their descriptions
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
190 @type url_list: list
2635
8ec6cc1a909f Change argument passing in notifications; some pylint and event.name fixes too
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2505
diff changeset
191
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
192 """
2160
7c8ee12f8c1d Fix an error with missing bot_host attribute. Use more descriptive names.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2154
diff changeset
193 server = request.cfg.notification_server
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
194
2948
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
195 if type(notification) != dict:
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
196 raise ValueError("notification must be of type dict!")
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
197
2688
da94375a625c Towards full data forms support. Not finished, probably broken. Don't merge.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2681
diff changeset
198 if type(notification['url_list']) != list:
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
199 raise ValueError("url_list must be of type list!")
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
200
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
201 try:
3873
e5a9570d3001 secrets configuration refactored - see the snippet for an example usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3638
diff changeset
202 server.send_notification(request.cfg.secrets['jabberbot'], jids, notification)
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
203 return True
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
204 except xmlrpclib.Error, err:
3637
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
205 logging.error("XML RPC error: %s" % str(err))
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
206 except Exception, err:
3637
92d08c1975b0 jabber notifications: don't translate log messages, fix non-existing logger
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
207 logging.error("Low-level communication error: %s" % str(err))
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
208