Debian Codegraph @

Xpjproject/pjsip-apps/src/python/samples/presence.py

raw symbols SCIP
1 #
2 # Presence and instant messaging
3 #
4 # Copyright (C) 2003-2008 Benny Prijono <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 import sys
21 import pjsua as pj
22
23 LOG_LEVEL = 3
24 pending_pres = None
25 pending_uri = None
26
27 def log_cb(level, str, len):
28 print str,
29
30 class MyAccountCallback(pj.AccountCallback):
31 def __init__(self, account=None):
32 pj.AccountCallback.__init__(self, account)
33
34 def on_incoming_subscribe(self, buddy, from_uri, contact_uri, pres):
35 global pending_pres, pending_uri
36 # Allow buddy to subscribe to our presence
37 if buddy:
38 return (200, None)
39 print 'Incoming SUBSCRIBE request from', from_uri
40 print 'Press "A" to accept and add, "R" to reject the request'
41 pending_pres = pres
42 pending_uri = from_uri
43 return (202, None)
44
45
46 class MyBuddyCallback(pj.BuddyCallback):
47 def __init__(self, buddy=None):
48 pj.BuddyCallback.__init__(self, buddy)
49
50 def on_state(self):
51 print "Buddy", self.buddy.info().uri, "is",
52 print self.buddy.info().online_text
53
54 def on_pager(self, mime_type, body):
55 print "Instant message from", self.buddy.info().uri,
56 print "(", mime_type, "):"
57 print body
58
59 def on_pager_status(self, body, im_id, code, reason):
60 if code >= 300:
61 print "Message delivery failed for message",
62 print body, "to", self.buddy.info().uri, ":", reason
63
64 def on_typing(self, is_typing):
65 if is_typing:
66 print self.buddy.info().uri, "is typing"
67 else:
68 print self.buddy.info().uri, "stops typing"
69
70
71 lib = pj.Lib()
72
73 try:
74 # Init library with default config and some customized
75 # logging config.
76 lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
77
78 # Create UDP transport which listens to any available port
79 transport = lib.create_transport(pj.TransportType.UDP,
80 pj.TransportConfig(0))
81 print "\nListening on", transport.info().host,
82 print "port", transport.info().port, "\n"
83
84 # Start the library
85 lib.start()
86
87 # Create local account
88 acc = lib.create_account_for_transport(transport, cb=MyAccountCallback())
89 acc.set_basic_status(True)
90
91 my_sip_uri = "sip:" + transport.info().host + \
92 ":" + str(transport.info().port)
93
94 buddy = None
95
96 # Menu loop
97 while True:
98 print "My SIP URI is", my_sip_uri
99 print "Menu: a=add buddy, d=delete buddy, t=toggle", \
100 " online status, i=send IM, q=quit"
101
102 input = sys.stdin.readline().rstrip("\r\n")
103 if input == "a":
104 # Add buddy
105 print "Enter buddy URI: ",
106 input = sys.stdin.readline().rstrip("\r\n")
107 if input == "":
108 continue
109
110 buddy = acc.add_buddy(input, cb=MyBuddyCallback())
111 buddy.subscribe()
112
113 elif input == "t":
114 acc.set_basic_status(not acc.info().online_status)
115
116 elif input == "i":
117 if not buddy:
118 print "Add buddy first"
119 continue
120
121 buddy.send_typing_ind(True)
122
123 print "Type the message: ",
124 input = sys.stdin.readline().rstrip("\r\n")
125 if input == "":
126 buddy.send_typing_ind(False)
127 continue
128
129 buddy.send_pager(input)
130
131 elif input == "d":
132 if buddy:
133 buddy.delete()
134 buddy = None
135 else:
136 print 'No buddy was added'
137
138 elif input == "A":
139 if pending_pres:
140 acc.pres_notify(pending_pres, pj.SubscriptionState.ACTIVE)
141 buddy = acc.add_buddy(pending_uri, cb=MyBuddyCallback())
142 buddy.subscribe()
143 pending_pres = None
144 pending_uri = None
145 else:
146 print "No pending request"
147
148 elif input == "R":
149 if pending_pres:
150 acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED,
151 "rejected")
152 pending_pres = None
153 pending_uri = None
154 else:
155 print "No pending request"
156
157 elif input == "q":
158 break
159
160 # Shutdown the library
161 acc.delete()
162 acc = None
163 if pending_pres:
164 acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED,
165 "rejected")
166 transport = None
167 lib.destroy()
168 lib = None
169
170 except pj.Error, e:
171 print "Exception: " + str(e)
172 lib.destroy()
173 lib = None
174