Index: src/sharingmatrix/cz/vity/freerapid/plugins/services/sharingmatrix/SharingMatrixFileRunner.java
===================================================================
--- src/sharingmatrix/cz/vity/freerapid/plugins/services/sharingmatrix/SharingMatrixFileRunner.java	(revision 2400)
+++ src/sharingmatrix/cz/vity/freerapid/plugins/services/sharingmatrix/SharingMatrixFileRunner.java	(working copy)
@@ -21,7 +21,7 @@
 class SharingMatrixFileRunner extends AbstractRunner {
 
     private final static Logger logger = Logger.getLogger(SharingMatrixFileRunner.class.getName());

     @Override
     public void runCheck() throws Exception {
         super.runCheck();
@@ -49,94 +49,122 @@
             final String linkId = PlugUtils.getStringBetween(contentAsString, "link_id = '", "'");
             final String linkName = PlugUtils.getStringBetween(contentAsString, "link_name = '", "'");
             String captchaURL = rootURL + "/images/captcha/";
+            boolean wait_timer = false;
+            String dlID = "";
+            int forced_wait_time = 0;
 
-            httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/download.php?type_membership=free&link_id=" + linkId).toHttpMethod();
-            if (makeRedirectedRequest(httpMethod)) {
-                int ctjv = PlugUtils.getWaitTimeBetween(getContentAsString(), "ctjv = '", "';", TimeUnit.SECONDS);
-                downloadTask.sleep(ctjv);
-                String dlID = "";
-                try {
-                    captchaURL = PlugUtils.getStringBetween(getContentAsString(), "CAPTCHA_IMAGE_URL = '", "'");
-                } catch(PluginImplementationException e) {
-                    logger.warning(e.getMessage());
-                }
-                if (getContentAsString().contains("_get2.php")) {
-                    dlID = PlugUtils.getStringBetween(getContentAsString(), "showLink(", ", '');");
-                    httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/_get2.php?link_id=" + linkId + "&link_name=" + linkName + "&dl_id=" + dlID + "&password=").toHttpMethod();
-                } else {
-                    boolean refreshCaptcha = false;
-                    InputStream is;
-                    BufferedImage captchaImage = null;
-                    CaptchaSupport captchaSupport = getCaptchaSupport();
-                    if( getContentAsString().contains("check_timer.php") ) {
-                        httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/check_timer.php?tmp=" + (new Random()).nextInt() ).toGetMethod();
-                        if(!makeRequest(httpMethod)) {
-                            throw new PluginImplementationException();
-                        }
-                        //Matcher matcher = getMatcherAgainstContent("img:([^$]*)");
-                        refreshCaptcha = true;
+            do {
+                wait_timer = false;
+                httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/download.php?type_membership=free&link_id=" + linkId).toHttpMethod();
+                if (makeRedirectedRequest(httpMethod)) {
+                    // waitime can be 1,5,10,15 and 45 mins (in seconds). After 45 mins, must wait a whole day for free downloaders before you can download again
+                    int ctjv = PlugUtils.getWaitTimeBetween(getContentAsString(), "ctjv = '", "';", TimeUnit.SECONDS);
+                    if (forced_wait_time != 0) {
+                        // whensharing matrix is busy, it tells us to wait 60 seconds, but if we do that, we have to
+                        // refresh the captcha, ask for user to input the captcha, and then it fails again
+                        // and tells us to wait another 60 seconds.  It's annoying to enter the captcha every 60
+                        // seconds for 3 hours or more. This forced_wait_time recognizes this situation and
+                        // forces a 1 hour wait when the sharingmatrix servers are busy.  Then you only
+                        // have to enter a captcha every hour (servers may still be busy, but it's far less annoying)
+                        downloadTask.sleep(forced_wait_time);
+                        forced_wait_time = 0;
                     } else {
-                        httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/include/crypt/cryptographp.php?cfg=0&").toHttpMethod();
-                        httpMethod.setFollowRedirects(true);
-                        is = client.makeRequestForFile(httpMethod);
-
-                        if (is == null) {
-                            throw new PluginImplementationException();
-                        }
-
-                        captchaImage = captchaSupport.loadCaptcha(is);
+                        downloadTask.sleep(ctjv);
                     }
-                    String captchaR = null;
-                    do {
-                        if( refreshCaptcha ) {
-                            httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/reload_captcha.php").toHttpMethod();
-                            if(!makeRequest(httpMethod)) {
+                    try {
+                        captchaURL = PlugUtils.getStringBetween(getContentAsString(), "CAPTCHA_IMAGE_URL = '", "'");
+                    } catch (PluginImplementationException e) {
+                        logger.warning(e.getMessage());
+                    }
+                    if (getContentAsString().contains("_get2.php")) {
+                        dlID = PlugUtils.getStringBetween(getContentAsString(), "showLink(", ", '');");
+                        httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/_get2.php?link_id=" + linkId + "&link_name=" + linkName + "&dl_id=" + dlID + "&password=").toHttpMethod();
+                    } else {
+                        boolean refreshCaptcha = false;
+                        InputStream is;
+                        BufferedImage captchaImage = null;
+                        CaptchaSupport captchaSupport = getCaptchaSupport();
+                        if (getContentAsString().contains("check_timer.php")) {
+                            httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/check_timer.php?tmp=" + (new Random()).nextInt()).toGetMethod();
+                            if (!makeRequest(httpMethod)) {
                                 throw new PluginImplementationException();
                             }
-                            httpMethod = getMethodBuilder().setReferer(fileURL).setAction(captchaURL + getContentAsString().trim()+".jpg").toHttpMethod();
+                            //Matcher matcher = getMatcherAgainstContent("img:([^$]*)");
+                            refreshCaptcha = true;
+                        } else {
+                            httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/include/crypt/cryptographp.php?cfg=0&").toHttpMethod();
+                            httpMethod.setFollowRedirects(true);
                             is = client.makeRequestForFile(httpMethod);
+
                             if (is == null) {
-                                throw new PluginImplementationException(httpMethod.getURI().toString());
+                                throw new PluginImplementationException();
                             }
+
                             captchaImage = captchaSupport.loadCaptcha(is);
                         }
-                        //System.out.println( getContentAsString() );
-                        captchaR = captchaSupport.askForCaptcha(captchaImage);
-                        if (captchaR == null) {
-                            throw new CaptchaEntryInputMismatchException();
-                        }
+                        String captchaR = null;
+                        do {
+                            if (refreshCaptcha) {
+                                httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/reload_captcha.php").toHttpMethod();
+                                if (!makeRequest(httpMethod)) {
+                                    throw new PluginImplementationException();
+                                }
+                                httpMethod = getMethodBuilder().setReferer(fileURL).setAction(captchaURL + getContentAsString().trim() + ".jpg").toHttpMethod();
+                                is = client.makeRequestForFile(httpMethod);
+                                if (is == null) {
+                                    throw new PluginImplementationException(httpMethod.getURI().toString());
+                                }
+                                captchaImage = captchaSupport.loadCaptcha(is);
+                            }
+                            //System.out.println( getContentAsString() );
+                            captchaR = captchaSupport.askForCaptcha(captchaImage);
+                            if (captchaR == null) {
+                                throw new CaptchaEntryInputMismatchException();
+                            }
 
-                        httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/verifier.php").setParameter("code", captchaR).toPostMethod();
-                        if (!makeRedirectedRequest(httpMethod)) {
-                            throw new ServiceConnectionProblemException();
+                            httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/verifier.php").setParameter("code", captchaR).toPostMethod();
+                            if (!makeRedirectedRequest(httpMethod)) {
+                                throw new ServiceConnectionProblemException();
+                            }
+
+                        } while (!getContentAsString().trim().equals("1"));
+                        httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/dl.php").toHttpMethod();
+                        if (makeRedirectedRequest(httpMethod)) {
+                            dlID = getContentAsString();
                         }
-
-                    } while (!getContentAsString().trim().equals("1"));
-                    httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/dl.php").toHttpMethod();
+                        httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/_get.php?link_id=" + linkId + "&link_name=" + linkName + "&dl_id=" + dlID + "&password=").toHttpMethod();
+                    }
                     if (makeRedirectedRequest(httpMethod)) {
-                        dlID=getContentAsString();
+                        contentAsString = getContentAsString();
+                        if (contentAsString.contains("Wait timer!")) {
+                            wait_timer = true;
+                        }
+                        if (contentAsString.contains("You can not download now!") ||
+                            contentAsString.contains("quota has been reached")) {
+                            wait_timer = true;
+                            forced_wait_time = 3600;
+                        }
                     }
-                    httpMethod = getMethodBuilder().setReferer(fileURL).setAction(rootURL + "/ajax_scripts/_get.php?link_id=" + linkId + "&link_name=" + linkName + "&dl_id=" + dlID + "&password=").toHttpMethod();
+                    else {
+                        throw new ServiceConnectionProblemException();
+                    }
                 }
-                if (makeRedirectedRequest(httpMethod)) {
-                    contentAsString = getContentAsString();
-                    httpMethod = getMethodBuilder().setReferer(fileURL).setAction("/download/" + PlugUtils.getStringBetween(contentAsString, "hash:\"", "\"") + "/" + dlID + "/").setBaseURL(PlugUtils.getStringBetween(contentAsString, "{serv:\"", "\"")).toHttpMethod();
-
-                    if (!tryDownloadAndSaveFile(httpMethod)) {
-                        checkAllProblems();
-                        logger.warning(getContentAsString());
-                        throw new IOException("File input stream is empty");
-                    }
-                } else {
+                else {
                     throw new ServiceConnectionProblemException();
                 }
-            } else {
-                throw new ServiceConnectionProblemException();
+            } while (wait_timer);
+
+            httpMethod = getMethodBuilder().setReferer(fileURL).setAction("/download/" + PlugUtils.getStringBetween(contentAsString, "hash:\"", "\"") + "/" + dlID + "/").setBaseURL(PlugUtils.getStringBetween(contentAsString, "{serv:\"", "\"")).toHttpMethod();
+
+            if (!tryDownloadAndSaveFile(httpMethod)) {
+                checkAllProblems();
+                logger.warning(getContentAsString());
+                throw new IOException("File input stream is empty");
             }
         } else {
             throw new InvalidURLOrServiceProblemException("Invalid URL or service problem");
         }
-    }
+   }
 
     private void checkSeriousProblems() throws ErrorDuringDownloadingException {
         final String contentAsString = getContentAsString();
@@ -144,6 +172,9 @@
         if (contentAsString.contains("File not found")) {
             throw new URLNotAvailableAnymoreException("File not found");
         }
+        if (contentAsString.contains("File has been deleted")) {
+            throw new URLNotAvailableAnymoreException("File has been deleted");
+        }
         if (contentAsString.contains("no available free download slots left for your country")) {
             throw new URLNotAvailableAnymoreException("We are very sorry, but for this file no available free download slots left for your country today");
         }

