Mercurial > libervia-backend
comparison frontends/src/jp/cmd_message.py @ 2081:2265d9df4cfb
jp (message/send): message/send can now send XHTML (-x) or rich (-r) messages
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 25 Sep 2016 19:18:45 +0200 |
parents | a2bc5089c2eb |
children | 8b37a62336c3 |
comparison
equal
deleted
inserted
replaced
2080:3626b2813158 | 2081:2265d9df4cfb |
---|---|
30 | 30 |
31 def __init__(self, host): | 31 def __init__(self, host): |
32 super(Send, self).__init__(host, 'send', help=_('send a message to a contact')) | 32 super(Send, self).__init__(host, 'send', help=_('send a message to a contact')) |
33 | 33 |
34 def add_parser_options(self): | 34 def add_parser_options(self): |
35 self.parser.add_argument("-l", "--lang", type=str, default='', help=_("language of the message")) | 35 self.parser.add_argument("-l", "--lang", type=str, default='', help=_(u"language of the message")) |
36 self.parser.add_argument("-s", "--separate", action="store_true", help=_("separate xmpp messages: send one message per line instead of one message alone.")) | 36 self.parser.add_argument("-s", "--separate", action="store_true", help=_(u"separate xmpp messages: send one message per line instead of one message alone.")) |
37 self.parser.add_argument("-n", "--new-line", action="store_true", help=_("add a new line at the beginning of the input (usefull for ascii art ;))")) | 37 self.parser.add_argument("-n", "--new-line", action="store_true", help=_(u"add a new line at the beginning of the input (usefull for ascii art ;))")) |
38 self.parser.add_argument("-S", "--subject", type=base.unicode_decoder, help=_("subject of the message")) | 38 self.parser.add_argument("-S", "--subject", type=base.unicode_decoder, help=_(u"subject of the message")) |
39 self.parser.add_argument("-L", "--subject_lang", type=str, default='', help=_("language of subject")) | 39 self.parser.add_argument("-L", "--subject_lang", type=str, default='', help=_(u"language of subject")) |
40 self.parser.add_argument("-t", "--type", choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,), default=C.MESS_TYPE_AUTO, help=_("type of the message")) | 40 self.parser.add_argument("-t", "--type", choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,), default=C.MESS_TYPE_AUTO, help=_("type of the message")) |
41 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid")) | 41 syntax = self.parser.add_mutually_exclusive_group() |
42 syntax.add_argument("-x", "--xhtml", action="store_true", help=_(u"XHTML body")) | |
43 syntax.add_argument("-r", "--rich", action="store_true", help=_(u"rich body")) | |
44 self.parser.add_argument("jid", type=base.unicode_decoder, help=_(u"the destination jid")) | |
42 | 45 |
43 def start(self): | 46 def start(self): |
47 if self.args.xhtml and self.args.separate: | |
48 self.disp(u"argument -s/--separate is not compatible yet with argument -x/--xhtml", error=True) | |
49 self.host.quit(2) | |
50 | |
44 jids = self.host.check_jids([self.args.jid]) | 51 jids = self.host.check_jids([self.args.jid]) |
45 jid = jids[0] | 52 jid = jids[0] |
46 self.sendStdin(jid) | 53 self.sendStdin(jid) |
47 | 54 |
48 def sendStdin(self, dest_jid): | 55 def sendStdin(self, dest_jid): |
49 """Send incomming data on stdin to jabber contact | 56 """Send incomming data on stdin to jabber contact |
50 | 57 |
51 @param dest_jid: destination jid | 58 @param dest_jid: destination jid |
52 """ | 59 """ |
53 header = "\n" if self.args.new_line else "" | 60 header = "\n" if self.args.new_line else "" |
61 stdin_lines = [stream.decode('utf-8','ignore') for stream in sys.stdin.readlines()] | |
62 extra = {} | |
54 if self.args.subject is None: | 63 if self.args.subject is None: |
55 subject = {} | 64 subject = {} |
56 else: | 65 else: |
57 subject = {self.args.subject_lang: self.args.subject} | 66 subject = {self.args.subject_lang: self.args.subject} |
58 | 67 |
68 if self.args.xhtml or self.args.rich: | |
69 key = u"xhtml" if self.args.xhtml else u"rich" | |
70 if self.args.lang: | |
71 key = u"{}_{}".format(key, self.args.lang) | |
72 extra[key] = clean_ustr(u"".join(stdin_lines)) | |
73 stdin_lines = [] | |
74 | |
59 if self.args.separate: #we send stdin in several messages | 75 if self.args.separate: #we send stdin in several messages |
60 | |
61 if header: | 76 if header: |
62 self.host.bridge.messageSend(dest_jid, {self.args.lang: header}, subject, self.args.type, profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore) | 77 self.host.bridge.messageSend(dest_jid, {self.args.lang: header}, subject, self.args.type, profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore) |
63 | 78 |
64 while (True): | 79 for line in stdin_lines: |
65 line = clean_ustr(sys.stdin.readline().decode('utf-8','ignore')) | 80 self.host.bridge.messageSend(dest_jid, {self.args.lang: line.replace("\n","")}, subject, self.args.type, extra, profile_key=self.host.profile, callback=lambda: None, errback=lambda ignore: ignore) |
66 if not line: | |
67 break | |
68 self.host.bridge.messageSend(dest_jid, {self.args.lang: line.replace("\n","")}, subject, self.args.type, profile_key=self.host.profile, callback=lambda: None, errback=lambda ignore: ignore) | |
69 | 81 |
70 else: | 82 else: |
71 msg = header + clean_ustr(u"".join([stream.decode('utf-8','ignore') for stream in sys.stdin.readlines()])) | 83 msg = {self.args.lang: header + clean_ustr(u"".join(stdin_lines))} if not (self.args.xhtml or self.args.rich) else {} |
72 self.host.bridge.messageSend(dest_jid, {self.args.lang: msg}, subject, self.args.type, profile_key=self.host.profile, callback=lambda: None, errback=lambda ignore: ignore) | 84 self.host.bridge.messageSend(dest_jid, msg, subject, self.args.type, extra, profile_key=self.host.profile, callback=lambda: None, errback=lambda ignore: ignore) |
73 | 85 |
74 | 86 |
75 class Message(base.CommandBase): | 87 class Message(base.CommandBase): |
76 subcommands = (Send, ) | 88 subcommands = (Send, ) |
77 | 89 |