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

# Source Code for Module pyxmpp.jabber.simple

     1  #
     2  # (C) Copyright 2005-2006 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  __revision__="$Id: client.py 528 2005-01-20 21:14:53Z jajcus $"
    22  __docformat__="restructuredtext en"
    23
    24 -def xmpp_do(jid,password,function,server=None,port=None):
    25      """Connect as client to a Jabber/XMPP server and call the provided
    26      function when stream is ready for IM. The function will be called
    27      with one argument -- the XMPP stream. After function returns the stream is
    28      closed."""
    29      from pyxmpp.jabber.client import JabberClient
    30      class Client(JabberClient):
    31          """The simplest client implementation."""
    32          def session_started(self):
    33              """Call the function provided when the session starts and exit."""
    34              function(self.stream)
    35              self.disconnect()
    36      c=Client(jid,password,server=server,port=port)
    37      c.connect()
    38      try:
    39          c.loop(1)
    40      except KeyboardInterrupt:
    41          print u"disconnecting..."
    42          c.disconnect()
    43
    44 -def send_message(my_jid, my_password, to_jid, body, subject=None,
    45          message_type=None, server=None, port=None):
    46      """Star an XMPP session and send a message, then exit.
    47
    48      :Parameters:
    49          - `my_jid`: sender JID.
    50          - `my_password`: sender password.
    51          - `to_jid`: recipient JID.
    52          - `body`: message body.
    53          - `subject`: message subject.
    54          - `message_type`: message type.
    55          - `server`: server to connect to (default: derivied from `my_jid` using
    56            DNS records).
    57          - `port`: TCP port number to connect to (default: retrieved using SRV
    58            DNS record, or 5222).
    59      :Types:
    60          - `my_jid`: `pyxmpp.jid.JID`
    61          - `my_password`: `unicode`
    62          - `to_jid`: `pyxmpp.jid.JID`
    63          - `body`: `unicode`
    64          - `subject`: `unicode`
    65          - `message_type`: `str`
    66          - `server`: `unicode` or `str`
    67          - `port`: `int`
    68      """
    69      from pyxmpp.message import Message
    70      msg=Message(to_jid=to_jid,body=body,subject=subject,stanza_type=message_type)
    71      def fun(stream):
    72          """Send a mesage `msg` via a stream."""
    73          stream.send(msg)
    74      xmpp_do(my_jid,my_password,fun,server,port)
    75
    76  # vi: sts=4 et sw=4
    77