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 __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
76