pyxmpp.jabber.simple

Package pyxmpp :: Package jabber :: Module simple |
[hide private]

# Source Code for Module pyxmpp.jabber.simple

     1  #
     2  # (C) Copyright 2005-2010 Jacek Konieczny <jajcus@jajcus.net>
     3  #
     4  # This program is free software; you can redistribute it and/or modify
     5  # it under the terms of the GNU Lesser General Public License Version
     6  # 2.1 as published by the Free Software Foundation.
     7  #
     8  # This program is distributed in the hope that it will be useful,
     9  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  # GNU Lesser General Public License for more details.
    12  #
    13  # You should have received a copy of the GNU Lesser General Public
    14  # License along with this program; if not, write to the Free Software
    15  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    16  #
    17  # pylint: disable-msg=W0232, E0201
    18
    19  """Simple API for simple things like sendig messages or single stanzas."""
    20
    21  __docformat__="restructuredtext en"
    22
    23 -def xmpp_do(jid,password,function,server=None,port=None):
    24      """Connect as client to a Jabber/XMPP server and call the provided
    25      function when stream is ready for IM. The function will be called
    26      with one argument -- the XMPP stream. After function returns the stream is
    27      closed."""
    28      from pyxmpp.jabber.client import JabberClient
    29      class Client(JabberClient):
    30          """The simplest client implementation."""
    31          def session_started(self):
    32              """Call the function provided when the session starts and exit."""
    33              function(self.stream)
    34              self.disconnect()
    35      c=Client(jid,password,server=server,port=port)
    36      c.connect()
    37      try:
    38          c.loop(1)
    39      except KeyboardInterrupt:
    40          print u"disconnecting..."
    41          c.disconnect()
    42
    43 -def send_message(my_jid, my_password, to_jid, body, subject=None,
    44          message_type=None, server=None, port=None):
    45      """Star an XMPP session and send a message, then exit.
    46
    47      :Parameters:
    48          - `my_jid`: sender JID.
    49          - `my_password`: sender password.
    50          - `to_jid`: recipient JID.
    51          - `body`: message body.
    52          - `subject`: message subject.
    53          - `message_type`: message type.
    54          - `server`: server to connect to (default: derivied from `my_jid` using
    55            DNS records).
    56          - `port`: TCP port number to connect to (default: retrieved using SRV
    57            DNS record, or 5222).
    58      :Types:
    59          - `my_jid`: `pyxmpp.jid.JID`
    60          - `my_password`: `unicode`
    61          - `to_jid`: `pyxmpp.jid.JID`
    62          - `body`: `unicode`
    63          - `subject`: `unicode`
    64          - `message_type`: `str`
    65          - `server`: `unicode` or `str`
    66          - `port`: `int`
    67      """
    68      from pyxmpp.message import Message
    69      msg=Message(to_jid=to_jid,body=body,subject=subject,stanza_type=message_type)
    70      def fun(stream):
    71          """Send a mesage `msg` via a stream."""
    72          stream.send(msg)
    73      xmpp_do(my_jid,my_password,fun,server,port)
    74
    75  # vi: sts=4 et sw=4
    76