root/apps/python/trunk/telsnom.py
| Revision 543, 5.6 kB (checked in by nadya, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | #!/usr/bin/python |
| 2 | |
| 3 | # CGI script for access .tel domains from the minibrowser |
| 4 | # on Snom SIP phones. |
| 5 | |
| 6 | # Copyright (c) 2009, Andrew McDonald |
| 7 | # All rights reserved. |
| 8 | # |
| 9 | # Redistribution and use in source and binary forms, with or without |
| 10 | # modification, are permitted provided that the following conditions |
| 11 | # are met: |
| 12 | # |
| 13 | # * Redistributions of source code must retain the above copyright |
| 14 | # notice, this list of conditions and the following disclaimer. |
| 15 | # |
| 16 | # * Redistributions in binary form must reproduce the above |
| 17 | # copyright notice, this list of conditions and the following |
| 18 | # disclaimer in the documentation and/or other materials provided |
| 19 | # with the distribution. |
| 20 | # |
| 21 | # * The name of the author may not be used to endorse or promote |
| 22 | # products derived from this software without specific prior |
| 23 | # written permission. |
| 24 | # |
| 25 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 28 | # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 29 | # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 31 | # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 33 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 35 | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | # POSSIBILITY OF SUCH DAMAGE. |
| 37 | |
| 38 | import cgi |
| 39 | import telquery |
| 40 | import sys |
| 41 | import os |
| 42 | import urllib2 |
| 43 | import M2Crypto |
| 44 | |
| 45 | def print_SnomIPPhoneText(search, text): |
| 46 | print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 47 | print "<SnomIPPhoneText>" |
| 48 | print "<Title>Search %s</Title>" % search |
| 49 | print "<Prompt>.tel Search</Prompt>" |
| 50 | print "<Text>%s</Text>" % text |
| 51 | print "</SnomIPPhoneText>" |
| 52 | return |
| 53 | |
| 54 | def print_SnomIPPhoneInput(server, script, telkey, friends): |
| 55 | print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 56 | print "<SnomIPPhoneInput>" |
| 57 | print "<Title>.tel search</Title>" |
| 58 | print "<Prompt>.tel Search</Prompt>" |
| 59 | print "<URL>http://%s%s</URL>" % (server, script) |
| 60 | print "<InputItem>" |
| 61 | print "<DisplayName>.tel Search</DisplayName>" |
| 62 | if telkey and friends: |
| 63 | print "<QueryStringParam>telkey=%s&friends=%s&search</QueryStringParam>" % (urllib2.quote(telkey), urllib2.quote(friends)) |
| 64 | else: |
| 65 | print "<QueryStringParam>search</QueryStringParam>" |
| 66 | print "<DefaultValue/>" |
| 67 | print "<InputFlags>l</InputFlags>" |
| 68 | print "</InputItem>" |
| 69 | print "</SnomIPPhoneInput>" |
| 70 | return |
| 71 | |
| 72 | def print_SnomIPPhoneDirectory(search, ts): |
| 73 | print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 74 | print "<SnomIPPhoneDirectory>" |
| 75 | print "<Title>Search %s</Title>" % search |
| 76 | print "<Prompt>.tel Search</Prompt>" |
| 77 | for (_,_,t,_) in ts: |
| 78 | if t[:4] == "tel:": |
| 79 | t = t[4:] |
| 80 | print "<DirectoryEntry>" |
| 81 | print "<Name>%s</Name>" % t |
| 82 | print "<Telephone>%s</Name>" % t |
| 83 | print "</DirectoryEntry>" |
| 84 | print "</SnomIPPhoneDirectory>" |
| 85 | return |
| 86 | |
| 87 | def print_SnomIPPhoneMenu(server, script, telkey, friends, friendlist): |
| 88 | print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
| 89 | print "<SnomIPPhoneMenu>" |
| 90 | print "<Title>.tel Search</Title>" |
| 91 | for f in sorted(friendlist.keys()): |
| 92 | print "<MenuItem>" |
| 93 | print "<Name>%s</Name>" % f |
| 94 | print "<URL>http://%s%s?telkey=%s&friends=%s&search=%s</QueryStringParam>" % \ |
| 95 | (server, script, urllib2.quote(telkey), urllib2.quote(friends), f) |
| 96 | print "</MenuItem>" |
| 97 | print "</SnomIPPhoneMenu>" |
| 98 | return |
| 99 | |
| 100 | def telsnom(): |
| 101 | print "Content-Type: text/xml" # XML is following |
| 102 | print # blank line, end of headers |
| 103 | |
| 104 | form = cgi.FieldStorage(keep_blank_values=True) |
| 105 | |
| 106 | if "telkey" in form: |
| 107 | telkey = form["telkey"].value |
| 108 | if len(telkey) == 0: |
| 109 | telkey = None |
| 110 | else: |
| 111 | telkey = None |
| 112 | if "friends" in form: |
| 113 | friends = form["friends"].value |
| 114 | if len(friends) == 0: |
| 115 | friends = None |
| 116 | else: |
| 117 | friends = None |
| 118 | |
| 119 | if not "search" in form or (len(form["search"].value) == 0 and \ |
| 120 | friends == None): |
| 121 | print_SnomIPPhoneInput(os.environ["SERVER_NAME"], |
| 122 | os.environ["SCRIPT_NAME"], |
| 123 | telkey, friends) |
| 124 | return |
| 125 | |
| 126 | s = form["search"].value |
| 127 | |
| 128 | friendlist = {} |
| 129 | if friends: |
| 130 | fsplit = friends.split(";") |
| 131 | for f in fsplit: |
| 132 | f1 = f.split(",") |
| 133 | friendlist[f1[0]] = f1[1] |
| 134 | |
| 135 | if len(s) == 0: |
| 136 | if telkey and friends: |
| 137 | print_SnomIPPhoneMenu(os.environ["SERVER_NAME"], |
| 138 | os.environ["SCRIPT_NAME"], |
| 139 | telkey, friends, friendlist) |
| 140 | else: |
| 141 | print_SnomIPPhoneText("all", "No friends.") |
| 142 | return |
| 143 | |
| 144 | if not form["search"].value[-4:] == ".tel": |
| 145 | s = form["search"].value+".tel" |
| 146 | |
| 147 | if telkey: |
| 148 | priv_key = M2Crypto.RSA.load_key_string(telkey, |
| 149 | M2Crypto.util.no_passphrase_callback) |
| 150 | else: |
| 151 | priv_key = None |
| 152 | |
| 153 | ts = telquery.query(s, "sip", priv_key, friendlist) |
| 154 | ts2 = telquery.query(s, "voice:tel", priv_key, friendlist) |
| 155 | ts += ts2 |
| 156 | ts.sort() |
| 157 | |
| 158 | if len(ts) == 0: |
| 159 | print_SnomIPPhoneText(s, "No numbers found.") |
| 160 | return |
| 161 | |
| 162 | print_SnomIPPhoneDirectory(s, ts) |
| 163 | |
| 164 | return |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |
| 168 | import cgitb |
| 169 | cgitb.enable(format="text") |
| 170 | |
| 171 | telsnom() |
Note: See TracBrowser
for help on using the browser.








