summaryrefslogtreecommitdiff
path: root/beinc_poller.py
diff options
context:
space:
mode:
authorSimeon Simeonov2014-05-08 15:56:45 +0200
committerSimeon Simeonov2014-05-08 15:56:45 +0200
commitf29504285598af79076c981985dccb1d2033efd4 (patch)
tree43b91225d4015d3726e9e460adbb2f6f392c52b7 /beinc_poller.py
parente4e15e7f4e594efc899016ba17cabd02fa344600 (diff)
beinc_poller added
Diffstat (limited to 'beinc_poller.py')
-rwxr-xr-xbeinc_poller.py182
1 files changed, 182 insertions, 0 deletions
diff --git a/beinc_poller.py b/beinc_poller.py
new file mode 100755
index 0000000..aba6353
--- /dev/null
+++ b/beinc_poller.py
@@ -0,0 +1,182 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Blackmore's Enhanced IRC-Notification Collection (BEINC) v1.0
5# Copyright (C) 2013-2014 Simeon Simeonov
6
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
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16
17# 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/>.
19
20
21import argparse
22import errno
23import getpass
24import httplib
25import json
26import os
27import sched
28import socket
29import ssl
30import sys
31import time
32import urllib
33import urllib2
34
35try:
36 import pynotify
37except ImportError as e:
38 sys.stderr.write('A working pynotify library is required by BEINC-poller\n')
39 sys.exit(1)
40
41
42__author__ = 'Simeon Simeonov'
43__version__ = '1.0'
44__license__ = 'GPL3'
45
46
47class ValidHTTPSConnection(httplib.HTTPConnection):
48 """
49 Implements a simple CERT verification functionality
50 """
51
52 default_port = httplib.HTTPS_PORT
53
54 def __init__(self, *args, **kwargs):
55 httplib.HTTPConnection.__init__(self, *args, **kwargs)
56
57 def connect(self):
58 sock = socket.create_connection((self.host, self.port),
59 self.timeout, self.source_address)
60 if self._tunnel_host:
61 self.sock = sock
62 self._tunnel()
63 self.sock = ssl.wrap_socket(sock,
64 ca_certs=global_beinc_cert_file,
65 cert_reqs=ssl.CERT_REQUIRED)
66
67
68class ValidHTTPSHandler(urllib2.HTTPSHandler):
69 """
70 Implements a simple CERT verification functionality
71 """
72
73 def https_open(self, req):
74 return self.do_open(ValidHTTPSConnection, req)
75
76
77def poll_notifications(scheduler, args):
78 """
79 """
80 try:
81 post_values = {'password': args.password}
82 data = urllib.urlencode(post_values)
83 req = urllib2.Request(args.url, data)
84 if args.cert: # check for cert validity
85 global global_beinc_cert_file # ugly hack
86 global_beinc_cert_file = args.cert
87 opener = urllib2.build_opener(ValidHTTPSHandler)
88 response = opener.open(req)
89 else: # ... or don't
90 response = urllib2.urlopen(req)
91 res_code = response.code
92 res_str = response.read()
93 if res_code == 200 and args.debug:
94 print('Server responded: OK')
95 print('Body:\n{0}'.format(res_str))
96 res_list = json.loads(res_str)
97 print(type(res_list))
98 response.close()
99 scheduler.enter(args.frequency,
100 1,
101 poll_notifications,
102 (scheduler, args))
103 except urllib2.HTTPError as e:
104 sys.stderr.write('BEINC-server error ({0} - {1})\n'.format(e.code,
105 e.reason))
106 except Exception as e:
107 sys.stderr.write('BEINC-poller error: {0}\n'.format(e))
108 sys.exit(errno.EPERM)
109
110
111def main():
112 parser = argparse.ArgumentParser(
113 description='The following options are available')
114 parser.add_argument(
115 'url',
116 metavar='URL',
117 type=str,
118 help='BEINC destination URL')
119 parser.add_argument(
120 '-c', '--cert-file',
121 metavar='FILE',
122 type=str,
123 dest='cert',
124 default='',
125 help='BEINC CA-cert to check the server-cert against (default: None)')
126 parser.add_argument(
127 '-d',
128 action='store_true',
129 dest='daemonize',
130 default=False,
131 help='Run the poller-process in the background')
132 parser.add_argument(
133 '-D', '--debug',
134 action='store_true',
135 dest='debug',
136 default=False,
137 help='Run the poller-process in debug-mode (disables daemonize)')
138 parser.add_argument(
139 '-f', '--frequency',
140 metavar='SECONDS',
141 type=int,
142 dest='frequency',
143 default=10,
144 help='Polling frequency in seconds (default: 10)')
145 parser.add_argument(
146 '-p', '--password',
147 metavar='PASSWORD',
148 type=str,
149 dest='password',
150 default='',
151 help='BEINC taget-password (default & recommended: prompt for passwd)')
152 parser.add_argument(
153 '-t', '--osd-timeout',
154 metavar='MILLISECONDS',
155 type=int,
156 dest='osd_timeout',
157 default=5000,
158 help='OSD timeout (default: 5000)')
159 parser.add_argument(
160 '-v', '--version',
161 action='version',
162 version='%(prog)s {0}'.format(__version__),
163 help='display program-version and exit')
164 args = parser.parse_args()
165 if not args.password:
166 try:
167 args.password = getpass.getpass()
168 except Exception as e:
169 sys.stderr.write('Prompt terminated\n')
170 sys.exit(errno.EACCES)
171
172 if not pynotify.init('BEINC Notify'):
173 sys.stderr.write('There was a problem with libnotify\n')
174 sys.exit(1)
175
176 sc = sched.scheduler(time.time, time.sleep)
177 sc.enter(args.frequency, 1, poll_notifications, (sc, args))
178 sc.run()
179
180
181if __name__ == '__main__':
182 main()