var http = require('follow-redirects').http; 
var getHostname = function(str){
	var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
	return str.match(re)[1].toString();
};
var checkForLinks = function(event, bot){
	if(match = event.message.match(/\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))/i)){
			var url = match[0], title;
			if(!url){
				return;
			}
			if(event.source.nick == 'clibot' || event.source.nick == bot.nick){
				return;
			}
			if(url.indexOf('http') !== 0){
				url = 'https://' + url;
			}
			var options = {
				host: getHostname(url),
				port: 80,
				path: url.replace(getHostname(url), ''),
				method: 'GET'
			};
			var req = http.get(options, function(res) {
				var body = '', contentType = res.headers['content-type'].split(';');
				res.setEncoding('utf8');
				if(res.statusCode !== 200 && res.statusCode !== 304 && res.statusCode !== 400){
					req.abort();
					return;
				}
				if(!contentType || contentType[0] !== 'text/html'){
					console.log('Aborting...');
					req.abort();
					return;
				}
				res.on('data', function(chunk){
					body += chunk;
					if(body.length > 2 * 1024 * 1024){
						console.log(body.length);
						req.abort();
						return;
					}
				});
				res.on('end', function(){
					if(body){
						var match = /<head.*?>.*?<title.*?>(.*?)<\/title>.*?<\/head>/.exec(body.replace(/(\r\n|\n|\r)/gm,'')), redirected;
						if(match && match[1]){
							title = match[1];
							if(title.length <= 0){
								title = 'Empty title';
							}else if(title.length > 100){
								title = title.substring(0, 100) + '...';
							}
							redirected = req.url !== url;
							bot.message(event.target, '[Link] ' + getHostname(req.url ? req.url : url) + ' ' + (redirected ?  '[redirected]' : '') + ': ' + title);
						}
					}
				});
			});
		}
};
module.exports = function(bot){
	bot.on('privmsg', function(event){
		var validChannels = bot.PluginConfigs.get('title.channels');
		if(validChannels){
			validChannels = validChannels.split(',');
		}
		if(validChannels){
			validChannels.forEach(function(channel){
				if(event.target == channel){
					checkForLinks(event, bot);
				}
			});
		}
	});
};
@@ -455,16 +455,26 @@
 %5D, title
+, hostname
 ;%0A%09%09%09if(
@@ -652,16 +652,104 @@
 l;%0A%09%09%09%7D%0A
+%09%09%09console.log(url);%0A%09%09%09hostname = getHostname(url);%0A%09%09%09if(!hostname)%7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A
 %09%09%09var o
@@ -769,32 +769,24 @@
 %09%09host: 
-getH
+h
 ostname
-(url)
 ,%0A%09%09%09%09po
@@ -807,39 +807,48 @@
 th: 
-url.replace(getHostname(url), '
+'/' + url.split(/%5C/+/g).slice(2).join('/
 '),%0A
@@ -871,16 +871,41 @@
 '%0A%09%09%09%7D;%0A
+%09%09%09console.log(options);%0A
 %09%09%09var r
@@ -940,17 +940,16 @@
 ion(res)
- 
 %7B%0A%09%09%09%09va
@@ -1579,16 +1579,18 @@
 %5C/head%3E/
+im
 .exec(bo
@@ -2047,24 +2047,51 @@
 %0A%09%09%09%09%7D);%0A%09%09%09
+%7D).on('error', function(e)%7B
 %7D);%0A%09%09%7D%0A%7D;%0Am
@@ -2384,24 +2384,35 @@
 = channel)%7B%0A
+%09%09%09%09%09try%7B%0A%09
 %09%09%09%09%09checkFo
@@ -2431,16 +2431,33 @@
 , bot);%0A
+%09%09%09%09%09%7Dcatch(e)%7B%7D%0A
 %09%09%09%09%7D%0A%09%09

Fix the title bugs