// // 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.*; import java.awt.event.*; import java.awt.Graphics; import java.awt.Event; import java.io.*; import java.net.*; import java.net.URL; //import java.applet.AppletContext; import java.applet.*; import java.util.Date; import java.lang.String; //boolean keepTrying = true; //Boolean keepTrying = true; //String keepTrying = "true"; public class AstMonitor extends java.applet.Applet implements ActionListener, Runnable { boolean keepTrying = true; public static final String myPause = "Pause"; public static final String myNext = "Next"; public static final String myEnd = "End"; TextField status; public static final String myStatPause = "Pause"; public static final String myStatNext = "Next"; public static final String myStatEnd = "End"; String message; int project; String tproj; Thread astThread; Button pause = new Button(myNext); Button end = new Button(myEnd); public void init() { message = "something here"; resize(130,200); setBackground( Color.black ); pause.setActionCommand("Pause"); pause.addActionListener(this); add(pause); end.setActionCommand("End"); end.addActionListener(this); add(end); astThread = new Thread(this); astThread.start(); } public void destroy() { keepTrying = false; astThread = null; ActionSocket(getParameter("thehost"),getParameter("station"),getParameter("manageruser"),getParameter("managerpass"),"Destroy",true); } public void run() { 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) {} repaint(); } } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); String thebase = "http://" + getCodeBase().getHost(); if (command.equals("pause")) { if (pause.getLabel().compareTo(myPause) == 0) { pause.setLabel(myPause); message = myStatPause; ActionSocket(getParameter("thehost"),getParameter("station"),getParameter("manageruser"),getParameter("managerpass"),"AgentPause",false); } else { pause.setLabel(myPause); message = myStatPause; ActionSocket(getParameter("thehost"),getParameter("station"),getParameter("manageruser"),getParameter("managerpass"),"AgentPause",true); } } repaint(); if (command.equals("end")) { stop(); ActionSocket(getParameter("thehost"),getParameter("station"),getParameter("manageruser"),getParameter("managerpass"),"AgentLogoff",true); } } public void paint(Graphics g) { g.setColor( Color.green ); g.drawString( " " + message,5,100); } public void ActionSocket(String host, String agent, String manuser, String manpass, String theAction, boolean tf) { Socket AstActionSocket = null; DataOutputStream osA = null; BufferedReader isA = null; try { AstActionSocket = new Socket(getCodeBase().getHost(), 5038); osA = new DataOutputStream(AstActionSocket.getOutputStream()); isA = new BufferedReader(new InputStreamReader(AstActionSocket.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 (AstActionSocket != null && osA != null && isA != null) { try { osA.writeBytes("Action: Login\r\n"); osA.writeBytes("UserName: " + manuser + "\r\n"); osA.writeBytes("Secret: " + manpass + "\r\n\r\n"); String resp, block = new String(); if (theAction == "AgentLogoff") { osA.writeBytes("Action: Agentlogoff\r\n"); osA.writeBytes("Agent: " + agent + "\r\n\r\n"); } if (theAction == "AgentPause") { osA.writeBytes("Action: AgentPause\r\n"); osA.writeBytes("Agent: " + agent + "\r\n"); if (tf) { osA.writeBytes("Pause: true\r\n\r\n"); } else { osA.writeBytes("Pause: false\r\n\r\n"); } } osA.writeBytes("Action: Logoff\r\n\r\n"); osA.close(); isA.close(); AstActionSocket.close(); } catch (UnknownHostException e) { System.err.println("Unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + 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 = "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(); 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"); } 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; } }