Script: mnotify.py

Mail notifications of private/highlight/DCC and more.
Author: maker — Version: 0.6 — License: Beerware
For WeeChat ≥ 0.3.0.
Tags: notify, py2, py3
Added: 2013-10-19 — Updated: 2019-04-13

Download GitHub Repository

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# -*- coding: utf-8 -*-
# mnotify.py
# Copyleft (c) 2013 maker <maker@python.it>
# Released under Beerware License
#
# based on:
# anotify.py
# Copyright (c) 2012 magnific0 <jacco.geul@gmail.com>
#
# based on:
# growl.py
# Copyright (c) 2011 Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Changelog:
# Ver: 0.4 by Antonin Skala tony762@gmx.com 3.2018
#
# Changed dcc regex to match new notify appears (weechat notify now contain IP)
# Added dcc send offer and dcc send start notify
# Setting for notify is divided to off (don't send), on (always send),
# away (send only when away).
# Changed default settings to match new scheme
# DCC get request show name with ip, network and size of file.
#
# Ver: 0.5 by Antonin Skala tony762@gmx.com 3.2019
# Repaired DCC Get FAILED message.
#
# Ver: 0.6 by Antonin Skala tony762@gmx.com 3.2019
# Support Python 2 and 3
#
# Help:
# Install and configure msmtp first (msmtp.sourceforge.net/)
# List and Change plugin settings by /set plugins.var.python.mnotify.*
# Change language to english -otherwise this will not work
# /set env LANG en_US.UTF-8
# /save
# /upgrade
#
# If running from TMux:
# /usr/bin/tmux -S /tmp/ircmux -2 new-session -d -s irc bash
# /usr/bin/tmux -S /tmp/ircmux -2 send -t irc "export LANG=en_US.UTF-8" ENTER
# /usr/bin/tmux -S /tmp/ircmux -2 send -t irc weechat ENTER

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------

import re
import sys
import subprocess
from email.mime.text import MIMEText

import weechat

SCRIPT_NAME = 'mnotify'
SCRIPT_AUTHOR = 'maker'
SCRIPT_VERSION = '0.6'
SCRIPT_LICENSE = 'Beerware License'
SCRIPT_DESC = 'Sends mail notifications upon events.'

# -----------------------------------------------------------------------------
# Settings
# -----------------------------------------------------------------------------
SETTINGS = {
    'show_public_message': 'off',
    'show_private_message': 'away',
    'show_public_action_message': 'off',
    'show_private_action_message': 'away',
    'show_notice_message': 'off',
    'show_invite_message': 'away',
    'show_highlighted_message': 'off',
    'show_server': 'away',
    'show_channel_topic': 'off',
    'show_dcc': 'on',
    'show_upgrade_ended': 'off',
    'sendmail': '/usr/bin/msmtp',
    'email_to': 'somebody@somwhere.xx',
    'email_from': 'irc@somwhere.xx'
}


# -----------------------------------------------------------------------------
# Globals
# -----------------------------------------------------------------------------
TAGGED_MESSAGES = {
    'public message or action': set(['irc_privmsg', 'notify_message']),
    'private message or action': set(['irc_privmsg', 'notify_private']),
    'notice message': set(['irc_notice', 'notify_private']),
    'invite message': set(['irc_invite', 'notify_highlight']),
    'channel topic': set(['irc_topic', ]),
}


UNTAGGED_MESSAGES = {
    'away status':
        re.compile(r'^You ((\w+).){2,3}marked as being away', re.UNICODE),
    'dcc chat request':
        re.compile(r'^xfer: incoming chat request from ([^\s]+) ', re.UNICODE),
    'dcc chat closed':
        re.compile(r'^xfer: chat closed with ([^\s]+) \(', re.UNICODE),
    'dcc get request':
        re.compile(
            r'^xfer: incoming file from (^\s|.+), name: ((?:,\w|[^,])+), (\d+) bytes',
            re.UNICODE),
    'dcc get completed':
        re.compile(r'^xfer: file ((?:,\w|[^,])+) received from ([^\s]+) ((?:,\w|[^,]+)): OK$', re.UNICODE),
    'dcc get failed':
        re.compile(
            r'^xfer: file ((?:,\w|[^,])+) received from ([^\s]+) ((?:,\w|[^,]+)): FAILED$',
            re.UNICODE),
    'dcc send offer':
        re.compile(r'^xfer: offering file to ([^\s]+) ((?:,\w|[^,])+), name: ((?:,\w|[^,])+) \(local', re.UNICODE),
    'dcc send start':
        re.compile(r'^xfer: sending file to ([^\s]+) ((?:,\w|.)+), name: ((?:,\w|[^,])+) \(local', re.UNICODE),
    'dcc send completed':
        re.compile(r'^xfer: file ((?:,\w|[^,])+) sent to ([^\s]+) ((?:,\w|[^,]+)): OK$', re.UNICODE),
    'dcc send failed':
        re.compile(r'^xfer: file ((?:,\w|[^,])+) sent to ([^\s]+) ((?:,\w|[^,]+)): FAILED$', re.UNICODE),
}


