| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | Shiny new words service maker |
|---|
| 6 | """ |
|---|
| 7 | |
|---|
| 8 | import sys, socket |
|---|
| 9 | |
|---|
| 10 | from twisted.application import strports |
|---|
| 11 | from twisted.application.service import MultiService |
|---|
| 12 | from twisted.python import usage |
|---|
| 13 | from twisted import plugin |
|---|
| 14 | |
|---|
| 15 | from twisted.words import iwords, service |
|---|
| 16 | from twisted.cred import checkers, credentials, portal, strcred |
|---|
| 17 | |
|---|
| 18 | class Options(usage.Options, strcred.AuthOptionMixin): |
|---|
| 19 | supportedInterfaces = [credentials.IUsernamePassword] |
|---|
| 20 | optParameters = [ |
|---|
| 21 | ('hostname', None, socket.gethostname(), |
|---|
| 22 | 'Name of this server; purely an informative')] |
|---|
| 23 | |
|---|
| 24 | interfacePlugins = {} |
|---|
| 25 | plg = None |
|---|
| 26 | for plg in plugin.getPlugins(iwords.IProtocolPlugin): |
|---|
| 27 | assert plg.name not in interfacePlugins |
|---|
| 28 | interfacePlugins[plg.name] = plg |
|---|
| 29 | optParameters.append(( |
|---|
| 30 | plg.name + '-port', |
|---|
| 31 | None, None, |
|---|
| 32 | 'strports description of the port to bind for the ' + plg.name + ' server')) |
|---|
| 33 | del plg |
|---|
| 34 | |
|---|
| 35 | def __init__(self, *a, **kw): |
|---|
| 36 | usage.Options.__init__(self, *a, **kw) |
|---|
| 37 | self['groups'] = [] |
|---|
| 38 | |
|---|
| 39 | def opt_group(self, name): |
|---|
| 40 | """Specify a group which should exist |
|---|
| 41 | """ |
|---|
| 42 | self['groups'].append(name.decode(sys.stdin.encoding)) |
|---|
| 43 | |
|---|
| 44 | def opt_passwd(self, filename): |
|---|
| 45 | """ |
|---|
| 46 | Name of a passwd-style file. (This is for |
|---|
| 47 | backwards-compatibility only; you should use the --auth |
|---|
| 48 | command instead.) |
|---|
| 49 | """ |
|---|
| 50 | self.addChecker(checkers.FilePasswordDB(filename)) |
|---|
| 51 | |
|---|
| 52 | def makeService(config): |
|---|
| 53 | credCheckers = config.get('credCheckers', []) |
|---|
| 54 | wordsRealm = service.InMemoryWordsRealm(config['hostname']) |
|---|
| 55 | wordsPortal = portal.Portal(wordsRealm, credCheckers) |
|---|
| 56 | |
|---|
| 57 | msvc = MultiService() |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | for plgName in config.interfacePlugins: |
|---|
| 61 | port = config.get(plgName + '-port') |
|---|
| 62 | if port is not None: |
|---|
| 63 | factory = config.interfacePlugins[plgName].getFactory(wordsRealm, wordsPortal) |
|---|
| 64 | svc = strports.service(port, factory) |
|---|
| 65 | svc.setServiceParent(msvc) |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | for g in config['groups']: |
|---|
| 70 | wordsRealm.createGroup(g) |
|---|
| 71 | |
|---|
| 72 | return msvc |
|---|