// // astcrm - General purpose CRM // // General purpose CRM software for asterisk // // Copyright (C) 2005, GnuDialer Project // // Heath Schultz // Richard Lyman // // This program is free software, distributed under the terms of // the GNU General Public License. // // Conversion to commandline // uncomment main function // comment out showDocument // comment out AstConnect in paint // hardcode baseurl // make AstConnect static import java.awt.Graphics; import java.awt.Event; import java.io.*; import java.net.*; import java.net.URL; import java.applet.AppletContext; //boolean keepTrying = true; //Boolean keepTrying = true; //String keepTrying = "true"; public class AstMonitor extends java.applet.Applet { boolean keepTrying = true; // public static void main(String Args[]) { // AstConnect("999.999.999.999","1","crm","1234"); // } public void init() { resize(1,1); } public void paint(Graphics g) { keepTrying = true; while(keepTrying) { AstConnect(getParameter("thehost"),getParameter("station"),getParameter("manageruser"),getParameter("managerpass")); // If we lose our connection or it fails, let's not hammer on asterisk. try { Thread.sleep(3000); } catch (Exception e) {} } } public void AstConnect(String host, String agent, String manuser, String manpass) { Socket AstSocket = null; DataOutputStream os = null; BufferedReader is = null; try { AstSocket = new Socket(getCodeBase().getHost(), 5038); os = new DataOutputStream(AstSocket.getOutputStream()); is = new BufferedReader(new InputStreamReader(AstSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Error finding host: hostname"); } catch (IOException e) { System.err.println("Error sending/receiving data to/from: hostname"); } if (AstSocket != null && os != null && is != null) { try { os.writeBytes("Action: Login\r\n"); os.writeBytes("UserName: " + manuser + "\r\n"); os.writeBytes("Secret: " + manpass + "\r\n\r\n"); String resp, block = new String(); String campaign = new String(); String leadid = new String(); String flushscreen = new String(); String sUrl = new String(); // String baseurl = getParameter("baseurl"); // String baseurl = "../cgi-bin/crm.cgi"; // String baseurl = "999.999.999.999"; // String baseurl = getCodeBase().toString().substring(0,getCodeBase().toString().length()-21) + "/cgi-bin/crm.cgi"; // String baseurl = "http://" + getCodeBase().getHost() + ":" + getCodeBase().getPort() + "/cgi-bin/crm.cgi"; // String thebase = "http://" + getCodeBase().getHost() + ":" + getCodeBase().getPort(); String baseurl = "http://" + getCodeBase().getHost() + "/cgi-bin/crm.cgi"; String thebase = "http://" + getCodeBase().getHost(); // String baseurl = "http://" + getAppletContext().getHost() + ":" + getAppletContext().getPort() + "/cgi-bin/crm.cgi"; int pos = 0; int end = 0; while ((resp = is.readLine()) != null) { if (resp.indexOf(":") != -1 || resp.indexOf("Agent") != -1) block += resp + "\n"; else { if (block.indexOf("Response: Success",0) != -1) { getAppletContext().showDocument(String2Url(baseurl + "?campaign=-1&leadid=0&agent=0&station=0"),"actualcrm"); try { Thread.sleep(500); } catch (Exception e) {} getAppletContext().showDocument(String2Url(baseurl + "?campaign=-2&leadid=0&agent=0&station=0"),"actualcrm"); } if (block.indexOf("Goodbye",0) != -1) { os.close(); is.close(); AstSocket.close(); keepTrying = false; // stop(); } if ((block.indexOf("Event: Agentlogoff") != -1 || block.indexOf("Event: AgentLogoff") != -1) && block.indexOf("Agent: " + agent + "\n") != -1) { // sUrl = baseurl + "?campaign=0&monitor=0&leadid=0&agent=" + agent + "&station=1"; getAppletContext().showDocument(String2Url(thebase + "/crm/crmlogin.html"),"_parent"); os.writeBytes("Action: Logoff\r\n\r\n"); // stop(); } if (block.indexOf("Event: UserEventClserCalled") != -1 && block.indexOf(campaign) != -1 && block.indexOf(leadid) != -1) { sUrl = baseurl + "?campaign=" + -2 + "&monitor=" + getParameter("monitor") + "&leadid=" + 0 + "&agent=" + agent + "&station=1"; // try { // Thread.sleep(250); // } // catch (Exception e) {} getAppletContext().showDocument(String2Url(sUrl),"actualcrm"); campaign = "bunkcampaign"; leadid = "bunkleadid"; } // if (block.indexOf("Event: UserEventAgentCalled") != -1 && block.indexOf("Agent/" + agent + " ") != -1) { if ((block.indexOf("Event: UserEventAgentCalled") != -1 || block.indexOf("Event: UserEventClserCalled") != -1) && block.indexOf("Agent/" + agent + " ") != -1) { // if (block.indexOf("Dial") != -1 && block.indexOf("Agent/" + agent + " ") != -1) { ////// System.out.println(block); flushscreen = block; pos = block.indexOf("~"); end = block.indexOf("-",pos); campaign = block.substring(pos+1,end); if (block.indexOf("UserEventClserCalled") != -1) { campaign += "-isclosercam"; } pos = end+1; end = block.indexOf("-",pos); leadid = block.substring(pos,end); // campaign = "test1"; // leadid = "5000"; sUrl = baseurl + "?campaign=" + campaign + "&monitor=" + getParameter("monitor") + "&leadid=" + leadid + "&agent=" + agent + "&station=1"; if (block.indexOf("ClserCalled") != -1) { try { Thread.sleep(250); } catch (Exception e) {} } getAppletContext().showDocument(String2Url(sUrl),"actualcrm"); } // changed usereventunlink to unlink // if (block.indexOf("Unlink") != -1 && (block.indexOf("Agent/" + agent + "") != -1 || block.indexOf("Agent/" + agent + "\r") != -1) && flushscreen.indexOf("closer") != -1) { // if (block.indexOf("Event: UserEventAgentCalled") != -1 && block.indexOf(campaign) != -1 && block.indexOf(leadid) != -1) { // sUrl = baseurl + // "?campaign=" + -2 + // "&monitor=" + getParameter("monitor") + // "&leadid=" + 0 + // "&agent=" + agent + // "&station=1"; // // getAppletContext().showDocument(String2Url(sUrl),"actualcrm"); // // campaign = ""; // leadid = ""; // // } if (block.indexOf("Unlink") != -1 && block.indexOf("Agent/" + agent + "") != -1 && flushscreen.indexOf("true") != -1) { sUrl = baseurl + "?campaign=" + -2 + "&monitor=" + getParameter("monitor") + "&leadid=" + 0 + "&agent=" + agent + "&station=1"; getAppletContext().showDocument(String2Url(sUrl),"actualcrm"); } block = ""; sUrl = ""; } if (resp.indexOf("DIALERQUIT") != -1) { break; } } os.close(); is.close(); AstSocket.close(); } catch (UnknownHostException e) { System.err.println("Unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } public static URL String2Url(String theString) { URL theUrl = null; try { theUrl = new URL(theString); return theUrl; } catch (Exception e) {} return theUrl; } }