/[svn.andrew.net.au]/asterisk/c2c.py
ViewVC logotype

Annotation of /asterisk/c2c.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 48 - (hide annotations)
Mon Sep 7 00:51:17 2009 UTC (15 years, 1 month ago) by apollock
File MIME type: text/x-python
File size: 4238 byte(s)
Initial commit of home-grown click to call interface

1 apollock 48 #!/usr/bin/python
2    
3     import cgi, Cookie
4     import cgitb; cgitb.enable()
5     import Asterisk.Manager
6     import time
7     import os
8    
9     _header_printed = False
10    
11     def print_header(headers=None, cookies=None):
12     """Spit out the required headers for a CGI script. Optionally set cookies"""
13    
14     global _header_printed
15     if not _header_printed:
16     print "Content-type: text/html"
17     if cookies:
18     print cookies
19     if headers:
20     for header in headers.keys():
21     print "%s: %s" % (header, headers.get(header))
22     print "\n"
23     _header_printed = True
24    
25    
26     def make_options(options={}, default=""):
27     html = ""
28     individual_options = []
29     for optionvalue in options.keys():
30     if optionvalue == default:
31     individual_options.append("""<option value="%s"%s>%s</option>""" % (optionvalue, " selected", options.get(optionvalue)))
32     else:
33     individual_options.append("""<option value="%s"%s>%s</option>""" % (optionvalue, "", options.get(optionvalue)))
34     html = "".join(individual_options)
35     return html
36    
37    
38     def display_form(default_source="", default_destination="", default_transport="", remember=False):
39     """Display the form HTML, optionally set default values"""
40    
41     print_header()
42     if remember:
43     remember = " checked"
44     print """<html>
45     <head>
46     <title>Click to call</title>
47     </head>
48     <body>
49     <h1>Asterisk click to call gateway</h1>
50     <form method="post">
51     <table>
52     <tr>
53     <td>Your number</td><td><input type="text" name="source" value="%(default_source)s"></td><td>via <select name="transport">%(options)s</select> (as dialled via the transport)</td>
54     </tr>
55     <tr>
56     <td>Number to call</td><td><input type="text" name="destination" value="%(default_destination)s"></td><td>(as dialled internally)</td>
57     </tr>
58     <tr>
59     <td colspan=3><input type="checkbox" name="save_source"%(remember)s> Remember my number and transport</td>
60     </tr>
61     <tr>
62     <td colspan=3><input type="submit" name="submit" value="Place call"><input type="reset" value="Clear"></td>
63     </tr>
64     </table>
65     </form>
66     </body>
67     </html>
68     """ % { 'default_source': default_source,
69     'default_destination': default_destination,
70     'options': make_options({"Zap/g3": "US Landline", "SIP/engin": "Australian SIP", "SIP": "Internal SIP extension"}, default_transport),
71     'remember': remember
72     }
73    
74    
75     def process_form(form):
76     """Process a submitted form"""
77    
78     if "source" not in form.keys():
79     display_form()
80     elif "destination" not in form.keys():
81     display_form(default_source=form.getfirst("source"))
82     else:
83     cookie = Cookie.SimpleCookie()
84     if "save_source" in form.keys():
85     cookie["source"] = form.getfirst("source")
86     cookie["source"]["expires"] = time.asctime(tuple([time.gmtime()[0]+1] + list(time.gmtime()[1:])))
87     cookie["transport"] = form.getfirst("transport")
88     cookie["transport"]["expires"] = cookie["source"]["expires"]
89     else:
90     cookie.load(os.environ.get("HTTP_COOKIE", ""))
91     if "source" in cookie:
92     cookie["source"] = ""
93     cookie["source"]["expires"] = time.asctime(time.gmtime())
94     if "transport" in cookie:
95     cookie["transport"] = ""
96     cookie["transport"]["expires"] = time.asctime(time.gmtime())
97     print_header(cookies=cookie)
98     print "<h1>Asterisk click to call gateway</h1>"
99     manager = None
100     try:
101     manager = Asterisk.Manager.Manager(("localhost", 5038), "apache", "letmein")
102     except Exception, e:
103     print "Sorry, got an exception (%s) when trying to connect to Asterisk" % e
104     #result = { "Message": "Simulating", "Response": "Success" }
105     if manager:
106     try:
107     result = manager.Originate(channel="%s/%s" % (form.getfirst("transport"), form.getfirst("source")), context="internal-ctc", extension="%s" % form.getfirst("destination"), priority=1)
108     if result:
109     print "%(Response)s: %(Message)s" % result
110     except Exception, e:
111     print "Got an exception: %s" % e
112    
113    
114     if __name__ == "__main__":
115     form = cgi.FieldStorage()
116     if not(form.has_key("submit")):
117     print_header()
118     cookies = Cookie.SimpleCookie()
119     cookies.load(os.environ.get("HTTP_COOKIE", ""))
120     default_source=cookies.get("source", "")
121     default_transport=cookies.get("transport", "")
122     if default_source:
123     default_source = default_source.value
124     if default_transport:
125     default_transport = default_transport.value
126     display_form(default_source=default_source, default_transport=default_transport, remember="source" in cookies)
127     else:
128     process_form(form)

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.22