home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2001 April
/
PCWorld_2001-04_cd.bin
/
Software
/
TemaCD
/
webclean
/
webcleanerconf
< prev
next >
Wrap
Text File
|
2001-01-14
|
52KB
|
1,254 lines
#!/usr/bin/env python2
""" webcleanerconf
Copyright (C) 2000,2001 Bastian Kleineidam
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
This is a nice graphical configuration tool for WebCleaner. Because
it depends on some classes that come only with WebCleaner, I do not
separate this tool from the WebCleaner source.
Developer note: you have to create() all windows which are created at
runtime. This is also a FAQ entry in the FOX documentation.
"""
import sys
if sys.version[:5] < "2.0":
raise SystemExit, "This program requires Python 2.0 or later."
from FXPy import *
import WebCleanerConf
import os,string,tempfile,time
from webfilter import _
from webfilter.get_dom import *
from webfilter.Rules import GetRuleFromNode, GetRuleFromName, FilterRules, Netlocparts
from types import IntType
# set the directory for new files
tempfile.tempdir = WebCleanerConf.config_dir
DEBUG_LEVEL=0
def debug(msg):
if DEBUG_LEVEL:
sys.stderr.write("DEBUG: "+msg+"\n")
def error(msg):
sys.stderr.write("error: "+msg+"\n")
HelpText = \
_("Most values have a tooltip. Point your mouse over the labels and"
"you can read them.\n"
"Unix users can also read the manual page with 'man webcleanerconf'.")
TipText = {
#Proxy GUI settings (configfile settings):
"Port": _("Set the port adress the WebCleaner proxy is listening on.\n"
"Default value is 8080."),
"Logfile": _("The name for the logfile can be empty (no logging), 'stdout'\n"
"(standard out) or a filename (relative or absolute)."),
"Buffer size": _("The buffer size tells the proxy to read data in chunks with the\n"
"given size."),
"Timeout": _("Network timeout after which the connection is disabled. Currently\n"
"this is used only with HTTPS connections, the other connections\n"
"have the system defined socket timeout."),
"Obfuscate IP": _("When this checkbox is active (configuration file value = 1) the\n"
"proxy translates the numerical IP name into an integer number.\n"
"For example '5.7.9.11' gets mapped to\n"
"5*2^24 + 7*2^16 + 9*2^8 + 11*2^0 = 84347147"),
"Debug level": _("Set the debugging level. Debug output is on stderr.\n"
"Debug levels:\n"
"0 - disabled (default)\n"
"1 - normal debug messages\n"
"2 - print requests and filtering\n"
"3 - detailed filter messages"),
"Parent Proxy": _("The parent proxy WebCleaner should use."),
"Rewriter": _("Rewrite HTML code. This is very powerful and can filter\n"
"almost all advertising and other crap."),
"Parent Proxy Port": _("The parent proxy port WebCleaner should use."),
"BinaryCharFilter": _("Replace illegal binary characters in HTML code like the quote\n"
"chars often found in Microsoft pages."),
"Header": _("Add, modify and delete HTTP headers of request and response."),
"Blocker": _("Block specific sites by URL name."),
"GifImage": _("Deanimates GIFs and removes all unwanted GIF image\n"
"extensions (for example GIF comments)."),
"Compress": _("Compression of documents with good compression ratio\n"
"like HTML, WAV, etc."),
#Filter GUI settings (configfile settings):
"Title": _("The title of the filter."),
"Description": _("Some good description what this filter does."),
"Language": _("If you specify incompatible regular expressions\n"
"choose the appropriate language here."),
"Filename": _("The name of the configuration file where these\n"
"filters are stored."),
"disable": _("You can disable a whole folder or single filter rules"),
}
RemoveText = \
_("You cannot remove folders. If you really want to get rid\n"
"of this folder, delete the appropriate configuration file.\n"
"It is always safer to disable a folder or filter instead of\n"
"deleting it!")
def lang_num(lang):
"""a little helper function for an FXComboBox"""
if lang=="Perl": return 1
if lang=="Python": return 2
return 0
def num_lang(num):
"""another little helper function for an FXComboBox"""
if num==1: return "Perl"
if num==2: return "Python"
return None
def get_time(secs):
"""return formatted timestamp"""
import time
t = time.localtime(secs)
return time.strftime("%d.%m.%Y %H:%M:%S", t)
# names of the filter types
Filternames = ['block', 'rewrite', 'allow', 'header', 'image', 'nocomments']
def loadIcon(app, filename):
"""load icons from the config_dir directory"""
filename = os.path.join(WebCleanerConf.config_dir, filename)
ext = string.lower(filename[-3:])
data = open(filename, 'rb').read()
if ext=='gif':
return FXGIFIcon(app, data)
if ext=='png':
return FXPNGIcon(app, data)
class FXRuleFrameFactory:
"""Every Rule (see webfilter/Rules.py) has a "fromFactory" function.
A factory has a function from***Rule for every different Rule
class.
This particular factory generates windows which display all the
variables found in a rule.
"""
def __init__(self, rootframe):
"""Because we store frames in a hierarchical tree, we need
a tree rootframe.
To distinguish frames we have a unique index for each."""
self.rootframe = rootframe
self.index = 0
def inc_index(self):
self.index += 1
def fromRule(self, rule):
self.inc_index()
return FXRuleFrame(self.rootframe, rule, self.index)
def fromRewriteRule(self, rule):
self.inc_index()
return FXRewriteRuleFrame(self.rootframe, rule, self.index)
def fromAllowRule(self, rule):
self.inc_index()
return FXAllowRuleFrame(self.rootframe, rule, self.index)
def fromBlockRule(self, rule):
self.inc_index()
return FXBlockRuleFrame(self.rootframe, rule, self.index)
def fromHeaderRule(self, rule):
self.inc_index()
return FXHeaderRuleFrame(self.rootframe, rule, self.index)
def fromImageRule(self, rule):
self.inc_index()
return FXImageRuleFrame(self.rootframe, rule, self.index)
def fromNocommentRule(self, rule):
self.inc_index()
return FXNocommentRuleFrame(self.rootframe, rule, self.index)
def fromFilterRules(self, rule):
self.inc_index()
return FXFilterRulesFrame(self.rootframe, rule, self.index)
class FXRuleFrame(FXVerticalFrame):
"""display all variables found in a basic Rule.
This means basically we have a FOX widget for each variable
found in the rule (e.g. for rule.title we have an FXTextField).
"""
(ID_TITLE,
ID_DESC,
ID_DISABLE_RULE,
ID_LAST,
) = range(FXVerticalFrame.ID_LAST, FXVerticalFrame.ID_LAST+4)
def __init__(self, parent, rule, index):
FXVerticalFrame.__init__(self, parent, LAYOUT_FILL_X|LAYOUT_FILL_Y)
# event map
FXMAPFUNC(self,SEL_COMMAND, FXRuleFrame.ID_TITLE,FXRuleFrame.onCmdTitle)
FXMAPFUNC(self,SEL_CHANGED, FXRuleFrame.ID_DESC, FXRuleFrame.onCmdDesc)
FXMAPFUNC(self,SEL_COMMAND, FXRuleFrame.ID_DISABLE_RULE,FXRuleFrame.onCmdDisableRule)
self.rule = rule
# augment the rule information with the (unique) index
self.rule.index = index
FXLabel(self, self.get_name(), opts=LAYOUT_CENTER_X|LAYOUT_TOP|LAYOUT_FILL_X)
f = FXHorizontalFrame(self, LAYOUT_FILL_X|LAYOUT_LEFT|LAYOUT_TOP, 0,0,0,0, 0,0,0,0, 0,0)
FXLabel(f, _("Title")+":\t"+TipText["Title"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXTextField(f, 15, self, FXRuleFrame.ID_TITLE)
t.setText(self.rule.title)
t = FXCheckButton(f, _("disable")+"\t"+TipText["disable"], self, FXRuleFrame.ID_DISABLE_RULE,ICON_AFTER_TEXT|LAYOUT_RIGHT|LAYOUT_CENTER_Y|LAYOUT_FILL_X)
t.setCheck(self.rule.disable)
FXLabel(self, _("Description")+":\t"+TipText["Description"])
f = FXVerticalFrame(self, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 0,0,0,0, 0,0)
t = FXText(f, self, FXRuleFrame.ID_DESC, opts=LAYOUT_FILL_X|LAYOUT_FILL_Y|TEXT_WORDWRAP)
t.setText(self.rule.desc)
t.setEditable()
def get_name(self):
"""display this name at the top of the window"""
s = string.capitalize(self.rule.get_name())+_(" rule")
s += " (ID%d"%self.rule.ruleid+") - "+get_time(self.rule.timestamp)
return s
def onCmdTitle(self, sender, sel, ptr):
title = string.strip(sender.getText())
if not title:
error(_("empty title"))
sender.setText(self.rule.title)
return 1
sender.setText(title)
self.rule.title = title
self.getApp().dirty = 1
debug("Rule title changed")
# send message to main window for treelist updating
win = self.getApp().getMainWindow()
win.handle(sender, MKUINT(ConfWindow.ID_TITLE,SEL_COMMAND), ptr)
return 1
def onCmdDesc(self, sender, sel, ptr):
if self.rule.desc != sender.getText():
self.rule.desc = sender.getText()
self.getApp().dirty = 1
debug("Rule description changed")
return 1
def onCmdDisableRule(self, sender, sel, ptr):
self.rule.disable = sender.getCheck()
self.getApp().dirty = 1
debug("Rule "+(self.rule.disable and "disabled" or "enabled"))
return 1
class FXRewriteRuleFrame(FXRuleFrame):
"""display all variables found in a RewriteRule"""
(ID_TAG,
ID_ENCLOSED_BLOCK,
ID_REPLACE_PART,
ID_ATTRIBUTE_ADD,
ID_ATTRIBUTE_EDIT,
ID_ATTRIBUTE_REMOVE,
ID_REPLACE_VALUE,
) = range(FXRuleFrame.ID_LAST, FXRuleFrame.ID_LAST+7)
def __init__(self, parent, rule, index):
FXRuleFrame.__init__(self, parent, rule, index)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_TAG,FXRewriteRuleFrame.onCmdTag)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_ENCLOSED_BLOCK,FXRewriteRuleFrame.onCmdEnclosed)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_REPLACE_PART,FXRewriteRuleFrame.onCmdReplacePart)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_REPLACE_VALUE,FXRewriteRuleFrame.onCmdReplaceValue)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_ATTRIBUTE_ADD,FXRewriteRuleFrame.onCmdAttributeAdd)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_ATTRIBUTE_EDIT,FXRewriteRuleFrame.onCmdAttributeEdit)
FXMAPFUNC(self,SEL_COMMAND,FXRewriteRuleFrame.ID_ATTRIBUTE_REMOVE,FXRewriteRuleFrame.onCmdAttributeRemove)
matrix = FXMatrix(self, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Tag name:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXTextField(matrix, 10, self, FXRewriteRuleFrame.ID_TAG)
t.setText(self.rule.tag)
FXLabel(matrix, _("Attributes:"))
f = FXHorizontalFrame(matrix, LAYOUT_FILL_X|LAYOUT_FIX_HEIGHT|FRAME_SUNKEN|FRAME_THICK, 0,0,0,60, 0,0,0,0, 0,0)
self.iconlist = FXIconList(f, opts=LAYOUT_FILL_X|LAYOUT_FILL_Y|ICONLIST_SINGLESELECT|ICONLIST_AUTOSIZE)
self.iconlist.appendHeader(_("Name"),NULL,50)
self.iconlist.appendHeader(_("Value"),NULL,175)
for name,value in self.rule.attrs.items():
self.iconlist.appendItem(name +"\t"+value.pattern)
FXLabel(matrix, "")
f = FXHorizontalFrame(matrix)
FXButton(f, _("Add"), None, self, FXRewriteRuleFrame.ID_ATTRIBUTE_ADD)
FXButton(f, _("Edit"), None, self, FXRewriteRuleFrame.ID_ATTRIBUTE_EDIT)
FXButton(f, _("Remove"), None, self, FXRewriteRuleFrame.ID_ATTRIBUTE_REMOVE)
FXLabel(matrix, _("Enclosed block:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXTextField(matrix, 20, self, FXRewriteRuleFrame.ID_ENCLOSED_BLOCK)
if self.rule.enclosed:
t.setText(self.rule.enclosed.pattern)
FXLabel(matrix, _("Replace part:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXComboBox(matrix,9,6,self, self.ID_REPLACE_PART,opts=COMBOBOX_INSERT_LAST|FRAME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_TOP)
t.appendItem(_("Tag"))
t.appendItem(_("Tag name"))
t.appendItem(_("Attribute"))
t.appendItem(_("Attribute value"))
t.appendItem(_("Complete tag"))
t.appendItem(_("Enclosed block"))
t.setEditable(0)
t.setCurrentItem(self.rule.replace[0])
FXLabel(matrix, _("Replace value:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXTextField(matrix, 20, self, FXRewriteRuleFrame.ID_REPLACE_VALUE)
t.setText(self.rule.replace[1])
def onCmdTag(self, sender, sel, ptr):
tag = string.strip(sender.getText())
if not tag:
error("empty tag name")
sender.setText(self.rule.tag)
return 1
self.rule.tag = tag
self.getApp().dirty = 1
debug("Changed rule tag name")
return 1
def onCmdEnclosed(self, sender, sel, ptr):
enclosed = string.strip(sender.getText())
if enclosed:
self.rule.enclosed = re.compile(enclosed)
else:
self.rule.enclosed = None
self.getApp().dirty = 1
debug("Changed rule enclosed block")
return 1
def onCmdReplacePart(self, sender, sel, ptr):
self.rule.replace[0] = sender.getCurrentItem()
self.getApp().dirty = 1
debug("Changed rule replace part")
return 1
def onCmdAttributeAdd(self, sender, sel, ptr):
dialog = FXDialogBox(self,_("Add Attribute"),DECOR_TITLE|DECOR_BORDER)
frame = FXVerticalFrame(dialog, LAYOUT_SIDE_TOP|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y|PACK_UNIFORM_WIDTH)
matrix = FXMatrix(frame, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Name:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
nametf = FXTextField(matrix, 20)
FXLabel(matrix, _("Value:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
valuetf = FXTextField(matrix, 20)
f = FXHorizontalFrame(frame)
FXButton(f, _("&Ok"), None, dialog, FXDialogBox.ID_ACCEPT,FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
FXButton(f, _("&Cancel"), None, dialog, FXDialogBox.ID_CANCEL,FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
if dialog.execute():
name = string.lower(string.strip(nametf.getText()))
if not name:
error(_("Empty attribute name"))
return 1
if self.rule.attrs.has_key(name):
error(_("Duplicate attribute name"))
return 1
try:
import re
value = string.strip(valuetf.getText())
value = re.compile(value)
except:
import sys
error(_("Invalid regex %s: %s") % (value,sys.exc_info()[1]))
return 1
self.rule.attrs[name] = value
self.getApp().dirty = 1
self.iconlist.appendItem(name+"\t"+value.pattern)
debug("Added rule attribute")
return 1
def onCmdAttributeEdit(self, sender, sel, ptr):
index = self.iconlist.getCurrentItem()
if index < 0: return 1
item = self.iconlist.retrieveItem(index)
if not item.isSelected(): return 1
name,value = string.split(item.getText(), '\t')
dialog = FXDialogBox(self, _("Edit Attribute"),DECOR_TITLE|DECOR_BORDER)
frame = FXVerticalFrame(dialog, LAYOUT_SIDE_TOP|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y|PACK_UNIFORM_WIDTH)
matrix = FXMatrix(frame, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("New name:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
nametf = FXTextField(matrix, 20)
nametf.setText(name)
FXLabel(matrix, _("New value:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
valuetf = FXTextField(matrix, 20)
valuetf.setText(value)
f = FXHorizontalFrame(frame)
FXButton(f, _("&Ok"), None, dialog, FXDialogBox.ID_ACCEPT,FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
FXButton(f, _("&Cancel"), None, dialog, FXDialogBox.ID_CANCEL,FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
if dialog.execute():
newname = string.lower(string.strip(nametf.getText()))
try:
import re
value = string.strip(valuetf.getText())
value = re.compile(value)
except:
import sys
error(_("Invalid regex %s: %s")%(value,sys.exc_info()[1]))
return 1
del self.rule.attrs[name]
self.rule.attrs[newname] = value
self.getApp().dirty = 1
self.iconlist.replaceItem(index, newname+"\t"+value.pattern)
debug("Changed rule attribute")
return 1
def onCmdAttributeRemove(self, sender, sel, ptr):
index = self.iconlist.getCurrentItem()
if index < 0: return 1
item = self.iconlist.retrieveItem(index)
if not item.isSelected(): return 1
name,value = string.split(item.getText(), '\t')
del self.rule.attrs[name]
self.getApp().dirty = 1
self.iconlist.removeItem(index)
debug("Removed rule attribute")
return 1
def onCmdReplaceValue(self, sender, sel, ptr):
self.rule.replace[1] = sender.getText()
self.getApp().dirty = 1
debug("Changed rule replace value")
return 1
class FXAllowRuleFrame(FXRuleFrame):
"""display all variables found in an AllowRule"""
(ID_SCHEME,
ID_HOST,
ID_PORT,
ID_PATH,
ID_PARAMETERS,
ID_QUERY,
ID_FRAGMENT,
ID_LAST,
) = range(FXRuleFrame.ID_LAST, FXRuleFrame.ID_LAST+8)
def __init__(self, parent, rule, index):
FXRuleFrame.__init__(self, parent, rule, index)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_SCHEME,FXAllowRuleFrame.onCmdScheme)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_HOST,FXAllowRuleFrame.onCmdHost)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_PORT,FXAllowRuleFrame.onCmdPort)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_PATH,FXAllowRuleFrame.onCmdPath)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_PARAMETERS,FXAllowRuleFrame.onCmdParameters)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_QUERY,FXAllowRuleFrame.onCmdQuery)
FXMAPFUNC(self,SEL_COMMAND,FXAllowRuleFrame.ID_FRAGMENT,FXAllowRuleFrame.onCmdFragment)
self.matrix = FXMatrix(self, 2, MATRIX_BY_COLUMNS)
FXLabel(self.matrix, _("URL Scheme:\tRegular expression to match the URL scheme"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_SCHEME)
tf.setText(self.rule.scheme)
FXLabel(self.matrix, _("URL Host:\tRegular expression to match the host"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_HOST)
tf.setText(self.rule.host)
FXLabel(self.matrix, _("URL Port:\tRegular expression to match the port"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_PORT)
tf.setText(self.rule.port)
FXLabel(self.matrix, _("URL Path:\tRegular expression to match the path"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_PATH)
tf.setText(self.rule.path)
FXLabel(self.matrix, _("URL Parameters:\tRegular expression to match the parameters"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_PARAMETERS)
tf.setText(self.rule.parameters)
FXLabel(self.matrix, _("URL Query:\tRegular expression to match the query"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_QUERY)
tf.setText(self.rule.query)
FXLabel(self.matrix, _("URL Fragment:\tRegular expression to match the fragment"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXAllowRuleFrame.ID_FRAGMENT)
tf.setText(self.rule.fragment)
def onCmdScheme(self, sender, sel, ptr):
self.rule.scheme = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule scheme")
return 1
def onCmdHost(self, sender, sel, ptr):
self.rule.host = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule host")
return 1
def onCmdPort(self, sender, sel, ptr):
self.rule.host = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule port")
return 1
def onCmdPath(self, sender, sel, ptr):
self.rule.path = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule path")
return 1
def onCmdParameters(self, sender, sel, ptr):
self.rule.parameters = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule parameters")
return 1
def onCmdQuery(self, sender, sel, ptr):
self.rule.query = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule query")
return 1
def onCmdFragment(self, sender, sel, ptr):
self.rule.fragment = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule fragment")
return 1
class FXBlockRuleFrame(FXAllowRuleFrame):
"""display all variables found in a BlockRule"""
ID_URL = FXAllowRuleFrame.ID_LAST
def __init__(self, parent, rule, index):
FXAllowRuleFrame.__init__(self, parent, rule, index)
FXMAPFUNC(self,SEL_COMMAND,FXBlockRuleFrame.ID_URL,FXBlockRuleFrame.onCmdUrl)
FXLabel(self.matrix, _("Blocked URL\tThe URL we want to show instead"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(self.matrix, 15, self, FXBlockRuleFrame.ID_URL)
tf.setText(self.rule.fragment)
def onCmdUrl(self, sender, sel, ptr):
self.rule.url = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule blocked url")
return 1
class FXHeaderRuleFrame(FXRuleFrame):
"""display all variables found in a HeaderRule"""
(ID_NAME,
ID_VALUE,
) = range(FXRuleFrame.ID_LAST, FXRuleFrame.ID_LAST+2)
def __init__(self, parent, rule, index):
FXRuleFrame.__init__(self, parent, rule, index)
FXMAPFUNC(self,SEL_COMMAND,FXHeaderRuleFrame.ID_NAME,FXHeaderRuleFrame.onCmdName)
FXMAPFUNC(self,SEL_COMMAND,FXHeaderRuleFrame.ID_VALUE,FXHeaderRuleFrame.onCmdValue)
matrix = FXMatrix(self, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Header name:\tRegular expression to match the header name"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(matrix, 15, self, FXHeaderRuleFrame.ID_NAME)
tf.setText(self.rule.name)
FXLabel(matrix, _("Header value:\tHeader value (empty means delete)"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
tf = FXTextField(matrix, 15, self, FXHeaderRuleFrame.ID_VALUE)
tf.setText(self.rule.value)
def onCmdName(self, sender, sel, ptr):
self.rule.name = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule header name")
return 1
def onCmdValue(self, sender, sel, ptr):
self.rule.value = string.strip(sender.getText())
self.getApp().dirty = 1
debug("Changed rule header value")
return 1
class FXImageRuleFrame(FXRuleFrame):
"""display all variables found in a ImageRule"""
(ID_WIDTH,
ID_HEIGHT,
) = range(FXRuleFrame.ID_LAST, FXRuleFrame.ID_LAST+2)
def __init__(self, parent, rule, index):
FXRuleFrame.__init__(self, parent, rule, index)
FXMAPFUNC(self,SEL_COMMAND,FXImageRuleFrame.ID_WIDTH,FXImageRuleFrame.onCmdWidth)
FXMAPFUNC(self,SEL_COMMAND,FXImageRuleFrame.ID_HEIGHT,FXImageRuleFrame.onCmdHeight)
matrix = FXMatrix(self, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Image width:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXSpinner(matrix, 4, self, FXImageRuleFrame.ID_WIDTH, SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK)
t.setRange(0,65535)
t.setValue(self.rule.width)
FXLabel(matrix, _("Image height:"), opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXSpinner(matrix, 4, self, FXImageRuleFrame.ID_HEIGHT, SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK)
t.setRange(0,65535)
t.setValue(self.rule.height)
def onCmdWidth(self, sender, sel, ptr):
self.rule.width = sender.getValue()
self.getApp().dirty = 1
debug("Changed rule image width")
return 1
def onCmdHeight(self, sender, sel, ptr):
self.rule.height = sender.getValue()
self.getApp().dirty = 1
debug("Changed rule image height")
return 1
class FXNocommentRuleFrame(FXRuleFrame):
"""display all variables found in a NocommentRule"""
pass
class FXFilterRulesFrame(FXRuleFrame):
"""display all variables found in a FilterRules rule"""
(ID_LANG,
ID_FILENAME,
) = range(FXRuleFrame.ID_LAST, FXRuleFrame.ID_LAST+2)
def __init__(self, parent, rule, index):
FXRuleFrame.__init__(self, parent, rule, index)
FXMAPFUNC(self,SEL_COMMAND,FXFilterRulesFrame.ID_FILENAME,FXFilterRulesFrame.onCmdFilename)
FXMAPFUNC(self,SEL_COMMAND,FXFilterRulesFrame.ID_LANG,FXFilterRulesFrame.onCmdLang)
matrix = FXMatrix(self, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Filename")+":\t"+TipText['Filename'], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXTextField(matrix, 22, self, FXFilterRulesFrame.ID_FILENAME)
t.setText(self.rule.filename)
t.setEditable(0)
FXLabel(matrix, _("Language")+":\t"+TipText['Language'], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
t = FXComboBox(matrix,5,3,self, self.ID_LANG,opts=COMBOBOX_INSERT_LAST|FRAME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_TOP)
t.appendItem("All")
t.appendItem("Perl")
t.appendItem("Python")
t.setEditable(0)
t.setCurrentItem(lang_num(self.rule.lang))
def onCmdFilename(self, sender, sel, ptr):
filename = string.strip(sender.getText())
if filename:
self.rule.filename = filename
self.getApp().dirty = 1
debug("Changed rule filename")
else:
error(_("empty filename"))
return 1
def onCmdLang(self, sender, sel, ptr):
self.rule.lang = num_lang(sender.getCurrentItem())
self.getApp().dirty = 1
debug("Changed rule language")
return 1
class ConfWindow(FXMainWindow):
"""The main window holds all data and windows to display"""
(ID_PORT,
ID_DEBUGLEVEL,
ID_FILTERMODULE,
ID_PARENTPROXY,
ID_PARENTPROXYPORT,
ID_ACCEPT,
ID_CANCEL,
ID_APPLY,
ID_BUFFERSIZE,
ID_TIMEOUT,
ID_OBFUSCATEIP,
ID_LOGFILE,
ID_ABOUT,
ID_HELP,
ID_TITLE,
ID_FILTER,
ID_NEWFOLDER,
ID_NEWFILTER,
ID_REMOVE,
ID_PROXYSTART,
ID_PROXYSTOP,
ID_CONFUPDATE,
ID_PROXYRESTART,
ID_PROXYRELOAD,
ID_PROXYSTATUS,
) = range(FXMainWindow.ID_LAST, FXMainWindow.ID_LAST+25)
def __init__(self, app):
FXMainWindow.__init__(self, app, "WebCleanerConf",w=640,h=500)
self.loadIcons(app)
self.readconfig()
self.eventMap()
FXTooltip(app, TOOLTIP_VARIABLE, 0, 0)
FXStatusbar(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER)
# About dialog
self.about = FXMessageBox(self, _("About WebCleaner"),"WebCleanerConf %s\n\n(C) 2000,2001 by %s" % (WebCleanerConf.version,WebCleanerConf.author), self.getIcon(),MBOX_OK)
self.help = FXMessageBox(self, _("WebCleanerConf Help"), HelpText, None, MBOX_OK)
self.removeDialog = FXMessageBox(self, _("Remove Folder"), RemoveText, None, MBOX_OK)
# main frame
mainframe = FXVerticalFrame(self, LAYOUT_FILL_X|LAYOUT_FILL_Y)
tabbook = FXTabBook(mainframe, None, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y)
self.proxySettings(tabbook)
self.filterSettings(tabbook)
# Buttons
frame = FXHorizontalFrame(mainframe, LAYOUT_FILL_X)
FXButton(frame, _(" &Ok "), None, self, self.ID_ACCEPT)
FXButton(frame, _("&Cancel"), None, self, self.ID_CANCEL)
FXButton(frame, _("A&pply"), None, self, self.ID_APPLY)
FXButton(frame, _("A&bout"), None, self, self.ID_ABOUT, opts=FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT)
FXButton(frame, _("&Help"), None, self, self.ID_HELP, opts=FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT)
daemonmenu = FXMenuPane(self)
FXMenuCommand(daemonmenu, "Start", None, self, self.ID_PROXYSTART)
FXMenuCommand(daemonmenu, "Stop", None, self, self.ID_PROXYSTOP)
FXMenuCommand(daemonmenu, "Restart", None, self, self.ID_PROXYRESTART)
FXMenuCommand(daemonmenu, "Reload", None, self, self.ID_PROXYRELOAD)
FXMenuCommand(daemonmenu, "Status", None, self, self.ID_PROXYSTATUS)
FXMenuButton(frame, "Proxy", None, daemonmenu, MENUBUTTON_ATTACH_BOTH|MENUBUTTON_DOWN|JUSTIFY_HZ_APART|LAYOUT_TOP|FRAME_RAISED|FRAME_THICK|ICON_AFTER_TEXT)
FXButton(frame, "Update...", None, self, self.ID_CONFUPDATE)
def create(self):
"""create the main window and show myself on the screen"""
FXMainWindow.create(self)
self.show()
def loadIcons(self, app):
"""load the needed icons"""
self.setIcon(loadIcon(app, 'iconbig.gif'))
self.icon_open = loadIcon(app, 'minifolderopen.gif')
self.icon_closed = loadIcon(app, 'minifolder.gif')
self.icon_doc = loadIcon(app, 'minidoc.gif')
def eventMap(self):
"""attach all events to (member) functions"""
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_ACCEPT,ConfWindow.onCmdAccept)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_CANCEL,ConfWindow.onCmdCancel)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_APPLY,ConfWindow.onCmdApply)
FXMAPFUNC(self,SEL_UPDATE,ConfWindow.ID_APPLY,ConfWindow.onUpdApply)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_ABOUT,ConfWindow.onCmdAbout)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_BUFFERSIZE,ConfWindow.onCmdBuffersize)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_TIMEOUT,ConfWindow.onCmdTimeout)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_OBFUSCATEIP,ConfWindow.onCmdObfuscateIp)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PORT,ConfWindow.onCmdPort)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_DEBUGLEVEL,ConfWindow.onCmdDebuglevel)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PARENTPROXY,ConfWindow.onCmdParentProxy)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PARENTPROXYPORT,ConfWindow.onCmdParentProxyPort)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_LOGFILE,ConfWindow.onCmdLogfile)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_FILTERMODULE,ConfWindow.onCmdFilterModule)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_FILTER,ConfWindow.onCmdFilter)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_HELP,ConfWindow.onCmdHelp)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_NEWFOLDER,ConfWindow.onCmdNewFolder)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_NEWFILTER,ConfWindow.onCmdNewFilter)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_REMOVE,ConfWindow.onCmdRemove)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_TITLE,ConfWindow.onCmdTitle)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PROXYSTART,ConfWindow.onCmdProxyStart)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PROXYSTOP,ConfWindow.onCmdProxyStop)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PROXYRESTART,ConfWindow.onCmdProxyRestart)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PROXYRELOAD,ConfWindow.onCmdProxyReload)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_PROXYSTATUS,ConfWindow.onCmdProxyStatus)
FXMAPFUNC(self,SEL_COMMAND,ConfWindow.ID_CONFUPDATE,ConfWindow.onCmdConfUpdate)
def proxySettings(self, tabbook):
"""generate the proxy setting tab"""
FXTabItem(tabbook, _("P&roxy Settings"), None)
proxy = FXVerticalFrame(tabbook, FRAME_THICK|FRAME_RAISED)
basics = FXGroupBox(proxy, _("Basic Values"), FRAME_RIDGE|LAYOUT_LEFT|LAYOUT_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y,0,0,0,0,5,5,5,5)
matrix = FXMatrix(basics, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Version"))
version = FXLabel(matrix, WebCleanerConf.version, opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
FXLabel(matrix, _("Port")+"\t"+TipText["Port"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
widget = FXSpinner(matrix, 4, self, self.ID_PORT, SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK)
widget.setRange(0,65535)
widget.setValue(self.port)
FXLabel(matrix, _("Logfile")+"\t"+TipText["Logfile"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
widget = FXTextField(matrix, 10, self, self.ID_LOGFILE)
widget.setText(self.logfile)
FXLabel(matrix, _("Buffer size")+"\t"+TipText["Buffer size"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
widget = FXSpinner(matrix, 3, self, self.ID_BUFFERSIZE, SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK)
widget.setRange(512,8192)
widget.setValue(self.buffersize)
FXLabel(matrix, _("Timeout")+"\t"+TipText["Timeout"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
widget = FXSpinner(matrix, 3, self, self.ID_TIMEOUT, SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK)
widget.setRange(1,600)
widget.setValue(self.timeout)
FXLabel(matrix, _("Obfuscate IP")+"\t"+TipText["Obfuscate IP"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
widget = FXCheckButton(matrix, None, self, self.ID_OBFUSCATEIP, opts=ICON_BEFORE_TEXT|LAYOUT_SIDE_TOP)
widget.setCheck(self.obfuscateip)
FXLabel(matrix, _("Debug level")+"\t"+TipText["Debug level"])
cols=0
d = FXComboBox(matrix,0,4,self, self.ID_DEBUGLEVEL,opts=COMBOBOX_INSERT_LAST|FRAME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_TOP)
for text in ["No debugging","Bring it on","Hurt me plenty","Nightmare"]:
text = _(text)
cols = max(len(text), cols)
d.appendItem(text)
d.setEditable(0)
# subtract 3 because acolumn is wider than text character
d.setNumColumns(cols-3)
d.setCurrentItem(self.debuglevel)
frame = FXHorizontalFrame(proxy, LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_SIDE_TOP)
filters = FXGroupBox(frame, _("Filter Modules"), FRAME_RIDGE|LAYOUT_LEFT|LAYOUT_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y,0,0,0,0,5,5,5,5)
hframe = FXVerticalFrame(filters,LAYOUT_SIDE_TOP)
for m in self.modules.keys():
cb = FXCheckButton(hframe, m+"\t"+TipText[m], self, self.ID_FILTERMODULE,opts=ICON_BEFORE_TEXT|LAYOUT_SIDE_TOP)
if self.modules[m]:
cb.setCheck()
groupbox = FXGroupBox(frame, _("Parent Proxy"), FRAME_RIDGE|LAYOUT_LEFT|LAYOUT_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y,0,0,0,0,5,5,5,5)
matrix = FXMatrix(groupbox, 2, MATRIX_BY_COLUMNS)
FXLabel(matrix, _("Host")+"\t"+TipText["Parent Proxy"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
parentproxy = FXTextField(matrix, 16, self, self.ID_PARENTPROXY)
parentproxy.setText(self.parentproxy)
FXLabel(matrix, _("Port")+"\t"+TipText["Parent Proxy Port"], opts=LAYOUT_CENTER_Y|LAYOUT_LEFT)
parentport = FXSpinner(matrix, 4, self, self.ID_PARENTPROXYPORT, SPIN_NORMAL|FRAME_SUNKEN|FRAME_THICK)
parentport.setRange(0,65535)
parentport.setValue(self.parentproxyport)
# proxySettings
def filterSettings(self, tabbook):
FXTabItem(tabbook, _("&Filter Settings"), None)
frame = FXHorizontalFrame(tabbook, FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_FILL_Y)
splitter = FXSplitter(frame, FRAME_THICK|FRAME_RAISED|LAYOUT_FILL_X|LAYOUT_FILL_Y)
treeframe = FXVerticalFrame(splitter, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,250,0, 0,0,0,0, 0,0)
self.tree = FXTreeList(treeframe, 0, self, self.ID_FILTER, opts=LAYOUT_FILL_X|LAYOUT_FILL_Y)
self.tree.setListStyle(TREELIST_SHOWS_LINES|TREELIST_SHOWS_BOXES|TREELIST_SINGLESELECT)
f = FXHorizontalFrame(treeframe, LAYOUT_FILL_X)
addmenu = FXMenuPane(self)
FXMenuCommand(addmenu, _("New Folder\t\tCreate a new folder."), None, self, self.ID_NEWFOLDER, opts=MENU_DEFAULT)
# Make new filter popup menu
filtermenu = FXMenuPane(self)
FXMenuCommand(filtermenu, "Allow", None, self, self.ID_NEWFILTER)
FXMenuCommand(filtermenu, "Block", None, self, self.ID_NEWFILTER)
FXMenuCommand(filtermenu, "Header", None, self, self.ID_NEWFILTER)
FXMenuCommand(filtermenu, "Image", None, self, self.ID_NEWFILTER)
FXMenuCommand(filtermenu, "Nocomments", None, self, self.ID_NEWFILTER)
FXMenuCommand(filtermenu, "Rewrite", None, self, self.ID_NEWFILTER)
FXMenuCascade(addmenu, _("New Filter"), None, filtermenu)
FXMenuButton(f, _("Add"), None, addmenu, MENUBUTTON_ATTACH_BOTH|MENUBUTTON_DOWN|JUSTIFY_HZ_APART|LAYOUT_TOP|FRAME_RAISED|FRAME_THICK|ICON_AFTER_TEXT)
FXButton(f, _("Remove"), None, self, self.ID_REMOVE)
self.filterswitcher = FXSwitcher(splitter, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y)
topItem = FXTreeItem('Filter', self.icon_open, self.icon_closed)
topItem.setData(0)
f=FXHorizontalFrame(self.filterswitcher)
FXLabel(f, _("All filters"))
self.topmost = self.tree.addItemLast(None, topItem)
self.tree.expandTree(self.topmost)
self.factory = FXRuleFrameFactory(self.filterswitcher)
for f in self.filters:
item = FXTreeItem(f.title, self.icon_open, self.icon_closed)
item.setData(f.fromFactory(self.factory).rule.index)
branch = self.tree.addItemLast(self.topmost, item)
for r in f.rules:
item = FXTreeItem(r.title, self.icon_doc, self.icon_doc)
item.setData(r.fromFactory(self.factory).rule.index)
self.tree.addItemLast(branch, item)
def newfilterid(self):
i=0
while 1:
if self.filterids.has_key(i):
i += 1
else:
break
return i
def onCmdNewFolder(self, sender, sel, ptr):
debug("new folder")
rule = FilterRules("No title","",0,0,tempfile.mktemp()+".zap")
rule.timestamp = time.time()
rule.ruleid = self.newfilterid()
self.filters.append(rule)
item = FXTreeItem(rule.title, self.icon_open, self.icon_closed)
frame = rule.fromFactory(self.factory)
frame.create()
item.setData(rule.index)
self.tree.addItemLast(self.topmost, item)
self.getApp().dirty = 1
return 1
def onCmdNewFilter(self, sender, sel, ptr):
debug("new filter")
if not self.filters: return 1
folder = self.tree.getCurrentItem()
debug("folder index %d" % folder.getData())
if folder.getData()==0:
folder = folder.getBelow()
elif not self.searchFilter(folder.getData()):
folder = folder.getParent()
debug("folder index %d" % folder.getData())
self.tree.expandTree(folder)
rule = GetRuleFromName(sender.getText())
rule.parent = self.searchFilter(folder.getData())
rule.timestamp = time.time()
rule.ruleid = rule.parent.newid()
rule.parent.timestamp = rule.timestamp
item = FXTreeItem(rule.title, self.icon_doc, self.icon_doc)
frame = rule.fromFactory(self.factory)
frame.create()
item.setData(rule.index)
rule.parent.append_rule(rule)
self.tree.addItemLast(folder, item)
self.getApp().dirty = 1
return 1
def searchFilter(self, index):
for f in self.filters:
if f.index == index:
return f
def onCmdTitle(self, sender, sel, ptr):
item = self.tree.getCurrentItem()
if item.isSelected():
self.tree.setItemText(item, sender.getText())
debug("updated tree item")
return 1
def onCmdRemove(self, sender, sel, ptr):
item = self.tree.getCurrentItem()
if item.isSelected():
if self.removeRule(item.getData()):
self.tree.removeItem(item)
self.filterswitcher.setCurrent(0)
self.getApp().dirty = 1
debug("removed filter")
else:
self.removeDialog.execute()
else:
error(_("no filter item selected"))
return 1
def onCmdAccept(self, sender, sel, ptr):
debug("Accept")
self.writeconfig()
self.getApp().exit(0)
return 1
def onCmdCancel(self, sender, sel, ptr):
debug("Cancel")
self.getApp().exit(0)
return 1
def onUpdApply(self, sender, sel, ptr):
if self.getApp().dirty:
sender.enable()
else:
sender.disable()
return 1
def onCmdApply(self, sender, sel, ptr):
debug("Apply")
self.writeconfig()
return 1
def onCmdAbout(self, sender, sel, ptr):
debug("About")
self.doShow(self.about)
return 1
def doShow(self, win):
win.execute(PLACEMENT_OWNER)
def onCmdHelp(self, sender, sel, ptr):
debug("Help")
self.doShow(self.help)
return 1
def onCmdPort(self, sender, sel, ptr):
self.port = sender.getValue()
self.getApp().dirty = 1
debug("Port=%d"%self.port)
return 1
def onCmdDebuglevel(self, sender, sel, ptr):
self.debuglevel = sender.getCurrentItem()
self.getApp().dirty = 1
debug("Debuglevel=%d"%self.debuglevel)
return 1
def onCmdBuffersize(self, sender, sel, ptr):
self.buffersize = sender.getValue()
self.getApp().dirty = 1
debug("Buffersize=%d" % self.buffersize)
return 1
def onCmdTimeout(self, sender, sel, ptr):
self.timeout = sender.getValue()
self.getApp().dirty = 1
debug("Timeout=%d" % self.timeout)
return 1
def onCmdObfuscateIp(self, sender, sel, ptr):
self.obfuscateip = sender.getCheck()
self.getApp().dirty = 1
debug("Obfuscateip=%d" % self.obfuscateip)
return 1
def onCmdParentProxy(self, sender, sel, ptr):
self.parentproxy = sender.getText()
self.getApp().dirty = 1
debug("Parentproxy=%s"%self.parentproxy)
return 1
def onCmdParentProxyPort(self, sender, sel, ptr):
self.parentproxyport = sender.getValue()
self.getApp().dirty = 1
debug("Parentproxyport=%d"%self.parentproxyport)
return 1
def onCmdLogfile(self, sender, sel, ptr):
self.logfile = sender.getText()
self.getApp().dirty = 1
debug("Logfile=%s"%self.logfile)
return 1
def onCmdFilterModule(self, sender, sel, ptr):
state = sender.getCheck()
module = sender.getText()
self.modules[module] = state
self.getApp().dirty = 1
debug("Filtermodule %s = %d" % (module, state))
return 1
def onCmdFilter(self, sender, sel, ptr):
if not ptr.isSelected(): return 1
index = ptr.getData()
debug("tree item index %d" % index)
if type(index) is IntType:
self.filterswitcher.setCurrent(index)
return 1
def onCmdProxyStart(self, sender, sel, ptr):
from webfilter import daemon,startfunc
try:
daemon.start(startfunc)
except SystemExit:
# parent does not exit
pass
debug("webcleaner start")
return 1
def onCmdProxyStop(self, sender, sel, ptr):
from webfilter import daemon
daemon.stop()
debug("webcleaner stop")
return 1
def onCmdProxyRestart(self, sender, sel, ptr):
from webfilter import daemon,startfunc
try:
daemon.restart(startfunc)
except SystemExit:
# parent does not exit
pass
debug("webcleaner restart")
return 1
def onCmdProxyReload(self, sender, sel, ptr):
from webfilter import daemon
daemon.reload()
debug("webcleaner reload")
return 1
def onCmdProxyStatus(self, sender, sel, ptr):
from webfilter import daemon
dialog = FXMessageBox(self,_("Proxy Status"),daemon.status(),None,MBOX_OK)
self.doShow(dialog)
debug("webcleaner status")
return 1
def onCmdConfUpdate(self, sender, sel, ptr):
"""download files from http://webcleaner.sourceforge.net/zappers/
and merge them into existing config"""
# XXX remove when stuff is implemented
dialog = FXMessageBox(self,_("Unimplemented"),_("Sorry, unimplemented"),None,MBOX_OK)
self.doShow(dialog)
return 1
# base url for all files
#url = "http://webcleaner.sourceforge.net/zappers/"
url = "file:/home/calvin/temp/"
import urllib
try:
data = urllib.urlopen(url+"oldtimestamps.txt").readlines()
data = map(string.split, data)
self.oldtimestamps = {}
for ruleid,timestamp in data:
self.oldtimestamps[ruleid] = timestamp
for file in urllib.urlopen(url+"list.txt").readlines():
filename, info = urllib.urlretrieve(url+file)
debug("merging "+file)
self.mergeFileData(filename, data)
except IOError, msg:
dialog = FXMessageBox(self,_("Update Error"),_("Update Error: %s") % msg,None,MBOX_OK)
self.doShow(dialog)
return 1
def mergeFileData(self, filename):
dom = get_dom(filename)
attrs = get_dom_attrs(dom)
# check filestamp: if they differ, merge; else do nothing
timestamp = long(get_attr(attrs, 'time'))
ruleid = int(get_attr(attrs, 'id'))
if self.oldtimestamps[ruleid] == timestamp:
debug("filter %s not modified" % filename)
return
debug("merging filter %s (ID%d)" % (filename,ruleid))
# XXX
def removeRule(self, index):
for f in self.filters:
for i in range(len(f.rules)):
if f.rules[i].index == index:
f.delete_rule(i)
return 1
return 0
def readconfig(self):
"""read the configuration from disc"""
self.configfile = os.path.join(WebCleanerConf.config_dir, "webcleaner.conf")
dom = get_dom(self.configfile)
attrs = get_dom_attrs(dom)
version = WebCleanerConf.version
self.version = get_attr(attrs, 'version')
if self.version > version:
raise SystemExit, _("Wrong config file version %s, I only understand <= %s") % (self.version, version)
self.port = int(get_attr(attrs, 'port'))
self.buffersize = int(get_attr(attrs, 'buffersize'))
self.parentproxyport = int(get_attr(attrs, 'parentproxyport'))
self.debuglevel = int(get_attr(attrs, 'debuglevel'))
self.parentproxy = get_attr(attrs, 'parentproxy')
self.logfile = get_attr(attrs, 'logfile')
self.timeout = int(get_attr(attrs, 'timeout'))
self.obfuscateip = int(get_attr(attrs, 'obfuscateip'))
self.modules = {"Header":0, "Blocker":0, "GifImage":0, "BinaryCharFilter":0,"Rewriter":0, "Compress":0,}
nodes = dom.getElementsByTagName('filter')
for n in nodes:
self.modules[get_node_attr(n, 'name')] = 1
from glob import glob
files = glob(WebCleanerConf.config_dir+"/*.zap")
self.filters = []
self.filterids = {}
# read filter files
for file in files:
try:
open(file)
except IOError:
error(_("can not read file %s") % file)
continue
dom = get_dom(file)
attrs = get_dom_attrs(dom)
title = get_attr(attrs, 'title')
desc = get_attr(attrs, 'desc')
disable = int(get_attr(attrs, 'disable'))
lang = get_attr(attrs, 'lang')
timestamp = long(get_attr(attrs, 'time'))
ruleid = int(get_attr(attrs, 'id'))
item = FilterRules(title, desc, disable, lang, file, timestamp, ruleid)
# sort the filter list by title
self.filters.append(item)
self.filterids[ruleid] = 1
self.append_rules(dom, item)
def writeconfig(self):
"""write the current configuration to disc"""
dirty = 0
try:
file = open(self.configfile, 'w')
file.write(self.toxml())
file.close()
except IOError:
error(_("can not write to file %s") % configfile)
dirty = 1
for f in self.filters:
try:
file = open(f.filename, 'w')
file.write(f.toxml())
file.close()
except IOError:
error(_("can not write to file %s") % f.filename)
dirty = 1
self.getApp().dirty = dirty
def toxml(self):
s = """<?xml version="1.0"?>
<!DOCTYPE webcleaner SYSTEM "webcleaner.dtd">
<webcleaner
"""
s += ' version="%s"\n' % self.version +\
' port="%d"\n' % self.port
if self.parentproxy:
s += ' parentproxy="%s"\n' % self.parentproxy
s += ' parentproxyport="%d"\n' % self.parentproxyport +\
' buffersize="%d"\n' % self.buffersize +\
' timeout="%d"\n' % self.timeout +\
' obfuscateip="%d"\n' % self.obfuscateip +\
' debuglevel="%d"\n' % self.debuglevel
if self.logfile:
s += ' logfile="%s"\n' % self.logfile
s += '>\n'
for key,val in self.modules.items():
if val:
s += '<filter name="%s"/>\n' % key
return s + '</webcleaner>\n'
def append_rules(self, dom, item):
for name in Filternames:
for node in dom.getElementsByTagName(name):
item.append_rule(GetRuleFromNode(node))
item.sort()
if __name__=='__main__':
app = FXApp("WebCleanerConf", "Die Kampfwurst")
app.init(sys.argv)
# dirty flag for the apply button
app.dirty = 0
# set default color scheme
app.baseColor = FXRGB(141,157,170)
app.backColor = FXRGB(211,223,232)
app.hiliteColor = FXRGB(226,225,220)
app.shadowColor = FXRGB(158,165,191)
app.borderColor = FXRGB(86,90,104)
app.selbackColor = FXRGB(20,58,84)
# create and run
ConfWindow(app)
app.create()
app.run()