-
Notifications
You must be signed in to change notification settings - Fork 22
/
irc.py
843 lines (692 loc) · 27.3 KB
/
irc.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# http://inamidst.com/saxo/
# Created by Sean B. Palmer
import atexit
import collections
import configparser
import imp
import importlib
import os.path
import queue
import re
import signal
import socket
import subprocess
import sys
import threading
import time
# Save PEP 3122!
if "." in __name__:
from . import common
from . import scheduler
from . import sqlite
from .saxo import path as saxo_path
from .saxo import version as saxo_version
else:
import common
import scheduler
import sqlite
from saxo import path as saxo_path
from saxo import version as saxo_version
lock = threading.Lock()
def debug(*args, **kargs):
with lock:
try:
print(*args, **kargs)
sys.stdout.flush()
except BrokenPipeError:
sys.exit()
def exit(code):
# TODO: Sock removal code
try: sys.exit(code)
finally: os._exit(code)
# TODO: Sometimes sys.exit doesn't work, not sure why
# List of threads:
# client.receive
# client.send
# every plugin function
# every process command, as a communication wrapper
# scheduler
# serve.listen
# every serve.connection instance
incoming = queue.Queue()
outgoing = queue.Queue()
regex_optional_prefix = re.compile(r"(?::([^! ]*)!?([^@ ]*)@?([^ ]*))?")
regex_parameter = re.compile(r"((?:(?<= :)[^\r\n]*)|(?:[^: \r\n][^ \r\n]*))")
def parse(octets):
text = octets.decode("utf-8", "replace")
match_prefix = regex_optional_prefix.match(text)
params = regex_parameter.findall(text[match_prefix.end():])
return match_prefix.groups(), params[0], params[1:]
class Message(object):
def __init__(self, saxo, octets):
prefix, command, parameters = parse(octets)
self.base = saxo.base[:]
self.config = saxo.config_cache.copy()
self.nick = prefix[0]
self.user = prefix[1]
self.host = prefix[2]
if self.nick and self.user and self.host:
self.prefix = self.nick + "!" + self.user + "@" + self.host
self.command = command
self.parameters = parameters
self.blocked = False
def send(*args):
saxo.send(*args)
self.send = send
def msg(*args):
saxo.send("PRIVMSG", *args)
self.msg = msg
if self.command == "PRIVMSG":
self.sender = self.parameters[0]
self.text = self.parameters[1]
self.private = self.sender == self.config["nick"]
if self.private:
self.sender = self.nick
# if saxo.address:
# # TODO: Why was this 498 for duxlot?
# self.limit = 493 - len(self.sender + saxo.address)
def say(text):
saxo.send("PRIVMSG", self.sender, text)
self.say = say
def reply(text):
saxo.send("PRIVMSG", self.sender, self.nick + ": " + text)
self.reply = reply
private = self.config.get("private")
if self.private:
if private == "deny":
self.blocked = True
elif private == "only":
self.blocked = True
self.cmd = None
cmdpfx = self.config["prefix"]
if self.text.startswith(cmdpfx):
text = self.text[len(cmdpfx):]
if " " in text:
cmd, arg = text.split(" ", 1)
else:
cmd, arg = text, ""
self.cmd = cmd
self.arg = arg
def authorised(self):
import re
config_owner = self.config.get("owner", "")
test_identity = False
if not "!" in config_owner:
test_identity = True
config_owner = config_owner + "!*@*"
mask = lambda g: "^" + re.escape(g).replace("\\*", ".*") + "$"
matches = re.match(mask(config_owner), self.prefix) is not None
if test_identity:
return matches and self.identified
return matches
def client(self, *args):
incoming.put(args)
# threaded
def socket_receive(sock):
def receive_loop(sock, incoming):
with sock.makefile("rb") as s:
# TODO: How do we know when we're connected?
incoming.put(("receiving",))
for octets in s:
incoming.put(("remote", octets))
try: receive_loop(sock, incoming)
except Exception as err:
# Usually IOError, EOFError, socket.error, or ssl.SSLError
debug(str(err))
incoming.put(("disco_receiving",))
def flood_protection(recent):
recent.append(time.monotonic())
if len(recent) > 3:
recent.popleft()
if (recent[2] - recent[0]) < 2:
# debug("sleeping 1")
time.sleep(1)
elif (recent[2] - recent[1]) < 1:
# debug("sleeping 0.5")
time.sleep(0.5)
# else:
# debug("sleeping 0")
else:
# debug("sleeping 0.25")
time.sleep(0.25)
# threaded
def socket_send(sock, flood=False):
def sending(sock, flood=False):
recent = collections.deque()
with sock.makefile("wb") as s:
incoming.put(("sending",))
while True:
octets = outgoing.get()
if octets is None:
debug("Sending Thread: Requested quit")
return True
debug("->", repr(octets.decode("utf-8", "replace")))
s.write(octets)
s.flush()
if not flood:
flood_protection(recent)
# TODO: Surely this is never reached?
debug("Sending Thread: No more data")
return False
try: sending(sock, flood)
except Exception as err:
# Usually BrokenPipeError
debug("Sending Thread: Error:", err)
incoming.put(("disco_sending",))
class SaxoConnectionError(Exception):
...
regex_link = re.compile(r"(http[s]?://[^<> \"\x01]+)[,.]?")
def command_path(base, cmd):
if ("\x00" in cmd) or (os.sep in cmd) or ("." in cmd):
return None
path = os.path.join(base, "commands", cmd)
if (not os.path.isfile(path)) or (not os.path.getsize(path)):
return None
return path
def utf8(obj):
return str(obj).encode("utf-8", "replace")
def utf8dict(data):
return {utf8(key): utf8(value) for key, value in data.items()}
def process(env, cmd, path, arg):
path = utf8(path)
env = utf8dict(env)
octets = utf8(arg)
try: proc = subprocess.Popen([path, octets], env=env,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
except PermissionError:
outs = "The command file does not have executable permissions"
except FileNotFoundError:
# Might have been removed just after running this thread
return
else:
authorised = "SAXO_AUTHORISED" in env
private = not env.get("SAXO_SENDER", "#").startswith("#")
timeout = 36 if (authorised and private) else 12
try: outs, errs = proc.communicate(octets + b"\n", timeout=timeout)
except subprocess.TimeoutExpired:
proc.kill()
# TODO: Use actual prefix
outs = "Sorry, %s took too long" % cmd
else:
outs = outs.decode("utf-8", "replace")
if "\n" in outs:
outs = outs.splitlines()[0]
code = proc.returncode or 0
# Otherwise: TypeError: unorderable types: NoneType() > int()
if (code > 0) and (not outs):
# TODO: Use actual prefix
outs = "Sorry, %s responded with an error" % cmd
return outs
class Saxo(object):
def __init__(self, base, opt):
self.base = base
self.opt = opt
self.events = {}
self.address = None
self.limit = None
self.discotimer = None
self.receiving = False
self.sending = False
self.receiving_thread = None
self.sending_thread = None
self.reconnecting = False
self.links = {}
self.environment_cache = os.environ.copy()
self.environment_cache["PYTHONPATH"] = saxo_path
# TODO: This needs to be changed when setting nick
self.environment_cache["SAXO_BOT"] = opt["client"]["nick"]
self.environment_cache["SAXO_BASE"] = base
self.environment_cache["SAXO_COMMANDS"] = \
os.path.join(base, "commands")
self.environment_cache["SAXO_VERSION"] = saxo_version
self.config_cache = {}
client_options = {
"channels", # Channels to join on startup
"nick", # Nickname of the bot, variable
"owner", # Full address of the owner
"prefix", # Command prefix
"flood", # Whether or not to flood
"private" # Whether to respond in private
}
for option in opt["client"]:
if option in client_options:
self.config_cache[option] = opt["client"].get(option)
else:
debug("Unknown option: %s" % option)
for section in opt:
if section == "client":
continue
self.config_cache[section] = dict(opt[section])
def run(self):
self.load()
self.connect()
self.handle()
def load(self):
# Update symlinks
common.populate(saxo_path, self.base)
# Load events
first = not self.events
self.events.clear()
def module_exists(name):
try: imp.find_module(name)
except ImportError:
return False
else: return True
if first and module_exists("plugins"):
debug("Warning: a 'plugins' module already exists")
if first and ("plugins" in sys.modules):
raise ImportError("'plugins' duplicated")
# This means we're using plugins as a namespace module
# Might have to move saxo.path's plugins/ to something else
# Otherwise it gets unionised into the namespace module
# if self.base not in sys.path:
# - not needed, because we clear up below
sys.path[:0] = [self.base]
plugins = os.path.join(self.base, "plugins")
plugins_package = importlib.__import__("plugins")
if next(iter(plugins_package.__path__)) != plugins:
# This is very unlikely to happen, because we pushed self.base
# to the front of sys.path, but perhaps some site configuration
# or other import mechanism may affect this
raise ImportError("non-saxo 'plugins' module")
setups = {}
for name in os.listdir(plugins):
if ("_" in name) or (not name.endswith(".py")):
continue
name = "plugins." + name[:-3]
if not name in sys.modules:
try: module = importlib.import_module(name)
except Exception as err:
debug("Error loading %s:" % name, err)
elif first:
raise ImportError("%r duplicated" % name)
else:
module = sys.modules[name]
try: module = imp.reload(module)
except Exception as err:
debug("Error reloading %s:" % name, err)
for attr in dir(module):
obj = getattr(module, attr)
if hasattr(obj, "saxo_event"):
try: self.events[obj.saxo_event].append(obj)
except KeyError:
self.events[obj.saxo_event] = [obj]
elif hasattr(obj, "saxo_setup"):
obj.saxo_name = module.__name__ + "." + obj.__name__
setups[obj.saxo_name] = obj
# debug("Loaded module:", name)
debug("%s setup functions" % len(setups))
graph = {}
for setup in setups.values():
deps = ["plugins." + dep for dep in setup.saxo_deps]
graph[setup.saxo_name] = deps
database_filename = os.path.join(self.base, "database.sqlite3")
with sqlite.Database(database_filename) as self.db:
for name in common.tsort(graph):
debug(name)
if name in setups:
setups[name](self)
else:
debug("Warning: Missing dependency:", name)
sys.path[:1] = []
def connect(self):
try: self.connect_sock()
except Exception as err:
raise SaxoConnectionError(str(err))
self.first = True
# TODO: Reset other state? e.g. self.address
receiving = (socket_receive, self.sock)
self.receiving_thread = common.thread(*receiving)
sending = (socket_send, self.sock, "flood" in self.opt["client"])
self.sending_thread = common.thread(*sending)
def connect_sock(self):
host = self.opt["server"]["host"]
port = int(self.opt["server"]["port"])
debug("Connecting to %s:%s" % (host, port))
# self.sock.connect((host, port))
self.sock = socket.create_connection((host, port))
# self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if "ssl" in self.opt["server"]:
import ssl
debug("Warning: Using SSL, but not validating the cert!")
self.sock = ssl.wrap_socket(
self.sock,
server_side=False,
cert_reqs=ssl.CERT_NONE) # TODO: or CERT_REQUIRED
def disconnect(self):
# NOTE: *Do not* close the sending thread gracefully
# The socket shutdown code below will be invoked before the queue
# item reaches the socket thread. That means the *next* thread will
# pick it up, because it'll quit with a pipe error anyway
# Close the socket forcefully
try: self.sock.shutdown(socket.SHUT_RDWR)
except: ...
try: self.sock.close()
except: ...
self.cancel_discotimer()
def socket_threads_active(self):
receiving = self.receiving_thread.is_alive()
sending = self.sending_thread.is_alive()
debug("RECV, SEND:", receiving, sending)
return receiving or sending
def cancel_discotimer(self):
if self.discotimer is not None:
try:
was_finished = self.discotimer.finished.is_set()
# Note: .cancel() still works on an expired timer!
# It just calls .finished.set()
self.discotimer.cancel()
if not was_finished:
debug("Cancelled probably unfinished disco timer")
self.discotimer = None
except:
...
def handle(self):
while True:
instruction_args = incoming.get()
instruction = instruction_args[0]
args = tuple(instruction_args[1:])
if instruction not in {"instances", "remote"}:
debug("handle:", instruction, args)
if not isinstance(instruction, str):
continue
method_name = "instruction_" + instruction
if hasattr(self, method_name):
method = getattr(self, method_name)
try: method(*args)
except Exception as err:
debug("handle error:", err)
# raise err # - for debug
else:
debug("Unknown instruction:", instruction)
def instruction_address(self, address):
self.address = address
self.limit = 512 - len(": \r\n") - len(address)
def instruction_connect(self, delay=5):
# Warning: This quits the bot if the bot is already connected!
# Use instruction_reconnect if you want to restart the bot
# NOTE: The instruction mechanism is synchronous
# This means we can't use self.receiving etc. to check connectivity
# We rely on self.disconnect() to have done the right thing
# The following is a check to make sure that it has
if self.socket_threads_active():
# Wait up to six seconds for the threads to quit
for attempt in range(12):
time.sleep(0.5)
if not self.socket_threads_active():
break
else:
# TODO: If the threads are still active, they should be killed
# Unfortunately, threads in python can't be killed
debug("ERROR! Unable to stop the socket threads")
exit(1)
if "flood" not in self.opt["client"]:
time.sleep(3)
try: self.connect()
except SaxoConnectionError as err:
# Retry, with 1sec more delay, up to a maximum of 30sec delay
def connect():
incoming.put(("connect", min(30, delay + 1)))
t = threading.Timer(delay, connect)
t.start()
def instruction_connected(self):
if ":connected" in self.events:
msg = Message(self, b"NOOP")
for function in self.events[":connected"]:
function(msg)
def instruction_command(self, prefix, sender, cmd, arg):
self.command(prefix, sender, cmd, arg)
def instruction_disco_receiving(self):
self.receiving = False
debug("Sending disconnected event to scheduler")
scheduler.incoming.put(("disconnected", ()))
if self.sending:
outgoing.put(None)
else:
incoming.put(("connect",))
def instruction_disco_sending(self):
self.sending = False
if self.receiving:
self.disconnect()
else:
incoming.put(("connect",))
def instruction_instances(self):
our_pid = os.getpid()
database_filename = os.path.join(self.base, "database.sqlite3")
with sqlite.Database(database_filename) as db:
pids = [row[0] for row in db["saxo_instances"]]
if len(pids) == 1:
if pids[0] == our_pid:
return
debug("Found another saxo instance! %s" % pids)
self.send("QUIT", "Another saxo instance was detected")
self.disconnect()
exit(0)
def instruction_join(self, channel):
# NOTE: .visit can still be followed by .join
# The JOIN will just fail on the server
channels = self.opt["client"]["channels"].split(" ")
if channel in channels:
# TODO: .visit it back?
return
channels.append(channel)
channels = " ".join(channels)
self.update_config("client", "channels", channels)
self.send("JOIN", channel)
def instruction_link(self, channel, link):
self.links[channel] = link
def instruction_message(self, text):
debug("IPC message:", text)
def instruction_msg(self, destination, text):
self.send("PRIVMSG", destination, text)
def instruction_noop(self):
...
def instruction_part(self, channel):
# NOTE: .leave can still be followed by .part
# The PART will just fail on the server
channels = self.opt["client"]["channels"].split(" ")
if channel not in channels:
# TODO: .leave it anyway?
return
channels.remove(channel)
channels = " ".join(channels)
self.update_config("client", "channels", channels)
self.send("PART", channel)
def instruction_periodic(self, name, period, cmd, arg, sender=None):
database_filename = os.path.join(self.base, "database.sqlite3")
with sqlite.Database(database_filename) as db:
p = (name, period, int(time.time()), b"scheduled",
common.b64pickle((cmd, arg, sender)))
# TODO: This fails silently if there's a type error?
db["saxo_periodic"].replace(p)
def instruction_ping(self):
now = time.time()
# Don't ping if connected less than a minute ago
if self.receiving > (now - 60):
debug("Won't start discotimer, because we recently reconnected")
return
self.send("PING", self.opt["client"]["nick"])
def reconnect():
incoming.put(("reconnect",))
# Make sure the timer is shorter than the pingloop!
# Default pingloop period is 180
self.discotimer = threading.Timer(30, reconnect)
self.discotimer.start()
def instruction_prefix(self, pfx):
self.update_config("client", "prefix", pfx)
def instruction_quit(self):
# Never call this from a thread, otherwise this can give an OSError
# TODO: Get the sender to pick this up and disconnect from there?
# Could be a problem if the sender has broken
self.send("QUIT")
self.disconnect()
exit(0)
def instruction_receiving(self):
self.receiving = time.time()
scheduler.incoming.put(("connected", ()))
def start_scheduler():
scheduler.incoming.put(("start", ()))
start = threading.Timer(3, start_scheduler)
start.start()
# TODO: Check that we really are connected
# TODO: Unit test for :connected event
incoming.put(("connected",))
def instruction_reconnect(self):
# disco_* will automatically reconnect
if self.receiving or self.receiving_thread.is_alive():
# Close the socket, which forces the receiving thread to quit
self.disconnect()
elif self.sending or self.sending_thread.is_alive():
# Signal the sending thread to quit
outgoing.put(None)
else:
debug("Reconnect requested during reconnect")
def instruction_reload(self, destination=None):
before = time.time()
self.load()
elapsed = time.time() - before
if destination:
self.send("PRIVMSG", destination,
"Reloaded in %s seconds" % round(elapsed, 3))
def instruction_remote(self, octets):
debug(repr(octets))
msg = Message(self, octets)
if msg.command == "PRIVMSG":
if msg.blocked:
return
if msg.cmd is not None:
self.command(msg)
elif msg.command == "PONG":
self.cancel_discotimer()
def run(name, msg):
if name in self.events:
for function in self.events[name]:
try: function(msg)
except Exception as err:
debug("Error:", function.__name__ + ":", err)
if self.first is True:
run(":1st", msg)
self.first = False
run(msg.command, msg)
run("*", msg)
def instruction_schedule(self, unixtime, command, args):
command = command.encode("ascii")
args = common.b64pickle(args)
# TODO: Why not just add it to the database ourselves?
scheduler.incoming.put(("schedule.add", (unixtime, command, args)))
def instruction_scheduled(self, cmd, arg, sender=None):
self.scheduled_command(cmd, arg, sender=sender)
def instruction_send(self, *args):
self.send(*args)
def instruction_sending(self):
self.sending = True
def command(self, msg):
cmd, arg = msg.cmd, msg.arg
path = command_path(self.base, cmd)
if path is None:
return
def command_process(env, cmd, path, arg):
outs = process(env, cmd, path, arg)
if outs:
self.send("PRIVMSG", msg.sender, outs)
env = self.environment_cache.copy()
env["SAXO_NICK"] = msg.nick
env["SAXO_SENDER"] = msg.sender
if msg.sender in self.links:
env["SAXO_URL"] = self.links[msg.sender]
if msg.authorised():
env["SAXO_AUTHORISED"] = "1"
common.thread(command_process, env, cmd, path, arg)
def scheduled_command(self, cmd, arg, sender=None):
path = command_path(self.base, cmd)
if path is None:
return
def command_process(env, cmd, path, arg):
outs = process(env, cmd, path, arg)
if outs and (sender is not None):
self.send("PRIVMSG", sender, outs)
env = self.environment_cache.copy()
env["SAXO_SCHEDULED"] = "1"
common.thread(command_process, env, cmd, path, arg)
def update_config(self, section, option, value):
self.opt[section][option] = value
config = os.path.join(self.base, "config")
with open(config, "w", encoding="utf-8") as f:
self.opt.write(f)
if section == "client":
self.config_cache[option] = value
else:
self.config_cache[section][option] = value
# TODO: Change the database
def send(self, *args):
# TODO: Loop detection
if len(args) > 1:
args = args[:-1] + (":" + args[-1],)
text = re.sub(r"[\r\n]", "", " ".join(args))
if len(args) == 3:
if (args[0] == "PRIVMSG") and args[1].startswith("#"):
search = regex_link.search(args[2])
if search:
incoming.put(("link", args[1], search.group(1)))
# Set a very conservative limit if we don't know the real limit
limit = self.limit or 360
while len(text.encode("utf-8", "replace")) > limit:
text = text[:-1]
octets = text.encode("utf-8", "replace")
outgoing.put(octets + b"\r\n")
def serve(sockname, incoming):
if os.path.exists(sockname):
os.remove(sockname)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(sockname)
sock.listen(1)
def listen(sock):
while True:
connection, client = sock.accept()
def handle(connection, client):
try:
for octets in connection.makefile("rb"):
try:
text = octets.decode("ascii", "replace")
text = text.strip("\n")
if " " in text:
instruction, data = text.split(" ", 1)
args = common.b64unpickle(data)
else:
instruction, args = text, tuple()
incoming.put((instruction,) + args)
except Exception as err:
debug("ERROR!", err.__class__.__name__, err)
finally:
connection.close()
common.thread(handle, connection, client)
common.thread(listen, sock)
E_NO_CONFIG = """
Are you sure this is a saxo configuration directory? If you need to make a new
configuration directory, use the `saxo create` command.
"""
def start(base):
# TODO: Check when two clients are running
common.exit_cleanly()
# http://stackoverflow.com/questions/11423225
# IGN rather than DFL, otherwise Popen.communicate can quit saxo
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
opt = configparser.ConfigParser(interpolation=None)
config = os.path.join(base, "config")
if not os.path.isfile(config):
error("missing config file in: `%s`" % config, E_NO_CONFIG)
opt.read(config)
# TODO: Defaulting?
# TODO: Warn if the config file is widely readable?
sockname = os.path.join(base, "client.sock")
serve(sockname, incoming)
os.chmod(sockname, 0o600)
# NOTE: If using os._exit, this doesn't work
def remove_sock(sockname):
if os.path.exists(sockname):
os.remove(sockname)
atexit.register(remove_sock, sockname)
sched = scheduler.Scheduler(incoming)
common.thread(sched.start, base)
saxo = Saxo(base, opt)
saxo.run()