aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins
diff options
context:
space:
mode:
authorGravatar keis <keijser@gmail.com>2011-01-01 03:24:14 +0100
committerGravatar keis <keijser@gmail.com>2011-01-01 03:24:14 +0100
commit484c060355bd44abba5d96261411f513bda48dbf (patch)
treeaf9e574728a1473f8c98e9522aaa85ef98db3fd8 /examples/data/plugins
parent7cf3fb88d1e094da5b516699a61d5e589c039e8f (diff)
parent8c8bd0fbd5644d91ff3cdd2b13c07f28e7e0f716 (diff)
Merge branch 'experimental' of git://github.com/Dieterbe/uzbl into history
Diffstat (limited to 'examples/data/plugins')
-rw-r--r--examples/data/plugins/cookies.py176
-rw-r--r--examples/data/plugins/downloads.py69
2 files changed, 245 insertions, 0 deletions
diff --git a/examples/data/plugins/cookies.py b/examples/data/plugins/cookies.py
new file mode 100644
index 0000000..c9fe2c3
--- /dev/null
+++ b/examples/data/plugins/cookies.py
@@ -0,0 +1,176 @@
+""" Basic cookie manager
+ forwards cookies to all other instances connected to the event manager"""
+
+from collections import defaultdict
+import os, re
+
+# these are symbolic names for the components of the cookie tuple
+symbolic = {'domain': 0, 'path':1, 'name':2, 'value':3, 'scheme':4, 'expires':5}
+
+_splitquoted = re.compile("( |\\\".*?\\\"|'.*?')")
+def splitquoted(text):
+ return [str(p.strip('\'"')) for p in _splitquoted.split(text) if p.strip()]
+
+# allows for partial cookies
+# ? allow wildcard in key
+def match(key, cookie):
+ for k,c in zip(key,cookie):
+ if k != c:
+ return False
+ return True
+
+class NullStore(object):
+ def add_cookie(self, rawcookie, cookie):
+ pass
+
+ def delete_cookie(self, rkey, key):
+ pass
+
+class ListStore(list):
+ def add_cookie(self, rawcookie, cookie):
+ self.append(rawcookie)
+
+ def delete_cookie(self, rkey, key):
+ self[:] = [x for x in self if not match(key, splitquoted(x))]
+
+class TextStore(object):
+ def __init__(self, filename):
+ self.filename = filename
+
+ def as_event(self, cookie):
+ if cookie[0].startswith("#HttpOnly_"):
+ domain = cookie[0][len("#HttpOnly_"):]
+ elif cookie[0].startswith('#'):
+ return None
+ else:
+ domain = cookie[0]
+ return (domain,
+ cookie[2],
+ cookie[5],
+ cookie[6],
+ 'https' if cookie[3] == 'TRUE' else 'http',
+ cookie[4])
+
+ def as_file(self, cookie):
+ return (cookie[0],
+ 'TRUE' if cookie[0].startswith('.') else 'FALSE',
+ cookie[1],
+ 'TRUE' if cookie[4] == 'https' else 'FALSE',
+ cookie[5],
+ cookie[2],
+ cookie[3])
+
+ def add_cookie(self, rawcookie, cookie):
+ assert len(cookie) == 6
+
+ # delete equal cookies (ignoring expire time, value and secure flag)
+ self.delete_cookie(None, cookie[:-3])
+
+ first = not os.path.exists(self.filename)
+ with open(self.filename, 'a') as f:
+ if first:
+ print >> f, "# HTTP Cookie File"
+ print >> f, '\t'.join(self.as_file(cookie))
+
+ def delete_cookie(self, rkey, key):
+ if not os.path.exists(self.filename):
+ return
+
+ # read all cookies
+ with open(self.filename, 'r') as f:
+ cookies = f.readlines()
+
+ # write those that don't match the cookie to delete
+ with open(self.filename, 'w') as f:
+ for l in cookies:
+ c = self.as_event(l.split('\t'))
+ if c is None or not match(key, c):
+ print >> f, l,
+
+xdg_data_home = os.environ.get('XDG_DATA_HOME', os.path.join(os.environ['HOME'], '.local/share'))
+DefaultStore = TextStore(os.path.join(xdg_data_home, 'uzbl/cookies.txt'))
+SessionStore = TextStore(os.path.join(xdg_data_home, 'uzbl/session-cookies.txt'))
+
+def match_list(_list, cookie):
+ for component, match in _list:
+ if match(cookie[component]) is not None:
+ return True
+ return False
+
+# accept a cookie only when:
+# a. there is no whitelist and the cookie is in the blacklist
+# b. the cookie is in the whitelist and not in the blacklist
+def accept_cookie(uzbl, cookie):
+ if uzbl.cookie_whitelist:
+ if match_list(uzbl.cookie_whitelist, cookie):
+ return not match_list(uzbl.cookie_blacklist, cookie)
+ return False
+
+ return not match_list(uzbl.cookie_blacklist, cookie)
+
+def expires_with_session(uzbl, cookie):
+ return cookie[5] == ''
+
+def get_recipents(uzbl):
+ """ get a list of Uzbl instances to send the cookie too. """
+ # This could be a lot more interesting
+ return [u for u in uzbl.parent.uzbls.values() if u is not uzbl]
+
+def get_store(uzbl, session=False):
+ if session:
+ return SessionStore
+ return DefaultStore
+
+def add_cookie(uzbl, cookie):
+ splitted = splitquoted(cookie)
+ if accept_cookie(uzbl, splitted):
+ for u in get_recipents(uzbl):
+ u.send('add_cookie %s' % cookie)
+
+ get_store(uzbl, expires_with_session(uzbl, splitted)).add_cookie(cookie, splitted)
+ else:
+ logger.debug('cookie %r is blacklisted' % splitted)
+ uzbl.send('delete_cookie %s' % cookie)
+
+def delete_cookie(uzbl, cookie):
+ for u in get_recipents(uzbl):
+ u.send('delete_cookie %s' % cookie)
+
+ splitted = splitquoted(cookie)
+ if len(splitted) == 6:
+ get_store(uzbl, expires_with_session(uzbl, splitted)).delete_cookie(cookie, splitted)
+ else:
+ for store in set([get_store(uzbl, session) for session in (True, False)]):
+ store.delete_cookie(cookie, splitted)
+
+# add a cookie matcher to a whitelist or a blacklist.
+# a matcher is a (component, re) tuple that matches a cookie when the
+# "component" part of the cookie matches the regular expression "re".
+# "component" is one of the keys defined in the variable "symbolic" above,
+# or the index of a component of a cookie tuple.
+def add_cookie_matcher(_list, arg):
+ component, regexp = splitquoted(arg)
+ try:
+ component = symbolic[component]
+ except KeyError:
+ component = int(component)
+ assert component <= 5
+ _list.append((component, re.compile(regexp).search))
+
+def blacklist(uzbl, arg):
+ add_cookie_matcher(uzbl.cookie_blacklist, arg)
+
+def whitelist(uzbl, arg):
+ add_cookie_matcher(uzbl.cookie_whitelist, arg)
+
+def init(uzbl):
+ connect_dict(uzbl, {
+ 'ADD_COOKIE': add_cookie,
+ 'DELETE_COOKIE': delete_cookie,
+ 'BLACKLIST_COOKIE': blacklist,
+ 'WHITELIST_COOKIE': whitelist
+ })
+ export_dict(uzbl, {
+ 'cookie_blacklist' : [],
+ 'cookie_whitelist' : []
+ })
diff --git a/examples/data/plugins/downloads.py b/examples/data/plugins/downloads.py
new file mode 100644
index 0000000..7bf32d7
--- /dev/null
+++ b/examples/data/plugins/downloads.py
@@ -0,0 +1,69 @@
+# this plugin does a very simple display of download progress. to use it, add
+# @downloads to your status_format.
+
+import os
+ACTIVE_DOWNLOADS = {}
+
+# after a download's status has changed this is called to update the status bar
+def update_download_section(uzbl):
+ global ACTIVE_DOWNLOADS
+
+ if len(ACTIVE_DOWNLOADS):
+ # add a newline before we list downloads
+ result = '&#10;downloads:'
+ for path in ACTIVE_DOWNLOADS:
+ # add each download
+ fn = os.path.basename(path)
+ progress, = ACTIVE_DOWNLOADS[path]
+
+ dl = " %s (%d%%)" % (fn, progress * 100)
+
+ # replace entities to make sure we don't break our markup
+ # (this could be done with an @[]@ expansion in uzbl, but then we
+ # can't use the &#10; above to make a new line)
+ dl = dl.replace("&", "&amp;").replace("<", "&lt;")
+ result += dl
+ else:
+ result = ''
+
+ # and the result gets saved to an uzbl variable that can be used in
+ # status_format
+ if uzbl.config.get('downloads', '') != result:
+ uzbl.config['downloads'] = result
+
+def download_started(uzbl, destination_path):
+ # add to the list of active downloads
+ global ACTIVE_DOWNLOADS
+ ACTIVE_DOWNLOADS[destination_path] = (0.0,)
+
+ # update the progress
+ update_download_section(uzbl)
+
+def download_progress(uzbl, args):
+ # parse the arguments
+ s = args.rindex(' ')
+ destination_path = args[:s]
+ progress = float(args[s+1:])
+
+ # update the progress
+ global ACTIVE_DOWNLOADS
+ ACTIVE_DOWNLOADS[destination_path] = (progress,)
+
+ # update the status bar variable
+ update_download_section(uzbl)
+
+def download_complete(uzbl, destination_path):
+ # remove from the list of active downloads
+ global ACTIVE_DOWNLOADS
+ del ACTIVE_DOWNLOADS[destination_path]
+
+ # update the status bar variable
+ update_download_section(uzbl)
+
+# plugin init hook
+def init(uzbl):
+ connect_dict(uzbl, {
+ 'DOWNLOAD_STARTED': download_started,
+ 'DOWNLOAD_PROGRESS': download_progress,
+ 'DOWNLOAD_COMPLETE': download_complete,
+ })