Created
August 8, 2010 18:33
-
-
Save inklesspen/514389 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var chefs = {}; | |
| var chefCount = 0; | |
| var wordFinder = /\w+/g; | |
| var chefCountPrefName = "bork.chefCount"; | |
| function chefPrefName(n) { | |
| return "bork.chef[" + n + "]"; | |
| } | |
| // called on load and reload | |
| function load( scriptFilePath ) { | |
| var defaults = new NSUserDefaults(); | |
| var count = defaults.integerForKey_("bork.chefCount"); | |
| if (count) { | |
| for (var n = 0; n < count; n++) { | |
| var possible = defaults.stringForKey_(chefPrefName(n)); | |
| if (possible) { | |
| chefs[possible] = true; | |
| chefCount++; | |
| } | |
| } | |
| } | |
| } | |
| // process the command and return true if you handle it or false to pass on to another plugin | |
| function processUserCommand( command, arguments, connection, view ) { | |
| // return true if the command was handled or to prevent other plugins or Colloquy from handling it | |
| if (command === "bork") { | |
| var defaults = new NSUserDefaults(); | |
| var hostmask = arguments.substr(1); | |
| if (arguments[0] === "+" && !(hostmask in chefs)) { | |
| chefs[hostmask] = true; | |
| chefCount++; | |
| alert("Added " + hostmask + " to list of chefs."); | |
| } else if (arguments[0] === "-" && (hostmask in chefs)) { | |
| delete chefs[hostmask]; | |
| // remove the highest key; we'll be rewriting all of them shortly | |
| defaults.removeObjectForKey_(chefPrefName(chefCount)); | |
| chefCount--; | |
| alert("Removed " + hostmask + " from list of chefs."); | |
| } | |
| defaults.setInteger_forKey_(chefCount, "bork.chefCount"); | |
| var n = 0; | |
| for (var chef in chefs) { | |
| if (chefs.hasOwnProperty(chef) && chefs[chef]) { | |
| defaults.setObject_forKey_(chef, chefPrefName(n)); | |
| n++; | |
| } | |
| } | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| // called for each incoming message, the message is mutable | |
| function processIncomingMessage( message, view ) { | |
| if (message.senderHostmask() in chefs) { | |
| var newBody = message.bodyAsPlainText().replace(wordFinder, "bork"); | |
| message.setBodyAsPlainText_(newBody); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment