-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircserver.py
67 lines (50 loc) · 1.92 KB
/
ircserver.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
from twisted.protocols import protocol
from twisted.words import service, ircservice
from twisted.internet import passport
class SimpleService(service.Service):
"""A simple IRC service that creates users on the fly."""
def removePerspective(self, name):
if self.participants.has_key(name):
del self.participants[name]
self.application.authorizer.removeIdentity(name)
def createParticipant(self, name):
if not self.participants.has_key(name):
log.msg("Created New Participant: %s" % name)
def getPerspectiveNamed(self, name):
if self.participants.has_key(name):
raise service.WordsError, "user exists"
else:
p = service.Participant(name)
p.setService(self)
ident = passport.Identity(name, self.application)
ident.setPassword("ugly hack")
self.application.authorizer.addIdentity(ident)
p.setIdentity(ident)
ident.addKeyForPerspective(p)
self.participants[name] = p
return p
class IRCChatter(ircservice.IRCChatter):
passwd = "ugly hack" # remove this to force user to send password
def connectionLost(self):
ircservice.IRCChatter.connectionLost(self)
print self.nickname
self.service.removePerspective(self.nickname)
class IRCGateway(protocol.Factory):
def __init__(self, service):
self.service = service
def buildProtocol(self, connection):
"""Build an IRC protocol to talk to my chat service.
"""
i = IRCChatter()
i.service = self.service
return i
def main():
"""Run an IRC server"""
from twisted.internet import main
app = main.Application("irc")
svc = SimpleService("twisted.words", app)
irc = IRCGateway(svc)
app.listenTCP(6667, irc)
app.run(0)
if __name__ == '__main__':
main()