comparison frontends/src/jp/message.py @ 814:59c7bc51c323

jp: refactoring using ArgParse
author Dal <kedals0@gmail.com>
date Wed, 05 Feb 2014 14:35:26 +0100
parents frontends/src/jp/jp@1fe00f0c9a91
children f8d534ed1d1e
comparison
equal deleted inserted replaced
813:1a1600491d9d 814:59c7bc51c323
1 import base
2 import sys
3 from sat.core.i18n import _
4
5 message_parser = base.subparser.add_parser('message', help = "Send a message to a contact")
6 message_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@',
7 help=_("Use PROFILE profile key (default: %(default)s)"))
8
9 message_parser.add_argument("-s", "--separate", action="store_true", default=False,
10 help=_("Separate xmpp messages: send one message per line instead of one message alone."))
11 message_parser.add_argument("-n", "--new-line", action="store_true", default=False,
12 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))"))
13 message_parser.add_argument("jid", type=str,
14 help=_("The destination jid"))
15
16 def launch(args):
17 profile = args.profile
18 jp = Message(profile, args.jid, args.new_line, args.separate)
19 jp.go()
20
21 message_parser.set_defaults(func=launch)
22
23 class Message(base.JPWithProfile):
24 def __init__(self, profile, dest_jid, new_line = False, separate = False):
25 base.JPWithProfile.__init__(self,profile)
26 self.dest_jid = dest_jid
27 self.new_line = new_line
28 self.separate = separate
29
30
31 def send_stdin(self, dest_jid , new_line = False, separate = False):
32 """Send incomming data on stdin to jabber contact"""
33 header = "\n" if new_line else ""
34
35 if separate: #we send stdin in several messages
36 if header:
37 self.bridge.sendMessage(dest_jid, header, profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore)
38 while (True):
39 line = base.clean_ustr(sys.stdin.readline().decode('utf-8','ignore'))
40 if not line:
41 break
42 self.bridge.sendMessage(dest_jid, line.replace("\n",""), profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore)
43 else:
44 self.bridge.sendMessage(dest_jid, header + base.clean_ustr(u"".join([stream.decode('utf-8','ignore') for stream in sys.stdin.readlines()])), profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore)
45
46 def run(self):
47 jids = self.check_jids([self.dest_jid])
48 jid = jids[0]
49 self.send_stdin(jid, self.new_line, self.separate)