summaryrefslogtreecommitdiff
path: root/beinc_poller.py
diff options
context:
space:
mode:
authorSimeon Simeonov2020-04-10 23:27:15 +0200
committerSimeon Simeonov2020-04-10 23:27:15 +0200
commit1a5e60fc1069d39db1b0935f4ac8e4cace83fb76 (patch)
treeac5f15c15c7a0fe7132be20d43a3f39683ac0204 /beinc_poller.py
parent9e3aee8c039b4b42232672762c2ea7cba097ce47 (diff)
Remove Python2 support and cleanup the code and the documentationv4.0-rc1
Diffstat (limited to 'beinc_poller.py')
-rwxr-xr-xbeinc_poller.py238
1 files changed, 0 insertions, 238 deletions
diff --git a/beinc_poller.py b/beinc_poller.py
deleted file mode 100755
index 624cd24..0000000
--- a/beinc_poller.py
+++ /dev/null
@@ -1,238 +0,0 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Blackmore's Enhanced IRC-Notification Collection (BEINC) v3.0
5# Copyright (C) 2013-2018 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 json
25import os
26import sched
27import socket
28import ssl
29import sys
30import time
31
32PY2 = sys.version_info[0] == 2
33PY3 = sys.version_info[0] == 3
34
35if PY3:
36 from urllib.parse import urlencode
37 from urllib.request import urlopen
38else:
39 from urllib import urlencode
40 from urllib2 import urlopen
41
42try:
43 if PY3:
44 import notify2 as pynotify
45 else:
46 import pynotify
47except ImportError as e:
48 pynotify = None
49
50
51__author__ = 'Simeon Simeonov'
52__version__ = '3.0'
53__license__ = 'GPL3'
54
55
56def display_notification(args, title, message):
57 """
58 A wrapper function for displaying a single notification
59
60 args: the argparse processed command-line arguments
61 title: notification title
62 message: notification message
63 """
64 if args.osd_sys == 'pynotify':
65 if not pynotify:
66 raise Exception(
67 'Could not load "pynotify".\n'
68 'Please install "pynotify" or use a different osd-system!\n'
69 'Terminating...\n')
70 if not pynotify.init('BEINC Notify'):
71 raise Exception('There was a problem with libnotify\n')
72 notification_obj = pynotify.Notification(summary=title,
73 message=message)
74 if PY3:
75 notification_obj.timeout = 1000 * args.osd_timeout
76 notification_obj.set_category('im.received')
77 else:
78 notification_obj.set_timeout(1000 * args.osd_timeout)
79 notification_obj.set_property(
80 'app_name',
81 '{0} {1}'.format(sys.argv[0], __version__))
82 notification_obj.show()
83 else:
84 raise Exception(
85 'Unsupported osd-system: {0}\n'.format(args.osd_sys))
86
87
88def poll_notifications(scheduler, args):
89 """
90 the core function initiated by the scheduler performing a single poll
91
92 scheduler: scheduler object
93 args: the argparse processed command-line arguments
94 """
95 try:
96 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
97 context.verify_mode = ssl.CERT_NONE
98 if args.cert:
99 context.verify_mode = ssl.CERT_REQUIRED
100 context.load_verify_locations(cafile=os.path.expanduser(args.cert))
101 context.check_hostname = bool(not args.disable_hostname_check)
102 if args.ciphers:
103 context.set_ciphers(args.ciphers)
104 response = urlopen(
105 args.url,
106 data=urlencode(
107 (
108 ('resource_name', args.rname),
109 ('password', args.password)
110 )).encode('utf-8'),
111 timeout=args.socket_timeout,
112 context=context)
113 response_dict = json.loads(response.read().decode('utf-8'))
114 if response.code != 200:
115 raise socket.error(response_dict.get('message', ''))
116 for entry in response_dict['data']['messages']:
117 display_notification(args,
118 entry.get('title', ''),
119 entry.get('message', ''))
120 response.close()
121 scheduler.enter(args.frequency,
122 1,
123 poll_notifications,
124 (scheduler, args))
125 except ssl.SSLError as e:
126 sys.stderr.write('BEINC SSL/TLS error: {0}\n'.format(e))
127 sys.exit(errno.EPERM)
128 except socket.error as e:
129 sys.stderr.write('BEINC connection error: {0}\n'.format(e))
130 sys.exit(errno.EPERM)
131 except Exception as e:
132 sys.stderr.write('BEINC generic client error: {0}\n'.format(e))
133 sys.exit(errno.EPERM)
134
135
136def main():
137 parser = argparse.ArgumentParser(
138 description='The following options are available')
139 parser.add_argument(
140 'url',
141 metavar='URL',
142 type=str,
143 help='BEINC server destination URL')
144 parser.add_argument(
145 '-c', '--cert-file',
146 metavar='FILE',
147 type=str,
148 dest='cert',
149 default='',
150 help='CA-cert to check the server-cert against '
151 '(default: Check disabled)')
152 parser.add_argument(
153 '--ciphers',
154 metavar='CIPHERS',
155 type=str,
156 dest='ciphers',
157 default='',
158 help='Preferred ciphers list (default: auto)')
159 parser.add_argument(
160 '--disable-hostname-check',
161 action='store_true',
162 dest='disable_hostname_check',
163 default=False,
164 help='Do not check whether server cert matches server hostname')
165 parser.add_argument(
166 '-f', '--frequency',
167 metavar='SECONDS',
168 type=int,
169 dest='frequency',
170 default=10,
171 help='Polling frequency in seconds (default: 10)')
172 parser.add_argument(
173 '-n', '--resource-name',
174 metavar='NAME',
175 type=str,
176 dest='rname',
177 required=True,
178 help='The name of the BEINC-resource on the remote server')
179 parser.add_argument(
180 '-o', '--osd-system',
181 metavar='SYSTEM',
182 type=str,
183 dest='osd_sys',
184 default='pynotify',
185 help='BEINC osd-system: "pynotify" (default)')
186 parser.add_argument(
187 '-p', '--password',
188 metavar='PASSWORD[FILE]',
189 type=str,
190 dest='password',
191 default='',
192 help='BEINC taget-password / text-file containing the target password'
193 ' (default & recommended: prompt for passwd)')
194 parser.add_argument(
195 '-T', '--socket-timeout',
196 metavar='SECONDS',
197 type=int,
198 dest='socket_timeout',
199 default=3,
200 help='Socket timeout in seconds (0=Python default) (default: 3)')
201 parser.add_argument(
202 '-t', '--osd-timeout',
203 metavar='SECONDS',
204 type=int,
205 dest='osd_timeout',
206 default=5,
207 help='OSD timeout (default: 5)')
208 parser.add_argument(
209 '-v', '--version',
210 action='version',
211 version='%(prog)s {0}'.format(__version__),
212 help='display program-version and exit')
213 args = parser.parse_args()
214 if not args.password:
215 try:
216 args.password = getpass.getpass()
217 except Exception as e:
218 sys.stderr.write('Prompt terminated\n')
219 sys.exit(errno.EACCES)
220 elif os.path.isfile(args.password):
221 try:
222 with open(args.password, 'r') as fp:
223 passwd = fp.readline()
224 if passwd.strip():
225 args.password = passwd.strip()
226 except Exception as e:
227 sys.stderr.write('Unable to open password file: {0}'.format(e))
228 sys.exit(1)
229 scheduler = sched.scheduler(time.time, time.sleep)
230 scheduler.enter(args.frequency,
231 1,
232 poll_notifications,
233 (scheduler, args))
234 scheduler.run()
235
236
237if __name__ == '__main__':
238 main()