weechat-scripts/email-notify.py

109 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Brian S. Stephan <bss@incorporeal.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Convenience commands for appending/prepending text to the channel topic
# or other buffer title.
#
# 2016-05-03 v0.1
# initial script
SCRIPT_NAME = "email-notify"
SCRIPT_AUTHOR = "bss <bss@incorporeal.org>"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Send an email on highlight and privmsg when away"
try:
import weechat
except:
print("This script must be run under WeeChat.")
print("Get WeeChat now at: http://www.weechat.org/")
import os
import subprocess
MAIL = '/usr/bin/mail'
settings = {
'to' : '',
'ignore' : '',
}
last_buffer = ''
def get_nick(s):
"""Strip nickmodes and prefix, suffix."""
if not s: return ''
# prefix and suffix
prefix = weechat.config_string(weechat.config_get('irc.look.nick_prefix'))
suffix = weechat.config_string(weechat.config_get('irc.look.nick_suffix'))
if s[0] == prefix:
s = s[1:]
if s[-1] == suffix:
s = s[:-1]
# nick mode
modes = '~+@!%'
s = s.lstrip(modes)
return s
def away_cb(data, buffer, time, tags, display, hilight, prefix, msg):
global last_buffer
# Check if we are away
if not weechat.buffer_get_string(buffer, 'localvar_away'):
return weechat.WEECHAT_RC_OK
if (hilight == 1 or 'notify_private' in tags) and display == 1:
if hilight == 1:
subj = weechat.buffer_get_string(buffer, 'short_name')
else:
subj = prefix
ignores = weechat.config_get_plugin('ignore').split(',')
if subj not in ignores:
prefix = get_nick(prefix)
last_buffer = weechat.buffer_get_string(buffer, 'plugin') + '.' + weechat.buffer_get_string(buffer, 'name')
send_mail("IRC: {0:s}".format(subj), "<{0:s}> {1:s}".format(prefix, msg))
return weechat.WEECHAT_RC_OK
def send_mail(subject, body):
to_addr = weechat.config_get_plugin('to')
mail = subprocess.Popen([MAIL, '-s', subject, to_addr], stdin=subprocess.PIPE)
mail.stdin.write(bytes(body, 'UTF-8'))
mail.stdin.close()
mail.wait()
def info_hook_cb(data, info_name, arguments):
global last_buffer
return last_buffer
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
for opt, val in settings.items():
if not weechat.config_is_set_plugin(opt):
weechat.config_set_plugin(opt, val)
weechat.hook_print('', '', '', 1, 'away_cb', '')
weechat.hook_info('%s_buffer' %SCRIPT_NAME, '', '', 'info_hook_cb', '')