-
Notifications
You must be signed in to change notification settings - Fork 1
/
telserv.py
153 lines (153 loc) · 5.71 KB
/
telserv.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
import threading
import socket
import os
class CommandLine:
def __init__(self, con, addr):
self.con = con
self.addr = addr
self.style = {
'bold': False,
'italic': False,
'underline': False,
'strike': False,
'blink': False,
'color': 0
}
def bbtoans(self, stringbb):
string = '\x1b[m'
idx = 0
change = False
style = self.style
while idx<len(stringbb):
change = False
if stringbb[idx:idx+3]=='[b]':
style['bold'] = True
change = True
idx+=3
if stringbb[idx:idx+4]=='[/b]':
style['bold'] = False
change = True
idx+=4
if stringbb[idx:idx+3]=='[i]':
style['italic'] = True
change = True
idx+=3
if stringbb[idx:idx+4]=='[/i]':
style['italic'] = False
change = True
idx+=4
if stringbb[idx:idx+3]=='[u]':
style['underline'] = True
change = True
idx+=3
if stringbb[idx:idx+4]=='[/u]':
style['underline'] = False
change = True
idx+=4
if stringbb[idx:idx+3]=='[s]':
style['strike'] = True
change = True
idx+=3
if stringbb[idx:idx+4]=='[/s]':
style['strike'] = False
change = True
idx+=4
if stringbb[idx:idx+7]=='[blink]':
style['blink'] = True
change = True
idx+=7
if stringbb[idx:idx+8]=='[/blink]':
style['blink'] = False
change = True
idx+=8
if stringbb[idx:idx+7]=='[color=' and stringbb[idx+9]==']':
style['color'] = stringbb[idx+7:idx+9]
change = True
idx+=10
if stringbb[idx:idx+8]=='[/color]':
style['color'] = 0
change = True
idx+=8
if change:
string+='\x1b[m'
if style['bold']: string+='\x1b[1m'
if style['italic']: string+='\x1b[3m'
if style['underline']: string+='\x1b[4m'
if style['strike']: string+='\x1b[9m'
if style['blink']: string+='\x1b[5m'
if style['color']: string+='\x1b[{}m'.format(style['color'])
else:
string+=stringbb[idx]
idx+=1
self.style = style
return string
def printnonew(self, string):
return self.con.send(string.encode())
def print(self, string):
return self.printnonew('{}\r\n'.format(string))
def printbbnonew(self, string):
return self.printnonew(self.bbtoans(string))
def printbb(self, string):
return self.print(self.bbtoans(string))
def printfile(self, file):
tmp = self.con.send(open(file, 'rb').read())
self.printnonew('\x1b[m')
return tmp
def input(self, delim='\r\n'):
stdin = ''
length = len(delim)
dat = self.con.recv(length).decode()
if delim==' ': return dat
while dat!=delim:
stdin += dat
dat = self.con.recv(length).decode()
return stdin
def inputhidden(self, censor=' ', delim='\r\n'):
stdin = ''
length = len(delim)
dat = self.con.recv(length).decode()
#self.printnonew('\x1b[D{}'.format(censor))
if delim==' ': return dat
while dat!=delim:
self.printnonew('\x1b[D{}'.format(censor))
stdin += dat
dat = self.con.recv(length).decode()
return stdin
def quit(self):
self.con.close()
class TelnetServer:
def __init__(self, func=lambda:None, port=23):
self.port = port
self.func = func
def __repr__(self):
return 'TelnetServer({})'.format(self.port)
def __str__(self):
return self.__repr__()
def start(self):
sock = socket.socket()
sock.bind(('',self.port))
def listen():
while True:
sock.listen(5)
threading.Thread(target=self.func, args=(CommandLine(*sock.accept()),)).start()
threading.Thread(target=listen, args=()).start()
def test(self):
os.system('telnet {}'.format(socket.gethostbyname(socket.gethostname())))
def load(file):
return open(file, 'r').read()
if __name__ == '__main__':
def main(line):
line.print('line.print prints the text passed in as an argument, And it also supports ANSI sequences.')
line.printnonew('line.printnonew is the same as line.print, except it does not add the newline at the end.\r\n')
line.printbb('[b]line.printbb[/b] prints [b][color=31]BB[/color][color=34]code[/color]![/b]')
# line.printbbnonew is self-explanitory.
line.printfile('directory\\of\\ansi\\art.ans')
line.printbb('[b]Username: [color=31]')
username = line.input() # Take input and stop when recieving '\r\n' which is the return key. ( '\r\n' is the default if there's no argument. )
line.printbb('[/color]Password: [color=34]')
password = line.inputhidden('*') # Take input just like line.input() but display each character as an asterisk.
line.printbb('[/color][color=33]Logged in![/color]')
server = tel.TelnetServer(main)
server.start()
if input('Test BBS?')=='yes':
server.test()