Mercurial > libervia-backend
annotate frontends/src/jp/message.py @ 815:f8d534ed1d1e
jp: added missing license headers
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 05 Feb 2014 14:52:38 +0100 |
parents | 59c7bc51c323 |
children |
rev | line source |
---|---|
815 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SAT command line tool | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 | |
814 | 20 import base |
21 import sys | |
771 | 22 from sat.core.i18n import _ |
402
f03688bdb858
jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents:
401
diff
changeset
|
23 |
814 | 24 message_parser = base.subparser.add_parser('message', help = "Send a message to a contact") |
25 message_parser.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', | |
26 help=_("Use PROFILE profile key (default: %(default)s)")) | |
0 | 27 |
814 | 28 message_parser.add_argument("-s", "--separate", action="store_true", default=False, |
29 help=_("Separate xmpp messages: send one message per line instead of one message alone.")) | |
30 message_parser.add_argument("-n", "--new-line", action="store_true", default=False, | |
31 help=_("Add a new line at the beginning of the input (usefull for ascii art ;))")) | |
32 message_parser.add_argument("jid", type=str, | |
33 help=_("The destination jid")) | |
0 | 34 |
814 | 35 def launch(args): |
36 profile = args.profile | |
37 jp = Message(profile, args.jid, args.new_line, args.separate) | |
38 jp.go() | |
0 | 39 |
814 | 40 message_parser.set_defaults(func=launch) |
0 | 41 |
814 | 42 class Message(base.JPWithProfile): |
43 def __init__(self, profile, dest_jid, new_line = False, separate = False): | |
44 base.JPWithProfile.__init__(self,profile) | |
45 self.dest_jid = dest_jid | |
46 self.new_line = new_line | |
47 self.separate = separate | |
393 | 48 |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
49 |
814 | 50 def send_stdin(self, dest_jid , new_line = False, separate = False): |
51 """Send incomming data on stdin to jabber contact""" | |
52 header = "\n" if new_line else "" | |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
53 |
814 | 54 if separate: #we send stdin in several messages |
0 | 55 if header: |
814 | 56 self.bridge.sendMessage(dest_jid, header, profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore) |
0 | 57 while (True): |
814 | 58 line = base.clean_ustr(sys.stdin.readline().decode('utf-8','ignore')) |
0 | 59 if not line: |
60 break | |
814 | 61 self.bridge.sendMessage(dest_jid, line.replace("\n",""), profile_key=self.profile, callback=lambda: None, errback=lambda ignore: ignore) |
0 | 62 else: |
814 | 63 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) |
0 | 64 |
814 | 65 def run(self): |
66 jids = self.check_jids([self.dest_jid]) | |
67 jid = jids[0] | |
68 self.send_stdin(jid, self.new_line, self.separate) |