-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasicflavor.py
165 lines (140 loc) · 4.1 KB
/
basicflavor.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
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
# -*- coding: utf-8 -*-
###############################################################################
# This file is part of twistedUlm.
#
# 2010, Christian Groeger <[email protected]>
#
# twistedUlm is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# twistedUlm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with twistedUlm. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
from twisted.internet.reactor import callLater
from cept import CHARS
class BasicFlavor():
"""
Inherit from this class to create new server flavors. You have several
callback methods to the CeptServer:
sendCb(str): used to send data to the user
closeCb(): close the connection
relayCb(host, port, commandAfter, sendHeader): relay a connection
"""
sendCb = None
closeCb = None
relayCb =None
userid = None
def hello(self):
"""
Called when the connection is established and header and user id is
parsed. May be used to send a welcome message.
"""
pass
def write(self, data):
"""
Data received from the user is passed to this function.
"""
pass
def dataSent(self):
"""
Called when all data passed to self.sendCb() has been sent. (According
the estimated speed of the user's connection)
"""
pass
def connectionLost(self, reason):
"""
Connection was lost, either because self.closeCb() was called or the
user hang up.
"""
pass
class RelayTest ( BasicFlavor ):
"""
Simple server-flavor to try to relay a connection.
"""
def hello(self):
self.sendCb("Trying to connect you to Ringworld...")
self.relayCb("rw1.m63.co.uk", 23, "*done#")
def write(self, data):
if data=="*done#":
self.sendCb("Goodbye.")
self.closeCb()
class DummyFlavor( BasicFlavor ):
"""
I used this to see what kind of user id I could expect. It sends a
greeting to the user, several alphabets and then closes the connection.
"""
def __init__(self):
self.elist=[]
self.olist=[]
self.nlist=[]
self.flist=[]
def hello(self):
self.sendCb(chr(CHARS['dct']))
print "start: no dct"
self.wc=callLater(10, self.welcome)
self.al=callLater(15, self.alphabet)
self.cl=callLater(40, self.dummy_goodbye)
def welcome(self):
e=""
o=""
n=""
f=""
for c in self.elist:
if c==0xff: e+=" "
else: e+="%x"%c
for c in self.olist:
if c==0xff: o+=" "
else: o+="%x"%c
for c in self.nlist: n+="%x"%c
for c in self.flist: f+="%x"%c
print "even",e
print "odd ",o
print "7bit",n
print "8bit",f
self.sendCb("Welcome to Bildschirmtext.\r\n\r\n\r\n")
def alphabet(self):
#self.sendCb(chr(CHARS['dct']))
self.sendCb("abcdefghijklmnopqrstuvwxyz\r\nABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n\r\n")
self.al=callLater(3, self.alphabet)
def dummy_goodbye(self):
if self.al.active(): self.al.cancel()
self.sendCb("Goodbye Miss Erika Mustermann.\r\n")
self.closeCb()
def connectionLost(self, reason):
if self.wc.active(): self.wc.cancel()
if self.al.active(): self.al.cancel()
if self.cl.active(): self.cl.cancel()
def write(self,data):
for c in data:
original=ord(c)
shorter=original>>1
o_str="..."
s_str="..."
for (k,v) in CHARS.iteritems():
if v==original:
o_str=k
if v==shorter:
s_str=k
pr=""
if shorter>=32 and shorter<=126:
pr=chr(shorter)
par=0
for i in range(8):
if (original&(1<<i))>0:
par+=1
if par%2==0:
self.elist.append(shorter)
self.olist.append(0xff)
else:
self.olist.append(shorter)
self.elist.append(0xff)
self.nlist.append(shorter)
self.flist.append(original)
print "0x%x %s\t0x%x %s <%s>\t%i"%(original,o_str,shorter,s_str,pr,par)