Search in sources :

Example 6 with DoBlockException

use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.

the class UnitymediaFirmware method doBlock.

public void doBlock(String number) throws DoBlockException, IOException {
    if (!this.isInitialized())
        throw new DoBlockException("Could not block number " + number + " on FritzBox: FritzBox firmware not initialized.");
    String urlstr = getProtocol() + this.m_address + ":" + this.m_port + "/fon_num/sperre_edit.lua";
    String postdata = ("mode_call=_in&rule_kind=rufnummer&rule_number=$NUMBER&current_rule=&current_mode=_new&backend_validation=false&apply=&sid=".replaceAll("\\$NUMBER", number) + this.m_sid);
    executeURL(urlstr, postdata, false);
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("Successfully added numer " + number + " to FritzBox block list.");
}
Also used : DoBlockException(de.janrufmonitor.fritzbox.firmware.exception.DoBlockException)

Example 7 with DoBlockException

use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.

the class Block method doBlock.

private boolean doBlock(IPhonenumber n) {
    String dial = n.getTelephoneNumber();
    if (!n.getIntAreaCode().equalsIgnoreCase(this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty(IJAMConst.GLOBAL_NAMESPACE, IJAMConst.GLOBAL_INTAREA))) {
        dial = "00" + n.getIntAreaCode() + dial;
    }
    if (!dial.startsWith("0"))
        dial = "0" + dial;
    String text = getI18nManager().getString(getNamespace(), "block", "description", getLanguage());
    text = StringUtils.replaceString(text, "{%1}", dial);
    if (MessageDialog.openConfirm(new Shell(DisplayManager.getDefaultDisplay()), this.getI18nManager().getString(this.getNamespace(), "block", "label", this.getLanguage()), text)) {
        FirmwareManager fwm = FirmwareManager.getInstance();
        try {
            if (!fwm.isLoggedIn())
                fwm.login();
            fwm.doBlock(dial);
            FritzBoxBlockedListManager.invalidate();
        } catch (IOException e) {
            this.m_logger.warning(e.toString());
            PropagationFactory.getInstance().fire(new Message(Message.ERROR, getNamespace(), "failedblock", e));
        } catch (FritzBoxLoginException e) {
            this.m_logger.warning(e.toString());
        } catch (DoBlockException e) {
            this.m_logger.warning(e.toString());
            PropagationFactory.getInstance().fire(new Message(Message.ERROR, getNamespace(), "failedblock", e));
        }
        return true;
    }
    return false;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) FritzBoxLoginException(de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException) Message(de.janrufmonitor.exception.Message) FirmwareManager(de.janrufmonitor.fritzbox.firmware.FirmwareManager) IOException(java.io.IOException) DoBlockException(de.janrufmonitor.fritzbox.firmware.exception.DoBlockException)

Example 8 with DoBlockException

use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.

the class AbstractFritzBoxFirmware method getBlockCount.

private int getBlockCount() throws DoBlockException, IOException {
    if (!this.isInitialized())
        throw new DoBlockException("Could get blocked caller list from FritzBox: FritzBox firmware not initialized.");
    InputStream in = null;
    try {
        in = this.getBlockedListAsStream();
    } catch (IOException e) {
        throw new DoBlockException(e.getMessage());
    }
    if (in == null)
        throw new DoBlockException("Could get blocked caller list from FritzBox: Could not read count from page.");
    int c = 0;
    StringBuffer parseBuffer = new StringBuffer();
    InputStreamReader inr = new InputStreamReader(in);
    BufferedReader bufReader = new BufferedReader(inr);
    // drop header
    String line = bufReader.readLine();
    // fasten the processing
    bufReader.skip(4096);
    while (bufReader.ready()) {
        line = bufReader.readLine();
        parseBuffer.append(line);
        parseBuffer.append(IJAMConst.CRLF);
    }
    bufReader.close();
    in.close();
    Pattern p = Pattern.compile("(telcfg:settings/CallerIDActions\\d+)");
    Matcher m = p.matcher(parseBuffer);
    while (m.find() && m.groupCount() == 1) {
        c++;
    }
    if (this.m_logger.isLoggable(Level.INFO))
        this.m_logger.info("Found " + c + " blocked caller numbers in FritzBox.");
    return c;
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) DoBlockException(de.janrufmonitor.fritzbox.firmware.exception.DoBlockException)

Example 9 with DoBlockException

use of de.janrufmonitor.fritzbox.firmware.exception.DoBlockException in project janrufmonitor by tbrandt77.

the class AbstractFritzBoxFirmware method getBlockedListAsStream.

private InputStream getBlockedListAsStream() throws DoBlockException, IOException {
    long start = System.currentTimeMillis();
    this.m_logger.info("Starting retrieving blocked list...");
    // The list should be updated now
    // Get the csv file for processing
    String urlstr = getProtocol() + this.m_address + ":" + this.m_port + getBaseURL();
    URL url;
    URLConnection urlConn;
    DataOutputStream printout;
    try {
        this.m_logger.info("Calling FritzBox URL: " + urlstr);
        url = new URL(urlstr);
    } catch (MalformedURLException e) {
        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
        throw new DoBlockException("Invalid URL: " + urlstr);
    }
    // If the url is valid load the data
    if (url != null && getFetchBlockedListPOSTData().trim().length() > 0) {
        urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        // Sending postdata to the fritz box
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        printout = new DataOutputStream(urlConn.getOutputStream());
        printout.writeBytes(getFetchBlockedListPOSTData().replaceAll("\\$LANG", this.m_language) + getFetchBlockedListURLAuthenticator().getAuthenticationToken());
        printout.flush();
        printout.close();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e2) {
            this.m_logger.log(Level.SEVERE, e2.getMessage(), e2);
        }
        try {
            // Get response data from the box
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            this.m_logger.info("Fetching blocked list from FritzBox took " + (System.currentTimeMillis() - start) + "ms");
            Stream.copy(urlConn.getInputStream(), bos);
            ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
            // this.m_logger.info(bos.toString());
            this.m_logger.info("Finished retrieving blocked list took " + (System.currentTimeMillis() - start) + "ms");
            urlConn.getInputStream().close();
            return bin;
        } catch (IOException e1) {
            this.m_logger.log(Level.SEVERE, e1.getMessage(), e1);
            throw new DoBlockException(e1.getMessage());
        }
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) DoBlockException(de.janrufmonitor.fritzbox.firmware.exception.DoBlockException)

Aggregations

DoBlockException (de.janrufmonitor.fritzbox.firmware.exception.DoBlockException)9 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Message (de.janrufmonitor.exception.Message)1 FirmwareManager (de.janrufmonitor.fritzbox.firmware.FirmwareManager)1 FritzBoxLoginException (de.janrufmonitor.fritzbox.firmware.exception.FritzBoxLoginException)1 GetBlockedListException (de.janrufmonitor.fritzbox.firmware.exception.GetBlockedListException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Shell (org.eclipse.swt.widgets.Shell)1