diff options
| author | Simeon Simeonov | 2015-04-17 12:02:18 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2015-04-17 12:02:18 +0200 |
| commit | b2120ed79c11505a326f18d8b465449dd0f6d57c (patch) | |
| tree | 80324301454d19c27126b8136b8ff487cb1d95d5 | |
| parent | 4a9f282bf7a153eecc5b205268df69cbeb4111b3 (diff) | |
beinc_weechat.py v2.0 completed
| -rw-r--r-- | beinc_weechat.py | 135 |
1 files changed, 69 insertions, 66 deletions
diff --git a/beinc_weechat.py b/beinc_weechat.py index 442b01f..c52396d 100644 --- a/beinc_weechat.py +++ b/beinc_weechat.py | |||
| @@ -216,21 +216,30 @@ class WeechatTarget(object): | |||
| 216 | """ | 216 | """ |
| 217 | self.__enabled = value | 217 | self.__enabled = value |
| 218 | 218 | ||
| 219 | @property | ||
| 220 | def connected(self): | ||
| 221 | """ | ||
| 222 | The target's connection status (read-only property) | ||
| 223 | """ | ||
| 224 | return bool(self.__connection) | ||
| 225 | |||
| 219 | def __repr__(self): | 226 | def __repr__(self): |
| 220 | """ | 227 | """ |
| 221 | """ | 228 | """ |
| 222 | return 'name: {0}\nurl: {1}\nchannel_list: {2}\nnick_list: {3}\n'\ | 229 | return ('name: {0}\nurl: {1}\nchannel_list: {2}\nnick_list: {3}\n' |
| 223 | 'channel_messages_policy: {4}\nprivate_messages_policy: {5}\n'\ | 230 | 'channel_messages_policy: {4}\nprivate_messages_policy: {5}\n' |
| 224 | 'notifications_policy: {6}\nenabled: {7}\ndebug: {8}\n\n'.format( | 231 | 'notifications_policy: {6}\nenabled: {7}\nconnected: {8}\n' |
| 225 | self.__name, | 232 | 'debug: {9}\n\n'.format( |
| 226 | self.__url, | 233 | self.__name, |
| 227 | ', '.join(self.__chans), | 234 | self.__url, |
| 228 | ', '.join(self.__nicks), | 235 | ', '.join(self.__chans), |
| 229 | self.__chan_messages_policy, | 236 | ', '.join(self.__nicks), |
| 230 | self.__priv_messages_policy, | 237 | self.__chan_messages_policy, |
| 231 | self.__notifications_policy, | 238 | self.__priv_messages_policy, |
| 232 | 'yes' if self.__enabled else 'no', | 239 | self.__notifications_policy, |
| 233 | 'yes' if self.__debug else 'no') | 240 | 'yes' if self.__enabled else 'no', |
| 241 | 'yes' if bool(self.__connection) else 'no', | ||
| 242 | 'yes' if self.__debug else 'no')) | ||
| 234 | 243 | ||
| 235 | def send_private_message_notification(self, values): | 244 | def send_private_message_notification(self, values): |
| 236 | """ | 245 | """ |
| @@ -239,16 +248,12 @@ class WeechatTarget(object): | |||
| 239 | values: dict pupulated by the irc msg-handler | 248 | values: dict pupulated by the irc msg-handler |
| 240 | """ | 249 | """ |
| 241 | try: | 250 | try: |
| 242 | title_str = self.__fetch_formatted_str(self.__pm_title_template, | 251 | title = self.__fetch_formatted_str(self.__pm_title_template, |
| 243 | values) | 252 | values) |
| 244 | message_str = self.__fetch_formatted_str( | 253 | message = self.__fetch_formatted_str( |
| 245 | self.__pm_message_template, | 254 | self.__pm_message_template, |
| 246 | values) | 255 | values) |
| 247 | post_values = {'title': title_str, | 256 | if not self.__send_beinc_message(title, message) and self.__debug: |
| 248 | 'message': message_str, | ||
| 249 | 'password': self.__password} | ||
| 250 | data = urllib.urlencode(post_values) | ||
| 251 | if not self.__send_beinc_message(data) and self.__debug: | ||
| 252 | beinc_prnt( | 257 | beinc_prnt( |
| 253 | 'BEINC DEBUG: send_private_message_notification-ERROR ' | 258 | 'BEINC DEBUG: send_private_message_notification-ERROR ' |
| 254 | 'for "{0}": __send_beinc_message -> False'.format( | 259 | 'for "{0}": __send_beinc_message -> False'.format( |
| @@ -266,16 +271,11 @@ class WeechatTarget(object): | |||
| 266 | values: dict pupulated by the irc msg-handler | 271 | values: dict pupulated by the irc msg-handler |
| 267 | """ | 272 | """ |
| 268 | try: | 273 | try: |
| 269 | title_str = self.__fetch_formatted_str(self.__cm_title_template, | 274 | title = self.__fetch_formatted_str(self.__cm_title_template, |
| 270 | values) | 275 | values) |
| 271 | message_str = self.__fetch_formatted_str( | 276 | message = self.__fetch_formatted_str(self.__cm_message_template, |
| 272 | self.__cm_message_template, | 277 | values) |
| 273 | values) | 278 | if not self.__send_beinc_message(title, message) and self.__debug: |
| 274 | post_values = {'title': title_str, | ||
| 275 | 'message': message_str, | ||
| 276 | 'password': self.__password} | ||
| 277 | data = urllib.urlencode(post_values) | ||
| 278 | if not self.__send_beinc_message(data) and self.__debug: | ||
| 279 | beinc_prnt( | 279 | beinc_prnt( |
| 280 | 'BEINC DEBUG: send_channel_message_notification-ERROR ' | 280 | 'BEINC DEBUG: send_channel_message_notification-ERROR ' |
| 281 | 'for "{0}": __send_beinc_message -> False'.format( | 281 | 'for "{0}": __send_beinc_message -> False'.format( |
| @@ -293,16 +293,11 @@ class WeechatTarget(object): | |||
| 293 | values: dict pupulated by the irc msg-handler | 293 | values: dict pupulated by the irc msg-handler |
| 294 | """ | 294 | """ |
| 295 | try: | 295 | try: |
| 296 | title_str = self.__fetch_formatted_str(self.__nm_title_template, | 296 | title = self.__fetch_formatted_str(self.__nm_title_template, |
| 297 | values) | 297 | values) |
| 298 | message_str = self.__fetch_formatted_str( | 298 | message = self.__fetch_formatted_str(self.__nm_message_template, |
| 299 | self.__nm_message_template, | 299 | values) |
| 300 | values) | 300 | if not self.__send_beinc_message(title, message) and self.__debug: |
| 301 | post_values = {'title': title_str, | ||
| 302 | 'message': message_str, | ||
| 303 | 'password': self.__password} | ||
| 304 | data = urllib.urlencode(post_values) | ||
| 305 | if not self.__send_beinc_message(data) and self.__debug: | ||
| 306 | beinc_prnt( | 301 | beinc_prnt( |
| 307 | 'BEINC DEBUG: send_notify_message_notification-ERROR ' | 302 | 'BEINC DEBUG: send_notify_message_notification-ERROR ' |
| 308 | 'for "{0}": __send_beinc_message -> False'.format( | 303 | 'for "{0}": __send_beinc_message -> False'.format( |
| @@ -321,11 +316,8 @@ class WeechatTarget(object): | |||
| 321 | message: a single message string | 316 | message: a single message string |
| 322 | """ | 317 | """ |
| 323 | try: | 318 | try: |
| 324 | post_values = {'title': 'BEINC broadcast', | 319 | title = 'BEINC broadcast' |
| 325 | 'message': message, | 320 | if not self.__send_beinc_message(title, message) and self.__debug: |
| 326 | 'password': self.__password} | ||
| 327 | data = urllib.urlencode(post_values) | ||
| 328 | if not self.__send_beinc_message(data) and self.__debug: | ||
| 329 | beinc_prnt( | 321 | beinc_prnt( |
| 330 | 'BEINC DEBUG: send_broadcast_notification-ERROR ' | 322 | 'BEINC DEBUG: send_broadcast_notification-ERROR ' |
| 331 | 'for "{0}": __send_beinc_message -> False'.format( | 323 | 'for "{0}": __send_beinc_message -> False'.format( |
| @@ -413,33 +405,44 @@ class WeechatTarget(object): | |||
| 413 | template = template.replace(key, value) | 405 | template = template.replace(key, value) |
| 414 | return template.encode('utf-8') | 406 | return template.encode('utf-8') |
| 415 | 407 | ||
| 416 | def __send_beinc_message(self, data): | 408 | def __send_beinc_message(self, title, message): |
| 417 | """ | 409 | """ |
| 418 | the function implements the BEINC "protocol" by generating a simple | 410 | the function implements the BEINC "protocol" by generating a simple |
| 419 | HTTP request | 411 | XMLRPC request |
| 420 | """ | 412 | """ |
| 421 | try: | 413 | try: |
| 422 | req = urllib2.Request(self.__url, data) | 414 | if not self.__connection and not self.__connection_setup(): |
| 423 | if self.__cert_file: | 415 | return False |
| 424 | global global_beinc_cert_file | 416 | if self.__socket_timeout: |
| 425 | global_beinc_cert_file = self.__cert_file | 417 | socket.setdefaulttimeout(self.__socket_timeout) |
| 426 | opener = urllib2.build_opener(ValidHTTPSHandler) | 418 | result = self.__connection.push(self.__name, |
| 427 | response = opener.open(req, timeout=self.__socket_timeout) | 419 | self.__password, |
| 428 | else: | 420 | title, |
| 429 | response = urllib2.urlopen(req, self.__socket_timeout) | 421 | message) |
| 430 | res_code = response.code | 422 | if self.__debug: |
| 431 | response.close() | 423 | beinc_prnt('BEINC DEBUG: Server responded: {0}'.format(result)) |
| 432 | if res_code == 200: | 424 | return True |
| 433 | return True | 425 | except xmlrpclib.Fault as fault: |
| 434 | except urllib2.HTTPError as e: | ||
| 435 | if self.__debug: | 426 | if self.__debug: |
| 436 | beinc_prnt( | 427 | beinc_prnt( |
| 437 | 'BEINC DEBUG: send_beinc_message-ERROR for "{0}": {1} ->' | 428 | 'BEINC DEBUG: Server answered with ' |
| 438 | ' ({2} - {3})'.format(self.__name, | 429 | 'errorCode={0}: {1}\n'.format( |
| 439 | e.url, | 430 | fault.faultCode, |
| 440 | e.code, | 431 | fault.faultString)) |
| 441 | e.reason)) | 432 | except ssl.SSLError as e: |
| 442 | # all other exception should be handled by the caller | 433 | if self.__debug: |
| 434 | beinc_prnt('BEINC DEBUG: SSL/TLS error: {0}\n'.format(e)) | ||
| 435 | except socket.error as e: | ||
| 436 | if self.__debug: | ||
| 437 | beinc_prnt('BEINC DEBUG: Connection error: {0}\n'.format(e)) | ||
| 438 | except Exception as e: | ||
| 439 | if self.__debug: | ||
| 440 | beinc_prnt('BEINC DEBUG: Unable to send message: {0}\n'.format( | ||
| 441 | e)) | ||
| 442 | finally: | ||
| 443 | if self.__socket_timeout: | ||
| 444 | socket.setdefaulttimeout(None) | ||
| 445 | self.__connection = None | ||
| 443 | return False | 446 | return False |
| 444 | 447 | ||
| 445 | 448 | ||
