about summary refs log tree commit diff stats
path: root/src/io/urlfilter.nim
blob: 457d79f8741553d7f1363232944129969ee5be07 (plain) (blame)
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
import std/options

import js/regex
import types/url

#TODO add denyhost/s for blocklists
type URLFilter* = object
  scheme: Option[string]
  allowschemes: seq[string]
  allowhost*: Option[string]
  allowhosts: seq[Regex]
  default: bool

proc newURLFilter*(scheme = none(string), allowschemes: seq[string] = @[],
    allowhost = none(string), allowhosts: seq[Regex] = @[],
    default = false): URLFilter =
  doAssert scheme.isSome or allowschemes.len == 0,
    "allowschemes without scheme is not supported"
  return URLFilter(
    scheme: scheme,
    allowschemes: allowschemes,
    allowhost: allowhost,
    allowhosts: allowhosts,
    default: default
  )

# Filters as follows:
# If scheme/s are given, only URLs with the same scheme are matched.
# Then, allowhost and allowhosts are checked; if none of these match the host,
# the function returns the value of `default'.
proc match*(filter: URLFilter, url: URL): bool =
  block check_scheme:
    if filter.scheme.isSome and filter.scheme.get != url.scheme:
      for scheme in filter.allowschemes:
        if scheme == url.scheme:
          break check_scheme
      return false
  let host = url.host
  if filter.allowhost.isSome and filter.allowhost.get == host:
    return true
  for regex in filter.allowhosts:
    if regex.match(host):
      return true
  return filter.default
class="w"> } } cmd := args[optind:] if len(cmd) == 0 { return errors.New("Usage: pipe [-mp] <cmd> [args...]") } provider := aerc.SelectedTab().(widgets.ProvidesMessage) if !pipeFull && !pipePart { if _, ok := provider.(*widgets.MessageViewer); ok { pipePart = true } else if _, ok := provider.(*widgets.AccountView); ok { pipeFull = true } else { return errors.New( "Neither -m nor -p specified and cannot infer default") } } if pipeFull { store := provider.Store() msg := provider.SelectedMessage() store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) { term, err := commands.QuickTerm(aerc, cmd, reader) if err != nil { aerc.PushError(" " + err.Error()) return } name := cmd[0] + " <" + msg.Envelope.Subject aerc.NewTab(term, name) }) } else if pipePart { p := provider.SelectedMessagePart() p.Store.FetchBodyPart(p.Msg.Uid, p.Index, func(reader io.Reader) { // email parts are encoded as 7bit (plaintext), quoted-printable, or base64 if strings.EqualFold(p.Part.Encoding, "base64") { reader = base64.NewDecoder(base64.StdEncoding, reader) } else if strings.EqualFold(p.Part.Encoding, "quoted-printable") { reader = quotedprintable.NewReader(reader) } term, err := commands.QuickTerm(aerc, cmd, reader) if err != nil { aerc.PushError(" " + err.Error()) return } name := fmt.Sprintf("%s <%s/[%d]", cmd[0], p.Msg.Envelope.Subject, p.Index) aerc.NewTab(term, name) }) } return nil }