aboutsummaryrefslogtreecommitdiffstats
path: root/pycrctrl.py
blob: 88ec0297318163cdfef27449874a275fbfa10c8f (plain)
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

#Copyright (c) 2017, George Tokmaji

#Permission to use, copy, modify, and/or distribute this software for any
#purpose with or without fee is hereby granted, provided that the above
#copyright notice and this permission notice appear in all copies.
#
#THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import os
sys = os.sys

import socket
import subprocess
import queue
import threading
from time import sleep
from platform import architecture

import re
import configparser
from io import BytesIO
import urllib.request
import random
import traceback

import tarfile
from enum import IntEnum

from gzip import GzipFile

import asyncio
from blinker import signal
from asyncirc import irc
import asyncirc.plugins.addressed

#
# Helpers
#

class list(list):
    """ Extended list """
    
    def __lshift__(self, other):
        self.append(other)
        return self

    def __rshift__(self, other):
        if isinstance(other, list):
            item = self.__getitem__(len(self) - 1)
            try:
                other.append(item)
                self.pop()
            except Exception:
                raise TypeError("Cannot pass item to {}!".format(other)) from None
    
    # Methods
    
    def isEmpty(self):
        return len(self) == 0

class Updater(object):
    parent = None
    __current_revision = ""
    lookuptable = {"64bit" : "amd64", "32bit" : "i386"}
    
    def __init__(self, parent):
        self.parent = parent
        with open(os.path.join(self.parent.path, "snapshot.id"), "rb") as fobj:
            self.__current_revision = fobj.read().decode("utf-8")
        start_new_thread(self.checkForUpdates, ())
    
    @property
    def current_revision(self):
        return self.__current_revision
    
    @current_revision.setter
    def current_revision(self, other):
        self.__current_revision = other
        if type(other) == str:
            try:
                other = other.encode("utf-8")
            except Exception:
                raise TypeError("Wrong datatype!") from None
        
        with open(os.path.join(self.parent.path, "snapshot.id"), "wb") as fobj:
            fobj.write(other)
    
    def checkForUpdates(self):
        while True:
            try:
                site = urllib.request.urlopen(self.parent.config["Addresses"]["snapshotList"]).read().decode("utf-8").split(self.parent.config["Updater"]["SplitString"])
                site.remove(site[0])
                site = [i.split("' title")[0] for i in site]
                
                x = None
                for i in site:
                    x = re.match(self.parent.config["RegExps"]["Snapshot"].format(sys.platform, self.lookuptable[architecture()[0]]), i)
                    if x:
                        rev = x.group(2)
                        
                        if self.current_revision != rev:
                            self.current_revision = rev
                            self.loadNewSnapshot(x)
                        
                        break
                if not x:
                    print("Updater.checkForUpdates: Regular expression doesn't match!", file=sys.stderr)
            
            except Exception as e:
                traceback.print_exc()
            finally:
                sleep(10)
    
    def loadNewSnapshot(self, reg):
        print(f"Updater: Downloading snapshot with id {self.current_revision}")
        with open(os.path.join(self.parent.path, "snapshot"), "wb") as fobj:
            fobj.write(urllib.request.urlopen(self.parent.config["Addresses"]["snapshotDownload"].format(reg.group(0).split("' title")[0])).read())
        
        #extract the snapshot
        tar = tarfile.open(os.path.join(self.parent.path, "snapshot"), mode="r:bz2")
        tar.extractall(path=self.parent.path)
        print("Updater: New snapshot has been extracted.")
        
        #get the openclonk-server autobuild
        site = json.loads(urllib.request.urlopen(self.parent.config["Addresses"]["autobuildList"]).read().decode("utf-8"))
        
        for commit in site:
            for build in commit["builds"]:
                if re.match(r"{}-{}-.*".format(sys.platform, self.lookuptable[architecture()[0]]), build["platform"]["triplet"]):
                    for b in build["components"]:
                        reg = re.match(self.parent.config["RegExps"]["Autobuild"], str(b["path"])) #skip the engine check as the only useful one is openclonk-server
                        if reg and (reg.group(1), reg.group(2), reg.group(3)) == (self.current_revision[:-3], sys.platform, self.lookuptable[architecture()[0]]):
                            print(f"Updater: Downloading autobuild with id {self.current_revision}")
                            buffer = BytesIO()
                            buffer.write(urllib.request.urlopen(self.parent.config["Addresses"]["autobuildDownload"].format(b["path"])).read())
                            buffer.seek(0)
                            with open(os.path.join(self.parent.path, self.parent.config["Updater"]["BinaryName"]), "wb") as fobj:
                                fobj.write(GzipFile(fileobj=buffer).read())
                                
                            print("Updater: New openclonk-server build has been extracted.")
                            os.chmod(os.path.join(self.parent.path, self.parent.config["Updater"]["BinaryName"]), os.stat(os.path.join(self.parent.path, self.parent.config["Updater"]["BinaryName"])).st_mode | 64)
                            return True
        
        

