about summary refs log tree commit diff stats
path: root/506math.mu
blob: 40b8ccdb54d66d9dd5e372b385f0aa51b8a8433e (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
45
46
47
48
49
50
51
52
53
fn abs n: int -> _/eax: int {
  compare n, 0
  {
    break-if->=
    negate n
  }
  return n
}

fn sgn n: int -> _/eax: int {
  compare n, 0
  {
    break-if-<=
    return 1
  }
  {
    break-if->=
    return -1
  }
  return 0
}

fn shift-left-by n: int, bits: int -> _/eax: int {
  var i/eax: int <- copy bits
  {
    compare i, 0
    break-if-<=
    shift-left n, 1
    i <- decrement
    loop
  }
  return n
}

fn shift-right-by n: int, bits: int -> _/eax: int {
  var i/eax: int <- copy bits
  {
    compare i, 0
    break-if-<=
    shift-right n, 1
    i <- decrement
    loop
  }
  return n
}

fn clear-lowest-bits _n: (addr int), bits: int {
  var dest/edi: (addr int) <- copy _n
  var n/eax: int <- copy *dest
  n <- shift-right-by n, bits
  n <- shift-left-by n, bits
  copy-to *dest, n
}
) switch method.Scheme { case "mailto": return unsubscribeMailto(aerc, method) case "http", "https": return unsubscribeHTTP(method) default: aerc.Logger().Printf("skipping unrecognized scheme: %s", method.Scheme) } } return errors.New("no supported unsubscribe methods found") } // parseUnsubscribeMethods reads the list-unsubscribe header and parses it as a // list of angle-bracket <> deliminated URLs. See RFC 2369. func parseUnsubscribeMethods(header string) (methods []*url.URL) { r := bufio.NewReader(strings.NewReader(header)) for { // discard until < _, err := r.ReadSlice('<') if err != nil { return } // read until < m, err := r.ReadSlice('>') if err != nil { return } m = m[:len(m)-1] if u, err := url.Parse(string(m)); err == nil { methods = append(methods, u) } } } func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error { widget := aerc.SelectedTab().(widgets.ProvidesMessage) acct := widget.SelectedAccount() defaults := map[string]string{ "To": u.Opaque, "Subject": u.Query().Get("subject"), } composer := widgets.NewComposer( aerc.Config(), acct.AccountConfig(), acct.Worker(), defaults, ) composer.SetContents(strings.NewReader(u.Query().Get("body"))) tab := aerc.NewTab(composer, "unsubscribe") composer.OnHeaderChange("Subject", func(subject string) { if subject == "" { tab.Name = "unsubscribe" } else { tab.Name = subject } tab.Content.Invalidate() }) return nil } func unsubscribeHTTP(u *url.URL) error { return lib.OpenFile(u.String()) }