Created
December 14, 2012 02:20
-
-
Save adamgreig/4282006 to your computer and use it in GitHub Desktop.
Weechat script to send an email when a new highlight or privmsg is received while away. NB only checks away status of the first server.
This file contains 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
# -*- coding: utf-8 -*- | |
# Copyright 2012 Adam Greig <[email protected]> | |
# | |
# email_away_highlights.py | |
# Email highlights received while away. | |
SCRIPT_NAME = "email_away_highlights" | |
SCRIPT_AUTHOR = "Adam Greig <[email protected]>" | |
SCRIPT_VERSION = "1.0" | |
SCRIPT_LICENSE = "GPL3" | |
SCRIPT_DESC = "Email highlights received while away." | |
import weechat | |
import smtplib | |
settings = { | |
'enabled': ('on', 'Whether or not to email highlights while away'), | |
'from_address': ('', 'Address emails are sent from'), | |
'to_address': ('', 'Address emails are sent to'), | |
'smtp_host': ('localhost', 'SMTP hostname'), | |
'smtp_tls': ('off', 'Use STARTTLS for email'), | |
'smtp_username': ('', 'SMTP username'), | |
'smtp_password': ('', 'SMTP password'), | |
} | |
weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, | |
SCRIPT_DESC, "", "") | |
for option, value in settings.items(): | |
if weechat.config_get_plugin(option) == "": | |
weechat.config_set_plugin(option, value[0]) | |
weechat.config_set_desc_plugin(option, "{1} (default: {0})".format(*value)) | |
weechat.hook_signal("weechat_pv", "send_notification_pv", "") | |
weechat.hook_signal("weechat_highlight", "send_notification_highlight", "") | |
weechat.hook_command("send_test_email", | |
"Send a test email with current settings", "", "", "", "send_test", "") | |
def check_away(): | |
"""really dodgy check if we're away.""" | |
infolist = weechat.infolist_get("irc_server", "", "") | |
weechat.infolist_next(infolist) | |
return weechat.infolist_integer(infolist, "is_away") | |
def send_test(data, buf, args): | |
send_email("Test Email from Weechat", "This is a test email from Weechat.") | |
return weechat.WEECHAT_RC_OK | |
def send_notification_pv(data, signal, message): | |
if check_away(): | |
send_email("IRC privmsg", message) | |
return weechat.WEECHAT_RC_OK | |
def send_notification_highlight(data, signal, message): | |
if check_away(): | |
send_email("IRC highlight", message) | |
return weechat.WEECHAT_RC_OK | |
def send_email(subject, message): | |
to_addr = weechat.config_get_plugin("to_address") | |
from_addr = weechat.config_get_plugin("from_address") | |
smtp_host = weechat.config_get_plugin("smtp_host") | |
smtp_tls = weechat.config_get_plugin("smtp_tls") | |
smtp_username = weechat.config_get_plugin("smtp_username") | |
smtp_password = weechat.config_get_plugin("smtp_password") | |
msg = "From: {from_addr}\r\nTo: {to_addr}\r\nSubject: {subject}\r\n\r\n" | |
msg += "{message}\r\n" | |
msg = msg.format(**locals()) | |
s = smtplib.SMTP(smtp_host) | |
if smtp_tls == "on": | |
s.starttls() | |
if smtp_username != "": | |
s.login(smtp_username, smtp_password) | |
s.sendmail(from_addr, to_addr, msg) | |
s.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment