diff options
Diffstat (limited to 'beinc_weechat.py')
| -rw-r--r-- | beinc_weechat.py | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/beinc_weechat.py b/beinc_weechat.py new file mode 100644 index 0000000..4abf6f1 --- /dev/null +++ b/beinc_weechat.py | |||
| @@ -0,0 +1,144 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import json | ||
| 5 | import os | ||
| 6 | import socket | ||
| 7 | import sys | ||
| 8 | |||
| 9 | import weechat | ||
| 10 | |||
| 11 | enabled = True | ||
| 12 | |||
| 13 | class WeechatTarget(object): | ||
| 14 | """ | ||
| 15 | """ | ||
| 16 | |||
| 17 | def __init__(self, target_dict): | ||
| 18 | """ | ||
| 19 | target_dict: the config-dictionary node that represents this instance | ||
| 20 | """ | ||
| 21 | |||
| 22 | self.__name = target_dict['name'] | ||
| 23 | self.__url = target_dict['target_url'] | ||
| 24 | self.__password = target_dict['target_password'] | ||
| 25 | self.__title_template = target_dict['title_template'] | ||
| 26 | self.__message_template = target_dict['message_template'] | ||
| 27 | self.__failed = False | ||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | def bosd_send_xosd_message(message): | ||
| 33 | for conn in connections: | ||
| 34 | try: | ||
| 35 | #weechat.prnt("", message) | ||
| 36 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| 37 | s.connect((conn['sbosdd_ip'], conn['sbosdd_port'])) | ||
| 38 | s.send(message) | ||
| 39 | s.close() | ||
| 40 | except: | ||
| 41 | continue | ||
| 42 | |||
| 43 | return True | ||
| 44 | |||
| 45 | |||
| 46 | def bosd_command(data, buffer, args): | ||
| 47 | global enabled | ||
| 48 | if args == 'on': | ||
| 49 | enabled = True | ||
| 50 | weechat.prnt(weechat.current_buffer(), 'bosd on') | ||
| 51 | elif args == 'off': | ||
| 52 | enabled = False | ||
| 53 | weechat.prnt(weechat.current_buffer(), 'bosd off') | ||
| 54 | elif args == 'reload': | ||
| 55 | weechat.prnt(weechat.current_buffer(), '{0} reloaded') | ||
| 56 | else: | ||
| 57 | bosd_send_xosd_message(args) | ||
| 58 | |||
| 59 | return weechat.WEECHAT_RC_OK | ||
| 60 | |||
| 61 | |||
| 62 | def bosd_privmsg_handler(data, signal, signal_data): | ||
| 63 | if not enabled: | ||
| 64 | return weechat.WEECHAT_RC_OK | ||
| 65 | |||
| 66 | prvmsg_dict = weechat.info_get_hashtable("irc_message_parse", | ||
| 67 | { "message": signal_data }) | ||
| 68 | |||
| 69 | |||
| 70 | server = signal.split(",")[0] | ||
| 71 | |||
| 72 | # channel = signal_data.split(":")[-1] | ||
| 73 | # my_nick = weechat.info_get("irc_nick_from_host", signal_data) | ||
| 74 | |||
| 75 | my_nick = weechat.info_get("irc_nick", server) | ||
| 76 | channel = prvmsg_dict['arguments'].split(":")[0].strip() | ||
| 77 | nick = prvmsg_dict['nick'] | ||
| 78 | message = ':'.join(prvmsg_dict['arguments'].split(':')[1:]).strip() | ||
| 79 | |||
| 80 | if (my_nick.lower() in message.lower()) or ('{0}.{1}'.format(server,channel.lower()) in notify_channels): | ||
| 81 | template_msg = '{0} @ {1} - {2}: {3}'.format(channel, | ||
| 82 | server, | ||
| 83 | nick, | ||
| 84 | message) | ||
| 85 | bosd_send_xosd_message(template_msg) | ||
| 86 | |||
| 87 | if my_nick.lower() in channel.lower(): | ||
| 88 | template_msg = '{0} @ {1}: {2}'.format(nick, server, message) | ||
| 89 | bosd_send_xosd_message(template_msg) | ||
| 90 | |||
| 91 | # weechat.prnt("", | ||
| 92 | # 'DEBUG: my_nick - {0}, server - {1}, channel - {2}'.format(my_nick, | ||
| 93 | # server, | ||
| 94 | # channel)) | ||
| 95 | |||
| 96 | return weechat.WEECHAT_RC_OK | ||
| 97 | |||
| 98 | |||
| 99 | def beinc_init(): | ||
| 100 | |||
| 101 | global enabled | ||
| 102 | global target_list | ||
| 103 | |||
| 104 | custom_error = '' | ||
| 105 | |||
| 106 | try: | ||
| 107 | beinc_config_file_str = os.path.join(weechat.info_get('weechat_dir', | ||
| 108 | ''), | ||
| 109 | 'beinc.json') | ||
| 110 | weechat.prnt('', 'Parsing {0}...'.format(beinc_config_file_str)) | ||
| 111 | |||
| 112 | custom_error = 'load error' | ||
| 113 | with open(beinc_config_file_str, 'r') as fp: | ||
| 114 | config_dict = json.load(fp) | ||
| 115 | |||
| 116 | # clear the target-list | ||
| 117 | target_list = [] | ||
| 118 | |||
| 119 | custom_error = 'target parse error' | ||
| 120 | for target in config_dict['irc_client']['targets']: | ||
| 121 | new_target = WeechatTarget(target) | ||
| 122 | target_list.append(new_target) | ||
| 123 | |||
| 124 | weechat.prnt('', 'Done!!!') | ||
| 125 | |||
| 126 | except Exception as e: | ||
| 127 | weechat.prnt('', 'ERROR: unable to parse {0}: {1} - {2}'.format( | ||
| 128 | beinc_config_file_str, custom_error, e)) | ||
| 129 | enabled = False | ||
| 130 | |||
| 131 | # do not return error / exit the script | ||
| 132 | # in order to give a smoother opportunity to fix a 'broken' config | ||
| 133 | return weechat.WEECHAT_RC_OK | ||
| 134 | |||
| 135 | return weechat.WEECHAT_RC_OK | ||
| 136 | |||
| 137 | |||
| 138 | weechat.register('beinc_weechat', 'Simeon Simeonov', '1.0', 'GPL3', 'Blackmore\'s Extended IRC Notification Collection (Weechat Client)', '', '') | ||
| 139 | weechat.hook_command('beinc', 'beinc on off toggle', '<on | off | reload>', 'description...', 'None', 'beinc_command', '') | ||
| 140 | weechat.hook_signal('*,irc_in2_privmsg', 'beinc_privmsg_handler', '') | ||
| 141 | |||
| 142 | beinc_init() | ||
| 143 | |||
| 144 | weechat.prnt('', 'beinc initiated!') | ||