DISPATCH_TABLE = {
    'away status': 'set_away_status',
    'public message or action': 'notify_public_message_or_action',
    'private message or action': 'notify_private_message_or_action',
    'notice message': 'notify_notice_message',
    'invite message': 'notify_invite_message',
    'channel topic': 'notify_channel_topic',
    'dcc chat request': 'notify_dcc_chat_request',
    'dcc chat closed': 'notify_dcc_chat_closed',
    'dcc get request': 'notify_dcc_get_request',
    'dcc get completed': 'notify_dcc_get_completed',
    'dcc get failed': 'notify_dcc_get_failed',
    'dcc send offer': 'notify_dcc_send_offer',
    'dcc send start': 'notify_dcc_send_start',
    'dcc send completed': 'notify_dcc_send_completed',
    'dcc send failed': 'notify_dcc_send_failed',
}


STATE = {
    'icon': None,
    'is_away': False
}


# -----------------------------------------------------------------------------
# Notifiers
# -----------------------------------------------------------------------------
def cb_irc_server_connected(data, signal, signal_data):
    '''Notify when connected to IRC server.'''
    if (weechat.config_get_plugin('show_server') == 'on'
        or (weechat.config_get_plugin('show_server') == "away"
            and STATE['is_away'])):
        a_notify(
            'Server',
            'Server Connected',
            'Connected to network {0}.'.format(signal_data))
    return weechat.WEECHAT_RC_OK


def cb_irc_server_disconnected(data, signal, signal_data):
    '''Notify when disconnected to IRC server.'''
    if (weechat.config_get_plugin('show_server') == 'on'
        or (weechat.config_get_plugin('show_server') == "away"
            and STATE['is_away'])):
        a_notify(
            'Server',
            'Server Disconnected',
            'Disconnected from network {0}.'.format(signal_data))
    return weechat.WEECHAT_RC_OK


def cb_notify_upgrade_ended(data, signal, signal_data):
    '''Notify on end of WeeChat upgrade.'''
    if (weechat.config_get_plugin('show_upgrade_ended') == 'on'
        or (weechat.config_get_plugin('show_upgrade_ended') == "away"
            and STATE['is_away'])):
        a_notify(
            'WeeChat',
            'WeeChat Upgraded',
            'WeeChat has been upgraded.')
    return weechat.WEECHAT_RC_OK


def notify_highlighted_message(buffername, prefix, message):
    '''Notify on highlighted message.'''
    if (weechat.config_get_plugin("show_highlighted_message") == "on"
        or (weechat.config_get_plugin("show_highlighted_message") == "away"
            and STATE['is_away'])):
        a_notify(
            'Highlight',
            'Highlighted on {0} by {1}'.format(buffername, prefix),
            "{0}: {1}".format(prefix, message),
        )


def notify_public_message_or_action(buffername, prefix, message, highlighted):
    '''Notify on public message or action.'''
    if prefix == ' *':
        regex = re.compile(r'^(\w+) (.+)$', re.UNICODE)
        match = regex.match(message)
        if match:
            prefix = match.group(1)
            message = match.group(2)
            notify_public_action_message(buffername, prefix,
                                         message, highlighted)
    else:
        if highlighted:
            notify_highlighted_message(buffername, prefix, message)
        elif (weechat.config_get_plugin("show_public_message") == "on"
              or (weechat.config_get_plugin("show_public_message") == "away"
                  and STATE['is_away'])):
            a_notify(
                'Public',
                'Public Message on {0}'.format(buffername),
                '{0}: {1}'.format(prefix, message))


def notify_private_message_or_action(buffername, prefix, message, highlighted):
    '''Notify on private message or action.'''
    regex = re.compile(r'^CTCP_MESSAGE.+?ACTION (.+)$', re.UNICODE)
    match = regex.match(message)
    if match:
        notify_private_action_message(buffername, prefix,
                                      match.group(1), highlighted)
    else:
        if prefix == ' *':
            regex = re.compile(r'^(\w+) (.+)$', re.UNICODE)
            match = regex.match(message)
            if match:
                prefix = match.group(1)
                message = match.group(2)
                notify_private_action_message(buffername, prefix,
                                              message, highlighted)
        else:
            if highlighted:
                notify_highlighted_message(buffername, prefix, message)
            elif (weechat.config_get_plugin("show_private_message") == "on"
                  or (weechat.config_get_plugin("show_private_message") == "away"
                      and STATE['is_away'])):
                a_notify(
                    'Private',
                    'Private Message',
                    '{0}: {1}'.format(prefix, message))


