1
# $Id: presence.py 2171 2008-07-24 09:01:33Z bennylp $
3
# Presence and instant messaging
5
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
def log_cb(level, str, len):
31
class MyAccountCallback(pj.AccountCallback):
32
def __init__(self, account=None):
33
pj.AccountCallback.__init__(self, account)
35
def on_incoming_subscribe(self, buddy, from_uri, contact_uri, pres):
36
global pending_pres, pending_uri
37
# Allow buddy to subscribe to our presence
40
print 'Incoming SUBSCRIBE request from', from_uri
41
print 'Press "A" to accept and add, "R" to reject the request'
43
pending_uri = from_uri
47
class MyBuddyCallback(pj.BuddyCallback):
48
def __init__(self, buddy=None):
49
pj.BuddyCallback.__init__(self, buddy)
52
print "Buddy", self.buddy.info().uri, "is",
53
print self.buddy.info().online_text
55
def on_pager(self, mime_type, body):
56
print "Instant message from", self.buddy.info().uri,
57
print "(", mime_type, "):"
60
def on_pager_status(self, body, im_id, code, reason):
62
print "Message delivery failed for message",
63
print body, "to", self.buddy.info().uri, ":", reason
65
def on_typing(self, is_typing):
67
print self.buddy.info().uri, "is typing"
69
print self.buddy.info().uri, "stops typing"
75
# Init library with default config and some customized
77
lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
79
# Create UDP transport which listens to any available port
80
transport = lib.create_transport(pj.TransportType.UDP,
81
pj.TransportConfig(0))
82
print "\nListening on", transport.info().host,
83
print "port", transport.info().port, "\n"
88
# Create local account
89
acc = lib.create_account_for_transport(transport, cb=MyAccountCallback())
90
acc.set_basic_status(True)
92
my_sip_uri = "sip:" + transport.info().host + \
93
":" + str(transport.info().port)
99
print "My SIP URI is", my_sip_uri
100
print "Menu: a=add buddy, d=delete buddy, t=toggle", \
101
" online status, i=send IM, q=quit"
103
input = sys.stdin.readline().rstrip("\r\n")
106
print "Enter buddy URI: ",
107
input = sys.stdin.readline().rstrip("\r\n")
111
buddy = acc.add_buddy(input, cb=MyBuddyCallback())
115
acc.set_basic_status(not acc.info().online_status)
119
print "Add buddy first"
122
buddy.send_typing_ind(True)
124
print "Type the message: ",
125
input = sys.stdin.readline().rstrip("\r\n")
127
buddy.send_typing_ind(False)
130
buddy.send_pager(input)
137
print 'No buddy was added'
141
acc.pres_notify(pending_pres, pj.SubscriptionState.ACTIVE)
142
buddy = acc.add_buddy(pending_uri, cb=MyBuddyCallback())
147
print "No pending request"
151
acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED,
156
print "No pending request"
161
# Shutdown the library
165
acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED,
172
print "Exception: " + str(e)