Commits

Fuco committed a30e0c5

+ added general url handler

  • Participants
  • Parent commits be73d4a

Comments (0)

Files changed (1)

src/cz/dasnet/dasik/plugin/URLInfo.java

  */
 package cz.dasnet.dasik.plugin;
 
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.net.URLConnection;
 import cz.dasnet.dasik.Dasik;
+import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.regex.Matcher;
     private static Logger log = Logger.getLogger(URLInfo.class);
     private static Pattern youtube = Pattern.compile(".*?(http://|www\\.|http://www\\.){0,1}youtube\\.com/.*?v=(.*?)($|&.*?|\\s+)");
     private static Pattern youtubeShort = Pattern.compile(".*?(http://){0,1}youtu\\.be/(.*?)($|&.*?|\\s+)");
-    private static Pattern url = Pattern.compile(".*((http:\\/\\/www\\.|(www\\.|http:\\/\\/))[_a-zA-Z0-9\\.\\-]+\\.[a-zA-Z]{2,4}\\/{0,1}[-_~&=\\?\\.a-zA-Z0-9\\/]*).*");
+    private static Pattern generalUrl = Pattern.compile(".*((http:\\/\\/www\\.|(www\\.|http:\\/\\/))[_a-zA-Z0-9\\.\\-]+\\.[a-zA-Z]{2,4}\\/{0,1}[-_~&=\\?\\.a-zA-Z0-9\\/]*).*");
     private SAXReader reader = new SAXReader();
 
     @Override
             return;
         }
 
-        m = url.matcher(message);
+        m = generalUrl.matcher(message);
         if (m.find()) {
             // general URL
+            String mURL = m.group(1);
+            if (!mURL.startsWith("http://")) {
+                mURL = "http://" + mURL;
+            }
+
+            URLConnection httpConn = null;
+            try {
+                URL url = new URL(mURL);
+                httpConn = url.openConnection();
+                httpConn.setDoInput(true);
+                httpConn.connect();
+                String mime = httpConn.getContentType();
+
+                if (mime.contains("text")) {
+                    InputStream in = httpConn.getInputStream();
+                    LineNumberReader lnr = new LineNumberReader(new BufferedReader(new InputStreamReader(in, "UTF-8")));
+
+                    String line;
+                    while ((line = lnr.readLine()) != null) {
+                        if (line.contains("</head>")) {
+                            break;
+                        }
+                        if (line.contains("<title>")) {
+                            while (!line.contains("</title>")) {
+                                line += lnr.readLine();
+                                if (line.contains("</head>")) {
+                                    break;
+                                }
+                            }
+                            break;
+                        }
+                    }
+
+                    int titleEnd = line.indexOf("</title>");
+                    if (titleEnd > 0) {
+                        line = line.substring(0, titleEnd);
+                    }
+
+                    line = line.replaceAll("\\s+", " ");
+                    line = line.replace("<title>", "Title: ");
+                    line = decodeEntities(line);
+
+                    line = line.substring(line.indexOf("Title:"));
+                    bot.send(channel, shorten(line, 350) + " (at " + url.getHost() + ")");
+                }
+
+            } catch (IOException ex) {
+                log.error("Error while downloading url: " + mURL, ex);
+            } finally {
+                if (httpConn != null) {
+                    try {
+                        httpConn.getInputStream().close();
+                    } catch (IOException exx) {
+                        log.error("Error closing connection to: " + mURL, exx);
+                    }
+                }
+            }
             return;
         }
     }