Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 171 lines (141 sloc) 6.004 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 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
## welcome system core
#
# Copyright (c) 2008, 2009 Adam Marshall Smith
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# Idea:
# - run a web server for serving misc welcome system related pages
# - GET /prefill/{key} gets a view of known details for {key}
# - POST /attend/{key} updates card store, notices new attendees, and prints
# - GET /print/{id} gets a print job's status
#
# - new attendees have their cards hooked out to any registered recipients

from twisted.internet import reactor
from twisted.python import log
from twisted.web import server, resource, static, http

import simplejson

import cards
import attendance
import printer
import hooks
import secure

####
#### Web Resources (like pages)
####

class RootRedirector(resource.Resource):
    def render(self, request):
        request.redirect("/static/welcome.html")
        return ""

class Printer(resource.Resource):
    isLeaf = True
    def __init__(self, printerManager):
        resource.Resource.__init__(self)
        self.printerManager = printerManager
    
    def render_GET(self, request):
        if len(request.postpath) is not 1:
            request.setResponseCode(http.NOT_FOUND)
            return ""
            
        jobId = int(request.postpath[0])
        jobStatus = "finished"
        if jobId in printerManager.getOutstandingJobs():
            jobStatus = "outstanding"
        elif jobId in printerManager.getFailedJobs():
            jobStatus = "failed"
        return simplejson.dumps(dict(status=jobStatus))
        
class Prefill(resource.Resource):
    isLeaf = True
    def __init__(self, attendanceManager):
        resource.Resource.__init__(self)
        self.attendanceManager = attendanceManager

    def render_GET(self, request):
        key = request.postpath[0]
        prefill = attendanceManager.prefill(key)
        request.setHeader("content-type", "application/json")
        return simplejson.dumps(prefill)
                
class Attend(resource.Resource):
    isLeaf = True
    def __init__(self, attendanceManager, printerManager, cardStore):
        resource.Resource.__init__(self)
        self.attendanceManager = attendanceManager
        self.printerManager = printerManager
        self.cardManager = cardStore

    def render_POST(self, request):
        key = request.postpath[0]
        updates = dict([(k,vs[0])for (k,vs) in request.args.items()])
        attendanceManager.attend(key, updates)

        fullCard = cardStore.getCard(key)
        printJobId, d = printerManager.printCard(fullCard)

        request.setHeader("content-type", "application/json")
        return simplejson.dumps(dict(printJobId=printJobId))

###
### Top Level
###

if __name__ == "__main__":
    import sys, os
    if len(sys.argv) != 3:
        print "%s cards.dat event_0" % sys.argv[0]
        sys.exit(1)
    dat = sys.argv[1] # pickle'd dict for backing the card store
    eventKey = sys.argv[2] # keyable name for identifying the CURRENT event

    log.startLogging(sys.stdout)
    
    hookManager = hooks.HookManager()
    # example: hookManager.addRecipient("http://cacti.fihn.net/")
    hookManager.addRecipient("http://localhost:10100/")
    
    cardStore = cards.CardStore(dat)

    def onAttend(key):
        fullCard = cardStore.getCard(key)
        hookManager.dispatchEvent(
          "org.superhappydevhouse.event.Attendance", fullCard, event_key=eventKey)
        log.msg("ARRIVAL: looks like %s arrived." % key)

    attendanceManager = \
        attendance.AttendanceManager(cardStore, eventKey, onAttend)
    
    printerManager = printer.PrinterManager()
    printerManager.updates.update(event_key=eventKey)

    # XXX: adding shdh_number for badge print compatibility
    parts = eventKey.split("_")
    if len(parts) < 2:
      print "Event key SHOULD REALLY look like \"shdh_99\"!!"
      sys.exit(2)
    printerManager.updates.update(shdh_number=parts[1])

    # secure welcome root resource
    sroot = resource.Resource()
    sroot.putChild('', RootRedirector())
    sroot.putChild('static', static.File('static'))
    sroot.putChild('printer', Printer(printerManager))
    sroot.putChild('prefill', Prefill(attendanceManager))
    sroot.putChild('attend', \
        Attend(attendanceManager, printerManager, cardStore))
    
    # insecure root
    iroot = static.File('bootstrap')

    # reactor setup

    if os.environ.get('INSECURE',False):
        reactor.listenTCP(10081, server.Site(sroot))

    print "just type ^D if you don't want to use the HTTPS port...it's optional..."
    try:
      reactor.listenSSL(10443, server.Site(sroot), \
        secure.ServerContextFactory(myKey='certs/server.pem', trustedCA='certs/certificate_authority/shdh-ca.pem'))
    except:
      print "couldn't start SSL server... missing certs/server.pem???\n python says:", sys.exc_info()

    reactor.listenTCP(10080, server.Site(iroot))
    
    log.msg("It's a piece of cake to break a pretty snake. [SYSTEM ONLINE]")
    reactor.run()
Something went wrong with that request. Please try again.