Last active
October 12, 2015 15:48
-
-
Save zwily/4050392 to your computer and use it in GitHub Desktop.
inputPlugin for LimeChat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Save this file as: ~/Library/Application Support/LimeChat/inputPlugin.js | |
// And download this version of LimeChat that supports inputPlugin.js: | |
// http://cl.ly/0h3B3t3a3H3L | |
// | |
// That build of LimeChat includes this commit: | |
// https://github.com/zwily/limechat/commit/db1cdee2a4b90c3bc2e4956794d92f42a7264193 | |
// | |
// Shortens URLs that look like graphite using goo.gl. This could | |
// also be more generic, and just shortens any long URL (that will | |
// probably end up in a line being split by limechat to get around | |
// IRC line length limits.) | |
function shortenGraphiteURLs(line) { | |
var graphiteRegex = new RegExp('https?:\\/\\/([a-z0-9.]+)\\/(?:render|graphlot)\\/\\?\\S+', "g"); | |
var titleRegex = new RegExp('title=([^&]+)'); | |
if (line.indexOf("render") > 0 || line.indexOf("graphlot") > 0) { | |
line = line.replace(graphiteRegex, function(url, domain) { | |
var title = false; | |
var match = titleRegex.exec(url); | |
if (match) { | |
title = decodeURIComponent(match[1]); | |
} | |
// Yeah, we're doing a synchronous AJAX request, cause this method is called | |
// synchronously. If google starts puking out, that could cause limechat to | |
// lock up while shortening a URL. | |
var ajax = new XMLHttpRequest(); | |
ajax.open("POST", "https://www.googleapis.com/urlshortener/v1/url", false); | |
ajax.setRequestHeader("Content-Type", "application/json"); | |
ajax.send(JSON.stringify({ "longUrl": url })); | |
var json = JSON.parse(ajax.responseText); | |
if (json.id) { | |
var res = json.id + " \x0315["; | |
if (title) { | |
res = res + "\x0303" + title + " "; | |
} | |
res = res + "\x0314" + domain + "\x0315]\x03 "; | |
return res; | |
} | |
return url; | |
}); | |
} | |
return line; | |
} | |
// Let's you [[blue]]colorize![[]] your IRC text. | |
function colorize(line) { | |
var colors = { | |
'00': '00', '01': '01', '02': '02', '03': '03', '04': '04', | |
'05': '05', '06': '06', '07': '07', '08': '08', '09': '09', | |
'10': '10', '11': '11', '12': '12', '13': '13', '14': '14', | |
'15': '15', 'white': '00', 'black': '01', 'blue': '02', | |
'green': '03', 'red': '04', 'brown': '05', 'purple': '06', | |
'orange': '07', 'yellow': '08', 'lime': '09', 'teal': '10', | |
'cyan': '11', 'aqua': '11', 'royal': '12', 'pink': '13', | |
'grey': '14', 'gray': '14', 'silver': '15' | |
}; | |
var colorRegex = new RegExp('\\[\\[(?:(\\d{2}|[a-z]{3,6})(?:,(\\d{2}|[a-z]{3,6}))?)?\\]\\]', 'g'); | |
return line.replace(colorRegex, function(match, fg, bg) { | |
if (match == "[[]]") { | |
return "\x03 "; | |
} | |
var fgcode = colors[fg]; | |
var bgcode = colors[bg]; | |
var res = match; | |
if (fgcode) { | |
res = "\x03" + fgcode; | |
if (bgcode) { | |
res = res + "," + bgcode; | |
} | |
} | |
return res; | |
}); | |
} | |
function handleInput(line, command, channel) { | |
try { | |
line = shortenGraphiteURLs(line); | |
line = colorize(line); | |
return line; | |
} | |
catch (e) { | |
return e.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment