diff options
| author | Simeon Simeonov | 2015-04-16 15:43:59 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2015-04-16 15:43:59 +0200 |
| commit | 9c06ebe90039423809f51af1d62098ae7f5c813e (patch) | |
| tree | ce5bb4a1988b0a7ea0b0465eadd9fd0fe335b624 /beinc_weechat.py | |
| parent | b2ccfda61b7b7a1b37d4f9ffed57fcc4585d9ce0 (diff) | |
beinc_weechat.py v2.0 started
Diffstat (limited to 'beinc_weechat.py')
| -rw-r--r-- | beinc_weechat.py | 77 |
1 files changed, 73 insertions, 4 deletions
diff --git a/beinc_weechat.py b/beinc_weechat.py index b875be6..5f7da5a 100644 --- a/beinc_weechat.py +++ b/beinc_weechat.py | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
| 3 | 3 | ||
| 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v1.0 | 4 | # Blackmore's Enhanced IRC-Notification Collection (BEINC) v2.0 |
| 5 | # Copyright (C) 2013-2015 Simeon Simeonov | 5 | # Copyright (C) 2013-2015 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 |
| @@ -26,14 +26,13 @@ import random | |||
| 26 | import socket | 26 | import socket |
| 27 | import ssl | 27 | import ssl |
| 28 | import sys | 28 | import sys |
| 29 | import urllib | 29 | import xmlrpclib |
| 30 | import urllib2 | ||
| 31 | 30 | ||
| 32 | import weechat | 31 | import weechat |
| 33 | 32 | ||
| 34 | 33 | ||
| 35 | __author__ = 'Simeon Simeonov' | 34 | __author__ = 'Simeon Simeonov' |
| 36 | __version__ = '1.0' | 35 | __version__ = '2.0' |
| 37 | __license__ = 'GPL3' | 36 | __license__ = 'GPL3' |
| 38 | 37 | ||
| 39 | 38 | ||
| @@ -46,6 +45,14 @@ BEINC_POLICY_ALL = 1 | |||
| 46 | BEINC_POLICY_LIST_ONLY = 2 | 45 | BEINC_POLICY_LIST_ONLY = 2 |
| 47 | BEINC_CURRENT_CONFIG_VERSION = 1 | 46 | BEINC_CURRENT_CONFIG_VERSION = 1 |
| 48 | 47 | ||
| 48 | BEINC_SSL_METHODS = {'SSLv3': ssl.PROTOCOL_SSLv3, | ||
| 49 | 'TLSv1': ssl.PROTOCOL_TLSv1} | ||
| 50 | try: | ||
| 51 | BEINC_SSL_METHODS.update({'TLSv1_1': ssl.PROTOCOL_TLSv1_1}) | ||
| 52 | BEINC_SSL_METHODS.update({'TLSv1_2': ssl.PROTOCOL_TLSv1_2}) | ||
| 53 | except: | ||
| 54 | pass | ||
| 55 | |||
| 49 | 56 | ||
| 50 | class ValidHTTPSConnection(httplib.HTTPConnection): | 57 | class ValidHTTPSConnection(httplib.HTTPConnection): |
| 51 | """ | 58 | """ |
| @@ -121,6 +128,11 @@ class WeechatTarget(object): | |||
| 121 | self.__debug = bool(target_dict.get('debug', False)) | 128 | self.__debug = bool(target_dict.get('debug', False)) |
| 122 | self.__enabled = bool(target_dict.get('enabled', True)) | 129 | self.__enabled = bool(target_dict.get('enabled', True)) |
| 123 | self.__socket_timeout = int(target_dict.get('socket_timeout', 3)) | 130 | self.__socket_timeout = int(target_dict.get('socket_timeout', 3)) |
| 131 | self.__ciphers = target_dict.get('ciphers', '') | ||
| 132 | self.__disable_hostname_check = bool( | ||
| 133 | target_dict.get('disable-hostname-check', False)) | ||
| 134 | self.__ssl_version = target_dict.get('ssl_version', 'auto') | ||
| 135 | self.__connection = None # xmlrpclib.ServerProxy instance | ||
| 124 | 136 | ||
| 125 | @property | 137 | @property |
| 126 | def name(self): | 138 | def name(self): |
| @@ -298,6 +310,63 @@ class WeechatTarget(object): | |||
| 298 | 'BEINC DEBUG: send_broadcast_notification-ERROR ' | 310 | 'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 299 | 'for "{0}": {1}'.format(self.__name, e)) | 311 | 'for "{0}": {1}'.format(self.__name, e)) |
| 300 | 312 | ||
| 313 | def __connection_setup(self): | ||
| 314 | """ | ||
| 315 | """ | ||
| 316 | if self.__connection: | ||
| 317 | return True | ||
| 318 | try: | ||
| 319 | ssl_version = BEINC_SSL_METHODS.get(self.__ssl_version, | ||
| 320 | ssl.PROTOCOL_SSLv23) | ||
| 321 | if sys.hexversion >= 0x20709f0: | ||
| 322 | # Python >= 2.7.9 | ||
| 323 | context = ssl.SSLContext(ssl_version) | ||
| 324 | context.verify_mode = ssl.CERT_REQUIRED | ||
| 325 | if not self.__cert_file: | ||
| 326 | context.verify_mode = ssl.CERT_NONE | ||
| 327 | context.check_hostname = bool( | ||
| 328 | not self.__disable_hostname_check) | ||
| 329 | if self.__cert_file: | ||
| 330 | context.load_verify_locations(os.path.expanduser( | ||
| 331 | self.__cert_file)) | ||
| 332 | if self.__ciphers: | ||
| 333 | context.set_ciphers(self.__ciphers) | ||
| 334 | transport = xmlrpclib.SafeTransport(context=context) | ||
| 335 | else: | ||
| 336 | # Python < 2.7.9 | ||
| 337 | ssl_options = {} | ||
| 338 | ssl_options['ssl_version'] = ssl_version | ||
| 339 | if self.__cert_file: | ||
| 340 | ssl_options['ca_certs'] = os.path.expanduser( | ||
| 341 | self.__cert_file) | ||
| 342 | ssl_options['cert_reqs'] = ssl.CERT_REQUIRED | ||
| 343 | if self.__ciphers: | ||
| 344 | ssl_options['ciphers'] = self.__ciphers | ||
| 345 | transport = BEINCCustomSafeTransport( | ||
| 346 | custom_ssl_options=ssl_options) | ||
| 347 | self.__connection = xmlrpclib.ServerProxy(self.__url, | ||
| 348 | transport=transport) | ||
| 349 | return True | ||
| 350 | except xmlrpclib.Fault as fault: | ||
| 351 | if self.__debug: | ||
| 352 | beinc_prnt( | ||
| 353 | 'BEINC DEBUG: Server answered with ' | ||
| 354 | 'errorCode={0}: {1}\n'.format( | ||
| 355 | fault.faultCode, | ||
| 356 | fault.faultString)) | ||
| 357 | except ssl.SSLError as e: | ||
| 358 | if self.__debug: | ||
| 359 | beinc_prnt('BEINC DEBUG: SSL/TLS error: {0}\n'.format(e)) | ||
| 360 | except socket.error as e: | ||
| 361 | if self.__debug: | ||
| 362 | beinc_prnt('BEINC DEBUG: Connection error: {0}\n'.format(e)) | ||
| 363 | except Exception as e: | ||
| 364 | if self.__debug: | ||
| 365 | beinc_prnt('BEINC DEBUG: Generic client error: {0}\n'.format( | ||
| 366 | e)) | ||
| 367 | self.__connection = None | ||
| 368 | return False | ||
| 369 | |||
| 301 | def __fetch_formatted_str(self, template, values): | 370 | def __fetch_formatted_str(self, template, values): |
| 302 | """ | 371 | """ |
| 303 | returns a formatted string by replacing the defined | 372 | returns a formatted string by replacing the defined |
