comparison sat_frontends/jp/cmd_message.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/cmd_message.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat_frontends.jp import base
21 import sys
22 from sat.core.i18n import _
23 from sat.core.constants import Const as C
24 from sat.tools.utils import clean_ustr
25
26 __commands__ = ["Message"]
27
28
29 class Send(base.CommandBase):
30
31 def __init__(self, host):
32 super(Send, self).__init__(host, 'send', help=_('send a message to a contact'))
33
34 def add_parser_options(self):
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=_(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=_(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=_(u"subject of the message"))
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"))
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"))
45
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
51 jids = self.host.check_jids([self.args.jid])
52 jid = jids[0]
53 self.sendStdin(jid)
54
55 def sendStdin(self, dest_jid):
56 """Send incomming data on stdin to jabber contact
57
58 @param dest_jid: destination jid
59 """
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 = {}
63 if self.args.subject is None:
64 subject = {}
65 else:
66 subject = {self.args.subject_lang: self.args.subject}
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
75 if self.args.separate: #we send stdin in several messages
76 if header:
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)
78
79 for line in stdin_lines:
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)
81
82 else:
83 msg = {self.args.lang: header + clean_ustr(u"".join(stdin_lines))} if not (self.args.xhtml or self.args.rich) else {}
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)
85
86
87 class Message(base.CommandBase):
88 subcommands = (Send, )
89
90 def __init__(self, host):
91 super(Message, self).__init__(host, 'message', use_profile=False, help=_('messages handling'))