From 46018892cb4dc428b0f110fc51e356e21479ffc0 Mon Sep 17 00:00:00 2001 From: Jer Noble Date: Tue, 20 Mar 2012 11:07:20 -0700 Subject: [PATCH 1/1] Initial commit. --- BetterAway.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ HiBack.js | 19 ++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 BetterAway.py create mode 100644 HiBack.js diff --git a/BetterAway.py b/BetterAway.py new file mode 100644 index 0000000..891f192 --- /dev/null +++ b/BetterAway.py @@ -0,0 +1,83 @@ +import objc +from Foundation import * +from AppKit import * +MVConnectionsController = objc.lookUpClass('MVConnectionsController') + +kIdleSuffixKey = 'BetterIdleIdleSuffix' +idleSuffix = '' +sleepObserver = None +wakeObserver = None + +class MyListener (NSObject): + def init(self): + return self + + def onDisplaySleep_(self, notification): + appendIdleSuffix() + pass + + def onDisplayWake_(self, notification): + removeIdleSuffix() + pass + +listener = MyListener.alloc().init() + +def setIdleSuffix( suffix ): + global idleSuffix + NSUserDefaults.standardUserDefaults().setObject_forKey_(suffix, kIdleSuffixKey) + idleSuffix = suffix + +def handleIdleSuffix( command, arguments ): + if not arguments: + return 'Idle suffix is currently "%s"' % idleSuffix + setIdleSuffix( arguments ) + return 'Set idle suffix to "%s"' % idleSuffix + +def appendIdleSuffix(): + for connection in MVConnectionsController.defaultController().connectedConnections(): + connection.setNickname_('%s|%s' % (connection.preferredNickname(), idleSuffix)) + +def removeIdleSuffix(): + for connection in MVConnectionsController.defaultController().connectedConnections(): + connection.setNickname_(connection.preferredNickname()) + +# called on load and reload +def load( scriptFilePath ): + global idleSuffix + defaults = NSUserDefaults.standardUserDefaults() + defaults.registerDefaults_({kIdleSuffixKey:'afk'}) + idleSuffix = defaults.stringForKey_(kIdleSuffixKey) + nc = NSWorkspace.sharedWorkspace().notificationCenter(); + nc.addObserver_selector_name_object_(listener, "onDisplaySleep:", "NSWorkspaceScreensDidSleepNotification", None) + nc.addObserver_selector_name_object_(listener, "onDisplayWake:", "NSWorkspaceScreensDidWakeNotification", None) + pass + +# called on unload and relead +def unload(): + NSUserDefaults.standardUserDefaults().synchronize() + NSWorkspace.sharedWorkspace().notificationCenter().removeObserver_(listener) + pass + +def printString(string, view): + if view.respondsToSelector_('addMessageToDisplay:asOutboundMessage:'): + view.addMessageToDisplay_asOutboundMessage_(string, False) + elif view.respondsToSelector_('addMessageToDisplay:fromUser:asAction:'): + view.addMessageToDisplay_fromUser_asAction_(string, None, True) + elif view.respondsToSelector_('addEventMessageToDisplay:withName:andAttributes:'): + view.addEventMessageToDisplay_withName_andAttributes_(string, 'BetterIdle', None); + +# process the command and return true if you handle it or False to pass on to another plugin +def processUserCommand( command, arguments, connection, view ): + # return true if the command was handled or to prevent other plugins or Colloquy from handling it + commands = {'idlestring': handleIdleSuffix} + if command in commands: + function = commands.get(str(command)) + result = function(str(command), arguments.string()) + printString(result, view) + + removeIdleSuffix() + return command in commands + +def processOutgoingMessage( message, view ): + removeIdleSuffix() + pass \ No newline at end of file diff --git a/HiBack.js b/HiBack.js new file mode 100644 index 0000000..f7b71fd --- /dev/null +++ b/HiBack.js @@ -0,0 +1,19 @@ +function processIncomingMessage( message, view ) { + // Disable HiBack when afk or otherwise away. + if (view.connection().nickname().indexOf('|') != -1) + return; + + // Search for 'hi !' anywhere in the message. + var searchString = 'hi ' + view.connection().nickname() + '!'; + if (message.bodyAsPlainText().indexOf('hi ' + view.connection().nickname() + '!') != -1 ) { + var msg = new JVMutableChatMessage('hi ' + message.senderNickname() + '!', view.connection().localUser()); + + // Wait between 2 and 6 seconds before responding. + var delay = (Math.random()*4000) + 2000; + setTimeout(function(){ + view.sendMessage(msg); + view.echoSentMessageToDisplay(msg); + }, delay); + } +} + -- 2.40.1