view 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
line wrap: on
line source

#! /usr/bin/python
# -*- coding: utf-8 -*-

# jp: a SAT command line tool
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

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)