diff options
Diffstat (limited to 'beinc_generic_client.py')
| -rwxr-xr-x | beinc_generic_client.py | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/beinc_generic_client.py b/beinc_generic_client.py index 476287e..44e8698 100755 --- a/beinc_generic_client.py +++ b/beinc_generic_client.py | |||
| @@ -1,8 +1,7 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | 2 | ||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.3 | 3 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) |
| 5 | # Copyright (C) 2013-2022 Simeon Simeonov | 4 | # Copyright (C) 2013-2024 Simeon Simeonov |
| 6 | 5 | ||
| 7 | # 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 |
| 8 | # 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,12 +16,14 @@ | |||
| 17 | # You should have received a copy of the GNU General Public License | 16 | # You should have received a copy of the GNU General Public License |
| 18 | # 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/>. |
| 19 | """A generic BEINC client that can be used for testing or as a template""" | 18 | """A generic BEINC client that can be used for testing or as a template""" |
| 19 | |||
| 20 | import argparse | 20 | import argparse |
| 21 | import errno | 21 | import errno |
| 22 | import getpass | 22 | import getpass |
| 23 | import io | 23 | import io |
| 24 | import json | 24 | import json |
| 25 | import os | 25 | import os |
| 26 | import pathlib | ||
| 26 | import socket | 27 | import socket |
| 27 | import ssl | 28 | import ssl |
| 28 | import sys | 29 | import sys |
| @@ -30,7 +31,7 @@ import urllib.parse | |||
| 30 | import urllib.request | 31 | import urllib.request |
| 31 | 32 | ||
| 32 | __author__ = 'Simeon Simeonov' | 33 | __author__ = 'Simeon Simeonov' |
| 33 | __version__ = '4.3' | 34 | __version__ = '4.4' |
| 34 | __license__ = 'GPL3' | 35 | __license__ = 'GPL3' |
| 35 | 36 | ||
| 36 | 37 | ||
| @@ -55,14 +56,14 @@ def fetch_password(args_password): | |||
| 55 | except KeyboardInterrupt: | 56 | except KeyboardInterrupt: |
| 56 | eprint(os.linesep + 'Prompt terminated') | 57 | eprint(os.linesep + 'Prompt terminated') |
| 57 | sys.exit(errno.EACCES) | 58 | sys.exit(errno.EACCES) |
| 58 | elif os.path.isfile(args_password): | 59 | elif pathlib.Path(args_password).is_file(): |
| 59 | try: | 60 | try: |
| 60 | with io.open(args_password, 'r', encoding='utf-8') as fp: | 61 | with io.open(args_password, 'r', encoding='utf-8') as fp: |
| 61 | passwd = fp.readline() | 62 | passwd = fp.readline() |
| 62 | if passwd.strip(): | 63 | if passwd.strip(): |
| 63 | return passwd.strip() | 64 | return passwd.strip() |
| 64 | except Exception as e: | 65 | except Exception as exp: |
| 65 | eprint(f'Unable to open password file: {e}') | 66 | eprint(f'Unable to open password file: {exp}') |
| 66 | sys.exit(1) | 67 | sys.exit(1) |
| 67 | return args_password | 68 | return args_password |
| 68 | 69 | ||
| @@ -83,17 +84,14 @@ def pull_notifications(ssl_context, args): | |||
| 83 | response = urllib.request.urlopen( | 84 | response = urllib.request.urlopen( |
| 84 | args.url, | 85 | args.url, |
| 85 | data=urllib.parse.urlencode( | 86 | data=urllib.parse.urlencode( |
| 86 | ( | 87 | (('resource_name', args.rname), ('password', args.password)) |
| 87 | ('resource_name', args.rname), | ||
| 88 | ('password', args.password), | ||
| 89 | ) | ||
| 90 | ).encode('utf-8'), | 88 | ).encode('utf-8'), |
| 91 | timeout=args.socket_timeout, | 89 | timeout=args.socket_timeout, |
| 92 | context=ssl_context, | 90 | context=ssl_context, |
| 93 | ) | 91 | ) |
| 94 | response_dict = json.loads(response.read().decode('utf-8')) | 92 | response_dict = json.loads(response.read().decode('utf-8')) |
| 95 | if response.code != 200: | 93 | if response.code != 200: |
| 96 | raise socket.error(response_dict.get('message', '')) | 94 | raise OSError(response_dict.get('message', '')) |
| 97 | return response_dict['data']['messages'] | 95 | return response_dict['data']['messages'] |
| 98 | 96 | ||
| 99 | 97 | ||
| @@ -122,7 +120,7 @@ def push_notification(ssl_context, args): | |||
| 122 | ) | 120 | ) |
| 123 | response_dict = json.loads(response.read().decode('utf-8')) | 121 | response_dict = json.loads(response.read().decode('utf-8')) |
| 124 | if response.code != 200: | 122 | if response.code != 200: |
| 125 | raise socket.error(response_dict.get('message', '')) | 123 | raise OSError(response_dict.get('message', '')) |
| 126 | 124 | ||
| 127 | 125 | ||
| 128 | def main(inargs=None): | 126 | def main(inargs=None): |
| @@ -131,10 +129,7 @@ def main(inargs=None): | |||
| 131 | description='The following options are available' | 129 | description='The following options are available' |
| 132 | ) | 130 | ) |
| 133 | parser.add_argument( | 131 | parser.add_argument( |
| 134 | 'url', | 132 | 'url', metavar='URL', type=str, help='BEINC server destination URL' |
| 135 | metavar='URL', | ||
| 136 | type=str, | ||
| 137 | help='BEINC server destination URL', | ||
| 138 | ) | 133 | ) |
| 139 | parser.add_argument( | 134 | parser.add_argument( |
| 140 | '-c', | 135 | '-c', |
| @@ -242,14 +237,14 @@ def main(inargs=None): | |||
| 242 | push_notification(context, args) | 237 | push_notification(context, args) |
| 243 | print('OK') | 238 | print('OK') |
| 244 | sys.exit(0) | 239 | sys.exit(0) |
| 245 | except ssl.SSLError as e: | 240 | except ssl.SSLError as err: |
| 246 | eprint(f'BEINC SSL/TLS error: {e}') | 241 | eprint(f'BEINC SSL/TLS error: {err}') |
| 247 | sys.exit(errno.EPERM) | 242 | sys.exit(errno.EPERM) |
| 248 | except socket.error as e: | 243 | except OSError as err: |
| 249 | eprint(f'BEINC connection error: {e}') | 244 | eprint(f'BEINC connection error: {err}') |
| 250 | sys.exit(errno.EPERM) | 245 | sys.exit(errno.EPERM) |
| 251 | except Exception as e: | 246 | except Exception as exp: |
| 252 | eprint(f'BEINC generic client error: {e}') | 247 | eprint(f'BEINC generic client error: {exp}') |
| 253 | sys.exit(errno.EPERM) | 248 | sys.exit(errno.EPERM) |
| 254 | 249 | ||
| 255 | 250 | ||