def notify_public_action_message(buffername, prefix, message, highlighted):
    '''Notify on public action message.'''
    if highlighted:
        notify_highlighted_message(buffername, prefix, message)
    elif (weechat.config_get_plugin("show_public_action_message") == "on"
          or (weechat.config_get_plugin("show_public_action_message") == "away"
              and STATE['is_away'])):
        a_notify(
            'Action',
            'Public Action Message on {0}'.format(buffername),
            '{0}: {1}'.format(prefix, message),
        )


def notify_private_action_message(buffername, prefix, message, highlighted):
    '''Notify on private action message.'''
    if highlighted:
        notify_highlighted_message(buffername, prefix, message)
    elif (weechat.config_get_plugin("show_private_action_message") == "on"
          or (weechat.config_get_plugin("show_private_action_message") == "away"
              and STATE['is_away'])):
        a_notify(
            'Action',
            'Private Action Message',
            '{0}: {1}'.format(prefix, message),
        )


def notify_notice_message(buffername, prefix, message, highlighted):
    '''Notify on notice message.'''
    regex = re.compile(r'^([^\s]*) [^:]*: (.+)$', re.UNICODE)
    match = regex.match(message)
    if match:
        prefix = match.group(1)
        message = match.group(2)
        if highlighted:
            notify_highlighted_message(buffername, prefix, message)
        elif (weechat.config_get_plugin("show_notice_message") == "on"
              or (weechat.config_get_plugin("show_notice_message") == "away"
                  and STATE['is_away'])):
            a_notify(
                'Notice',
                'Notice Message',
                '{0}: {1}'.format(prefix, message))


def notify_invite_message(buffername, prefix, message, highlighted):
    '''Notify on channel invitation message.'''
    if (weechat.config_get_plugin("show_invite_message") == "on"
        or (weechat.config_get_plugin("show_invite_message") == "away"
            and STATE['is_away'])):
        regex = re.compile(
            r'^You have been invited to ([^\s]+) by ([^\s]+)$', re.UNICODE)
        match = regex.match(message)
        if match:
            channel = match.group(1)
            nick = match.group(2)
            a_notify(
                'Invite',
                'Channel Invitation',
                '{0} has invited you to join {1}.'.format(nick, channel))


def notify_channel_topic(buffername, prefix, message, highlighted):
    '''Notify on channel topic change.'''
    if (weechat.config_get_plugin("show_channel_topic") == "on"
        or (weechat.config_get_plugin("show_channel_topic") == "away"
            and STATE['is_away'])):
        regex = re.compile(
            r'^\w+ has (?:changed|unset) topic for ([^\s]+)' +
            r'(?:(?: from "(?:.+)")? to "(.+)")?',
            re.UNICODE)
        match = regex.match(message)
        if match:
            channel = match.group(1)
            topic = match.group(2) or ''
            a_notify(
                'Channel',
                'Channel Topic on {0}'.format(buffername),
                "{0}: {1}".format(channel, topic))


def notify_dcc_chat_request(match):
    '''Notify on DCC chat request.'''
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(1)
        a_notify(
            'DCC',
            'Direct Chat Request',
            '{0} wants to chat directly.'.format(nick))


def notify_dcc_chat_closed(match):
    '''Notify on DCC chat termination.'''
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(1)
        a_notify(
            'DCC',
            'Direct Chat Ended',
            'Direct chat with {0} has ended.'.format(nick))


def notify_dcc_get_request(match):
    'Notify on DCC get request.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(1)
        file_name = match.group(2)
        file_size = int(match.group(3))
        a_notify(
            'DCC',
            'File Transfer Request',
            '{0} wants to send you {1} and size is {2}.'.format(nick, file_name, humanbytes(file_size)))


def notify_dcc_get_completed(match):
    'Notify on DCC get completion.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(2)
        file_name = match.group(1)
        a_notify(
            'DCC',
            'Download Complete',
            'Downloading {1} from {0} completed.'.format(nick, file_name))


def notify_dcc_get_failed(match):
    'Notify on DCC get failure.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(2)
        file_name = match.group(1)
        a_notify(
            'DCC',
            'Download Failed',
            'Downloading {1} from {0} failed.'.format(nick, file_name))


def notify_dcc_send_offer(match):
    'Notify on DCC send offer.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(1)
        file_name = match.group(3)
        a_notify(
            'DCC',
            'Offering File Upload',
            'Offering {1} to {0}.'.format(nick, file_name))


