Changeset 72 for emailtotracscript/trunk/email2trac.py.in
- Timestamp:
- 05/22/06 11:44:59 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
emailtotracscript/trunk/email2trac.py.in
r71 r72 99 99 self.db = None 100 100 101 # Some useful mail constants 102 # 103 self.author = None 104 self.email_addr = None 105 self.email_field = None 106 101 107 self.VERSION = version 102 108 if self.VERSION == 0.8: … … 216 222 217 223 def email_header_txt(self, m): 218 # if not m['Subject']: 219 # subject = '(geen subject)' 220 # else: 221 # subject = self.to_unicode(m['Subject']) 222 # 223 # head = "'''Subject:''' %s [[BR]]" % subject 224 # if m['From'] and len(m['From']) > 0: 225 # head = "%s'''From:''' %s [[BR]]" % (head, m['From']) 226 # if m['Date'] and len(m['Date']) > 0: 227 # head = "%s'''Date:''' %s [[BR]]" %(head, m['Date']) 228 224 """ 225 Display To and CC addresses in description field 226 """ 229 227 str = '' 230 228 if m['To'] and len(m['To']) > 0 and m['To'] != 'hic@sara.nl': … … 247 245 ticket['owner'] = "UNKNOWN" 248 246 249 def set_reply_fields(self, ticket, message): 250 """ 251 Bla Bla 252 """ 253 author, email_addr = email.Utils.parseaddr(message['from']) 254 email_str = self.to_unicode(message['from']) 247 def get_author_emailaddrs(self, message): 248 """ 249 Get the default author name and email address from the message 250 """ 251 self.author, self.email_addr = email.Utils.parseaddr(message['from']) 255 252 256 253 # Look for email address in registered trac users … … 260 257 else: 261 258 users = [ u for (u, n, e) in self.env.get_known_users(self.db) 262 if e == email_addr ]259 if e == self.email_addr ] 263 260 264 261 if len(users) == 1: 265 ticket['reporter'] = users[0] 266 else: 267 ticket['reporter'] = email_str 262 self.email_field = users[0] 263 else: 264 self.email_field = self.to_unicode(message['from']) 265 266 def set_reply_fields(self, ticket, message): 267 """ 268 Set all the right fields for a new ticket 269 """ 270 ticket['reporter'] = self.email_field 268 271 269 272 # Put all CC-addresses in ticket CC field … … 280 283 if self.notification: 281 284 try: 282 addrs.remove(( author,email_addr))285 addrs.remove((self.author, self.email_addr)) 283 286 except ValueError, detail: 284 287 pass … … 289 292 except: 290 293 ticket['cc'] = mail 291 return author, email_addr292 294 293 295 def save_email_for_debug(self, message): … … 319 321 return False 320 322 321 # Strip # and : frim ticket_id323 # Strip '#' and ':' from ticket_id 322 324 ticket_id = result.group('ticketnr') 323 325 ticket_id = int(ticket_id[1:-1]) 324 print ticket_id325 326 326 327 tkt = Ticket(self.db, ticket_id) 327 print tkt 328 329 body_text = self.get_body_text(m) 330 body_text = '{{{\n%s\n}}}' %body_text 331 tkt.save_changes(self.db, self.author, body_text) 332 333 self.attachments(m, tkt) 334 if self.notification: 335 self.notify(tkt, False) 336 328 337 return True 329 338 … … 336 345 if self.DEBUG > 1: # save the entire e-mail message text 337 346 self.save_email_for_debug(msg) 347 self.debug_attachments(msg) 338 348 339 349 self.db = self.env.get_db_cnx() 350 self.get_author_emailaddrs(msg) 351 352 if self.get_config('notification', 'smtp_enabled') in ['true']: 353 self.notification = 1 354 else: 355 self.notification = 0 356 357 # Is this a previous ticket 358 # 340 359 if self.ticket_update(msg): 341 print 'Ticket has been updated now new one' 342 sys.exit(0) 343 else: 344 print 'New ticket' 345 346 sys.exit(0) 360 return True 347 361 348 362 tkt = Ticket(self.env) 349 363 tkt['status'] = 'new' 350 351 if self.get_config('notification', 'smtp_enabled') in ['true']:352 self.notification = 1353 else:354 self.notification = 0355 364 356 365 # Some defaults … … 382 391 # 383 392 self.set_owner(tkt) 384 author, email_addr =self.set_reply_fields(tkt, msg)393 self.set_reply_fields(tkt, msg) 385 394 386 395 # produce e-mail like header … … 390 399 head = self.email_header_txt(msg) 391 400 392 if self.DEBUG > 0: 393 self.debug_attachments(msg) 394 395 self.description(msg, tkt, head, author, email_addr) 401 402 body_text = self.get_body_text(msg) 403 tkt['description'] = '\n{{{\n%s\n}}}\n' %(body_text) 396 404 397 405 # Insert ticket in database … … 402 410 tkt['id'] = tkt.insert() 403 411 404 # 405 # Just how to show to update description 406 # 407 #tkt['summary'] = '#%s: %s' %(tkt['id'], tkt['summary']) 408 #tkt.save_changes(self.db, author, "") 409 410 self.attachments(msg, tkt, author) 412 # Else we do no have a ticket id for the subject line 413 # 414 mailto = '' 415 if self.MAILTO: 416 mailto = self.html_mailto_link(self.to_unicode(msg['subject']), tkt['id'], body_text) 417 tkt['description'] = '%s%s\n{{{\n%s\n}}}\n' %(head, mailto, body_text) 418 tkt.save_changes(self.db, self.author, "") 419 420 self.attachments(msg, tkt) 411 421 if self.notification: 412 422 self.notify(tkt) 413 423 414 415 def description(self, msg, tkt, head, author, email): 416 """ 417 put the message text in the ticket description 424 def get_body_text(self, msg): 425 """ 426 put the message text in the ticket description or in the changes field 418 427 message text can be plain text or html or something else 419 428 """ … … 440 449 ubody_text = unicode(body_text, charset).encode('utf-8') 441 450 442 tkt['description'] = '\n{{{\n\n%s\n}}}\n' %(ubody_text)443 444 451 elif part.get_content_type() == 'text/html': 445 tkt['description'] = '%s\n\n(see attachment for HTML mail message)\n' \ 446 %(head) 447 body_text = tkt['description'] 452 ubody_text = '\n\n(see attachment for HTML mail message)\n' 448 453 449 454 else: 450 tkt['description'] = '%s\n\n(see attachment for message)\n' %(head) 451 body_text = tkt['description'] 455 ubody_text = '\n\n(see attachment for message)\n' 452 456 453 457 has_description = 1 … … 455 459 456 460 if not has_description: 457 tkt['description'] = '%s\n\n(no plain text message, see attachments)' % head 458 has_description = 1 459 460 if self.MAILTO: 461 mailto = self.html_mailto_link(author, email, self.to_unicode(msg['subject']), ubody_text) 462 tkt['description'] = '%s\n%s %s' %(head, mailto, tkt['description']) 463 464 465 def notify(self, tkt): 461 ubody_text = '\n\n(see attachment for message)\n' 462 463 return ubody_text 464 465 def notify(self, tkt , new=True): 466 466 try: 467 467 # create false {abs_}href properties, to trick Notify() … … 474 474 tn.template_name = self.notify_template; 475 475 476 tn.notify(tkt, new ticket=True)476 tn.notify(tkt, new) 477 477 478 478 except Exception, e: … … 484 484 485 485 486 def html_mailto_link(self, author, mail_addr, subject, body):487 if not author:488 author = mail_addr486 def html_mailto_link(self, subject, id, body): 487 if not self.author: 488 author = self.mail_addr 489 489 else: 490 author = self.to_unicode( author)490 author = self.to_unicode(self.author) 491 491 492 492 # Must find a fix … … 498 498 499 499 # Temporary fix 500 str = 'mailto:%s?subject=%s' %(urllib.quote( mail_addr), urllib.quote('Re: %s' % subject))500 str = 'mailto:%s?subject=%s' %(urllib.quote(self.email_addr), urllib.quote('Re: #%s: %s' %(id, subject))) 501 501 str = '\n{{{\n#!html\n<a href="%s">Reply to: %s</a>\n}}}\n' %(str, author) 502 502 503 503 return str 504 504 505 def attachments(self, message, ticket , user):505 def attachments(self, message, ticket): 506 506 '''save any attachments as file in the ticket's directory''' 507 507 … … 522 522 filename = 'part%04d' % count 523 523 524 ext = mimetypes.guess_extension(part.get_ type())524 ext = mimetypes.guess_extension(part.get_content_type()) 525 525 if not ext: 526 526 ext = '.bin' … … 568 568 cursor.execute('INSERT INTO attachment VALUES("%s","%s","%s",%d,%d,"%s","%s","%s")' 569 569 %('ticket', urllib.quote(str(ticket['id'])), filename + '?format=raw', filesize, 570 int(time.time()),'', user, 'e-mail') )570 int(time.time()),'', self.author, 'e-mail') ) 571 571 self.db.commit() 572 572 else:
Note: See TracChangeset
for help on using the changeset viewer.