from bottle import run, get, post, request from threading import Lock, Event, Thread from queue import Queue import serial import cgi import copy import sys # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 # # Copyright (C) 2019, SPiNNiX # Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # # 0. You just DO WHAT THE FUCK YOU WANT TO. lock = Lock() queue = Queue() stop_event = Event() TEMPLATE=''' Toilet Paper as a Service

TPaaS - Toilet Paper as a Service

Submit Print Job

Give your print job a name:



A single line on toilet paper is up to 44 characters long.
There are three print modes: Pick up your print at CCC-CH assembly.

Print Queue

    {printqueue}
''' @get('/') def home(): with queue.mutex: q = copy.copy(queue.queue) items = [f'{" "*12}
  • {x[0]}
  • \n' for x in q] return TEMPLATE.format(printqueue=''.join(items)) @post("/") def web(): action = 'wrap' if request.forms.action in ['wrap', 'cut', 'test']: action = request.forms.action content = request.forms.get("txt") or '' name = cgi.html.escape(request.forms.get("printjob") or '') queue.put((name, content, action)) return home() def prt(): with serial.Serial("/dev/ttyUSB0", 19200, xonxoff=True) as s: print('tty opened', file=sys.stderr) while not stop_event.is_set(): name, content, action = queue.get() print(f'now printing: {name}', file=sys.stderr) if action == 'wrap': output = '' for line in content.split("\r\n"): while len(line) > 44: l, line = line[0:44], line[44:] l = l or ' ' output += f'{" "*22}{l}\n' line = line or ' ' output += f'{" "*22}{line}\n' nln = len([1 for x in output.split("\n")]) print(f'Printing {nln} lines in wrap mode', file=sys.stderr) s.write(output.encode('cp437', errors='ignore')) elif action == 'cut': output = '' for line in content.split("\r\n"): line = line or ' ' output += f'{" "*22}{line[0:44]}\n' nln = len([1 for x in output.split("\n")]) print(f'Printing {nln} lines in wrap mode', file=sys.stderr) s.write(output.encode('cp437', errors='ignore')) elif action == 'test': print('Printing hello world', file=sys.stderr) s.write(f'{" "*22}HELLO, 36C3 - TOILET PAPER EXHAUSTION\n'.encode('cp437', errors='ignore')) def main(): t = Thread(target=prt) t.start() run(host='0.0.0.0', port=80, debug=True) if __name__ == '__main__': main()