def notify_dcc_send_start(match):
    'Notify on DCC send start.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(1)
        file_name = match.group(3)
        a_notify(
            'DCC',
            'Start File Upload',
            'Uploading {1} to {0}.'.format(nick, file_name))


def notify_dcc_send_completed(match):
    'Notify on DCC send completion.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(2)
        file_name = match.group(1)
        a_notify(
            'DCC',
            'Upload Complete',
            'Upload {1} to {0} completed.'.format(nick, file_name))


def notify_dcc_send_failed(match):
    'Notify on DCC send failure.'
    if (weechat.config_get_plugin("show_dcc") == "on"
        or (weechat.config_get_plugin("show_dcc") == "away"
            and STATE['is_away'])):
        nick = match.group(2)
        file_name = match.group(1)
        a_notify(
            'DCC',
            'Upload Failed',
            'Upload {1} to {0} failed.'.format(nick, file_name))


# -----------------------------------------------------------------------------
# Utility
# -----------------------------------------------------------------------------
def set_away_status(match):
    status = match.group(1)
    if status == 'been ':
        STATE['is_away'] = True
    if status == 'longer ':
        STATE['is_away'] = False


def cb_process_message(
    data,
    wbuffer,
    date,
    tags,
    displayed,
    highlight,
    prefix,
    message
):
    '''Delegates incoming messages to appropriate handlers.'''
    tags = set(tags.split(','))
    functions = globals()
    is_public_message = tags.issuperset(
        TAGGED_MESSAGES['public message or action'])
    buffer_name = weechat.buffer_get_string(wbuffer, 'name')
    dcc_buffer_regex = re.compile(r'^irc_dcc\.', re.UNICODE)
    dcc_buffer_match = dcc_buffer_regex.match(buffer_name)
    highlighted = False
    if int(highlight):
        highlighted = True
    # Private DCC message identifies itself as public.
    if is_public_message and dcc_buffer_match:
        notify_private_message_or_action(buffer_name, prefix,
                                         message, highlighted)
        return weechat.WEECHAT_RC_OK
    # Pass identified, untagged message to its designated function.
    for key, value in UNTAGGED_MESSAGES.items():
        match = value.match(message)
        if match:
            functions[DISPATCH_TABLE[key]](match)
            return weechat.WEECHAT_RC_OK
    # Pass identified, tagged message to its designated function.
    for key, value in TAGGED_MESSAGES.items():
        if tags.issuperset(value):
            functions[DISPATCH_TABLE[key]](buffer_name, prefix,
                                           message, highlighted)
            return weechat.WEECHAT_RC_OK
    return weechat.WEECHAT_RC_OK


def humanbytes(B):
    B = float(B)
    KB = float(1024)
    MB = float(KB ** 2)  # 1,048,576
    GB = float(KB ** 3)  # 1,073,741,824
    TB = float(KB ** 4)  # 1,099,511,627,776
    if B < KB:
        return '{0} {1}'.format(B, 'Bytes' if 0 == B > 1 else 'Byte')
    elif KB <= B < MB:
        return '{0:.2f} KB'.format(B/KB)
    elif MB <= B < GB:
        return '{0:.2f} MB'.format(B/MB)
    elif GB <= B < TB:
        return '{0:.2f} GB'.format(B/GB)
    elif TB <= B:
        return '{0:.2f} TB'.format(B/TB)


def a_notify(notification, subject, message):
    msg = MIMEText(message)
    msg['From'] = weechat.config_get_plugin('email_from')
    msg['To'] = weechat.config_get_plugin('email_to')
    msg['Subject'] = subject

    p = subprocess.Popen(
        [weechat.config_get_plugin('sendmail'),
         weechat.config_get_plugin('email_to')],
        stdin=subprocess.PIPE,

    )
    if sys.version_info[0] > 2:
        p.communicate(input=str.encode(msg.as_string()))
    else:
        p.communicate(input=str(msg))


# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
def main():
    '''Sets up WeeChat notifications.'''
    # Initialize options.
    for option, value in SETTINGS.items():
        if not weechat.config_is_set_plugin(option):
            weechat.config_set_plugin(option, value)
    # Register hooks.
    weechat.hook_signal(
        'irc_server_connected',
        'cb_irc_server_connected',
        '')
    weechat.hook_signal(
        'irc_server_disconnected',
        'cb_irc_server_disconnected',
        '')
    weechat.hook_signal('upgrade_ended', 'cb_upgrade_ended', '')
    weechat.hook_print('', '', '', 1, 'cb_process_message', '')


if __name__ == '__main__' and weechat.register(
    SCRIPT_NAME,
    SCRIPT_AUTHOR,
    SCRIPT_VERSION,
    SCRIPT_LICENSE,
    SCRIPT_DESC,
    '',
    ''
):
    main()