Skip to content

Commit

Permalink
Merge branch 'release-0.5'.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rvac committed May 27, 2016
2 parents 669cf89 + 17367b8 commit 1fbc360
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
__pycache__
*.py[co]

# Unit tests and coverage reports
# Code coverage settings and reports
/.coverage
/coverage
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ dev

* -

0.5 (2016-05-27)
----------------

* Added a new option: `ignore_buffers`. It is a comma-separated list of buffers
from which no notifications should be shown.
* Added a new option: `ignore_buffers_starting_with`. It is a comma-separated
list of buffer prefixes from which no notifications should be shown.
* Show default values of options in their descriptions (e.g. when viewed via
`iset.pl`).

0.4 (2016-04-23)
----------------

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ plugins.var.python.notify_send.XXX YYY` or by using the
notifications from the same buffer. It is used to protect from floods/spam.
Set it to `0` to disable this feature (i.e. all notifications will be shown).
Default: `500` milliseconds.
* `ignore_buffers`: A comma-separated list of buffers from which no
notifications should be shown. You can use either short names (`#buffer`) or
full names (`network.#buffer`). Default: `''`.
* `ignore_buffers_starting_with`: A comma-separated list of buffer prefixes
from which no notifications should be shown. Default: `''`.
* `ignore_nicks`: A comma-separated list of nicks from which no notifications
should be shown. Default: `''`.
* `ignore_nicks_starting_with`: A comma-separated list of nick prefixes from
Expand Down
68 changes: 64 additions & 4 deletions notify_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import time


# Ensure that we are running under weechat.
# Ensure that we are running under WeeChat.
try:
import weechat
except ImportError:
Expand All @@ -49,7 +49,7 @@
SCRIPT_AUTHOR = 's3rvac'

# Version of the script.
SCRIPT_VERSION = '0.4'
SCRIPT_VERSION = '0.5'

# License under which the script is distributed.
SCRIPT_LICENSE = 'MIT'
Expand Down Expand Up @@ -87,6 +87,16 @@
'A minimal delay between successive notifications from the same '
'buffer (in milliseconds; set to 0 to show all notifications).'
),
'ignore_buffers': (
'',
'A comma-separated list of buffers from which no notifications should '
'be shown.'
),
'ignore_buffers_starting_with': (
'',
'A comma-separated list of buffer prefixes from which no '
'notifications should be shown.'
),
'ignore_nicks': (
'',
'A comma-separated list of nicks from which no notifications should '
Expand Down Expand Up @@ -145,6 +155,15 @@ def default_value_of(option):
return OPTIONS[option][0]


def add_default_value_to(description, default_value):
"""Adds the given default value to the given option description."""
# All descriptions end with a period, so do not add another period.
return '{} Default: {}.'.format(
description,
default_value if default_value else '""'
)


def nick_from_prefix(prefix):
"""Returns a nick from the given prefix.
Expand Down Expand Up @@ -190,7 +209,10 @@ def notification_should_be_sent_disregarding_time(buffer, nick, is_highlight):
if not notify_when_away():
return False

if ignore_notifications_from(nick):
if ignore_notifications_from_nick(nick):
return False

if ignore_notifications_from_buffer(buffer):
return False

if is_private_message(buffer):
Expand Down Expand Up @@ -292,7 +314,44 @@ def i_am_author_of_message(buffer, nick):
return weechat.buffer_get_string(buffer, 'localvar_nick') == nick


def ignore_notifications_from(nick):
def ignore_notifications_from_buffer(buffer):
"""Should notifications from the given buffer be ignored?"""
# The 'buffer' parameter is actually the buffer's ID (e.g. '0x2719cf0'). We
# have to check its name (e.g. 'freenode.#weechat') and short name (e.g.
# '#weechat').
buffer_names = [
weechat.buffer_get_string(buffer, 'short_name'),
weechat.buffer_get_string(buffer, 'name')
]

for buffer_name in buffer_names:
if buffer_name and buffer_name in ignored_buffers():
return True

for buffer_name in buffer_names:
for prefix in ignored_buffer_prefixes():
if prefix and buffer_name and buffer_name.startswith(prefix):
return True

return False


def ignored_buffers():
"""A generator of buffers from which notifications should be ignored."""
for buffer in weechat.config_get_plugin('ignore_buffers').split(','):
yield buffer.strip()


def ignored_buffer_prefixes():
"""A generator of buffer prefixes from which notifications should be
ignored.
"""
prefixes = weechat.config_get_plugin('ignore_buffers_starting_with')
for prefix in prefixes.split(','):
yield prefix.strip()


def ignore_notifications_from_nick(nick):
"""Should notifications from the given nick be ignored?"""
if nick in ignored_nicks():
return True
Expand Down Expand Up @@ -423,6 +482,7 @@ def send_notification(notification):

# Initialization.
for option, (default_value, description) in OPTIONS.items():
description = add_default_value_to(description, default_value)
weechat.config_set_desc_plugin(option, description)
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, default_value)
Expand Down
Loading

0 comments on commit 1fbc360

Please sign in to comment.