annotate MoinMoin/events/jabbernotify.py @ 2999:e94b940f3a84

add a comment about listen backlog to moin.fcg (port from 1.6)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 06 Jan 2008 17:32:33 +0100
parents 361c8b582018
children 16ae95df840a
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
2315
69ed234c3241 Bail out if the secret string is empty.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2307
diff changeset
13 from MoinMoin import error
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
14 from MoinMoin.Page import Page
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
15 from MoinMoin.user import User, getUserList
2595
5e7b45489430 moved python2.3/2.4 compatibility code to MoinMoin.support.python_compatibility (thanks to Heinrich Wendel)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2505
diff changeset
16 from MoinMoin.support.python_compatibility import set
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
17 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
18
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
19 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
20 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
21
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2074
diff changeset
22
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
23 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
24 """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
25
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
26 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
27
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 # 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
29 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
30 return
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
31
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
32 if isinstance(event, ev.PageChangedEvent):
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
33 return handle_page_changed(event, False)
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
34 elif isinstance(event, ev.TrivialPageChangedEvent):
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
35 return handle_page_changed(event, True)
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
36 elif isinstance(event, ev.JabberIDSetEvent) or isinstance(event, 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
37 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
38 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
39 return handle_file_attached(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
40 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
41 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
42 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
43 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
44 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
45 return handle_user_created(event)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
46
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
47
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
48 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
49 """ 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
50
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
51 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
52 server = request.cfg.notification_server
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.getText
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
54
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
55 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
56 if isinstance(event, ev.JabberIDSetEvent):
2105
cc5067ea1235 Fix brain-damaged behaviour I introduced yestarday.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2098
diff changeset
57 server.addJIDToRoster(request.cfg.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
58 else:
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
59 server.removeJIDFromRoster(request.cfg.secret, event.jid)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
60
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:
2323
a43db0817020 Use stdlib's logging instead of prints.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
62 ev.logger.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:
2323
a43db0817020 Use stdlib's logging instead of prints.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
64 ev.logger.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)
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
73 event_name = event.name
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
74 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
75 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
76 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
77
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 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
79 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
80
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
81 attachlink = request.getBaseURL() + getAttachUrl(event.pagename, event.filename, request)
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
82 pagelink = request.getQualifiedURL(page.url(request, {}, relative=False))
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
83
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
84 for lang in subscribers.keys():
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 _ = lambda text: request.getText(text, lang=lang, formatted=False)
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 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
87 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
88 {'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
89
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
90 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
91 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
92 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
93
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
94 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
95 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
96
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
97 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
98
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
99
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
100 def handle_page_changed(event, trivial):
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
101 """ 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
102 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
103 page = event.page
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
104
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
105 subscribers = page.getSubscribers(request, return_users=1, trivial=trivial)
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
106 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
107 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
108 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
109
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
110
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 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
112 """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
113
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
114 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
115 page = event.page
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
116
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
117 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
118 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
119 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
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"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
139
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
140 jids = []
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
141 user_ids = getUserList(event.request)
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
142 event_name = event.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
143
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
144 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
145 sitename = event.request.cfg.sitename
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
146 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
147
2948
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
148 msg = notification.user_created_message(event.request, sitename, username, email)
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
149
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
150 for id in user_ids:
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
151 usr = User(event.request, id=id)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
152
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
153 # Currently send this only to super users
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2385
diff changeset
154 if usr.isSuperUser() and usr.jid and event_name in usr.jabber_subscribed_events:
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
155 jids.append(usr.jid)
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
156
2948
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
157 data = {'action': "user_created", 'subject': msg['subject'], 'text': msg['body'],
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
158 'url_list': []}
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
159
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
160 send_notification(event.request, jids, data)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
161
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
162
2505
0a90c1bad60f Again, some fixes suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2494
diff changeset
163 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
164 """Sends notification about page being changed in some way"""
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
165 _ = request.getText
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
166
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
167 # 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
168 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
169 recipients = set()
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
170
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
171 for lang in subscribers:
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
172 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
173 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
174 msg = notification.page_change_message(change_type, request, page, lang, **kwargs)
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
175 page_url = request.getQualifiedURL(page.url(request, relative=False))
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
176 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
177 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
178 '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
179 '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
180 '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
181 '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
182
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 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
184
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
185 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
186 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
187
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
188 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
189 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
190
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
191 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
192 """ 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
193
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
194 @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
195 @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
196 @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
197 @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
198 @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
199
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
200 """
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
201 _ = request.getText
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
202 server = request.cfg.notification_server
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
203
2948
361c8b582018 Fix errors with user creation notification as reported by cb22
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2697
diff changeset
204 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
205 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
206
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
207 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
208 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
209
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
210 try:
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
211 server.send_notification(request.cfg.secret, 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
212 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
213 except xmlrpclib.Error, err:
2323
a43db0817020 Use stdlib's logging instead of prints.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
214 ev.logger.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
215 except Exception, err:
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
216 ev.logger.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
217