diff options
Diffstat (limited to 'beinc_weechat.py')
| -rw-r--r-- | beinc_weechat.py | 286 |
1 files changed, 160 insertions, 126 deletions
diff --git a/beinc_weechat.py b/beinc_weechat.py index 75af86a..2288b74 100644 --- a/beinc_weechat.py +++ b/beinc_weechat.py | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | 2 | ||
| 3 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.0 | 3 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.0 |
| 4 | # Copyright (C) 2013-2020 Simeon Simeonov | 4 | # Copyright (C) 2013-2022 Simeon Simeonov |
| 5 | 5 | ||
| 6 | # This program is free software: you can redistribute it and/or modify | 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by | 7 | # it under the terms of the GNU General Public License as published by |
| @@ -17,6 +17,7 @@ | |||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | """BEINC client for Weechat""" | 18 | """BEINC client for Weechat""" |
| 19 | import datetime | 19 | import datetime |
| 20 | import io | ||
| 20 | import json | 21 | import json |
| 21 | import os | 22 | import os |
| 22 | import socket | 23 | import socket |
| @@ -26,9 +27,8 @@ import urllib.request | |||
| 26 | 27 | ||
| 27 | import weechat | 28 | import weechat |
| 28 | 29 | ||
| 29 | |||
| 30 | __author__ = 'Simeon Simeonov' | 30 | __author__ = 'Simeon Simeonov' |
| 31 | __version__ = '4.1' | 31 | __version__ = '4.2' |
| 32 | __license__ = 'GPL3' | 32 | __license__ = 'GPL3' |
| 33 | 33 | ||
| 34 | 34 | ||
| @@ -61,38 +61,46 @@ class WeechatTarget: | |||
| 61 | if self._url == '': | 61 | if self._url == '': |
| 62 | raise Exception('"target_url" not defined for target') | 62 | raise Exception('"target_url" not defined for target') |
| 63 | self._password = target_dict.get('target_password', '') | 63 | self._password = target_dict.get('target_password', '') |
| 64 | self._pm_title_template = target_dict.get('pm_title_template', | 64 | self._pm_title_template = target_dict.get( |
| 65 | '%s @ %S') | 65 | 'pm_title_template', '%s @ %S' |
| 66 | self._pm_message_template = target_dict.get('pm_message_template', | 66 | ) |
| 67 | '%m') | 67 | self._pm_message_template = target_dict.get( |
| 68 | self._cm_title_template = target_dict.get('cm_title_template', | 68 | 'pm_message_template', '%m' |
| 69 | '%c @ %S') | 69 | ) |
| 70 | self._cm_message_template = target_dict.get('cm_message_template', | 70 | self._cm_title_template = target_dict.get( |
| 71 | '%s -> %m') | 71 | 'cm_title_template', '%c @ %S' |
| 72 | self._nm_title_template = target_dict.get('nm_title_template', | 72 | ) |
| 73 | '%c @ %S') | 73 | self._cm_message_template = target_dict.get( |
| 74 | self._nm_message_template = target_dict.get('nm_message_template', | 74 | 'cm_message_template', '%s -> %m' |
| 75 | '%s -> %m') | 75 | ) |
| 76 | self._nm_title_template = target_dict.get( | ||
| 77 | 'nm_title_template', '%c @ %S' | ||
| 78 | ) | ||
| 79 | self._nm_message_template = target_dict.get( | ||
| 80 | 'nm_message_template', '%s -> %m' | ||
| 81 | ) | ||
| 76 | self._chans = set(target_dict.get('channel_list', [])) | 82 | self._chans = set(target_dict.get('channel_list', [])) |
| 77 | self._nicks = set(target_dict.get('nick_list', [])) | 83 | self._nicks = set(target_dict.get('nick_list', [])) |
| 78 | self._chan_messages_policy = int(target_dict.get( | 84 | self._chan_messages_policy = int( |
| 79 | 'channel_messages_policy', | 85 | target_dict.get('channel_messages_policy', BEINC_POLICY_LIST_ONLY) |
| 80 | BEINC_POLICY_LIST_ONLY)) | 86 | ) |
| 81 | self._priv_messages_policy = int(target_dict.get( | 87 | self._priv_messages_policy = int( |
| 82 | 'private_messages_policy', | 88 | target_dict.get('private_messages_policy', BEINC_POLICY_ALL) |
| 83 | BEINC_POLICY_ALL)) | 89 | ) |
| 84 | self._notifications_policy = int(target_dict.get( | 90 | self._notifications_policy = int( |
| 85 | 'notifications_policy', | 91 | target_dict.get('notifications_policy', BEINC_POLICY_ALL) |
| 86 | BEINC_POLICY_ALL)) | 92 | ) |
| 87 | self._cert_file = target_dict.get('target_cert_file') | 93 | self._cert_file = target_dict.get('target_cert_file') |
| 88 | self._timestamp_format = target_dict.get('target_timestamp_format', | 94 | self._timestamp_format = target_dict.get( |
| 89 | '%H:%M:%S') | 95 | 'target_timestamp_format', '%H:%M:%S' |
| 96 | ) | ||
| 90 | self._debug = bool(target_dict.get('debug', False)) | 97 | self._debug = bool(target_dict.get('debug', False)) |
| 91 | self._enabled = bool(target_dict.get('enabled', True)) | 98 | self._enabled = bool(target_dict.get('enabled', True)) |
| 92 | self._socket_timeout = int(target_dict.get('socket_timeout', 3)) | 99 | self._socket_timeout = int(target_dict.get('socket_timeout', 3)) |
| 93 | self._ssl_ciphers = target_dict.get('ssl_ciphers', '') | 100 | self._ssl_ciphers = target_dict.get('ssl_ciphers', '') |
| 94 | self._disable_hostname_check = bool( | 101 | self._disable_hostname_check = bool( |
| 95 | target_dict.get('disable-hostname-check', False)) | 102 | target_dict.get('disable-hostname-check', False) |
| 103 | ) | ||
| 96 | self._ssl_version = target_dict.get('ssl_version', 'auto') | 104 | self._ssl_version = target_dict.get('ssl_version', 'auto') |
| 97 | self._last_message = None # datetime.datetime instance | 105 | self._last_message = None # datetime.datetime instance |
| 98 | self._context = None | 106 | self._context = None |
| @@ -143,26 +151,22 @@ class WeechatTarget: | |||
| 143 | last_message = 'never' | 151 | last_message = 'never' |
| 144 | if self._last_message is not None: | 152 | if self._last_message is not None: |
| 145 | last_message = self._last_message.strftime('%Y-%m-%d %H:%M:%S') | 153 | last_message = self._last_message.strftime('%Y-%m-%d %H:%M:%S') |
| 146 | return ('name: {0}\nurl: {1}\nenabled: {2}\nchannel_list: {3}\n' | 154 | return ( |
| 147 | 'nick_list: {4}\nchannel_messages_policy: {5}\n' | 155 | f'name: {self._name}\nurl: {self._url}\n' |
| 148 | 'private_messages_policy: {6}\nnotifications_policy: {7}\n' | 156 | f"enabled: {'yes' if self._enabled else 'no'}\n" |
| 149 | 'last message: {8}\nsocket timeout: {9}\nssl-version: {10}\n' | 157 | f"channel_list: {', '.join(self._chans)}\n" |
| 150 | 'ciphers: {11}\ndisable hostname check: {12}\n' | 158 | f"nick_list: {', '.join(self._nicks)}\n" |
| 151 | 'debug: {13}\n\n'.format( | 159 | f'channel_messages_policy: {self._chan_messages_policy}\n' |
| 152 | self._name, | 160 | f'private_messages_policy: {self._priv_messages_policy}\n' |
| 153 | self._url, | 161 | f'notifications_policy: {self._notifications_policy}\n' |
| 154 | 'yes' if self._enabled else 'no', | 162 | f'last message: {last_message}\n' |
| 155 | ', '.join(self._chans), | 163 | f'socket timeout: {self._socket_timeout}\n' |
| 156 | ', '.join(self._nicks), | 164 | f'ssl-version: {self._ssl_version}\n' |
| 157 | self._chan_messages_policy, | 165 | f"ciphers: {self._ssl_ciphers or 'auto'}\n" |
| 158 | self._priv_messages_policy, | 166 | "disable hostname check: " |
| 159 | self._notifications_policy, | 167 | f"{'yes' if self._disable_hostname_check else 'no'}\n" |
| 160 | last_message, | 168 | f"debug: {'yes' if self._debug else 'no'}\n\n" |
| 161 | self._socket_timeout, | 169 | ) |
| 162 | self._ssl_version, | ||
| 163 | self._ssl_ciphers or 'auto', | ||
| 164 | 'yes' if self._disable_hostname_check else 'no', | ||
| 165 | 'yes' if self._debug else 'no')) | ||
| 166 | 170 | ||
| 167 | def send_private_message_notification(self, values): | 171 | def send_private_message_notification(self, values): |
| 168 | """ | 172 | """ |
| @@ -172,20 +176,22 @@ class WeechatTarget: | |||
| 172 | :type value: dict | 176 | :type value: dict |
| 173 | """ | 177 | """ |
| 174 | try: | 178 | try: |
| 175 | title = self._fetch_formatted_str(self._pm_title_template, | 179 | title = self._fetch_formatted_str(self._pm_title_template, values) |
| 176 | values) | ||
| 177 | message = self._fetch_formatted_str( | 180 | message = self._fetch_formatted_str( |
| 178 | self._pm_message_template, | 181 | self._pm_message_template, |
| 179 | values) | 182 | values, |
| 183 | ) | ||
| 180 | if not self._send_beinc_message(title, message) and self._debug: | 184 | if not self._send_beinc_message(title, message) and self._debug: |
| 181 | beinc_prnt( | 185 | beinc_prnt( |
| 182 | f'BEINC DEBUG: send_private_message_notification-ERROR ' | 186 | f'BEINC DEBUG: send_private_message_notification-ERROR ' |
| 183 | f'for "{self._name}": _send_beinc_message -> False') | 187 | f'for "{self._name}": _send_beinc_message -> False' |
| 188 | ) | ||
| 184 | except Exception as e: | 189 | except Exception as e: |
| 185 | if self._debug: | 190 | if self._debug: |
| 186 | beinc_prnt( | 191 | beinc_prnt( |
| 187 | f'BEINC DEBUG: send_private_message_notification-ERROR ' | 192 | f'BEINC DEBUG: send_private_message_notification-ERROR ' |
| 188 | f'for "{self._name}": {e}') | 193 | f'for "{self._name}": {e}' |
| 194 | ) | ||
| 189 | 195 | ||
| 190 | def send_channel_message_notification(self, values): | 196 | def send_channel_message_notification(self, values): |
| 191 | """ | 197 | """ |
| @@ -195,19 +201,21 @@ class WeechatTarget: | |||
| 195 | :type value: dict | 201 | :type value: dict |
| 196 | """ | 202 | """ |
| 197 | try: | 203 | try: |
| 198 | title = self._fetch_formatted_str(self._cm_title_template, | 204 | title = self._fetch_formatted_str(self._cm_title_template, values) |
| 199 | values) | 205 | message = self._fetch_formatted_str( |
| 200 | message = self._fetch_formatted_str(self._cm_message_template, | 206 | self._cm_message_template, values |
| 201 | values) | 207 | ) |
| 202 | if not self._send_beinc_message(title, message) and self._debug: | 208 | if not self._send_beinc_message(title, message) and self._debug: |
| 203 | beinc_prnt( | 209 | beinc_prnt( |
| 204 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' | 210 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' |
| 205 | f'for "{self._name}": _send_beinc_message -> False') | 211 | f'for "{self._name}": _send_beinc_message -> False' |
| 212 | ) | ||
| 206 | except Exception as e: | 213 | except Exception as e: |
| 207 | if self._debug: | 214 | if self._debug: |
| 208 | beinc_prnt( | 215 | beinc_prnt( |
| 209 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' | 216 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' |
| 210 | f'for "{self._name}": {e}') | 217 | f'for "{self._name}": {e}' |
| 218 | ) | ||
| 211 | 219 | ||
| 212 | def send_notify_message_notification(self, values): | 220 | def send_notify_message_notification(self, values): |
| 213 | """ | 221 | """ |
| @@ -217,19 +225,21 @@ class WeechatTarget: | |||
| 217 | :type value: dict | 225 | :type value: dict |
| 218 | """ | 226 | """ |
| 219 | try: | 227 | try: |
| 220 | title = self._fetch_formatted_str(self._nm_title_template, | 228 | title = self._fetch_formatted_str(self._nm_title_template, values) |
| 221 | values) | 229 | message = self._fetch_formatted_str( |
| 222 | message = self._fetch_formatted_str(self._nm_message_template, | 230 | self._nm_message_template, values |
| 223 | values) | 231 | ) |
| 224 | if not self._send_beinc_message(title, message) and self._debug: | 232 | if not self._send_beinc_message(title, message) and self._debug: |
| 225 | beinc_prnt( | 233 | beinc_prnt( |
| 226 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' | 234 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' |
| 227 | f'for "{self._name}": _send_beinc_message -> False') | 235 | f'for "{self._name}": _send_beinc_message -> False' |
| 236 | ) | ||
| 228 | except Exception as e: | 237 | except Exception as e: |
| 229 | if self._debug: | 238 | if self._debug: |
| 230 | beinc_prnt( | 239 | beinc_prnt( |
| 231 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' | 240 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' |
| 232 | f'for "{self._name}": {e}') | 241 | f'for "{self._name}": {e}' |
| 242 | ) | ||
| 233 | 243 | ||
| 234 | def send_broadcast_notification(self, message): | 244 | def send_broadcast_notification(self, message): |
| 235 | """ | 245 | """ |
| @@ -244,26 +254,30 @@ class WeechatTarget: | |||
| 244 | if not self._send_beinc_message(title, message) and self._debug: | 254 | if not self._send_beinc_message(title, message) and self._debug: |
| 245 | beinc_prnt( | 255 | beinc_prnt( |
| 246 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' | 256 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 247 | f'for "{self._name}": _send_beinc_message -> False') | 257 | f'for "{self._name}": _send_beinc_message -> False' |
| 258 | ) | ||
| 248 | except Exception as e: | 259 | except Exception as e: |
| 249 | if self._debug: | 260 | if self._debug: |
| 250 | beinc_prnt( | 261 | beinc_prnt( |
| 251 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' | 262 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 252 | f'for "{self._name}": {e}') | 263 | f'for "{self._name}": {e}' |
| 264 | ) | ||
| 253 | 265 | ||
| 254 | def _context_setup(self): | 266 | def _context_setup(self): |
| 255 | """Sets up the SSL context""" | 267 | """Sets up the SSL context""" |
| 256 | if self._context is not None: | 268 | if self._context is not None: |
| 257 | return True | 269 | return True |
| 258 | try: | 270 | try: |
| 259 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) | 271 | context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 260 | context.verify_mode = ssl.CERT_NONE | ||
| 261 | if self._cert_file: | 272 | if self._cert_file: |
| 262 | context.verify_mode = ssl.CERT_REQUIRED | 273 | context.verify_mode = ssl.CERT_REQUIRED |
| 263 | context.load_verify_locations(cafile=os.path.expanduser( | 274 | context.load_verify_locations( |
| 264 | self._cert_file)) | 275 | cafile=os.path.expanduser(self._cert_file) |
| 265 | context.check_hostname = bool( | 276 | ) |
| 266 | not self._disable_hostname_check) | 277 | context.check_hostname = bool(not self._disable_hostname_check) |
| 278 | else: | ||
| 279 | context.check_hostname = False | ||
| 280 | context.verify_mode = ssl.CERT_NONE | ||
| 267 | if self._ssl_ciphers and self._ssl_ciphers != 'auto': | 281 | if self._ssl_ciphers and self._ssl_ciphers != 'auto': |
| 268 | context.set_ciphers(self._ssl_ciphers) | 282 | context.set_ciphers(self._ssl_ciphers) |
| 269 | self._context = context | 283 | self._context = context |
| @@ -292,13 +306,15 @@ class WeechatTarget: | |||
| 292 | :rtype: str | 306 | :rtype: str |
| 293 | """ | 307 | """ |
| 294 | timestamp = datetime.datetime.now().strftime(self._timestamp_format) | 308 | timestamp = datetime.datetime.now().strftime(self._timestamp_format) |
| 295 | replacements = {'%S': values['server'], | 309 | replacements = { |
| 296 | '%s': values['source_nick'], | 310 | '%S': values['server'], |
| 297 | '%c': values['channel'], | 311 | '%s': values['source_nick'], |
| 298 | '%m': values['message'], | 312 | '%c': values['channel'], |
| 299 | '%t': timestamp, | 313 | '%m': values['message'], |
| 300 | '%p': 'BEINC', | 314 | '%t': timestamp, |
| 301 | '%n': values['own_nick']} | 315 | '%p': 'BEINC', |
| 316 | '%n': values['own_nick'], | ||
| 317 | } | ||
| 302 | for key, value in replacements.items(): | 318 | for key, value in replacements.items(): |
| 303 | template = template.replace(key, value) | 319 | template = template.replace(key, value) |
| 304 | return template | 320 | return template |
| @@ -327,16 +343,20 @@ class WeechatTarget: | |||
| 327 | ('resource_name', self._name), | 343 | ('resource_name', self._name), |
| 328 | ('password', self._password), | 344 | ('password', self._password), |
| 329 | ('title', title), | 345 | ('title', title), |
| 330 | ('message', message) | 346 | ('message', message), |
| 331 | )).encode('utf-8'), | 347 | ) |
| 348 | ).encode('utf-8'), | ||
| 332 | timeout=self._socket_timeout, | 349 | timeout=self._socket_timeout, |
| 333 | context=self._context) | 350 | context=self._context, |
| 351 | ) | ||
| 334 | response_dict = json.loads(response.read().decode('utf-8')) | 352 | response_dict = json.loads(response.read().decode('utf-8')) |
| 335 | if response.code != 200: | 353 | if response.code != 200: |
| 336 | raise socket.error(response_dict.get('message', '')) | 354 | raise socket.error(response_dict.get('message', '')) |
| 337 | if self._debug: | 355 | if self._debug: |
| 338 | beinc_prnt('BEINC DEBUG: Server responded: {0}'.format( | 356 | beinc_prnt( |
| 339 | response_dict.get('message'))) | 357 | "BEINC DEBUG: Server responded: " |
| 358 | f"{response_dict.get('message')}" | ||
| 359 | ) | ||
| 340 | self._last_message = datetime.datetime.now() | 360 | self._last_message = datetime.datetime.now() |
| 341 | return True | 361 | return True |
| 342 | except ssl.SSLError as e: | 362 | except ssl.SSLError as e: |
| @@ -378,7 +398,7 @@ def beinc_cmd_target_handler(cmd_tokens): | |||
| 378 | if cmd_tokens[0] == 'list': | 398 | if cmd_tokens[0] == 'list': |
| 379 | beinc_prnt('--- Globals ---') | 399 | beinc_prnt('--- Globals ---') |
| 380 | for key, value in global_values.items(): | 400 | for key, value in global_values.items(): |
| 381 | beinc_prnt('{key} -> {value}'.format(key=key, value=str(value))) | 401 | beinc_prnt(f'{key} -> {str(value)}') |
| 382 | beinc_prnt('--- Targets ---') | 402 | beinc_prnt('--- Targets ---') |
| 383 | for target in target_list: | 403 | for target in target_list: |
| 384 | beinc_prnt(str(target)) | 404 | beinc_prnt(str(target)) |
| @@ -430,8 +450,10 @@ def beinc_command(data, buffer_obj, args): | |||
| 430 | elif cmd_tokens[0] == 'target': | 450 | elif cmd_tokens[0] == 'target': |
| 431 | return beinc_cmd_target_handler(cmd_tokens[1:]) | 451 | return beinc_cmd_target_handler(cmd_tokens[1:]) |
| 432 | else: | 452 | else: |
| 433 | beinc_prnt('syntax: /beinc < on | off | reload |' | 453 | beinc_prnt( |
| 434 | ' broadcast <text> | target <action> >') | 454 | 'syntax: /beinc < on | off | reload |' |
| 455 | ' broadcast <text> | target <action> >' | ||
| 456 | ) | ||
| 435 | return weechat.WEECHAT_RC_OK | 457 | return weechat.WEECHAT_RC_OK |
| 436 | 458 | ||
| 437 | 459 | ||
| @@ -439,8 +461,9 @@ def beinc_privmsg_handler(data, signal, signal_data): | |||
| 439 | """Callback function the *PRIVMSG* IRC messages hooked by Weechat""" | 461 | """Callback function the *PRIVMSG* IRC messages hooked by Weechat""" |
| 440 | if not enabled: | 462 | if not enabled: |
| 441 | return weechat.WEECHAT_RC_OK | 463 | return weechat.WEECHAT_RC_OK |
| 442 | prvmsg_dict = weechat.info_get_hashtable('irc_message_parse', | 464 | prvmsg_dict = weechat.info_get_hashtable( |
| 443 | {'message': signal_data}) | 465 | 'irc_message_parse', {'message': signal_data} |
| 466 | ) | ||
| 444 | # packing the privmsg handler values | 467 | # packing the privmsg handler values |
| 445 | ph_values = {} | 468 | ph_values = {} |
| 446 | ph_values['server'] = signal.split(',')[0] | 469 | ph_values['server'] = signal.split(',')[0] |
| @@ -448,7 +471,8 @@ def beinc_privmsg_handler(data, signal, signal_data): | |||
| 448 | ph_values['channel'] = prvmsg_dict['arguments'].split(':')[0].strip() | 471 | ph_values['channel'] = prvmsg_dict['arguments'].split(':')[0].strip() |
| 449 | ph_values['source_nick'] = prvmsg_dict['nick'] | 472 | ph_values['source_nick'] = prvmsg_dict['nick'] |
| 450 | ph_values['message'] = ':'.join( | 473 | ph_values['message'] = ':'.join( |
| 451 | prvmsg_dict['arguments'].split(':')[1:]).strip() | 474 | prvmsg_dict['arguments'].split(':')[1:] |
| 475 | ).strip() | ||
| 452 | if ph_values['channel'] == ph_values['own_nick']: | 476 | if ph_values['channel'] == ph_values['own_nick']: |
| 453 | # priv messages are handled here | 477 | # priv messages are handled here |
| 454 | if not global_values['global_private_messages_policy']: | 478 | if not global_values['global_private_messages_policy']: |
| @@ -458,10 +482,10 @@ def beinc_privmsg_handler(data, signal, signal_data): | |||
| 458 | continue | 482 | continue |
| 459 | p_messages_policy = target.private_messages_policy | 483 | p_messages_policy = target.private_messages_policy |
| 460 | if p_messages_policy == BEINC_POLICY_ALL or ( | 484 | if p_messages_policy == BEINC_POLICY_ALL or ( |
| 461 | p_messages_policy == BEINC_POLICY_LIST_ONLY and | 485 | p_messages_policy == BEINC_POLICY_LIST_ONLY |
| 462 | '{0}.{1}'.format( | 486 | and f"{ph_values['server']}.{ph_values['source_nick'].lower()}" |
| 463 | ph_values['server'], | 487 | in target.nicks |
| 464 | ph_values['source_nick'].lower()) in target.nicks): | 488 | ): |
| 465 | target.send_private_message_notification(ph_values) | 489 | target.send_private_message_notification(ph_values) |
| 466 | elif ph_values['own_nick'].lower() in ph_values['message'].lower(): | 490 | elif ph_values['own_nick'].lower() in ph_values['message'].lower(): |
| 467 | # notify messages are handled here | 491 | # notify messages are handled here |
| @@ -471,10 +495,9 @@ def beinc_privmsg_handler(data, signal, signal_data): | |||
| 471 | if not target.enabled: | 495 | if not target.enabled: |
| 472 | continue | 496 | continue |
| 473 | if target.notifications_policy == BEINC_POLICY_ALL or ( | 497 | if target.notifications_policy == BEINC_POLICY_ALL or ( |
| 474 | target.notifications_policy == BEINC_POLICY_LIST_ONLY and | 498 | target.notifications_policy == BEINC_POLICY_LIST_ONLY |
| 475 | '{0}.{1}'.format( | 499 | and f"{ph_values['server']}.{ph_values['channel'].lower()}" |
| 476 | ph_values['server'], | 500 | in target.chans |
| 477 | ph_values['channel'].lower()) in target.chans | ||
| 478 | ): | 501 | ): |
| 479 | target.send_notify_message_notification(ph_values) | 502 | target.send_notify_message_notification(ph_values) |
| 480 | elif global_values['global_channel_messages_policy']: | 503 | elif global_values['global_channel_messages_policy']: |
| @@ -486,10 +509,10 @@ def beinc_privmsg_handler(data, signal, signal_data): | |||
| 486 | continue | 509 | continue |
| 487 | c_messages_policy = target.channel_messages_policy | 510 | c_messages_policy = target.channel_messages_policy |
| 488 | if c_messages_policy == BEINC_POLICY_ALL or ( | 511 | if c_messages_policy == BEINC_POLICY_ALL or ( |
| 489 | c_messages_policy == BEINC_POLICY_LIST_ONLY and | 512 | c_messages_policy == BEINC_POLICY_LIST_ONLY |
| 490 | '{0}.{1}'.format( | 513 | and f"{ph_values['server']}.{ph_values['channel'].lower()}" |
| 491 | ph_values['server'], | 514 | in target.chans |
| 492 | ph_values['channel'].lower()) in target.chans): | 515 | ): |
| 493 | target.send_channel_message_notification(ph_values) | 516 | target.send_channel_message_notification(ph_values) |
| 494 | return weechat.WEECHAT_RC_OK | 517 | return weechat.WEECHAT_RC_OK |
| 495 | 518 | ||
| @@ -517,24 +540,28 @@ def beinc_init(): | |||
| 517 | try: | 540 | try: |
| 518 | beinc_config_file_str = os.path.join( | 541 | beinc_config_file_str = os.path.join( |
| 519 | weechat.info_get('weechat_dir', ''), | 542 | weechat.info_get('weechat_dir', ''), |
| 520 | 'beinc_weechat.json') | 543 | 'beinc_weechat.json', |
| 544 | ) | ||
| 521 | beinc_prnt(f'Parsing {beinc_config_file_str}...') | 545 | beinc_prnt(f'Parsing {beinc_config_file_str}...') |
| 522 | custom_error = 'load error' | 546 | custom_error = 'load error' |
| 523 | with open(beinc_config_file_str, 'r') as fp: | 547 | with io.open(beinc_config_file_str, 'r', encoding='utf-8') as fp: |
| 524 | config_dict = json.load(fp) | 548 | config_dict = json.load(fp) |
| 525 | custom_error = 'target parse error' | 549 | custom_error = 'target parse error' |
| 526 | global_values['use_current_buffer'] = bool( | 550 | global_values['use_current_buffer'] = bool( |
| 527 | config_dict['irc_client'].get( | 551 | config_dict['irc_client'].get('use_current_buffer', False) |
| 528 | 'use_current_buffer', False)) | 552 | ) |
| 529 | if config_dict.get('config_version', | 553 | if ( |
| 530 | 0) != BEINC_CURRENT_CONFIG_VERSION: | 554 | config_dict.get('config_version', 0) |
| 531 | beinc_prnt('WARNING: The version of the config-file: {0} ({1}) ' | 555 | != BEINC_CURRENT_CONFIG_VERSION |
| 532 | 'does not correspond to the latest version supported ' | 556 | ): |
| 533 | 'by this program ({2})\nCheck beinc_config_sample.json ' | 557 | beinc_prnt( |
| 534 | 'for the newest features!'.format( | 558 | "WARNING: The version of the config-file: " |
| 535 | beinc_config_file_str, | 559 | f"{beinc_config_file_str} " |
| 536 | config_dict.get('config_version', 0), | 560 | f"({config_dict.get('config_version', 0)}) " |
| 537 | BEINC_CURRENT_CONFIG_VERSION)) | 561 | "does not correspond to the latest version supported " |
| 562 | f"by this program ({BEINC_CURRENT_CONFIG_VERSION})\n" | ||
| 563 | "Check beinc_config_sample.json for the newest features!" | ||
| 564 | ) | ||
| 538 | for target in config_dict['irc_client']['targets']: | 565 | for target in config_dict['irc_client']['targets']: |
| 539 | try: | 566 | try: |
| 540 | new_target = WeechatTarget(target) | 567 | new_target = WeechatTarget(target) |
| @@ -551,8 +578,10 @@ def beinc_init(): | |||
| 551 | beinc_prnt(f'BEINC target "{new_target.name}" added') | 578 | beinc_prnt(f'BEINC target "{new_target.name}" added') |
| 552 | beinc_prnt('Done!') | 579 | beinc_prnt('Done!') |
| 553 | except Exception as e: | 580 | except Exception as e: |
| 554 | beinc_prnt(f'ERROR: unable to parse {beinc_config_file_str}: ' | 581 | beinc_prnt( |
| 555 | f'{custom_error} - {e}\nBEINC is now disabled') | 582 | f'ERROR: unable to parse {beinc_config_file_str}: ' |
| 583 | f'{custom_error} - {e}\nBEINC is now disabled' | ||
| 584 | ) | ||
| 556 | enabled = False | 585 | enabled = False |
| 557 | # do not return error / exit the script | 586 | # do not return error / exit the script |
| 558 | # in order to give a smoother opportunity to fix a 'broken' config | 587 | # in order to give a smoother opportunity to fix a 'broken' config |
| @@ -567,19 +596,24 @@ weechat.register( | |||
| 567 | __license__, | 596 | __license__, |
| 568 | 'Blackmore\'s Extended IRC Notification Collection (Weechat Client)', | 597 | 'Blackmore\'s Extended IRC Notification Collection (Weechat Client)', |
| 569 | '', | 598 | '', |
| 570 | '') | 599 | '', |
| 600 | ) | ||
| 571 | version = weechat.info_get('version_number', '') or 0 | 601 | version = weechat.info_get('version_number', '') or 0 |
| 572 | if int(version) < 0x00040000: | 602 | if int(version) < 0x00040000: |
| 573 | weechat.prnt('', 'WeeChat version >= 0.4.0 is required to run beinc') | 603 | weechat.prnt('', 'WeeChat version >= 0.4.0 is required to run beinc') |
| 574 | else: | 604 | else: |
| 575 | weechat.hook_command('beinc', | 605 | weechat.hook_command( |
| 576 | 'BEINC command', ('< broadcast <message> | on | off |' | 606 | 'beinc', |
| 577 | ' reload | target <action> >'), | 607 | 'BEINC command', |
| 578 | ('Available target actions:\n' | 608 | '< broadcast <message> | on | off | reload | target <action> >', |
| 579 | 'disable <target name>\nenable <target name>\nlist'), | 609 | ( |
| 580 | 'None', | 610 | 'Available target actions:\n' |
| 581 | 'beinc_command', | 611 | 'disable <target name>\nenable <target name>\nlist' |
| 582 | '') | 612 | ), |
| 613 | 'None', | ||
| 614 | 'beinc_command', | ||
| 615 | '', | ||
| 616 | ) | ||
| 583 | weechat.hook_signal('*,irc_in2_privmsg', 'beinc_privmsg_handler', '') | 617 | weechat.hook_signal('*,irc_in2_privmsg', 'beinc_privmsg_handler', '') |
| 584 | beinc_init() | 618 | beinc_init() |
| 585 | weechat.prnt('', 'beinc initiated!') | 619 | weechat.prnt('', 'beinc initiated!') |
