1
from pylons import config
3
from ocsmanager.lib.utils import validateDocXML
6
class NotificationModel:
8
def getNewMailParams(self, payload):
9
"""Retrieve newmail parameters."""
10
(error, xmlData) = validateDocXML(payload)
11
if error is True: return (error, xmlData)
13
# Retrieve MAPIStore management object
14
mgmt = config['mapistore']
16
# Initialize dictionary
19
# Retrieve notification and ensure it's newmail
20
notification = xmlData.find('notification')
21
if notification is None: return (True, 'Missing Notification')
22
if not 'category' in notification.attrib: return (True, 'Missing notification type')
23
if notification.attrib['category'] != 'newmail': return (True, 'Invalid notification type')
26
param = notification.find('backend')
27
if param is None or param.text is None: return (True, 'Invalid/Missing backend parameter')
28
if mgmt.registered_backend(param.text) is False: return (True, 'Specified backend is invalid')
29
params['backend'] = param.text
32
param = notification.find('folder')
33
if param is None or param.text is None: return (True, 'Invalid/Missing folder parameter')
34
params['folder'] = param.text
37
param = notification.find('messageID')
38
if param is None or param.text is None: retrun (True, 'Invalid/Missing messageID parameter')
39
params['messageID'] = param.text
42
param = notification.find('username')
43
if param is None or param.text is None: return (True, 'Invalid/Missing username parameter')
44
params['vuser'] = param.text
46
# Search for openchange user matching above attributes
47
ed = mgmt.existing_users(params['backend'], params['vuser'], params['folder'])
48
if ed['count'] == 0: return (True, 'Invalid user')
51
for info in ed['infos']:
52
ret = mgmt.registered_message(params['backend'], info['username'], params['vuser'],
53
params['folder'], info['mapistoreURI'], params['messageID'])
55
print 'Message already registered for user %s' % info['username']
58
# Register the message in all users indexing databases
59
message = mgmt.register_message(params['backend'], info['username'],
60
info['mapistoreURI'], params['messageID'])
61
if not message: print "Unable to register URI for user %s" % (info['username'])
63
print "[REGISTERED] user %s: (%s, %s)" % (info['username'], hex(message[0]), message[1])
65
# Case where all users referencing this folder already got notified
66
if count == ed['count']: return (True, 'Message already registered')
68
# Only trigger a notification for registered users
69
rd = mgmt.registered_users(params['backend'], param.text)
71
if rd["count"] == 0: return (True, 'User not registered')
72
params['usernames'] = rd["usernames"]
74
# Trigger newmail notification for registered users (rd) who subscribed for newmail Notification
75
for info in ed['infos']:
76
for username in params['usernames']:
77
# print 'Searching for fnevNewmail for %s on %s' % (username, info['mapistoreURI'])
78
# ret = mgmt.registered_subscription(username, info['mapistoreURI'], 1, 4)
80
print 'Sending newmail notif on /%s#newmail' % username
81
ret = mgmt.send_newmail(username, info['username'], info['mapistoreURI'], message[1])
83
print 'Error while sending newmail notif'
85
return (False, params)