Skip to content

Instantly share code, notes, and snippets.

@ivanelson
Created August 14, 2013 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanelson/6236385 to your computer and use it in GitHub Desktop.
Save ivanelson/6236385 to your computer and use it in GitHub Desktop.
email2trac - Integration Trac and GLPI
def ticket_update_by_subject(self, subject):
"""
This list of Re: prefixes is probably incomplete. Taken from
wikipedia. Here is how the subject is matched
- Re: <subject>
- Re: (<Mail list label>:)+ <subject>
So we must have the last column
"""
self.logger.debug('function ticket_update_by_subject')
found_id = None
if self.parameters.ticket_update and self.parameters.ticket_update_by_subject:
SUBJECT_RE = re.compile(r'^(?:(?:RE|AW|VS|SV|FW|FWD):\s*)+(.*)', re.IGNORECASE)
result = SUBJECT_RE.search(subject)
LOG = open('/tmp/log.txt','a')
LOG.write('Coletando emails...: %s\n' % result)
LOG.close()
if 'Service Desk #' in subject.encode('utf8'):
result = subject.encode('utf8')
formata = u'[Service Desk #%s]' % (re.findall(r'#(\w+)',result)[0],)
result = formata #formata.encode('utf8')
LOG = open('/tmp/log.txt','a')
LOG.write('formata: %s\n' % formata)
LOG.write('result: %s\n' % result)
LOG.close()
if result:
## This is a reply
#
try:
orig_subject = result if 'Service Desk #' in result else result.group(1)
if 'Service Desk #' in result:
summaries = [orig_subject, '%s%%' % orig_subject]
self.logger.debug('SERVICEDESK subject search string: %s' %(orig_subject))
except TypeError, e:
orig_subject = result.group(1)
summaries = [orig_subject, '%%: %s' % orig_subject]
self.logger.debug('subject search string: %s' %(orig_subject))
cursor = self.db.cursor()
#if 'Service Desk #' in result:
# summaries = [orig_subject, '%s%%' % orig_subject]
# self.logger.debug('SERVICEDESK subject search string: %s' %(orig_subject))
#else:
# summaries = [orig_subject, '%%: %s' % orig_subject]
## Time resolution is in micoseconds
#
search_date = datetime.now(util.datefmt.utc) - timedelta(days=self.parameters.ticket_update_by_subject_lookback)
if self.VERSION < 0.12:
lookback = util.datefmt.to_timestamp(search_date)
else:
lookback = util.datefmt.to_utimestamp(search_date)
for summary in summaries:
self.logger.debug('Looking for summary matching: "%s"' % summary)
sql = """SELECT id, reporter FROM ticket
WHERE changetime >= %s AND summary LIKE %s
ORDER BY changetime DESC"""
cursor.execute(sql, [lookback, summary.strip()])
for row in cursor:
(matched_id, sql_reporter) = row
self.logger.debug('ID Ticket: %s' % matched_id)
self.logger.debug('sql_reporter : %s' % sql_reporter)
## Save first entry.
#
if not found_id:
found_id = matched_id
## If subject and reporter are the same. The we certainly have found the right ticket
#
if sql_reporter == self.author:
self.logger.debug('Found matching reporter: %s with ticket id: %d' %(sql_reporter, matched_id))
found_id = matched_id
break
if found_id:
self.logger.debug('Found matching ticket id: %d' % found_id)
found_id = '#%d' % found_id
return (found_id, orig_subject)
# obsolete !?? 12 Aug 2011
#subject = orig_subject
return (found_id, subject)
@thri015
Copy link

thri015 commented Oct 16, 2020

hi could you please guide me, how to use this file in GLPI

@thri015
Copy link

thri015 commented Oct 16, 2020

hi could you please help , how to use this file in GLPI

@ivanelson
Copy link
Author

Hi
What do you really need? This code is a small change in this plugin using Trac.

https://oss.trac.surfsara.nl/email2trac

GLPI sent a notification from the ticket to a specific user, so this plugin captured that email and added a Tick to the trac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment