diff options
Diffstat (limited to 'beinc_weechat.py')
| -rw-r--r-- | beinc_weechat.py | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/beinc_weechat.py b/beinc_weechat.py index b3550d8..cdbfe1f 100644 --- a/beinc_weechat.py +++ b/beinc_weechat.py | |||
| @@ -1,7 +1,5 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) |
| 2 | 2 | # Copyright (C) 2013-2024 Simeon Simeonov | |
| 3 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.3 | ||
| 4 | # Copyright (C) 2013-2022 Simeon Simeonov | ||
| 5 | 3 | ||
| 6 | # This program is free software: you can redistribute it and/or modify | 4 | # 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 | 5 | # it under the terms of the GNU General Public License as published by |
| @@ -16,11 +14,11 @@ | |||
| 16 | # You should have received a copy of the GNU General Public License | 14 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | """BEINC client for Weechat""" | 16 | """BEINC client for Weechat""" |
| 17 | |||
| 19 | import datetime | 18 | import datetime |
| 20 | import io | 19 | import io |
| 21 | import json | 20 | import json |
| 22 | import os | 21 | import os |
| 23 | import socket | ||
| 24 | import ssl | 22 | import ssl |
| 25 | import urllib.parse | 23 | import urllib.parse |
| 26 | import urllib.request | 24 | import urllib.request |
| @@ -28,7 +26,7 @@ import urllib.request | |||
| 28 | import weechat | 26 | import weechat |
| 29 | 27 | ||
| 30 | __author__ = 'Simeon Simeonov' | 28 | __author__ = 'Simeon Simeonov' |
| 31 | __version__ = '4.3' | 29 | __version__ = '4.4' |
| 32 | __license__ = 'GPL3' | 30 | __license__ = 'GPL3' |
| 33 | 31 | ||
| 34 | 32 | ||
| @@ -178,19 +176,18 @@ class WeechatTarget: | |||
| 178 | try: | 176 | try: |
| 179 | title = self._fetch_formatted_str(self._pm_title_template, values) | 177 | title = self._fetch_formatted_str(self._pm_title_template, values) |
| 180 | message = self._fetch_formatted_str( | 178 | message = self._fetch_formatted_str( |
| 181 | self._pm_message_template, | 179 | self._pm_message_template, values |
| 182 | values, | ||
| 183 | ) | 180 | ) |
| 184 | if not self._send_beinc_message(title, message) and self._debug: | 181 | if not self._send_beinc_message(title, message) and self._debug: |
| 185 | beinc_prnt( | 182 | beinc_prnt( |
| 186 | f'BEINC DEBUG: send_private_message_notification-ERROR ' | 183 | f'BEINC DEBUG: send_private_message_notification-ERROR ' |
| 187 | f'for "{self._name}": _send_beinc_message -> False' | 184 | f'for "{self._name}": _send_beinc_message -> False' |
| 188 | ) | 185 | ) |
| 189 | except Exception as e: | 186 | except Exception as exp: |
| 190 | if self._debug: | 187 | if self._debug: |
| 191 | beinc_prnt( | 188 | beinc_prnt( |
| 192 | f'BEINC DEBUG: send_private_message_notification-ERROR ' | 189 | f'BEINC DEBUG: send_private_message_notification-ERROR ' |
| 193 | f'for "{self._name}": {e}' | 190 | f'for "{self._name}": {exp}' |
| 194 | ) | 191 | ) |
| 195 | 192 | ||
| 196 | def send_channel_message_notification(self, values): | 193 | def send_channel_message_notification(self, values): |
| @@ -210,11 +207,11 @@ class WeechatTarget: | |||
| 210 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' | 207 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' |
| 211 | f'for "{self._name}": _send_beinc_message -> False' | 208 | f'for "{self._name}": _send_beinc_message -> False' |
| 212 | ) | 209 | ) |
| 213 | except Exception as e: | 210 | except Exception as exp: |
| 214 | if self._debug: | 211 | if self._debug: |
| 215 | beinc_prnt( | 212 | beinc_prnt( |
| 216 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' | 213 | f'BEINC DEBUG: send_channel_message_notification-ERROR ' |
| 217 | f'for "{self._name}": {e}' | 214 | f'for "{self._name}": {exp}' |
| 218 | ) | 215 | ) |
| 219 | 216 | ||
| 220 | def send_notify_message_notification(self, values): | 217 | def send_notify_message_notification(self, values): |
| @@ -234,11 +231,11 @@ class WeechatTarget: | |||
| 234 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' | 231 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' |
| 235 | f'for "{self._name}": _send_beinc_message -> False' | 232 | f'for "{self._name}": _send_beinc_message -> False' |
| 236 | ) | 233 | ) |
| 237 | except Exception as e: | 234 | except Exception as exp: |
| 238 | if self._debug: | 235 | if self._debug: |
| 239 | beinc_prnt( | 236 | beinc_prnt( |
| 240 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' | 237 | f'BEINC DEBUG: send_notify_message_notification-ERROR ' |
| 241 | f'for "{self._name}": {e}' | 238 | f'for "{self._name}": {exp}' |
| 242 | ) | 239 | ) |
| 243 | 240 | ||
| 244 | def send_broadcast_notification(self, message): | 241 | def send_broadcast_notification(self, message): |
| @@ -256,11 +253,11 @@ class WeechatTarget: | |||
| 256 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' | 253 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 257 | f'for "{self._name}": _send_beinc_message -> False' | 254 | f'for "{self._name}": _send_beinc_message -> False' |
| 258 | ) | 255 | ) |
| 259 | except Exception as e: | 256 | except Exception as exp: |
| 260 | if self._debug: | 257 | if self._debug: |
| 261 | beinc_prnt( | 258 | beinc_prnt( |
| 262 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' | 259 | f'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 263 | f'for "{self._name}": {e}' | 260 | f'for "{self._name}": {exp}' |
| 264 | ) | 261 | ) |
| 265 | 262 | ||
| 266 | def _context_setup(self): | 263 | def _context_setup(self): |
| @@ -282,12 +279,12 @@ class WeechatTarget: | |||
| 282 | context.set_ciphers(self._ssl_ciphers) | 279 | context.set_ciphers(self._ssl_ciphers) |
| 283 | self._context = context | 280 | self._context = context |
| 284 | return True | 281 | return True |
| 285 | except ssl.SSLError as e: | 282 | except ssl.SSLError as err: |
| 286 | if self._debug: | 283 | if self._debug: |
| 287 | beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {e}\n') | 284 | beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {err}\n') |
| 288 | except Exception as e: | 285 | except Exception as exp: |
| 289 | if self._debug: | 286 | if self._debug: |
| 290 | beinc_prnt(f'BEINC DEBUG: Generic context error: {e}\n') | 287 | beinc_prnt(f'BEINC DEBUG: Generic context error: {exp}\n') |
| 291 | self._context = None | 288 | self._context = None |
| 292 | return False | 289 | return False |
| 293 | 290 | ||
| @@ -351,7 +348,7 @@ class WeechatTarget: | |||
| 351 | ) | 348 | ) |
| 352 | response_dict = json.loads(response.read().decode('utf-8')) | 349 | response_dict = json.loads(response.read().decode('utf-8')) |
| 353 | if response.code != 200: | 350 | if response.code != 200: |
| 354 | raise socket.error(response_dict.get('message', '')) | 351 | raise OSError(response_dict.get('message', '')) |
| 355 | if self._debug: | 352 | if self._debug: |
| 356 | beinc_prnt( | 353 | beinc_prnt( |
| 357 | "BEINC DEBUG: Server responded: " | 354 | "BEINC DEBUG: Server responded: " |
| @@ -359,15 +356,15 @@ class WeechatTarget: | |||
| 359 | ) | 356 | ) |
| 360 | self._last_message = datetime.datetime.now() | 357 | self._last_message = datetime.datetime.now() |
| 361 | return True | 358 | return True |
| 362 | except ssl.SSLError as e: | 359 | except ssl.SSLError as err: |
| 363 | if self._debug: | 360 | if self._debug: |
| 364 | beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {e}\n') | 361 | beinc_prnt(f'BEINC DEBUG: SSL/TLS error: {err}\n') |
| 365 | except socket.error as e: | 362 | except OSError as err: |
| 366 | if self._debug: | 363 | if self._debug: |
| 367 | beinc_prnt(f'BEINC DEBUG: Connection error: {e}\n') | 364 | beinc_prnt(f'BEINC DEBUG: Connection error: {err}\n') |
| 368 | except Exception as e: | 365 | except Exception as exp: |
| 369 | if self._debug: | 366 | if self._debug: |
| 370 | beinc_prnt(f'BEINC DEBUG: Unable to send message: {e}\n') | 367 | beinc_prnt(f'BEINC DEBUG: Unable to send message: {exp}\n') |
| 371 | return False | 368 | return False |
| 372 | 369 | ||
| 373 | 370 | ||
| @@ -398,7 +395,7 @@ def beinc_cmd_target_handler(cmd_tokens): | |||
| 398 | if cmd_tokens[0] == 'list': | 395 | if cmd_tokens[0] == 'list': |
| 399 | beinc_prnt('--- Globals ---') | 396 | beinc_prnt('--- Globals ---') |
| 400 | for key, value in global_values.items(): | 397 | for key, value in global_values.items(): |
| 401 | beinc_prnt(f'{key} -> {str(value)}') | 398 | beinc_prnt(f'{key} -> {value}') |
| 402 | beinc_prnt('--- Targets ---') | 399 | beinc_prnt('--- Targets ---') |
| 403 | for target in target_list: | 400 | for target in target_list: |
| 404 | beinc_prnt(str(target)) | 401 | beinc_prnt(str(target)) |
| @@ -539,8 +536,7 @@ def beinc_init(): | |||
| 539 | 536 | ||
| 540 | try: | 537 | try: |
| 541 | beinc_config_file_str = os.path.join( | 538 | beinc_config_file_str = os.path.join( |
| 542 | weechat.info_get('weechat_dir', ''), | 539 | weechat.info_get('weechat_dir', ''), 'beinc_weechat.json' |
| 543 | 'beinc_weechat.json', | ||
| 544 | ) | 540 | ) |
| 545 | beinc_prnt(f'Parsing {beinc_config_file_str}...') | 541 | beinc_prnt(f'Parsing {beinc_config_file_str}...') |
| 546 | custom_error = 'load error' | 542 | custom_error = 'load error' |
| @@ -565,8 +561,8 @@ def beinc_init(): | |||
| 565 | for target in config_dict['irc_client']['targets']: | 561 | for target in config_dict['irc_client']['targets']: |
| 566 | try: | 562 | try: |
| 567 | new_target = WeechatTarget(target) | 563 | new_target = WeechatTarget(target) |
| 568 | except Exception as e: | 564 | except Exception as exp: |
| 569 | beinc_prnt(f'Unable to add target: {e}') | 565 | beinc_prnt(f'Unable to add target: {exp}') |
| 570 | continue | 566 | continue |
| 571 | if new_target.channel_messages_policy: | 567 | if new_target.channel_messages_policy: |
| 572 | global_values['global_channel_messages_policy'] = True | 568 | global_values['global_channel_messages_policy'] = True |
| @@ -577,10 +573,10 @@ def beinc_init(): | |||
| 577 | target_list.append(new_target) | 573 | target_list.append(new_target) |
| 578 | beinc_prnt(f'BEINC target "{new_target.name}" added') | 574 | beinc_prnt(f'BEINC target "{new_target.name}" added') |
| 579 | beinc_prnt('Done!') | 575 | beinc_prnt('Done!') |
| 580 | except Exception as e: | 576 | except Exception as exp: |
| 581 | beinc_prnt( | 577 | beinc_prnt( |
| 582 | f'ERROR: unable to parse {beinc_config_file_str}: ' | 578 | f'ERROR: unable to parse {beinc_config_file_str}: ' |
| 583 | f'{custom_error} - {e}\nBEINC is now disabled' | 579 | f'{custom_error} - {exp}\nBEINC is now disabled' |
| 584 | ) | 580 | ) |
| 585 | enabled = False | 581 | enabled = False |
| 586 | # do not return error / exit the script | 582 | # do not return error / exit the script |
| @@ -594,7 +590,7 @@ weechat.register( | |||
| 594 | __author__, | 590 | __author__, |
| 595 | __version__, | 591 | __version__, |
| 596 | __license__, | 592 | __license__, |
| 597 | 'Blackmore\'s Extended IRC Notification Collection (Weechat Client)', | 593 | "Blackmore's Extended IRC Notification Collection (Weechat Client)", |
| 598 | '', | 594 | '', |
| 599 | '', | 595 | '', |
| 600 | ) | 596 | ) |
