Package emma :: Package module :: Package find_email
[hide private]
[frames] | no frames]

Source Code for Package emma.module.find_email

  1  """ 
  2  find an email from irc 
  3   
  4  @copyright: (c) 2011 hackmeeting U{http://sindominio.net/hackmeeting} 
  5  @author: Ruben Pollan 
  6  @organization: hackmeeting U{http://sindominio.net/hackmeeting} 
  7  @contact: meskio@sindominio.net 
  8  @license: 
  9    This program is free software; you can redistribute it and/or 
 10    modify it under the terms of the Do What The Fuck You Want To 
 11    Public License, Version 2, as published by Sam Hocevar. See 
 12    U{http://sam.zoy.org/projects/COPYING.WTFPL} for more details. 
 13  """ 
 14   
 15  from emma.events import Event, subscribe, run_event 
 16  from emma.module import Module 
 17  from emma.complement import use_lock 
 18  from emma.interface.message import Message 
19 20 21 -class find_email(Module):
22 - def run(self):
23 self.search = {} 24 """ {channel:[email]} """ 25 26 help_event = Event(event="help", identifier=self.conf['im_id']) 27 subscribe(help_event, self.help_handler) 28 cmd_event = Event(event="command", identifier=self.conf['im_id']) 29 subscribe(cmd_event, self.cmd_handler)
30
31 - def help_handler(self, event, data):
32 if not event.interface in ["irc", "xmpp"]: 33 return "" 34 35 if not data: 36 return _(" * find From:/hackmeeting/,Tags:asamblea\n" \ 37 " Use for search on emails stored by emma\n" \ 38 " * display 0\n" \ 39 " Display an email from a search list generated\n") 40 elif data in ('find', _('find')): 41 return _("Use for search on emails stored by emma.\n" \ 42 "Search terms are introduced separated by ','" \ 43 "with the form 'Field:string',\n" \ 44 "string can be a regular expression between '/'.\n" \ 45 "Ex: find From:/meskio.*/,Tags:asamblea,Body:/squat/") 46 elif data in ('display', _('display')): 47 return _("Once a 'find' command is call use the 'display'" \ 48 "command to output the email\n" \ 49 "with the index number give as parameter of 'display'\n" \ 50 "Ex: display 0") 51 else: 52 return ""
53
54 - def cmd_handler(self, event, data):
55 interface = event.interface 56 if not interface in ["irc", "xmpp"]: 57 return 58 59 cmd, args = data[0] 60 to = data[1]['From'] 61 if interface == 'irc' and data[1]['To'][0] == '#': 62 to = data[1]['To'] 63 64 if cmd in ("find", _("find")): 65 event = Event("db", "email", self.conf['email_id']) 66 search = self.parse_args(args) 67 self.log("Find: " + str(search)) 68 res = run_event(event, search) 69 if not res or not res[0]: 70 self.say(_("Not found any email"), to, interface) 71 else: 72 self.add_search(res[0], to) 73 self.show_list(to, interface) 74 elif cmd in ("display", _("display")) and to in self.search: 75 emails = self.search[to] 76 try: 77 email_index = int(args) 78 except ValueError: 79 self.say(_("Not valid index: %s") % args, to, interface) 80 return 81 if len(emails) > email_index: 82 self.show_email(emails[email_index], to, interface) 83 else: 84 err_str = (_("Index not in range(0-%(number)d): %(args)s") % 85 {'number': len(emails) - 1, 'args': args}) 86 self.say(err_str, to, interface)
87 88 @use_lock
89 - def add_search(self, emails, channel):
90 self.search[channel] = emails
91
92 - def show_list(self, channel, interface):
93 emails = self.search[channel] 94 string = "" 95 for i, email in zip(range(len(emails)), emails): 96 date = email.get('Date', '') 97 frm = email.get('From', '') 98 sbj = email.get('Subject', '') 99 string += "%d - %s %s %s\n" % (i, date, frm, sbj) 100 self.say(string, channel, interface)
101
102 - def show_email(self, email, channel, interface):
103 string = "" 104 # We don't need keys translated when searching for them in the 105 # email, but they must be translated when presented to the user, so 106 # we define a temporal _ function to overwrite builting one. 107 # See gettext documentation on deferred translations: 108 # http://docs.python.org/library/gettext.html?highlight=gettext#deferred-translations 109 def N_(msg): return msg 110 keys = [N_('From'), N_('To'), N_('Cc'), N_('Date'), N_('Subject')] 111 for key in keys: 112 if key in email: 113 string += "%s: %s\n" % (_(key), email[key]) 114 body = [" " + line for line in email['Body'].split('\n')] 115 string += '\n'.join(body) 116 self.say(string, channel, interface)
117
118 - def say(self, msg, channel, interface):
119 event = Event(event="send", interface=interface, \ 120 identifier=self.conf['im_id']) 121 message = Message(msg, channel) 122 run_event(event, message)
123
124 - def parse_args(self, args):
125 #FIXME: improve to take care of spaces 126 splited = [item.split(":") for item in args.split(",")] 127 return dict(splited)
128