#!/usr/bin/env python
"""conf.py: (v0.3) Look at a conference and report on what's in it.
Copyright 2005 micolous.  Licensed under the terms of the GNU General Public
License version 2.0, as published by the Free Software Foundation, Inc., 
1 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

For full license text, see www.gnu.org/licenses/gpl.html
For the latest (and previous) versions of the script, see 
http://lz129.concreteairship.com/static/projects/asterisk/conf-monitor/

New in 0.3:
 - Fixed a formatting error where the channel name and flags would overlap.
"""

import telnetlib

# Settings
host = "localhost"
port = 5038
conf = 8123
username = "YOURUSERNAME"
secret = "YOURPASSWORD"
# End Settings

print """Content-Type: text/html
"""

def htmlhead():
  print """<html>
  <head>
    <title>Conference Bridge Monitor</title>
    <style type="text/css">
      html, body {
        background-color: #000;
        color: #fff;
      }
      
      table {
        border: 1px #fff solid;
        left: 2%;
        right: 2%;
        width: 96%;
        position: relative;
      }
      
      td {
        border: 1px #fff dotted;
      }
    </style>
  </head>
  
  <body>
    <h1>Asterisk Conference #""" + str(conf) + """</h1>
    """
    
def htmlfoot():
  print """
  </body>
</html>"""

try:
  mgrlnk = telnetlib.Telnet(host, port)
except:
  htmlhead()
  print "<p>There was an error trying to connect to the remote Asterisk server.  Maybe the server isn't running, or you have specified incorrect details?</p>"
  htmlfoot()
  sys.exit(0)

mgrlnk.read_until("Asterisk Call Manager",10)
mgrlnk.read_until("\n")

mgrlnk.write("Action: Login\r\n")
mgrlnk.write("Username: " + username + "\r\n")
mgrlnk.write("Secret: " + secret + "\r\n")
mgrlnk.write("\r\n")

mgrlnk.read_until("Response: ")
buf = mgrlnk.read_until("\n")[:-2]
if buf == "Error":
  htmlhead()
  print "<p>Error authenticating to Asterisk.</p>"
  print mgrlnk.read_until("\n")
  htmlfoot()
  mgrlnk.close()
  sys.exit(0)
  
# auth ok, get details of conf
mgrlnk.write("Action: Command\r\n")
mgrlnk.write("Command: meetme list " + str(conf) + "\r\n")
mgrlnk.write("\r\n")

mgrlnk.read_until("Response: ")
buf = mgrlnk.read_until("\n")[:-2]
if buf == "Follows":
  # ok
  mgrlnk.read_until("Privilege: Command\r\n")
  buf = mgrlnk.read_until("--END COMMAND--")[:-len("--END COMMAND--")]
  mgrlnk.close()
  htmlhead()
  if buf == "No active conferences.\n" or buf == "No such conference: " + str(conf) + ".\n":
    print "<p>There isn't anyone in the conference at this time.  Maybe you should ask some people on the IRC channel...</p>"
  else:
    print "<!-- " + buf + " -->"
    bufl = buf.split("\n")
    ucount = len(bufl)-2
    if ucount > 1:
      print "<p>There are <b>" + str(ucount) + "</b> participants in the conference.</p>"
    else:
      print "<p>There is only <b>1</b> participant in the conference.  What a loser. :)</p>"

    print "<table>"
    print "<tr><th>User</th><th>Caller ID</th><th>Name</th><th>Channel</th><th>Flags</th></tr>"
    for line in bufl[:len(bufl)-2]:
      userno = line[8:10]
      cid = line[11:23].strip().strip("<>&")
      cidname = line[24:44].strip().strip("<>&")
      channel = line[54:].split(" ")[0].strip()
      flagsList = line[54:].split(" ")[1:]
      flags = ""
      for flag in flagsList:
        flags = flags + " " + flag.strip()
      print "<tr><td>" + userno + "</td><td>" + cid + "</td><td>" + cidname + "</td><td>" + channel + "</td><td>" + flags + "</td></tr>"
      
    print "</table>"
  htmlfoot()
else:
  mgrlnk.close()
  htmlhead()
  print "Access denied to Asterisk Manager"
  htmlfoot()

