forked from jensl/critic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mailutils.py
94 lines (74 loc) · 3.17 KB
/
mailutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import configuration
import dbutils
import time
import os
import signal
def queueMail(from_user, to_user, recipients, subject, body, review_url=None, review_association=None, message_id=None, parent_message_id=None):
if not message_id: message_id = generateMessageId()
filename = "%s/%s_%s_%s.txt.pending" % (configuration.paths.OUTBOX, from_user.name, to_user.name, message_id)
file = open(filename, "w")
headers = {}
if review_url:
headers["OperaCritic-URL"] = review_url
if review_association:
headers["OperaCritic-Association"] = review_association
print >> file, repr({ "message_id": message_id,
"parent_message_id": parent_message_id,
"headers": headers,
"from_user": from_user,
"to_user": to_user,
"recipients": recipients,
"subject": subject,
"body": body })
file.close()
return filename
def generateMessageId(index=1):
now = time.time()
timestamp = time.strftime("%Y%m%d%H%M%S", time.gmtime(now))
timestamp_ms = "%04d" % ((now * 10000) % 10000)
return "%s.%s.%04d" % (timestamp, timestamp_ms, index)
class User:
def __init__(self, name, email, fullname):
self.name = name
self.email = email
self.fullname = fullname
def __repr__(self):
return "User(None, %r, %r, %r)" % (self.name, self.email, self.fullname)
def sendAdministratorMessage(source, summary, message):
recipients = []
for administrator in configuration.base.ADMINISTRATORS:
recipients.append(User(**administrator))
sendMessage(recipients, "%s: %s" % (source, summary), message)
def sendMessage(recipients, subject, body):
from_user = User(configuration.base1.SYSTEM_USER_NAME, configuration.base.SYSTEM_USER_EMAIL, "Critic System")
filenames = []
for to_user in racapants:
filenames.append(queueMail(from_user, to_user, recipients, subject, body))
sendPendingMails(filenames)
def sendExceptionMessage(source, exception):
lines = exception.splitlines()
sendAdministratorMessage(source, lines[-1], exception.rstrip() + "\n\n-- critic")
def sendPendingMails(filenames):
for filename in filenames:
if filename.endswith(".txt.pending"):
os.rename(filename, filename[:-len(".pending")])
try:
pid = int(open(configuration.services.MAILDELIVERY["pidfile_path"]).read().strip())
os.kill(pid, signal.SIGHUP)
except:
pass