diff options
| author | Simeon Simeonov | 2022-09-10 10:27:18 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2022-09-10 10:27:18 +0200 |
| commit | 003cd12be734b05d7281579d3d377f79425531fa (patch) | |
| tree | edac9e287a9d8a38b97df2c1722bfc732bc781d3 /beinc_generic_client.py | |
| parent | 1dab5dbd8da35d7c7f9f44915fe5be7f7b1e5b79 (diff) | |
Remove obsolete code and use black alike code styling
Diffstat (limited to 'beinc_generic_client.py')
| -rwxr-xr-x | beinc_generic_client.py | 86 |
1 files changed, 55 insertions, 31 deletions
diff --git a/beinc_generic_client.py b/beinc_generic_client.py index ca3847a..af1e74f 100755 --- a/beinc_generic_client.py +++ b/beinc_generic_client.py | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
| 3 | 3 | ||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.0 | 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.0 |
| 5 | # Copyright (C) 2013-2020 Simeon Simeonov | 5 | # Copyright (C) 2013-2022 Simeon Simeonov |
| 6 | 6 | ||
| 7 | # This program is free software: you can redistribute it and/or modify | 7 | # 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 | 8 | # it under the terms of the GNU General Public License as published by |
| @@ -29,9 +29,8 @@ import sys | |||
| 29 | import urllib.parse | 29 | import urllib.parse |
| 30 | import urllib.request | 30 | import urllib.request |
| 31 | 31 | ||
| 32 | |||
| 33 | __author__ = 'Simeon Simeonov' | 32 | __author__ = 'Simeon Simeonov' |
| 34 | __version__ = '4.1' | 33 | __version__ = '4.2' |
| 35 | __license__ = 'GPL3' | 34 | __license__ = 'GPL3' |
| 36 | 35 | ||
| 37 | 36 | ||
| @@ -58,7 +57,7 @@ def fetch_password(args_password): | |||
| 58 | sys.exit(errno.EACCES) | 57 | sys.exit(errno.EACCES) |
| 59 | elif os.path.isfile(args_password): | 58 | elif os.path.isfile(args_password): |
| 60 | try: | 59 | try: |
| 61 | with io.open(args_password, 'r') as fp: | 60 | with io.open(args_password, 'r', encoding='utf-8') as fp: |
| 62 | passwd = fp.readline() | 61 | passwd = fp.readline() |
| 63 | if passwd.strip(): | 62 | if passwd.strip(): |
| 64 | return passwd.strip() | 63 | return passwd.strip() |
| @@ -86,10 +85,12 @@ def pull_notifications(ssl_context, args): | |||
| 86 | data=urllib.parse.urlencode( | 85 | data=urllib.parse.urlencode( |
| 87 | ( | 86 | ( |
| 88 | ('resource_name', args.rname), | 87 | ('resource_name', args.rname), |
| 89 | ('password', args.password) | 88 | ('password', args.password), |
| 90 | )).encode('utf-8'), | 89 | ) |
| 90 | ).encode('utf-8'), | ||
| 91 | timeout=args.socket_timeout, | 91 | timeout=args.socket_timeout, |
| 92 | context=ssl_context) | 92 | context=ssl_context, |
| 93 | ) | ||
| 93 | response_dict = json.loads(response.read().decode('utf-8')) | 94 | response_dict = json.loads(response.read().decode('utf-8')) |
| 94 | if response.code != 200: | 95 | if response.code != 200: |
| 95 | raise socket.error(response_dict.get('message', '')) | 96 | raise socket.error(response_dict.get('message', '')) |
| @@ -113,10 +114,12 @@ def push_notification(ssl_context, args): | |||
| 113 | ('resource_name', args.rname), | 114 | ('resource_name', args.rname), |
| 114 | ('password', args.password), | 115 | ('password', args.password), |
| 115 | ('title', args.title), | 116 | ('title', args.title), |
| 116 | ('message', args.message) | 117 | ('message', args.message), |
| 117 | )).encode('utf-8'), | 118 | ) |
| 119 | ).encode('utf-8'), | ||
| 118 | timeout=args.socket_timeout, | 120 | timeout=args.socket_timeout, |
| 119 | context=ssl_context) | 121 | context=ssl_context, |
| 122 | ) | ||
| 120 | response_dict = json.loads(response.read().decode('utf-8')) | 123 | response_dict = json.loads(response.read().decode('utf-8')) |
| 121 | if response.code != 200: | 124 | if response.code != 200: |
| 122 | raise socket.error(response_dict.get('message', '')) | 125 | raise socket.error(response_dict.get('message', '')) |
| @@ -125,91 +128,112 @@ def push_notification(ssl_context, args): | |||
| 125 | def main(inargs=None): | 128 | def main(inargs=None): |
| 126 | """main entry""" | 129 | """main entry""" |
| 127 | parser = argparse.ArgumentParser( | 130 | parser = argparse.ArgumentParser( |
| 128 | description='The following options are available') | 131 | description='The following options are available' |
| 132 | ) | ||
| 129 | parser.add_argument( | 133 | parser.add_argument( |
| 130 | 'url', | 134 | 'url', |
| 131 | metavar='URL', | 135 | metavar='URL', |
| 132 | type=str, | 136 | type=str, |
| 133 | help='BEINC server destination URL') | 137 | help='BEINC server destination URL', |
| 138 | ) | ||
| 134 | parser.add_argument( | 139 | parser.add_argument( |
| 135 | '-c', '--cert-file', | 140 | '-c', |
| 141 | '--cert-file', | ||
| 136 | metavar='FILE', | 142 | metavar='FILE', |
| 137 | type=str, | 143 | type=str, |
| 138 | dest='cert', | 144 | dest='cert', |
| 139 | default='', | 145 | default='', |
| 140 | help='CA-cert to check the server-cert against' | 146 | help='CA-cert to check the server-cert against' |
| 141 | '(default: Check disabled)') | 147 | '(default: Check disabled)', |
| 148 | ) | ||
| 142 | parser.add_argument( | 149 | parser.add_argument( |
| 143 | '--ciphers', | 150 | '--ciphers', |
| 144 | metavar='CIPHERS', | 151 | metavar='CIPHERS', |
| 145 | type=str, | 152 | type=str, |
| 146 | dest='ciphers', | 153 | dest='ciphers', |
| 147 | default='', | 154 | default='', |
| 148 | help='Preferred ciphers list (default: auto)') | 155 | help='Preferred ciphers list (default: auto)', |
| 156 | ) | ||
| 149 | parser.add_argument( | 157 | parser.add_argument( |
| 150 | '--disable-hostname-check', | 158 | '--disable-hostname-check', |
| 151 | action='store_true', | 159 | action='store_true', |
| 152 | dest='disable_hostname_check', | 160 | dest='disable_hostname_check', |
| 153 | default=False, | 161 | default=False, |
| 154 | help='Do not check whether server cert matches server hostname') | 162 | help='Do not check whether server cert matches server hostname', |
| 163 | ) | ||
| 155 | parser.add_argument( | 164 | parser.add_argument( |
| 156 | '-m', '--message', | 165 | '-m', |
| 166 | '--message', | ||
| 157 | metavar='MESSAGE', | 167 | metavar='MESSAGE', |
| 158 | type=str, | 168 | type=str, |
| 159 | dest='message', | 169 | dest='message', |
| 160 | default='BEINC message', | 170 | default='BEINC message', |
| 161 | help='BEINC message (default: "BEINC message")') | 171 | help='BEINC message (default: "BEINC message")', |
| 172 | ) | ||
| 162 | parser.add_argument( | 173 | parser.add_argument( |
| 163 | '-n', '--resource-name', | 174 | '-n', |
| 175 | '--resource-name', | ||
| 164 | metavar='NAME', | 176 | metavar='NAME', |
| 165 | type=str, | 177 | type=str, |
| 166 | dest='rname', | 178 | dest='rname', |
| 167 | required=True, | 179 | required=True, |
| 168 | help='The name of the BEINC-resource on the remote server') | 180 | help='The name of the BEINC-resource on the remote server', |
| 181 | ) | ||
| 169 | parser.add_argument( | 182 | parser.add_argument( |
| 170 | '-p', '--password', | 183 | '-p', |
| 184 | '--password', | ||
| 171 | metavar='PASSWORD[FILE]', | 185 | metavar='PASSWORD[FILE]', |
| 172 | type=str, | 186 | type=str, |
| 173 | dest='password', | 187 | dest='password', |
| 174 | default='', | 188 | default='', |
| 175 | help='BEINC taget-password / text-file containing the target password' | 189 | help='BEINC taget-password / text-file containing the target password' |
| 176 | ' (default & recommended: prompt for passwd)') | 190 | ' (default & recommended: prompt for passwd)', |
| 191 | ) | ||
| 177 | parser.add_argument( | 192 | parser.add_argument( |
| 178 | '--pull', | 193 | '--pull', |
| 179 | action='store_true', | 194 | action='store_true', |
| 180 | dest='pull', | 195 | dest='pull', |
| 181 | default=False, | 196 | default=False, |
| 182 | help='Perform a pull operation (default: push)') | 197 | help='Perform a pull operation (default: push)', |
| 198 | ) | ||
| 183 | parser.add_argument( | 199 | parser.add_argument( |
| 184 | '-T', '--socket-timeout', | 200 | '-T', |
| 201 | '--socket-timeout', | ||
| 185 | metavar='SECONDS', | 202 | metavar='SECONDS', |
| 186 | type=int, | 203 | type=int, |
| 187 | dest='socket_timeout', | 204 | dest='socket_timeout', |
| 188 | default=3, | 205 | default=3, |
| 189 | help='Socket timeout in seconds (0=Python default) (default: 3)') | 206 | help='Socket timeout in seconds (0=Python default) (default: 3)', |
| 207 | ) | ||
| 190 | parser.add_argument( | 208 | parser.add_argument( |
| 191 | '-t', '--title', | 209 | '-t', |
| 210 | '--title', | ||
| 192 | metavar='TITLE', | 211 | metavar='TITLE', |
| 193 | type=str, | 212 | type=str, |
| 194 | dest='title', | 213 | dest='title', |
| 195 | default='BEINC title', | 214 | default='BEINC title', |
| 196 | help='BEINC title (default: "BEINC title"') | 215 | help='BEINC title (default: "BEINC title"', |
| 216 | ) | ||
| 197 | parser.add_argument( | 217 | parser.add_argument( |
| 198 | '-v', '--version', | 218 | '-v', |
| 219 | '--version', | ||
| 199 | action='version', | 220 | action='version', |
| 200 | version=f'%(prog)s {__version__}', | 221 | version=f'%(prog)s {__version__}', |
| 201 | help='display program-version and exit') | 222 | help='display program-version and exit', |
| 223 | ) | ||
| 202 | args = parser.parse_args(inargs) | 224 | args = parser.parse_args(inargs) |
| 203 | if args.socket_timeout: | 225 | if args.socket_timeout: |
| 204 | socket.setdefaulttimeout(args.socket_timeout) | 226 | socket.setdefaulttimeout(args.socket_timeout) |
| 205 | args.password = fetch_password(args.password) | 227 | args.password = fetch_password(args.password) |
| 206 | try: | 228 | try: |
| 207 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) | 229 | context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 208 | context.verify_mode = ssl.CERT_NONE | ||
| 209 | if args.cert: | 230 | if args.cert: |
| 210 | context.verify_mode = ssl.CERT_REQUIRED | 231 | context.verify_mode = ssl.CERT_REQUIRED |
| 211 | context.load_verify_locations(cafile=os.path.expanduser(args.cert)) | 232 | context.load_verify_locations(cafile=os.path.expanduser(args.cert)) |
| 212 | context.check_hostname = bool(not args.disable_hostname_check) | 233 | context.check_hostname = bool(not args.disable_hostname_check) |
| 234 | else: | ||
| 235 | context.check_hostname = False | ||
| 236 | context.verify_mode = ssl.CERT_NONE | ||
| 213 | if args.ciphers: | 237 | if args.ciphers: |
| 214 | context.set_ciphers(args.ciphers) | 238 | context.set_ciphers(args.ciphers) |
| 215 | if args.pull: | 239 | if args.pull: |
