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
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
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
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
90 self.search[channel] = emails
91
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
103 string = ""
104
105
106
107
108
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):
123
125
126 splited = [item.split(":") for item in args.split(",")]
127 return dict(splited)
128