class PyCRCtrl(object):
    """Server control"""
    irc = None
    
    clonk = None
    server_thread = None
    stopped = False
    config = configparser.ConfigParser()
    
    scenario = ""
    commands = {}
    
    path = None
    config_path = None
    scenlist = []
    league_scenlist = []
    
    topic = "Kein laufendes Spiel."
    
    __state = "Lobby"
    __ingamechat = "aktiviert"
    
    updater = None
    shutdowned = False
    
    @property
    def state(self):
        return self.__state
    
    @state.setter
    def state(self, text):
        if text in ["Lobby", "Lädt", "Läuft"]:
            self.__state = text
            self.setTopic("Aktuelles Szenario: {} | {}{} | Ingamechat ist {}.".format(self.scenario, self.state, (" | Liga" if self.scenario in self.league_scenlist else ""), self.ingamechat))
    
    @property
    def ingamechat(self):
        return self.__ingamechat
    
    @ingamechat.setter
    def ingamechat(self, text):
        if text in ["aktiviert", "deaktiviert"]:
            self.__ingamechat = text
            self.state = self.state
    
    def __init__(self, path, config="pycrctrl.ini"):
        self.path = path
        self.loadConfigFile(config)
        self.ingamechat = "aktiviert" if self.config["IRC"].getboolean("Ingamechat") else "deaktiviert"
        self.loadScenarioList()
        self.startIRC()
        
        self.queue = queue.Queue(5)
        if self.config["Updater"].getboolean("Enabled"):
            self.updater = Updater(self)
    
    def loadScenarioList(self) -> None:
        if self.path == None:
            raise OSError("No path specified")
        
        with open(os.path.join(self.path,"scenarios.lst"), "r") as fobj:
            self.scenlist = fobj.readlines()
        
        with open(os.path.join(self.path, "scenarios_league.lst"), "r") as fobj:
            self.league_scenlist = fobj.readlines()
        
        print("DEBUG: Scenario lists loaded.")
    
    def startIRC(self) -> None:
        asyncirc.plugins.addressed.register_command_character(self.config["General"]["Prefix"])
        self.irc = irc.connect(self.config["IRC"]["Server"], self.config["IRC"]["Port"], use_ssl=self.config["IRC"].getboolean("SSL"))
        self.irc.register(*([self.config["IRC"]["Nick"]] * 3), password=self.config["IRC"]["Password"])
        self.irc.join([*(list(self.config["Channels"].values())), "#openclonk-atlantis"])
        
    def loadConfigFile(self, config) -> None:
        if self.path == None:
            raise OSError("No path specified")
        
        parser = configparser.ConfigParser()
        self.config_path = conf = os.path.join(self.path, config)
        
        if os.path.isdir(conf):
            raise OSError("{} is a directory!".format(conf))
        
        elif os.path.isfile(conf):
            self.config.read(conf)
        
        elif not os.path.exists(conf):
            c = """[General]
Prefix=@

[Clonk]
Engine=clonk
Encoding=utf-8
Commandline=/fullscreen /lobby:300 /record /faircrew
Autohost=false

[IRC]
Ingamechat=false
Nick=PyCRCtrl
Password=
Server=irc.euirc.net
Port=6667
SSL=false

[Channels]
Parent=
Ingame=

[Updater]
Enabled=false
BinaryName=
SplitString=

[Addresses]
snapshotList=http://openclonk.org/nightly-builds
snapshotDownload=http://openclonk.org/builds/nightly/snapshots/{}
autobuildList=https://autobuild.openclonk.org/api/v1/jobs
autobuildAddress=https://autobuild.openclonk.org/static/binaries/{}

[RegExps]
LobbyStart=((?:Los geht's!|Action go!)\\s*)
Start=Start!
PlayerJoin=^Client (.+) (?:verbunden|connected)\.\s*$
PlayerLeave=^Client (.+) (?:entfernt|removed).*
Shutdown=Spiel ausgewertet.*
Autobuild=.*/openclonk-server-(.*)-(.*)-(.*)-.*
Snapshot=openclonk-snapshot-(.*)-(.*)-{}-{}-."""
            
            self.config.read_string(c)
            with open(conf, "w") as fobj:
                self.config.write(fobj)
    
    def startClonk(self):
        try:
            while True:
                if self.scenario == "":
                    if self.queue.empty() == False:
                        self.scenario = self.queue.get()
                    else:
                        self.scenario = random.choice(self.scenlist).splitlines()[0]
                
                self.clonk = subprocess.Popen(
                    './{} {} "{}"'.format(self.config["Clonk"]["Engine"], self.config["Clonk"]["Commandline"] + " " + self.config["Clonk"]["commandlinePrefix"]  + ("league" if self.scenario in self.league_scenlist else "noleague"), self.scenario),
                    0,
                    None,
                    subprocess.PIPE,
                    subprocess.PIPE,
                    subprocess.STDOUT,
                    shell=True,
                    cwd=self.path,
                    encoding=self.config["Clonk"]["Encoding"]
                    )
                self.state = "Lobby"
                self.readServerOutput()
                if self.config["Clonk"].getboolean("Autohost") == False:
                    self.server_thread = None
                    self.setTopic("Kein laufendes Spiel.") 
                    break
        
        finally:
            if self.clonk:
                self.clonk.stdin.close()
    
    def readServerOutput(self):
        while True:
            try:
                output = self.clonk.stdout.readline()
                
                if re.match(self.config["RegExps"]["Shutdown"], output):
                    self.clonk.stdin.close()
                elif output == "" and self.clonk.poll() is not None:
                    if self.clonk:
                        self.clonk.stdin.close()
                        self.clonk = None
                        self.scenario = ""
                        return
                
                elif output:
                    output = output.strip()
                    output = output[(output.find("] ") if output.find("] ") != -1 else -2) + len("] "):] # TODO: Make this code readable
                    
                    if output[0] == ">":
                        output = output[1:]
                    
                    print(output)
                    
                    self.checkForCommands(output, reply=self.writeToServer)
                    
                    if re.match(self.config["RegExps"]["LobbyStart"], output):
                        self.state = "Lädt"
                
                    elif re.match(self.config["RegExps"]["Start"], output):
                        self.state = "Läuft"
                
                    if self.ingamechat == "aktiviert":
                        if self.isMessage(output) and f"<{self.irc.nickname}>" not in output:
                                self.irc.say(self.config["Channels"]["Ingame"], f"[Clonk]{output}")
                        
                        elif (re.match(self.config["RegExps"]["PlayerJoin"], output) or re.match(self.config["RegExps"]["PlayerLeave"], output)):
                            self.irc.say(self.config["Channels"]["Ingame"], output)
                
                
            except KeyboardInterrupt:
                if self.clonk.stdin:
                    self.clonk.stdin.close()
            
            except Exception as e:
                traceback.print_exc()
                continue
    
    def checkForCommands(self, msg, reply) -> None:
        part = self.isCommand(msg)
        if part and part[1] != self.irc.nick:
            cmd = part[2].split(" ", 1)
            found = False
            if len(cmd) > 0:
                key = cmd[0].strip()
                
                if signal(f"cmd-{key}").receivers:
                    found = True
                
                msg = [reply]
                msg.extend(cmd[1].strip().split(" ") if len(cmd) > 1 else [])
                
                signal(f"cmd-{key}").send(msg)
            
            if not found:
                reply('Unbekannter Befehl: "' + part[2] + '"!')
    
    def isMessage(self, msg):
        if self.isCommand(msg):
            return
        
        return re.match(self.config["RegExps"]["Message"], msg)
    
    def isCommand(self, msg):
        return re.match(self.config["RegExps"]["Command"].format(prefix=self.config["General"]["Prefix"]), msg)
    
    def addScenario(self, link):
        name = ""
        for item in link.split("/"):
            if re.match(r"(.*)\.[oc][c4]s",item):
                name = item
                break
        
        site = urllib.request.urlopen(link).read() #WARNING: Raises an error if the link is invalid!
        with open(os.path.join(self.path, name),"wb") as fobj:
            fobj.write(site)
        
        try:
            self.scenlist.index(name)
        except Exception:
            self.scenlist.append(name)
        return self
    
    def writeToServer(self, text : str) -> None:
        if not self.clonk:
            return
        
        elif self.clonk.stdin:
            self.clonk.stdin.write(f"{text}\n")
            self.clonk.stdin.flush()
    
    def setTopic(self, text=None) -> None:
        if not self.irc:
            return
        
        if self.topic != text:
            self.topic = text
            channel = self.config["Channels"]["Ingame"]
            self.irc.writeln(f"TOPIC {self.config['Channels']['Ingame']} :{text}")
    
    def shutdown(self):
        if self.shutdowned:
            return
        
        del self.updater
        
        if self.clonk and self.clonk.stdin and not self.clonk.stdin.closed:
            self.clonk.stdin.close()
        
        with open(self.config_path, "w") as fobj:
            self.config.write(fobj)
            print("Config file saved.")
        
        with open(os.path.join(self.path, "scenarios.lst"), "w") as fobj:
            fobj.writelines(self.scenlist)
        
        with open(os.path.join(self.path, "scenarios_league.lst"), "w") as fobj:
            fobj.writelines(self.league_scenlist)
        
        print("Scenario lists saved.")
        self.setTopic("Kein laufendes Spiel.")
        self.shutdowned = True
    
    def on(self, e):
        def process(f):
            signal(e).connect(f)
            return f
        return process

