Benny Prijono | b85ba65 | 2008-07-11 00:55:22 +0000 | [diff] [blame] | 1 | # $Id$ |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 2 | # |
| 3 | # Presence and instant messaging |
| 4 | # |
| 5 | # Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 6 | # |
| 7 | import sys |
| 8 | import pjsua as pj |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 9 | |
| 10 | LOG_LEVEL = 3 |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 11 | pending_pres = None |
| 12 | pending_uri = None |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 13 | |
| 14 | def log_cb(level, str, len): |
| 15 | print str, |
| 16 | |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 17 | class MyAccountCallback(pj.AccountCallback): |
| 18 | def __init__(self, account=None): |
| 19 | pj.AccountCallback.__init__(self, account) |
| 20 | |
| 21 | def on_incoming_subscribe(self, buddy, from_uri, contact_uri, pres): |
| 22 | global pending_pres, pending_uri |
| 23 | # Allow buddy to subscribe to our presence |
| 24 | if buddy: |
| 25 | return (200, None) |
| 26 | print 'Incoming SUBSCRIBE request from', from_uri |
| 27 | print 'Press "A" to accept and add, "R" to reject the request' |
| 28 | pending_pres = pres |
| 29 | pending_uri = from_uri |
| 30 | return (202, None) |
| 31 | |
| 32 | |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 33 | class MyBuddyCallback(pj.BuddyCallback): |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 34 | def __init__(self, buddy=None): |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 35 | pj.BuddyCallback.__init__(self, buddy) |
| 36 | |
| 37 | def on_state(self): |
| 38 | print "Buddy", self.buddy.info().uri, "is", |
| 39 | print self.buddy.info().online_text |
| 40 | |
| 41 | def on_pager(self, mime_type, body): |
| 42 | print "Instant message from", self.buddy.info().uri, |
| 43 | print "(", mime_type, "):" |
| 44 | print body |
| 45 | |
| 46 | def on_pager_status(self, body, im_id, code, reason): |
| 47 | if code >= 300: |
| 48 | print "Message delivery failed for message", |
| 49 | print body, "to", self.buddy.info().uri, ":", reason |
| 50 | |
| 51 | def on_typing(self, is_typing): |
| 52 | if is_typing: |
| 53 | print self.buddy.info().uri, "is typing" |
| 54 | else: |
| 55 | print self.buddy.info().uri, "stops typing" |
| 56 | |
| 57 | |
| 58 | lib = pj.Lib() |
| 59 | |
| 60 | try: |
| 61 | # Init library with default config and some customized |
| 62 | # logging config. |
| 63 | lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) |
| 64 | |
| 65 | # Create UDP transport which listens to any available port |
| 66 | transport = lib.create_transport(pj.TransportType.UDP, |
| 67 | pj.TransportConfig(0)) |
| 68 | print "\nListening on", transport.info().host, |
| 69 | print "port", transport.info().port, "\n" |
| 70 | |
| 71 | # Start the library |
| 72 | lib.start() |
| 73 | |
| 74 | # Create local account |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 75 | acc = lib.create_account_for_transport(transport, cb=MyAccountCallback()) |
| 76 | acc.set_basic_status(True) |
| 77 | |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 78 | my_sip_uri = "sip:" + transport.info().host + \ |
| 79 | ":" + str(transport.info().port) |
| 80 | |
| 81 | buddy = None |
| 82 | |
| 83 | # Menu loop |
| 84 | while True: |
| 85 | print "My SIP URI is", my_sip_uri |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 86 | print "Menu: a=add buddy, d=delete buddy, t=toggle", \ |
| 87 | " online status, i=send IM, q=quit" |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 88 | |
| 89 | input = sys.stdin.readline().rstrip("\r\n") |
| 90 | if input == "a": |
| 91 | # Add buddy |
| 92 | print "Enter buddy URI: ", |
| 93 | input = sys.stdin.readline().rstrip("\r\n") |
| 94 | if input == "": |
| 95 | continue |
| 96 | |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 97 | buddy = acc.add_buddy(input, cb=MyBuddyCallback()) |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 98 | buddy.subscribe() |
| 99 | |
| 100 | elif input == "t": |
| 101 | acc.set_basic_status(not acc.info().online_status) |
| 102 | |
| 103 | elif input == "i": |
| 104 | if not buddy: |
| 105 | print "Add buddy first" |
| 106 | continue |
| 107 | |
| 108 | buddy.send_typing_ind(True) |
| 109 | |
| 110 | print "Type the message: ", |
| 111 | input = sys.stdin.readline().rstrip("\r\n") |
| 112 | if input == "": |
| 113 | buddy.send_typing_ind(False) |
| 114 | continue |
| 115 | |
| 116 | buddy.send_pager(input) |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 117 | |
| 118 | elif input == "d": |
| 119 | if buddy: |
| 120 | buddy.delete() |
| 121 | buddy = None |
| 122 | else: |
| 123 | print 'No buddy was added' |
| 124 | |
| 125 | elif input == "A": |
| 126 | if pending_pres: |
| 127 | acc.pres_notify(pending_pres, pj.SubscriptionState.ACTIVE) |
| 128 | buddy = acc.add_buddy(pending_uri, cb=MyBuddyCallback()) |
| 129 | buddy.subscribe() |
| 130 | pending_pres = None |
| 131 | pending_uri = None |
| 132 | else: |
| 133 | print "No pending request" |
| 134 | |
| 135 | elif input == "R": |
| 136 | if pending_pres: |
| 137 | acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED, |
| 138 | "rejected") |
| 139 | pending_pres = None |
| 140 | pending_uri = None |
| 141 | else: |
| 142 | print "No pending request" |
| 143 | |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 144 | elif input == "q": |
| 145 | break |
| 146 | |
| 147 | # Shutdown the library |
Benny Prijono | 5504045 | 2008-07-21 18:20:57 +0000 | [diff] [blame] | 148 | acc.delete() |
| 149 | acc = None |
| 150 | if pending_pres: |
| 151 | acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED, |
| 152 | "rejected") |
| 153 | transport = None |
Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame] | 154 | lib.destroy() |
| 155 | lib = None |
| 156 | |
| 157 | except pj.Error, e: |
| 158 | print "Exception: " + str(e) |
| 159 | lib.destroy() |
| 160 | lib = None |
| 161 | |