diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/src/jp/message.py	Wed Feb 05 14:35:26 2014 +0100
@@ -0,0 +1,49 @@
+import base
+import sys
+from sat.core.i18n import _
+
+message_parser = base.subparser.add_parser('message', help = "Send a message to a contact")
+message_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@',
+            help=_("Use PROFILE profile key (default: %(default)s)"))
+
+message_parser.add_argument("-s", "--separate", action="store_true", default=False,
+            help=_("Separate xmpp messages: send one message per line instead of one message alone."))
+message_parser.add_argument("-n", "--new-line", action="store_true", default=False,
+            help=_("Add a new line at the beginning of the input (usefull for ascii art ;))"))
+message_parser.add_argument("jid", type=str,
+            help=_("The destination jid"))
+
+def launch(args):
+    profile = args.profile
+    jp = Message(profile, args.jid, args.new_line, args.separate)
+    jp.go()
+
+message_parser.set_defaults(func=launch)
+
+class Message(base.JPWithProfile):
+    def __init__(self, profile, dest_jid, new_line = False, separate = False):
+        base.JPWithProfile.__init__(self,profile)
+        self.dest_jid = dest_jid
+        self.new_line = new_line
+        self.separate = separate
+
+
+    def send_stdin(self, dest_jid , new_line = False, separate = False):
+        """Send incomming data on stdin to jabber contact"""
+        header = "\n" if new_line else ""
+
+        if separate:  #we send stdin in several messages
+            if header:
+                self.bridge.sendMessage(dest_jid, header, profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore)
+            while (True):
+                line = base.clean_ustr(sys.stdin.readline().decode('utf-8','ignore'))
+                if not line:
+                    break
+                self.bridge.sendMessage(dest_jid, line.replace("\n",""), profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore)
+        else:
+            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)
+
+    def run(self):
+        jids = self.check_jids([self.dest_jid])
+        jid = jids[0]
+        self.send_stdin(jid, self.new_line, self.separate)