astman.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. function Astman() {
  22. var me = this;
  23. var channels = new Array;
  24. var lastselect;
  25. var selecttarget;
  26. this.setURL = function(url) {
  27. this.url = url;
  28. };
  29. this.setEventCallback = function(callback) {
  30. this.eventcallback = callback;
  31. };
  32. this.setDebug = function(debug) {
  33. this.debug = debug;
  34. };
  35. this.clickChannel = function(ev) {
  36. var target = ev.target;
  37. // XXX This is icky, we statically use astmanEngine to call the callback XXX
  38. if (me.selecttarget)
  39. me.restoreTarget(me.selecttarget);
  40. while(!target.id || !target.id.length)
  41. target=target.parentNode;
  42. me.selecttarget = target.id;
  43. target.className = "chanlistselected";
  44. me.chancallback(target.id);
  45. };
  46. this.restoreTarget = function(targetname) {
  47. var other;
  48. target = $(targetname);
  49. if (!target)
  50. return;
  51. if (target.previousSibling) {
  52. other = target.previousSibling.previousSibling.className;
  53. } else if (target.nextSibling) {
  54. other = target.nextSibling.nextSibling.className;
  55. }
  56. if (other) {
  57. if (other == "chanlisteven")
  58. target.className = "chanlistodd";
  59. else
  60. target.className = "chanlisteven";
  61. } else
  62. target.className = "chanlistodd";
  63. };
  64. this.channelUpdate = function(msg, channame) {
  65. var fields = new Array("callerid", "calleridname", "context", "extension", "priority", "account", "state", "link", "uniqueid" );
  66. if (!channame || !channame.length)
  67. channame = msg.headers['channel'];
  68. if (!channels[channame])
  69. channels[channame] = new Array();
  70. if (msg.headers.event) {
  71. if (msg.headers.event == "Hangup") {
  72. delete channels[channame];
  73. } else if (msg.headers.event == "Link") {
  74. var chan1 = msg.headers.channel1;
  75. var chan2 = msg.headers.channel2;
  76. if (chan1 && channels[chan1])
  77. channels[chan1].link = chan2;
  78. if (chan2 && channels[chan2])
  79. channels[chan2].link = chan1;
  80. } else if (msg.headers.event == "Unlink") {
  81. var chan1 = msg.headers.channel1;
  82. var chan2 = msg.headers.channel2;
  83. if (chan1 && channels[chan1])
  84. delete channels[chan1].link;
  85. if (chan2 && channels[chan2])
  86. delete channels[chan2].link;
  87. } else if (msg.headers.event == "Rename") {
  88. var oldname = msg.headers.oldname;
  89. var newname = msg.headers.newname;
  90. if (oldname && channels[oldname]) {
  91. channels[newname] = channels[oldname];
  92. delete channels[oldname];
  93. }
  94. } else {
  95. channels[channame]['channel'] = channame;
  96. for (x=0;x<fields.length;x++) {
  97. if (msg.headers[fields[x]])
  98. channels[channame][fields[x]] = msg.headers[fields[x]];
  99. }
  100. }
  101. } else {
  102. channels[channame]['channel'] = channame;
  103. for (x=0;x<fields.length;x++) {
  104. if (msg.headers[fields[x]])
  105. channels[channame][fields[x]] = msg.headers[fields[x]];
  106. }
  107. }
  108. };
  109. this.channelClear = function() {
  110. channels = new Array;
  111. }
  112. this.channelInfo = function(channame) {
  113. return channels[channame];
  114. };
  115. this.channelTable = function(callback) {
  116. var s, x;
  117. var cclass, count=0;
  118. var found = 0;
  119. var foundactive = 0;
  120. var fieldlist = new Array("channel", "callerid", "calleridname", "context", "extension", "priority");
  121. me.chancallback = callback;
  122. s = "<table class='chantable' align='center'>\n";
  123. s = s + "\t<tr class='labels' id='labels'><td>Channel</td><td>State</td><td>Caller</td><td>Location</td><td>Link</td></tr>";
  124. count=0;
  125. for (x in channels) {
  126. if (channels[x].channel) {
  127. if (count % 2)
  128. cclass = "chanlistodd";
  129. else
  130. cclass = "chanlisteven";
  131. if (me.selecttarget && (me.selecttarget == x)) {
  132. cclass = "chanlistselected";
  133. foundactive = 1;
  134. }
  135. count++;
  136. s = s + "\t<tr class='" + cclass + "' id='" + channels[x].channel + "' onClick='astmanEngine.clickChannel(event)'>";
  137. s = s + "<td>" + channels[x].channel + "</td>";
  138. if (channels[x].state)
  139. s = s + "<td>" + channels[x].state + "</td>";
  140. else
  141. s = s + "<td><i class='light'>unknown</i></td>";
  142. if (channels[x].calleridname && channels[x].callerid && channels[x].calleridname != "<unknown>") {
  143. cid = channels[x].calleridname.escapeHTML() + " &lt;" + channels[x].callerid.escapeHTML() + "&gt;";
  144. } else if (channels[x].calleridname && (channels[x].calleridname != "<unknown>")) {
  145. cid = channels[x].calleridname.escapeHTML();
  146. } else if (channels[x].callerid) {
  147. cid = channels[x].callerid.escapeHTML();
  148. } else {
  149. cid = "<i class='light'>Unknown</i>";
  150. }
  151. s = s + "<td>" + cid + "</td>";
  152. if (channels[x].extension) {
  153. s = s + "<td>" + channels[x].extension + "@" + channels[x].context + ":" + channels[x].priority + "</td>";
  154. } else {
  155. s = s + "<td><i class='light'>None</i></td>";
  156. }
  157. if (channels[x].link) {
  158. s = s + "<td>" + channels[x].link + "</td>";
  159. } else {
  160. s = s + "<td><i class='light'>None</i></td>";
  161. }
  162. s = s + "</tr>\n";
  163. found++;
  164. }
  165. }
  166. if (!found)
  167. s += "<tr><td colspan=" + fieldlist.length + "><i class='light'>No active channels</i></td>\n";
  168. s += "</table>\n";
  169. if (!foundactive) {
  170. me.selecttarget = null;
  171. }
  172. return s;
  173. };
  174. this.parseResponse = function(t, callback) {
  175. var msgs = new Array();
  176. var inmsg = 0;
  177. var msgnum = 0;
  178. var x,y;
  179. var s = t.responseText;
  180. var allheaders = s.split('\r\n');
  181. if (me.debug)
  182. me.debug.value = "\n";
  183. for (x=0;x<allheaders.length;x++) {
  184. if (allheaders[x].length) {
  185. var fields = allheaders[x].split(': ');
  186. if (!inmsg) {
  187. msgs[msgnum] = new Object();
  188. msgs[msgnum].headers = new Array();
  189. msgs[msgnum].names = new Array();
  190. y=0;
  191. }
  192. msgs[msgnum].headers[fields[0].toLowerCase()] = fields[1];
  193. msgs[msgnum].names[y++] = fields[0].toLowerCase();
  194. if (me.debug)
  195. me.debug.value = me.debug.value + "field " + fields[0] + "/" + fields[1] + "\n";
  196. inmsg=1;
  197. } else {
  198. if (inmsg) {
  199. if (me.debug)
  200. me.debug.value = me.debug.value + " new message\n";
  201. inmsg = 0;
  202. msgnum++;
  203. }
  204. }
  205. }
  206. if (me.debug) {
  207. me.debug.value = me.debug.value + "msgnum is " + msgnum + " and array length is " + msgs.length + "\n";
  208. me.debug.value = me.debug.value + "length is " + msgs.length + "\n";
  209. var x, y;
  210. for (x=0;x<msgs.length;x++) {
  211. for (y=0;y<msgs[x].names.length;y++) {
  212. me.debug.value = me.debug.value + "msg "+ (x + 1) + "/" + msgs[x].names[y] + "/" + msgs[x].headers[msgs[x].names[y]] + "\n";
  213. }
  214. }
  215. }
  216. callback(msgs);
  217. };
  218. this.managerResponse = function(t) {
  219. me.parseResponse(t, me.callback);
  220. };
  221. this.doEvents = function(msgs) {
  222. me.eventcallback(msgs);
  223. };
  224. this.eventResponse = function(t) {
  225. me.parseResponse(t, me.doEvents);
  226. };
  227. this.sendRequest = function(request, callback) {
  228. var tmp;
  229. var opt = {
  230. method: 'get',
  231. asynchronous: true,
  232. onSuccess: this.managerResponse,
  233. onFailure: function(t) {
  234. alert("Error: " + t.status + ": " + t.statusText);
  235. }
  236. };
  237. me.callback = callback;
  238. opt.parameters = request;
  239. tmp = new Ajax.Request(this.url, opt);
  240. };
  241. this.pollEvents = function() {
  242. var tmp;
  243. var opt = {
  244. method: 'get',
  245. asynchronous: true,
  246. onSuccess: this.eventResponse,
  247. onFailure: function(t) {
  248. alert("Event Error: " + t.status + ": " + t.statusText);
  249. }
  250. };
  251. opt.parameters="action=waitevent";
  252. tmp = new Ajax.Request(this.url, opt);
  253. };
  254. };
  255. astmanEngine = new Astman();