# Usage: ./pycrctrl.py path config
server = PyCRCtrl(sys.argv[1], sys.argv[2])

@server.irc.on("message")
def on_message(message, user, target, text):
    channel = server.config["Channels"]["Ingame"]
    if target == channel and server.ingamechat == "aktiviert" and user.nick != server.config["IRC"]["Nick"]:
        server.writeToServer(f"[IRC]<{user.nick}> {text}")

@server.irc.on("addressed")
def on_addressed(message, user, target, text):
    if target == server.config["Channels"]["Ingame"] and server.ingamechat == "aktiviert":
        return
    
    def reply(text):
        server.irc.say(target, text)
    
    try:
        server.checkForCommands(f"<{user}> {server.config['General']['Prefix']}{text}", reply=reply)
    except Exception as e:
        traceback.print_exc()

@server.on("cmd-start")
def start(args):
    server.stopped = False
    try:
        time = int(args[1])
    except Exception:
        time = 5
    args[0](f"/start {time}")

@server.on("cmd-stop")
def stop(args):
    def stopping():
        while server.clonk and server.stopped:
            server.writeToServer("/start 60000")
            sleep(100)
    
    if server.stopped == False:
        server.stopped = True
        threading.Thread(target=stopping).start()

@server.on("cmd-queue")
def queue(args):
    reply = args[0]
    reply("Warteschlange:")
    for i, scen in enumerate(server.queue.queue, start=1):
        reply(f"{i}. {scen}")

