This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 14th, 2012  |  syntax: None  |  size: 5.85 KB  |  views: 43  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ------------------------------------------------------------------------
  2. -- Simple URI-based content filter                                    --
  3. -- (C) 2010 Chris van Dijk (quigybo) <quigybo@hotmail.com>            --
  4. -- (C) 2010 Mason Larobina (mason-l) <mason.larobina@gmail.com>       --
  5. --                                                                    --
  6. -- Download an Adblock Plus compatible filter list to $XDG_DATA_HOME  --
  7. -- and update the "filterfiles" entry below. EasyList is the most     --
  8. -- popular Adblock Plus filter list: http://easylist.adblockplus.org/ --
  9. -- Filterlists need to be updated regularly (~weekly), use cron!      --
  10. ------------------------------------------------------------------------
  11.  
  12. local info = info
  13. local ipairs = ipairs
  14. local io = io
  15. local os = os
  16. local string = string
  17. local table = table
  18. local tostring = tostring
  19. local webview = webview
  20. local lousy = require "lousy"
  21. local capi = { luakit = luakit }
  22.  
  23. module("adblock")
  24.  
  25. --- Module global variables
  26. local enabled = true
  27. -- Adblock Plus compatible filter lists
  28. local filterfiles = { capi.luakit.data_dir .. "/easylist.txt" }
  29. -- String patterns to filter URI's with
  30. local whitelist = {}
  31. local blacklist = {}
  32. -- Functions to filter URI's by
  33. -- Return true or false to allow or block respectively, nil to continue matching
  34. local filterfuncs = {}
  35.  
  36. -- Enable or disable filtering
  37. enable = function ()
  38.     enabled = true
  39. end
  40. disable = function ()
  41.     enabled = false
  42. end
  43.  
  44. -- Convert Adblock Plus filter description to lua string pattern
  45. -- See http://adblockplus.org/en/filters for more information
  46. abp_to_pattern = function (s)
  47.     -- Strip filter options
  48.     local opts
  49.     local pos = string.find(s, "%$")
  50.     if pos then
  51.         s, opts = string.sub(s, 0, pos-1), string.sub(s, pos+1)
  52.     end
  53.  
  54.     -- Protect magic characters (^$()%.[]*+-?) not used by ABP (^$()[]*)
  55.     s = string.gsub(s, "([%%%.%+%-%?])", "%%%1")
  56.  
  57.     -- Wildcards are globbing
  58.     s = string.gsub(s, "%*", "%.%*")
  59.  
  60.     -- Caret is separator (anything but a letter, a digit, or one of the following: - . %)
  61.     s = string.gsub(s, "%^", "[^%%w%%-%%.%%%%]")
  62.  
  63.     -- Double pipe is domain anchor (beginning only)
  64.     -- Unfortunately "||example.com" will also match "wexample.com" (lua doesn't do grouping)
  65.     s = string.gsub(s, "^||", "^https?://w?w?w?%%d?%.?")
  66.  
  67.     -- Pipe is anchor
  68.     s = string.gsub(s, "^|", "%^")
  69.     s = string.gsub(s, "|$", "%$")
  70.  
  71.     -- Convert to lowercase ($match-case option is not honoured)
  72.     s = string.lower(s)
  73.  
  74.     return s
  75. end
  76.  
  77. -- Parses an Adblock Plus compatible filter list
  78. parse_abpfilterlist = function (filename)
  79.     if os.exists(filename) then
  80.         info("adblock: loading filterlist %s", filename)
  81.     else
  82.         info("adblock: error loading filter list (%s: No such file or directory)", filename)
  83.     end
  84.     local pat
  85.     local white, black = {}, {}
  86.     for line in io.lines(filename) do
  87.         -- Ignore comments, header and blank lines
  88.         if line:match("^[![]") or line:match("^$") then
  89.             -- dammitwhydoesntluahaveacontinuestatement
  90.  
  91.         -- Ignore element hiding
  92.         elseif line:match("#") then
  93.  
  94.         -- Check for exceptions (whitelist)
  95.         elseif line:match("^@@") then
  96.             pat = abp_to_pattern(string.sub(line, 3))
  97.             if pat == "^http://" then io.stdout:write("White: " .. line .. " into " .. pat .. "\n") end
  98.             if pat then table.insert(white, pat) end
  99.  
  100.         -- Add everything else to blacklist
  101.         else
  102.             pat = abp_to_pattern(line)
  103.             if pat == "^http:" then io.stdout:write("Black: " .. line .. " into " .. pat .. "\n") end
  104.             if pat then table.insert(black, pat) end
  105.         end
  106.         end
  107.  
  108.     return white, black
  109. end
  110.  
  111. -- Load filter list files
  112. load = function ()
  113.     for _, filename in ipairs(filterfiles) do
  114.         local white, black = parse_abpfilterlist(filename)
  115.         whitelist = lousy.util.table.join(whitelist or {}, white)
  116.         blacklist = lousy.util.table.join(blacklist or {}, black)
  117.     end
  118. end
  119.  
  120. -- Tests URI against user-defined filter functions, then whitelist, then blacklist
  121. match = function (uri, signame)
  122.     -- Matching is not case sensitive
  123.     uri = string.lower(uri)
  124.     signame = signame or ""
  125.  
  126.     -- Test uri against filterfuncs
  127.     for _, func in ipairs(filterfuncs) do
  128.         local ret = func(uri)
  129.         if ret ~= nil then
  130.             info("adblock: filter function %s returned %s to uri %s", tostring(func), tostring(ret), uri)
  131.             return ret
  132.         end
  133.     end
  134.  
  135.     -- Check for a match to whitelist
  136.     for _, pattern in ipairs(whitelist) do
  137.         if string.match(uri, pattern) then
  138.             info("adblock: allowing %q as pattern %q matched to uri %s", signame, pattern, uri)
  139.             io.stdout:write ("Allowed " .. signame .. " as pattern " .. pattern .. " matched to uri " .. uri .. "\n")
  140.             return true
  141.         end
  142.     end
  143.  
  144.     -- Check for a match to blacklist
  145.     for _, pattern in ipairs(blacklist) do
  146.         if string.match(uri, pattern) then
  147.             info("adblock: blocking %q as pattern %q matched to uri %s", signame, pattern, uri)
  148.             io.stdout:write ("Blocked " .. signame .. " as pattern " .. pattern .. " matched to uri " .. uri .. "\n")
  149.             return false
  150.         end
  151.     end
  152. end
  153.  
  154. -- Direct requests to match function
  155. filter = function (v, uri, signame)
  156.     if enabled then return match(uri, signame or "") end
  157. end
  158.  
  159. -- Connect signals to all webview widgets on creation
  160. webview.init_funcs.adblock_signals = function (view, w)
  161.     view:add_signal("navigation-request",        function (v, uri) return filter(v, uri, "navigation-request")        end)
  162.     view:add_signal("resource-request-starting", function (v, uri) return filter(v, uri, "resource-request-starting") end)
  163. end
  164.  
  165. -- Initialise module
  166. load()
clone this paste RAW Paste Data