-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetnotify.py
More file actions
executable file
·118 lines (97 loc) · 3.79 KB
/
netnotify.py
File metadata and controls
executable file
·118 lines (97 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import logging
import socket
import sys
import daemonocle
import notify2
from user_config import Config, Section, StringOption, IntegerOption
class ServerConfig(Config):
"""netnotify server configuration."""
application = "netnotify"
author = "nihlaeth"
class ServerSection(Section):
listen = StringOption(doc='IP to listen on', default='0.0.0.0')
port = IntegerOption(doc='port to listen on', default=5757)
pid_file = StringOption(
doc='path to PID file', default='/tmp/netnotify.pid')
server = ServerSection()
class LogSection(Section):
log_file = StringOption(
doc='file to write logs to', default='/var/log/netnotify.log')
verbosity = StringOption(
doc='one of critical, error, warning, info, debug or notset',
default='warning')
logging = LogSection()
def notification_daemon():
config = ServerConfig(file_name="server", cli=False)
logging.basicConfig(
filename=config.logging.log_file,
level=getattr(logging, config.logging.verbosity.upper()),
format='%(asctime)s [%(levelname)s] %(message)s',
)
logging.info("server starting")
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
logging.critical("Failed to create socket: {}".format(msg))
sys.exit()
logging.debug("Created socket")
port = 5757
server_socket.bind((config.server.listen, config.server.port))
logging.debug("Now listening on port".format(config.server.port))
server_socket.listen(10)
notify2.init("Python notification Daemon")
while True:
client, address = server_socket.accept()
data = client.recv(4096)
logging.info("Got connection from {}: {}".format(address, data))
datalist = data.split(":", 1)
title = datalist[0]
try:
body = datalist[1]
except IndexError:
print "No notification body received, using title"
body = title
notification = notify2.Notification(title, body)
notification.show()
client.close()
def usage():
print("netnotify daemon")
print("Daemon actions: {}".format(daemonocle.Daemon.list_actions()))
print("Generate configuration file: --generate-config")
def daemon_controller():
if len(sys.argv) < 2:
usage()
elif sys.argv[1] in daemonocle.Daemon.list_actions():
config = ServerConfig(file_name="server", cli=False)
daemon = daemonocle.Daemon(
detach=False,
worker=notification_daemon,
pidfile=config.server.pid_file)
daemon.do_action(sys.argv[1])
elif sys.argv[1] == "--generate-config":
config = ServerConfig(file_name="server")
else:
usage()
class ClientConfig(Config):
"""netnotify client configuration."""
application = "netnotify"
author = "nihlaeth"
class ServerSection(Section):
address = StringOption(doc='server address', default='127.0.0.1')
port = IntegerOption(doc='port server is listening on', default=5757)
server = ServerSection()
class NotificationSection(Section):
"""Notification message data. Fill out this section for default values."""
title = StringOption(
doc='notification title', required=True)
message = StringOption(
doc='notification message', required=True)
notification = NotificationSection()
def send():
config = ClientConfig(file_name="client")
notification_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
notification_socket.connect((config.server.address, config.server.port))
notification_socket.send("{}:{}".format(
config.notification.title,
config.notification.message))
notification_socket.close()