Merge "pjsip_options: wrongly applied "UNKNOWN" status"
[asterisk/asterisk.git] / static-http / astman.js
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Javascript routines or accessing manager routines over HTTP.
5  *
6  * Copyright (C) 1999 - 2006, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  *
20  */
21
22
23 function Astman() {
24         var me = this;
25         var channels = new Array;
26         var lastselect;
27         var selecttarget;
28         this.setURL = function(url) {
29                 this.url = url;
30         };
31         this.setEventCallback = function(callback) {
32                 this.eventcallback = callback;
33         };
34         this.setDebug = function(debug) {
35                 this.debug = debug;
36         };
37         this.clickChannel = function(ev) {
38                 var target = ev.target;
39                 // XXX This is icky, we statically use astmanEngine to call the callback XXX 
40                 if (me.selecttarget)
41                         me.restoreTarget(me.selecttarget);
42                 while(!target.id || !target.id.length)
43                         target=target.parentNode;
44                 me.selecttarget = target.id;
45                 target.className = "chanlistselected";
46                 me.chancallback(target.id);
47         };
48         this.restoreTarget = function(targetname) {
49                 var other;
50                 target = $(targetname);
51                 if (!target)
52                         return;
53                 if (target.previousSibling) {
54                         other = target.previousSibling.previousSibling.className;
55                 } else if (target.nextSibling) {
56                         other = target.nextSibling.nextSibling.className;
57                 }
58                 if (other) {
59                         if (other == "chanlisteven") 
60                                 target.className = "chanlistodd";
61                         else
62                                 target.className = "chanlisteven";
63                 } else
64                                 target.className = "chanlistodd";
65         };
66         this.channelUpdate = function(msg, channame) {
67                 var fields = new Array("callerid", "calleridname", "context", "extension", "priority", "account", "state", "link", "uniqueid" );
68
69                 if (!channame || !channame.length)
70                         channame = msg.headers['channel'];
71
72                 if (!channels[channame])
73                         channels[channame] = new Array();
74                         
75                 if (msg.headers.event) {
76                         if (msg.headers.event == "Hangup") {
77                                 delete channels[channame];
78                         } else if (msg.headers.event == "Link") {
79                                 var chan1 = msg.headers.channel1;
80                                 var chan2 = msg.headers.channel2;
81                                 if (chan1 && channels[chan1])
82                                         channels[chan1].link = chan2;
83                                 if (chan2 && channels[chan2])
84                                         channels[chan2].link = chan1;
85                         } else if (msg.headers.event == "Unlink") {
86                                 var chan1 = msg.headers.channel1;
87                                 var chan2 = msg.headers.channel2;
88                                 if (chan1 && channels[chan1])
89                                         delete channels[chan1].link;
90                                 if (chan2 && channels[chan2])
91                                         delete channels[chan2].link;
92                         } else if (msg.headers.event == "Rename") {
93                                 var oldname = msg.headers.oldname;
94                                 var newname = msg.headers.newname;
95                                 if (oldname && channels[oldname]) {
96                                         channels[newname] = channels[oldname];
97                                         delete channels[oldname];
98                                 }
99                         } else {
100                                 channels[channame]['channel'] = channame;
101                                 for (x=0;x<fields.length;x++) {
102                                         if (msg.headers[fields[x]])
103                                                 channels[channame][fields[x]] = msg.headers[fields[x]];
104                                 }
105                         }
106                 } else {
107                         channels[channame]['channel'] = channame;
108                         for (x=0;x<fields.length;x++) {
109                                 if (msg.headers[fields[x]])
110                                         channels[channame][fields[x]] = msg.headers[fields[x]];
111                         }
112                 }
113         };
114         this.channelClear = function() {
115                 channels = new Array;
116         }
117         this.channelInfo = function(channame) {
118                 return channels[channame];
119         };
120         this.channelTable = function(callback) {
121                 var s, x;
122                 var cclass, count=0;
123                 var found = 0;
124                 var foundactive = 0;
125                 var fieldlist = new Array("channel", "callerid", "calleridname", "context", "extension", "priority");
126
127                 me.chancallback = callback;
128                 s = "<table class='chantable' align='center'>\n";
129                 s = s + "\t<tr class='labels' id='labels'><td>Channel</td><td>State</td><td>Caller</td><td>Location</td><td>Link</td></tr>";
130                 count=0;
131                 for (x in channels) {
132                         if (channels[x].channel) {
133                                 if (count % 2)
134                                         cclass = "chanlistodd";
135                                 else
136                                         cclass = "chanlisteven";
137                                 if (me.selecttarget && (me.selecttarget == x)) {
138                                         cclass = "chanlistselected";
139                                         foundactive = 1;
140                                 }
141                                 count++;
142                                 s = s + "\t<tr class='" + cclass + "' id='" + channels[x].channel + "' onClick='astmanEngine.clickChannel(event)'>";
143                                 s = s + "<td>" + channels[x].channel + "</td>";
144                                 if (channels[x].state)
145                                         s = s + "<td>" + channels[x].state + "</td>";
146                                 else
147                                         s = s + "<td><i class='light'>unknown</i></td>";
148                                 if (channels[x].calleridname && channels[x].callerid && channels[x].calleridname != "<unknown>") {
149                                         cid = channels[x].calleridname.escapeHTML() + " &lt;" + channels[x].callerid.escapeHTML() + "&gt;";
150                                 } else if (channels[x].calleridname && (channels[x].calleridname != "<unknown>")) {
151                                         cid = channels[x].calleridname.escapeHTML();
152                                 } else if (channels[x].callerid) {
153                                         cid = channels[x].callerid.escapeHTML();
154                                 } else {
155                                         cid = "<i class='light'>Unknown</i>";
156                                 }
157                                 s = s + "<td>" + cid + "</td>";
158                                 if (channels[x].extension) {
159                                         s = s + "<td>" + channels[x].extension + "@" + channels[x].context + ":" + channels[x].priority + "</td>";
160                                 } else {
161                                         s = s + "<td><i class='light'>None</i></td>";
162                                 }
163                                 if (channels[x].link) {
164                                         s = s + "<td>" + channels[x].link + "</td>";
165                                 } else {
166                                         s = s + "<td><i class='light'>None</i></td>";
167                                 }
168                                 s = s + "</tr>\n";
169                                 found++;
170                         }
171                 }
172                 if (!found)
173                         s += "<tr><td colspan=" + fieldlist.length + "><i class='light'>No active channels</i></td>\n";
174                 s += "</table>\n";
175                 if (!foundactive) {
176                         me.selecttarget = null;
177                 }
178                 return s;
179         };
180         this.parseResponse = function(t, callback) {
181                 var msgs = new Array();
182                 var inmsg = 0;
183                 var msgnum = 0;
184                 var x,y;
185                 var s = t.responseText;
186                 var allheaders = s.split('\r\n');
187                 if (me.debug) 
188                         me.debug.value = "\n";
189                 for (x=0;x<allheaders.length;x++) {
190                         if (allheaders[x].length) {
191                                 var fields = allheaders[x].split(': ');
192                                 if (!inmsg) {
193                                         msgs[msgnum] = new Object();
194                                         msgs[msgnum].headers = new Array();
195                                         msgs[msgnum].names = new Array();
196                                         y=0;
197                                 }
198                                 msgs[msgnum].headers[fields[0].toLowerCase()] = fields[1];
199                                 msgs[msgnum].names[y++] = fields[0].toLowerCase();
200                                 if (me.debug)
201                                         me.debug.value = me.debug.value + "field " + fields[0] + "/" + fields[1] + "\n";
202                                 inmsg=1;
203                         } else {
204                                 if (inmsg) {
205                                         if (me.debug)
206                                                 me.debug.value = me.debug.value + " new message\n";
207                                         inmsg = 0;
208                                         msgnum++;
209                                 }
210                         }
211                 }
212                 if (me.debug) {
213                         me.debug.value = me.debug.value + "msgnum is " + msgnum + " and array length is " + msgs.length + "\n";
214                         me.debug.value = me.debug.value + "length is " + msgs.length + "\n";
215                         var x, y;
216                         for (x=0;x<msgs.length;x++) {
217                                 for (y=0;y<msgs[x].names.length;y++)  {
218                                         me.debug.value = me.debug.value + "msg "+ (x + 1) + "/" + msgs[x].names[y] + "/" + msgs[x].headers[msgs[x].names[y]] + "\n";
219                                 }
220                         }
221                 }
222                 callback(msgs);
223         };
224         this.managerResponse = function(t) {
225                 me.parseResponse(t, me.callback);
226         };
227         this.doEvents = function(msgs) {
228                 me.eventcallback(msgs);
229         };
230         this.eventResponse = function(t) {
231                 me.parseResponse(t, me.doEvents);
232         };
233         this.sendRequest = function(request, callback) {
234                 var tmp;
235                 var opt = {
236                         method: 'get',
237                         asynchronous: true,
238                         onSuccess: this.managerResponse,
239                         onFailure: function(t) {
240                                 alert("Error: " + t.status + ": " + t.statusText);
241                         }
242                 };
243                 me.callback = callback;
244                 opt.parameters = request;
245                 tmp = new Ajax.Request(this.url, opt);
246         };
247         this.pollEvents = function() {
248                 var tmp;
249                 var opt = {
250                         method: 'get',
251                         asynchronous: true,
252                         onSuccess: this.eventResponse,
253                         onFailure: function(t) {
254                                 alert("Event Error: " + t.status + ": " + t.statusText);
255                         }
256                 };
257                 opt.parameters="action=waitevent";
258                 tmp = new Ajax.Request(this.url, opt);
259         };
260 };
261
262 astmanEngine = new Astman();