@server.on("cmd-list")
def lst(args):
    reply = args[0]
    if reply != server.writeToServer:
        reply("Die Szenarienliste kann nur ingame angesehen werden!")
        return
    
    reply("Verfügbare Szenarien:")
    for scen in server.scenlist:
        reply(scen)

@server.on("cmd-irc")
def cmd_irc(args):
    reply = args[0]
    if not args:
        reply("Keine Parameter angegeben!")
    
    if args[1] == "ingamechat":
        if len(args) <= 1 or args[2] == "off":
            server.ingamechat = "deaktivert"
        elif args[2] == "on":
            server.ingamechat = "aktiviert"

@server.on("cmd-host")
def host(args):
    reply = args[0]
    if len(args) <= 1:
        reply("Bitte gib einen Szenarionamen an!")
    scenario = args[1].strip()
    
    if scenario == "random":
        scenario = random.choice(server.scenlist).strip()
    
    elif scenario not in server.scenlist:
        reply(f"Szenario {scenario} nicht gefunden!")
    
    if not server.server_thread:
        server.scenario = scenario
        server.server_thread = threading.Thread(target=server.startClonk)
        server.server_thread.start()
        reply(f"Szenario {scenario} wird jetzt gehostet.")
    
    elif not server.queue.full():
        server.queue.put(scenario)
        reply(f"Szenario {scenario} wurde der Warteschlange hinzugefügt.")
    
    else:
        reply("Warteschlange ist voll!")

@server.on("cmd-quit")
def quit(args):
    reply = args[0]
    if server.clonk and server.clonk.stdin:
        server.clonk.stdin.close()
        server.server_thread.terminate()
        server.shutdown()
        sleep(5)
        raise SystemExit

try:
    asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
    sys.exit(0)