Last active
March 10, 2017 18:38
-
-
Save dstanek/d1bdc7eae621e821087d0a514d10dfd5 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
# Copyright 2016 David Stanek <[email protected]> | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); you may | |
# not use this file except in compliance with the License. You may obtain | |
# a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
# License for the specific language governing permissions and limitations | |
# under the License. | |
import collections | |
import subprocess | |
import time | |
import weechat | |
script_name = 'alert.py' | |
author = 'David Stanek <[email protected]>' | |
version = '0.1' | |
license = 'Apache 2.0' | |
description = 'send some alerts using notify-send' | |
settings = { | |
'icon': '/usr/share/icons/hicolor/32x32/apps/weechat.png', | |
'mention_timeout': str(1 * 60 * 1000), # 1 minute | |
'connection_timeout': str(15 * 1000), # 15 seconds | |
'dm_timeout': str(5 * 60 * 1000), # 5 minutes | |
} | |
class Message(object): | |
def __init__(self, data): | |
self._msg_hash = weechat.info_get_hashtable( | |
'irc_message_parse', {'message': data}) | |
text = property(lambda self: self._msg_hash['text']) | |
from_nick = property(lambda self: self._msg_hash['nick']) | |
channel = property(lambda self: self._msg_hash['channel']) | |
class ConversationTracker(object): | |
TIMEOUT = 120 | |
def __init__(self): | |
# ('nick', 'channel'): last_active_in_conversation}, | |
self._active = collections.defaultdict(int) | |
def add_message_to_conversation(self, nick, channel): | |
self._active[(nick, channel)] = time.time() | |
def is_in_conversation(self, nick, channel): | |
return time.time() - self._active[(nick, channel)] <= self.TIMEOUT | |
def _trim_message(message): | |
return message if len(message) <= 128 else message[:128] + ' ...' | |
def notify_send(title, body, timeout=None): | |
icon = weechat.config_get_plugin('icon') | |
timeout = timeout or str(60 * 1000 * 3) | |
subprocess.call(['notify-send', '-i', icon, '-t', timeout, title, body]) | |
def recv_message(data, signal, signal_data): | |
message = Message(signal_data) | |
server = signal.split(',')[0] | |
my_nick = weechat.info_get('irc_nick', server).lower() | |
if message.channel == my_nick: # private message | |
title = 'private message from {0}'.format(message.from_nick) | |
timeout = weechat.config_get_plugin('dm_timeout') | |
notify_send(title, _trim_message(message.text), timeout) | |
return weechat.WEECHAT_RC_OK | |
if my_nick in message.text: # mention | |
if '@here' in message.text: | |
# slack sucks | |
return weechat.WEECHAT_RC_OK | |
title = '{0} on {1}'.format(message.from_nick, message.channel) | |
timeout = weechat.config_get_plugin('mention_timeout') | |
notify_send(title, _trim_message(message.text), timeout) | |
active_conversations.add_message_to_conversation(message.from_nick, message.channel) | |
elif active_conversations.is_in_conversation(message.from_nick, message.channel): | |
title = '... {0} on {1}'.format(message.from_nick, message.channel) | |
timeout = weechat.config_get_plugin('mention_timeout') | |
notify_send(title, _trim_message(message.text), timeout) | |
return weechat.WEECHAT_RC_OK | |
def server_connected(data, signal, signal_data): | |
timeout = weechat.config_get_plugin('connection_timeout') | |
body = 'Connected to network {0}.'.format(signal_data) | |
notify_send('Connected', body, timeout) | |
return weechat.WEECHAT_RC_OK | |
def server_disconnected(data, signal, signal_data): | |
timeout = weechat.config_get_plugin('connection_timeout') | |
body = 'Disconnected from network {0}.'.format(signal_data) | |
notify_send('Disconnected', body, timeout) | |
return weechat.WEECHAT_RC_OK | |
if __name__ == '__main__': | |
active_conversations = ConversationTracker() | |
weechat.register(script_name, author, version, license, description, '', '') | |
for option, default_value in settings.items(): | |
if weechat.config_get_plugin(option) == '': | |
weechat.config_set_plugin(option, default_value) | |
weechat.hook_signal('*,irc_in2_PRIVMSG', 'recv_message', '') | |
weechat.hook_signal('irc_server_connected', 'server_connected', '') | |
weechat.hook_signal('irc_server_disconnected', 'server_disconnected', '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment