diff options
| author | Simeon Simeonov | 2012-11-13 15:40:27 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2012-11-13 15:40:27 +0100 |
| commit | 5c2becae337ea7e2bcebec2912e4626d853b16aa (patch) | |
| tree | 8176d6f36f1c9d758670bdd0c0752167af775ba7 | |
| parent | b1b484a0b93a5651da7d1ca20e77f70089b215bb (diff) | |
All working with sqlite
| -rw-r--r-- | bosd.py | 92 | ||||
| -rwxr-xr-x | socket_server.py | 30 | ||||
| -rwxr-xr-x | web_server.py | 62 | ||||
| -rwxr-xr-x | weechat_notify.py | 48 |
4 files changed, 221 insertions, 11 deletions
| @@ -0,0 +1,92 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import socket | ||
| 5 | import weechat | ||
| 6 | |||
| 7 | #### CONFIG #### | ||
| 8 | enabled = True | ||
| 9 | |||
| 10 | notify_channels = [ | ||
| 11 | "RedpillLinpro.#python", | ||
| 12 | "Exile.#hin", | ||
| 13 | "RedpillLinpro.#adult", | ||
| 14 | "Exile.#pichove" | ||
| 15 | ] | ||
| 16 | |||
| 17 | connections = [ {'sbosdd_ip': '127.0.0.1', | ||
| 18 | 'sbosdd_port': 9999}, | ||
| 19 | # {'sbosdd_ip': "10.0.0.2", | ||
| 20 | # 'sbosdd_port': 9999} | ||
| 21 | ] | ||
| 22 | ################ | ||
| 23 | |||
| 24 | def bosd_send_xosd_message(message): | ||
| 25 | for conn in connections: | ||
| 26 | try: | ||
| 27 | #weechat.prnt("", message) | ||
| 28 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| 29 | s.connect((conn['sbosdd_ip'], conn['sbosdd_port'])) | ||
| 30 | s.send(message) | ||
| 31 | s.close() | ||
| 32 | except: | ||
| 33 | continue | ||
| 34 | |||
| 35 | return True | ||
| 36 | |||
| 37 | |||
| 38 | def bosd_command(data, buffer, args): | ||
| 39 | global enabled | ||
| 40 | if args == 'on': | ||
| 41 | enabled = True | ||
| 42 | weechat.prnt(weechat.current_buffer(), "bosd on") | ||
| 43 | elif args == 'off': | ||
| 44 | enabled = False | ||
| 45 | weechat.prnt(weechat.current_buffer(), "bosd off") | ||
| 46 | else: | ||
| 47 | bosd_send_xosd_message(args) | ||
| 48 | |||
| 49 | return weechat.WEECHAT_RC_OK | ||
| 50 | |||
| 51 | |||
| 52 | def bosd_privmsg_handler(data, signal, signal_data): | ||
| 53 | if not enabled: | ||
| 54 | return weechat.WEECHAT_RC_OK | ||
| 55 | |||
| 56 | prvmsg_dict = weechat.info_get_hashtable("irc_message_parse", | ||
| 57 | { "message": signal_data }) | ||
| 58 | |||
| 59 | |||
| 60 | server = signal.split(",")[0] | ||
| 61 | |||
| 62 | # channel = signal_data.split(":")[-1] | ||
| 63 | # my_nick = weechat.info_get("irc_nick_from_host", signal_data) | ||
| 64 | |||
| 65 | my_nick = weechat.info_get("irc_nick", server) | ||
| 66 | channel = prvmsg_dict['arguments'].split(":")[0].strip() | ||
| 67 | nick = prvmsg_dict['nick'] | ||
| 68 | message = ':'.join(prvmsg_dict['arguments'].split(':')[1:]).strip() | ||
| 69 | |||
| 70 | if (my_nick in message) or ('{0}.{1}'.format(server,channel) in notify_channels): | ||
| 71 | template_msg = '{0} @ {1} - {2}: {3}'.format(channel, | ||
| 72 | server, | ||
| 73 | nick, | ||
| 74 | message) | ||
| 75 | bosd_send_xosd_message(template_msg) | ||
| 76 | |||
| 77 | if my_nick in channel: | ||
| 78 | template_msg = '{0} @ {1}: {2}'.format(nick, server, message) | ||
| 79 | bosd_send_xosd_message(template_msg) | ||
| 80 | |||
| 81 | #my_str = "%s - %s - %s - %s" % (my_nick, server, channel, message) | ||
| 82 | #weechat.prnt("", my_str) | ||
| 83 | |||
| 84 | return weechat.WEECHAT_RC_OK | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | weechat.register('bosd', 'Simeon Simeonov', '1.0', 'GPL3', 'Socket notification script', "", "") | ||
| 89 | weechat.hook_command("bosd", "bosd on off toggle", "<on | off>", "description...", "None", "bosd_command", "") | ||
| 90 | weechat.hook_signal("*,irc_in2_privmsg", "bosd_privmsg_handler", "") | ||
| 91 | |||
| 92 | weechat.prnt("", "bosd initiated!") | ||
diff --git a/socket_server.py b/socket_server.py index 7297d24..f683712 100755 --- a/socket_server.py +++ b/socket_server.py | |||
| @@ -2,18 +2,18 @@ | |||
| 2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
| 3 | 3 | ||
| 4 | import SocketServer | 4 | import SocketServer |
| 5 | import os | ||
| 5 | import sys | 6 | import sys |
| 6 | 7 | ||
| 7 | from sqlalchemy import create_engine | 8 | from sqlalchemy import create_engine |
| 8 | from sqlalchemy.orm import sessionmaker | 9 | from sqlalchemy.orm import sessionmaker |
| 9 | 10 | ||
| 10 | from bpynotify_orm import NotificationEntry | 11 | from bpynotify_orm import NotificationEntry, notification_entry_table |
| 11 | 12 | ||
| 12 | 13 | ||
| 13 | class CustomSocketServer(SocketServer.TCPServer): | 14 | class CustomSocketServer(SocketServer.TCPServer): |
| 14 | 15 | ||
| 15 | def __init__(self, server_address, RequestHandlerClass, session): | 16 | def __init__(self, server_address, RequestHandlerClass, session): |
| 16 | print "Done" | ||
| 17 | SocketServer.TCPServer.__init__(self, | 17 | SocketServer.TCPServer.__init__(self, |
| 18 | server_address, | 18 | server_address, |
| 19 | RequestHandlerClass) | 19 | RequestHandlerClass) |
| @@ -30,14 +30,10 @@ class MyTCPHandler(SocketServer.StreamRequestHandler): | |||
| 30 | """ | 30 | """ |
| 31 | 31 | ||
| 32 | def handle(self): | 32 | def handle(self): |
| 33 | # self.request is the TCP socket connected to the client | ||
| 34 | ##self.data = self.request.recv(1024).strip() | ||
| 35 | ##print "{} wrote:".format(self.client_address[0]) | ||
| 36 | ##print self.data | ||
| 37 | 33 | ||
| 38 | self.data = self.rfile.readline().strip() | 34 | self.data = self.rfile.readline().decode('utf8').strip() |
| 39 | 35 | #print(self.data) | |
| 40 | entry = NotificationEntry(unicode(self.data)) | 36 | entry = NotificationEntry(self.data) |
| 41 | self.server.session.add(entry) | 37 | self.server.session.add(entry) |
| 42 | self.server.session.commit() | 38 | self.server.session.commit() |
| 43 | 39 | ||
| @@ -46,14 +42,26 @@ def main(): | |||
| 46 | 42 | ||
| 47 | HOST, PORT = "localhost", 9999 | 43 | HOST, PORT = "localhost", 9999 |
| 48 | 44 | ||
| 45 | if len(sys.argv) == 2 and sys.argv[1] == '-d': | ||
| 46 | try: | ||
| 47 | pid = os.fork() | ||
| 48 | if pid > 0: | ||
| 49 | sys.exit(0) | ||
| 50 | except Exception as e: | ||
| 51 | sys.stderr.write("Unable to daemonize. Fork error: {0}".format(e)) | ||
| 52 | sys.exit(1) | ||
| 53 | |||
| 49 | try: | 54 | try: |
| 55 | |||
| 50 | engine = create_engine('sqlite:///notifications.db') | 56 | engine = create_engine('sqlite:///notifications.db') |
| 51 | Session = sessionmaker(bind=engine) # bound session | 57 | Session = sessionmaker(bind=engine) # bound session |
| 52 | session = Session() | 58 | session = Session() |
| 53 | 59 | ||
| 54 | server = CustomSocketServer((HOST, PORT), MyTCPHandler, session) | 60 | server = CustomSocketServer((HOST, PORT), MyTCPHandler, session) |
| 55 | # Activate the server; this will keep running until you | 61 | |
| 56 | # interrupt the program with Ctrl-C | 62 | # clean all previous entries # |
| 63 | notification_entry_table.delete(bind=engine).execute() | ||
| 64 | |||
| 57 | server.serve_forever() | 65 | server.serve_forever() |
| 58 | 66 | ||
| 59 | except Exception as e: | 67 | except Exception as e: |
diff --git a/web_server.py b/web_server.py new file mode 100755 index 0000000..a8ccc7c --- /dev/null +++ b/web_server.py | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import json | ||
| 5 | import sys | ||
| 6 | |||
| 7 | import cherrypy | ||
| 8 | |||
| 9 | from sqlalchemy import create_engine | ||
| 10 | from sqlalchemy.orm import sessionmaker | ||
| 11 | |||
| 12 | from bpynotify_orm import NotificationEntry, notification_entry_table | ||
| 13 | |||
| 14 | |||
| 15 | class WebNotifyServer(object): | ||
| 16 | |||
| 17 | def __init__(self, session): | ||
| 18 | self.session = session | ||
| 19 | |||
| 20 | @cherrypy.expose | ||
| 21 | def notifications(self): | ||
| 22 | query = self.session.query(NotificationEntry)\ | ||
| 23 | .filter(NotificationEntry.viewed==False)\ | ||
| 24 | .order_by(NotificationEntry.id) | ||
| 25 | entries = query.all() | ||
| 26 | entry_list = list() | ||
| 27 | for entry in entries: | ||
| 28 | #print(entry.message) | ||
| 29 | entry_list.append(entry.message) | ||
| 30 | entry.viewed = True | ||
| 31 | self.session.commit() | ||
| 32 | print (json.dumps(entry_list, sort_keys=True, indent=4)) | ||
| 33 | return json.dumps(entry_list, sort_keys=True, indent=4) | ||
| 34 | |||
| 35 | |||
| 36 | def main(): | ||
| 37 | |||
| 38 | cherrypy.config.update({ | ||
| 39 | 'server.socket_host': '10.0.0.4', | ||
| 40 | 'server.socket_port': 40004, | ||
| 41 | |||
| 42 | 'server.ssl_module': 'pyopenssl', | ||
| 43 | 'server.ssl_certificate': '/home/blackmore/gitprogs/bpynotify/cert.crt', | ||
| 44 | 'server.ssl_private_key': '/home/blackmore/gitprogs/bpynotify/key.pem', | ||
| 45 | }) | ||
| 46 | |||
| 47 | try: | ||
| 48 | engine = create_engine('sqlite:///notifications.db') | ||
| 49 | Session = sessionmaker(bind=engine) # bound session | ||
| 50 | session = Session() | ||
| 51 | |||
| 52 | cherrypy.quickstart(WebNotifyServer(session)) | ||
| 53 | |||
| 54 | except Exception as e: | ||
| 55 | sys.stderr.write("WebServer error: {0}".format(e)) | ||
| 56 | sys.exit(1) | ||
| 57 | |||
| 58 | sys.exit(0) | ||
| 59 | |||
| 60 | |||
| 61 | if __name__ == "__main__": | ||
| 62 | main() | ||
diff --git a/weechat_notify.py b/weechat_notify.py new file mode 100755 index 0000000..f070551 --- /dev/null +++ b/weechat_notify.py | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import json | ||
| 5 | import urllib2 | ||
| 6 | import sched | ||
| 7 | import sys | ||
| 8 | import time | ||
| 9 | |||
| 10 | import pynotify | ||
| 11 | |||
| 12 | POLLING_FREQUENCY = 10 | ||
| 13 | NOTIFICATION_URL = 'https://simeon.simeonov.no:40004/notifications/' | ||
| 14 | TIMEOUT_SEC = 3 | ||
| 15 | |||
| 16 | def poll_notifications(scheduler): | ||
| 17 | """ | ||
| 18 | """ | ||
| 19 | try: | ||
| 20 | req = urllib2.Request(NOTIFICATION_URL) | ||
| 21 | response = urllib2.urlopen(req) | ||
| 22 | response_str = response.read() | ||
| 23 | |||
| 24 | entries = json.loads(response_str) | ||
| 25 | for entry in entries: | ||
| 26 | n = pynotify.Notification('IRC', entry, 'dialog-information') | ||
| 27 | n.set_timeout(TIMEOUT_SEC) | ||
| 28 | n.show() | ||
| 29 | |||
| 30 | except Exception as e: | ||
| 31 | sys.stderr.write("Client error: {0}".format(e)) | ||
| 32 | sys.exit(1) | ||
| 33 | |||
| 34 | scheduler.enter(POLLING_FREQUENCY, 1, poll_notifications, (scheduler,)) | ||
| 35 | |||
| 36 | |||
| 37 | def main(): | ||
| 38 | |||
| 39 | if not pynotify.init("Weechat Notify"): | ||
| 40 | sys.stderr.write("There was a problem with libnotify") | ||
| 41 | sys.exit(1) | ||
| 42 | |||
| 43 | sc = sched.scheduler(time.time, time.sleep) | ||
| 44 | sc.enter(POLLING_FREQUENCY, 1, poll_notifications, (sc,)) | ||
| 45 | sc.run() | ||
| 46 | |||
| 47 | if __name__ == '__main__': | ||
| 48 | main() | ||
