]> 105106.c2e0p.group Git - colloquy_plugins.git/commitdiff
Initial commit.
authorJer Noble <[email protected]>
Tue, 20 Mar 2012 18:07:20 +0000 (11:07 -0700)
committerJer Noble <[email protected]>
Tue, 20 Mar 2012 18:07:20 +0000 (11:07 -0700)
BetterAway.py [new file with mode: 0644]
HiBack.js [new file with mode: 0644]

diff --git a/BetterAway.py b/BetterAway.py
new file mode 100644 (file)
index 0000000..891f192
--- /dev/null
@@ -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 (file)
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 <nick>!' 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);
+    }
+}
+