Benny Prijono | 9c46114 | 2008-07-10 22:41:20 +0000 | [diff] [blame^] | 1 | # $Id:$ |
| 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 |
| 9 | import threading |
| 10 | |
| 11 | LOG_LEVEL = 3 |
| 12 | |
| 13 | def log_cb(level, str, len): |
| 14 | print str, |
| 15 | |
| 16 | class MyBuddyCallback(pj.BuddyCallback): |
| 17 | def __init__(self, buddy): |
| 18 | pj.BuddyCallback.__init__(self, buddy) |
| 19 | |
| 20 | def on_state(self): |
| 21 | print "Buddy", self.buddy.info().uri, "is", |
| 22 | print self.buddy.info().online_text |
| 23 | |
| 24 | def on_pager(self, mime_type, body): |
| 25 | print "Instant message from", self.buddy.info().uri, |
| 26 | print "(", mime_type, "):" |
| 27 | print body |
| 28 | |
| 29 | def on_pager_status(self, body, im_id, code, reason): |
| 30 | if code >= 300: |
| 31 | print "Message delivery failed for message", |
| 32 | print body, "to", self.buddy.info().uri, ":", reason |
| 33 | |
| 34 | def on_typing(self, is_typing): |
| 35 | if is_typing: |
| 36 | print self.buddy.info().uri, "is typing" |
| 37 | else: |
| 38 | print self.buddy.info().uri, "stops typing" |
| 39 | |
| 40 | |
| 41 | lib = pj.Lib() |
| 42 | |
| 43 | try: |
| 44 | # Init library with default config and some customized |
| 45 | # logging config. |
| 46 | lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) |
| 47 | |
| 48 | # Create UDP transport which listens to any available port |
| 49 | transport = lib.create_transport(pj.TransportType.UDP, |
| 50 | pj.TransportConfig(0)) |
| 51 | print "\nListening on", transport.info().host, |
| 52 | print "port", transport.info().port, "\n" |
| 53 | |
| 54 | # Start the library |
| 55 | lib.start() |
| 56 | |
| 57 | # Create local account |
| 58 | acc = lib.create_account_for_transport(transport) |
| 59 | |
| 60 | my_sip_uri = "sip:" + transport.info().host + \ |
| 61 | ":" + str(transport.info().port) |
| 62 | |
| 63 | buddy = None |
| 64 | |
| 65 | # Menu loop |
| 66 | while True: |
| 67 | print "My SIP URI is", my_sip_uri |
| 68 | print "Menu: a=add buddy, t=toggle online status, i=send IM, q=quit" |
| 69 | |
| 70 | input = sys.stdin.readline().rstrip("\r\n") |
| 71 | if input == "a": |
| 72 | # Add buddy |
| 73 | print "Enter buddy URI: ", |
| 74 | input = sys.stdin.readline().rstrip("\r\n") |
| 75 | if input == "": |
| 76 | continue |
| 77 | |
| 78 | buddy = acc.add_buddy(input) |
| 79 | cb = MyBuddyCallback(buddy) |
| 80 | buddy.set_callback(cb) |
| 81 | |
| 82 | buddy.subscribe() |
| 83 | |
| 84 | elif input == "t": |
| 85 | acc.set_basic_status(not acc.info().online_status) |
| 86 | |
| 87 | elif input == "i": |
| 88 | if not buddy: |
| 89 | print "Add buddy first" |
| 90 | continue |
| 91 | |
| 92 | buddy.send_typing_ind(True) |
| 93 | |
| 94 | print "Type the message: ", |
| 95 | input = sys.stdin.readline().rstrip("\r\n") |
| 96 | if input == "": |
| 97 | buddy.send_typing_ind(False) |
| 98 | continue |
| 99 | |
| 100 | buddy.send_pager(input) |
| 101 | |
| 102 | elif input == "q": |
| 103 | break |
| 104 | |
| 105 | # Shutdown the library |
| 106 | lib.destroy() |
| 107 | lib = None |
| 108 | |
| 109 | except pj.Error, e: |
| 110 | print "Exception: " + str(e) |
| 111 | lib.destroy() |
| 112 | lib = None |
| 113 | |