summaryrefslogtreecommitdiff
path: root/beinc_weechat.py
blob: 4abf6f1b2f12a4d978c2527ec6a5b90523b32326 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import os
import socket
import sys

import weechat

enabled = True

class WeechatTarget(object):
    """
    """
    
    def __init__(self, target_dict):
        """
        target_dict: the config-dictionary node that represents this instance
        """

        self.__name = target_dict['name']
        self.__url = target_dict['target_url']
        self.__password = target_dict['target_password']
        self.__title_template = target_dict['title_template']
        self.__message_template = target_dict['message_template']
        self.__failed = False
        



def bosd_send_xosd_message(message):
    for conn in connections:
        try:
            #weechat.prnt("", message)
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((conn['sbosdd_ip'], conn['sbosdd_port']))
            s.send(message)
            s.close()
        except:
            continue

    return True


def bosd_command(data, buffer, args):
    global enabled
    if args == 'on':
        enabled = True
        weechat.prnt(weechat.current_buffer(), 'bosd on')
    elif args == 'off':
        enabled = False
        weechat.prnt(weechat.current_buffer(), 'bosd off')
    elif args == 'reload':
        weechat.prnt(weechat.current_buffer(), '{0} reloaded')
    else:
        bosd_send_xosd_message(args)

    return weechat.WEECHAT_RC_OK


def bosd_privmsg_handler(data, signal, signal_data):
    if not enabled:
        return weechat.WEECHAT_RC_OK

    prvmsg_dict = weechat.info_get_hashtable("irc_message_parse",
                                  { "message": signal_data })


    server = signal.split(",")[0]
    
    # channel = signal_data.split(":")[-1]
    # my_nick = weechat.info_get("irc_nick_from_host", signal_data)
   
    my_nick = weechat.info_get("irc_nick", server)
    channel = prvmsg_dict['arguments'].split(":")[0].strip()
    nick = prvmsg_dict['nick']
    message = ':'.join(prvmsg_dict['arguments'].split(':')[1:]).strip()
    
    if (my_nick.lower() in message.lower()) or ('{0}.{1}'.format(server,channel.lower()) in notify_channels):
        template_msg = '{0} @ {1} - {2}: {3}'.format(channel, 
                                                     server, 
                                                     nick, 
                                                     message)
        bosd_send_xosd_message(template_msg)

    if my_nick.lower() in channel.lower():
        template_msg = '{0} @ {1}: {2}'.format(nick, server, message)
        bosd_send_xosd_message(template_msg)

    # weechat.prnt("", 
    #              'DEBUG: my_nick - {0}, server - {1}, channel - {2}'.format(my_nick, 
    #                                                                         server, 
    #                                                                         channel))

    return weechat.WEECHAT_RC_OK


def beinc_init():

    global enabled
    global target_list

    custom_error = ''

    try:
        beinc_config_file_str = os.path.join(weechat.info_get('weechat_dir',
                                                              ''),
                                             'beinc.json')
        weechat.prnt('', 'Parsing {0}...'.format(beinc_config_file_str))

        custom_error = 'load error'
        with open(beinc_config_file_str, 'r') as fp:
            config_dict = json.load(fp)

        # clear the target-list
        target_list = []

        custom_error = 'target parse error'
        for target in config_dict['irc_client']['targets']:
            new_target = WeechatTarget(target)
            target_list.append(new_target)

        weechat.prnt('', 'Done!!!')

    except Exception as e:
        weechat.prnt('', 'ERROR: unable to parse {0}: {1} - {2}'.format(
            beinc_config_file_str, custom_error, e))
        enabled = False

        # do not return error / exit the script
        # in order to give a smoother opportunity to fix a 'broken' config
        return weechat.WEECHAT_RC_OK
    
    return weechat.WEECHAT_RC_OK


weechat.register('beinc_weechat', 'Simeon Simeonov', '1.0', 'GPL3', 'Blackmore\'s Extended IRC Notification Collection (Weechat Client)', '', '')
weechat.hook_command('beinc', 'beinc on off toggle', '<on | off | reload>', 'description...', 'None', 'beinc_command', '')
weechat.hook_signal('*,irc_in2_privmsg', 'beinc_privmsg_handler', '')

beinc_init()

weechat.prnt('', 'beinc initiated!')