-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcepthtml.py
239 lines (199 loc) · 6.67 KB
/
cepthtml.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- 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/>.
###############################################################################
import cept
import re, string
class ceptHTML():
"""
Here a "BtxML" page is parsed into a nice structure.
"""
def __init__(self,name=''):
self.name=name
self.requestedName=''
self.parseError=False
self.links={}
self.loadpage=''
self.loadpageTimeout=-1
self.relayHost=''
self.relayPort=-1
self.relayAfter=''
self.relayHeader=False
self.nohistory=False
#self.inputSize=-1
#self.inputTarget=''
#self.inputMethod='GET'
self.nextPage=''
self.disconnect=-1 # this is a timeout
self.body=''
def reset(self, name=''):
"""
Clear all previously res values.
"""
self.__init__(name)
def _addLink(self,name,link):
self.links[name]=link
def getLink(self,name):
"""
This can be used to check if the current user input is a link to
another page, it returns None if it isn't.
"""
try:
return self.links[name]
except:
return None
def _translateTag(self,tag):
"""
Used by parseHTML to translate <tags> into single chars, Tags may be
any of the characters specified in cept.py or hexadecimal values in the
form of <0xXX>
"""
tag=tag.group(1)
try:
if tag[0:2]=='0x' or tag[0:2]=='0X':
#print "tag %s interpreted as %i" %(tag, int(tag[2:], 16))
return chr(int(tag[2:], 16))
except:
pass
try:
return chr(cept.CHARS[tag])
except:
#print "unknown tag: "+tag
return "<"+tag+">"
def _interpretSitename(self,name):
"""
This just removes whitespaces, *s and #s from the beginning and end of
a string.
"""
try:
name=string.strip(str(name),string.whitespace+"\"'")
except:
name=''
name=string.lstrip(name,'*')
name=string.rstrip(name,'#')
return name
def _parseError(self):
"""
Page couldn't be parsed, so set some values to standard-values.
"""
self.parseError=True
self.nextPage='0'
self.body="The page named "+str(self.name)+" cannot be processed.\nPress # or *page# to continue: "
def parseHTML(self, content, pagename=''):
"""
V inefficient parsing using regular expressions at the moment,
I'll fix that later.
"""
self.reset()
self.name=pagename
# pages must be enclosed by cept-tags
pagecontent = re.search('<cept>(.*)</cept>',content,re.I|re.S)
if pagecontent == None:
# print("ParseError: missing <cept>-tags")
self._parseError()
return
# print("<cept>-tags found")
head=re.search('<head>(.*)</head>',pagecontent.group(1),re.I|re.S)
if head is not None:
# print("<head>-tags found")
metatags=re.findall('<meta\s+?(.*?)>',head.group(1),re.I|re.S)
if metatags is not None:
for tag in metatags:
metaname=re.search('name\s*?=\s*?[\'\"]?(\S*?)[\'\"\s]',tag,re.I|re.S)
metacontent=re.search('content=\s*?[\'\"]?(\S*?)[\'\"\s]',tag,re.I|re.S)
if metaname != None and metacontent != None:
if metaname.group(1)=="load_page":
self.loadpage=self._interpretSitename(metacontent.group(1))
# print("setting loadpage to "+page.loadpage)
elif metaname.group(1)=="load_timeout":
try:
self.loadpageTimeout=int(metacontent.group(1))
# print("setting loadpage timeout to %i" %page.loadpageTimeout)
except:
pass
elif metaname.group(1)=="next_page":
self.nextPage=self._interpretSitename(metacontent.group(1))
# print("setting next page to "+page.nextPage)
elif metaname.group(1)=="disconnect":
try:
self.disconnect=int(metacontent.group(1))
# print("will disconnect in %i seconds" %page.disconnect)
except:
pass
elif metaname.group(1)=="relay":
try:
(host,port)=metacontent.group(1).split(":")
# print("setting relay to %s:%i"%(host,int(port)))
self.relayHost=host
self.relayPort=int(port)
except:
pass
elif metaname.group(1)=="after_relay":
try:
self.relayAfter=self._interpretSitename(metacontent.group(1))
except:
pass
elif metaname.group(1)=="relay_header":
try:
if string.lower(metacontent.group(1))=="true":
self.relayHeader=True
except:
pass
elif metaname.group(1)=="nohistory":
try:
if string.lower(metacontent.group(1))=="true":
self.nohistory=True
except:
pass
#elif metaname.group(1)=="input_size":
#try:
#self.inputSize=int(metacontent.group(1))
## print("page wants input with up to %i chars" %page.inputSize)
#except:
#pass
#elif metaname.group(1)=="input_target":
#try:
#self.inputTarget=interpretSitename(metacontent.group(1))
## print("input should be send to %s" %page.inputTarget)
#except:
#pass
#elif metaname.group(1)=="input_method":
#try:
#if metacontent.group(1)=="POST":
#self.inputMethod="POST"
#else:
#self.inputMethod="GET"
## print("input is send using %s" %page.inputMethod)
#except:
#pass
elif metaname.group(1)=="hyperlinks":
for link in metacontent.group(1).split(","):
parsedLink=re.search("^(\d+)(\*[a-zA-Z0-9]+#)$",link)
if parsedLink is not None:
linkfrom=parsedLink.group(1)
linkto=self._interpretSitename(parsedLink.group(2))
# print("adding link from %s to %s" %(linkfrom,linkto))
self._addLink(linkfrom,linkto)
# the body is just plain text and cept-commands, cept-command-characters
body=re.search('<body>(.*)</body>',pagecontent.group(1),re.I|re.S)
if body is not None:
# print("<body>-tags found")
body=re.sub(re.compile("(<!--.*?-->)|\s",re.I|re.S), "", body.group(1))
body=re.sub(re.compile("<([a-zA-Z0-9]+?)>",re.I|re.S), self._translateTag, body)
#print body
self.body=body