1   
 2   
 3   
 4   
 5   
 6   
 7   
 8   
 9   
10   
11   
12   
13   
14   
15   
16   
17   
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   
77