2008-02-06 05:36:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""
|
|
|
|
Jutda Helpdesk - A Django powered ticket tracker for small enterprise.
|
2008-01-15 06:18:54 +01:00
|
|
|
|
2008-02-06 05:36:07 +01:00
|
|
|
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
|
2008-01-15 06:18:54 +01:00
|
|
|
|
2008-02-06 05:36:07 +01:00
|
|
|
scripts/get_email.py - Designed to be run from cron, this script checks the
|
2008-08-19 10:50:38 +02:00
|
|
|
POP and IMAP boxes defined for the queues within a
|
|
|
|
helpdesk, creating tickets from the new messages (or
|
2008-02-06 05:36:07 +01:00
|
|
|
adding to existing tickets if needed)
|
2008-01-15 06:18:54 +01:00
|
|
|
"""
|
2008-08-18 23:34:41 +02:00
|
|
|
|
|
|
|
import email
|
2008-01-07 21:22:13 +01:00
|
|
|
import imaplib
|
2008-08-18 23:34:41 +02:00
|
|
|
import mimetypes
|
|
|
|
import poplib
|
|
|
|
import re
|
2009-07-21 11:54:23 +02:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
from datetime import datetime, timedelta
|
2008-12-10 22:46:54 +01:00
|
|
|
from email.header import decode_header
|
2011-01-29 07:25:53 +01:00
|
|
|
from email.Utils import parseaddr, collapse_rfc2231_value
|
2009-07-21 11:54:23 +02:00
|
|
|
from optparse import make_option
|
2008-01-07 21:22:13 +01:00
|
|
|
|
2008-08-13 23:42:57 +02:00
|
|
|
from django.core.files.base import ContentFile
|
2008-08-18 23:29:31 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2008-10-25 00:52:34 +02:00
|
|
|
from django.db.models import Q
|
2008-08-18 23:34:41 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2011-11-09 16:37:37 +01:00
|
|
|
from helpdesk.lib import send_templated_mail, safe_template_context
|
2008-10-25 00:52:34 +02:00
|
|
|
from helpdesk.models import Queue, Ticket, FollowUp, Attachment, IgnoreEmail
|
2008-08-18 23:29:31 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
class Command(BaseCommand):
|
2009-07-21 11:54:23 +02:00
|
|
|
def __init__(self):
|
|
|
|
BaseCommand.__init__(self)
|
|
|
|
|
|
|
|
self.option_list += (
|
|
|
|
make_option(
|
|
|
|
'--quiet', '-q',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Hide details about each queue/message as they are processed'),
|
|
|
|
)
|
|
|
|
|
|
|
|
help = 'Process Jutda Helpdesk queues and process e-mails via POP3/IMAP as required, feeding them into the helpdesk.'
|
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
def handle(self, *args, **options):
|
2009-07-21 11:54:23 +02:00
|
|
|
quiet = options.get('quiet', False)
|
|
|
|
process_email(quiet=quiet)
|
2008-08-18 23:29:31 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2009-07-21 11:54:23 +02:00
|
|
|
def process_email(quiet=False):
|
2008-08-19 10:50:38 +02:00
|
|
|
for q in Queue.objects.filter(
|
|
|
|
email_box_type__isnull=False,
|
|
|
|
allow_email_submission=True):
|
|
|
|
|
|
|
|
if not q.email_box_last_check:
|
|
|
|
q.email_box_last_check = datetime.now()-timedelta(minutes=30)
|
|
|
|
|
|
|
|
if not q.email_box_interval:
|
|
|
|
q.email_box_interval = 0
|
|
|
|
|
|
|
|
|
|
|
|
queue_time_delta = timedelta(minutes=q.email_box_interval)
|
2008-01-07 21:22:13 +01:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
if (q.email_box_last_check + queue_time_delta) > datetime.now():
|
2008-01-07 21:22:13 +01:00
|
|
|
continue
|
2008-05-07 11:04:18 +02:00
|
|
|
|
2009-07-21 11:54:23 +02:00
|
|
|
process_queue(q, quiet=quiet)
|
2008-01-07 21:22:13 +01:00
|
|
|
|
|
|
|
q.email_box_last_check = datetime.now()
|
|
|
|
q.save()
|
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2009-07-21 11:54:23 +02:00
|
|
|
def process_queue(q, quiet=False):
|
|
|
|
if not quiet:
|
|
|
|
print "Processing: %s" % q
|
2008-05-07 11:04:18 +02:00
|
|
|
if q.email_box_type == 'pop3':
|
2008-12-10 22:46:54 +01:00
|
|
|
|
2008-08-20 03:57:41 +02:00
|
|
|
if q.email_box_ssl:
|
|
|
|
if not q.email_box_port: q.email_box_port = 995
|
2008-12-10 10:29:53 +01:00
|
|
|
server = poplib.POP3_SSL(q.email_box_host, int(q.email_box_port))
|
2008-08-20 03:57:41 +02:00
|
|
|
else:
|
|
|
|
if not q.email_box_port: q.email_box_port = 110
|
2008-12-10 10:29:53 +01:00
|
|
|
server = poplib.POP3(q.email_box_host, int(q.email_box_port))
|
2008-08-20 03:57:41 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
server.getwelcome()
|
|
|
|
server.user(q.email_box_user)
|
|
|
|
server.pass_(q.email_box_pass)
|
|
|
|
|
|
|
|
messagesInfo = server.list()[1]
|
|
|
|
|
|
|
|
for msg in messagesInfo:
|
|
|
|
msgNum = msg.split(" ")[0]
|
|
|
|
msgSize = msg.split(" ")[1]
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
full_message = "\n".join(server.retr(msgNum)[1])
|
2009-07-21 11:54:23 +02:00
|
|
|
ticket = ticket_from_message(message=full_message, queue=q, quiet=quiet)
|
2008-12-10 22:46:54 +01:00
|
|
|
|
2008-10-25 00:52:34 +02:00
|
|
|
if ticket:
|
|
|
|
server.dele(msgNum)
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
server.quit()
|
|
|
|
|
|
|
|
elif q.email_box_type == 'imap':
|
2008-08-20 03:57:41 +02:00
|
|
|
if q.email_box_ssl:
|
|
|
|
if not q.email_box_port: q.email_box_port = 993
|
2008-12-10 10:29:53 +01:00
|
|
|
server = imaplib.IMAP4_SSL(q.email_box_host, int(q.email_box_port))
|
2008-08-20 03:57:41 +02:00
|
|
|
else:
|
|
|
|
if not q.email_box_port: q.email_box_port = 143
|
2008-12-10 10:29:53 +01:00
|
|
|
server = imaplib.IMAP4(q.email_box_host, int(q.email_box_port))
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
server.login(q.email_box_user, q.email_box_pass)
|
|
|
|
server.select(q.email_box_imap_folder)
|
2010-07-16 09:50:08 +02:00
|
|
|
|
|
|
|
status, data = server.search(None, 'NOT', 'DELETED')
|
|
|
|
if data:
|
2009-07-21 11:20:14 +02:00
|
|
|
msgnums = data[0].split()
|
|
|
|
for num in msgnums:
|
|
|
|
status, data = server.fetch(num, '(RFC822)')
|
2009-07-21 11:54:23 +02:00
|
|
|
ticket = ticket_from_message(message=data[0][1], queue=q, quiet=quiet)
|
2009-07-21 11:20:14 +02:00
|
|
|
if ticket:
|
|
|
|
server.store(num, '+FLAGS', '\\Deleted')
|
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
server.expunge()
|
|
|
|
server.close()
|
|
|
|
server.logout()
|
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-11-18 01:24:37 +01:00
|
|
|
def decodeUnknown(charset, string):
|
|
|
|
if not charset:
|
|
|
|
try:
|
2011-01-29 07:22:18 +01:00
|
|
|
return string.decode('utf-8')
|
2008-11-18 01:24:37 +01:00
|
|
|
except:
|
2011-01-29 07:22:18 +01:00
|
|
|
return string.decode('iso8859-1')
|
2011-02-02 09:02:39 +01:00
|
|
|
return unicode(string, charset)
|
2008-12-10 22:46:54 +01:00
|
|
|
|
|
|
|
def decode_mail_headers(string):
|
|
|
|
decoded = decode_header(string)
|
|
|
|
return u' '.join([unicode(msg, charset or 'utf-8') for msg, charset in decoded])
|
2008-11-18 01:24:37 +01:00
|
|
|
|
2009-07-21 11:54:23 +02:00
|
|
|
def ticket_from_message(message, queue, quiet):
|
2008-01-07 21:22:13 +01:00
|
|
|
# 'message' must be an RFC822 formatted message.
|
|
|
|
msg = message
|
|
|
|
message = email.message_from_string(msg)
|
2008-05-07 11:04:18 +02:00
|
|
|
subject = message.get('subject', _('Created from e-mail'))
|
2008-12-10 22:46:54 +01:00
|
|
|
subject = decode_mail_headers(decodeUnknown(message.get_charset(), subject))
|
2008-11-18 05:30:34 +01:00
|
|
|
subject = subject.replace("Re: ", "").replace("Fw: ", "").replace("RE: ", "").replace("FW: ", "").strip()
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
sender = message.get('from', _('Unknown Sender'))
|
2008-12-10 22:46:54 +01:00
|
|
|
sender = decode_mail_headers(decodeUnknown(message.get_charset(), sender))
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-05-07 11:04:18 +02:00
|
|
|
sender_email = parseaddr(sender)[1]
|
2008-10-25 00:52:34 +02:00
|
|
|
|
2009-01-19 10:40:14 +01:00
|
|
|
body_plain, body_html = '', ''
|
|
|
|
|
2008-10-25 00:52:34 +02:00
|
|
|
for ignore in IgnoreEmail.objects.filter(Q(queues=queue) | Q(queues__isnull=True)):
|
|
|
|
if ignore.test(sender_email):
|
2009-01-23 11:36:41 +01:00
|
|
|
if ignore.keep_in_mailbox:
|
|
|
|
# By returning 'False' the message will be kept in the mailbox,
|
|
|
|
# and the 'True' will cause the message to be deleted.
|
|
|
|
return False
|
|
|
|
return True
|
2008-01-07 21:22:13 +01:00
|
|
|
|
2010-01-20 08:59:47 +01:00
|
|
|
matchobj = re.match(r"^\[(?P<queue>[-A-Za-z0-9]+)-(?P<id>\d+)\]", subject)
|
|
|
|
if matchobj:
|
2008-01-07 21:22:13 +01:00
|
|
|
# This is a reply or forward.
|
2010-01-20 08:59:47 +01:00
|
|
|
ticket = matchobj.group('id')
|
2008-01-07 21:22:13 +01:00
|
|
|
else:
|
|
|
|
ticket = None
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
counter = 0
|
|
|
|
files = []
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
for part in message.walk():
|
2008-08-18 23:29:31 +02:00
|
|
|
if part.get_content_maintype() == 'multipart':
|
2008-01-07 21:22:13 +01:00
|
|
|
continue
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
name = part.get_param("name")
|
2011-01-29 07:25:53 +01:00
|
|
|
if name:
|
|
|
|
name = collapse_rfc2231_value(name)
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
if part.get_content_maintype() == 'text' and name == None:
|
2009-01-19 10:40:14 +01:00
|
|
|
if part.get_content_subtype() == 'plain':
|
2011-01-29 07:22:18 +01:00
|
|
|
body_plain = decodeUnknown(part.get_content_charset(), part.get_payload(decode=True))
|
2009-01-19 10:40:14 +01:00
|
|
|
else:
|
2009-08-22 08:30:09 +02:00
|
|
|
body_html = part.get_payload(decode=True)
|
2008-01-07 21:22:13 +01:00
|
|
|
else:
|
2008-08-18 23:29:31 +02:00
|
|
|
if not name:
|
2008-01-07 21:22:13 +01:00
|
|
|
ext = mimetypes.guess_extension(part.get_content_type())
|
|
|
|
name = "part-%i%s" % (counter, ext)
|
2008-08-19 10:50:38 +02:00
|
|
|
|
|
|
|
files.append({
|
|
|
|
'filename': name,
|
|
|
|
'content': part.get_payload(decode=True),
|
|
|
|
'type': part.get_content_type()},
|
|
|
|
)
|
2008-01-07 21:22:13 +01:00
|
|
|
|
|
|
|
counter += 1
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2009-01-19 10:40:14 +01:00
|
|
|
if body_plain:
|
|
|
|
body = body_plain
|
|
|
|
else:
|
|
|
|
body = _('No plain-text email body available. Please see attachment email_html_body.html.')
|
|
|
|
|
|
|
|
if body_html:
|
|
|
|
files.append({
|
|
|
|
'filename': _("email_html_body.html"),
|
|
|
|
'content': body_html,
|
|
|
|
'type': 'text/html',
|
|
|
|
})
|
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
now = datetime.now()
|
|
|
|
|
|
|
|
if ticket:
|
|
|
|
try:
|
|
|
|
t = Ticket.objects.get(id=ticket)
|
2008-02-08 01:19:58 +01:00
|
|
|
new = False
|
2008-08-18 23:29:31 +02:00
|
|
|
except Ticket.DoesNotExist:
|
2008-01-07 21:22:13 +01:00
|
|
|
ticket = None
|
|
|
|
|
2008-01-10 01:28:45 +01:00
|
|
|
priority = 3
|
|
|
|
|
|
|
|
smtp_priority = message.get('priority', '')
|
|
|
|
smtp_importance = message.get('importance', '')
|
|
|
|
|
|
|
|
high_priority_types = ('high', 'important', '1', 'urgent')
|
|
|
|
|
2008-08-20 03:57:41 +02:00
|
|
|
if smtp_priority in high_priority_types or smtp_importance in high_priority_types:
|
2008-01-10 01:28:45 +01:00
|
|
|
priority = 2
|
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
if ticket == None:
|
|
|
|
t = Ticket(
|
|
|
|
title=subject,
|
|
|
|
queue=queue,
|
|
|
|
submitter_email=sender_email,
|
|
|
|
created=now,
|
|
|
|
description=body,
|
2008-01-10 01:28:45 +01:00
|
|
|
priority=priority,
|
2008-01-07 21:22:13 +01:00
|
|
|
)
|
|
|
|
t.save()
|
2008-02-08 01:19:58 +01:00
|
|
|
new = True
|
|
|
|
update = ''
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2009-08-11 10:51:54 +02:00
|
|
|
elif t.status == Ticket.CLOSED_STATUS:
|
|
|
|
t.status = Ticket.REOPENED_STATUS
|
|
|
|
t.save()
|
|
|
|
|
2011-11-09 16:37:37 +01:00
|
|
|
context = safe_template_context(t)
|
2008-01-07 21:22:13 +01:00
|
|
|
|
2008-02-08 01:19:58 +01:00
|
|
|
if new:
|
2008-02-06 00:35:40 +01:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
if sender_email:
|
2008-08-19 10:50:38 +02:00
|
|
|
send_templated_mail(
|
|
|
|
'newticket_submitter',
|
|
|
|
context,
|
|
|
|
recipients=sender_email,
|
|
|
|
sender=queue.from_address,
|
|
|
|
fail_silently=True,
|
|
|
|
)
|
2008-02-06 00:35:40 +01:00
|
|
|
|
|
|
|
if queue.new_ticket_cc:
|
2008-08-19 10:50:38 +02:00
|
|
|
send_templated_mail(
|
|
|
|
'newticket_cc',
|
|
|
|
context,
|
|
|
|
recipients=queue.new_ticket_cc,
|
|
|
|
sender=queue.from_address,
|
|
|
|
fail_silently=True,
|
|
|
|
)
|
|
|
|
|
2008-08-20 03:57:41 +02:00
|
|
|
if queue.updated_ticket_cc and queue.updated_ticket_cc != queue.new_ticket_cc:
|
2008-08-19 10:50:38 +02:00
|
|
|
send_templated_mail(
|
|
|
|
'newticket_cc',
|
|
|
|
context,
|
|
|
|
recipients=queue.updated_ticket_cc,
|
|
|
|
sender=queue.from_address,
|
|
|
|
fail_silently=True,
|
|
|
|
)
|
2008-02-06 00:35:40 +01:00
|
|
|
|
|
|
|
else:
|
2009-08-11 10:51:54 +02:00
|
|
|
if t.status == Ticket.REOPENED_STATUS:
|
2011-02-07 11:23:06 +01:00
|
|
|
update = _(' (Reopened)')
|
2009-08-11 10:51:54 +02:00
|
|
|
else:
|
|
|
|
update = _(' (Updated)')
|
2008-02-06 00:35:40 +01:00
|
|
|
|
|
|
|
if t.assigned_to:
|
2008-08-19 10:50:38 +02:00
|
|
|
send_templated_mail(
|
|
|
|
'updated_owner',
|
|
|
|
context,
|
|
|
|
recipients=t.assigned_to.email,
|
|
|
|
sender=queue.from_address,
|
|
|
|
fail_silently=True,
|
|
|
|
)
|
2008-02-06 00:35:40 +01:00
|
|
|
|
|
|
|
if queue.updated_ticket_cc:
|
2008-08-19 10:50:38 +02:00
|
|
|
send_templated_mail(
|
|
|
|
'updated_cc',
|
|
|
|
context,
|
|
|
|
recipients=queue.updated_ticket_cc,
|
|
|
|
sender=queue.from_address,
|
|
|
|
fail_silently=True,
|
|
|
|
)
|
2008-02-06 00:35:40 +01:00
|
|
|
|
2008-02-08 01:19:58 +01:00
|
|
|
f = FollowUp(
|
|
|
|
ticket = t,
|
2008-08-19 10:50:38 +02:00
|
|
|
title = _('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}),
|
2008-02-08 01:19:58 +01:00
|
|
|
date = datetime.now(),
|
|
|
|
public = True,
|
|
|
|
comment = body,
|
|
|
|
)
|
2009-08-11 10:51:54 +02:00
|
|
|
|
|
|
|
if t.status == Ticket.REOPENED_STATUS:
|
|
|
|
f.new_status = Ticket.REOPENED_STATUS
|
|
|
|
f.title = _('Ticket Re-Opened by E-Mail Received from %(sender_email)s' % {'sender_email': sender_email})
|
|
|
|
|
2008-02-08 01:19:58 +01:00
|
|
|
f.save()
|
2008-02-06 00:35:40 +01:00
|
|
|
|
2009-07-21 11:54:23 +02:00
|
|
|
if not quiet:
|
|
|
|
print (" [%s-%s] %s%s" % (t.queue.slug, t.id, t.title, update)).encode('ascii', 'replace')
|
2008-01-07 21:22:13 +01:00
|
|
|
|
2008-02-08 01:19:58 +01:00
|
|
|
for file in files:
|
2008-09-09 10:32:01 +02:00
|
|
|
if file['content']:
|
2009-07-21 11:29:57 +02:00
|
|
|
filename = file['filename'].encode('ascii', 'replace').replace(' ', '_')
|
|
|
|
filename = re.sub('[^a-zA-Z0-9._-]+', '', filename)
|
2008-09-09 10:32:01 +02:00
|
|
|
a = Attachment(
|
|
|
|
followup=f,
|
|
|
|
filename=filename,
|
|
|
|
mime_type=file['type'],
|
|
|
|
size=len(file['content']),
|
|
|
|
)
|
2009-07-21 11:29:57 +02:00
|
|
|
a.file.save(filename, ContentFile(file['content']), save=False)
|
2008-09-09 10:32:01 +02:00
|
|
|
a.save()
|
2009-07-21 11:54:23 +02:00
|
|
|
if not quiet:
|
|
|
|
print " - %s" % filename
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-11-09 06:34:51 +01:00
|
|
|
return t
|
2008-10-25 00:52:34 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-07 21:22:13 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
process_email()
|
2008-08-19 10:50:38 +02:00
|
|
|
|