summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2016-05-12 23:56:07 -0400
committerChristopher Speller <crspeller@gmail.com>2016-05-12 23:56:07 -0400
commit38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8 (patch)
treea4fde09672192b97d453ad605b030bd5a10c5a45 /vendor/github.com/mattermost
parent84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (diff)
downloadchat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.gz
chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.bz2
chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.zip
Moving to glide
Diffstat (limited to 'vendor/github.com/mattermost')
-rw-r--r--vendor/github.com/mattermost/rsc/.gitignore22
-rw-r--r--vendor/github.com/mattermost/rsc/README.md4
-rw-r--r--vendor/github.com/mattermost/rsc/app/app.go39
-rw-r--r--vendor/github.com/mattermost/rsc/app/app.yaml23
-rw-r--r--vendor/github.com/mattermost/rsc/app/index.yaml16
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/appfile/main.go156
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/appmount/main.go287
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/client/client.go150
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/fs/fs.go273
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/fs/local.go82
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/proto/data.go55
-rw-r--r--vendor/github.com/mattermost/rsc/appfs/server/app.go982
-rw-r--r--vendor/github.com/mattermost/rsc/arq/arq.go663
-rw-r--r--vendor/github.com/mattermost/rsc/arq/arqfs/main.go247
-rw-r--r--vendor/github.com/mattermost/rsc/arq/crypto.go93
-rw-r--r--vendor/github.com/mattermost/rsc/arq/data.go240
-rw-r--r--vendor/github.com/mattermost/rsc/arq/hist/hist.go160
-rw-r--r--vendor/github.com/mattermost/rsc/arq/unpack.go227
-rw-r--r--vendor/github.com/mattermost/rsc/blog/atom/atom.go58
-rw-r--r--vendor/github.com/mattermost/rsc/blog/main.go15
-rw-r--r--vendor/github.com/mattermost/rsc/blog/post/post.go534
-rw-r--r--vendor/github.com/mattermost/rsc/blog/post/qr.go34
-rw-r--r--vendor/github.com/mattermost/rsc/cmd/crypt/crypt.go79
-rw-r--r--vendor/github.com/mattermost/rsc/cmd/issue/issue.go185
-rw-r--r--vendor/github.com/mattermost/rsc/cmd/jfmt/main.go37
-rw-r--r--vendor/github.com/mattermost/rsc/crypt/crypt.go150
-rw-r--r--vendor/github.com/mattermost/rsc/devweb/main.go317
-rw-r--r--vendor/github.com/mattermost/rsc/devweb/slave/slave.go26
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/debug.go20
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/fuse.go1650
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/fuse_kernel.go539
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/fuse_kernel_darwin.go58
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/fuse_kernel_linux.go50
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/fuse_kernel_std.go1
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/fuse_test.go594
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/hellofs/hello.go62
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/mount_darwin.go122
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/mount_linux.go67
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/serve.go1022
-rw-r--r--vendor/github.com/mattermost/rsc/fuse/tree.go93
-rw-r--r--vendor/github.com/mattermost/rsc/gf256/blog_test.go85
-rw-r--r--vendor/github.com/mattermost/rsc/gf256/gf256_test.go194
-rw-r--r--vendor/github.com/mattermost/rsc/google/acme/Chat/main.go575
-rw-r--r--vendor/github.com/mattermost/rsc/google/chat.go39
-rw-r--r--vendor/github.com/mattermost/rsc/google/gmail/gmail.go1241
-rw-r--r--vendor/github.com/mattermost/rsc/google/gmailsend/send.go370
-rw-r--r--vendor/github.com/mattermost/rsc/google/googleserver/chat.go80
-rw-r--r--vendor/github.com/mattermost/rsc/google/googleserver/main.go139
-rw-r--r--vendor/github.com/mattermost/rsc/google/main.go181
-rw-r--r--vendor/github.com/mattermost/rsc/gtfs/gtfs.pb.go440
-rw-r--r--vendor/github.com/mattermost/rsc/imap/Makefile15
-rw-r--r--vendor/github.com/mattermost/rsc/imap/decode.go227
-rw-r--r--vendor/github.com/mattermost/rsc/imap/decode_test.go26
-rw-r--r--vendor/github.com/mattermost/rsc/imap/imap.go1110
-rw-r--r--vendor/github.com/mattermost/rsc/imap/imap_test.go433
-rw-r--r--vendor/github.com/mattermost/rsc/imap/mail.go468
-rw-r--r--vendor/github.com/mattermost/rsc/imap/mail_test.go335
-rw-r--r--vendor/github.com/mattermost/rsc/imap/rfc2045.txt1739
-rw-r--r--vendor/github.com/mattermost/rsc/imap/rfc2971.txt451
-rw-r--r--vendor/github.com/mattermost/rsc/imap/rfc3501.txt6051
-rw-r--r--vendor/github.com/mattermost/rsc/imap/sx.go350
-rw-r--r--vendor/github.com/mattermost/rsc/imap/sx_test.go60
-rw-r--r--vendor/github.com/mattermost/rsc/imap/tcs.go602
-rw-r--r--vendor/github.com/mattermost/rsc/keychain/doc.go28
-rw-r--r--vendor/github.com/mattermost/rsc/keychain/mac.go107
-rw-r--r--vendor/github.com/mattermost/rsc/mbta/Passages.pbbin0 -> 69799 bytes
-rw-r--r--vendor/github.com/mattermost/rsc/mbta/Vehicles.pbbin0 -> 35798 bytes
-rw-r--r--vendor/github.com/mattermost/rsc/mbta/mbta.go28
-rwxr-xr-xvendor/github.com/mattermost/rsc/mkapp51
-rw-r--r--vendor/github.com/mattermost/rsc/plist/plist.go179
-rw-r--r--vendor/github.com/mattermost/rsc/plist/plist_test.go110
-rw-r--r--vendor/github.com/mattermost/rsc/qr/coding/qr_test.go133
-rw-r--r--vendor/github.com/mattermost/rsc/qr/libqrencode/Makefile4
-rw-r--r--vendor/github.com/mattermost/rsc/qr/libqrencode/qrencode.go149
-rw-r--r--vendor/github.com/mattermost/rsc/qr/png_test.go73
-rw-r--r--vendor/github.com/mattermost/rsc/qr/web/pic.go506
-rw-r--r--vendor/github.com/mattermost/rsc/qr/web/play.go1118
-rw-r--r--vendor/github.com/mattermost/rsc/qr/web/resize/resize.go152
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/copy.go225
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/main.go96
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/match.go406
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/merge.go103
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/sort.go199
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/sparse.go80
-rw-r--r--vendor/github.com/mattermost/rsc/regexp/regmerge/utf.go270
-rw-r--r--vendor/github.com/mattermost/rsc/rosetta/graph/Makefile7
-rw-r--r--vendor/github.com/mattermost/rsc/rosetta/graph/graph.go133
-rw-r--r--vendor/github.com/mattermost/rsc/rosetta/maze/Makefile8
-rw-r--r--vendor/github.com/mattermost/rsc/rosetta/maze/maze.go191
-rw-r--r--vendor/github.com/mattermost/rsc/s3get/main.go85
-rw-r--r--vendor/github.com/mattermost/rsc/smugmug/smug.go542
-rw-r--r--vendor/github.com/mattermost/rsc/smugmug/smugup/main.go158
l---------vendor/github.com/mattermost/rsc/tmp/app1
l---------vendor/github.com/mattermost/rsc/tmp/appfs1
l---------vendor/github.com/mattermost/rsc/tmp/arq1
l---------vendor/github.com/mattermost/rsc/tmp/blog1
l---------vendor/github.com/mattermost/rsc/tmp/cmd1
l---------vendor/github.com/mattermost/rsc/tmp/crypt1
l---------vendor/github.com/mattermost/rsc/tmp/devweb1
l---------vendor/github.com/mattermost/rsc/tmp/fuse1
l---------vendor/github.com/mattermost/rsc/tmp/gf2561
l---------vendor/github.com/mattermost/rsc/tmp/google1
l---------vendor/github.com/mattermost/rsc/tmp/gtfs1
l---------vendor/github.com/mattermost/rsc/tmp/imap1
l---------vendor/github.com/mattermost/rsc/tmp/keychain1
l---------vendor/github.com/mattermost/rsc/tmp/mbta1
l---------vendor/github.com/mattermost/rsc/tmp/mkapp1
l---------vendor/github.com/mattermost/rsc/tmp/plist1
l---------vendor/github.com/mattermost/rsc/tmp/qr1
l---------vendor/github.com/mattermost/rsc/tmp/regexp1
l---------vendor/github.com/mattermost/rsc/tmp/rosetta1
l---------vendor/github.com/mattermost/rsc/tmp/s3get1
l---------vendor/github.com/mattermost/rsc/tmp/smugmug1
l---------vendor/github.com/mattermost/rsc/tmp/xmpp1
-rw-r--r--vendor/github.com/mattermost/rsc/xmpp/xmpp.go572
115 files changed, 30378 insertions, 0 deletions
diff --git a/vendor/github.com/mattermost/rsc/.gitignore b/vendor/github.com/mattermost/rsc/.gitignore
new file mode 100644
index 000000000..00268614f
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/.gitignore
@@ -0,0 +1,22 @@
+# Compiled Object files, Static and Dynamic libs (Shared Objects)
+*.o
+*.a
+*.so
+
+# Folders
+_obj
+_test
+
+# Architecture specific extensions/prefixes
+*.[568vq]
+[568vq].out
+
+*.cgo1.go
+*.cgo2.c
+_cgo_defun.c
+_cgo_gotypes.go
+_cgo_export.*
+
+_testmain.go
+
+*.exe
diff --git a/vendor/github.com/mattermost/rsc/README.md b/vendor/github.com/mattermost/rsc/README.md
new file mode 100644
index 000000000..d110adfb2
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/README.md
@@ -0,0 +1,4 @@
+rsc
+===
+
+fork of Russ Cox's code.google.com/p/rsc \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/app/app.go b/vendor/github.com/mattermost/rsc/app/app.go
new file mode 100644
index 000000000..d6db65dd4
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/app/app.go
@@ -0,0 +1,39 @@
+package app
+
+import (
+ "fmt"
+ "net/http"
+
+ "appengine"
+ "appengine/memcache"
+
+ _ "github.com/mattermost/rsc/appfs/server"
+ _ "github.com/mattermost/rsc/blog/post"
+)
+
+func init() {
+ http.HandleFunc("/admin/", Admin)
+}
+
+func Admin(w http.ResponseWriter, req *http.Request) {
+ c := appengine.NewContext(req)
+ switch req.FormValue("op") {
+ default:
+ fmt.Fprintf(w, "unknown op %s\n", req.FormValue("op"))
+ case "memcache-get":
+ key := req.FormValue("key")
+ item, err := memcache.Get(c, key)
+ if err != nil {
+ fmt.Fprintf(w, "ERROR: %s\n", err)
+ return
+ }
+ w.Write(item.Value)
+ case "memcache-delete":
+ key := req.FormValue("key")
+ if err := memcache.Delete(c, key); err != nil {
+ fmt.Fprintf(w, "ERROR: %s\n", err)
+ return
+ }
+ fmt.Fprintf(w, "deleted %s\n", key)
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/app/app.yaml b/vendor/github.com/mattermost/rsc/app/app.yaml
new file mode 100644
index 000000000..d45119345
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/app/app.yaml
@@ -0,0 +1,23 @@
+# mkapp
+# ~/pub/go_appengine/dev_appserver.py --high_replication tmp
+# ~/pub/go_appengine/appcfg.py update tmp
+
+application: rsc-swtch-app
+runtime: go
+version: test
+api_version: go1
+
+handlers:
+- url: /\.appfs.*
+ script: _go_app
+ secure: always
+- url: /draft(/.*)?
+ script: _go_app
+ login: required
+- url: /admin(/.*)?
+ script: _go_app
+ login: admin
+
+# MUST BE LAST
+- url: /.*
+ script: _go_app
diff --git a/vendor/github.com/mattermost/rsc/app/index.yaml b/vendor/github.com/mattermost/rsc/app/index.yaml
new file mode 100644
index 000000000..c94524657
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/app/index.yaml
@@ -0,0 +1,16 @@
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run. If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED"). If you want to manage some indexes
+# manually, move them above the marker line. The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
+
+- kind: FileInfo
+ ancestor: yes
+ properties:
+ - name: Path
diff --git a/vendor/github.com/mattermost/rsc/appfs/appfile/main.go b/vendor/github.com/mattermost/rsc/appfs/appfile/main.go
new file mode 100644
index 000000000..6d77df9aa
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/appfile/main.go
@@ -0,0 +1,156 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// appfile is a command-line interface to an appfs file system.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+
+ "github.com/mattermost/rsc/appfs/client"
+ "github.com/mattermost/rsc/keychain"
+)
+
+var c client.Client
+
+func init() {
+ flag.StringVar(&c.Host, "h", "localhost:8080", "app serving host")
+ flag.StringVar(&c.User, "u", "", "user name")
+ flag.StringVar(&c.Password, "p", "", "password")
+}
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: appfile [-h host] cmd args...\n")
+ fmt.Fprintf(os.Stderr, "\n")
+ fmt.Fprintf(os.Stderr, "Commands are:\n")
+ for _, c := range cmd {
+ fmt.Fprintf(os.Stderr, "\t%s\n", c.name)
+ }
+ os.Exit(2)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ usage()
+ }
+
+ if c.Password == "" {
+ var err error
+ c.User, c.Password, err = keychain.UserPasswd(c.Host, "")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "unable to obtain user and password: %s\n", err)
+ os.Exit(2)
+ }
+ }
+
+ name, args := args[0], args[1:]
+ for _, c := range cmd {
+ if name == c.name {
+ switch c.arg {
+ case 0, 1:
+ if len(args) != c.arg {
+ if c.arg == 0 {
+ fmt.Fprintf(os.Stderr, "%s takes no arguments\n", name)
+ os.Exit(2)
+ }
+ fmt.Fprintf(os.Stderr, "%s requires 1 argument\n", name)
+ os.Exit(2)
+ }
+ case 2:
+ if len(args) == 0 {
+ fmt.Fprintf(os.Stderr, "%s requires at least 1 argument\n", name)
+ os.Exit(2)
+ }
+ }
+ c.fn(args)
+ return
+ }
+ }
+ fmt.Fprintf(os.Stderr, "unknown command %s\n", name)
+ os.Exit(2)
+}
+
+var cmd = []struct {
+ name string
+ fn func([]string)
+ arg int
+}{
+ {"mkdir", mkdir, 2},
+ {"write", write, 1},
+ {"read", read, 2},
+ {"mkfs", mkfs, 0},
+ {"stat", stat, 2},
+}
+
+func mkdir(args []string) {
+ for _, name := range args {
+ if err := c.Create(name, true); err != nil {
+ log.Printf("mkdir %s: %v", name, err)
+ }
+ }
+}
+
+func write(args []string) {
+ name := args[0]
+ data, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ log.Printf("reading stdin: %v", err)
+ return
+ }
+ c.Create(name, false)
+ if err := c.Write(name, data); err != nil {
+ log.Printf("write %s: %v", name, err)
+ }
+}
+
+func read(args []string) {
+ for _, name := range args {
+ fi, err := c.Stat(name)
+ if err != nil {
+ log.Printf("stat %s: %v", name, err)
+ continue
+ }
+ if fi.IsDir {
+ dirs, err := c.ReadDir(name)
+ if err != nil {
+ log.Printf("read %s: %v", name, err)
+ continue
+ }
+ for _, fi := range dirs {
+ fmt.Printf("%+v\n", *fi)
+ }
+ } else {
+ data, err := c.Read(name)
+ if err != nil {
+ log.Printf("read %s: %v", name, err)
+ continue
+ }
+ os.Stdout.Write(data)
+ }
+ }
+}
+
+func mkfs([]string) {
+ if err := c.Mkfs(); err != nil {
+ log.Printf("mkfs: %v", err)
+ }
+}
+
+func stat(args []string) {
+ for _, name := range args {
+ fi, err := c.Stat(name)
+ if err != nil {
+ log.Printf("stat %s: %v", name, err)
+ continue
+ }
+ fmt.Printf("%+v\n", *fi)
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/appfs/appmount/main.go b/vendor/github.com/mattermost/rsc/appfs/appmount/main.go
new file mode 100644
index 000000000..2c9f867d3
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/appmount/main.go
@@ -0,0 +1,287 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// appmount mounts an appfs file system.
+package main
+
+import (
+ "bytes"
+ "encoding/gob"
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "os/exec"
+ "path"
+ "strings"
+ "syscall"
+ "time"
+ "sync"
+ "runtime"
+
+ "github.com/mattermost/rsc/appfs/client"
+ "github.com/mattermost/rsc/appfs/proto"
+ "github.com/mattermost/rsc/fuse"
+ "github.com/mattermost/rsc/keychain"
+)
+
+var usageMessage = `usage: appmount [-h host] [-u user] [-p password] /mnt
+
+Appmount mounts the appfs file system on the named mount point.
+
+The default host is localhost:8080.
+`
+
+// Shared between master and slave.
+var z struct {
+ Client client.Client
+ Debug *bool
+ Mtpt string
+}
+
+var fc *fuse.Conn
+var cl = &z.Client
+
+func init() {
+ flag.StringVar(&cl.Host, "h", "localhost:8080", "app serving host")
+ flag.StringVar(&cl.User, "u", "", "user name")
+ flag.StringVar(&cl.Password, "p", "", "password")
+ z.Debug = flag.Bool("debug", false, "")
+}
+
+func usage() {
+ fmt.Fprint(os.Stderr, usageMessage)
+ os.Exit(2)
+}
+
+func main() {
+ log.SetFlags(0)
+
+ if len(os.Args) == 2 && os.Args[1] == "MOUNTSLAVE" {
+ mountslave()
+ return
+ }
+
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ usage()
+ }
+ z.Mtpt = args[0]
+
+ if cl.Password == "" {
+ var err error
+ cl.User, cl.Password, err = keychain.UserPasswd(cl.Host, "")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "unable to obtain user and password: %s\n", err)
+ os.Exit(2)
+ }
+ }
+
+ if _, err := cl.Stat("/"); err != nil {
+ log.Fatal(err)
+ }
+
+ // Run in child so that we can exit once child is running.
+ r, w, err := os.Pipe()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var buf bytes.Buffer
+ enc := gob.NewEncoder(&buf)
+ enc.Encode(&z)
+
+ cmd := exec.Command(os.Args[0], "MOUNTSLAVE")
+ cmd.Stdin = &buf
+ cmd.Stdout = w
+ cmd.Stderr = os.Stderr
+ if err := cmd.Start(); err != nil {
+ log.Fatalf("mount process: %v", err)
+ }
+ w.Close()
+
+ ok := make([]byte, 10)
+ n, _ := r.Read(ok)
+ if n != 2 || string(ok[0:2]) != "OK" {
+ os.Exit(1)
+ }
+
+ fmt.Fprintf(os.Stderr, "mounted on %s\n", z.Mtpt)
+}
+
+func mountslave() {
+ stdout, _ := syscall.Dup(1)
+ syscall.Dup2(2, 1)
+
+ r := gob.NewDecoder(os.Stdin)
+ if err := r.Decode(&z); err != nil {
+ log.Fatalf("gob decode: %v", err)
+ }
+
+ fc, err := fuse.Mount(z.Mtpt)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer exec.Command("umount", z.Mtpt).Run()
+
+ if *z.Debug {
+ fuse.Debugf = log.Printf
+ }
+
+ syscall.Write(stdout, []byte("OK"))
+ syscall.Close(stdout)
+ fc.Serve(FS{})
+}
+
+type FS struct{}
+
+func (FS) Root() (fuse.Node, fuse.Error) {
+ return file("/")
+}
+
+type File struct {
+ Name string
+ FileInfo *proto.FileInfo
+ Data []byte
+}
+
+type statEntry struct {
+ fi *proto.FileInfo
+ err error
+ t time.Time
+}
+
+var statCache struct {
+ mu sync.Mutex
+ m map[string] statEntry
+}
+
+func stat(name string) (*proto.FileInfo, error) {
+ if runtime.GOOS == "darwin" && strings.Contains(name, "/._") {
+ // Mac resource forks
+ return nil, fmt.Errorf("file not found")
+ }
+ statCache.mu.Lock()
+ e, ok := statCache.m[name]
+ statCache.mu.Unlock()
+ if ok && time.Since(e.t) < 2*time.Minute {
+ return e.fi, e.err
+ }
+ fi, err := cl.Stat(name)
+ saveStat(name, fi, err)
+ return fi, err
+}
+
+func saveStat(name string, fi *proto.FileInfo, err error) {
+ if *z.Debug {
+if fi != nil {
+ fmt.Fprintf(os.Stderr, "savestat %s %+v\n", name, *fi)
+} else {
+ fmt.Fprintf(os.Stderr, "savestat %s %v\n", name, err)
+}
+ }
+ statCache.mu.Lock()
+ if statCache.m == nil {
+ statCache.m = make(map[string]statEntry)
+ }
+ statCache.m[name] = statEntry{fi, err, time.Now()}
+ statCache.mu.Unlock()
+}
+
+func delStat(name string) {
+ statCache.mu.Lock()
+ if statCache.m != nil {
+ delete(statCache.m, name)
+ }
+ statCache.mu.Unlock()
+}
+
+func file(name string) (fuse.Node, fuse.Error) {
+ fi, err := stat(name)
+ if err != nil {
+ if strings.Contains(err.Error(), "no such entity") {
+ return nil, fuse.ENOENT
+ }
+ if *z.Debug {
+ log.Printf("stat %s: %v", name, err)
+ }
+ return nil, fuse.EIO
+ }
+ return &File{name, fi, nil}, nil
+}
+
+func (f *File) Attr() (attr fuse.Attr) {
+ fi := f.FileInfo
+ attr.Mode = 0666
+ if fi.IsDir {
+ attr.Mode |= 0111 | os.ModeDir
+ }
+ attr.Mtime = fi.ModTime
+ attr.Size = uint64(fi.Size)
+ return
+}
+
+func (f *File) Lookup(name string, intr fuse.Intr) (fuse.Node, fuse.Error) {
+ return file(path.Join(f.Name, name))
+}
+
+func (f *File) ReadAll(intr fuse.Intr) ([]byte, fuse.Error) {
+ data, err := cl.Read(f.Name)
+ if err != nil {
+ log.Printf("read %s: %v", f.Name, err)
+ return nil, fuse.EIO
+ }
+ return data, nil
+}
+
+func (f *File) ReadDir(intr fuse.Intr) ([]fuse.Dirent, fuse.Error) {
+ fis, err := cl.ReadDir(f.Name)
+ if err != nil {
+ log.Printf("read %s: %v", f.Name, err)
+ return nil, fuse.EIO
+ }
+ var dirs []fuse.Dirent
+ for _, fi := range fis {
+ saveStat(path.Join(f.Name, fi.Name), fi, nil)
+ dirs = append(dirs, fuse.Dirent{Name: fi.Name})
+ }
+ return dirs, nil
+}
+
+func (f *File) WriteAll(data []byte, intr fuse.Intr) fuse.Error {
+ defer delStat(f.Name)
+ if err := cl.Write(f.Name[1:], data); err != nil {
+ log.Printf("write %s: %v", f.Name, err)
+ return fuse.EIO
+ }
+ return nil
+}
+
+func (f *File) Mkdir(req *fuse.MkdirRequest, intr fuse.Intr) (fuse.Node, fuse.Error) {
+ defer delStat(f.Name)
+ p := path.Join(f.Name, req.Name)
+ if err := cl.Create(p[1:], true); err != nil {
+ log.Printf("mkdir %s: %v", p, err)
+ return nil, fuse.EIO
+ }
+ delStat(p)
+ return file(p)
+}
+
+func (f *File) Create(req *fuse.CreateRequest, resp *fuse.CreateResponse, intr fuse.Intr) (fuse.Node, fuse.Handle, fuse.Error) {
+ defer delStat(f.Name)
+ p := path.Join(f.Name, req.Name)
+ if err := cl.Create(p[1:], false); err != nil {
+ log.Printf("create %s: %v", p, err)
+ return nil, nil, fuse.EIO
+ }
+ delStat(p)
+ n, err := file(p)
+ if err != nil {
+ return nil, nil, err
+ }
+ return n, n, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/appfs/client/client.go b/vendor/github.com/mattermost/rsc/appfs/client/client.go
new file mode 100644
index 000000000..a1deb291d
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/client/client.go
@@ -0,0 +1,150 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package client implements a basic appfs client.
+package client
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/mattermost/rsc/appfs/proto"
+)
+
+type Client struct {
+ Host string
+ User string
+ Password string
+}
+
+func (c *Client) url(op, path string) string {
+ scheme := "https"
+ if strings.HasPrefix(c.Host, "localhost:") {
+ scheme = "http"
+ }
+ if strings.HasSuffix(op, "/") && strings.HasPrefix(path, "/") {
+ path = path[1:]
+ }
+ return scheme + "://"+ c.User + ":" + c.Password + "@" + c.Host + op + path
+}
+
+func (c *Client) do(u string) error {
+ _, err := c.get(u)
+ return err
+}
+
+func (c *Client) get(u string) ([]byte, error) {
+ tries := 0
+ for {
+ r, err := http.Get(u)
+ if err != nil {
+ return nil, err
+ }
+ defer r.Body.Close()
+ data, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return nil, err
+ }
+ if r.StatusCode != 200 {
+ if r.StatusCode == 500 {
+ if tries++; tries < 3 {
+ fmt.Printf("%s %s; sleeping\n", r.Status, data)
+ time.Sleep(5*time.Second)
+ continue
+ }
+ }
+ return nil, fmt.Errorf("%s %s", r.Status, data)
+ }
+ return data, nil
+ }
+ panic("unreachable")
+}
+
+func (c *Client) post(u string, data []byte) ([]byte, error) {
+ tries := 0
+ for {
+ r, err := http.Post(u, proto.PostContentType, bytes.NewBuffer(data))
+ if err != nil {
+ return nil, err
+ }
+ defer r.Body.Close()
+ rdata, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return nil, err
+ }
+ if r.StatusCode != 200 {
+ if r.StatusCode == 500 {
+ if tries++; tries < 3 {
+ fmt.Printf("%s %s; sleeping\n", r.Status, rdata)
+ time.Sleep(5*time.Second)
+ continue
+ }
+ }
+ return nil, fmt.Errorf("%s %s", r.Status, rdata)
+ }
+ return rdata, nil
+ }
+ panic("unreachable")
+}
+
+func (c *Client) Create(path string, isdir bool) error {
+ u := c.url(proto.CreateURL, path)
+ if isdir {
+ u += "?dir=1"
+ }
+ return c.do(u)
+}
+
+func (c *Client) Read(path string) ([]byte, error) {
+ return c.get(c.url(proto.ReadURL, path))
+}
+
+func (c *Client) Write(path string, data []byte) error {
+ u := c.url(proto.WriteURL, path)
+ _, err := c.post(u, data)
+ return err
+}
+
+func (c *Client) Mkfs() error {
+ return c.do(c.url(proto.MkfsURL, ""))
+}
+
+func (c *Client) Stat(path string) (*proto.FileInfo, error) {
+ data, err := c.get(c.url(proto.StatURL, path))
+ if err != nil {
+ return nil, err
+ }
+ var fi proto.FileInfo
+ if err := json.Unmarshal(data, &fi); err != nil {
+ return nil, err
+ }
+ return &fi, nil
+}
+
+func (c *Client) ReadDir(path string) ([]*proto.FileInfo, error) {
+ data, err := c.Read(path)
+ if err != nil {
+ return nil, err
+ }
+ dec := json.NewDecoder(bytes.NewBuffer(data))
+ var out []*proto.FileInfo
+ for {
+ var fi proto.FileInfo
+ err := dec.Decode(&fi)
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return out, err
+ }
+ out = append(out, &fi)
+ }
+ return out, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/appfs/fs/fs.go b/vendor/github.com/mattermost/rsc/appfs/fs/fs.go
new file mode 100644
index 000000000..ac6657393
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/fs/fs.go
@@ -0,0 +1,273 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package fs is an indirection layer, allowing code to use a
+// file system without knowing whether it is the host file system
+// (running without App Engine) or the datastore-based app
+// file system (running on App Engine).
+//
+// When compiled locally, fs refers to files in the local file system,
+// and the cache saves nothing.
+//
+// When compiled for App Engine, fs uses the appfs file system
+// and the memcache-based cache.
+package fs
+
+import (
+ "bytes"
+ "encoding/gob"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "os"
+ "time"
+
+ "github.com/mattermost/rsc/appfs/proto"
+)
+
+type AppEngine interface {
+ NewContext(req *http.Request) interface{}
+ CacheRead(ctxt interface{}, name, path string) (key interface{}, data []byte, found bool)
+ CacheWrite(ctxt, key interface{}, data []byte)
+ Read(ctxt interface{}, path string) ([]byte, *proto.FileInfo, error)
+ Write(ctxt interface{}, path string, data []byte) error
+ Remove(ctxt interface{}, path string) error
+ Mkdir(ctxt interface{}, path string) error
+ ReadDir(ctxt interface{}, path string) ([]proto.FileInfo, error)
+ Criticalf(ctxt interface{}, format string, args ...interface{})
+ User(ctxt interface{}) string
+}
+
+var ae AppEngine
+
+func Register(impl AppEngine) {
+ ae = impl
+}
+
+// Root is the root of the local file system. It has no effect on App Engine.
+var Root = "."
+
+// A Context is an opaque context that is needed to perform file system
+// operations. Each context is associated with a single HTTP request.
+type Context struct {
+ context
+ ae interface{}
+}
+
+// NewContext returns a context associated with the given HTTP request.
+func NewContext(req *http.Request) *Context {
+ if ae != nil {
+ ctxt := ae.NewContext(req)
+ return &Context{ae: ctxt}
+ }
+ return newContext(req)
+}
+
+// A CacheKey is an opaque cache key that can be used to store new entries
+// in the cache. To ensure that the cache remains consistent with the underlying
+// file system, the correct procedure is:
+//
+// 1. Use CacheRead (or CacheLoad) to attempt to load the entry. If it succeeds, use it.
+// If not, continue, saving the CacheKey.
+//
+// 2. Read from the file system and construct the entry that would have
+// been in the cache. In order to be consistent, all the file system reads
+// should only refer to parts of the file system in the tree rooted at the path
+// passed to CacheRead.
+//
+// 3. Save the entry using CacheWrite (or CacheStore), using the key that was
+// created by the CacheRead (or CacheLoad) executed before reading from the
+// file system.
+//
+type CacheKey struct {
+ cacheKey
+ ae interface{}
+}
+
+// CacheRead reads from cache the entry with the given name and path.
+// The path specifies the scope of information stored in the cache entry.
+// An entry is invalidated by a write to any location in the file tree rooted at path.
+// The name is an uninterpreted identifier to distinguish the cache entry
+// from other entries using the same path.
+//
+// If it finds a cache entry, CacheRead returns the data and found=true.
+// If it does not find a cache entry, CacheRead returns data=nil and found=false.
+// Either way, CacheRead returns an appropriate cache key for storing to the
+// cache entry using CacheWrite.
+func (c *Context) CacheRead(name, path string) (ckey CacheKey, data []byte, found bool) {
+ if ae != nil {
+ key, data, found := ae.CacheRead(c.ae, name, path)
+ return CacheKey{ae: key}, data, found
+ }
+ return c.cacheRead(ckey, path)
+}
+
+// CacheLoad uses CacheRead to load gob-encoded data and decodes it into value.
+func (c *Context) CacheLoad(name, path string, value interface{}) (ckey CacheKey, found bool) {
+ ckey, data, found := c.CacheRead(name, path)
+ if found {
+ if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(value); err != nil {
+ c.Criticalf("gob Decode: %v", err)
+ found = false
+ }
+ }
+ return
+}
+
+// CacheWrite writes an entry to the cache with the given key, path, and data.
+// The cache entry will be invalidated the next time the file tree rooted at path is
+// modified in anyway.
+func (c *Context) CacheWrite(ckey CacheKey, data []byte) {
+ if ae != nil {
+ ae.CacheWrite(c.ae, ckey.ae, data)
+ return
+ }
+ c.cacheWrite(ckey, data)
+}
+
+// CacheStore uses CacheWrite to save the gob-encoded form of value.
+func (c *Context) CacheStore(ckey CacheKey, value interface{}) {
+ var buf bytes.Buffer
+ if err := gob.NewEncoder(&buf).Encode(value); err != nil {
+ c.Criticalf("gob Encode: %v", err)
+ return
+ }
+ c.CacheWrite(ckey, buf.Bytes())
+}
+
+// Read returns the data associated with the file named by path.
+// It is a copy and can be modified without affecting the file.
+func (c *Context) Read(path string) ([]byte, *proto.FileInfo, error) {
+ if ae != nil {
+ return ae.Read(c.ae, path)
+ }
+ return c.read(path)
+}
+
+// Write replaces the data associated with the file named by path.
+func (c *Context) Write(path string, data []byte) error {
+ if ae != nil {
+ return ae.Write(c.ae, path, data)
+ }
+ return c.write(path, data)
+}
+
+// Remove removes the file named by path.
+func (c *Context) Remove(path string) error {
+ if ae != nil {
+ return ae.Remove(c.ae, path)
+ }
+ return c.remove(path)
+}
+
+// Mkdir creates a directory with the given path.
+// If the path already exists and is a directory, Mkdir returns no error.
+func (c *Context) Mkdir(path string) error {
+ if ae != nil {
+ return ae.Mkdir(c.ae, path)
+ }
+ return c.mkdir(path)
+}
+
+// ReadDir returns the contents of the directory named by the path.
+func (c *Context) ReadDir(path string) ([]proto.FileInfo, error) {
+ if ae != nil {
+ return ae.ReadDir(c.ae, path)
+ }
+ return c.readdir(path)
+}
+
+// ServeFile serves the named file as the response to the HTTP request.
+func (c *Context) ServeFile(w http.ResponseWriter, req *http.Request, name string) {
+ root := &httpFS{c, name}
+ http.FileServer(root).ServeHTTP(w, req)
+}
+
+// Criticalf logs the message at critical priority.
+func (c *Context) Criticalf(format string, args ...interface{}) {
+ if ae != nil {
+ ae.Criticalf(c.ae, format, args...)
+ }
+ log.Printf(format, args...)
+}
+
+// User returns the name of the user running the request.
+func (c *Context) User() string {
+ if ae != nil {
+ return ae.User(c.ae)
+ }
+ return os.Getenv("USER")
+}
+
+type httpFS struct {
+ c *Context
+ name string
+}
+
+type httpFile struct {
+ data []byte
+ fi *proto.FileInfo
+ off int
+}
+
+func (h *httpFS) Open(_ string) (http.File, error) {
+ data, fi, err := h.c.Read(h.name)
+ if err != nil {
+ return nil, err
+ }
+ return &httpFile{data, fi, 0}, nil
+}
+
+func (f *httpFile) Close() error {
+ return nil
+}
+
+type fileInfo struct {
+ p *proto.FileInfo
+}
+
+func (f *fileInfo) IsDir() bool { return f.p.IsDir }
+func (f *fileInfo) Name() string { return f.p.Name }
+func (f *fileInfo) ModTime() time.Time { return f.p.ModTime }
+func (f *fileInfo) Size() int64 { return f.p.Size }
+func (f *fileInfo) Sys() interface{} { return f.p }
+func (f *fileInfo) Mode() os.FileMode {
+ if f.p.IsDir {
+ return os.ModeDir | 0777
+ }
+ return 0666
+}
+
+func (f *httpFile) Stat() (os.FileInfo, error) {
+ return &fileInfo{f.fi}, nil
+}
+
+func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
+ return nil, fmt.Errorf("no directory")
+}
+
+func (f *httpFile) Read(data []byte) (int, error) {
+ if f.off >= len(f.data) {
+ return 0, io.EOF
+ }
+ n := copy(data, f.data[f.off:])
+ f.off += n
+ return n, nil
+}
+
+func (f *httpFile) Seek(offset int64, whence int) (int64, error) {
+ off := int(offset)
+ if int64(off) != offset {
+ return 0, fmt.Errorf("invalid offset")
+ }
+ switch whence {
+ case 1:
+ off += f.off
+ case 2:
+ off += len(f.data)
+ }
+ f.off = off
+ return int64(off), nil
+}
diff --git a/vendor/github.com/mattermost/rsc/appfs/fs/local.go b/vendor/github.com/mattermost/rsc/appfs/fs/local.go
new file mode 100644
index 000000000..c78b35b64
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/fs/local.go
@@ -0,0 +1,82 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fs
+
+import (
+ "io/ioutil"
+ "net/http"
+ "os"
+ "path/filepath"
+
+ "github.com/mattermost/rsc/appfs/proto"
+)
+
+type context struct{}
+
+type cacheKey struct{}
+
+func newContext(req *http.Request) *Context {
+ return &Context{}
+}
+
+func (*context) cacheRead(ckey CacheKey, path string) (CacheKey, []byte, bool) {
+ return ckey, nil, false
+}
+
+func (*context) cacheWrite(ckey CacheKey, data []byte) {
+}
+
+func (*context) read(path string) ([]byte, *proto.FileInfo, error) {
+ p := filepath.Join(Root, path)
+ dir, err := os.Stat(p)
+ if err != nil {
+ return nil, nil, err
+ }
+ fi := &proto.FileInfo{
+ Name: dir.Name(),
+ ModTime: dir.ModTime(),
+ Size: dir.Size(),
+ IsDir: dir.IsDir(),
+ }
+ data, err := ioutil.ReadFile(p)
+ return data, fi, err
+}
+
+func (*context) write(path string, data []byte) error {
+ p := filepath.Join(Root, path)
+ return ioutil.WriteFile(p, data, 0666)
+}
+
+func (*context) remove(path string) error {
+ p := filepath.Join(Root, path)
+ return os.Remove(p)
+}
+
+func (*context) mkdir(path string) error {
+ p := filepath.Join(Root, path)
+ fi, err := os.Stat(p)
+ if err == nil && fi.IsDir() {
+ return nil
+ }
+ return os.Mkdir(p, 0777)
+}
+
+func (*context) readdir(path string) ([]proto.FileInfo, error) {
+ p := filepath.Join(Root, path)
+ dirs, err := ioutil.ReadDir(p)
+ if err != nil {
+ return nil, err
+ }
+ var out []proto.FileInfo
+ for _, dir := range dirs {
+ out = append(out, proto.FileInfo{
+ Name: dir.Name(),
+ ModTime: dir.ModTime(),
+ Size: dir.Size(),
+ IsDir: dir.IsDir(),
+ })
+ }
+ return out, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/appfs/proto/data.go b/vendor/github.com/mattermost/rsc/appfs/proto/data.go
new file mode 100644
index 000000000..ac15411a8
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/proto/data.go
@@ -0,0 +1,55 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package proto defines the protocol between appfs client and server.
+package proto
+
+import "time"
+
+// An Auth appears, JSON-encoded, as the X-Appfs-Auth header line,
+// to authenticate a request made to the file server.
+// The authentication scheme could be made more sophisticated, but since
+// we are already forcing the use of TLS, a plain password is fine for now.
+type Auth struct {
+ Password string
+}
+
+// GET /.appfs/stat/path returns the metadata for a file or directory,
+// a JSON-encoded FileInfo.
+const StatURL = "/.appfs/stat/"
+
+// GET /.appfs/read/path returns the content of the file or directory.
+// The body of the response is the raw file or directory content.
+// The content of a directory is a sequence of JSON-encoded FileInfo.
+const ReadURL = "/.appfs/read/"
+
+// POST to /.appfs/write/path writes new data to a file.
+// The X-Appfs-SHA1 header is the SHA1 hash of the data.
+// The body of the request is the raw file content.
+const WriteURL = "/.appfs/write/"
+
+// POST to /.appfs/mount initializes the file system if it does not
+// yet exist in the datastore.
+const MkfsURL = "/.appfs/mkfs"
+
+// POST to /.appfs/create/path creates a new file or directory.
+// The named path must not already exist; its parent must exist.
+// The query parameter dir=1 indicates that a directory should be created.
+const CreateURL = "/.appfs/create/"
+
+// POST to /.appfs/remove/path removes the file or directory.
+// A directory must be empty to be removed.
+const RemoveURL = "/.appfs/remove/"
+
+// A FileInfo is a directory entry.
+type FileInfo struct {
+ Name string // final path element
+ ModTime time.Time
+ Size int64
+ IsDir bool
+}
+
+// PostContentType is the Content-Type for POSTed data.
+// There is no encoding or framing: it is just raw data bytes.
+const PostContentType = "x-appfs/raw"
diff --git a/vendor/github.com/mattermost/rsc/appfs/server/app.go b/vendor/github.com/mattermost/rsc/appfs/server/app.go
new file mode 100644
index 000000000..9486eac41
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/appfs/server/app.go
@@ -0,0 +1,982 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package server implements an appfs server backed by the
+// App Engine datastore.
+package server
+
+import (
+ "bytes"
+ "crypto/sha1"
+ "encoding/base64"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "path"
+ "strconv"
+ "strings"
+ "time"
+
+ "appengine"
+ "appengine/datastore"
+ "appengine/memcache"
+ "appengine/user"
+
+ "github.com/mattermost/rsc/appfs/fs"
+ "github.com/mattermost/rsc/appfs/proto"
+)
+
+const pwFile = "/.password"
+var chatty = false
+
+func init() {
+ handle(proto.ReadURL, (*request).read)
+ handle(proto.WriteURL, (*request).write)
+ handle(proto.StatURL, (*request).stat)
+ handle(proto.MkfsURL, (*request).mkfs)
+ handle(proto.CreateURL, (*request).create)
+ handle(proto.RemoveURL, (*request).remove)
+}
+
+type request struct {
+ w http.ResponseWriter
+ req *http.Request
+ c appengine.Context
+ name string
+ mname string
+ key *datastore.Key
+}
+
+func auth(r *request) bool {
+ hdr := r.req.Header.Get("Authorization")
+ if !strings.HasPrefix(hdr, "Basic ") {
+ return false
+ }
+ data, err := base64.StdEncoding.DecodeString(hdr[6:])
+ if err != nil {
+ return false
+ }
+ i := bytes.IndexByte(data, ':')
+ if i < 0 {
+ return false
+ }
+ user, passwd := string(data[:i]), string(data[i+1:])
+
+ _, data, err = read(r.c, pwFile)
+ if err != nil {
+ r.c.Errorf("reading %s: %v", pwFile, err)
+ if _, err := mkfs(r.c); err != nil {
+ r.c.Errorf("creating fs: %v", err)
+ }
+ _, data, err = read(r.c, pwFile)
+ if err != nil {
+ r.c.Errorf("reading %s again: %v", pwFile, err)
+ return false
+ }
+ }
+
+ lines := strings.Split(string(data), "\n")
+ for _, line := range lines {
+ if strings.HasPrefix(line, "#") {
+ continue
+ }
+ f := strings.Fields(line)
+ if len(f) < 3 {
+ continue
+ }
+ if f[0] == user {
+ return hash(f[1]+passwd) == f[2]
+ }
+ }
+ return false
+}
+
+func hash(s string) string {
+ h := sha1.New()
+ h.Write([]byte(s))
+ return fmt.Sprintf("%x", h.Sum(nil))
+}
+
+func handle(prefix string, f func(*request)) {
+ http.HandleFunc(prefix, func(w http.ResponseWriter, req *http.Request) {
+ c := appengine.NewContext(req)
+ r := &request{
+ w: w,
+ req: req,
+ c: c,
+ }
+
+ if strings.HasSuffix(prefix, "/") {
+ r.name, r.mname, r.key = mangle(c, req.URL.Path[len(prefix)-1:])
+ } else {
+ req.URL.Path = "/"
+ }
+ defer func() {
+ if err := recover(); err != nil {
+ w.WriteHeader(http.StatusConflict)
+ fmt.Fprintf(w, "%s\n", err)
+ }
+ }()
+
+ if !auth(r) {
+ w.Header().Set("WWW-Authenticate", "Basic realm=\"appfs\"")
+ http.Error(w, "Need auth", http.StatusUnauthorized)
+ return
+ }
+
+ f(r)
+ })
+}
+
+func mangle(c appengine.Context, name string) (string, string, *datastore.Key) {
+ name = path.Clean("/" + name)
+ n := strings.Count(name, "/")
+ if name == "/" {
+ n = 0
+ }
+ mname := fmt.Sprintf("%d%s", n, name)
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ key := datastore.NewKey(c, "FileInfo", mname, 0, root)
+ return name, mname, key
+}
+
+type FileInfo struct {
+ Path string // mangled path
+ Name string
+ Qid int64 // assigned unique id number
+ Seq int64 // modification sequence number in file tree
+ ModTime time.Time
+ Size int64
+ IsDir bool
+}
+
+type FileData struct {
+ Data []byte
+}
+
+func stat(c appengine.Context, name string) (*FileInfo, error) {
+ var fi FileInfo
+ name, _, key := mangle(c, name)
+ c.Infof("DATASTORE Stat %q", name)
+ err := datastore.Get(c, key, &fi)
+ if err != nil {
+ return nil, err
+ }
+ return &fi, nil
+}
+
+func (r *request) saveStat(fi *FileInfo) {
+ jfi, err := json.Marshal(&fi)
+ if err != nil {
+ panic(err)
+ }
+ r.w.Header().Set("X-Appfs-Stat", string(jfi))
+}
+
+func (r *request) tx(f func(c appengine.Context) error) {
+ err := datastore.RunInTransaction(r.c, f, &datastore.TransactionOptions{XG: true})
+ if err != nil {
+ panic(err)
+ }
+}
+
+func (r *request) stat() {
+ var fi *FileInfo
+ r.tx(func(c appengine.Context) error {
+ fi1, err := stat(c, r.name)
+ if err != nil {
+ return err
+ }
+ fi = fi1
+ return nil
+ })
+
+ jfi, err := json.Marshal(&fi)
+ if err != nil {
+ panic(err)
+ }
+ r.w.Write(jfi)
+}
+
+func read(c appengine.Context, name string) (fi *FileInfo, data []byte, err error) {
+ name, _, _ = mangle(c, name)
+ fi1, err := stat(c, name)
+ if err != nil {
+ return nil, nil, err
+ }
+ if fi1.IsDir {
+ dt, err := readdir(c, name)
+ if err != nil {
+ return nil, nil, err
+ }
+ fi = fi1
+ data = dt
+ return fi, data, nil
+ }
+
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ dkey := datastore.NewKey(c, "FileData", "", fi1.Qid, root)
+ var fd FileData
+ c.Infof("DATASTORE Read %q", name)
+ if err := datastore.Get(c, dkey, &fd); err != nil {
+ return nil, nil, err
+ }
+ fi = fi1
+ data = fd.Data
+ return fi, data, nil
+}
+
+func (r *request) read() {
+ var (
+ fi *FileInfo
+ data []byte
+ )
+ r.tx(func(c appengine.Context) error {
+ var err error
+ fi, data, err = read(r.c, r.name)
+ return err
+ })
+ r.saveStat(fi)
+ r.w.Write(data)
+}
+
+func readdir(c appengine.Context, name string) ([]byte, error) {
+ name, _, _ = mangle(c, name)
+ var buf bytes.Buffer
+
+ n := strings.Count(name, "/")
+ if name == "/" {
+ name = ""
+ n = 0
+ }
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ first := fmt.Sprintf("%d%s/", n+1, name)
+ limit := fmt.Sprintf("%d%s0", n+1, name)
+ c.Infof("DATASTORE ReadDir %q", name)
+ q := datastore.NewQuery("FileInfo").
+ Filter("Path >=", first).
+ Filter("Path <", limit).
+ Ancestor(root)
+ enc := json.NewEncoder(&buf)
+ it := q.Run(c)
+ var fi FileInfo
+ var pfi proto.FileInfo
+ for {
+ fi = FileInfo{}
+ _, err := it.Next(&fi)
+ if err != nil {
+ if err == datastore.Done {
+ break
+ }
+ return nil, err
+ }
+ pfi = proto.FileInfo{
+ Name: fi.Name,
+ ModTime: fi.ModTime,
+ Size: fi.Size,
+ IsDir: fi.IsDir,
+ }
+ if err := enc.Encode(&pfi); err != nil {
+ return nil, err
+ }
+ }
+
+ return buf.Bytes(), nil
+}
+
+func readdirRaw(c appengine.Context, name string) ([]proto.FileInfo, error) {
+ name, _, _ = mangle(c, name)
+ n := strings.Count(name, "/")
+ if name == "/" {
+ name = ""
+ n = 0
+ }
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ first := fmt.Sprintf("%d%s/", n+1, name)
+ limit := fmt.Sprintf("%d%s0", n+1, name)
+ c.Infof("DATASTORE ReadDir %q", name)
+ q := datastore.NewQuery("FileInfo").
+ Filter("Path >=", first).
+ Filter("Path <", limit).
+ Ancestor(root)
+ it := q.Run(c)
+ var fi FileInfo
+ var pfi proto.FileInfo
+ var out []proto.FileInfo
+ for {
+ fi = FileInfo{}
+ _, err := it.Next(&fi)
+ if err != nil {
+ if err == datastore.Done {
+ break
+ }
+ return nil, err
+ }
+ pfi = proto.FileInfo{
+ Name: fi.Name,
+ ModTime: fi.ModTime,
+ Size: fi.Size,
+ IsDir: fi.IsDir,
+ }
+ out = append(out, pfi)
+ }
+println("READDIR", name, len(out))
+ return out, nil
+}
+
+
+var initPasswd = `# Password file
+# This file controls access to the server.
+# The format is lines of space-separated fields:
+# user salt pwhash
+# The pwhash is the SHA1 of the salt string concatenated with the password.
+
+# user=dummy password=dummy (replace with your own entries)
+dummy 12345 faa863c7d3d41893f80165c704b714d5e31bdd3b
+`
+
+func (r *request) mkfs() {
+ var fi *FileInfo
+ r.tx(func(c appengine.Context) error {
+ var err error
+ fi, err = mkfs(c)
+ return err
+ })
+ r.saveStat(fi)
+}
+
+func mkfs(c appengine.Context) (fi *FileInfo, err error) {
+ fi1, err := stat(c, "/")
+ if err == nil {
+ return fi1, nil
+ }
+
+ // Root needs to be created.
+ // Probably root key does too.
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ _, err = datastore.Put(c, root, &struct{}{})
+ if err != nil {
+ return nil, fmt.Errorf("mkfs put root: %s", err)
+ }
+
+ // Entry for /.
+ _, mpath, key := mangle(c, "/")
+ fi3 := FileInfo{
+ Path: mpath,
+ Name: "/",
+ Seq: 2, // 2, not 1, because we're going to write password file with #2
+ Qid: 1,
+ ModTime: time.Now(),
+ Size: 0,
+ IsDir: true,
+ }
+ _, err = datastore.Put(c, key, &fi3)
+ if err != nil {
+ return nil, fmt.Errorf("mkfs put /: %s", err)
+ }
+
+ /*
+ * Would like to use this code but App Engine apparently
+ * does not let Get observe the effect of a Put in the same
+ * transaction. What planet does that make sense on?
+ * Instead, we have to execute just the datastore writes that this
+ * sequence would.
+ *
+ _, err = create(c, pwFile, false)
+ if err != nil {
+ return nil, fmt.Errorf("mkfs create .password: %s", err)
+ }
+ _, err = write(c, pwFile, []byte(initPasswd))
+ if err != nil {
+ return nil, fmt.Errorf("mkfs write .password: %s", err)
+ }
+ *
+ */
+
+ {
+ name, mname, key := mangle(c, pwFile)
+
+ // Create data object.
+ dataKey := int64(2)
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ dkey := datastore.NewKey(c, "FileData", "", dataKey, root)
+ _, err := datastore.Put(c, dkey, &FileData{[]byte(initPasswd)})
+ if err != nil {
+ return nil, err
+ }
+
+ // Create new directory entry.
+ _, elem := path.Split(name)
+ fi1 = &FileInfo{
+ Path: mname,
+ Name: elem,
+ Qid: 2,
+ Seq: 2,
+ ModTime: time.Now(),
+ Size: int64(len(initPasswd)),
+ IsDir: false,
+ }
+ if _, err := datastore.Put(c, key, fi1); err != nil {
+ return nil, err
+ }
+ }
+
+ return &fi3, nil
+}
+
+func (r *request) write() {
+ data, err := ioutil.ReadAll(r.req.Body)
+ if err != nil {
+ panic(err)
+ }
+
+ var fi *FileInfo
+ var seq int64
+ r.tx(func(c appengine.Context) error {
+ var err error
+ fi, seq, err = write(r.c, r.name, data)
+ return err
+ })
+ updateCacheTime(r.c, seq)
+ r.saveStat(fi)
+}
+
+func write(c appengine.Context, name string, data []byte) (*FileInfo, int64, error) {
+ name, _, key := mangle(c, name)
+
+ // Check that file exists and is not a directory.
+ fi1, err := stat(c, name)
+ if err != nil {
+ return nil, 0, err
+ }
+ if fi1.IsDir {
+ return nil, 0, fmt.Errorf("cannot write to directory")
+ }
+
+ // Fetch and increment root sequence number.
+ rfi, err := stat(c, "/")
+ if err != nil {
+ return nil, 0, err
+ }
+ rfi.Seq++
+
+ // Write data.
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ dkey := datastore.NewKey(c, "FileData", "", fi1.Qid, root)
+ fd := &FileData{data}
+ if _, err := datastore.Put(c, dkey, fd); err != nil {
+ return nil, 0, err
+ }
+
+ // Update directory entry.
+ fi1.Seq = rfi.Seq
+ fi1.Size = int64(len(data))
+ fi1.ModTime = time.Now()
+ if _, err := datastore.Put(c, key, fi1); err != nil {
+ return nil, 0, err
+ }
+
+ // Update sequence numbers all the way to the root.
+ if err := updateSeq(c, name, rfi.Seq, 1); err != nil {
+ return nil, 0, err
+ }
+
+ return fi1, rfi.Seq, nil
+}
+
+func updateSeq(c appengine.Context, name string, seq int64, skip int) error {
+ p := path.Clean(name)
+ for i := 0; ; i++ {
+ if i >= skip {
+ _, _, key := mangle(c, p)
+ var fi FileInfo
+ if err := datastore.Get(c, key, &fi); err != nil {
+ return err
+ }
+ fi.Seq = seq
+ if _, err := datastore.Put(c, key, &fi); err != nil {
+ return err
+ }
+ }
+ if p == "/" {
+ break
+ }
+ p, _ = path.Split(p)
+ p = path.Clean(p)
+ }
+ return nil
+}
+
+func (r *request) remove() {
+ panic("remove not implemented")
+}
+
+func (r *request) create() {
+ var fi *FileInfo
+ var seq int64
+ isDir := r.req.FormValue("dir") == "1"
+ r.tx(func(c appengine.Context) error {
+ var err error
+ fi, seq, err = create(r.c, r.name, isDir, nil)
+ return err
+ })
+ updateCacheTime(r.c, seq)
+ r.saveStat(fi)
+}
+
+func create(c appengine.Context, name string, isDir bool, data []byte) (*FileInfo, int64, error) {
+ name, mname, key := mangle(c, name)
+
+ // File must not exist.
+ fi1, err := stat(c, name)
+ if err == nil {
+ return nil, 0, fmt.Errorf("file already exists")
+ }
+ if err != datastore.ErrNoSuchEntity {
+ return nil, 0, err
+ }
+
+ // Parent must exist and be a directory.
+ p, _ := path.Split(name)
+ fi2, err := stat(c, p)
+ if err != nil {
+ if err == datastore.ErrNoSuchEntity {
+ return nil, 0, fmt.Errorf("parent directory %q does not exist", p)
+ }
+ return nil, 0, err
+ }
+ if !fi2.IsDir {
+ return nil, 0, fmt.Errorf("parent %q is not a directory", p)
+ }
+
+ // Fetch and increment root sequence number.
+ rfi, err := stat(c, "/")
+ if err != nil {
+ return nil, 0, err
+ }
+ rfi.Seq++
+
+ var dataKey int64
+ // Create data object.
+ if !isDir {
+ dataKey = rfi.Seq
+ root := datastore.NewKey(c, "RootKey", "v2:", 0, nil)
+ dkey := datastore.NewKey(c, "FileData", "", dataKey, root)
+ _, err := datastore.Put(c, dkey, &FileData{data})
+ if err != nil {
+ return nil, 0, err
+ }
+ }
+
+ // Create new directory entry.
+ _, elem := path.Split(name)
+ fi1 = &FileInfo{
+ Path: mname,
+ Name: elem,
+ Qid: rfi.Seq,
+ Seq: rfi.Seq,
+ ModTime: time.Now(),
+ Size: int64(len(data)),
+ IsDir: isDir,
+ }
+ if _, err := datastore.Put(c, key, fi1); err != nil {
+ return nil, 0, err
+ }
+
+ // Update sequence numbers all the way to root,
+ // but skip entry we just wrote.
+ if err := updateSeq(c, name, rfi.Seq, 1); err != nil {
+ return nil, 0, err
+ }
+
+ return fi1, rfi.Seq, nil
+}
+
+// Implementation of fs.AppEngine.
+
+func init() {
+ fs.Register(ae{})
+}
+
+type ae struct{}
+
+func tx(c interface{}, f func(c appengine.Context) error) error {
+ return datastore.RunInTransaction(c.(appengine.Context), f, &datastore.TransactionOptions{XG: true})
+}
+
+func (ae) NewContext(req *http.Request) interface{} {
+ return appengine.NewContext(req)
+}
+
+func (ae) User(ctxt interface{}) string {
+ c := ctxt.(appengine.Context)
+ u := user.Current(c)
+ if u == nil {
+ return "?"
+ }
+ return u.String()
+}
+
+type cacheKey struct {
+ t int64
+ name string
+}
+
+func (ae) CacheRead(ctxt interface{}, name, path string) (key interface{}, data []byte, found bool) {
+ c := ctxt.(appengine.Context)
+ t, data, _, err := cacheRead(c, "cache", name, path)
+ return &cacheKey{t, name}, data, err == nil
+}
+
+func (ae) CacheWrite(ctxt, key interface{}, data []byte) {
+ c := ctxt.(appengine.Context)
+ k := key.(*cacheKey)
+ cacheWrite(c, k.t, "cache", k.name, data)
+}
+
+func (ae ae) Read(ctxt interface{}, name string) (data []byte, pfi *proto.FileInfo, err error) {
+ c := ctxt.(appengine.Context)
+ name = path.Clean("/"+name)
+ if chatty {
+ c.Infof("AE Read %s", name)
+ }
+ _, data, pfi, err = cacheRead(c, "data", name, name)
+ if err != nil {
+ err = fmt.Errorf("Read %q: %v", name, err)
+ }
+ return
+}
+
+func (ae) Write(ctxt interface{}, path string, data []byte) error {
+ var seq int64
+ err := tx(ctxt, func(c appengine.Context) error {
+ _, err := stat(c, path)
+ if err != nil {
+ _, seq, err = create(c, path, false, data)
+ } else {
+ _, seq, err = write(c, path, data)
+ }
+ return err
+ })
+ if seq != 0 {
+ updateCacheTime(ctxt.(appengine.Context), seq)
+ }
+ if err != nil {
+ err = fmt.Errorf("Write %q: %v", path, err)
+ }
+ return err
+}
+
+func (ae) Remove(ctxt interface{}, path string) error {
+ return fmt.Errorf("remove not implemented")
+}
+
+func (ae) Mkdir(ctxt interface{}, path string) error {
+ var seq int64
+ err := tx(ctxt, func(c appengine.Context) error {
+ var err error
+ _, seq, err = create(c, path, true, nil)
+ return err
+ })
+ if seq != 0 {
+ updateCacheTime(ctxt.(appengine.Context), seq)
+ }
+ if err != nil {
+ err = fmt.Errorf("Mkdir %q: %v", path, err)
+ }
+ return err
+}
+
+func (ae) Criticalf(ctxt interface{}, format string, args ...interface{}) {
+ ctxt.(appengine.Context).Criticalf(format, args...)
+}
+
+type readDirCacheEntry struct {
+ Dir []proto.FileInfo
+ Error string
+}
+
+func (ae) ReadDir(ctxt interface{}, name string) (dir []proto.FileInfo, err error) {
+ c := ctxt.(appengine.Context)
+ name = path.Clean("/"+name)
+ t, data, _, err := cacheRead(c, "dir", name, name)
+ if err == nil {
+ var e readDirCacheEntry
+ if err := json.Unmarshal(data, &e); err == nil {
+ if chatty {
+ c.Infof("cached ReadDir %q", name)
+ }
+ if e.Error != "" {
+ return nil, errors.New(e.Error)
+ }
+ return e.Dir, nil
+ }
+ c.Criticalf("unmarshal cached dir %q: %v", name)
+ }
+ err = tx(ctxt, func(c appengine.Context) error {
+ var err error
+ dir, err = readdirRaw(c, name)
+ return err
+ })
+ var e readDirCacheEntry
+ e.Dir = dir
+ if err != nil {
+ err = fmt.Errorf("ReadDir %q: %v", name, err)
+ e.Error = err.Error()
+ }
+ if data, err := json.Marshal(&e); err != nil {
+ c.Criticalf("json marshal cached dir: %v", err)
+ } else {
+ c.Criticalf("caching dir %q@%d %d bytes", name, t, len(data))
+ cacheWrite(c, t, "dir", name, data)
+ }
+ return
+}
+
+// Caching of file system data.
+//
+// The cache stores entries under keys of the form time,space,name,
+// where time is the time at which the entry is valid for, space is a name
+// space identifier, and name is an arbitrary name.
+//
+// A key of the form t,mtime,path maps to an integer value giving the
+// modification time of the named path at root time t.
+// The special key 0,mtime,/ is an integer giving the current time at the root.
+//
+// A key of the form t,data,path maps to the content of path at time t.
+//
+// Thus, a read from path should first obtain the root time,
+// then obtain the modification time for the path at that root time
+// then obtain the data for that path.
+// t1 = get(0,mtime,/)
+// t2 = get(t1,mtime,path)
+// data = get(t2,data,path)
+//
+// The API allows clients to cache their own data too, with expiry tied to
+// the modification time of a particular path (file or directory). To look
+// up one of those, we use:
+// t1 = get(0,mtime,/)
+// t2 = get(t1,mtime,path)
+// data = get(t2,clientdata,name)
+//
+// To store data in the cache, the t1, t2 should be determined before reading
+// from datastore. Then the data should be saved under t2. This ensures
+// that if a datastore update happens after the read but before the cache write,
+// we'll be writing to an entry that will no longer be used (t2).
+
+const rootMemcacheKey = "0,mtime,/"
+
+func updateCacheTime(c appengine.Context, seq int64) {
+ const key = rootMemcacheKey
+ bseq := []byte(strconv.FormatInt(seq, 10))
+ for tries := 0; tries < 10; tries++ {
+ item, err := memcache.Get(c, key)
+ if err != nil {
+ c.Infof("memcache.Get %q: %v", key, err)
+ err = memcache.Add(c, &memcache.Item{Key: key, Value: bseq})
+ if err == nil {
+ c.Infof("memcache.Add %q %q ok", key, bseq)
+ return
+ }
+ c.Infof("memcache.Add %q %q: %v", key, bseq, err)
+ }
+ v, err := strconv.ParseInt(string(item.Value), 10, 64)
+ if err != nil {
+ c.Criticalf("memcache.Get %q = %q (%v)", key, item.Value, err)
+ return
+ }
+ if v >= seq {
+ return
+ }
+ item.Value = bseq
+ err = memcache.CompareAndSwap(c, item)
+ if err == nil {
+ c.Infof("memcache.CAS %q %d->%d ok", key, v, seq)
+ return
+ }
+ c.Infof("memcache.CAS %q %d->%d: %v", key, v, seq, err)
+ }
+ c.Criticalf("repeatedly failed to update root key")
+}
+
+func cacheTime(c appengine.Context) (t int64, err error) {
+ const key = rootMemcacheKey
+ item, err := memcache.Get(c, key)
+ if err == nil {
+ v, err := strconv.ParseInt(string(item.Value), 10, 64)
+ if err == nil {
+ if chatty {
+ c.Infof("cacheTime %q = %v", key, v)
+ }
+ return v, nil
+ }
+ c.Criticalf("memcache.Get %q = %q (%v) - deleting", key, item.Value, err)
+ memcache.Delete(c, key)
+ }
+ fi, err := stat(c, "/")
+ if err != nil {
+ c.Criticalf("stat /: %v", err)
+ return 0, err
+ }
+ updateCacheTime(c, fi.Seq)
+ return fi.Seq, nil
+}
+
+func cachePathTime(c appengine.Context, path string) (t int64, err error) {
+ t, err = cacheTime(c)
+ if err != nil {
+ return 0, err
+ }
+
+ key := fmt.Sprintf("%d,mtime,%s", t, path)
+ item, err := memcache.Get(c, key)
+ if err == nil {
+ v, err := strconv.ParseInt(string(item.Value), 10, 64)
+ if err == nil {
+ if chatty {
+ c.Infof("cachePathTime %q = %v", key, v)
+ }
+ return v, nil
+ }
+ c.Criticalf("memcache.Get %q = %q (%v) - deleting", key, item.Value, err)
+ memcache.Delete(c, key)
+ }
+
+ var seq int64
+ if fi, err := stat(c, path); err == nil {
+ seq = fi.Seq
+ }
+
+ c.Infof("cachePathTime save %q = %v", key, seq)
+ item = &memcache.Item{Key: key, Value: []byte(strconv.FormatInt(seq, 10))}
+ if err := memcache.Set(c, item); err != nil {
+ c.Criticalf("memcache.Set %q %q: %v", key, item.Value, err)
+ }
+ return seq, nil
+}
+
+type statCacheEntry struct {
+ FileInfo *proto.FileInfo
+ Error string
+}
+
+func cacheRead(c appengine.Context, kind, name, path string) (mtime int64, data []byte, pfi *proto.FileInfo, err error) {
+ for tries := 0; tries < 10; tries++ {
+ t, err := cachePathTime(c, path)
+ if err != nil {
+ return 0, nil, nil, err
+ }
+
+ key := fmt.Sprintf("%d,%s,%s", t, kind, name)
+ item, err := memcache.Get(c, key)
+ var data []byte
+ if item != nil {
+ data = item.Value
+ }
+ if err != nil {
+ c.Infof("memcache miss %q %v", key, err)
+ } else if chatty {
+ c.Infof("memcache hit %q (%d bytes)", key, len(data))
+ }
+ if kind != "data" {
+ // Not a file; whatever memcache says is all we have.
+ return t, data, nil, err
+ }
+
+ // Load stat from cache (includes negative entry).
+ statkey := fmt.Sprintf("%d,stat,%s", t, name)
+ var st statCacheEntry
+ _, err = memcache.JSON.Get(c, statkey, &st)
+ if err == nil {
+ if st.Error != "" {
+ if chatty {
+ c.Infof("memcache hit stat error %q %q", statkey, st.Error)
+ }
+ err = errors.New(st.Error)
+ } else {
+ if chatty {
+ c.Infof("memcache hit stat %q", statkey)
+ }
+ }
+ if err != nil || data != nil {
+ return t, data, st.FileInfo, err
+ }
+ }
+
+ // Need stat, or maybe stat+data.
+ var fi *FileInfo
+ if data != nil {
+ c.Infof("stat %q", name)
+ fi, err = stat(c, name)
+ if err == nil && fi.Seq != t {
+ c.Criticalf("loaded %s but found stat %d", key, fi.Seq)
+ continue
+ }
+ } else {
+ c.Infof("read %q", name)
+ fi, data, err = read(c, name)
+ if err == nil && fi.Seq != t {
+ c.Infof("loaded %s but found read %d", key, fi.Seq)
+ t = fi.Seq
+ key = fmt.Sprintf("%d,data,%s", t, name)
+ statkey = fmt.Sprintf("%d,stat,%s", t, name)
+ }
+
+ // Save data to memcache.
+ if err == nil {
+ if true || chatty {
+ c.Infof("save data in memcache %q", key)
+ }
+ item := &memcache.Item{Key: key, Value: data}
+ if err := memcache.Set(c, item); err != nil {
+ c.Criticalf("failed to cache %s: %v", key, err)
+ }
+ }
+ }
+
+ // Cache stat, including error.
+ st = statCacheEntry{}
+ if fi != nil {
+ st.FileInfo = &proto.FileInfo{
+ Name: fi.Name,
+ ModTime: fi.ModTime,
+ Size: fi.Size,
+ IsDir: fi.IsDir,
+ }
+ }
+ if err != nil {
+ st.Error = err.Error()
+ // If this is a deadline exceeded, do not cache.
+ if strings.Contains(st.Error, "Canceled") || strings.Contains(st.Error, "Deadline") {
+ return t, data, st.FileInfo, err
+ }
+ }
+ if chatty {
+ c.Infof("save stat in memcache %q", statkey)
+ }
+ if err := memcache.JSON.Set(c, &memcache.Item{Key: statkey, Object: &st}); err != nil {
+ c.Criticalf("failed to cache %s: %v", statkey, err)
+ }
+
+ // Done!
+ return t, data, st.FileInfo, err
+ }
+
+ c.Criticalf("failed repeatedly in cacheRead")
+ return 0, nil, nil, errors.New("cacheRead loop failed")
+}
+
+func cacheWrite(c appengine.Context, t int64, kind, name string, data []byte) error {
+ mkey := fmt.Sprintf("%d,%s,%s", t, kind, name)
+ if true || chatty {
+ c.Infof("cacheWrite %s %d bytes", mkey, len(data))
+ }
+ err := memcache.Set(c, &memcache.Item{Key: mkey, Value: data})
+ if err != nil {
+ c.Criticalf("cacheWrite memcache.Set %q: %v", mkey, err)
+ }
+ return err
+}
diff --git a/vendor/github.com/mattermost/rsc/arq/arq.go b/vendor/github.com/mattermost/rsc/arq/arq.go
new file mode 100644
index 000000000..85a5138e9
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/arq/arq.go
@@ -0,0 +1,663 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package arq implements read-only access to Arq backups stored on S3.
+// Arq is a Mac backup tool (http://www.haystacksoftware.com/arq/)
+// but the package can read the backups regardless of operating system.
+package arq
+
+import (
+ "bytes"
+ "compress/gzip"
+ "encoding/binary"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "time"
+
+ "github.com/mattermost/rsc/plist"
+ "launchpad.net/goamz/aws"
+ "launchpad.net/goamz/s3"
+)
+
+// A Conn represents a connection to an S3 server holding Arq backups.
+type Conn struct {
+ b *s3.Bucket
+ cache string
+ altCache string
+}
+
+// cachedir returns the canonical directory in which to cache data.
+func cachedir() string {
+ if runtime.GOOS == "darwin" {
+ return filepath.Join(os.Getenv("HOME"), "Library/Caches/arq-cache")
+ }
+ return filepath.Join(os.Getenv("HOME"), ".cache/arq-cache")
+}
+
+// Dial establishes a connection to an S3 server holding Arq backups.
+func Dial(auth aws.Auth) (*Conn, error) {
+ buck := fmt.Sprintf("%s-com-haystacksoftware-arq", strings.ToLower(auth.AccessKey))
+ b := s3.New(auth, aws.USEast).Bucket(buck)
+ c := &Conn{
+ b: b,
+ cache: filepath.Join(cachedir(), buck),
+ }
+ if runtime.GOOS == "darwin" {
+ c.altCache = filepath.Join(os.Getenv("HOME"), "Library/Arq/Cache.noindex/"+buck)
+ }
+
+ // Check that the bucket works by listing computers (relatively cheap).
+ if _, err := c.list("", "/", 10); err != nil {
+ return nil, err
+ }
+
+ // Create S3 lookaside cache directory.
+
+ return c, nil
+}
+
+func (c *Conn) list(prefix, delim string, max int) (*s3.ListResp, error) {
+ resp, err := c.b.List(prefix, delim, "", max)
+ if err != nil {
+ return nil, err
+ }
+ ret := resp
+ for max == 0 && resp.IsTruncated {
+ last := resp.Contents[len(resp.Contents)-1].Key
+ resp, err = c.b.List(prefix, delim, last, max)
+ if err != nil {
+ return ret, err
+ }
+ ret.Contents = append(ret.Contents, resp.Contents...)
+ ret.CommonPrefixes = append(ret.CommonPrefixes, resp.CommonPrefixes...)
+ }
+ return ret, nil
+}
+
+func (c *Conn) altCachePath(name string) string {
+ if c.altCache == "" || !strings.Contains(name, "/packsets/") {
+ return ""
+ }
+ i := strings.Index(name, "-trees/")
+ if i < 0 {
+ i = strings.Index(name, "-blobs/")
+ if i < 0 {
+ return ""
+ }
+ }
+ i += len("-trees/") + 2
+ if i >= len(name) {
+ return ""
+ }
+ return filepath.Join(c.altCache, name[:i]+"/"+name[i:])
+}
+
+func (c *Conn) cget(name string) (data []byte, err error) {
+ cache := filepath.Join(c.cache, name)
+ f, err := os.Open(cache)
+ if err == nil {
+ defer f.Close()
+ return ioutil.ReadAll(f)
+ }
+ if altCache := c.altCachePath(name); altCache != "" {
+ f, err := os.Open(altCache)
+ if err == nil {
+ defer f.Close()
+ return ioutil.ReadAll(f)
+ }
+ }
+
+ data, err = c.bget(name)
+ if err != nil {
+ return nil, err
+ }
+
+ dir, _ := filepath.Split(cache)
+ os.MkdirAll(dir, 0700)
+ ioutil.WriteFile(cache, data, 0600)
+ return data, nil
+}
+
+func (c *Conn) bget(name string) (data []byte, err error) {
+ for i := 0; ; {
+ data, err = c.b.Get(name)
+ if err == nil {
+ break
+ }
+ if i++; i >= 5 {
+ return nil, err
+ }
+ log.Print(err)
+ }
+ return data, nil
+}
+
+func (c *Conn) DeleteCache() {
+ os.RemoveAll(c.cache)
+}
+
+// Computers returns a list of the computers with backups available on the S3 server.
+func (c *Conn) Computers() ([]*Computer, error) {
+ // Each backup is a top-level directory with a computerinfo file in it.
+ list, err := c.list("", "/", 0)
+ if err != nil {
+ return nil, err
+ }
+ var out []*Computer
+ for _, p := range list.CommonPrefixes {
+ data, err := c.bget(p + "computerinfo")
+ if err != nil {
+ continue
+ }
+ var info computerInfo
+ if err := plist.Unmarshal(data, &info); err != nil {
+ return nil, err
+ }
+
+ comp := &Computer{
+ Name: info.ComputerName,
+ User: info.UserName,
+ UUID: p[:len(p)-1],
+ conn: c,
+ index: map[score]ientry{},
+ }
+
+ salt, err := c.cget(p + "salt")
+ if err != nil {
+ return nil, err
+ }
+ comp.crypto.salt = salt
+
+ out = append(out, comp)
+ }
+ return out, nil
+}
+
+// A Computer represents a computer with backups (Folders).
+type Computer struct {
+ Name string // name of computer
+ User string // name of user
+ UUID string
+ conn *Conn
+ crypto cryptoState
+ index map[score]ientry
+}
+
+// Folders returns a list of the folders that have been backed up on the computer.
+func (c *Computer) Folders() ([]*Folder, error) {
+ // Each folder is a file under computer/buckets/.
+ list, err := c.conn.list(c.UUID+"/buckets/", "", 0)
+ if err != nil {
+ return nil, err
+ }
+ var out []*Folder
+ for _, obj := range list.Contents {
+ data, err := c.conn.bget(obj.Key)
+ if err != nil {
+ return nil, err
+ }
+ var info folderInfo
+ if err := plist.Unmarshal(data, &info); err != nil {
+ return nil, err
+ }
+ out = append(out, &Folder{
+ Path: info.LocalPath,
+ uuid: info.BucketUUID,
+ comp: c,
+ conn: c.conn,
+ })
+ }
+ return out, nil
+}
+
+// Unlock records the password to use when decrypting
+// backups from this computer. It must be called before calling Trees
+// in any folder obtained for this computer.
+func (c *Computer) Unlock(pw string) {
+ c.crypto.unlock(pw)
+}
+
+func (c *Computer) scget(sc score) ([]byte, error) {
+ if c.crypto.c == nil {
+ return nil, fmt.Errorf("computer not yet unlocked")
+ }
+
+ var data []byte
+ var err error
+ ie, ok := c.index[sc]
+ if ok {
+ data, err = c.conn.cget(ie.File)
+ if err != nil {
+ return nil, err
+ }
+
+ //fmt.Printf("offset size %d %d\n", ie.Offset, ie.Size)
+ if len(data) < int(ie.Offset+ie.Size) {
+ return nil, fmt.Errorf("short pack block")
+ }
+
+ data = data[ie.Offset:]
+ if ie.Size < 1+8+1+8+8 {
+ return nil, fmt.Errorf("short pack block")
+ }
+
+ bo := binary.BigEndian
+
+ if data[0] != 1 {
+ return nil, fmt.Errorf("missing mime type")
+ }
+ n := bo.Uint64(data[1:])
+ if 1+8+n > uint64(len(data)) {
+ return nil, fmt.Errorf("malformed mime type")
+ }
+ mimeType := data[1+8 : 1+8+n]
+ data = data[1+8+n:]
+
+ n = bo.Uint64(data[1:])
+ if 1+8+n > uint64(len(data)) {
+ return nil, fmt.Errorf("malformed name")
+ }
+ name := data[1+8 : 1+8+n]
+ data = data[1+8+n:]
+
+ _, _ = mimeType, name
+ // fmt.Printf("%s %s\n", mimeType, name)
+
+ n = bo.Uint64(data[0:])
+ if int64(n) != ie.Size {
+ return nil, fmt.Errorf("unexpected data length %d %d", n, ie.Size)
+ }
+ if 8+n > uint64(len(data)) {
+ return nil, fmt.Errorf("short data %d %d", 8+n, len(data))
+ }
+
+ data = data[8 : 8+n]
+ } else {
+ data, err = c.conn.cget(c.UUID + "/objects/" + sc.String())
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+
+ data = c.crypto.decrypt(data)
+ return data, nil
+}
+
+// A Folder represents a backed-up tree on a computer.
+type Folder struct {
+ Path string // root of tree of last backup
+ uuid string
+ comp *Computer
+ conn *Conn
+}
+
+// Load loads xxx
+func (f *Folder) Load() error {
+ if err := f.comp.loadPack(f.uuid, "-trees"); err != nil {
+ return err
+ }
+ if err := f.comp.loadPack(f.uuid, "-blobs"); err != nil {
+ return err
+ }
+ return nil
+}
+
+func (c *Computer) loadPack(fold, suf string) error {
+ list, err := c.conn.list(c.UUID+"/packsets/"+fold+suf+"/", "", 0)
+ if err != nil {
+ return err
+ }
+
+ for _, obj := range list.Contents {
+ if !strings.HasSuffix(obj.Key, ".index") {
+ continue
+ }
+ data, err := c.conn.cget(obj.Key)
+ if err != nil {
+ return err
+ }
+ // fmt.Printf("pack %s\n", obj.Key)
+ c.saveIndex(obj.Key[:len(obj.Key)-len(".index")]+".pack", data)
+ }
+ return nil
+}
+
+func (c *Computer) saveIndex(file string, data []byte) error {
+ const (
+ headerSize = 4 + 4 + 4*256
+ entrySize = 8 + 8 + 20 + 4
+ trailerSize = 20
+ )
+ bo := binary.BigEndian
+ if len(data) < headerSize+trailerSize {
+ return fmt.Errorf("short index")
+ }
+ i := len(data) - trailerSize
+ sum1 := sha(data[:i])
+ sum2 := binaryScore(data[i:])
+ if !sum1.Equal(sum2) {
+ return fmt.Errorf("invalid sha index")
+ }
+
+ obj := data[headerSize : len(data)-trailerSize]
+ n := len(obj) / entrySize
+ if n*entrySize != len(obj) {
+ return fmt.Errorf("misaligned index %d %d", n*entrySize, len(obj))
+ }
+ nn := bo.Uint32(data[headerSize-4:])
+ if int(nn) != n {
+ return fmt.Errorf("inconsistent index %d %d\n", nn, n)
+ }
+
+ for i := 0; i < n; i++ {
+ e := obj[i*entrySize:]
+ var ie ientry
+ ie.File = file
+ ie.Offset = int64(bo.Uint64(e[0:]))
+ ie.Size = int64(bo.Uint64(e[8:]))
+ ie.Score = binaryScore(e[16:])
+ c.index[ie.Score] = ie
+ }
+ return nil
+}
+
+// Trees returns a list of the individual backup snapshots for the folder.
+// Note that different trees from the same Folder might have different Paths
+// if the folder was "relocated" using the Arq interface.
+func (f *Folder) Trees() ([]*Tree, error) {
+ data, err := f.conn.bget(f.comp.UUID + "/bucketdata/" + f.uuid + "/refs/heads/master")
+ if err != nil {
+ return nil, err
+ }
+ sc := hexScore(string(data))
+ if err != nil {
+ return nil, err
+ }
+
+ var out []*Tree
+ for {
+ data, err = f.comp.scget(sc)
+ if err != nil {
+ return nil, err
+ }
+
+ var com commit
+ if err := unpack(data, &com); err != nil {
+ return nil, err
+ }
+
+ var info folderInfo
+ if err := plist.Unmarshal(com.BucketXML, &info); err != nil {
+ return nil, err
+ }
+
+ t := &Tree{
+ Time: com.CreateTime,
+ Path: info.LocalPath,
+ Score: com.Tree.Score,
+
+ commit: com,
+ comp: f.comp,
+ folder: f,
+ info: info,
+ }
+ out = append(out, t)
+
+ if len(com.ParentCommits) == 0 {
+ break
+ }
+
+ sc = com.ParentCommits[0].Score
+ }
+
+ for i, n := 0, len(out)-1; i < n-i; i++ {
+ out[i], out[n-i] = out[n-i], out[i]
+ }
+ return out, nil
+}
+
+func (f *Folder) Trees2() ([]*Tree, error) {
+ list, err := f.conn.list(f.comp.UUID+"/bucketdata/"+f.uuid+"/refs/logs/master/", "", 0)
+ if err != nil {
+ return nil, err
+ }
+
+ var out []*Tree
+ for _, obj := range list.Contents {
+ data, err := f.conn.cget(obj.Key)
+ if err != nil {
+ return nil, err
+ }
+ var l reflog
+ if err := plist.Unmarshal(data, &l); err != nil {
+ return nil, err
+ }
+
+ sc := hexScore(l.NewHeadSHA1)
+ if err != nil {
+ return nil, err
+ }
+
+ data, err = f.comp.scget(sc)
+ if err != nil {
+ return nil, err
+ }
+
+ var com commit
+ if err := unpack(data, &com); err != nil {
+ return nil, err
+ }
+
+ var info folderInfo
+ if err := plist.Unmarshal(com.BucketXML, &info); err != nil {
+ return nil, err
+ }
+
+ t := &Tree{
+ Time: com.CreateTime,
+ Path: info.LocalPath,
+ Score: com.Tree.Score,
+
+ commit: com,
+ comp: f.comp,
+ folder: f,
+ info: info,
+ }
+ out = append(out, t)
+ }
+ return out, nil
+}
+
+// A Tree represents a single backed-up file tree snapshot.
+type Tree struct {
+ Time time.Time // time back-up completed
+ Path string // root of backed-up tree
+ Score [20]byte
+
+ comp *Computer
+ folder *Folder
+ commit commit
+ info folderInfo
+
+ raw tree
+ haveRaw bool
+}
+
+// Root returns the File for the tree's root directory.
+func (t *Tree) Root() (*File, error) {
+ if !t.haveRaw {
+ data, err := t.comp.scget(t.Score)
+ if err != nil {
+ return nil, err
+ }
+ if err := unpack(data, &t.raw); err != nil {
+ return nil, err
+ }
+ t.haveRaw = true
+ }
+
+ dir := &File{
+ t: t,
+ dir: &t.raw,
+ n: &nameNode{"/", node{IsTree: true}},
+ }
+ return dir, nil
+}
+
+// A File represents a file or directory in a tree.
+type File struct {
+ t *Tree
+ n *nameNode
+ dir *tree
+ byName map[string]*nameNode
+}
+
+func (f *File) loadDir() error {
+ if f.dir == nil {
+ data, err := f.t.comp.scget(f.n.Node.Blob[0].Score)
+ if err != nil {
+ return err
+ }
+ var dir tree
+ if err := unpack(data, &dir); err != nil {
+ return err
+ }
+ f.dir = &dir
+ }
+ return nil
+}
+
+func (f *File) Lookup(name string) (*File, error) {
+ if !f.n.Node.IsTree {
+ return nil, fmt.Errorf("lookup in non-directory")
+ }
+ if f.byName == nil {
+ if err := f.loadDir(); err != nil {
+ return nil, err
+ }
+ f.byName = map[string]*nameNode{}
+ for _, n := range f.dir.Nodes {
+ f.byName[n.Name] = n
+ }
+ }
+ n := f.byName[name]
+ if n == nil {
+ return nil, fmt.Errorf("no entry %q", name)
+ }
+ return &File{t: f.t, n: n}, nil
+}
+
+func (f *File) Stat() *Dirent {
+ if f.n.Node.IsTree {
+ if err := f.loadDir(); err == nil {
+ return &Dirent{
+ Name: f.n.Name,
+ ModTime: f.dir.Mtime.Time(),
+ Mode: fileMode(f.dir.Mode),
+ Size: 0,
+ }
+ }
+ }
+ return &Dirent{
+ Name: f.n.Name,
+ ModTime: f.n.Node.Mtime.Time(),
+ Mode: fileMode(f.n.Node.Mode),
+ Size: int64(f.n.Node.UncompressedSize),
+ }
+}
+
+type Dirent struct {
+ Name string
+ ModTime time.Time
+ Mode os.FileMode
+ Size int64
+}
+
+func (f *File) ReadDir() ([]Dirent, error) {
+ if !f.n.Node.IsTree {
+ return nil, fmt.Errorf("ReadDir in non-directory")
+ }
+ if err := f.loadDir(); err != nil {
+ return nil, err
+ }
+ var out []Dirent
+ for _, n := range f.dir.Nodes {
+ out = append(out, Dirent{
+ Name: n.Name,
+ ModTime: n.Node.Mtime.Time(),
+ Mode: fileMode(n.Node.Mode),
+ })
+ }
+ return out, nil
+}
+
+func (f *File) Open() (io.ReadCloser, error) {
+ return &fileReader{t: f.t, blob: f.n.Node.Blob, n: &f.n.Node}, nil
+}
+
+type fileReader struct {
+ t *Tree
+ n *node
+ blob []sscore
+ cur io.Reader
+ close []io.Closer
+}
+
+func (f *fileReader) Read(b []byte) (int, error) {
+ for {
+ if f.cur != nil {
+ n, err := f.cur.Read(b)
+ if n > 0 || err != nil && err != io.EOF {
+ return n, err
+ }
+ for _, cl := range f.close {
+ cl.Close()
+ }
+ f.close = f.close[:0]
+ f.cur = nil
+ }
+
+ if len(f.blob) == 0 {
+ break
+ }
+
+ // TODO: Get a direct reader, not a []byte.
+ data, err := f.t.comp.scget(f.blob[0].Score)
+ if err != nil {
+ return 0, err
+ }
+ rc := ioutil.NopCloser(bytes.NewBuffer(data))
+
+ if f.n.CompressData {
+ gz, err := gzip.NewReader(rc)
+ if err != nil {
+ rc.Close()
+ return 0, err
+ }
+ f.close = append(f.close, gz)
+ f.cur = gz
+ } else {
+ f.cur = rc
+ }
+ f.close = append(f.close, rc)
+ f.blob = f.blob[1:]
+ }
+
+ return 0, io.EOF
+}
+
+func (f *fileReader) Close() error {
+ for _, cl := range f.close {
+ cl.Close()
+ }
+ f.close = f.close[:0]
+ f.cur = nil
+ return nil
+}
diff --git a/vendor/github.com/mattermost/rsc/arq/arqfs/main.go b/vendor/github.com/mattermost/rsc/arq/arqfs/main.go
new file mode 100644
index 000000000..9e9001133
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/arq/arqfs/main.go
@@ -0,0 +1,247 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+/*
+Arqfs implements a file system interface to a collection of Arq backups.
+
+ usage: arqfs [-m mtpt]
+
+Arqfs mounts the Arq backups on the file system directory mtpt,
+(default /mnt/arq). The directory must exist and be writable by
+the current user.
+
+Arq
+
+Arq is an Amazon S3-based backup system for OS X and sold by
+Haystack Software (http://www.haystacksoftware.com/arq/).
+This software reads backups written by Arq.
+It is not affiliated with or connected to Haystack Software.
+
+Passwords
+
+Arqfs reads necessary passwords from the OS X keychain.
+It expects at least two entries:
+
+The keychain entry for s3.amazonaws.com should list the Amazon S3 access ID
+as user name and the S3 secret key as password.
+
+Each backup being accessed must have its own keychain entry for
+host arq.swtch.com, listing the backup UUID as user name and the encryption
+password as the password.
+
+Arqfs will not prompt for passwords or create these entries itself: they must
+be created using the Keychain Access application.
+
+FUSE
+
+Arqfs creates a virtual file system using the FUSE file system layer.
+On OS X, it requires OSXFUSE (http://osxfuse.github.com/).
+
+Cache
+
+Reading the Arq backups efficiently requires caching directory tree information
+on local disk instead of reading the same data from S3 repeatedly. Arqfs caches
+data downloaded from S3 in $HOME/Library/Caches/arq-cache/.
+If an Arq installation is present on the same machine, arqfs will look in
+its cache ($HOME/Library/Arq/Cache.noindex) first, but arqfs will not
+write to Arq's cache directory.
+
+Bugs
+
+Arqfs only runs on OS X for now, because both FUSE and the keychain access
+packages have not been ported to other systems yet.
+
+Both Arqfs and the FUSE package on which it is based have seen only light
+use. There are likely to be bugs. Mail rsc@swtch.com with reports.
+
+*/
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+ "os/exec"
+ "syscall"
+
+ "github.com/mattermost/rsc/arq"
+ "github.com/mattermost/rsc/fuse"
+ "github.com/mattermost/rsc/keychain"
+ "launchpad.net/goamz/aws"
+)
+
+var mtpt = flag.String("m", "/mnt/arq", "")
+
+func main() {
+ log.SetFlags(0)
+
+ if len(os.Args) == 3 && os.Args[1] == "MOUNTSLAVE" {
+ *mtpt = os.Args[2]
+ mountslave()
+ return
+ }
+
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, "usage: arqfs [-m /mnt/arq]\n")
+ os.Exit(2)
+ }
+ flag.Parse()
+ if len(flag.Args()) != 0 {
+ flag.Usage()
+ }
+
+ // Run in child so that we can exit once child is running.
+ r, w, err := os.Pipe()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ cmd := exec.Command(os.Args[0], "MOUNTSLAVE", *mtpt)
+ cmd.Stdout = w
+ cmd.Stderr = os.Stderr
+ if err := cmd.Start(); err != nil {
+ log.Fatalf("mount process: %v", err)
+ }
+ w.Close()
+
+ buf := make([]byte, 10)
+ n, _ := r.Read(buf)
+ if n != 2 || string(buf[0:2]) != "OK" {
+ os.Exit(1)
+ }
+
+ fmt.Fprintf(os.Stderr, "mounted on %s\n", *mtpt)
+}
+
+func mountslave() {
+ stdout, _ := syscall.Dup(1)
+ syscall.Dup2(2, 1)
+
+ access, secret, err := keychain.UserPasswd("s3.amazonaws.com", "")
+ if err != nil {
+ log.Fatal(err)
+ }
+ auth := aws.Auth{AccessKey: access, SecretKey: secret}
+
+ conn, err := arq.Dial(auth)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ comps, err := conn.Computers()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ fs := &fuse.Tree{}
+ for _, c := range comps {
+ fmt.Fprintf(os.Stderr, "scanning %s...\n", c.Name)
+
+ // TODO: Better password protocol.
+ _, pw, err := keychain.UserPasswd("arq.swtch.com", c.UUID)
+ if err != nil {
+ log.Fatal(err)
+ }
+ c.Unlock(pw)
+
+ folders, err := c.Folders()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ lastDate := ""
+ n := 0
+ for _, f := range folders {
+ if err := f.Load(); err != nil {
+ log.Fatal(err)
+ }
+ trees, err := f.Trees()
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, t := range trees {
+ y, m, d := t.Time.Date()
+ date := fmt.Sprintf("%04d/%02d%02d", y, m, d)
+ suffix := ""
+ if date == lastDate {
+ n++
+ suffix = fmt.Sprintf(".%d", n)
+ } else {
+ n = 0
+ }
+ lastDate = date
+ f, err := t.Root()
+ if err != nil {
+ log.Print(err)
+ }
+ // TODO: Pass times to fs.Add.
+ // fmt.Fprintf(os.Stderr, "%v %s %x\n", t.Time, c.Name+"/"+date+suffix+"/"+t.Path, t.Score)
+ fs.Add(c.Name+"/"+date+suffix+"/"+t.Path, &fuseNode{f})
+ }
+ }
+ }
+
+ fmt.Fprintf(os.Stderr, "mounting...\n")
+
+ c, err := fuse.Mount(*mtpt)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer exec.Command("umount", *mtpt).Run()
+
+ syscall.Write(stdout, []byte("OK"))
+ syscall.Close(stdout)
+ c.Serve(fs)
+}
+
+type fuseNode struct {
+ arq *arq.File
+}
+
+func (f *fuseNode) Attr() fuse.Attr {
+ de := f.arq.Stat()
+ return fuse.Attr{
+ Mode: de.Mode,
+ Mtime: de.ModTime,
+ Size: uint64(de.Size),
+ }
+}
+
+func (f *fuseNode) Lookup(name string, intr fuse.Intr) (fuse.Node, fuse.Error) {
+ ff, err := f.arq.Lookup(name)
+ if err != nil {
+ return nil, fuse.ENOENT
+ }
+ return &fuseNode{ff}, nil
+}
+
+func (f *fuseNode) ReadDir(intr fuse.Intr) ([]fuse.Dirent, fuse.Error) {
+ adir, err := f.arq.ReadDir()
+ if err != nil {
+ return nil, fuse.EIO
+ }
+ var dir []fuse.Dirent
+ for _, ade := range adir {
+ dir = append(dir, fuse.Dirent{
+ Name: ade.Name,
+ })
+ }
+ return dir, nil
+}
+
+// TODO: Implement Read+Release, not ReadAll, to avoid giant buffer.
+func (f *fuseNode) ReadAll(intr fuse.Intr) ([]byte, fuse.Error) {
+ rc, err := f.arq.Open()
+ if err != nil {
+ return nil, fuse.EIO
+ }
+ defer rc.Close()
+ data, err := ioutil.ReadAll(rc)
+ if err != nil {
+ return data, fuse.EIO
+ }
+ return data, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/arq/crypto.go b/vendor/github.com/mattermost/rsc/arq/crypto.go
new file mode 100644
index 000000000..e567ec36d
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/arq/crypto.go
@@ -0,0 +1,93 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package arq
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/sha1"
+ "hash"
+ "log"
+
+ "bitbucket.org/taruti/pbkdf2.go" // TODO: Pull in copy
+)
+
+type cryptoState struct {
+ c cipher.Block
+ iv []byte
+ salt []byte
+}
+
+func (c *cryptoState) unlock(pw string) {
+ const (
+ iter = 1000
+ keyLen = 48
+ aesKeyLen = 32
+ aesIVLen = 16
+ )
+ key1 := pbkdf2.Pbkdf2([]byte(pw), c.salt, iter, sha1.New, keyLen)
+ var key2 []byte
+ key2, c.iv = bytesToKey(sha1.New, c.salt, key1, iter, aesKeyLen, aesIVLen)
+ c.c, _ = aes.NewCipher(key2)
+}
+
+func (c *cryptoState) decrypt(data []byte) []byte {
+ dec := cipher.NewCBCDecrypter(c.c, c.iv)
+ if len(data)%aes.BlockSize != 0 {
+ log.Fatal("bad block")
+ }
+ dec.CryptBlocks(data, data)
+ // fmt.Printf("% x\n", data)
+ // fmt.Printf("%s\n", data)
+
+ // unpad
+ {
+ n := len(data)
+ p := int(data[n-1])
+ if p == 0 || p > aes.BlockSize {
+ log.Fatal("impossible padding")
+ }
+ for i := 0; i < p; i++ {
+ if data[n-1-i] != byte(p) {
+ log.Fatal("bad padding")
+ }
+ }
+ data = data[:n-p]
+ }
+ return data
+}
+
+func sha(data []byte) score {
+ h := sha1.New()
+ h.Write(data)
+ var sc score
+ copy(sc[:], h.Sum(nil))
+ return sc
+}
+
+func bytesToKey(hf func() hash.Hash, salt, data []byte, iter int, keySize, ivSize int) (key, iv []byte) {
+ h := hf()
+ var d, dcat []byte
+ sum := make([]byte, 0, h.Size())
+ for len(dcat) < keySize+ivSize {
+ // D_i = HASH^count(D_(i-1) || data || salt)
+ h.Reset()
+ h.Write(d)
+ h.Write(data)
+ h.Write(salt)
+ sum = h.Sum(sum[:0])
+
+ for j := 1; j < iter; j++ {
+ h.Reset()
+ h.Write(sum)
+ sum = h.Sum(sum[:0])
+ }
+
+ d = append(d[:0], sum...)
+ dcat = append(dcat, d...)
+ }
+
+ return dcat[:keySize], dcat[keySize : keySize+ivSize]
+}
diff --git a/vendor/github.com/mattermost/rsc/arq/data.go b/vendor/github.com/mattermost/rsc/arq/data.go
new file mode 100644
index 000000000..74eaba450
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/arq/data.go
@@ -0,0 +1,240 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// On-cloud data structures
+
+package arq
+
+import (
+ "fmt"
+ "os"
+ "time"
+)
+
+// plist data structures
+
+type computerInfo struct {
+ UserName string `plist:"userName"`
+ ComputerName string `plist:"computerName"`
+}
+
+type folderInfo struct {
+ BucketUUID string
+ BucketName string
+ ComputerUUID string
+ LocalPath string
+ LocalMountPoint string
+ // don't care about IgnoredRelativePaths or Excludes
+}
+
+type reflog struct {
+ OldHeadSHA1 string `plist:"oldHeadSHA1"`
+ NewHeadSHA1 string `plist:"newHeadSHA1"`
+}
+
+// binary data structures
+
+type score [20]byte
+
+type sscore struct {
+ Score score `arq:"HexScore"`
+ StretchKey bool // v4+
+}
+
+type tag string
+
+type commit struct {
+ Tag tag `arq:"CommitV005"`
+ Author string
+ Comment string
+ ParentCommits []sscore
+ Tree sscore
+ Location string
+ MergeCommonAncestor sscore
+ CreateTime time.Time
+ Failed []failed // v3+
+ BucketXML []byte // v5+
+}
+
+type tree struct {
+ Tag tag `arq:"TreeV015"`
+ CompressXattr bool
+ CompressACL bool
+ Xattr sscore
+ XattrSize uint64
+ ACL sscore
+ Uid int32
+ Gid int32
+ Mode int32
+ Mtime unixTime
+ Flags int64
+ FinderFlags int32
+ XFinderFlags int32
+ StDev int32
+ StIno int32
+ StNlink uint32
+ StRdev int32
+ Ctime unixTime
+ StBlocks int64
+ StBlksize uint32
+ AggrSize uint64
+ Crtime unixTime
+ Nodes []*nameNode `arq:"count32"`
+}
+
+type nameNode struct {
+ Name string
+ Node node
+}
+
+type node struct {
+ IsTree bool
+ CompressData bool
+ CompressXattr bool
+ CompressACL bool
+ Blob []sscore `arq:"count32"`
+ UncompressedSize uint64
+ Thumbnail sscore
+ Preview sscore
+ Xattr sscore
+ XattrSize uint64
+ ACL sscore
+ Uid int32
+ Gid int32
+ Mode int32
+ Mtime unixTime
+ Flags int64
+ FinderFlags int32
+ XFinderFlags int32
+ FinderFileType string
+ FinderFileCreator string
+ IsExtHidden bool
+ StDev int32
+ StIno int32
+ StNlink uint32
+ StRdev int32
+ Ctime unixTime
+ CreateTime unixTime
+ StBlocks int64
+ StBlksize uint32
+}
+
+func fileMode(m int32) os.FileMode {
+ const (
+ // Darwin file mode.
+ S_IFBLK = 0x6000
+ S_IFCHR = 0x2000
+ S_IFDIR = 0x4000
+ S_IFIFO = 0x1000
+ S_IFLNK = 0xa000
+ S_IFMT = 0xf000
+ S_IFREG = 0x8000
+ S_IFSOCK = 0xc000
+ S_IFWHT = 0xe000
+ S_ISGID = 0x400
+ S_ISTXT = 0x200
+ S_ISUID = 0x800
+ S_ISVTX = 0x200
+ )
+ mode := os.FileMode(m & 0777)
+ switch m & S_IFMT {
+ case S_IFBLK, S_IFWHT:
+ mode |= os.ModeDevice
+ case S_IFCHR:
+ mode |= os.ModeDevice | os.ModeCharDevice
+ case S_IFDIR:
+ mode |= os.ModeDir
+ case S_IFIFO:
+ mode |= os.ModeNamedPipe
+ case S_IFLNK:
+ mode |= os.ModeSymlink
+ case S_IFREG:
+ // nothing to do
+ case S_IFSOCK:
+ mode |= os.ModeSocket
+ }
+ if m&S_ISGID != 0 {
+ mode |= os.ModeSetgid
+ }
+ if m&S_ISUID != 0 {
+ mode |= os.ModeSetuid
+ }
+ if m&S_ISVTX != 0 {
+ mode |= os.ModeSticky
+ }
+ return mode
+}
+
+type unixTime struct {
+ Sec int64
+ Nsec int64
+}
+
+func (t *unixTime) Time() time.Time {
+ return time.Unix(t.Sec, t.Nsec)
+}
+
+type failed struct {
+ Path string
+ Error string
+}
+
+type ientry struct {
+ File string
+ Offset int64
+ Size int64
+ Score score
+}
+
+func (s score) Equal(t score) bool {
+ for i := range s {
+ if s[i] != t[i] {
+ return false
+ }
+ }
+ return true
+}
+
+func (s score) String() string {
+ return fmt.Sprintf("%x", s[:])
+}
+
+func binaryScore(b []byte) score {
+ if len(b) < 20 {
+ panic("BinaryScore: not enough data")
+ }
+ var sc score
+ copy(sc[:], b)
+ return sc
+}
+
+func hexScore(b string) score {
+ if len(b) < 40 {
+ panic("HexScore: not enough data")
+ }
+ var sc score
+ for i := 0; i < 40; i++ {
+ ch := b[i]
+ if '0' <= ch && ch <= '9' {
+ ch -= '0'
+ } else if 'a' <= ch && ch <= 'f' {
+ ch -= 'a' - 10
+ } else {
+ panic("HexScore: invalid lower hex digit")
+ }
+ if i%2 == 0 {
+ ch <<= 4
+ }
+ sc[i/2] |= ch
+ }
+ return sc
+}
+
+func (ss sscore) String() string {
+ str := ss.Score.String()
+ if ss.StretchKey {
+ str += "Y"
+ }
+ return str
+}
diff --git a/vendor/github.com/mattermost/rsc/arq/hist/hist.go b/vendor/github.com/mattermost/rsc/arq/hist/hist.go
new file mode 100644
index 000000000..02fb5fbf0
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/arq/hist/hist.go
@@ -0,0 +1,160 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+/*
+Hist shows the history of a given file, using Arq backups.
+
+ usage: hist [-d] [-h host] [-m mtpt] [-s yyyy/mmdd] file ...
+
+The -d flag causes it to show diffs between successive versions.
+
+By default, hist assumes backups are mounted at mtpt/host, where
+mtpt defaults to /mnt/arq and host is the first element of the local host name.
+Hist starts the file list with the present copy of the file.
+
+The -h and -s flags override these assumptions.
+
+*/
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "regexp"
+ "strings"
+)
+
+var usageString = `usage: hist [-d] [-h host] [-m mtpt] [-s yyyy/mmdd] file ...
+
+Hist lists the known versions of the given file.
+The -d flag causes it to show diffs between successive versions.
+
+By default, hist assumes backups are mounted at mtpt/host, where
+mtpt defaults to /mnt/arq and host is the first element of the local host name.
+Hist starts the file list with the present copy of the file.
+
+The -h and -s flags override these assumptions.
+`
+
+var (
+ diff = flag.Bool("d", false, "diff")
+ host = flag.String("h", defaultHost(), "host name")
+ mtpt = flag.String("m", "/mnt/arq", "mount point")
+ vers = flag.String("s", "", "version")
+)
+
+func defaultHost() string {
+ name, _ := os.Hostname()
+ if name == "" {
+ name = "gnot"
+ }
+ if i := strings.Index(name, "."); i >= 0 {
+ name = name[:i]
+ }
+ return name
+}
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprint(os.Stderr, usageString)
+ os.Exit(2)
+ }
+
+ flag.Parse()
+ args := flag.Args()
+ if len(args) == 0 {
+ flag.Usage()
+ }
+
+ dates := loadDates()
+ for _, file := range args {
+ list(dates, file)
+ }
+}
+
+var (
+ yyyy = regexp.MustCompile(`^\d{4}$`)
+ mmdd = regexp.MustCompile(`^\d{4}(\.\d+)?$`)
+)
+
+func loadDates() []string {
+ var all []string
+ ydir, err := ioutil.ReadDir(filepath.Join(*mtpt, *host))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+ os.Exit(3)
+ }
+ for _, y := range ydir {
+ if !y.IsDir() || !yyyy.MatchString(y.Name()) {
+ continue
+ }
+ ddir, err := ioutil.ReadDir(filepath.Join(*mtpt, *host, y.Name()))
+ if err != nil {
+ continue
+ }
+ for _, d := range ddir {
+ if !d.IsDir() || !mmdd.MatchString(d.Name()) {
+ continue
+ }
+ date := y.Name() + "/" + d.Name()
+ if *vers > date {
+ continue
+ }
+ all = append(all, filepath.Join(*mtpt, *host, date))
+ }
+ }
+ return all
+}
+
+const timeFormat = "Jan 02 15:04:05 MST 2006"
+
+func list(dates []string, file string) {
+ var (
+ last os.FileInfo
+ lastPath string
+ )
+
+ fi, err := os.Stat(file)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "hist: warning: %s: %v\n", file, err)
+ } else {
+ fmt.Printf("%s %s %d\n", fi.ModTime().Format(timeFormat), file, fi.Size())
+ last = fi
+ lastPath = file
+ }
+
+ file, err = filepath.Abs(file)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "hist: abs: %v\n", err)
+ return
+ }
+
+ for i := len(dates)-1; i >= 0; i-- {
+ p := filepath.Join(dates[i], file)
+ fi, err := os.Stat(p)
+ if err != nil {
+ continue
+ }
+ if last != nil && fi.ModTime() == last.ModTime() && fi.Size() == last.Size() {
+ continue
+ }
+ if *diff {
+ cmd := exec.Command("diff", lastPath, p)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ if err := cmd.Start(); err != nil {
+ fmt.Fprintf(os.Stderr, "%s\n", err)
+ }
+ cmd.Wait()
+ }
+ fmt.Printf("%s %s %d\n", fi.ModTime().Format(timeFormat), p, fi.Size())
+ last = fi
+ lastPath = p
+ }
+}
+
diff --git a/vendor/github.com/mattermost/rsc/arq/unpack.go b/vendor/github.com/mattermost/rsc/arq/unpack.go
new file mode 100644
index 000000000..ec4296a7c
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/arq/unpack.go
@@ -0,0 +1,227 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Parsing of Arq's binary data structures.
+
+package arq
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "reflect"
+ "time"
+)
+
+var errMalformed = fmt.Errorf("malformed data")
+var tagType = reflect.TypeOf(tag(""))
+var timeType = reflect.TypeOf(time.Time{})
+var scoreType = reflect.TypeOf(score{})
+
+func unpack(data []byte, v interface{}) error {
+ data, err := unpackValue(data, reflect.ValueOf(v).Elem(), "")
+ if err != nil {
+ return err
+ }
+ if len(data) != 0 {
+ if len(data) > 100 {
+ return fmt.Errorf("more data than expected: %x...", data[:100])
+ }
+ return fmt.Errorf("more data than expected: %x", data)
+ }
+ return nil
+}
+
+func unpackValue(data []byte, v reflect.Value, tag string) ([]byte, error) {
+ //println("unpackvalue", v.Type().String(), len(data))
+ switch v.Kind() {
+ case reflect.String:
+ if v.Type() == tagType {
+ if tag == "" {
+ panic("arqfs: missing reflect tag on Tag field")
+ }
+ if len(data) < len(tag) || !bytes.Equal(data[:len(tag)], []byte(tag)) {
+ return nil, errMalformed
+ }
+ data = data[len(tag):]
+ return data, nil
+ }
+ if len(data) < 1 {
+ return nil, errMalformed
+ }
+ if data[0] == 0 {
+ data = data[1:]
+ v.SetString("")
+ return data, nil
+ }
+ if data[0] != 1 || len(data) < 1+8 {
+ return nil, errMalformed
+ }
+ n := binary.BigEndian.Uint64(data[1:])
+ data = data[1+8:]
+ if n >= uint64(len(data)) {
+ return nil, errMalformed
+ }
+ str := data[:n]
+ data = data[n:]
+ v.SetString(string(str))
+ return data, nil
+
+ case reflect.Uint32:
+ if len(data) < 4 {
+ return nil, errMalformed
+ }
+ v.SetUint(uint64(binary.BigEndian.Uint32(data)))
+ data = data[4:]
+ return data, nil
+
+ case reflect.Int32:
+ if len(data) < 4 {
+ return nil, errMalformed
+ }
+ v.SetInt(int64(binary.BigEndian.Uint32(data)))
+ data = data[4:]
+ return data, nil
+
+ case reflect.Uint8:
+ if len(data) < 1 {
+ return nil, errMalformed
+ }
+ v.SetUint(uint64(data[0]))
+ data = data[1:]
+ return data, nil
+
+ case reflect.Uint64:
+ if len(data) < 8 {
+ return nil, errMalformed
+ }
+ v.SetUint(binary.BigEndian.Uint64(data))
+ data = data[8:]
+ return data, nil
+
+ case reflect.Int64:
+ if len(data) < 8 {
+ return nil, errMalformed
+ }
+ v.SetInt(int64(binary.BigEndian.Uint64(data)))
+ data = data[8:]
+ return data, nil
+
+ case reflect.Ptr:
+ v.Set(reflect.New(v.Type().Elem()))
+ return unpackValue(data, v.Elem(), tag)
+
+ case reflect.Slice:
+ var n int
+ if tag == "count32" {
+ n32 := binary.BigEndian.Uint32(data)
+ n = int(n32)
+ if uint32(n) != n32 {
+ return nil, errMalformed
+ }
+ data = data[4:]
+ } else {
+ if len(data) < 8 {
+ return nil, errMalformed
+ }
+ n64 := binary.BigEndian.Uint64(data)
+ n = int(n64)
+ if uint64(n) != n64 {
+ return nil, errMalformed
+ }
+ data = data[8:]
+ }
+ v.Set(v.Slice(0, 0))
+ if v.Type().Elem().Kind() == reflect.Uint8 {
+ // Fast case for []byte
+ if len(data) < n {
+ return nil, errMalformed
+ }
+ v.Set(reflect.AppendSlice(v, reflect.ValueOf(data[:n])))
+ return data[n:], nil
+ }
+ for i := 0; i < n; i++ {
+ elem := reflect.New(v.Type().Elem()).Elem()
+ var err error
+ data, err = unpackValue(data, elem, "")
+ if err != nil {
+ return nil, err
+ }
+ v.Set(reflect.Append(v, elem))
+ }
+ return data, nil
+
+ case reflect.Array:
+ if v.Type() == scoreType && tag == "HexScore" {
+ var s string
+ data, err := unpackValue(data, reflect.ValueOf(&s).Elem(), "")
+ if err != nil {
+ return nil, err
+ }
+ if len(s) != 0 {
+ v.Set(reflect.ValueOf(hexScore(s)))
+ }
+ return data, nil
+ }
+ n := v.Len()
+ if v.Type().Elem().Kind() == reflect.Uint8 {
+ // Fast case for [n]byte
+ if len(data) < n {
+ return nil, errMalformed
+ }
+ reflect.Copy(v, reflect.ValueOf(data))
+ data = data[n:]
+ return data, nil
+ }
+ for i := 0; i < n; i++ {
+ var err error
+ data, err = unpackValue(data, v.Index(i), "")
+ if err != nil {
+ return nil, err
+ }
+ }
+ return data, nil
+
+ case reflect.Bool:
+ if len(data) < 1 || data[0] > 1 {
+ if len(data) >= 1 {
+ println("badbool", data[0])
+ }
+ return nil, errMalformed
+ }
+ v.SetBool(data[0] == 1)
+ data = data[1:]
+ return data, nil
+
+ case reflect.Struct:
+ if v.Type() == timeType {
+ if len(data) < 1 || data[0] > 1 {
+ return nil, errMalformed
+ }
+ if data[0] == 0 {
+ v.Set(reflect.ValueOf(time.Time{}))
+ return data[1:], nil
+ }
+ data = data[1:]
+ if len(data) < 8 {
+ return nil, errMalformed
+ }
+ ms := binary.BigEndian.Uint64(data)
+ v.Set(reflect.ValueOf(time.Unix(int64(ms/1e3), int64(ms%1e3)*1e6)))
+ return data[8:], nil
+ }
+ for i := 0; i < v.NumField(); i++ {
+ f := v.Type().Field(i)
+ fv := v.Field(i)
+ var err error
+ data, err = unpackValue(data, fv, f.Tag.Get("arq"))
+ if err != nil {
+ return nil, err
+ }
+ }
+ return data, nil
+ }
+
+ panic("arqfs: unexpected type in unpackValue: " + v.Type().String())
+}
diff --git a/vendor/github.com/mattermost/rsc/blog/atom/atom.go b/vendor/github.com/mattermost/rsc/blog/atom/atom.go
new file mode 100644
index 000000000..4038e4fcd
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/blog/atom/atom.go
@@ -0,0 +1,58 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Adapted from encoding/xml/read_test.go.
+
+// Package atom defines XML data structures for an Atom feed.
+package atom
+
+import (
+ "encoding/xml"
+ "time"
+)
+
+type Feed struct {
+ XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
+ Title string `xml:"title"`
+ ID string `xml:"id"`
+ Link []Link `xml:"link"`
+ Updated TimeStr `xml:"updated"`
+ Author *Person `xml:"author"`
+ Entry []*Entry `xml:"entry"`
+}
+
+type Entry struct {
+ Title string `xml:"title"`
+ ID string `xml:"id"`
+ Link []Link `xml:"link"`
+ Published TimeStr `xml:"published"`
+ Updated TimeStr `xml:"updated"`
+ Author *Person `xml:"author"`
+ Summary *Text `xml:"summary"`
+ Content *Text `xml:"content"`
+}
+
+type Link struct {
+ Rel string `xml:"rel,attr"`
+ Href string `xml:"href,attr"`
+}
+
+type Person struct {
+ Name string `xml:"name"`
+ URI string `xml:"uri"`
+ Email string `xml:"email"`
+ InnerXML string `xml:",innerxml"`
+}
+
+type Text struct {
+ Type string `xml:"type,attr"`
+ Body string `xml:",chardata"`
+}
+
+type TimeStr string
+
+func Time(t time.Time) TimeStr {
+ return TimeStr(t.Format("2006-01-02T15:04:05-07:00"))
+}
+
diff --git a/vendor/github.com/mattermost/rsc/blog/main.go b/vendor/github.com/mattermost/rsc/blog/main.go
new file mode 100644
index 000000000..182dc88f1
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/blog/main.go
@@ -0,0 +1,15 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "github.com/mattermost/rsc/devweb/slave"
+
+ _ "github.com/mattermost/rsc/blog/post"
+)
+
+func main() {
+ slave.Main()
+}
diff --git a/vendor/github.com/mattermost/rsc/blog/post/post.go b/vendor/github.com/mattermost/rsc/blog/post/post.go
new file mode 100644
index 000000000..beae651f8
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/blog/post/post.go
@@ -0,0 +1,534 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package post
+
+import (
+ "bytes"
+ "encoding/json"
+ "encoding/xml"
+ "fmt"
+ "html/template"
+ "net/http"
+ "os"
+ "path"
+ "runtime/debug"
+ "sort"
+ "strings"
+ "time"
+
+ "github.com/mattermost/rsc/appfs/fs"
+ "github.com/mattermost/rsc/appfs/proto"
+ "github.com/mattermost/rsc/blog/atom"
+)
+
+func init() {
+ fs.Root = os.Getenv("HOME") + "/app/"
+ http.HandleFunc("/", serve)
+ http.Handle("/feeds/posts/default", http.RedirectHandler("/feed.atom", http.StatusFound))
+}
+
+var funcMap = template.FuncMap{
+ "now": time.Now,
+ "date": timeFormat,
+}
+
+func timeFormat(fmt string, t time.Time) string {
+ return t.Format(fmt)
+}
+
+type blogTime struct {
+ time.Time
+}
+
+var timeFormats = []string{
+ time.RFC3339,
+ "Monday, January 2, 2006",
+ "January 2, 2006 15:00 -0700",
+}
+
+func (t *blogTime) UnmarshalJSON(data []byte) (err error) {
+ str := string(data)
+ for _, f := range timeFormats {
+ tt, err := time.Parse(`"`+f+`"`, str)
+ if err == nil {
+ t.Time = tt
+ return nil
+ }
+ }
+ return fmt.Errorf("did not recognize time: %s", str)
+}
+
+type PostData struct {
+ FileModTime time.Time
+ FileSize int64
+
+ Title string
+ Date blogTime
+ Name string
+ OldURL string
+ Summary string
+ Favorite bool
+
+ Reader []string
+
+ PlusAuthor string // Google+ ID of author
+ PlusPage string // Google+ Post ID for comment post
+ PlusAPIKey string // Google+ API key
+ PlusURL string
+ HostURL string // host URL
+ Comments bool
+
+ article string
+}
+
+func (d *PostData) canRead(user string) bool {
+ for _, r := range d.Reader {
+ if r == user {
+ return true
+ }
+ }
+ return false
+}
+
+func (d *PostData) IsDraft() bool {
+ return d.Date.IsZero() || d.Date.After(time.Now())
+}
+
+// To find PlusPage value:
+// https://www.googleapis.com/plus/v1/people/116810148281701144465/activities/public?key=AIzaSyB_JO6hyAJAL659z0Dmu0RUVVvTx02ZPMM
+//
+
+const owner = "rsc@swtch.com"
+const plusRsc = "116810148281701144465"
+const plusKey = "AIzaSyB_JO6hyAJAL659z0Dmu0RUVVvTx02ZPMM"
+const feedID = "tag:research.swtch.com,2012:research.swtch.com"
+
+var replacer = strings.NewReplacer(
+ "⁰", "<sup>0</sup>",
+ "¹", "<sup>1</sup>",
+ "²", "<sup>2</sup>",
+ "³", "<sup>3</sup>",
+ "⁴", "<sup>4</sup>",
+ "⁵", "<sup>5</sup>",
+ "⁶", "<sup>6</sup>",
+ "⁷", "<sup>7</sup>",
+ "⁸", "<sup>8</sup>",
+ "⁹", "<sup>9</sup>",
+ "ⁿ", "<sup>n</sup>",
+ "₀", "<sub>0</sub>",
+ "₁", "<sub>1</sub>",
+ "₂", "<sub>2</sub>",
+ "₃", "<sub>3</sub>",
+ "₄", "<sub>4</sub>",
+ "₅", "<sub>5</sub>",
+ "₆", "<sub>6</sub>",
+ "₇", "<sub>7</sub>",
+ "₈", "<sub>8</sub>",
+ "₉", "<sub>9</sub>",
+ "``", "&ldquo;",
+ "''", "&rdquo;",
+)
+
+func serve(w http.ResponseWriter, req *http.Request) {
+ ctxt := fs.NewContext(req)
+
+ defer func() {
+ if err := recover(); err != nil {
+ var buf bytes.Buffer
+ fmt.Fprintf(&buf, "panic: %s\n\n", err)
+ buf.Write(debug.Stack())
+ ctxt.Criticalf("%s", buf.String())
+
+ http.Error(w, buf.String(), 500)
+ }
+ }()
+
+ p := path.Clean("/" + req.URL.Path)
+/*
+ if strings.Contains(req.Host, "appspot.com") {
+ http.Redirect(w, req, "http://research.swtch.com" + p, http.StatusFound)
+ }
+*/
+ if p != req.URL.Path {
+ http.Redirect(w, req, p, http.StatusFound)
+ return
+ }
+
+ if p == "/feed.atom" {
+ atomfeed(w, req)
+ return
+ }
+
+ if strings.HasPrefix(p, "/20") && strings.Contains(p[1:], "/") {
+ // Assume this is an old-style URL.
+ oldRedirect(ctxt, w, req, p)
+ }
+
+ user := ctxt.User()
+ isOwner := ctxt.User() == owner || len(os.Args) >= 2 && os.Args[1] == "LISTEN_STDIN"
+ if p == "" || p == "/" || p == "/draft" {
+ if p == "/draft" && user == "?" {
+ ctxt.Criticalf("/draft loaded by %s", user)
+ notfound(ctxt, w, req)
+ return
+ }
+ toc(w, req, p == "/draft", isOwner, user)
+ return
+ }
+
+ draft := false
+ if strings.HasPrefix(p, "/draft/") {
+ if user == "?" {
+ ctxt.Criticalf("/draft loaded by %s", user)
+ notfound(ctxt, w, req)
+ return
+ }
+ draft = true
+ p = p[len("/draft"):]
+ }
+
+ if strings.Contains(p[1:], "/") {
+ notfound(ctxt, w, req)
+ return
+ }
+
+ if strings.Contains(p, ".") {
+ // Let Google's front end servers cache static
+ // content for a short amount of time.
+ httpCache(w, 5*time.Minute)
+ ctxt.ServeFile(w, req, "blog/static/"+p)
+ return
+ }
+
+ // Use just 'blog' as the cache path so that if we change
+ // templates, all the cached HTML gets invalidated.
+ var data []byte
+ pp := "bloghtml:"+p
+ if draft && !isOwner {
+ pp += ",user="+user
+ }
+ if key, ok := ctxt.CacheLoad(pp, "blog", &data); !ok {
+ meta, article, err := loadPost(ctxt, p, req)
+ if err != nil || meta.IsDraft() != draft || (draft && !isOwner && !meta.canRead(user)) {
+ ctxt.Criticalf("no %s for %s", p, user)
+ notfound(ctxt, w, req)
+ return
+ }
+ t := mainTemplate(ctxt)
+ template.Must(t.New("article").Parse(article))
+
+ var buf bytes.Buffer
+ meta.Comments = true
+ if err := t.Execute(&buf, meta); err != nil {
+ panic(err)
+ }
+ data = buf.Bytes()
+ ctxt.CacheStore(key, data)
+ }
+ w.Write(data)
+}
+
+func notfound(ctxt *fs.Context, w http.ResponseWriter, req *http.Request) {
+ var buf bytes.Buffer
+ var data struct {
+ HostURL string
+ }
+ data.HostURL = hostURL(req)
+ t := mainTemplate(ctxt)
+ if err := t.Lookup("404").Execute(&buf, &data); err != nil {
+ panic(err)
+ }
+ w.WriteHeader(404)
+ w.Write(buf.Bytes())
+}
+
+func mainTemplate(c *fs.Context) *template.Template {
+ t := template.New("main")
+ t.Funcs(funcMap)
+
+ main, _, err := c.Read("blog/main.html")
+ if err != nil {
+ panic(err)
+ }
+ style, _, _ := c.Read("blog/style.html")
+ main = append(main, style...)
+ _, err = t.Parse(string(main))
+ if err != nil {
+ panic(err)
+ }
+ return t
+}
+
+func loadPost(c *fs.Context, name string, req *http.Request) (meta *PostData, article string, err error) {
+ meta = &PostData{
+ Name: name,
+ Title: "TITLE HERE",
+ PlusAuthor: plusRsc,
+ PlusAPIKey: plusKey,
+ HostURL: hostURL(req),
+ }
+
+ art, fi, err := c.Read("blog/post/" + name)
+ if err != nil {
+ return nil, "", err
+ }
+ if bytes.HasPrefix(art, []byte("{\n")) {
+ i := bytes.Index(art, []byte("\n}\n"))
+ if i < 0 {
+ panic("cannot find end of json metadata")
+ }
+ hdr, rest := art[:i+3], art[i+3:]
+ if err := json.Unmarshal(hdr, meta); err != nil {
+ panic(fmt.Sprintf("loading %s: %s", name, err))
+ }
+ art = rest
+ }
+ meta.FileModTime = fi.ModTime
+ meta.FileSize = fi.Size
+
+ return meta, replacer.Replace(string(art)), nil
+}
+
+type byTime []*PostData
+
+func (x byTime) Len() int { return len(x) }
+func (x byTime) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
+func (x byTime) Less(i, j int) bool { return x[i].Date.Time.After(x[j].Date.Time) }
+
+type TocData struct {
+ Draft bool
+ HostURL string
+ Posts []*PostData
+}
+
+func toc(w http.ResponseWriter, req *http.Request, draft bool, isOwner bool, user string) {
+ c := fs.NewContext(req)
+
+ var data []byte
+ keystr := fmt.Sprintf("blog:toc:%v", draft)
+ if req.FormValue("readdir") != "" {
+ keystr += ",readdir=" + req.FormValue("readdir")
+ }
+ if draft {
+ keystr += ",user="+user
+ }
+
+ if key, ok := c.CacheLoad(keystr, "blog", &data); !ok {
+ c := fs.NewContext(req)
+ dir, err := c.ReadDir("blog/post")
+ if err != nil {
+ panic(err)
+ }
+
+ if req.FormValue("readdir") == "1" {
+ fmt.Fprintf(w, "%d dir entries\n", len(dir))
+ return
+ }
+
+ postCache := map[string]*PostData{}
+ if data, _, err := c.Read("blogcache"); err == nil {
+ if err := json.Unmarshal(data, &postCache); err != nil {
+ c.Criticalf("unmarshal blogcache: %v", err)
+ }
+ }
+
+ ch := make(chan *PostData, len(dir))
+ const par = 20
+ var limit = make(chan bool, par)
+ for i := 0; i < par; i++ {
+ limit <- true
+ }
+ for _, d := range dir {
+ if meta := postCache[d.Name]; meta != nil && meta.FileModTime.Equal(d.ModTime) && meta.FileSize == d.Size {
+ ch <- meta
+ continue
+ }
+
+ <-limit
+ go func(d proto.FileInfo) {
+ defer func() { limit <- true }()
+ meta, _, err := loadPost(c, d.Name, req)
+ if err != nil {
+ // Should not happen: we just listed the directory.
+ c.Criticalf("loadPost %s: %v", d.Name, err)
+ return
+ }
+ ch <- meta
+ }(d)
+ }
+ for i := 0; i < par; i++ {
+ <-limit
+ }
+ close(ch)
+ postCache = map[string]*PostData{}
+ var all []*PostData
+ for meta := range ch {
+ postCache[meta.Name] = meta
+ if meta.IsDraft() == draft && (!draft || isOwner || meta.canRead(user)) {
+ all = append(all, meta)
+ }
+ }
+ sort.Sort(byTime(all))
+
+ if data, err := json.Marshal(postCache); err != nil {
+ c.Criticalf("marshal blogcache: %v", err)
+ } else if err := c.Write("blogcache", data); err != nil {
+ c.Criticalf("write blogcache: %v", err)
+ }
+
+ var buf bytes.Buffer
+ t := mainTemplate(c)
+ if err := t.Lookup("toc").Execute(&buf, &TocData{draft, hostURL(req), all}); err != nil {
+ panic(err)
+ }
+ data = buf.Bytes()
+ c.CacheStore(key, data)
+ }
+ w.Write(data)
+}
+
+func oldRedirect(ctxt *fs.Context, w http.ResponseWriter, req *http.Request, p string) {
+ m := map[string]string{}
+ if key, ok := ctxt.CacheLoad("blog:oldRedirectMap", "blog/post", &m); !ok {
+ dir, err := ctxt.ReadDir("blog/post")
+ if err != nil {
+ panic(err)
+ }
+
+ for _, d := range dir {
+ meta, _, err := loadPost(ctxt, d.Name, req)
+ if err != nil {
+ // Should not happen: we just listed the directory.
+ panic(err)
+ }
+ m[meta.OldURL] = "/" + d.Name
+ }
+
+ ctxt.CacheStore(key, m)
+ }
+
+ if url, ok := m[p]; ok {
+ http.Redirect(w, req, url, http.StatusFound)
+ return
+ }
+
+ notfound(ctxt, w, req)
+}
+
+func hostURL(req *http.Request) string {
+ if strings.HasPrefix(req.Host, "localhost") {
+ return "http://localhost:8080"
+ }
+ return "http://research.swtch.com"
+}
+
+func atomfeed(w http.ResponseWriter, req *http.Request) {
+ c := fs.NewContext(req)
+
+ c.Criticalf("Header: %v", req.Header)
+
+ var data []byte
+ if key, ok := c.CacheLoad("blog:atomfeed", "blog/post", &data); !ok {
+ dir, err := c.ReadDir("blog/post")
+ if err != nil {
+ panic(err)
+ }
+
+ var all []*PostData
+ for _, d := range dir {
+ meta, article, err := loadPost(c, d.Name, req)
+ if err != nil {
+ // Should not happen: we just loaded the directory.
+ panic(err)
+ }
+ if meta.IsDraft() {
+ continue
+ }
+ meta.article = article
+ all = append(all, meta)
+ }
+ sort.Sort(byTime(all))
+
+ show := all
+ if len(show) > 10 {
+ show = show[:10]
+ for _, meta := range all[10:] {
+ if meta.Favorite {
+ show = append(show, meta)
+ }
+ }
+ }
+
+ feed := &atom.Feed{
+ Title: "research!rsc",
+ ID: feedID,
+ Updated: atom.Time(show[0].Date.Time),
+ Author: &atom.Person{
+ Name: "Russ Cox",
+ URI: "https://plus.google.com/" + plusRsc,
+ Email: "rsc@swtch.com",
+ },
+ Link: []atom.Link{
+ {Rel: "self", Href: hostURL(req) + "/feed.atom"},
+ },
+ }
+
+ for _, meta := range show {
+ t := template.New("main")
+ t.Funcs(funcMap)
+ main, _, err := c.Read("blog/atom.html")
+ if err != nil {
+ panic(err)
+ }
+ _, err = t.Parse(string(main))
+ if err != nil {
+ panic(err)
+ }
+ template.Must(t.New("article").Parse(meta.article))
+ var buf bytes.Buffer
+ if err := t.Execute(&buf, meta); err != nil {
+ panic(err)
+ }
+
+ e := &atom.Entry{
+ Title: meta.Title,
+ ID: feed.ID + "/" + meta.Name,
+ Link: []atom.Link{
+ {Rel: "alternate", Href: meta.HostURL + "/" + meta.Name},
+ },
+ Published: atom.Time(meta.Date.Time),
+ Updated: atom.Time(meta.Date.Time),
+ Summary: &atom.Text{
+ Type: "text",
+ Body: meta.Summary,
+ },
+ Content: &atom.Text{
+ Type: "html",
+ Body: buf.String(),
+ },
+ }
+
+ feed.Entry = append(feed.Entry, e)
+ }
+
+ data, err = xml.Marshal(&feed)
+ if err != nil {
+ panic(err)
+ }
+
+ c.CacheStore(key, data)
+ }
+
+ // Feed readers like to hammer us; let Google cache the
+ // response to reduce the traffic we have to serve.
+ httpCache(w, 15*time.Minute)
+
+ w.Header().Set("Content-Type", "application/atom+xml")
+ w.Write(data)
+}
+
+func httpCache(w http.ResponseWriter, dt time.Duration) {
+ w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int(dt.Seconds())))
+}
diff --git a/vendor/github.com/mattermost/rsc/blog/post/qr.go b/vendor/github.com/mattermost/rsc/blog/post/qr.go
new file mode 100644
index 000000000..a1a03fdbf
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/blog/post/qr.go
@@ -0,0 +1,34 @@
+package post
+
+import (
+ "fmt"
+ "net/http"
+ "runtime/debug"
+
+ qrweb "github.com/mattermost/rsc/qr/web"
+)
+
+func carp(f http.HandlerFunc) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ defer func() {
+ if err := recover(); err != nil {
+ w.Header().Set("Content-Type", "text/plain")
+ fmt.Fprintf(w, "<pre>\npanic: %s\n\n%s\n", err, debug.Stack())
+ }
+ }()
+ f.ServeHTTP(w, req)
+ })
+}
+
+func init() {
+ // http.Handle("/qr/bits", carp(qrweb.Bits))
+ http.Handle("/qr/frame", carp(qrweb.Frame))
+ http.Handle("/qr/frames", carp(qrweb.Frames))
+ http.Handle("/qr/mask", carp(qrweb.Mask))
+ http.Handle("/qr/masks", carp(qrweb.Masks))
+ http.Handle("/qr/arrow", carp(qrweb.Arrow))
+ http.Handle("/qr/draw", carp(qrweb.Draw))
+ http.Handle("/qr/bitstable", carp(qrweb.BitsTable))
+ http.Handle("/qr/encode", carp(qrweb.Encode))
+ http.Handle("/qr/show/", carp(qrweb.Show))
+}
diff --git a/vendor/github.com/mattermost/rsc/cmd/crypt/crypt.go b/vendor/github.com/mattermost/rsc/cmd/crypt/crypt.go
new file mode 100644
index 000000000..4a2150540
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/cmd/crypt/crypt.go
@@ -0,0 +1,79 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Crypt is a simple password-based encryption program,
+// demonstrating how to use github.com/mattermost/rsc/crypt.
+//
+// Encrypt input to output using password:
+// crypt password <input >output
+//
+// Decrypt input to output using password:
+// crypt -d password <input >output
+//
+// Yes, the password is a command-line argument. This is a demo of the
+// github.com/mattermost/rsc/crypt package. It's not intended for real use.
+//
+package main
+
+import (
+ "encoding/base64"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "strings"
+
+ "github.com/mattermost/rsc/crypt"
+)
+
+func main() {
+ args := os.Args[1:]
+ encrypt := true
+ if len(args) >= 1 && args[0] == "-d" {
+ encrypt = false
+ args = args[1:]
+ }
+ if len(args) != 1 || strings.HasPrefix(args[0], "-") {
+ fmt.Fprintf(os.Stderr, "usage: crypt [-d] password < input > output\n")
+ os.Exit(2)
+ }
+ password := args[0]
+
+ data, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "reading stdin: %v\n", err)
+ os.Exit(1)
+ }
+ if encrypt {
+ pkt, err := crypt.Encrypt(password, data)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+ os.Exit(1)
+ }
+ str := base64.StdEncoding.EncodeToString(pkt)
+ for len(str) > 60 {
+ fmt.Printf("%s\n", str[:60])
+ str = str[60:]
+ }
+ fmt.Printf("%s\n", str)
+ } else {
+ pkt, err := base64.StdEncoding.DecodeString(strings.Map(noSpace, string(data)))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "decoding input: %v\n", err)
+ os.Exit(1)
+ }
+ dec, err := crypt.Decrypt(password, pkt)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+ os.Exit(1)
+ }
+ os.Stdout.Write(dec)
+ }
+}
+
+func noSpace(r rune) rune {
+ if r == ' ' || r == '\t' || r == '\n' {
+ return -1
+ }
+ return r
+}
diff --git a/vendor/github.com/mattermost/rsc/cmd/issue/issue.go b/vendor/github.com/mattermost/rsc/cmd/issue/issue.go
new file mode 100644
index 000000000..651a65d96
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/cmd/issue/issue.go
@@ -0,0 +1,185 @@
+package main
+
+import (
+ "encoding/xml"
+ "flag"
+ "fmt"
+ "html"
+ "log"
+ "net/http"
+ "net/url"
+ "os"
+ "sort"
+ "strconv"
+ "strings"
+ "time"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, `usage: issue [-p project] query
+
+If query is a single number, prints the full history for the issue.
+Otherwise, prints a table of matching results.
+The special query 'go1' is shorthand for 'Priority-Go1'.
+`)
+ os.Exit(2)
+}
+
+type Feed struct {
+ Entry Entries `xml:"entry"`
+}
+
+type Entry struct {
+ ID string `xml:"id"`
+ Title string `xml:"title"`
+ Published time.Time `xml:"published"`
+ Content string `xml:"content"`
+ Updates []Update `xml:"updates"`
+ Author struct {
+ Name string `xml:"name"`
+ } `xml:"author"`
+ Owner string `xml:"owner"`
+ Status string `xml:"status"`
+ Label []string `xml:"label"`
+}
+
+type Update struct {
+ Summary string `xml:"summary"`
+ Owner string `xml:"ownerUpdate"`
+ Label string `xml:"label"`
+ Status string `xml:"status"`
+}
+
+type Entries []Entry
+
+func (e Entries) Len() int { return len(e) }
+func (e Entries) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
+func (e Entries) Less(i, j int) bool { return e[i].Title < e[j].Title }
+
+var project = flag.String("p", "go", "code.google.com project identifier")
+var v = flag.Bool("v", false, "verbose")
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ if flag.NArg() != 1 {
+ usage()
+ }
+
+ full := false
+ q := flag.Arg(0)
+ n, _ := strconv.Atoi(q)
+ if n != 0 {
+ q = "id:" + q
+ full = true
+ }
+ if q == "go1" {
+ q = "label:Priority-Go1"
+ }
+
+ log.SetFlags(0)
+
+ query := url.Values{
+ "q": {q},
+ "max-results": {"400"},
+ }
+ if !full {
+ query["can"] = []string{"open"}
+ }
+ u := "https://code.google.com/feeds/issues/p/" + *project + "/issues/full?" + query.Encode()
+ if *v {
+ log.Print(u)
+ }
+ r, err := http.Get(u)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var feed Feed
+ if err := xml.NewDecoder(r.Body).Decode(&feed); err != nil {
+ log.Fatal(err)
+ }
+ r.Body.Close()
+
+ sort.Sort(feed.Entry)
+ for _, e := range feed.Entry {
+ id := e.ID
+ if i := strings.Index(id, "id="); i >= 0 {
+ id = id[:i+len("id=")]
+ }
+ fmt.Printf("%s\t%s\n", id, e.Title)
+ if full {
+ fmt.Printf("Reported by %s (%s)\n", e.Author.Name, e.Published.Format("2006-01-02 15:04:05"))
+ if e.Owner != "" {
+ fmt.Printf("\tOwner: %s\n", e.Owner)
+ }
+ if e.Status != "" {
+ fmt.Printf("\tStatus: %s\n", e.Status)
+ }
+ for _, l := range e.Label {
+ fmt.Printf("\tLabel: %s\n", l)
+ }
+ if e.Content != "" {
+ fmt.Printf("\n\t%s\n", wrap(html.UnescapeString(e.Content), "\t"))
+ }
+ u := "https://code.google.com/feeds/issues/p/" + *project + "/issues/" + id + "/comments/full"
+ if *v {
+ log.Print(u)
+ }
+ r, err := http.Get(u)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var feed Feed
+ if err := xml.NewDecoder(r.Body).Decode(&feed); err != nil {
+ log.Fatal(err)
+ }
+ r.Body.Close()
+
+ for _, e := range feed.Entry {
+ fmt.Printf("\n%s (%s)\n", e.Title, e.Published.Format("2006-01-02 15:04:05"))
+ for _, up := range e.Updates {
+ if up.Summary != "" {
+ fmt.Printf("\tSummary: %s\n", up.Summary)
+ }
+ if up.Owner != "" {
+ fmt.Printf("\tOwner: %s\n", up.Owner)
+ }
+ if up.Status != "" {
+ fmt.Printf("\tStatus: %s\n", up.Status)
+ }
+ if up.Label != "" {
+ fmt.Printf("\tLabel: %s\n", up.Label)
+ }
+ }
+ if e.Content != "" {
+ fmt.Printf("\n\t%s\n", wrap(html.UnescapeString(e.Content), "\t"))
+ }
+ }
+ }
+ }
+}
+
+func wrap(t string, prefix string) string {
+ out := ""
+ t = strings.Replace(t, "\r\n", "\n", -1)
+ lines := strings.Split(t, "\n")
+ for i, line := range lines {
+ if i > 0 {
+ out += "\n" + prefix
+ }
+ s := line
+ for len(s) > 70 {
+ i := strings.LastIndex(s[:70], " ")
+ if i < 0 {
+ i = 69
+ }
+ i++
+ out += s[:i] + "\n" + prefix
+ s = s[i:]
+ }
+ out += s
+ }
+ return out
+}
diff --git a/vendor/github.com/mattermost/rsc/cmd/jfmt/main.go b/vendor/github.com/mattermost/rsc/cmd/jfmt/main.go
new file mode 100644
index 000000000..31c5eddfc
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/cmd/jfmt/main.go
@@ -0,0 +1,37 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// jfmt reads JSON from standard input, formats it, and writes it to standard output.
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+)
+
+func main() {
+ log.SetFlags(0)
+
+ if len(os.Args) > 1 {
+ fmt.Fprintf(os.Stderr, "usage: json < input > output\n")
+ os.Exit(2)
+ }
+
+ // TODO: Can do on the fly.
+
+ data, err := ioutil.ReadAll(os.Stdin)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var buf bytes.Buffer
+ json.Indent(&buf, data, "", " ")
+ buf.WriteByte('\n')
+
+ os.Stdout.Write(buf.Bytes())
+}
diff --git a/vendor/github.com/mattermost/rsc/crypt/crypt.go b/vendor/github.com/mattermost/rsc/crypt/crypt.go
new file mode 100644
index 000000000..c65129be4
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/crypt/crypt.go
@@ -0,0 +1,150 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package crypt provides simple, password-based encryption and decryption of data blobs.
+package crypt
+
+import (
+ "bytes"
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/hmac"
+ "crypto/rand"
+ "crypto/sha1"
+ "fmt"
+ "io"
+
+ "code.google.com/p/go.crypto/pbkdf2"
+)
+
+// This program manipulates encrypted, signed packets with the following format:
+// 1 byte version
+// 8 byte salt
+// 4 byte key hash
+// aes.BlockSize-byte IV
+// aes.BlockSize-byte encryption (maybe longer)
+// sha1.Size-byte HMAC signature
+
+const version = 0
+
+// deriveKey returns the AES key, HMAC-SHA1 key, and key hash for
+// the given password, salt combination.
+func deriveKey(password string, salt []byte) (aesKey, hmacKey, keyHash []byte) {
+ const keySize = 16
+ key := pbkdf2.Key([]byte(password), salt, 4096, 2*keySize, sha1.New)
+ aesKey = key[:keySize]
+ hmacKey = key[keySize:]
+ h := sha1.New()
+ h.Write(key)
+ keyHash = h.Sum(nil)[:4]
+ return
+}
+
+// Encrypt encrypts the plaintext into an encrypted packet
+// using the given password. The password is required for
+// decryption.
+func Encrypt(password string, plaintext []byte) (encrypted []byte, err error) {
+ // Derive key material from password and salt.
+ salt := make([]byte, 8)
+ _, err = io.ReadFull(rand.Reader, salt)
+ if err != nil {
+ return nil, err
+ }
+ aesKey, hmacKey, keyHash := deriveKey(password, salt)
+
+ // Pad.
+ n := aes.BlockSize - len(plaintext)%aes.BlockSize
+ dec := make([]byte, len(plaintext)+n)
+ copy(dec, plaintext)
+ for i := len(plaintext); i < len(dec); i++ {
+ dec[i] = byte(n)
+ }
+
+ // Encrypt.
+ iv := make([]byte, aes.BlockSize)
+ _, err = io.ReadFull(rand.Reader, iv)
+ if err != nil {
+ return nil, err
+ }
+ aesBlock, err := aes.NewCipher(aesKey)
+ if err != nil {
+ // Cannot happen - key is right size.
+ panic("aes: " + err.Error())
+ }
+ m := cipher.NewCBCEncrypter(aesBlock, iv)
+ enc := make([]byte, len(dec))
+ m.CryptBlocks(enc, dec)
+
+ // Construct packet.
+ var pkt []byte
+ pkt = append(pkt, version)
+ pkt = append(pkt, salt...)
+ pkt = append(pkt, keyHash...)
+ pkt = append(pkt, iv...)
+ pkt = append(pkt, enc...)
+
+ // Sign.
+ h := hmac.New(sha1.New, hmacKey)
+ h.Write(pkt)
+ pkt = append(pkt, h.Sum(nil)...)
+
+ return pkt, nil
+}
+
+// Decrypt decrypts the encrypted packet using the given password.
+// It returns the decrypted data.
+func Decrypt(password string, encrypted []byte) (plaintext []byte, err error) {
+ // Pull apart packet.
+ pkt := encrypted
+ if len(pkt) < 1+8+4+2*aes.BlockSize+sha1.Size {
+ return nil, fmt.Errorf("encrypted packet too short")
+ }
+ vers, pkt := pkt[:1], pkt[1:]
+ salt, pkt := pkt[:8], pkt[8:]
+ hash, pkt := pkt[:4], pkt[4:]
+ iv, pkt := pkt[:aes.BlockSize], pkt[aes.BlockSize:]
+ enc, sig := pkt[:len(pkt)-sha1.Size], pkt[len(pkt)-sha1.Size:]
+
+ if vers[0] != version || len(enc)%aes.BlockSize != 0 {
+ return nil, fmt.Errorf("malformed encrypted packet")
+ }
+
+ // Derive key and check against hash.
+ aesKey, hmacKey, keyHash := deriveKey(password, salt)
+ if !bytes.Equal(hash, keyHash) {
+ return nil, fmt.Errorf("incorrect password - %x vs %x", hash, keyHash)
+ }
+
+ // Verify signature.
+ h := hmac.New(sha1.New, hmacKey)
+ h.Write(encrypted[:len(encrypted)-len(sig)])
+ if !bytes.Equal(sig, h.Sum(nil)) {
+ return nil, fmt.Errorf("cannot authenticate encrypted packet")
+ }
+
+ // Decrypt.
+ aesBlock, err := aes.NewCipher(aesKey)
+ if err != nil {
+ // Cannot happen - key is right size.
+ panic("aes: " + err.Error())
+ }
+ m := cipher.NewCBCDecrypter(aesBlock, iv)
+ dec := make([]byte, len(enc))
+ m.CryptBlocks(dec, enc)
+
+ // Unpad.
+ pad := dec[len(dec)-1]
+ if pad <= 0 || pad > aes.BlockSize {
+ return nil, fmt.Errorf("malformed packet padding")
+ }
+ for _, b := range dec[len(dec)-int(pad):] {
+ if b != pad {
+ return nil, fmt.Errorf("malformed packet padding")
+ }
+ }
+ dec = dec[:len(dec)-int(pad)]
+
+ // Success!
+ return dec, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/devweb/main.go b/vendor/github.com/mattermost/rsc/devweb/main.go
new file mode 100644
index 000000000..7a09f032b
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/devweb/main.go
@@ -0,0 +1,317 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Devweb is a simple environment for developing a web server.
+// It runs its own web server on the given address and proxies
+// all requests to the http server program named by importpath.
+// It takes care of recompiling and restarting the program as needed.
+//
+// The server program should be a trivial main program like:
+//
+// package main
+//
+// import (
+// "github.com/mattermost/rsc/devweb/slave"
+//
+// _ "this/package"
+// _ "that/package"
+// )
+//
+// func main() {
+// slave.Main()
+// }
+//
+// The import _ lines import packages that register HTTP handlers,
+// like in an App Engine program.
+//
+// As you make changes to this/package or that/package (or their
+// dependencies), devweb recompiles and relaunches the servers as
+// needed to serve requests.
+//
+package main
+
+// BUG(rsc): Devweb should probably
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/json"
+ "flag"
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "net/http"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "sync"
+ "time"
+)
+
+func usage() {
+ fmt.Fprint(os.Stderr, `usage: devweb [-addr :8000] importpath
+
+Devweb runs a web server on the given address and proxies all requests
+to the http server program named by importpath. It takes care of
+recompiling and restarting the program as needed.
+
+The http server program must itself have a -addr argument that
+says which TCP port to listen on.
+`,
+ )
+}
+
+var addr = flag.String("addr", ":8000", "web service address")
+var rootPackage string
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ args := flag.Args()
+ if len(args) != 1 {
+ usage()
+ }
+ rootPackage = args[0]
+
+ log.Fatal(http.ListenAndServe(*addr, http.HandlerFunc(relay)))
+}
+
+func relay(w http.ResponseWriter, req *http.Request) {
+ defer func() {
+ if err := recover(); err != nil {
+ http.Error(w, fmt.Sprint(err), 200)
+ }
+ }()
+
+ c, proxy, err := buildProxy()
+ if err != nil {
+ panic(err)
+ }
+ defer c.Close()
+ _ = proxy
+
+ outreq := new(http.Request)
+ *outreq = *req // includes shallow copies of maps, but okay
+
+ outreq.Proto = "HTTP/1.1"
+ outreq.ProtoMajor = 1
+ outreq.ProtoMinor = 1
+ outreq.Close = false
+
+ // Remove the connection header to the backend. We want a
+ // persistent connection, regardless of what the client sent
+ // to us. This is modifying the same underlying map from req
+ // (shallow copied above) so we only copy it if necessary.
+ if outreq.Header.Get("Connection") != "" {
+ outreq.Header = make(http.Header)
+ copyHeader(outreq.Header, req.Header)
+ outreq.Header.Del("Connection")
+ }
+
+ outreq.Write(c)
+
+ br := bufio.NewReader(c)
+ resp, err := http.ReadResponse(br, outreq)
+ if err != nil {
+ panic(err)
+ }
+
+ copyHeader(w.Header(), resp.Header)
+ w.WriteHeader(resp.StatusCode)
+
+ if resp.Body != nil {
+ io.Copy(w, resp.Body)
+ }
+}
+
+func copyHeader(dst, src http.Header) {
+ for k, vv := range src {
+ for _, v := range vv {
+ dst.Add(k, v)
+ }
+ }
+}
+
+type cmdProxy struct {
+ cmd *exec.Cmd
+ addr string
+}
+
+func (p *cmdProxy) kill() {
+ if p == nil {
+ return
+ }
+ p.cmd.Process.Kill()
+ p.cmd.Wait()
+}
+
+var proxyInfo struct {
+ sync.Mutex
+ build time.Time
+ check time.Time
+ active *cmdProxy
+ err error
+}
+
+func buildProxy() (c net.Conn, proxy *cmdProxy, err error) {
+ p := &proxyInfo
+
+ t := time.Now()
+ p.Lock()
+ defer p.Unlock()
+ if t.Before(p.check) {
+ // We waited for the lock while someone else dialed.
+ // If we can connect, done.
+ if p.active != nil {
+ if c, err := net.DialTimeout("tcp", p.active.addr, 5*time.Second); err == nil {
+ return c, p.active, nil
+ }
+ }
+ }
+
+ defer func() {
+ p.err = err
+ p.check = time.Now()
+ }()
+
+ pkgs, err := loadPackage(rootPackage)
+ if err != nil {
+ return nil, nil, fmt.Errorf("load %s: %s", rootPackage, err)
+ }
+
+ deps := pkgs[0].Deps
+ if len(deps) > 0 && deps[0] == "C" {
+ deps = deps[1:]
+ }
+ pkgs1, err := loadPackage(deps...)
+ if err != nil {
+ return nil, nil, fmt.Errorf("load %v: %s", deps, err)
+ }
+ pkgs = append(pkgs, pkgs1...)
+
+ var latest time.Time
+
+ for _, pkg := range pkgs {
+ var files []string
+ files = append(files, pkg.GoFiles...)
+ files = append(files, pkg.CFiles...)
+ files = append(files, pkg.HFiles...)
+ files = append(files, pkg.SFiles...)
+ files = append(files, pkg.CgoFiles...)
+
+ for _, file := range files {
+ if fi, err := os.Stat(filepath.Join(pkg.Dir, file)); err == nil && fi.ModTime().After(latest) {
+ latest = fi.ModTime()
+ }
+ }
+ }
+
+ if latest.After(p.build) {
+ p.active.kill()
+ p.active = nil
+
+ out, err := exec.Command("go", "build", "-o", "prox.exe", rootPackage).CombinedOutput()
+ if len(out) > 0 {
+ return nil, nil, fmt.Errorf("%s", out)
+ }
+ if err != nil {
+ return nil, nil, err
+ }
+
+ p.build = latest
+ }
+
+ // If we can connect, done.
+ if p.active != nil {
+ if c, err := net.DialTimeout("tcp", p.active.addr, 5*time.Second); err == nil {
+ return c, p.active, nil
+ }
+ }
+
+ // Otherwise, start a new server.
+ p.active.kill()
+ p.active = nil
+
+ l, err := net.Listen("tcp", "localhost:0")
+ if err != nil {
+ return nil, nil, err
+ }
+ addr := l.Addr().String()
+
+ cmd := exec.Command("prox.exe", "LISTEN_STDIN")
+ cmd.Stdin, err = l.(*net.TCPListener).File()
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ if err != nil {
+ l.Close()
+ return nil, nil, err
+ }
+ err = cmd.Start()
+ l.Close()
+
+ if err != nil {
+ return nil, nil, err
+ }
+
+ c, err = net.DialTimeout("tcp", addr, 5*time.Second)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ p.active = &cmdProxy{cmd, addr}
+ return c, p.active, nil
+}
+
+type Pkg struct {
+ ImportPath string
+ Dir string
+ GoFiles []string
+ CFiles []string
+ HFiles []string
+ SFiles []string
+ CgoFiles []string
+ Deps []string
+}
+
+func loadPackage(name ...string) ([]*Pkg, error) {
+ args := []string{"list", "-json"}
+ args = append(args, name...)
+ var stderr bytes.Buffer
+ cmd := exec.Command("go", args...)
+ r, err := cmd.StdoutPipe()
+ if err != nil {
+ return nil, err
+ }
+ cmd.Stderr = &stderr
+ if err := cmd.Start(); err != nil {
+ return nil, err
+ }
+
+ dec := json.NewDecoder(r)
+ var pkgs []*Pkg
+ for {
+ p := new(Pkg)
+ if err := dec.Decode(p); err != nil {
+ if err == io.EOF {
+ break
+ }
+ cmd.Process.Kill()
+ return nil, err
+ }
+ pkgs = append(pkgs, p)
+ }
+
+ err = cmd.Wait()
+ if b := stderr.Bytes(); len(b) > 0 {
+ return nil, fmt.Errorf("%s", b)
+ }
+ if err != nil {
+ return nil, err
+ }
+ if len(pkgs) != len(name) {
+ return nil, fmt.Errorf("found fewer packages than expected")
+ }
+ return pkgs, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/devweb/slave/slave.go b/vendor/github.com/mattermost/rsc/devweb/slave/slave.go
new file mode 100644
index 000000000..a6983c23c
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/devweb/slave/slave.go
@@ -0,0 +1,26 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package slave
+
+import (
+ "fmt"
+ "log"
+ "net"
+ "net/http"
+ "os"
+)
+
+func Main() {
+ if len(os.Args) != 2 || os.Args[1] != "LISTEN_STDIN" {
+ fmt.Fprintf(os.Stderr, "devweb slave must be invoked by devweb\n")
+ os.Exit(2)
+ }
+ l, err := net.FileListener(os.Stdin)
+ if err != nil {
+ log.Fatal(err)
+ }
+ os.Stdin.Close()
+ log.Fatal(http.Serve(l, nil))
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/debug.go b/vendor/github.com/mattermost/rsc/fuse/debug.go
new file mode 100644
index 000000000..2dc5d4312
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/debug.go
@@ -0,0 +1,20 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// FUSE service loop, for servers that wish to use it.
+
+package fuse
+
+import (
+ "runtime"
+)
+
+func stack() string {
+ buf := make([]byte, 1024)
+ return string(buf[:runtime.Stack(buf, false)])
+}
+
+var Debugf = nop
+
+func nop(string, ...interface{}) {}
diff --git a/vendor/github.com/mattermost/rsc/fuse/fuse.go b/vendor/github.com/mattermost/rsc/fuse/fuse.go
new file mode 100644
index 000000000..2ad10c93e
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/fuse.go
@@ -0,0 +1,1650 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Adapted from Plan 9 from User Space's src/cmd/9pfuse/fuse.c,
+// which carries this notice:
+//
+// The files in this directory are subject to the following license.
+//
+// The author of this software is Russ Cox.
+//
+// Copyright (c) 2006 Russ Cox
+//
+// Permission to use, copy, modify, and distribute this software for any
+// purpose without fee is hereby granted, provided that this entire notice
+// is included in all copies of any software which is or includes a copy
+// or modification of this software and in all copies of the supporting
+// documentation for such software.
+//
+// THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
+// WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR WARRANTY
+// OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS
+// FITNESS FOR ANY PARTICULAR PURPOSE.
+
+// Package fuse enables writing FUSE file systems on FreeBSD, Linux, and OS X.
+//
+// On OS X, it requires OSXFUSE (http://osxfuse.github.com/).
+//
+// There are two approaches to writing a FUSE file system. The first is to speak
+// the low-level message protocol, reading from a Conn using ReadRequest and
+// writing using the various Respond methods. This approach is closest to
+// the actual interaction with the kernel and can be the simplest one in contexts
+// such as protocol translators.
+//
+// Servers of synthesized file systems tend to share common bookkeeping
+// abstracted away by the second approach, which is to call the Conn's
+// Serve method to serve the FUSE protocol using
+// an implementation of the service methods in the interfaces
+// FS (file system), Node (file or directory), and Handle (opened file or directory).
+// There are a daunting number of such methods that can be written,
+// but few are required.
+// The specific methods are described in the documentation for those interfaces.
+//
+// The hellofs subdirectory contains a simple illustration of the ServeFS approach.
+//
+// Service Methods
+//
+// The required and optional methods for the FS, Node, and Handle interfaces
+// have the general form
+//
+// Op(req *OpRequest, resp *OpResponse, intr Intr) Error
+//
+// where Op is the name of a FUSE operation. Op reads request parameters
+// from req and writes results to resp. An operation whose only result is
+// the error result omits the resp parameter. Multiple goroutines may call
+// service methods simultaneously; the methods being called are responsible
+// for appropriate synchronization.
+//
+// Interrupted Operations
+//
+// In some file systems, some operations
+// may take an undetermined amount of time. For example, a Read waiting for
+// a network message or a matching Write might wait indefinitely. If the request
+// is cancelled and no longer needed, the package will close intr, a chan struct{}.
+// Blocking operations should select on a receive from intr and attempt to
+// abort the operation early if the receive succeeds (meaning the channel is closed).
+// To indicate that the operation failed because it was aborted, return fuse.EINTR.
+//
+// If an operation does not block for an indefinite amount of time, the intr parameter
+// can be ignored.
+//
+// Authentication
+//
+// All requests types embed a Header, meaning that the method can inspect
+// req.Pid, req.Uid, and req.Gid as necessary to implement permission checking.
+// Alternately, XXX.
+//
+// Mount Options
+//
+// XXX
+//
+package fuse
+
+// BUG(rsc): The mount code for FreeBSD has not been written yet.
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "runtime"
+ "sync"
+ "syscall"
+ "time"
+ "unsafe"
+)
+
+// A Conn represents a connection to a mounted FUSE file system.
+type Conn struct {
+ fd int
+ buf []byte
+ wio sync.Mutex
+
+ serveConn
+}
+
+// Mount mounts a new FUSE connection on the named directory
+// and returns a connection for reading and writing FUSE messages.
+func Mount(dir string) (*Conn, error) {
+ // TODO(rsc): mount options (...string?)
+ fd, errstr := mount(dir)
+ if errstr != "" {
+ return nil, errors.New(errstr)
+ }
+
+ return &Conn{fd: fd}, nil
+}
+
+// A Request represents a single FUSE request received from the kernel.
+// Use a type switch to determine the specific kind.
+// A request of unrecognized type will have concrete type *Header.
+type Request interface {
+ // Hdr returns the Header associated with this request.
+ Hdr() *Header
+
+ // RespondError responds to the request with the given error.
+ RespondError(Error)
+
+ String() string
+
+ // handle returns the HandleID for the request, or 0.
+ handle() HandleID
+}
+
+// A RequestID identifies an active FUSE request.
+type RequestID uint64
+
+// A NodeID is a number identifying a directory or file.
+// It must be unique among IDs returned in LookupResponses
+// that have not yet been forgotten by ForgetRequests.
+type NodeID uint64
+
+// A HandleID is a number identifying an open directory or file.
+// It only needs to be unique while the directory or file is open.
+type HandleID uint64
+
+// The RootID identifies the root directory of a FUSE file system.
+const RootID NodeID = rootID
+
+// A Header describes the basic information sent in every request.
+type Header struct {
+ Conn *Conn // connection this request was received on
+ ID RequestID // unique ID for request
+ Node NodeID // file or directory the request is about
+ Uid uint32 // user ID of process making request
+ Gid uint32 // group ID of process making request
+ Pid uint32 // process ID of process making request
+}
+
+func (h *Header) String() string {
+ return fmt.Sprintf("ID=%#x Node=%#x Uid=%d Gid=%d Pid=%d", h.ID, h.Node, h.Uid, h.Gid, h.Pid)
+}
+
+func (h *Header) Hdr() *Header {
+ return h
+}
+
+func (h *Header) handle() HandleID {
+ return 0
+}
+
+// An Error is a FUSE error.
+type Error interface {
+ errno() int32
+}
+
+const (
+ // ENOSYS indicates that the call is not supported.
+ ENOSYS = Errno(syscall.ENOSYS)
+
+ // ESTALE is used by Serve to respond to violations of the FUSE protocol.
+ ESTALE = Errno(syscall.ESTALE)
+
+ ENOENT = Errno(syscall.ENOENT)
+ EIO = Errno(syscall.EIO)
+ EPERM = Errno(syscall.EPERM)
+)
+
+type errno int
+
+func (e errno) errno() int32 {
+ return int32(e)
+}
+
+// Errno implements Error using a syscall.Errno.
+type Errno syscall.Errno
+
+func (e Errno) errno() int32 {
+ return int32(e)
+}
+
+func (e Errno) String() string {
+ return syscall.Errno(e).Error()
+}
+
+func (h *Header) RespondError(err Error) {
+ // FUSE uses negative errors!
+ // TODO: File bug report against OSXFUSE: positive error causes kernel panic.
+ out := &outHeader{Error: -err.errno(), Unique: uint64(h.ID)}
+ h.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+var maxWrite = syscall.Getpagesize()
+var bufSize = 4096 + maxWrite
+
+// a message represents the bytes of a single FUSE message
+type message struct {
+ conn *Conn
+ buf []byte // all bytes
+ hdr *inHeader // header
+ off int // offset for reading additional fields
+}
+
+func newMessage(c *Conn) *message {
+ m := &message{conn: c, buf: make([]byte, bufSize)}
+ m.hdr = (*inHeader)(unsafe.Pointer(&m.buf[0]))
+ return m
+}
+
+func (m *message) len() uintptr {
+ return uintptr(len(m.buf) - m.off)
+}
+
+func (m *message) data() unsafe.Pointer {
+ var p unsafe.Pointer
+ if m.off < len(m.buf) {
+ p = unsafe.Pointer(&m.buf[m.off])
+ }
+ return p
+}
+
+func (m *message) bytes() []byte {
+ return m.buf[m.off:]
+}
+
+func (m *message) Header() Header {
+ h := m.hdr
+ return Header{Conn: m.conn, ID: RequestID(h.Unique), Node: NodeID(h.Nodeid), Uid: h.Uid, Gid: h.Gid, Pid: h.Pid}
+}
+
+// fileMode returns a Go os.FileMode from a Unix mode.
+func fileMode(unixMode uint32) os.FileMode {
+ mode := os.FileMode(unixMode & 0777)
+ switch unixMode & syscall.S_IFMT {
+ case syscall.S_IFREG:
+ // nothing
+ case syscall.S_IFDIR:
+ mode |= os.ModeDir
+ case syscall.S_IFCHR:
+ mode |= os.ModeCharDevice | os.ModeDevice
+ case syscall.S_IFBLK:
+ mode |= os.ModeDevice
+ case syscall.S_IFIFO:
+ mode |= os.ModeNamedPipe
+ case syscall.S_IFLNK:
+ mode |= os.ModeSymlink
+ case syscall.S_IFSOCK:
+ mode |= os.ModeSocket
+ default:
+ // no idea
+ mode |= os.ModeDevice
+ }
+ if unixMode&syscall.S_ISUID != 0 {
+ mode |= os.ModeSetuid
+ }
+ if unixMode&syscall.S_ISGID != 0 {
+ mode |= os.ModeSetgid
+ }
+ return mode
+}
+
+func (c *Conn) ReadRequest() (Request, error) {
+ // TODO: Some kind of buffer reuse.
+ m := newMessage(c)
+ n, err := syscall.Read(c.fd, m.buf)
+ if err != nil && err != syscall.ENODEV {
+ return nil, err
+ }
+ if n <= 0 {
+ return nil, io.EOF
+ }
+ m.buf = m.buf[:n]
+
+ if n < inHeaderSize {
+ return nil, errors.New("fuse: message too short")
+ }
+
+ // FreeBSD FUSE sends a short length in the header
+ // for FUSE_INIT even though the actual read length is correct.
+ if n == inHeaderSize+initInSize && m.hdr.Opcode == opInit && m.hdr.Len < uint32(n) {
+ m.hdr.Len = uint32(n)
+ }
+
+ // OSXFUSE sometimes sends the wrong m.hdr.Len in a FUSE_WRITE message.
+ if m.hdr.Len < uint32(n) && m.hdr.Len >= uint32(unsafe.Sizeof(writeIn{})) && m.hdr.Opcode == opWrite {
+ m.hdr.Len = uint32(n)
+ }
+
+ if m.hdr.Len != uint32(n) {
+ return nil, fmt.Errorf("fuse: read %d opcode %d but expected %d", n, m.hdr.Opcode, m.hdr.Len)
+ }
+
+ m.off = inHeaderSize
+
+ // Convert to data structures.
+ // Do not trust kernel to hand us well-formed data.
+ var req Request
+ switch m.hdr.Opcode {
+ default:
+ println("No opcode", m.hdr.Opcode)
+ goto unrecognized
+
+ case opLookup:
+ buf := m.bytes()
+ n := len(buf)
+ if n == 0 || buf[n-1] != '\x00' {
+ goto corrupt
+ }
+ req = &LookupRequest{
+ Header: m.Header(),
+ Name: string(buf[:n-1]),
+ }
+
+ case opForget:
+ in := (*forgetIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &ForgetRequest{
+ Header: m.Header(),
+ N: in.Nlookup,
+ }
+
+ case opGetattr:
+ req = &GetattrRequest{
+ Header: m.Header(),
+ }
+
+ case opSetattr:
+ in := (*setattrIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &SetattrRequest{
+ Header: m.Header(),
+ Valid: SetattrValid(in.Valid),
+ Handle: HandleID(in.Fh),
+ Size: in.Size,
+ Atime: time.Unix(int64(in.Atime), int64(in.AtimeNsec)),
+ Mtime: time.Unix(int64(in.Mtime), int64(in.MtimeNsec)),
+ Mode: fileMode(in.Mode),
+ Uid: in.Uid,
+ Gid: in.Gid,
+ Bkuptime: in.BkupTime(),
+ Chgtime: in.Chgtime(),
+ Flags: in.Flags(),
+ }
+
+ case opReadlink:
+ if len(m.bytes()) > 0 {
+ goto corrupt
+ }
+ req = &ReadlinkRequest{
+ Header: m.Header(),
+ }
+
+ case opSymlink:
+ // m.bytes() is "newName\0target\0"
+ names := m.bytes()
+ if len(names) == 0 || names[len(names)-1] != 0 {
+ goto corrupt
+ }
+ i := bytes.IndexByte(names, '\x00')
+ if i < 0 {
+ goto corrupt
+ }
+ newName, target := names[0:i], names[i+1:len(names)-1]
+ req = &SymlinkRequest{
+ Header: m.Header(),
+ NewName: string(newName),
+ Target: string(target),
+ }
+
+ case opLink:
+ in := (*linkIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ newName := m.bytes()[unsafe.Sizeof(*in):]
+ if len(newName) < 2 || newName[len(newName)-1] != 0 {
+ goto corrupt
+ }
+ newName = newName[:len(newName)-1]
+ req = &LinkRequest{
+ Header: m.Header(),
+ OldNode: NodeID(in.Oldnodeid),
+ NewName: string(newName),
+ }
+
+ case opMknod:
+ in := (*mknodIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ name := m.bytes()[unsafe.Sizeof(*in):]
+ if len(name) < 2 || name[len(name)-1] != '\x00' {
+ goto corrupt
+ }
+ name = name[:len(name)-1]
+ req = &MknodRequest{
+ Header: m.Header(),
+ Mode: fileMode(in.Mode),
+ Rdev: in.Rdev,
+ Name: string(name),
+ }
+
+ case opMkdir:
+ in := (*mkdirIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ name := m.bytes()[unsafe.Sizeof(*in):]
+ i := bytes.IndexByte(name, '\x00')
+ if i < 0 {
+ goto corrupt
+ }
+ req = &MkdirRequest{
+ Header: m.Header(),
+ Name: string(name[:i]),
+ Mode: fileMode(in.Mode) | os.ModeDir,
+ }
+
+ case opUnlink, opRmdir:
+ buf := m.bytes()
+ n := len(buf)
+ if n == 0 || buf[n-1] != '\x00' {
+ goto corrupt
+ }
+ req = &RemoveRequest{
+ Header: m.Header(),
+ Name: string(buf[:n-1]),
+ Dir: m.hdr.Opcode == opRmdir,
+ }
+
+ case opRename:
+ in := (*renameIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ newDirNodeID := NodeID(in.Newdir)
+ oldNew := m.bytes()[unsafe.Sizeof(*in):]
+ // oldNew should be "old\x00new\x00"
+ if len(oldNew) < 4 {
+ goto corrupt
+ }
+ if oldNew[len(oldNew)-1] != '\x00' {
+ goto corrupt
+ }
+ i := bytes.IndexByte(oldNew, '\x00')
+ if i < 0 {
+ goto corrupt
+ }
+ oldName, newName := string(oldNew[:i]), string(oldNew[i+1:len(oldNew)-1])
+ // log.Printf("RENAME: newDirNode = %d; old = %q, new = %q", newDirNodeID, oldName, newName)
+ req = &RenameRequest{
+ Header: m.Header(),
+ NewDir: newDirNodeID,
+ OldName: oldName,
+ NewName: newName,
+ }
+
+ case opOpendir, opOpen:
+ in := (*openIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &OpenRequest{
+ Header: m.Header(),
+ Dir: m.hdr.Opcode == opOpendir,
+ Flags: in.Flags,
+ Mode: fileMode(in.Mode),
+ }
+
+ case opRead, opReaddir:
+ in := (*readIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &ReadRequest{
+ Header: m.Header(),
+ Dir: m.hdr.Opcode == opReaddir,
+ Handle: HandleID(in.Fh),
+ Offset: int64(in.Offset),
+ Size: int(in.Size),
+ }
+
+ case opWrite:
+ in := (*writeIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ r := &WriteRequest{
+ Header: m.Header(),
+ Handle: HandleID(in.Fh),
+ Offset: int64(in.Offset),
+ Flags: WriteFlags(in.WriteFlags),
+ }
+ buf := m.bytes()[unsafe.Sizeof(*in):]
+ if uint32(len(buf)) < in.Size {
+ goto corrupt
+ }
+ r.Data = buf
+ req = r
+
+ case opStatfs:
+ req = &StatfsRequest{
+ Header: m.Header(),
+ }
+
+ case opRelease, opReleasedir:
+ in := (*releaseIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &ReleaseRequest{
+ Header: m.Header(),
+ Dir: m.hdr.Opcode == opReleasedir,
+ Handle: HandleID(in.Fh),
+ Flags: in.Flags,
+ ReleaseFlags: ReleaseFlags(in.ReleaseFlags),
+ LockOwner: in.LockOwner,
+ }
+
+ case opFsync:
+ in := (*fsyncIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &FsyncRequest{
+ Header: m.Header(),
+ Handle: HandleID(in.Fh),
+ Flags: in.FsyncFlags,
+ }
+
+ case opSetxattr:
+ var size uint32
+ var r *SetxattrRequest
+ if runtime.GOOS == "darwin" {
+ in := (*setxattrInOSX)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ r = &SetxattrRequest{
+ Flags: in.Flags,
+ Position: in.Position,
+ }
+ size = in.Size
+ m.off += int(unsafe.Sizeof(*in))
+ } else {
+ in := (*setxattrIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ r = &SetxattrRequest{}
+ size = in.Size
+ m.off += int(unsafe.Sizeof(*in))
+ }
+ r.Header = m.Header()
+ name := m.bytes()
+ i := bytes.IndexByte(name, '\x00')
+ if i < 0 {
+ goto corrupt
+ }
+ r.Name = string(name[:i])
+ r.Xattr = name[i+1:]
+ if uint32(len(r.Xattr)) < size {
+ goto corrupt
+ }
+ r.Xattr = r.Xattr[:size]
+ req = r
+
+ case opGetxattr:
+ if runtime.GOOS == "darwin" {
+ in := (*getxattrInOSX)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &GetxattrRequest{
+ Header: m.Header(),
+ Size: in.Size,
+ Position: in.Position,
+ }
+ } else {
+ in := (*getxattrIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &GetxattrRequest{
+ Header: m.Header(),
+ Size: in.Size,
+ }
+ }
+
+ case opListxattr:
+ if runtime.GOOS == "darwin" {
+ in := (*getxattrInOSX)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &ListxattrRequest{
+ Header: m.Header(),
+ Size: in.Size,
+ Position: in.Position,
+ }
+ } else {
+ in := (*getxattrIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &ListxattrRequest{
+ Header: m.Header(),
+ Size: in.Size,
+ }
+ }
+
+ case opRemovexattr:
+ buf := m.bytes()
+ n := len(buf)
+ if n == 0 || buf[n-1] != '\x00' {
+ goto corrupt
+ }
+ req = &RemovexattrRequest{
+ Header: m.Header(),
+ Name: string(buf[:n-1]),
+ }
+
+ case opFlush:
+ in := (*flushIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &FlushRequest{
+ Header: m.Header(),
+ Handle: HandleID(in.Fh),
+ Flags: in.FlushFlags,
+ LockOwner: in.LockOwner,
+ }
+
+ case opInit:
+ in := (*initIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &InitRequest{
+ Header: m.Header(),
+ Major: in.Major,
+ Minor: in.Minor,
+ MaxReadahead: in.MaxReadahead,
+ Flags: InitFlags(in.Flags),
+ }
+
+ case opFsyncdir:
+ panic("opFsyncdir")
+ case opGetlk:
+ panic("opGetlk")
+ case opSetlk:
+ panic("opSetlk")
+ case opSetlkw:
+ panic("opSetlkw")
+
+ case opAccess:
+ in := (*accessIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ req = &AccessRequest{
+ Header: m.Header(),
+ Mask: in.Mask,
+ }
+
+ case opCreate:
+ in := (*openIn)(m.data())
+ if m.len() < unsafe.Sizeof(*in) {
+ goto corrupt
+ }
+ name := m.bytes()[unsafe.Sizeof(*in):]
+ i := bytes.IndexByte(name, '\x00')
+ if i < 0 {
+ goto corrupt
+ }
+ req = &CreateRequest{
+ Header: m.Header(),
+ Flags: in.Flags,
+ Mode: fileMode(in.Mode),
+ Name: string(name[:i]),
+ }
+
+ case opInterrupt:
+ panic("opInterrupt")
+ case opBmap:
+ panic("opBmap")
+
+ case opDestroy:
+ req = &DestroyRequest{
+ Header: m.Header(),
+ }
+
+ // OS X
+ case opSetvolname:
+ panic("opSetvolname")
+ case opGetxtimes:
+ panic("opGetxtimes")
+ case opExchange:
+ panic("opExchange")
+ }
+
+ return req, nil
+
+corrupt:
+ println("malformed message")
+ return nil, fmt.Errorf("fuse: malformed message")
+
+unrecognized:
+ // Unrecognized message.
+ // Assume higher-level code will send a "no idea what you mean" error.
+ h := m.Header()
+ return &h, nil
+}
+
+func (c *Conn) respond(out *outHeader, n uintptr) {
+ c.wio.Lock()
+ defer c.wio.Unlock()
+ out.Len = uint32(n)
+ msg := (*[1 << 30]byte)(unsafe.Pointer(out))[:n]
+ nn, err := syscall.Write(c.fd, msg)
+ if nn != len(msg) || err != nil {
+ log.Printf("RESPOND WRITE: %d %v", nn, err)
+ log.Printf("with stack: %s", stack())
+ }
+}
+
+func (c *Conn) respondData(out *outHeader, n uintptr, data []byte) {
+ c.wio.Lock()
+ defer c.wio.Unlock()
+ // TODO: use writev
+ out.Len = uint32(n + uintptr(len(data)))
+ msg := make([]byte, out.Len)
+ copy(msg, (*[1 << 30]byte)(unsafe.Pointer(out))[:n])
+ copy(msg[n:], data)
+ syscall.Write(c.fd, msg)
+}
+
+// An InitRequest is the first request sent on a FUSE file system.
+type InitRequest struct {
+ Header
+ Major uint32
+ Minor uint32
+ MaxReadahead uint32
+ Flags InitFlags
+}
+
+func (r *InitRequest) String() string {
+ return fmt.Sprintf("Init [%s] %d.%d ra=%d fl=%v", &r.Header, r.Major, r.Minor, r.MaxReadahead, r.Flags)
+}
+
+// An InitResponse is the response to an InitRequest.
+type InitResponse struct {
+ MaxReadahead uint32
+ Flags InitFlags
+ MaxWrite uint32
+}
+
+func (r *InitResponse) String() string {
+ return fmt.Sprintf("Init %+v", *r)
+}
+
+// Respond replies to the request with the given response.
+func (r *InitRequest) Respond(resp *InitResponse) {
+ out := &initOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Major: kernelVersion,
+ Minor: kernelMinorVersion,
+ MaxReadahead: resp.MaxReadahead,
+ Flags: uint32(resp.Flags),
+ MaxWrite: resp.MaxWrite,
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A StatfsRequest requests information about the mounted file system.
+type StatfsRequest struct {
+ Header
+}
+
+func (r *StatfsRequest) String() string {
+ return fmt.Sprintf("Statfs [%s]\n", &r.Header)
+}
+
+// Respond replies to the request with the given response.
+func (r *StatfsRequest) Respond(resp *StatfsResponse) {
+ out := &statfsOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ St: kstatfs{
+ Blocks: resp.Blocks,
+ Bfree: resp.Bfree,
+ Bavail: resp.Bavail,
+ Files: resp.Files,
+ Bsize: resp.Bsize,
+ Namelen: resp.Namelen,
+ Frsize: resp.Frsize,
+ },
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A StatfsResponse is the response to a StatfsRequest.
+type StatfsResponse struct {
+ Blocks uint64 // Total data blocks in file system.
+ Bfree uint64 // Free blocks in file system.
+ Bavail uint64 // Free blocks in file system if you're not root.
+ Files uint64 // Total files in file system.
+ Ffree uint64 // Free files in file system.
+ Bsize uint32 // Block size
+ Namelen uint32 // Maximum file name length?
+ Frsize uint32 // ?
+}
+
+func (r *StatfsResponse) String() string {
+ return fmt.Sprintf("Statfs %+v", *r)
+}
+
+// An AccessRequest asks whether the file can be accessed
+// for the purpose specified by the mask.
+type AccessRequest struct {
+ Header
+ Mask uint32
+}
+
+func (r *AccessRequest) String() string {
+ return fmt.Sprintf("Access [%s] mask=%#x", &r.Header, r.Mask)
+}
+
+// Respond replies to the request indicating that access is allowed.
+// To deny access, use RespondError.
+func (r *AccessRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// An Attr is the metadata for a single file or directory.
+type Attr struct {
+ Inode uint64 // inode number
+ Size uint64 // size in bytes
+ Blocks uint64 // size in blocks
+ Atime time.Time // time of last access
+ Mtime time.Time // time of last modification
+ Ctime time.Time // time of last inode change
+ Crtime time.Time // time of creation (OS X only)
+ Mode os.FileMode // file mode
+ Nlink uint32 // number of links
+ Uid uint32 // owner uid
+ Gid uint32 // group gid
+ Rdev uint32 // device numbers
+ Flags uint32 // chflags(2) flags (OS X only)
+}
+
+func unix(t time.Time) (sec uint64, nsec uint32) {
+ nano := t.UnixNano()
+ sec = uint64(nano / 1e9)
+ nsec = uint32(nano % 1e9)
+ return
+}
+
+func (a *Attr) attr() (out attr) {
+ out.Ino = a.Inode
+ out.Size = a.Size
+ out.Blocks = a.Blocks
+ out.Atime, out.AtimeNsec = unix(a.Atime)
+ out.Mtime, out.MtimeNsec = unix(a.Mtime)
+ out.Ctime, out.CtimeNsec = unix(a.Ctime)
+ out.SetCrtime(unix(a.Crtime))
+ out.Mode = uint32(a.Mode) & 0777
+ switch {
+ default:
+ out.Mode |= syscall.S_IFREG
+ case a.Mode&os.ModeDir != 0:
+ out.Mode |= syscall.S_IFDIR
+ case a.Mode&os.ModeDevice != 0:
+ if a.Mode&os.ModeCharDevice != 0 {
+ out.Mode |= syscall.S_IFCHR
+ } else {
+ out.Mode |= syscall.S_IFBLK
+ }
+ case a.Mode&os.ModeNamedPipe != 0:
+ out.Mode |= syscall.S_IFIFO
+ case a.Mode&os.ModeSymlink != 0:
+ out.Mode |= syscall.S_IFLNK
+ case a.Mode&os.ModeSocket != 0:
+ out.Mode |= syscall.S_IFSOCK
+ }
+ if a.Mode&os.ModeSetuid != 0 {
+ out.Mode |= syscall.S_ISUID
+ }
+ if a.Mode&os.ModeSetgid != 0 {
+ out.Mode |= syscall.S_ISGID
+ }
+ out.Nlink = a.Nlink
+ if out.Nlink < 1 {
+ out.Nlink = 1
+ }
+ out.Uid = a.Uid
+ out.Gid = a.Gid
+ out.Rdev = a.Rdev
+ out.SetFlags(a.Flags)
+
+ return
+}
+
+// A GetattrRequest asks for the metadata for the file denoted by r.Node.
+type GetattrRequest struct {
+ Header
+}
+
+func (r *GetattrRequest) String() string {
+ return fmt.Sprintf("Getattr [%s]", &r.Header)
+}
+
+// Respond replies to the request with the given response.
+func (r *GetattrRequest) Respond(resp *GetattrResponse) {
+ out := &attrOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A GetattrResponse is the response to a GetattrRequest.
+type GetattrResponse struct {
+ AttrValid time.Duration // how long Attr can be cached
+ Attr Attr // file attributes
+}
+
+func (r *GetattrResponse) String() string {
+ return fmt.Sprintf("Getattr %+v", *r)
+}
+
+// A GetxattrRequest asks for the extended attributes associated with r.Node.
+type GetxattrRequest struct {
+ Header
+ Size uint32 // maximum size to return
+ Position uint32 // offset within extended attributes
+}
+
+func (r *GetxattrRequest) String() string {
+ return fmt.Sprintf("Getxattr [%s] %d @%d", &r.Header, r.Size, r.Position)
+}
+
+// Respond replies to the request with the given response.
+func (r *GetxattrRequest) Respond(resp *GetxattrResponse) {
+ out := &getxattrOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Size: uint32(len(resp.Xattr)),
+ }
+ r.Conn.respondData(&out.outHeader, unsafe.Sizeof(*out), resp.Xattr)
+}
+
+// A GetxattrResponse is the response to a GetxattrRequest.
+type GetxattrResponse struct {
+ Xattr []byte
+}
+
+func (r *GetxattrResponse) String() string {
+ return fmt.Sprintf("Getxattr %x", r.Xattr)
+}
+
+// A ListxattrRequest asks to list the extended attributes associated with r.Node.
+type ListxattrRequest struct {
+ Header
+ Size uint32 // maximum size to return
+ Position uint32 // offset within attribute list
+}
+
+func (r *ListxattrRequest) String() string {
+ return fmt.Sprintf("Listxattr [%s] %d @%d", &r.Header, r.Size, r.Position)
+}
+
+// Respond replies to the request with the given response.
+func (r *ListxattrRequest) Respond(resp *ListxattrResponse) {
+ out := &getxattrOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Size: uint32(len(resp.Xattr)),
+ }
+ r.Conn.respondData(&out.outHeader, unsafe.Sizeof(*out), resp.Xattr)
+}
+
+// A ListxattrResponse is the response to a ListxattrRequest.
+type ListxattrResponse struct {
+ Xattr []byte
+}
+
+func (r *ListxattrResponse) String() string {
+ return fmt.Sprintf("Listxattr %x", r.Xattr)
+}
+
+// A RemovexattrRequest asks to remove an extended attribute associated with r.Node.
+type RemovexattrRequest struct {
+ Header
+ Name string // name of extended attribute
+}
+
+func (r *RemovexattrRequest) String() string {
+ return fmt.Sprintf("Removexattr [%s] %q", &r.Header, r.Name)
+}
+
+// Respond replies to the request, indicating that the attribute was removed.
+func (r *RemovexattrRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// A SetxattrRequest asks to set an extended attribute associated with a file.
+type SetxattrRequest struct {
+ Header
+ Flags uint32
+ Position uint32 // OS X only
+ Name string
+ Xattr []byte
+}
+
+func (r *SetxattrRequest) String() string {
+ return fmt.Sprintf("Setxattr [%s] %q %x fl=%v @%#x", &r.Header, r.Name, r.Xattr, r.Flags, r.Position)
+}
+
+// Respond replies to the request, indicating that the extended attribute was set.
+func (r *SetxattrRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// A LookupRequest asks to look up the given name in the directory named by r.Node.
+type LookupRequest struct {
+ Header
+ Name string
+}
+
+func (r *LookupRequest) String() string {
+ return fmt.Sprintf("Lookup [%s] %q", &r.Header, r.Name)
+}
+
+// Respond replies to the request with the given response.
+func (r *LookupRequest) Respond(resp *LookupResponse) {
+ out := &entryOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Nodeid: uint64(resp.Node),
+ Generation: resp.Generation,
+ EntryValid: uint64(resp.EntryValid / time.Second),
+ EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A LookupResponse is the response to a LookupRequest.
+type LookupResponse struct {
+ Node NodeID
+ Generation uint64
+ EntryValid time.Duration
+ AttrValid time.Duration
+ Attr Attr
+}
+
+func (r *LookupResponse) String() string {
+ return fmt.Sprintf("Lookup %+v", *r)
+}
+
+// An OpenRequest asks to open a file or directory
+type OpenRequest struct {
+ Header
+ Dir bool // is this Opendir?
+ Flags uint32
+ Mode os.FileMode
+}
+
+func (r *OpenRequest) String() string {
+ return fmt.Sprintf("Open [%s] dir=%v fl=%v mode=%v", &r.Header, r.Dir, r.Flags, r.Mode)
+}
+
+// Respond replies to the request with the given response.
+func (r *OpenRequest) Respond(resp *OpenResponse) {
+ out := &openOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Fh: uint64(resp.Handle),
+ OpenFlags: uint32(resp.Flags),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A OpenResponse is the response to a OpenRequest.
+type OpenResponse struct {
+ Handle HandleID
+ Flags OpenFlags
+}
+
+func (r *OpenResponse) String() string {
+ return fmt.Sprintf("Open %+v", *r)
+}
+
+// A CreateRequest asks to create and open a file (not a directory).
+type CreateRequest struct {
+ Header
+ Name string
+ Flags uint32
+ Mode os.FileMode
+}
+
+func (r *CreateRequest) String() string {
+ return fmt.Sprintf("Create [%s] %q fl=%v mode=%v", &r.Header, r.Name, r.Flags, r.Mode)
+}
+
+// Respond replies to the request with the given response.
+func (r *CreateRequest) Respond(resp *CreateResponse) {
+ out := &createOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+
+ Nodeid: uint64(resp.Node),
+ Generation: resp.Generation,
+ EntryValid: uint64(resp.EntryValid / time.Second),
+ EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+
+ Fh: uint64(resp.Handle),
+ OpenFlags: uint32(resp.Flags),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A CreateResponse is the response to a CreateRequest.
+// It describes the created node and opened handle.
+type CreateResponse struct {
+ LookupResponse
+ OpenResponse
+}
+
+func (r *CreateResponse) String() string {
+ return fmt.Sprintf("Create %+v", *r)
+}
+
+// A MkdirRequest asks to create (but not open) a directory.
+type MkdirRequest struct {
+ Header
+ Name string
+ Mode os.FileMode
+}
+
+func (r *MkdirRequest) String() string {
+ return fmt.Sprintf("Mkdir [%s] %q mode=%v", &r.Header, r.Name, r.Mode)
+}
+
+// Respond replies to the request with the given response.
+func (r *MkdirRequest) Respond(resp *MkdirResponse) {
+ out := &entryOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Nodeid: uint64(resp.Node),
+ Generation: resp.Generation,
+ EntryValid: uint64(resp.EntryValid / time.Second),
+ EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A MkdirResponse is the response to a MkdirRequest.
+type MkdirResponse struct {
+ LookupResponse
+}
+
+func (r *MkdirResponse) String() string {
+ return fmt.Sprintf("Mkdir %+v", *r)
+}
+
+// A ReadRequest asks to read from an open file.
+type ReadRequest struct {
+ Header
+ Dir bool // is this Readdir?
+ Handle HandleID
+ Offset int64
+ Size int
+}
+
+func (r *ReadRequest) handle() HandleID {
+ return r.Handle
+}
+
+func (r *ReadRequest) String() string {
+ return fmt.Sprintf("Read [%s] %#x %d @%#x dir=%v", &r.Header, r.Handle, r.Size, r.Offset, r.Dir)
+}
+
+// Respond replies to the request with the given response.
+func (r *ReadRequest) Respond(resp *ReadResponse) {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respondData(out, unsafe.Sizeof(*out), resp.Data)
+}
+
+// A ReadResponse is the response to a ReadRequest.
+type ReadResponse struct {
+ Data []byte
+}
+
+func (r *ReadResponse) String() string {
+ return fmt.Sprintf("Read %x", r.Data)
+}
+
+// A ReleaseRequest asks to release (close) an open file handle.
+type ReleaseRequest struct {
+ Header
+ Dir bool // is this Releasedir?
+ Handle HandleID
+ Flags uint32 // flags from OpenRequest
+ ReleaseFlags ReleaseFlags
+ LockOwner uint32
+}
+
+func (r *ReleaseRequest) handle() HandleID {
+ return r.Handle
+}
+
+func (r *ReleaseRequest) String() string {
+ return fmt.Sprintf("Release [%s] %#x fl=%v rfl=%v owner=%#x", &r.Header, r.Handle, r.Flags, r.ReleaseFlags, r.LockOwner)
+}
+
+// Respond replies to the request, indicating that the handle has been released.
+func (r *ReleaseRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// A DestroyRequest is sent by the kernel when unmounting the file system.
+// No more requests will be received after this one, but it should still be
+// responded to.
+type DestroyRequest struct {
+ Header
+}
+
+func (r *DestroyRequest) String() string {
+ return fmt.Sprintf("Destroy [%s]", &r.Header)
+}
+
+// Respond replies to the request.
+func (r *DestroyRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// A ForgetRequest is sent by the kernel when forgetting about r.Node
+// as returned by r.N lookup requests.
+type ForgetRequest struct {
+ Header
+ N uint64
+}
+
+func (r *ForgetRequest) String() string {
+ return fmt.Sprintf("Forget [%s] %d", &r.Header, r.N)
+}
+
+// Respond replies to the request, indicating that the forgetfulness has been recorded.
+func (r *ForgetRequest) Respond() {
+ // Don't reply to forget messages.
+}
+
+// A Dirent represents a single directory entry.
+type Dirent struct {
+ Inode uint64 // inode this entry names
+ Type uint32 // ?
+ Name string // name of entry
+}
+
+// AppendDirent appends the encoded form of a directory entry to data
+// and returns the resulting slice.
+func AppendDirent(data []byte, dir Dirent) []byte {
+ de := dirent{
+ Ino: dir.Inode,
+ Namelen: uint32(len(dir.Name)),
+ Type: dir.Type,
+ }
+ de.Off = uint64(len(data) + direntSize + (len(dir.Name)+7)&^7)
+ data = append(data, (*[direntSize]byte)(unsafe.Pointer(&de))[:]...)
+ data = append(data, dir.Name...)
+ n := direntSize + uintptr(len(dir.Name))
+ if n%8 != 0 {
+ var pad [8]byte
+ data = append(data, pad[:8-n%8]...)
+ }
+ return data
+}
+
+// A WriteRequest asks to write to an open file.
+type WriteRequest struct {
+ Header
+ Handle HandleID
+ Offset int64
+ Data []byte
+ Flags WriteFlags
+}
+
+func (r *WriteRequest) String() string {
+ return fmt.Sprintf("Write [%s] %#x %d @%d fl=%v", &r.Header, r.Handle, len(r.Data), r.Offset, r.Flags)
+}
+
+// Respond replies to the request with the given response.
+func (r *WriteRequest) Respond(resp *WriteResponse) {
+ out := &writeOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Size: uint32(resp.Size),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+func (r *WriteRequest) handle() HandleID {
+ return r.Handle
+}
+
+// A WriteResponse replies to a write indicating how many bytes were written.
+type WriteResponse struct {
+ Size int
+}
+
+func (r *WriteResponse) String() string {
+ return fmt.Sprintf("Write %+v", *r)
+}
+
+// A SetattrRequest asks to change one or more attributes associated with a file,
+// as indicated by Valid.
+type SetattrRequest struct {
+ Header
+ Valid SetattrValid
+ Handle HandleID
+ Size uint64
+ Atime time.Time
+ Mtime time.Time
+ Mode os.FileMode
+ Uid uint32
+ Gid uint32
+
+ // OS X only
+ Bkuptime time.Time
+ Chgtime time.Time
+ Crtime time.Time
+ Flags uint32 // see chflags(2)
+}
+
+func (r *SetattrRequest) String() string {
+ var buf bytes.Buffer
+ fmt.Fprintf(&buf, "Setattr [%s]", &r.Header)
+ if r.Valid.Mode() {
+ fmt.Fprintf(&buf, " mode=%v", r.Mode)
+ }
+ if r.Valid.Uid() {
+ fmt.Fprintf(&buf, " uid=%d", r.Uid)
+ }
+ if r.Valid.Gid() {
+ fmt.Fprintf(&buf, " gid=%d", r.Gid)
+ }
+ if r.Valid.Size() {
+ fmt.Fprintf(&buf, " size=%d", r.Size)
+ }
+ if r.Valid.Atime() {
+ fmt.Fprintf(&buf, " atime=%v", r.Atime)
+ }
+ if r.Valid.Mtime() {
+ fmt.Fprintf(&buf, " mtime=%v", r.Mtime)
+ }
+ if r.Valid.Handle() {
+ fmt.Fprintf(&buf, " handle=%#x", r.Handle)
+ } else {
+ fmt.Fprintf(&buf, " handle=INVALID-%#x", r.Handle)
+ }
+ if r.Valid.Crtime() {
+ fmt.Fprintf(&buf, " crtime=%v", r.Crtime)
+ }
+ if r.Valid.Chgtime() {
+ fmt.Fprintf(&buf, " chgtime=%v", r.Chgtime)
+ }
+ if r.Valid.Bkuptime() {
+ fmt.Fprintf(&buf, " bkuptime=%v", r.Bkuptime)
+ }
+ if r.Valid.Flags() {
+ fmt.Fprintf(&buf, " flags=%#x", r.Flags)
+ }
+ return buf.String()
+}
+
+func (r *SetattrRequest) handle() HandleID {
+ if r.Valid.Handle() {
+ return r.Handle
+ }
+ return 0
+}
+
+// Respond replies to the request with the given response,
+// giving the updated attributes.
+func (r *SetattrRequest) Respond(resp *SetattrResponse) {
+ out := &attrOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A SetattrResponse is the response to a SetattrRequest.
+type SetattrResponse struct {
+ AttrValid time.Duration // how long Attr can be cached
+ Attr Attr // file attributes
+}
+
+func (r *SetattrResponse) String() string {
+ return fmt.Sprintf("Setattr %+v", *r)
+}
+
+// A FlushRequest asks for the current state of an open file to be flushed
+// to storage, as when a file descriptor is being closed. A single opened Handle
+// may receive multiple FlushRequests over its lifetime.
+type FlushRequest struct {
+ Header
+ Handle HandleID
+ Flags uint32
+ LockOwner uint64
+}
+
+func (r *FlushRequest) String() string {
+ return fmt.Sprintf("Flush [%s] %#x fl=%#x lk=%#x", &r.Header, r.Handle, r.Flags, r.LockOwner)
+}
+
+func (r *FlushRequest) handle() HandleID {
+ return r.Handle
+}
+
+// Respond replies to the request, indicating that the flush succeeded.
+func (r *FlushRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// A RemoveRequest asks to remove a file or directory.
+type RemoveRequest struct {
+ Header
+ Name string // name of extended attribute
+ Dir bool // is this rmdir?
+}
+
+func (r *RemoveRequest) String() string {
+ return fmt.Sprintf("Remove [%s] %q dir=%v", &r.Header, r.Name, r.Dir)
+}
+
+// Respond replies to the request, indicating that the file was removed.
+func (r *RemoveRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+// A SymlinkRequest is a request to create a symlink making NewName point to Target.
+type SymlinkRequest struct {
+ Header
+ NewName, Target string
+}
+
+func (r *SymlinkRequest) String() string {
+ return fmt.Sprintf("Symlink [%s] from %q to target %q", &r.Header, r.NewName, r.Target)
+}
+
+func (r *SymlinkRequest) handle() HandleID {
+ return 0
+}
+
+// Respond replies to the request, indicating that the symlink was created.
+func (r *SymlinkRequest) Respond(resp *SymlinkResponse) {
+ out := &entryOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Nodeid: uint64(resp.Node),
+ Generation: resp.Generation,
+ EntryValid: uint64(resp.EntryValid / time.Second),
+ EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A SymlinkResponse is the response to a SymlinkRequest.
+type SymlinkResponse struct {
+ LookupResponse
+}
+
+// A ReadlinkRequest is a request to read a symlink's target.
+type ReadlinkRequest struct {
+ Header
+}
+
+func (r *ReadlinkRequest) String() string {
+ return fmt.Sprintf("Readlink [%s]", &r.Header)
+}
+
+func (r *ReadlinkRequest) handle() HandleID {
+ return 0
+}
+
+func (r *ReadlinkRequest) Respond(target string) {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respondData(out, unsafe.Sizeof(*out), []byte(target))
+}
+
+// A LinkRequest is a request to create a hard link.
+type LinkRequest struct {
+ Header
+ OldNode NodeID
+ NewName string
+}
+
+func (r *LinkRequest) Respond(resp *LookupResponse) {
+ out := &entryOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Nodeid: uint64(resp.Node),
+ Generation: resp.Generation,
+ EntryValid: uint64(resp.EntryValid / time.Second),
+ EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A RenameRequest is a request to rename a file.
+type RenameRequest struct {
+ Header
+ NewDir NodeID
+ OldName, NewName string
+}
+
+func (r *RenameRequest) handle() HandleID {
+ return 0
+}
+
+func (r *RenameRequest) String() string {
+ return fmt.Sprintf("Rename [%s] from %q to dirnode %d %q", &r.Header, r.OldName, r.NewDir, r.NewName)
+}
+
+func (r *RenameRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+type MknodRequest struct {
+ Header
+ Name string
+ Mode os.FileMode
+ Rdev uint32
+}
+
+func (r *MknodRequest) String() string {
+ return fmt.Sprintf("Mknod [%s] Name %q mode %v rdev %d", &r.Header, r.Name, r.Mode, r.Rdev)
+}
+
+func (r *MknodRequest) Respond(resp *LookupResponse) {
+ out := &entryOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ Nodeid: uint64(resp.Node),
+ Generation: resp.Generation,
+ EntryValid: uint64(resp.EntryValid / time.Second),
+ EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
+ AttrValid: uint64(resp.AttrValid / time.Second),
+ AttrValidNsec: uint32(resp.AttrValid % time.Second / time.Nanosecond),
+ Attr: resp.Attr.attr(),
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+type FsyncRequest struct {
+ Header
+ Handle HandleID
+ Flags uint32
+}
+
+func (r *FsyncRequest) String() string {
+ return fmt.Sprintf("Fsync [%s] Handle %v Flags %v", &r.Header, r.Handle, r.Flags)
+}
+
+func (r *FsyncRequest) Respond() {
+ out := &outHeader{Unique: uint64(r.ID)}
+ r.Conn.respond(out, unsafe.Sizeof(*out))
+}
+
+/*{
+
+// A XXXRequest xxx.
+type XXXRequest struct {
+ Header
+ xxx
+}
+
+func (r *XXXRequest) String() string {
+ return fmt.Sprintf("XXX [%s] xxx", &r.Header)
+}
+
+func (r *XXXRequest) handle() HandleID {
+ return r.Handle
+}
+
+// Respond replies to the request with the given response.
+func (r *XXXRequest) Respond(resp *XXXResponse) {
+ out := &xxxOut{
+ outHeader: outHeader{Unique: uint64(r.ID)},
+ xxx,
+ }
+ r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
+}
+
+// A XXXResponse is the response to a XXXRequest.
+type XXXResponse struct {
+ xxx
+}
+
+func (r *XXXResponse) String() string {
+ return fmt.Sprintf("XXX %+v", *r)
+}
+
+ }
+*/
diff --git a/vendor/github.com/mattermost/rsc/fuse/fuse_kernel.go b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel.go
new file mode 100644
index 000000000..a57360e63
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel.go
@@ -0,0 +1,539 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Derived from FUSE's fuse_kernel.h
+/*
+ This file defines the kernel interface of FUSE
+ Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
+
+
+ This -- and only this -- header file may also be distributed under
+ the terms of the BSD Licence as follows:
+
+ Copyright (C) 2001-2007 Miklos Szeredi. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+*/
+
+package fuse
+
+import (
+ "fmt"
+ "unsafe"
+)
+
+// Version is the FUSE version implemented by the package.
+const Version = "7.8"
+
+const (
+ kernelVersion = 7
+ kernelMinorVersion = 8
+ rootID = 1
+)
+
+type kstatfs struct {
+ Blocks uint64
+ Bfree uint64
+ Bavail uint64
+ Files uint64
+ Ffree uint64
+ Bsize uint32
+ Namelen uint32
+ Frsize uint32
+ Padding uint32
+ Spare [6]uint32
+}
+
+type fileLock struct {
+ Start uint64
+ End uint64
+ Type uint32
+ Pid uint32
+}
+
+// The SetattrValid are bit flags describing which fields in the SetattrRequest
+// are included in the change.
+type SetattrValid uint32
+
+const (
+ SetattrMode SetattrValid = 1 << 0
+ SetattrUid SetattrValid = 1 << 1
+ SetattrGid SetattrValid = 1 << 2
+ SetattrSize SetattrValid = 1 << 3
+ SetattrAtime SetattrValid = 1 << 4
+ SetattrMtime SetattrValid = 1 << 5
+ SetattrHandle SetattrValid = 1 << 6 // TODO: What does this mean?
+
+ // Linux only(?)
+ SetattrAtimeNow SetattrValid = 1 << 7
+ SetattrMtimeNow SetattrValid = 1 << 8
+ SetattrLockOwner SetattrValid = 1 << 9 // http://www.mail-archive.com/git-commits-head@vger.kernel.org/msg27852.html
+
+ // OS X only
+ SetattrCrtime SetattrValid = 1 << 28
+ SetattrChgtime SetattrValid = 1 << 29
+ SetattrBkuptime SetattrValid = 1 << 30
+ SetattrFlags SetattrValid = 1 << 31
+)
+
+func (fl SetattrValid) Mode() bool { return fl&SetattrMode != 0 }
+func (fl SetattrValid) Uid() bool { return fl&SetattrUid != 0 }
+func (fl SetattrValid) Gid() bool { return fl&SetattrGid != 0 }
+func (fl SetattrValid) Size() bool { return fl&SetattrSize != 0 }
+func (fl SetattrValid) Atime() bool { return fl&SetattrAtime != 0 }
+func (fl SetattrValid) Mtime() bool { return fl&SetattrMtime != 0 }
+func (fl SetattrValid) Handle() bool { return fl&SetattrHandle != 0 }
+func (fl SetattrValid) Crtime() bool { return fl&SetattrCrtime != 0 }
+func (fl SetattrValid) Chgtime() bool { return fl&SetattrChgtime != 0 }
+func (fl SetattrValid) Bkuptime() bool { return fl&SetattrBkuptime != 0 }
+func (fl SetattrValid) Flags() bool { return fl&SetattrFlags != 0 }
+
+func (fl SetattrValid) String() string {
+ return flagString(uint32(fl), setattrValidNames)
+}
+
+var setattrValidNames = []flagName{
+ {uint32(SetattrMode), "SetattrMode"},
+ {uint32(SetattrUid), "SetattrUid"},
+ {uint32(SetattrGid), "SetattrGid"},
+ {uint32(SetattrSize), "SetattrSize"},
+ {uint32(SetattrAtime), "SetattrAtime"},
+ {uint32(SetattrMtime), "SetattrMtime"},
+ {uint32(SetattrHandle), "SetattrHandle"},
+ {uint32(SetattrCrtime), "SetattrCrtime"},
+ {uint32(SetattrChgtime), "SetattrChgtime"},
+ {uint32(SetattrBkuptime), "SetattrBkuptime"},
+ {uint32(SetattrFlags), "SetattrFlags"},
+}
+
+// The OpenFlags are returned in the OpenResponse.
+type OpenFlags uint32
+
+const (
+ OpenDirectIO OpenFlags = 1 << 0 // bypass page cache for this open file
+ OpenKeepCache OpenFlags = 1 << 1 // don't invalidate the data cache on open
+ OpenNonSeekable OpenFlags = 1 << 2 // (Linux?)
+
+ OpenPurgeAttr OpenFlags = 1 << 30 // OS X
+ OpenPurgeUBC OpenFlags = 1 << 31 // OS X
+)
+
+func (fl OpenFlags) String() string {
+ return flagString(uint32(fl), openFlagNames)
+}
+
+var openFlagNames = []flagName{
+ {uint32(OpenDirectIO), "OpenDirectIO"},
+ {uint32(OpenKeepCache), "OpenKeepCache"},
+ {uint32(OpenPurgeAttr), "OpenPurgeAttr"},
+ {uint32(OpenPurgeUBC), "OpenPurgeUBC"},
+}
+
+// The InitFlags are used in the Init exchange.
+type InitFlags uint32
+
+const (
+ InitAsyncRead InitFlags = 1 << 0
+ InitPosixLocks InitFlags = 1 << 1
+
+ InitCaseSensitive InitFlags = 1 << 29 // OS X only
+ InitVolRename InitFlags = 1 << 30 // OS X only
+ InitXtimes InitFlags = 1 << 31 // OS X only
+)
+
+type flagName struct {
+ bit uint32
+ name string
+}
+
+var initFlagNames = []flagName{
+ {uint32(InitAsyncRead), "InitAsyncRead"},
+ {uint32(InitPosixLocks), "InitPosixLocks"},
+ {uint32(InitCaseSensitive), "InitCaseSensitive"},
+ {uint32(InitVolRename), "InitVolRename"},
+ {uint32(InitXtimes), "InitXtimes"},
+}
+
+func (fl InitFlags) String() string {
+ return flagString(uint32(fl), initFlagNames)
+}
+
+func flagString(f uint32, names []flagName) string {
+ var s string
+
+ if f == 0 {
+ return "0"
+ }
+
+ for _, n := range names {
+ if f&n.bit != 0 {
+ s += "+" + n.name
+ f &^= n.bit
+ }
+ }
+ if f != 0 {
+ s += fmt.Sprintf("%+#x", f)
+ }
+ return s[1:]
+}
+
+// The ReleaseFlags are used in the Release exchange.
+type ReleaseFlags uint32
+
+const (
+ ReleaseFlush ReleaseFlags = 1 << 0
+)
+
+func (fl ReleaseFlags) String() string {
+ return flagString(uint32(fl), releaseFlagNames)
+}
+
+var releaseFlagNames = []flagName{
+ {uint32(ReleaseFlush), "ReleaseFlush"},
+}
+
+// Opcodes
+const (
+ opLookup = 1
+ opForget = 2 // no reply
+ opGetattr = 3
+ opSetattr = 4
+ opReadlink = 5
+ opSymlink = 6
+ opMknod = 8
+ opMkdir = 9
+ opUnlink = 10
+ opRmdir = 11
+ opRename = 12
+ opLink = 13
+ opOpen = 14
+ opRead = 15
+ opWrite = 16
+ opStatfs = 17
+ opRelease = 18
+ opFsync = 20
+ opSetxattr = 21
+ opGetxattr = 22
+ opListxattr = 23
+ opRemovexattr = 24
+ opFlush = 25
+ opInit = 26
+ opOpendir = 27
+ opReaddir = 28
+ opReleasedir = 29
+ opFsyncdir = 30
+ opGetlk = 31
+ opSetlk = 32
+ opSetlkw = 33
+ opAccess = 34
+ opCreate = 35
+ opInterrupt = 36
+ opBmap = 37
+ opDestroy = 38
+ opIoctl = 39 // Linux?
+ opPoll = 40 // Linux?
+
+ // OS X
+ opSetvolname = 61
+ opGetxtimes = 62
+ opExchange = 63
+)
+
+// The read buffer is required to be at least 8k but may be much larger
+const minReadBuffer = 8192
+
+type entryOut struct {
+ outHeader
+ Nodeid uint64 // Inode ID
+ Generation uint64 // Inode generation
+ EntryValid uint64 // Cache timeout for the name
+ AttrValid uint64 // Cache timeout for the attributes
+ EntryValidNsec uint32
+ AttrValidNsec uint32
+ Attr attr
+}
+
+type forgetIn struct {
+ Nlookup uint64
+}
+
+type attrOut struct {
+ outHeader
+ AttrValid uint64 // Cache timeout for the attributes
+ AttrValidNsec uint32
+ Dummy uint32
+ Attr attr
+}
+
+// OS X
+type getxtimesOut struct {
+ outHeader
+ Bkuptime uint64
+ Crtime uint64
+ BkuptimeNsec uint32
+ CrtimeNsec uint32
+}
+
+type mknodIn struct {
+ Mode uint32
+ Rdev uint32
+ // "filename\x00" follows.
+}
+
+type mkdirIn struct {
+ Mode uint32
+ Padding uint32
+ // filename follows
+}
+
+type renameIn struct {
+ Newdir uint64
+ // "oldname\x00newname\x00" follows
+}
+
+// OS X
+type exchangeIn struct {
+ Olddir uint64
+ Newdir uint64
+ Options uint64
+}
+
+type linkIn struct {
+ Oldnodeid uint64
+}
+
+type setattrInCommon struct {
+ Valid uint32
+ Padding uint32
+ Fh uint64
+ Size uint64
+ LockOwner uint64 // unused on OS X?
+ Atime uint64
+ Mtime uint64
+ Unused2 uint64
+ AtimeNsec uint32
+ MtimeNsec uint32
+ Unused3 uint32
+ Mode uint32
+ Unused4 uint32
+ Uid uint32
+ Gid uint32
+ Unused5 uint32
+}
+
+type openIn struct {
+ Flags uint32
+ Mode uint32
+}
+
+type openOut struct {
+ outHeader
+ Fh uint64
+ OpenFlags uint32
+ Padding uint32
+}
+
+type createOut struct {
+ outHeader
+
+ Nodeid uint64 // Inode ID
+ Generation uint64 // Inode generation
+ EntryValid uint64 // Cache timeout for the name
+ AttrValid uint64 // Cache timeout for the attributes
+ EntryValidNsec uint32
+ AttrValidNsec uint32
+ Attr attr
+
+ Fh uint64
+ OpenFlags uint32
+ Padding uint32
+}
+
+type releaseIn struct {
+ Fh uint64
+ Flags uint32
+ ReleaseFlags uint32
+ LockOwner uint32
+}
+
+type flushIn struct {
+ Fh uint64
+ FlushFlags uint32
+ Padding uint32
+ LockOwner uint64
+}
+
+type readIn struct {
+ Fh uint64
+ Offset uint64
+ Size uint32
+ Padding uint32
+}
+
+type writeIn struct {
+ Fh uint64
+ Offset uint64
+ Size uint32
+ WriteFlags uint32
+}
+
+type writeOut struct {
+ outHeader
+ Size uint32
+ Padding uint32
+}
+
+// The WriteFlags are returned in the WriteResponse.
+type WriteFlags uint32
+
+func (fl WriteFlags) String() string {
+ return flagString(uint32(fl), writeFlagNames)
+}
+
+var writeFlagNames = []flagName{}
+
+const compatStatfsSize = 48
+
+type statfsOut struct {
+ outHeader
+ St kstatfs
+}
+
+type fsyncIn struct {
+ Fh uint64
+ FsyncFlags uint32
+ Padding uint32
+}
+
+type setxattrIn struct {
+ Size uint32
+ Flags uint32
+}
+
+type setxattrInOSX struct {
+ Size uint32
+ Flags uint32
+
+ // OS X only
+ Position uint32
+ Padding uint32
+}
+
+type getxattrIn struct {
+ Size uint32
+ Padding uint32
+}
+
+type getxattrInOSX struct {
+ Size uint32
+ Padding uint32
+
+ // OS X only
+ Position uint32
+ Padding2 uint32
+}
+
+type getxattrOut struct {
+ outHeader
+ Size uint32
+ Padding uint32
+}
+
+type lkIn struct {
+ Fh uint64
+ Owner uint64
+ Lk fileLock
+}
+
+type lkOut struct {
+ outHeader
+ Lk fileLock
+}
+
+type accessIn struct {
+ Mask uint32
+ Padding uint32
+}
+
+type initIn struct {
+ Major uint32
+ Minor uint32
+ MaxReadahead uint32
+ Flags uint32
+}
+
+const initInSize = int(unsafe.Sizeof(initIn{}))
+
+type initOut struct {
+ outHeader
+ Major uint32
+ Minor uint32
+ MaxReadahead uint32
+ Flags uint32
+ Unused uint32
+ MaxWrite uint32
+}
+
+type interruptIn struct {
+ Unique uint64
+}
+
+type bmapIn struct {
+ Block uint64
+ BlockSize uint32
+ Padding uint32
+}
+
+type bmapOut struct {
+ outHeader
+ Block uint64
+}
+
+type inHeader struct {
+ Len uint32
+ Opcode uint32
+ Unique uint64
+ Nodeid uint64
+ Uid uint32
+ Gid uint32
+ Pid uint32
+ Padding uint32
+}
+
+const inHeaderSize = int(unsafe.Sizeof(inHeader{}))
+
+type outHeader struct {
+ Len uint32
+ Error int32
+ Unique uint64
+}
+
+type dirent struct {
+ Ino uint64
+ Off uint64
+ Namelen uint32
+ Type uint32
+ Name [0]byte
+}
+
+const direntSize = 8 + 8 + 4 + 4
diff --git a/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_darwin.go b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_darwin.go
new file mode 100644
index 000000000..f7bc37766
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_darwin.go
@@ -0,0 +1,58 @@
+package fuse
+
+import (
+ "time"
+)
+
+type attr struct {
+ Ino uint64
+ Size uint64
+ Blocks uint64
+ Atime uint64
+ Mtime uint64
+ Ctime uint64
+ Crtime_ uint64 // OS X only
+ AtimeNsec uint32
+ MtimeNsec uint32
+ CtimeNsec uint32
+ CrtimeNsec uint32 // OS X only
+ Mode uint32
+ Nlink uint32
+ Uid uint32
+ Gid uint32
+ Rdev uint32
+ Flags_ uint32 // OS X only; see chflags(2)
+}
+
+func (a *attr) SetCrtime(s uint64, ns uint32) {
+ a.Crtime_, a.CrtimeNsec = s, ns
+}
+
+func (a *attr) SetFlags(f uint32) {
+ a.Flags_ = f
+}
+
+type setattrIn struct {
+ setattrInCommon
+
+ // OS X only
+ Bkuptime_ uint64
+ Chgtime_ uint64
+ Crtime uint64
+ BkuptimeNsec uint32
+ ChgtimeNsec uint32
+ CrtimeNsec uint32
+ Flags_ uint32 // see chflags(2)
+}
+
+func (in *setattrIn) BkupTime() time.Time {
+ return time.Unix(int64(in.Bkuptime_), int64(in.BkuptimeNsec))
+}
+
+func (in *setattrIn) Chgtime() time.Time {
+ return time.Unix(int64(in.Chgtime_), int64(in.ChgtimeNsec))
+}
+
+func (in *setattrIn) Flags() uint32 {
+ return in.Flags_
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_linux.go b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_linux.go
new file mode 100644
index 000000000..914bc3063
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_linux.go
@@ -0,0 +1,50 @@
+package fuse
+
+import "time"
+
+type attr struct {
+ Ino uint64
+ Size uint64
+ Blocks uint64
+ Atime uint64
+ Mtime uint64
+ Ctime uint64
+ AtimeNsec uint32
+ MtimeNsec uint32
+ CtimeNsec uint32
+ Mode uint32
+ Nlink uint32
+ Uid uint32
+ Gid uint32
+ Rdev uint32
+ // Blksize uint32 // Only in protocol 7.9
+ // padding_ uint32 // Only in protocol 7.9
+}
+
+func (a *attr) Crtime() time.Time {
+ return time.Time{}
+}
+
+func (a *attr) SetCrtime(s uint64, ns uint32) {
+ // Ignored on Linux.
+}
+
+func (a *attr) SetFlags(f uint32) {
+ // Ignored on Linux.
+}
+
+type setattrIn struct {
+ setattrInCommon
+}
+
+func (in *setattrIn) BkupTime() time.Time {
+ return time.Time{}
+}
+
+func (in *setattrIn) Chgtime() time.Time {
+ return time.Time{}
+}
+
+func (in *setattrIn) Flags() uint32 {
+ return 0
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_std.go b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_std.go
new file mode 100644
index 000000000..074cfd322
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/fuse_kernel_std.go
@@ -0,0 +1 @@
+package fuse
diff --git a/vendor/github.com/mattermost/rsc/fuse/fuse_test.go b/vendor/github.com/mattermost/rsc/fuse/fuse_test.go
new file mode 100644
index 000000000..61533b8c5
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/fuse_test.go
@@ -0,0 +1,594 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fuse
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+ "os/exec"
+ "runtime"
+ "syscall"
+ "testing"
+ "time"
+)
+
+var fuseRun = flag.String("fuserun", "", "which fuse test to run. runs all if empty.")
+
+// umount tries its best to unmount dir.
+func umount(dir string) {
+ err := exec.Command("umount", dir).Run()
+ if err != nil && runtime.GOOS == "linux" {
+ exec.Command("/bin/fusermount", "-u", dir).Run()
+ }
+}
+
+func TestFuse(t *testing.T) {
+ Debugf = log.Printf
+ dir, err := ioutil.TempDir("", "fusetest")
+ if err != nil {
+ t.Fatal(err)
+ }
+ os.MkdirAll(dir, 0777)
+
+ c, err := Mount(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer umount(dir)
+
+ go func() {
+ err := c.Serve(testFS{})
+ if err != nil {
+ fmt.Printf("SERVE ERROR: %v\n", err)
+ }
+ }()
+
+ waitForMount(t, dir)
+
+ for _, tt := range fuseTests {
+ if *fuseRun == "" || *fuseRun == tt.name {
+ t.Logf("running %T", tt.node)
+ tt.node.test(dir+"/"+tt.name, t)
+ }
+ }
+}
+
+func waitForMount(t *testing.T, dir string) {
+ // Filename to wait for in dir:
+ probeEntry := *fuseRun
+ if probeEntry == "" {
+ probeEntry = fuseTests[0].name
+ }
+ for tries := 0; tries < 100; tries++ {
+ _, err := os.Stat(dir + "/" + probeEntry)
+ if err == nil {
+ return
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ t.Fatalf("mount did not work")
+}
+
+var fuseTests = []struct {
+ name string
+ node interface {
+ Node
+ test(string, *testing.T)
+ }
+}{
+ {"readAll", readAll{}},
+ {"readAll1", &readAll1{}},
+ {"write", &write{}},
+ {"writeAll", &writeAll{}},
+ {"writeAll2", &writeAll2{}},
+ {"release", &release{}},
+ {"mkdir1", &mkdir1{}},
+ {"create1", &create1{}},
+ {"create2", &create2{}},
+ {"symlink1", &symlink1{}},
+ {"link1", &link1{}},
+ {"rename1", &rename1{}},
+ {"mknod1", &mknod1{}},
+}
+
+// TO TEST:
+// Statfs
+// Lookup(*LookupRequest, *LookupResponse)
+// Getattr(*GetattrRequest, *GetattrResponse)
+// Attr with explicit inode
+// Setattr(*SetattrRequest, *SetattrResponse)
+// Access(*AccessRequest)
+// Open(*OpenRequest, *OpenResponse)
+// Getxattr, Setxattr, Listxattr, Removexattr
+// Write(*WriteRequest, *WriteResponse)
+// Flush(*FlushRequest, *FlushResponse)
+
+// Test Read calling ReadAll.
+
+type readAll struct{ file }
+
+const hi = "hello, world"
+
+func (readAll) ReadAll(intr Intr) ([]byte, Error) {
+ return []byte(hi), nil
+}
+
+func (readAll) test(path string, t *testing.T) {
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ t.Errorf("readAll: %v", err)
+ return
+ }
+ if string(data) != hi {
+ t.Errorf("readAll = %q, want %q", data, hi)
+ }
+}
+
+// Test Read.
+
+type readAll1 struct{ file }
+
+func (readAll1) Read(req *ReadRequest, resp *ReadResponse, intr Intr) Error {
+ HandleRead(req, resp, []byte(hi))
+ return nil
+}
+
+func (readAll1) test(path string, t *testing.T) {
+ readAll{}.test(path, t)
+}
+
+// Test Write calling basic Write, with an fsync thrown in too.
+
+type write struct {
+ file
+ data []byte
+ gotfsync bool
+}
+
+func (w *write) Write(req *WriteRequest, resp *WriteResponse, intr Intr) Error {
+ w.data = append(w.data, req.Data...)
+ resp.Size = len(req.Data)
+ return nil
+}
+
+func (w *write) Fsync(r *FsyncRequest, intr Intr) Error {
+ w.gotfsync = true
+ return nil
+}
+
+func (w *write) test(path string, t *testing.T) {
+ log.Printf("pre-write Create")
+ f, err := os.Create(path)
+ if err != nil {
+ t.Fatalf("Create: %v", err)
+ }
+ log.Printf("pre-write Write")
+ n, err := f.Write([]byte(hi))
+ if err != nil {
+ t.Fatalf("Write: %v", err)
+ }
+ if n != len(hi) {
+ t.Fatalf("short write; n=%d; hi=%d", n, len(hi))
+ }
+
+ err = syscall.Fsync(int(f.Fd()))
+ if err != nil {
+ t.Fatalf("Fsync = %v", err)
+ }
+ if !w.gotfsync {
+ t.Errorf("never received expected fsync call")
+ }
+
+ log.Printf("pre-write Close")
+ err = f.Close()
+ if err != nil {
+ t.Fatalf("Close: %v", err)
+ }
+ log.Printf("post-write Close")
+ if string(w.data) != hi {
+ t.Errorf("writeAll = %q, want %q", w.data, hi)
+ }
+}
+
+// Test Write calling WriteAll.
+
+type writeAll struct {
+ file
+ data []byte
+ gotfsync bool
+}
+
+func (w *writeAll) Fsync(r *FsyncRequest, intr Intr) Error {
+ w.gotfsync = true
+ return nil
+}
+
+func (w *writeAll) WriteAll(data []byte, intr Intr) Error {
+ w.data = data
+ return nil
+}
+
+func (w *writeAll) test(path string, t *testing.T) {
+ err := ioutil.WriteFile(path, []byte(hi), 0666)
+ if err != nil {
+ t.Fatalf("WriteFile: %v", err)
+ return
+ }
+ if string(w.data) != hi {
+ t.Errorf("writeAll = %q, want %q", w.data, hi)
+ }
+}
+
+// Test Write calling Setattr+Write+Flush.
+
+type writeAll2 struct {
+ file
+ data []byte
+ setattr bool
+ flush bool
+}
+
+func (w *writeAll2) Setattr(req *SetattrRequest, resp *SetattrResponse, intr Intr) Error {
+ w.setattr = true
+ return nil
+}
+
+func (w *writeAll2) Flush(req *FlushRequest, intr Intr) Error {
+ w.flush = true
+ return nil
+}
+
+func (w *writeAll2) Write(req *WriteRequest, resp *WriteResponse, intr Intr) Error {
+ w.data = append(w.data, req.Data...)
+ resp.Size = len(req.Data)
+ return nil
+}
+
+func (w *writeAll2) test(path string, t *testing.T) {
+ err := ioutil.WriteFile(path, []byte(hi), 0666)
+ if err != nil {
+ t.Errorf("WriteFile: %v", err)
+ return
+ }
+ if !w.setattr || string(w.data) != hi || !w.flush {
+ t.Errorf("writeAll = %v, %q, %v, want %v, %q, %v", w.setattr, string(w.data), w.flush, true, hi, true)
+ }
+}
+
+// Test Mkdir.
+
+type mkdir1 struct {
+ dir
+ name string
+}
+
+func (f *mkdir1) Mkdir(req *MkdirRequest, intr Intr) (Node, Error) {
+ f.name = req.Name
+ return &mkdir1{}, nil
+}
+
+func (f *mkdir1) test(path string, t *testing.T) {
+ f.name = ""
+ err := os.Mkdir(path+"/foo", 0777)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ if f.name != "foo" {
+ t.Error(err)
+ return
+ }
+}
+
+// Test Create (and fsync)
+
+type create1 struct {
+ dir
+ name string
+ f *writeAll
+}
+
+func (f *create1) Create(req *CreateRequest, resp *CreateResponse, intr Intr) (Node, Handle, Error) {
+ f.name = req.Name
+ f.f = &writeAll{}
+ return f.f, f.f, nil
+}
+
+func (f *create1) test(path string, t *testing.T) {
+ f.name = ""
+ ff, err := os.Create(path + "/foo")
+ if err != nil {
+ t.Errorf("create1 WriteFile: %v", err)
+ return
+ }
+
+ err = syscall.Fsync(int(ff.Fd()))
+ if err != nil {
+ t.Fatalf("Fsync = %v", err)
+ }
+
+ if !f.f.gotfsync {
+ t.Errorf("never received expected fsync call")
+ }
+
+ ff.Close()
+ if f.name != "foo" {
+ t.Errorf("create1 name=%q want foo", f.name)
+ }
+}
+
+// Test Create + WriteAll + Remove
+
+type create2 struct {
+ dir
+ name string
+ f *writeAll
+ fooExists bool
+}
+
+func (f *create2) Create(req *CreateRequest, resp *CreateResponse, intr Intr) (Node, Handle, Error) {
+ f.name = req.Name
+ f.f = &writeAll{}
+ return f.f, f.f, nil
+}
+
+func (f *create2) Lookup(name string, intr Intr) (Node, Error) {
+ if f.fooExists && name == "foo" {
+ return file{}, nil
+ }
+ return nil, ENOENT
+}
+
+func (f *create2) Remove(r *RemoveRequest, intr Intr) Error {
+ if f.fooExists && r.Name == "foo" && !r.Dir {
+ f.fooExists = false
+ return nil
+ }
+ return ENOENT
+}
+
+func (f *create2) test(path string, t *testing.T) {
+ f.name = ""
+ err := ioutil.WriteFile(path+"/foo", []byte(hi), 0666)
+ if err != nil {
+ t.Fatalf("create2 WriteFile: %v", err)
+ }
+ if string(f.f.data) != hi {
+ t.Fatalf("create2 writeAll = %q, want %q", f.f.data, hi)
+ }
+
+ f.fooExists = true
+ log.Printf("pre-Remove")
+ err = os.Remove(path + "/foo")
+ if err != nil {
+ t.Fatalf("Remove: %v", err)
+ }
+ err = os.Remove(path + "/foo")
+ if err == nil {
+ t.Fatalf("second Remove = nil; want some error")
+ }
+}
+
+// Test symlink + readlink
+
+type symlink1 struct {
+ dir
+ newName, target string
+}
+
+func (f *symlink1) Symlink(req *SymlinkRequest, intr Intr) (Node, Error) {
+ f.newName = req.NewName
+ f.target = req.Target
+ return symlink{target: req.Target}, nil
+}
+
+func (f *symlink1) test(path string, t *testing.T) {
+ const target = "/some-target"
+
+ err := os.Symlink(target, path+"/symlink.file")
+ if err != nil {
+ t.Errorf("os.Symlink: %v", err)
+ return
+ }
+
+ if f.newName != "symlink.file" {
+ t.Errorf("symlink newName = %q; want %q", f.newName, "symlink.file")
+ }
+ if f.target != target {
+ t.Errorf("symlink target = %q; want %q", f.target, target)
+ }
+
+ gotName, err := os.Readlink(path + "/symlink.file")
+ if err != nil {
+ t.Errorf("os.Readlink: %v", err)
+ return
+ }
+ if gotName != target {
+ t.Errorf("os.Readlink = %q; want %q", gotName, target)
+ }
+}
+
+// Test link
+
+type link1 struct {
+ dir
+ newName string
+}
+
+func (f *link1) Lookup(name string, intr Intr) (Node, Error) {
+ if name == "old" {
+ return file{}, nil
+ }
+ return nil, ENOENT
+}
+
+func (f *link1) Link(r *LinkRequest, old Node, intr Intr) (Node, Error) {
+ f.newName = r.NewName
+ return file{}, nil
+}
+
+func (f *link1) test(path string, t *testing.T) {
+ err := os.Link(path+"/old", path+"/new")
+ if err != nil {
+ t.Fatalf("Link: %v", err)
+ }
+ if f.newName != "new" {
+ t.Fatalf("saw Link for newName %q; want %q", f.newName, "new")
+ }
+}
+
+// Test Rename
+
+type rename1 struct {
+ dir
+ renames int
+}
+
+func (f *rename1) Lookup(name string, intr Intr) (Node, Error) {
+ if name == "old" {
+ return file{}, nil
+ }
+ return nil, ENOENT
+}
+
+func (f *rename1) Rename(r *RenameRequest, newDir Node, intr Intr) Error {
+ if r.OldName == "old" && r.NewName == "new" && newDir == f {
+ f.renames++
+ return nil
+ }
+ return EIO
+}
+
+func (f *rename1) test(path string, t *testing.T) {
+ err := os.Rename(path+"/old", path+"/new")
+ if err != nil {
+ t.Fatalf("Rename: %v", err)
+ }
+ if f.renames != 1 {
+ t.Fatalf("expected rename didn't happen")
+ }
+ err = os.Rename(path+"/old2", path+"/new2")
+ if err == nil {
+ t.Fatal("expected error on second Rename; got nil")
+ }
+}
+
+// Test Release.
+
+type release struct {
+ file
+ did bool
+}
+
+func (r *release) Release(*ReleaseRequest, Intr) Error {
+ r.did = true
+ return nil
+}
+
+func (r *release) test(path string, t *testing.T) {
+ r.did = false
+ f, err := os.Open(path)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ f.Close()
+ time.Sleep(1 * time.Second)
+ if !r.did {
+ t.Error("Close did not Release")
+ }
+}
+
+// Test mknod
+
+type mknod1 struct {
+ dir
+ gotr *MknodRequest
+}
+
+func (f *mknod1) Mknod(r *MknodRequest, intr Intr) (Node, Error) {
+ f.gotr = r
+ return fifo{}, nil
+}
+
+func (f *mknod1) test(path string, t *testing.T) {
+ if os.Getuid() != 0 {
+ t.Logf("skipping unless root")
+ return
+ }
+ defer syscall.Umask(syscall.Umask(0))
+ err := syscall.Mknod(path+"/node", syscall.S_IFIFO|0666, 123)
+ if err != nil {
+ t.Fatalf("Mknod: %v", err)
+ }
+ if f.gotr == nil {
+ t.Fatalf("no recorded MknodRequest")
+ }
+ if g, e := f.gotr.Name, "node"; g != e {
+ t.Errorf("got Name = %q; want %q", g, e)
+ }
+ if g, e := f.gotr.Rdev, uint32(123); g != e {
+ if runtime.GOOS == "linux" {
+ // Linux fuse doesn't echo back the rdev if the node
+ // isn't a device (we're using a FIFO here, as that
+ // bit is portable.)
+ } else {
+ t.Errorf("got Rdev = %v; want %v", g, e)
+ }
+ }
+ if g, e := f.gotr.Mode, os.FileMode(os.ModeNamedPipe|0666); g != e {
+ t.Errorf("got Mode = %v; want %v", g, e)
+ }
+ t.Logf("Got request: %#v", f.gotr)
+}
+
+type file struct{}
+type dir struct{}
+type fifo struct{}
+type symlink struct {
+ target string
+}
+
+func (f file) Attr() Attr { return Attr{Mode: 0666} }
+func (f dir) Attr() Attr { return Attr{Mode: os.ModeDir | 0777} }
+func (f fifo) Attr() Attr { return Attr{Mode: os.ModeNamedPipe | 0666} }
+func (f symlink) Attr() Attr { return Attr{Mode: os.ModeSymlink | 0666} }
+
+func (f symlink) Readlink(*ReadlinkRequest, Intr) (string, Error) {
+ return f.target, nil
+}
+
+type testFS struct{}
+
+func (testFS) Root() (Node, Error) {
+ return testFS{}, nil
+}
+
+func (testFS) Attr() Attr {
+ return Attr{Mode: os.ModeDir | 0555}
+}
+
+func (testFS) Lookup(name string, intr Intr) (Node, Error) {
+ for _, tt := range fuseTests {
+ if tt.name == name {
+ return tt.node, nil
+ }
+ }
+ return nil, ENOENT
+}
+
+func (testFS) ReadDir(intr Intr) ([]Dirent, Error) {
+ var dirs []Dirent
+ for _, tt := range fuseTests {
+ if *fuseRun == "" || *fuseRun == tt.name {
+ log.Printf("Readdir; adding %q", tt.name)
+ dirs = append(dirs, Dirent{Name: tt.name})
+ }
+ }
+ return dirs, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/hellofs/hello.go b/vendor/github.com/mattermost/rsc/fuse/hellofs/hello.go
new file mode 100644
index 000000000..d915473f1
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/hellofs/hello.go
@@ -0,0 +1,62 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Hellofs implements a simple "hello world" file system.
+package main
+
+import (
+ "log"
+ "os"
+
+ "github.com/mattermost/rsc/fuse"
+)
+
+func main() {
+ c, err := fuse.Mount("/mnt/hellofs")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ c.Serve(FS{})
+}
+
+// FS implements the hello world file system.
+type FS struct{}
+
+func (FS) Root() (fuse.Node, fuse.Error) {
+ return Dir{}, nil
+}
+
+// Dir implements both Node and Handle for the root directory.
+type Dir struct{}
+
+func (Dir) Attr() fuse.Attr {
+ return fuse.Attr{Mode: os.ModeDir | 0555}
+}
+
+func (Dir) Lookup(name string, intr fuse.Intr) (fuse.Node, fuse.Error) {
+ if name == "hello" {
+ return File{}, nil
+ }
+ return nil, fuse.ENOENT
+}
+
+var dirDirs = []fuse.Dirent{
+ {Inode: 2, Name: "hello", Type: 0},
+}
+
+func (Dir) ReadDir(intr fuse.Intr) ([]fuse.Dirent, fuse.Error) {
+ return dirDirs, nil
+}
+
+// File implements both Node and Handle for the hello file.
+type File struct{}
+
+func (File) Attr() fuse.Attr {
+ return fuse.Attr{Mode: 0444}
+}
+
+func (File) ReadAll(intr fuse.Intr) ([]byte, fuse.Error) {
+ return []byte("hello, world\n"), nil
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/mount_darwin.go b/vendor/github.com/mattermost/rsc/fuse/mount_darwin.go
new file mode 100644
index 000000000..5e2caaa76
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/mount_darwin.go
@@ -0,0 +1,122 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TODO: Rewrite using package syscall not cgo
+
+package fuse
+
+/*
+
+// Adapted from Plan 9 from User Space's src/cmd/9pfuse/fuse.c,
+// which carries this notice:
+//
+// The files in this directory are subject to the following license.
+//
+// The author of this software is Russ Cox.
+//
+// Copyright (c) 2006 Russ Cox
+//
+// Permission to use, copy, modify, and distribute this software for any
+// purpose without fee is hereby granted, provided that this entire notice
+// is included in all copies of any software which is or includes a copy
+// or modification of this software and in all copies of the supporting
+// documentation for such software.
+//
+// THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
+// WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR WARRANTY
+// OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS
+// FITNESS FOR ANY PARTICULAR PURPOSE.
+
+#include <stdlib.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#define nil ((void*)0)
+
+static int
+mountfuse(char *mtpt, char **err)
+{
+ int i, pid, fd, r;
+ char buf[200];
+ struct vfsconf vfs;
+ char *f;
+
+ if(getvfsbyname("fusefs", &vfs) < 0){
+ if(access(f="/Library/Filesystems/osxfusefs.fs"
+ "/Support/load_osxfusefs", 0) < 0){
+ *err = strdup("cannot find load_fusefs");
+ return -1;
+ }
+ if((r=system(f)) < 0){
+ snprintf(buf, sizeof buf, "%s: %s", f, strerror(errno));
+ *err = strdup(buf);
+ return -1;
+ }
+ if(r != 0){
+ snprintf(buf, sizeof buf, "load_fusefs failed: exit %d", r);
+ *err = strdup(buf);
+ return -1;
+ }
+ if(getvfsbyname("osxfusefs", &vfs) < 0){
+ snprintf(buf, sizeof buf, "getvfsbyname osxfusefs: %s", strerror(errno));
+ *err = strdup(buf);
+ return -1;
+ }
+ }
+
+ // Look for available FUSE device.
+ for(i=0;; i++){
+ snprintf(buf, sizeof buf, "/dev/osxfuse%d", i);
+ if(access(buf, 0) < 0){
+ *err = strdup("no available fuse devices");
+ return -1;
+ }
+ if((fd = open(buf, O_RDWR)) >= 0)
+ break;
+ }
+
+ pid = fork();
+ if(pid < 0)
+ return -1;
+ if(pid == 0){
+ snprintf(buf, sizeof buf, "%d", fd);
+ setenv("MOUNT_FUSEFS_CALL_BY_LIB", "", 1);
+ // Different versions of MacFUSE put the
+ // mount_fusefs binary in different places.
+ // Try all.
+ // Leopard location
+ setenv("MOUNT_FUSEFS_DAEMON_PATH",
+ "/Library/Filesystems/osxfusefs.fs/Support/mount_osxfusefs", 1);
+ execl("/Library/Filesystems/osxfusefs.fs/Support/mount_osxfusefs",
+ "mount_osxfusefs",
+ "-o", "iosize=4096", buf, mtpt, nil);
+ fprintf(stderr, "exec mount_osxfusefs: %s\n", strerror(errno));
+ _exit(1);
+ }
+ return fd;
+}
+
+*/
+import "C"
+
+import "unsafe"
+
+func mount(dir string) (int, string) {
+ errp := (**C.char)(C.malloc(16))
+ *errp = nil
+ defer C.free(unsafe.Pointer(errp))
+ cdir := C.CString(dir)
+ defer C.free(unsafe.Pointer(cdir))
+ fd := C.mountfuse(cdir, errp)
+ var err string
+ if *errp != nil {
+ err = C.GoString(*errp)
+ }
+ return int(fd), err
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/mount_linux.go b/vendor/github.com/mattermost/rsc/fuse/mount_linux.go
new file mode 100644
index 000000000..e5bc58b8a
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/mount_linux.go
@@ -0,0 +1,67 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fuse
+
+import (
+ "fmt"
+ "net"
+ "os"
+ "os/exec"
+ "syscall"
+)
+
+func mount(dir string) (fusefd int, errmsg string) {
+ fds, err := syscall.Socketpair(syscall.AF_FILE, syscall.SOCK_STREAM, 0)
+ if err != nil {
+ return -1, fmt.Sprintf("socketpair error: %v", err)
+ }
+ defer syscall.Close(fds[0])
+ defer syscall.Close(fds[1])
+
+ cmd := exec.Command("/bin/fusermount", "--", dir)
+ cmd.Env = append(os.Environ(), "_FUSE_COMMFD=3")
+
+ writeFile := os.NewFile(uintptr(fds[0]), "fusermount-child-writes")
+ defer writeFile.Close()
+ cmd.ExtraFiles = []*os.File{writeFile}
+
+ out, err := cmd.CombinedOutput()
+ if len(out) > 0 || err != nil {
+ return -1, fmt.Sprintf("fusermount: %q, %v", out, err)
+ }
+
+ readFile := os.NewFile(uintptr(fds[1]), "fusermount-parent-reads")
+ defer readFile.Close()
+ c, err := net.FileConn(readFile)
+ if err != nil {
+ return -1, fmt.Sprintf("FileConn from fusermount socket: %v", err)
+ }
+ defer c.Close()
+
+ uc, ok := c.(*net.UnixConn)
+ if !ok {
+ return -1, fmt.Sprintf("unexpected FileConn type; expected UnixConn, got %T", c)
+ }
+
+ buf := make([]byte, 32) // expect 1 byte
+ oob := make([]byte, 32) // expect 24 bytes
+ _, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
+ scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
+ if err != nil {
+ return -1, fmt.Sprintf("ParseSocketControlMessage: %v", err)
+ }
+ if len(scms) != 1 {
+ return -1, fmt.Sprintf("expected 1 SocketControlMessage; got scms = %#v", scms)
+ }
+ scm := scms[0]
+ gotFds, err := syscall.ParseUnixRights(&scm)
+ if err != nil {
+ return -1, fmt.Sprintf("syscall.ParseUnixRights: %v", err)
+ }
+ if len(gotFds) != 1 {
+ return -1, fmt.Sprintf("wanted 1 fd; got %#v", gotFds)
+ }
+ return gotFds[0], ""
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/serve.go b/vendor/github.com/mattermost/rsc/fuse/serve.go
new file mode 100644
index 000000000..fa4f27e3f
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/serve.go
@@ -0,0 +1,1022 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// FUSE service loop, for servers that wish to use it.
+
+package fuse
+
+import (
+ "fmt"
+ "hash/fnv"
+ "io"
+ "log"
+ "os"
+ "path"
+ "sync"
+ "syscall"
+ "time"
+)
+
+// TODO: FINISH DOCS
+
+// An Intr is a channel that signals that a request has been interrupted.
+// Being able to receive from the channel means the request has been
+// interrupted.
+type Intr chan struct{}
+
+func (Intr) String() string { return "fuse.Intr" }
+
+// An FS is the interface required of a file system.
+//
+// Root() (Node, Error)
+//
+// Root is called to obtain the Node for the file system root.
+//
+// Optional Methods
+//
+// An FS implementation may implement
+// additional methods to handle the corresponding FUSE requests:
+//
+// Init(req *InitRequest, resp *InitResponse) Error
+//
+// Init is called to initialize the FUSE connection.
+// It can inspect the request and adjust the response as desired.
+// The default response sets MaxReadahead to 0 and MaxWrite to 4096.
+// Init must return promptly.
+//
+// Statfs(resp *StatfsResponse, intr Intr) Error
+//
+// Statfs is called to obtain file system metadata. It should write that data to resp.
+//
+// Rename(req *RenameRequest, intr Intr) Error
+//
+// XXXX this is not implemented like this. Instead, Rename is a method
+// on the source dierctory node, and takes a newDir Node parameter. Fix it like this?
+// Rename is called to rename the file req.OldName in the directory req.OldDir to
+// become the file req.NewName in the directory req.NewDir.
+//
+type FS interface {
+ Root() (Node, Error)
+}
+
+// A Node is the interface required of a file or directory.
+// See the documentation for type FS for general information
+// pertaining to all methods.
+//
+// Getattr(resp *GetattrResponse, intr Intr) fuse.Error
+//
+// Getattr obtains the standard metadata for the receiver.
+// It should store that metadata in resp.
+//
+// Open(xxx, intr Intr) (Handle, fuse.Error)
+//
+// Open opens the receiver.
+// XXX note about access. XXX OpenFlags.
+// XXX note that the Node may be a file or directory.
+//
+// Optional Methods
+//
+// An Node implementation may implement additional methods
+// to handle the corresponding FUSE requests.
+//
+// These optional requests can be called for both file and directory nodes:
+//
+// Access
+//
+// Access checks whether the calling context has permission for
+// the given operations on the receiver. If so, Access should return nil. If not, Access should
+// return EPERM. Note that this call affects the result of the access(2) system call
+// but not the open(2) system call. If Access is not implemented, the Node behaves
+// as if it always returns nil (permission granted), relying on checks in Open instead.
+//
+// Getxattr
+//
+// Getxattr obtains an extended attribute for the receiver.
+// XXX
+//
+// Listxattr
+//
+// Listxattr lists the extended attributes recorded for the receiver.
+//
+// Removexattr
+//
+// Removexattr removes an extended attribute from the receiver.
+//
+// Setattr
+//
+// Setattr sets the standard metadata for the receiver.
+//
+// Setxattr
+//
+// Setxattr sets an extended attribute for the receiver.
+//
+// Optional Directory Methods
+//
+// These optional requests will be called only for directory nodes:
+//
+// Create(xxx)
+//
+// Create creates
+//
+// Link(xxx)
+//
+// Link XXX
+//
+// Lookup(name string, intr Intr) (Node, Error)
+//
+// Lookup looks up a specific entry in the receiver,
+// which must be a directory. Lookup should return a Node
+// corresponding to the entry. If the name does not exist in
+// the directory, Lookup should return nil, err.
+//
+// Lookup need not to handle the names "." and "..".
+//
+// Mkdir
+//
+// Mkdir creates XXX
+//
+// Mknod XXX
+//
+// XXX
+//
+// Remove
+//
+// Remove removes the entry with the given name from
+// the receiver, which must be a directory. The entry to be removed
+// may correspond to a file (unlink) or to a directory (rmdir).
+//
+// Symlink
+//
+// Symlink creates a new symbolic link in the receiver, which must be a directory.
+// The entry
+//
+// Optional Symlink Methods
+//
+// This optional request will be called only for symbolic link nodes:
+//
+// Readlink
+//
+// Readlink reads a symbolic link.
+type Node interface {
+ Attr() Attr
+}
+
+var startTime = time.Now()
+
+func nodeAttr(inode uint64, n Node) (attr Attr) {
+ attr = n.Attr()
+ if attr.Nlink == 0 {
+ attr.Nlink = 1
+ }
+ if attr.Atime.IsZero() {
+ attr.Atime = startTime
+ }
+ if attr.Mtime.IsZero() {
+ attr.Mtime = startTime
+ }
+ if attr.Ctime.IsZero() {
+ attr.Ctime = startTime
+ }
+ if attr.Crtime.IsZero() {
+ attr.Crtime = startTime
+ }
+ if attr.Inode == 0 {
+ attr.Inode = inode
+ }
+ return
+}
+
+// A Handle is the interface required of an opened file or directory.
+// See the documentation for type FS for general information
+// pertaining to all methods.
+//
+// Flush
+//
+// Flush is called each time the file or directory is closed. Because there can be
+// multiple file descriptors referring to a single opened file, Flush can be called
+// multiple times.
+//
+// Optional Methods
+//
+// A Handle implementation may implement additional methods to handle
+// the corresponding FUSE requests. The most common to implement are
+// Read, ReadDir, and Write.
+//
+// Fsync
+//
+// Getlk
+//
+// Read
+//
+// Readdir
+//
+// Release
+//
+// Setlk
+//
+// Setlkw
+//
+// Write
+//
+type Handle interface {
+}
+
+// Serve serves the FUSE connection by making calls to the methods
+// of fs and the Nodes and Handles it makes available. It returns only
+// when the connection has been closed or an unexpected error occurs.
+func (c *Conn) Serve(fs FS) error {
+ if c.req != nil {
+ panic("fuse: Serve called twice")
+ }
+ c.req = map[RequestID]*serveRequest{}
+
+ root, err := fs.Root()
+ if err != nil {
+ return fmt.Errorf("cannot obtain root node: %v", syscall.Errno(err.(Errno)).Error())
+ }
+ c.node = append(c.node, nil, &serveNode{name: "/", node: root})
+ c.handle = append(c.handle, nil)
+
+ for {
+ req, err := c.ReadRequest()
+ if err != nil {
+ if err == io.EOF {
+ break
+ }
+ return err
+ }
+
+ go c.serve(fs, req)
+ }
+ return nil
+}
+
+type serveConn struct {
+ meta sync.Mutex
+ req map[RequestID]*serveRequest
+ node []*serveNode
+ handle []*serveHandle
+ freeNode []NodeID
+ freeHandle []HandleID
+ nodeGen uint64
+ nodeHandles []map[HandleID]bool // open handles for a node; slice index is NodeID
+}
+
+type serveRequest struct {
+ Request Request
+ Intr Intr
+}
+
+type serveNode struct {
+ name string
+ node Node
+ inode uint64
+ isDir bool
+}
+
+func (sn *serveNode) attr() (attr Attr) {
+ attr = nodeAttr(sn.inode, sn.node)
+ if attr.Inode == 0 {
+ sn.inode = hash(sn.name)
+ attr.Inode = sn.inode
+ }
+ sn.isDir = attr.Mode&os.ModeDir != 0
+ return
+}
+
+func hash(s string) uint64 {
+ f := fnv.New64()
+ f.Write([]byte(s))
+ return f.Sum64()
+}
+
+type serveHandle struct {
+ handle Handle
+ readData []byte
+ trunc bool
+ writeData []byte
+ nodeID NodeID
+}
+
+func (c *Conn) saveNode(name string, node Node) (id NodeID, gen uint64, sn *serveNode) {
+ sn = &serveNode{name: name, node: node}
+ c.meta.Lock()
+ if n := len(c.freeNode); n > 0 {
+ id = c.freeNode[n-1]
+ c.freeNode = c.freeNode[:n-1]
+ c.node[id] = sn
+ c.nodeGen++
+ } else {
+ id = NodeID(len(c.node))
+ c.node = append(c.node, sn)
+ }
+ gen = c.nodeGen
+ c.meta.Unlock()
+ return
+}
+
+func (c *Conn) saveHandle(handle Handle, nodeID NodeID) (id HandleID, shandle *serveHandle) {
+ c.meta.Lock()
+ shandle = &serveHandle{handle: handle, nodeID: nodeID}
+ if n := len(c.freeHandle); n > 0 {
+ id = c.freeHandle[n-1]
+ c.freeHandle = c.freeHandle[:n-1]
+ c.handle[id] = shandle
+ } else {
+ id = HandleID(len(c.handle))
+ c.handle = append(c.handle, shandle)
+ }
+
+ // Update mapping from node ID -> set of open Handle IDs.
+ for len(c.nodeHandles) <= int(nodeID) {
+ c.nodeHandles = append(c.nodeHandles, nil)
+ }
+ if c.nodeHandles[nodeID] == nil {
+ c.nodeHandles[nodeID] = make(map[HandleID]bool)
+ }
+ c.nodeHandles[nodeID][id] = true
+
+ c.meta.Unlock()
+ return
+}
+
+func (c *Conn) dropNode(id NodeID) {
+ c.meta.Lock()
+ c.node[id] = nil
+ if len(c.nodeHandles) > int(id) {
+ c.nodeHandles[id] = nil
+ }
+ c.freeNode = append(c.freeNode, id)
+ c.meta.Unlock()
+}
+
+func (c *Conn) dropHandle(id HandleID) {
+ c.meta.Lock()
+ h := c.handle[id]
+ delete(c.nodeHandles[h.nodeID], id)
+ c.handle[id] = nil
+ c.freeHandle = append(c.freeHandle, id)
+ c.meta.Unlock()
+}
+
+func (c *Conn) serve(fs FS, r Request) {
+ intr := make(Intr)
+ req := &serveRequest{Request: r, Intr: intr}
+
+ Debugf("<- %s", req)
+ var node Node
+ var handle Handle
+ var snode *serveNode
+ var shandle *serveHandle
+ c.meta.Lock()
+ hdr := r.Hdr()
+ if id := hdr.Node; id != 0 {
+ if id < NodeID(len(c.node)) {
+ snode = c.node[uint(id)]
+ }
+ if snode == nil {
+ c.meta.Unlock()
+ println("missing node", id, len(c.node), snode)
+ Debugf("-> %#x %v", hdr.ID, ESTALE)
+ r.RespondError(ESTALE)
+ return
+ }
+ node = snode.node
+ }
+ if id := r.handle(); id != 0 {
+ if id < HandleID(len(c.handle)) {
+ shandle = c.handle[uint(id)]
+ }
+ if shandle == nil {
+ println("missing handle", id, len(c.handle), shandle)
+ c.meta.Unlock()
+ Debugf("-> %#x %v", hdr.ID, ESTALE)
+ r.RespondError(ESTALE)
+ return
+ }
+ handle = shandle.handle
+ }
+ intr = make(chan struct{})
+ if c.req[hdr.ID] != nil {
+ // This happens with OSXFUSE. Assume it's okay and
+ // that we'll never see an interrupt for this one.
+ // Otherwise everything wedges. TODO: Report to OSXFUSE?
+ intr = nil
+ } else {
+ c.req[hdr.ID] = req
+ }
+ c.meta.Unlock()
+
+ // Call this before responding.
+ // After responding is too late: we might get another request
+ // with the same ID and be very confused.
+ done := func(resp interface{}) {
+ Debugf("-> %#x %v", hdr.ID, resp)
+ c.meta.Lock()
+ c.req[hdr.ID] = nil
+ c.meta.Unlock()
+ }
+
+ switch r := r.(type) {
+ default:
+ // Note: To FUSE, ENOSYS means "this server never implements this request."
+ // It would be inappropriate to return ENOSYS for other operations in this
+ // switch that might only be unavailable in some contexts, not all.
+ done(ENOSYS)
+ r.RespondError(ENOSYS)
+
+ // FS operations.
+ case *InitRequest:
+ s := &InitResponse{
+ MaxWrite: 4096,
+ }
+ if fs, ok := fs.(interface {
+ Init(*InitRequest, *InitResponse, Intr) Error
+ }); ok {
+ if err := fs.Init(r, s, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ }
+ done(s)
+ r.Respond(s)
+
+ case *StatfsRequest:
+ s := &StatfsResponse{}
+ if fs, ok := fs.(interface {
+ Statfs(*StatfsRequest, *StatfsResponse, Intr) Error
+ }); ok {
+ if err := fs.Statfs(r, s, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ }
+ done(s)
+ r.Respond(s)
+
+ // Node operations.
+ case *GetattrRequest:
+ s := &GetattrResponse{}
+ if n, ok := node.(interface {
+ Getattr(*GetattrRequest, *GetattrResponse, Intr) Error
+ }); ok {
+ if err := n.Getattr(r, s, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ } else {
+ s.AttrValid = 1 * time.Minute
+ s.Attr = snode.attr()
+ }
+ done(s)
+ r.Respond(s)
+
+ case *SetattrRequest:
+ s := &SetattrResponse{}
+
+ // Special-case truncation, if no other bits are set
+ // and the open Handles all have a WriteAll method.
+ if r.Valid&SetattrSize != 0 && r.Size == 0 {
+ type writeAll interface {
+ WriteAll([]byte, Intr) Error
+ }
+ switch r.Valid {
+ case SetattrLockOwner | SetattrSize, SetattrSize:
+ // Seen on Linux. Handle isn't set.
+ c.meta.Lock()
+ for hid := range c.nodeHandles[hdr.Node] {
+ shandle := c.handle[hid]
+ if _, ok := shandle.handle.(writeAll); ok {
+ shandle.trunc = true
+ }
+ }
+ c.meta.Unlock()
+ case SetattrHandle | SetattrSize:
+ // Seen on OS X; the Handle is provided.
+ if _, ok := handle.(writeAll); ok {
+ shandle.trunc = true
+ }
+ }
+ }
+
+ log.Printf("setattr %v", r)
+ if n, ok := node.(interface {
+ Setattr(*SetattrRequest, *SetattrResponse, Intr) Error
+ }); ok {
+ if err := n.Setattr(r, s, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(s)
+ r.Respond(s)
+ break
+ }
+
+ if s.AttrValid == 0 {
+ s.AttrValid = 1 * time.Minute
+ }
+ s.Attr = snode.attr()
+ done(s)
+ r.Respond(s)
+
+ case *SymlinkRequest:
+ s := &SymlinkResponse{}
+ n, ok := node.(interface {
+ Symlink(*SymlinkRequest, Intr) (Node, Error)
+ })
+ if !ok {
+ done(EIO) // XXX or EPERM like Mkdir?
+ r.RespondError(EIO)
+ break
+ }
+ n2, err := n.Symlink(r, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ c.saveLookup(&s.LookupResponse, snode, r.NewName, n2)
+ done(s)
+ r.Respond(s)
+
+ case *ReadlinkRequest:
+ n, ok := node.(interface {
+ Readlink(*ReadlinkRequest, Intr) (string, Error)
+ })
+ if !ok {
+ done(EIO) /// XXX or EPERM?
+ r.RespondError(EIO)
+ break
+ }
+ target, err := n.Readlink(r, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(target)
+ r.Respond(target)
+
+ case *LinkRequest:
+ n, ok := node.(interface {
+ Link(r *LinkRequest, old Node, intr Intr) (Node, Error)
+ })
+ if !ok {
+ log.Printf("Node %T doesn't implement fuse Link", node)
+ done(EIO) /// XXX or EPERM?
+ r.RespondError(EIO)
+ break
+ }
+ c.meta.Lock()
+ var oldNode *serveNode
+ if int(r.OldNode) < len(c.node) {
+ oldNode = c.node[r.OldNode]
+ }
+ c.meta.Unlock()
+ if oldNode == nil {
+ log.Printf("In LinkRequest, node %d not found", r.OldNode)
+ done(EIO)
+ r.RespondError(EIO)
+ break
+ }
+ n2, err := n.Link(r, oldNode.node, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ s := &LookupResponse{}
+ c.saveLookup(s, snode, r.NewName, n2)
+ done(s)
+ r.Respond(s)
+
+ case *RemoveRequest:
+ n, ok := node.(interface {
+ Remove(*RemoveRequest, Intr) Error
+ })
+ if !ok {
+ done(EIO) /// XXX or EPERM?
+ r.RespondError(EIO)
+ break
+ }
+ err := n.Remove(r, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(nil)
+ r.Respond()
+
+ case *AccessRequest:
+ if n, ok := node.(interface {
+ Access(*AccessRequest, Intr) Error
+ }); ok {
+ if err := n.Access(r, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ }
+ done(r)
+ r.Respond()
+
+ case *LookupRequest:
+ var n2 Node
+ var err Error
+ s := &LookupResponse{}
+ if n, ok := node.(interface {
+ Lookup(string, Intr) (Node, Error)
+ }); ok {
+ n2, err = n.Lookup(r.Name, intr)
+ } else if n, ok := node.(interface {
+ Lookup(*LookupRequest, *LookupResponse, Intr) (Node, Error)
+ }); ok {
+ n2, err = n.Lookup(r, s, intr)
+ } else {
+ done(ENOENT)
+ r.RespondError(ENOENT)
+ break
+ }
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ c.saveLookup(s, snode, r.Name, n2)
+ done(s)
+ r.Respond(s)
+
+ case *MkdirRequest:
+ s := &MkdirResponse{}
+ n, ok := node.(interface {
+ Mkdir(*MkdirRequest, Intr) (Node, Error)
+ })
+ if !ok {
+ done(EPERM)
+ r.RespondError(EPERM)
+ break
+ }
+ n2, err := n.Mkdir(r, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ c.saveLookup(&s.LookupResponse, snode, r.Name, n2)
+ done(s)
+ r.Respond(s)
+
+ case *OpenRequest:
+ s := &OpenResponse{Flags: OpenDirectIO}
+ var h2 Handle
+ if n, ok := node.(interface {
+ Open(*OpenRequest, *OpenResponse, Intr) (Handle, Error)
+ }); ok {
+ hh, err := n.Open(r, s, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ h2 = hh
+ } else {
+ h2 = node
+ }
+ s.Handle, _ = c.saveHandle(h2, hdr.Node)
+ done(s)
+ r.Respond(s)
+
+ case *CreateRequest:
+ n, ok := node.(interface {
+ Create(*CreateRequest, *CreateResponse, Intr) (Node, Handle, Error)
+ })
+ if !ok {
+ // If we send back ENOSYS, FUSE will try mknod+open.
+ done(EPERM)
+ r.RespondError(EPERM)
+ break
+ }
+ s := &CreateResponse{OpenResponse: OpenResponse{Flags: OpenDirectIO}}
+ n2, h2, err := n.Create(r, s, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ c.saveLookup(&s.LookupResponse, snode, r.Name, n2)
+ h, shandle := c.saveHandle(h2, hdr.Node)
+ s.Handle = h
+ shandle.trunc = true
+ done(s)
+ r.Respond(s)
+
+ case *GetxattrRequest, *SetxattrRequest, *ListxattrRequest, *RemovexattrRequest:
+ // TODO: Use n.
+ done(ENOSYS)
+ r.RespondError(ENOSYS)
+
+ case *ForgetRequest:
+ n, ok := node.(interface {
+ Forget()
+ })
+ if ok {
+ n.Forget()
+ }
+ c.dropNode(hdr.Node)
+ done(r)
+ r.Respond()
+
+ // Handle operations.
+ case *ReadRequest:
+ s := &ReadResponse{Data: make([]byte, 0, r.Size)}
+ if snode.isDir {
+ if h, ok := handle.(interface {
+ ReadDir(Intr) ([]Dirent, Error)
+ }); ok {
+ if shandle.readData == nil {
+ attr := snode.attr()
+ dirs, err := h.ReadDir(intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ var data []byte
+ data = AppendDirent(data, Dirent{Inode: attr.Inode, Name: "."})
+ data = AppendDirent(data, Dirent{Inode: attr.Inode, Name: ".."})
+ for _, dir := range dirs {
+ if dir.Inode == 0 {
+ dir.Inode = hash(path.Join(snode.name, dir.Name))
+ }
+ data = AppendDirent(data, dir)
+ }
+ shandle.readData = data
+ }
+ HandleRead(r, s, shandle.readData)
+ done(s)
+ r.Respond(s)
+ break
+ }
+ } else {
+ if h, ok := handle.(interface {
+ ReadAll(Intr) ([]byte, Error)
+ }); ok {
+ if shandle.readData == nil {
+ data, err := h.ReadAll(intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ if data == nil {
+ data = []byte{}
+ }
+ shandle.readData = data
+ }
+ HandleRead(r, s, shandle.readData)
+ done(s)
+ r.Respond(s)
+ break
+ }
+ }
+ h, ok := handle.(interface {
+ Read(*ReadRequest, *ReadResponse, Intr) Error
+ })
+ if !ok {
+ fmt.Printf("NO READ FOR %T\n", handle)
+ done(EIO)
+ r.RespondError(EIO)
+ break
+ }
+ if err := h.Read(r, s, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(s)
+ r.Respond(s)
+
+ case *WriteRequest:
+ s := &WriteResponse{}
+ if shandle.trunc && r.Offset == int64(len(shandle.writeData)) {
+ shandle.writeData = append(shandle.writeData, r.Data...)
+ s.Size = len(r.Data)
+ done(s)
+ r.Respond(s)
+ break
+ }
+ if h, ok := handle.(interface {
+ Write(*WriteRequest, *WriteResponse, Intr) Error
+ }); ok {
+ if err := h.Write(r, s, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(s)
+ r.Respond(s)
+ break
+ }
+ println("NO WRITE")
+ done(EIO)
+ r.RespondError(EIO)
+
+ case *FlushRequest:
+ if shandle.trunc {
+ h := handle.(interface {
+ WriteAll([]byte, Intr) Error
+ })
+ if err := h.WriteAll(shandle.writeData, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ shandle.writeData = nil
+ shandle.trunc = false
+ }
+ if h, ok := handle.(interface {
+ Flush(*FlushRequest, Intr) Error
+ }); ok {
+ if err := h.Flush(r, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ }
+ done(nil)
+ r.Respond()
+
+ case *ReleaseRequest:
+ // No matter what, release the handle.
+ c.dropHandle(r.handle())
+ if h, ok := handle.(interface {
+ Release(*ReleaseRequest, Intr) Error
+ }); ok {
+ if err := h.Release(r, intr); err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ }
+ done(nil)
+ r.Respond()
+
+ case *DestroyRequest:
+ fs, ok := fs.(interface {
+ Destroy()
+ })
+ if ok {
+ fs.Destroy()
+ }
+ done(nil)
+ r.Respond()
+
+ case *RenameRequest:
+ c.meta.Lock()
+ var newDirNode *serveNode
+ if int(r.NewDir) < len(c.node) {
+ newDirNode = c.node[r.NewDir]
+ }
+ c.meta.Unlock()
+ if newDirNode == nil {
+ println("RENAME NEW DIR NODE NOT FOUND")
+ done(EIO)
+ r.RespondError(EIO)
+ break
+ }
+ n, ok := node.(interface {
+ Rename(r *RenameRequest, newDir Node, intr Intr) Error
+ })
+ if !ok {
+ log.Printf("Node %T missing Rename method", node)
+ done(EIO) // XXX or EPERM like Mkdir?
+ r.RespondError(EIO)
+ break
+ }
+ err := n.Rename(r, newDirNode.node, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(nil)
+ r.Respond()
+
+ case *MknodRequest:
+ n, ok := node.(interface {
+ Mknod(r *MknodRequest, intr Intr) (Node, Error)
+ })
+ if !ok {
+ log.Printf("Node %T missing Mknod method", node)
+ done(EIO)
+ r.RespondError(EIO)
+ break
+ }
+ n2, err := n.Mknod(r, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ s := &LookupResponse{}
+ c.saveLookup(s, snode, r.Name, n2)
+ done(s)
+ r.Respond(s)
+
+ case *FsyncRequest:
+ n, ok := node.(interface {
+ Fsync(r *FsyncRequest, intr Intr) Error
+ })
+ if !ok {
+ log.Printf("Node %T missing Fsync method", node)
+ done(EIO)
+ r.RespondError(EIO)
+ break
+ }
+ err := n.Fsync(r, intr)
+ if err != nil {
+ done(err)
+ r.RespondError(err)
+ break
+ }
+ done(nil)
+ r.Respond()
+
+ /* case *FsyncdirRequest:
+ done(ENOSYS)
+ r.RespondError(ENOSYS)
+
+ case *GetlkRequest, *SetlkRequest, *SetlkwRequest:
+ done(ENOSYS)
+ r.RespondError(ENOSYS)
+
+ // One of a kind.
+ case *InterruptRequest:
+ c.meta.Lock()
+ ireq := c.req[r.OldID]
+ if ireq != nil && ireq.Intr != nil {
+ close(ireq.Intr)
+ ireq.Intr = nil
+ }
+ c.meta.Unlock()
+ done(nil)
+ r.Respond()
+
+ case *BmapRequest:
+ done(ENOSYS)
+ r.RespondError(ENOSYS)
+
+ case *SetvolnameRequest, *GetxtimesRequest, *ExchangeRequest:
+ done(ENOSYS)
+ r.RespondError(ENOSYS)
+ */
+ }
+}
+
+func (c *Conn) saveLookup(s *LookupResponse, snode *serveNode, elem string, n2 Node) {
+ name := path.Join(snode.name, elem)
+ var sn *serveNode
+ s.Node, s.Generation, sn = c.saveNode(name, n2)
+ if s.EntryValid == 0 {
+ s.EntryValid = 1 * time.Minute
+ }
+ if s.AttrValid == 0 {
+ s.AttrValid = 1 * time.Minute
+ }
+ s.Attr = sn.attr()
+}
+
+// HandleRead handles a read request assuming that data is the entire file content.
+// It adjusts the amount returned in resp according to req.Offset and req.Size.
+func HandleRead(req *ReadRequest, resp *ReadResponse, data []byte) {
+ if req.Offset >= int64(len(data)) {
+ data = nil
+ } else {
+ data = data[req.Offset:]
+ }
+ if len(data) > req.Size {
+ data = data[:req.Size]
+ }
+ n := copy(resp.Data[:req.Size], data)
+ resp.Data = resp.Data[:n]
+}
+
+// DataHandle returns a read-only Handle that satisfies reads
+// using the given data.
+func DataHandle(data []byte) Handle {
+ return &dataHandle{data}
+}
+
+type dataHandle struct {
+ data []byte
+}
+
+func (d *dataHandle) Read(intr Intr) ([]byte, Error) {
+ return d.data, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/fuse/tree.go b/vendor/github.com/mattermost/rsc/fuse/tree.go
new file mode 100644
index 000000000..fec0a748f
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/fuse/tree.go
@@ -0,0 +1,93 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// FUSE directory tree, for servers that wish to use it with the service loop.
+
+package fuse
+
+import (
+ "os"
+ pathpkg "path"
+ "strings"
+)
+
+// A Tree implements a basic directory tree for FUSE.
+type Tree struct {
+ tree
+}
+
+func (t *Tree) Root() (Node, Error) {
+ return &t.tree, nil
+}
+
+// Add adds the path to the tree, resolving to the given node.
+// If path or a prefix of path has already been added to the tree,
+// Add panics.
+func (t *Tree) Add(path string, node Node) {
+ path = pathpkg.Clean("/" + path)[1:]
+ elems := strings.Split(path, "/")
+ dir := Node(&t.tree)
+ for i, elem := range elems {
+ dt, ok := dir.(*tree)
+ if !ok {
+ panic("fuse: Tree.Add for " + strings.Join(elems[:i], "/") + " and " + path)
+ }
+ n := dt.lookup(elem)
+ if n != nil {
+ if i+1 == len(elems) {
+ panic("fuse: Tree.Add for " + path + " conflicts with " + elem)
+ }
+ dir = n
+ } else {
+ if i+1 == len(elems) {
+ dt.add(elem, node)
+ } else {
+ dir = &tree{}
+ dt.add(elem, dir)
+ }
+ }
+ }
+}
+
+type treeDir struct {
+ name string
+ node Node
+}
+
+type tree struct {
+ dir []treeDir
+}
+
+func (t *tree) lookup(name string) Node {
+ for _, d := range t.dir {
+ if d.name == name {
+ return d.node
+ }
+ }
+ return nil
+}
+
+func (t *tree) add(name string, n Node) {
+ t.dir = append(t.dir, treeDir{name, n})
+}
+
+func (t *tree) Attr() Attr {
+ return Attr{Mode: os.ModeDir | 0555}
+}
+
+func (t *tree) Lookup(name string, intr Intr) (Node, Error) {
+ n := t.lookup(name)
+ if n != nil {
+ return n, nil
+ }
+ return nil, ENOENT
+}
+
+func (t *tree) ReadDir(intr Intr) ([]Dirent, Error) {
+ var out []Dirent
+ for _, d := range t.dir {
+ out = append(out, Dirent{Name: d.name})
+ }
+ return out, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/gf256/blog_test.go b/vendor/github.com/mattermost/rsc/gf256/blog_test.go
new file mode 100644
index 000000000..12cc7deb0
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/gf256/blog_test.go
@@ -0,0 +1,85 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains a straightforward implementation of
+// Reed-Solomon encoding, along with a benchmark.
+// It goes with http://research.swtch.com/field.
+//
+// For an optimized implementation, see gf256.go.
+
+package gf256
+
+import (
+ "bytes"
+ "fmt"
+ "testing"
+)
+
+// BlogECC writes to check the error correcting code bytes
+// for data using the given Reed-Solomon parameters.
+func BlogECC(rs *RSEncoder, m []byte, check []byte) {
+ if len(check) < rs.c {
+ panic("gf256: invalid check byte length")
+ }
+ if rs.c == 0 {
+ return
+ }
+
+ // The check bytes are the remainder after dividing
+ // data padded with c zeros by the generator polynomial.
+
+ // p = data padded with c zeros.
+ var p []byte
+ n := len(m) + rs.c
+ if len(rs.p) >= n {
+ p = rs.p
+ } else {
+ p = make([]byte, n)
+ }
+ copy(p, m)
+ for i := len(m); i < len(p); i++ {
+ p[i] = 0
+ }
+
+ gen := rs.gen
+
+ // Divide p by gen, leaving the remainder in p[len(data):].
+ // p[0] is the most significant term in p, and
+ // gen[0] is the most significant term in the generator.
+ for i := 0; i < len(m); i++ {
+ k := f.Mul(p[i], f.Inv(gen[0])) // k = pi / g0
+ // p -= k·g
+ for j, g := range gen {
+ p[i+j] = f.Add(p[i+j], f.Mul(k, g))
+ }
+ }
+
+ copy(check, p[len(m):])
+ rs.p = p
+}
+
+func BenchmarkBlogECC(b *testing.B) {
+ data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
+ check := []byte{0x29, 0x41, 0xb3, 0x93, 0x8, 0xe8, 0xa3, 0xe7, 0x63, 0x8f}
+ out := make([]byte, len(check))
+ rs := NewRSEncoder(f, len(check))
+ for i := 0; i < b.N; i++ {
+ BlogECC(rs, data, out)
+ }
+ b.SetBytes(int64(len(data)))
+ if !bytes.Equal(out, check) {
+ fmt.Printf("have %#v want %#v\n", out, check)
+ }
+}
+
+func TestBlogECC(t *testing.T) {
+ data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
+ check := []byte{0xa5, 0x24, 0xd4, 0xc1, 0xed, 0x36, 0xc7, 0x87, 0x2c, 0x55}
+ out := make([]byte, len(check))
+ rs := NewRSEncoder(f, len(check))
+ BlogECC(rs, data, out)
+ if !bytes.Equal(out, check) {
+ t.Errorf("have %x want %x", out, check)
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/gf256/gf256_test.go b/vendor/github.com/mattermost/rsc/gf256/gf256_test.go
new file mode 100644
index 000000000..f77fa7d67
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/gf256/gf256_test.go
@@ -0,0 +1,194 @@
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package gf256
+
+import (
+ "bytes"
+ "fmt"
+ "testing"
+)
+
+var f = NewField(0x11d, 2) // x^8 + x^4 + x^3 + x^2 + 1
+
+func TestBasic(t *testing.T) {
+ if f.Exp(0) != 1 || f.Exp(1) != 2 || f.Exp(255) != 1 {
+ panic("bad Exp")
+ }
+}
+
+func TestECC(t *testing.T) {
+ data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
+ check := []byte{0xa5, 0x24, 0xd4, 0xc1, 0xed, 0x36, 0xc7, 0x87, 0x2c, 0x55}
+ out := make([]byte, len(check))
+ rs := NewRSEncoder(f, len(check))
+ rs.ECC(data, out)
+ if !bytes.Equal(out, check) {
+ t.Errorf("have %x want %x", out, check)
+ }
+}
+
+func TestLinear(t *testing.T) {
+ d1 := []byte{0x00, 0x00}
+ c1 := []byte{0x00, 0x00}
+ out := make([]byte, len(c1))
+ rs := NewRSEncoder(f, len(c1))
+ if rs.ECC(d1, out); !bytes.Equal(out, c1) {
+ t.Errorf("ECBytes(%x, %d) = %x, want 0", d1, len(c1), out)
+ }
+ d2 := []byte{0x00, 0x01}
+ c2 := make([]byte, 2)
+ rs.ECC(d2, c2)
+ d3 := []byte{0x00, 0x02}
+ c3 := make([]byte, 2)
+ rs.ECC(d3, c3)
+ cx := make([]byte, 2)
+ for i := range cx {
+ cx[i] = c2[i] ^ c3[i]
+ }
+ d4 := []byte{0x00, 0x03}
+ c4 := make([]byte, 2)
+ rs.ECC(d4, c4)
+ if !bytes.Equal(cx, c4) {
+ t.Errorf("ECBytes(%x, 2) = %x\nECBytes(%x, 2) = %x\nxor = %x\nECBytes(%x, 2) = %x",
+ d2, c2, d3, c3, cx, d4, c4)
+ }
+}
+
+func TestGaussJordan(t *testing.T) {
+ rs := NewRSEncoder(f, 2)
+ m := make([][]byte, 16)
+ for i := range m {
+ m[i] = make([]byte, 4)
+ m[i][i/8] = 1 << uint(i%8)
+ rs.ECC(m[i][:2], m[i][2:])
+ }
+ if false {
+ fmt.Printf("---\n")
+ for _, row := range m {
+ fmt.Printf("%x\n", row)
+ }
+ }
+ b := []uint{0, 1, 2, 3, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, 26, 27}
+ for i := 0; i < 16; i++ {
+ bi := b[i]
+ if m[i][bi/8]&(1<<(7-bi%8)) == 0 {
+ for j := i + 1; ; j++ {
+ if j >= len(m) {
+ t.Errorf("lost track for %d", bi)
+ break
+ }
+ if m[j][bi/8]&(1<<(7-bi%8)) != 0 {
+ m[i], m[j] = m[j], m[i]
+ break
+ }
+ }
+ }
+ for j := i + 1; j < len(m); j++ {
+ if m[j][bi/8]&(1<<(7-bi%8)) != 0 {
+ for k := range m[j] {
+ m[j][k] ^= m[i][k]
+ }
+ }
+ }
+ }
+ if false {
+ fmt.Printf("---\n")
+ for _, row := range m {
+ fmt.Printf("%x\n", row)
+ }
+ }
+ for i := 15; i >= 0; i-- {
+ bi := b[i]
+ for j := i - 1; j >= 0; j-- {
+ if m[j][bi/8]&(1<<(7-bi%8)) != 0 {
+ for k := range m[j] {
+ m[j][k] ^= m[i][k]
+ }
+ }
+ }
+ }
+ if false {
+ fmt.Printf("---\n")
+ for _, row := range m {
+ fmt.Printf("%x", row)
+ out := make([]byte, 2)
+ if rs.ECC(row[:2], out); !bytes.Equal(out, row[2:]) {
+ fmt.Printf(" - want %x", out)
+ }
+ fmt.Printf("\n")
+ }
+ }
+}
+
+func BenchmarkECC(b *testing.B) {
+ data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
+ check := []byte{0x29, 0x41, 0xb3, 0x93, 0x8, 0xe8, 0xa3, 0xe7, 0x63, 0x8f}
+ out := make([]byte, len(check))
+ rs := NewRSEncoder(f, len(check))
+ for i := 0; i < b.N; i++ {
+ rs.ECC(data, out)
+ }
+ b.SetBytes(int64(len(data)))
+ if !bytes.Equal(out, check) {
+ fmt.Printf("have %#v want %#v\n", out, check)
+ }
+}
+
+func TestGen(t *testing.T) {
+ for i := 0; i < 256; i++ {
+ _, lg := f.gen(i)
+ if lg[0] != 0 {
+ t.Errorf("#%d: %x", i, lg)
+ }
+ }
+}
+
+func TestReducible(t *testing.T) {
+ var count = []int{1, 2, 3, 6, 9, 18, 30, 56, 99, 186} // oeis.org/A1037
+ for i, want := range count {
+ n := 0
+ for p := 1 << uint(i+2); p < 1<<uint(i+3); p++ {
+ if !reducible(p) {
+ n++
+ }
+ }
+ if n != want {
+ t.Errorf("#reducible(%d-bit) = %d, want %d", i+2, n, want)
+ }
+ }
+}
+
+func TestExhaustive(t *testing.T) {
+ for poly := 0x100; poly < 0x200; poly++ {
+ if reducible(poly) {
+ continue
+ }
+ α := 2
+ for !generates(α, poly) {
+ α++
+ }
+ f := NewField(poly, α)
+ for p := 0; p < 256; p++ {
+ for q := 0; q < 256; q++ {
+ fm := int(f.Mul(byte(p), byte(q)))
+ pm := mul(p, q, poly)
+ if fm != pm {
+ t.Errorf("NewField(%#x).Mul(%#x, %#x) = %#x, want %#x", poly, p, q, fm, pm)
+ }
+ }
+ }
+ }
+}
+
+func generates(α, poly int) bool {
+ x := α
+ for i := 0; i < 254; i++ {
+ if x == 1 {
+ return false
+ }
+ x = mul(x, α, poly)
+ }
+ return true
+}
diff --git a/vendor/github.com/mattermost/rsc/google/acme/Chat/main.go b/vendor/github.com/mattermost/rsc/google/acme/Chat/main.go
new file mode 100644
index 000000000..a161d8f10
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/acme/Chat/main.go
@@ -0,0 +1,575 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+/*
+TODO:
+ - Del of main window should move to other window.
+ - Editing main window should update status on \n or something like that.
+ - Make use of full names from roster
+*/
+
+import (
+ "bytes"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+ "regexp"
+ "strings"
+ "time"
+
+ "code.google.com/p/goplan9/plan9/acme"
+ "github.com/mattermost/rsc/google"
+ "github.com/mattermost/rsc/xmpp"
+)
+
+var acmeDebug = flag.Bool("acmedebug", false, "print acme debugging")
+
+type Window struct {
+ *acme.Win // acme window
+ *acme.Event // most recent event received
+ err error // error reading event
+ typ string // kind of window "main", "chat"
+ name string // acme window title
+ remote string // for typ=="chat", remote address
+ dirty bool // window is dirty
+ blinky bool // window's dirty box is blinking
+
+ lastTime time.Time
+}
+
+type Msg struct {
+ w *Window // window where message belongs
+ *xmpp.Chat // recently received chat
+ err error // error reading chat message
+}
+
+var (
+ client *xmpp.Client // current xmpp client (can reconnect)
+ acct google.Account // google acct info
+ statusCache = make(map[string][]*xmpp.Presence)
+ active = make(map[string]*Window) // active windows
+ acmeChan = make(chan *Window) // acme events
+ msgChan = make(chan *Msg) // chat events
+ mainWin *Window
+ status = xmpp.Available
+ statusMsg = ""
+ lastActivity time.Time
+)
+
+const (
+ awayTime = 10 * time.Minute
+ extendedAwayTime = 30 * time.Minute
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: Chat [-a acct] name...\n")
+ flag.PrintDefaults()
+ os.Exit(2)
+}
+
+var acctName = flag.String("a", "", "account to use")
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+
+ acct = google.Acct(*acctName)
+
+ aw, err := acme.New()
+ if err != nil {
+ log.Fatal(err)
+ }
+ aw.Name("Chat/" + acct.Nick + "/")
+
+ client, err = xmpp.NewClient("talk.google.com:443", acct.Email, acct.Password)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ w := &Window{Win: aw, typ: "main", name: "Chat/" + acct.Nick + "/"}
+ data, err := ioutil.ReadFile(google.Dir() + "/chat." + acct.Nick)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err == nil {
+ w.Write("body", data)
+ }
+ mainWin = w
+ active[w.name] = w
+ go w.readAcme()
+ client.Roster()
+ setStatus(status)
+ go w.readChat()
+ lastActivity = time.Now()
+
+ tick := time.Tick(0.5e9)
+Loop:
+ for len(active) > 0 {
+ select {
+ case w := <-acmeChan:
+ if w == nil {
+ // Sync with reader.
+ continue
+ }
+ if w.err != nil {
+ if active[w.name] == nil {
+ continue
+ }
+ log.Fatal(w.err)
+ }
+ if *acmeDebug {
+ fmt.Fprintf(os.Stderr, "%s %c%c %d,%d %q\n", w.name, w.C1, w.C2, w.Q0, w.Q1, w.Text)
+ }
+ if w.C1 == 'M' || w.C1 == 'K' {
+ lastActivity = time.Now()
+ if status != xmpp.Available {
+ setStatus(xmpp.Available)
+ }
+ }
+ if (w.C2 == 'x' || w.C2 == 'X') && string(w.Text) == "Del" {
+ // TODO: Hangup connection for w.typ == "acct"?
+ delete(active, w.name)
+ w.Del(true)
+ continue Loop
+ }
+
+ switch w.typ {
+ case "main":
+ switch w.C2 {
+ case 'L': // Button 3 in body: load chat window for contact.
+ w.expand()
+ fallthrough
+ case 'l': // Button 3 in tag
+ arg := string(w.Text)
+ showContact(arg)
+ continue Loop
+ }
+ case "chat":
+ if w.C1 == 'F' && w.C2 == 'I' {
+ continue Loop
+ }
+ if w.C1 != 'M' && w.C1 != 'K' {
+ break
+ }
+ if w.blinky {
+ w.blinky = false
+ w.Fprintf("ctl", "dirty\n")
+ }
+ switch w.C2 {
+ case 'X', 'x':
+ if string(w.Text) == "Ack" {
+ w.Fprintf("ctl", "clean\n")
+ }
+ case 'I':
+ w.sendMsg()
+ continue Loop
+ }
+ }
+ w.WriteEvent(w.Event)
+
+ case msg := <-msgChan:
+ w := msg.w
+ if msg.err != nil {
+ w.Fprintf("body", "ERROR: %s\n", msg.err)
+ continue Loop
+ }
+ you := msg.Remote
+ if i := strings.Index(you, "/"); i >= 0 {
+ you = you[:i]
+ }
+ switch msg.Type {
+ case "chat":
+ w := showContact(you)
+ text := strings.TrimSpace(msg.Text)
+ if text == "" {
+ // Probably a composing notification.
+ continue
+ }
+ w.message("> %s\n", text)
+ w.blinky = true
+ w.dirty = true
+
+ case "presence":
+ pr := msg.Presence
+ pr, new := savePresence(pr, you)
+ if !new {
+ continue
+ }
+ w := lookContact(you)
+ if w != nil {
+ w.status(pr)
+ }
+ mainStatus(pr, you)
+ }
+
+ case t := <-tick:
+ switch status {
+ case xmpp.Available:
+ if t.Sub(lastActivity) > awayTime {
+ setStatus(xmpp.Away)
+ }
+ case xmpp.Away:
+ if t.Sub(lastActivity) > extendedAwayTime {
+ setStatus(xmpp.ExtendedAway)
+ }
+ }
+ for _, w := range active {
+ if w.blinky {
+ w.dirty = !w.dirty
+ if w.dirty {
+ w.Fprintf("ctl", "dirty\n")
+ } else {
+ w.Fprintf("ctl", "clean\n")
+ }
+ }
+ }
+ }
+ }
+}
+
+func setStatus(st xmpp.Status) {
+ status = st
+ client.Status(status, statusMsg)
+ mainWin.statusTag(status, statusMsg)
+}
+
+func savePresence(pr *xmpp.Presence, you string) (pr1 *xmpp.Presence, new bool) {
+ old := cachedPresence(you)
+
+ pr.StatusMsg = strings.TrimSpace(pr.StatusMsg)
+ c := statusCache[you]
+ for i, p := range c {
+ if p.Remote == pr.Remote {
+ c[i] = pr
+ c[0], c[i] = c[i], c[0]
+ goto Best
+ }
+ }
+ c = append(c, pr)
+ c[0], c[len(c)-1] = c[len(c)-1], c[0]
+ statusCache[you] = c
+
+Best:
+ best := cachedPresence(you)
+ return best, old == nil || old.Status != best.Status || old.StatusMsg != best.StatusMsg
+}
+
+func cachedPresence(you string) *xmpp.Presence {
+ c := statusCache[you]
+ if len(c) == 0 {
+ return nil
+ }
+ best := c[0]
+ for _, p := range c {
+ if p.Status > best.Status {
+ best = p
+ }
+ }
+ return best
+}
+
+func short(st xmpp.Status) string {
+ switch st {
+ case xmpp.Unavailable:
+ return "?"
+ case xmpp.ExtendedAway:
+ return "x"
+ case xmpp.Away:
+ return "-"
+ case xmpp.Available:
+ return "+"
+ case xmpp.DoNotDisturb:
+ return "!"
+ }
+ return st.String()
+}
+
+func long(st xmpp.Status) string {
+ switch st {
+ case xmpp.Unavailable:
+ return "unavailable"
+ case xmpp.ExtendedAway:
+ return "offline"
+ case xmpp.Away:
+ return "away"
+ case xmpp.Available:
+ return "available"
+ case xmpp.DoNotDisturb:
+ return "busy"
+ }
+ return st.String()
+}
+
+func (w *Window) time() string {
+ /*
+ Auto-date chat windows:
+
+ Show date and time on first message.
+ Show time if minute is different from last message.
+ Show date if day is different from last message.
+
+ Oct 10 12:01 > hi
+ 12:03 hello there
+ 12:05 > what's up?
+
+ 12:10 [Away]
+ */
+ now := time.Now()
+ m1, d1, y1 := w.lastTime.Date()
+ m2, d2, y2 := now.Date()
+ w.lastTime = now
+
+ if m1 != m2 || d1 != d2 || y1 != y2 {
+ return now.Format("Jan 2 15:04 ")
+ }
+ return now.Format("15:04 ")
+}
+
+func (w *Window) status(pr *xmpp.Presence) {
+ msg := ""
+ if pr.StatusMsg != "" {
+ msg = ": " + pr.StatusMsg
+ }
+ w.message("[%s%s]\n", long(pr.Status), msg)
+
+ w.statusTag(pr.Status, pr.StatusMsg)
+}
+
+func (w *Window) statusTag(status xmpp.Status, statusMsg string) {
+ data, err := w.ReadAll("tag")
+ if err != nil {
+ log.Printf("read tag: %v", err)
+ return
+ }
+ //log.Printf("tag1: %s\n", data)
+ i := bytes.IndexByte(data, '|')
+ if i >= 0 {
+ data = data[i+1:]
+ } else {
+ data = nil
+ }
+ //log.Printf("tag2: %s\n", data)
+ j := bytes.IndexByte(data, '|')
+ if j >= 0 {
+ data = data[j+1:]
+ }
+ //log.Printf("tag3: %s\n", data)
+
+ msg := ""
+ if statusMsg != "" {
+ msg = " " + statusMsg
+ }
+ w.Ctl("cleartag\n")
+ w.Write("tag", []byte(" "+short(status)+msg+" |"+string(data)))
+}
+
+func mainStatus(pr *xmpp.Presence, you string) {
+ w := mainWin
+ if err := w.Addr("#0/^(.[ \t]+)?" + regexp.QuoteMeta(you) + "([ \t]*|$)/"); err != nil {
+ return
+ }
+ q0, q1, err := w.ReadAddr()
+ if err != nil {
+ log.Printf("ReadAddr: %s\n", err)
+ return
+ }
+ if err := w.Addr("#%d/"+regexp.QuoteMeta(you)+"/", q0); err != nil {
+ log.Printf("Addr2: %s\n", err)
+ }
+ q2, q3, err := w.ReadAddr()
+ if err != nil {
+ log.Printf("ReadAddr2: %s\n", err)
+ return
+ }
+
+ space := " "
+ if q1 > q3 || pr.StatusMsg == "" { // already have or don't need space
+ space = ""
+ }
+ if err := w.Addr("#%d/.*/", q1); err != nil {
+ log.Printf("Addr3: %s\n", err)
+ }
+ w.Fprintf("data", "%s%s", space, pr.StatusMsg)
+
+ space = ""
+ if q0 == q2 {
+ w.Addr("#%d,#%d", q0, q0)
+ space = " "
+ } else {
+ w.Addr("#%d,#%d", q0, q0+1)
+ }
+ w.Fprintf("data", "%s%s", short(pr.Status), space)
+}
+
+func (w *Window) expand() {
+ // Use selection if any.
+ w.Fprintf("ctl", "addr=dot\n")
+ q0, q1, err := w.ReadAddr()
+ if err == nil && q0 <= w.Q0 && w.Q0 <= q1 {
+ goto Read
+ }
+ if err = w.Addr("#%d-/[a-zA-Z0-9_@.\\-]*/,#%d+/[a-zA-Z0-9_@.\\-]*/", w.Q0, w.Q1); err != nil {
+ log.Printf("expand: %v", err)
+ return
+ }
+ q0, q1, err = w.ReadAddr()
+ if err != nil {
+ log.Printf("expand: %v", err)
+ return
+ }
+
+Read:
+ data, err := w.ReadAll("xdata")
+ if err != nil {
+ log.Printf("read: %v", err)
+ return
+ }
+ w.Text = data
+ w.Q0 = q0
+ w.Q1 = q1
+ return
+}
+
+// Invariant: in chat windows, the acme addr corresponds to the
+// empty string just before the input being typed. Text before addr
+// is the chat history (usually ending in a blank line).
+
+func (w *Window) message(format string, args ...interface{}) {
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("message; addr=%d,%d", q0, q1)
+ }
+ if err := w.Addr(".-/\\n?\\n?/"); err != nil && *acmeDebug {
+ log.Printf("set addr: %s", err)
+ }
+ q0, _, _ := w.ReadAddr()
+ nl := ""
+ if q0 > 0 {
+ nl = "\n"
+ }
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("inserting; addr=%d,%d", q0, q1)
+ }
+ w.Fprintf("data", nl+w.time()+format+"\n", args...)
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("wrote; addr=%d,%d", q0, q1)
+ }
+}
+
+func (w *Window) sendMsg() {
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("sendMsg; addr=%d,%d", q0, q1)
+ }
+ if err := w.Addr(`.,./(.|\n)*\n/`); err != nil {
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("no text (%s); addr=%d,%d", err, q0, q1)
+ }
+ return
+ }
+ q0, q1, _ := w.ReadAddr()
+ if *acmeDebug {
+ log.Printf("found msg; addr=%d,%d", q0, q1)
+ }
+ line, _ := w.ReadAll("xdata")
+ trim := string(bytes.TrimSpace(line))
+ if len(trim) > 0 {
+ err := client.Send(xmpp.Chat{Remote: w.remote, Type: "chat", Text: trim})
+
+ // Select blank line before input (if any) and input.
+ w.Addr("#%d-/\\n?\\n?/,#%d", q0, q1)
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("selected text; addr=%d,%d", q0, q1)
+ }
+ q0, _, _ := w.ReadAddr()
+
+ // Overwrite with \nmsg\n\n.
+ // Leaves addr after final \n, which is where we want it.
+ nl := ""
+ if q0 > 0 {
+ nl = "\n"
+ }
+ errstr := ""
+ if err != nil {
+ errstr = fmt.Sprintf("\n%s", errstr)
+ }
+ w.Fprintf("data", "%s%s%s%s\n\n", nl, w.time(), trim, errstr)
+ if *acmeDebug {
+ q0, q1, _ := w.ReadAddr()
+ log.Printf("wrote; addr=%d,%d", q0, q1)
+ }
+ w.Fprintf("ctl", "clean\n")
+ }
+}
+
+func (w *Window) readAcme() {
+ for {
+ e, err := w.ReadEvent()
+ if err != nil {
+ w.err = err
+ acmeChan <- w
+ break
+ }
+ //fmt.Printf("%c%c %d,%d %d,%d %#x %#q %#q %#q\n", e.C1, e.C2, e.Q0, e.Q1, e.OrigQ0, e.OrigQ1, e.Flag, e.Text, e.Arg, e.Loc)
+ w.Event = e
+ acmeChan <- w
+ acmeChan <- nil
+ }
+}
+
+func (w *Window) readChat() {
+ for {
+ msg, err := client.Recv()
+ if err != nil {
+ msgChan <- &Msg{w: w, err: err}
+ break
+ }
+ //fmt.Printf("%s\n", *msg)
+ msgChan <- &Msg{w: w, Chat: &msg}
+ }
+}
+
+func lookContact(you string) *Window {
+ return active["Chat/"+acct.Nick+"/"+you]
+}
+
+func showContact(you string) *Window {
+ w := lookContact(you)
+ if w != nil {
+ w.Ctl("show\n")
+ return w
+ }
+
+ ww, err := acme.New()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ name := "Chat/" + acct.Nick + "/" + you
+ ww.Name(name)
+ w = &Window{Win: ww, typ: "chat", name: name, remote: you}
+ w.Fprintf("body", "\n")
+ w.Addr("#1")
+ w.OpenEvent()
+ w.Fprintf("ctl", "cleartag\n")
+ w.Fprintf("tag", " Ack")
+ if p := cachedPresence(you); p != nil {
+ w.status(p)
+ }
+ active[name] = w
+ go w.readAcme()
+ return w
+}
+
+func randid() string {
+ return fmt.Sprint(time.Now())
+}
diff --git a/vendor/github.com/mattermost/rsc/google/chat.go b/vendor/github.com/mattermost/rsc/google/chat.go
new file mode 100644
index 000000000..8e9ae1c50
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/chat.go
@@ -0,0 +1,39 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package google
+
+import "github.com/mattermost/rsc/xmpp"
+
+type ChatID struct {
+ ID string
+ Email string
+ Status xmpp.Status
+ StatusMsg string
+}
+
+type ChatSend struct {
+ ID *ChatID
+ Msg xmpp.Chat
+}
+
+func (g *Client) ChatRecv(cid *ChatID) (*xmpp.Chat, error) {
+ var msg xmpp.Chat
+ if err := g.client.Call("goog.ChatRecv", cid, &msg); err != nil {
+ return nil, err
+ }
+ return &msg, nil
+}
+
+func (g *Client) ChatStatus(cid *ChatID) error {
+ return g.client.Call("goog.ChatRecv", cid, &Empty{})
+}
+
+func (g *Client) ChatSend(cid *ChatID, msg *xmpp.Chat) error {
+ return g.client.Call("goog.ChatSend", &ChatSend{cid, *msg}, &Empty{})
+}
+
+func (g *Client) ChatRoster(cid *ChatID) error {
+ return g.client.Call("goog.ChatRoster", cid, &Empty{})
+}
diff --git a/vendor/github.com/mattermost/rsc/google/gmail/gmail.go b/vendor/github.com/mattermost/rsc/google/gmail/gmail.go
new file mode 100644
index 000000000..1d4072ef7
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/gmail/gmail.go
@@ -0,0 +1,1241 @@
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "flag"
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "os/exec"
+ "os/signal"
+ "regexp"
+ "sort"
+ "strings"
+ "time"
+
+ "github.com/mattermost/rsc/google"
+ "github.com/mattermost/rsc/imap"
+)
+
+var cmdtab = []struct {
+ Name string
+ Args int
+ F func(*Cmd, *imap.MsgPart) *imap.MsgPart
+ TF func(*Cmd, []*imap.Msg) *imap.MsgPart
+ Help string
+}{
+ {"+", 0, pluscmd, tpluscmd, "+ print the next message"},
+ {"a", 1, rcmd, nil, "a reply to sender and recipients"},
+ {"b", 0, bcmd, nil, "b print the next 10 headers"},
+ {"d", 0, dcmd, tdcmd, "d mark for deletion"},
+ {"f", 1, fcmd, tfcmd, "f forward message"},
+ {"h", 0, hcmd, nil, "h print elided message summary (,h for all)"},
+ {"help", 0, nil, nil, "help print this info"},
+ {"i", 0, icmd, nil, "i incorporate new mail"},
+ {"m", 0, mcmd, tmcmd, "m mute and delete thread (gmail only)"},
+ {"mime", 0, mimecmd, nil, "mime print message's MIME structure "},
+ {"p", 0, pcmd, nil, "p print the processed message"},
+ // { "p+", 0, pcmd, nil, "p print the processed message, showing all quoted text" },
+ {"P", 0, Pcmd, nil, "P print the raw message"},
+ {`"`, 0, quotecmd, nil, `" print a quoted version of msg`},
+ {"q", 0, qcmd, nil, "q exit and remove all deleted mail"},
+ {"r", 1, rcmd, nil, "r [addr] reply to sender plus any addrs specified"},
+ {"s", 1, scmd, tscmd, "s name copy message to named mailbox (label for gmail)"},
+ {"u", 0, ucmd, nil, "u remove deletion mark"},
+ // { "w", 1, wcmd, nil, "w file store message contents as file" },
+ {"W", 0, Wcmd, nil, "W open in web browser"},
+ {"x", 0, xcmd, nil, "x exit without flushing deleted messages"},
+ {"y", 0, ycmd, nil, "y synchronize with mail box"},
+ {"=", 1, eqcmd, nil, "= print current message number"},
+ {"|", 1, pipecmd, nil, "|cmd pipe message body to a command"},
+ // { "||", 1, rpipecmd, nil, "||cmd pipe raw message to a command" },
+ {"!", 1, bangcmd, nil, "!cmd run a command"},
+}
+
+func init() {
+ // Have to insert helpcmd by hand because it refers to cmdtab,
+ // so it would cause an init loop above.
+ for i := range cmdtab {
+ if cmdtab[i].Name == "help" {
+ cmdtab[i].F = helpcmd
+ }
+ }
+}
+
+type Cmd struct {
+ Name string
+ Args []string
+ Line string // Args[0:] original text
+ ArgLine string // Args[1:] original text
+ F func(*Cmd, *imap.MsgPart) *imap.MsgPart
+ TF func(*Cmd, []*imap.Msg) *imap.MsgPart
+ Delete bool
+ Thread bool
+ Targ *imap.MsgPart
+ Targs []*imap.Msg
+ A1, A2 int
+}
+
+var (
+ bin = bufio.NewReader(os.Stdin)
+ bout = bufio.NewWriter(os.Stdout)
+
+ acctName = flag.String("a", "", "account to use")
+
+ dot *imap.MsgPart // Selected messages
+
+ inbox *imap.Box
+ msgs []*imap.Msg
+ msgNum = make(map[*imap.Msg]int)
+ deleted = make(map[*imap.Msg]bool)
+ isGmail = false
+ acct google.Account
+ threaded bool
+ interrupted bool
+
+ maxfrom int
+ subjlen int
+)
+
+func nextMsg(m *imap.Msg) *imap.Msg {
+ i := msgNum[m]
+ i++
+ if i >= len(msgs) {
+ return nil
+ }
+ return msgs[i]
+}
+
+func main() {
+ flag.BoolVar(&imap.Debug, "imapdebug", false, "imap debugging trace")
+ flag.Parse()
+
+ acct = google.Acct(*acctName)
+
+ if args := flag.Args(); len(args) > 0 {
+ for i := range args {
+ args[i] = "-to=" + args[i]
+ }
+ cmd := exec.Command("gmailsend", append([]string{"-a", acct.Email, "-i"}, args...)...)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ if err := cmd.Run(); err != nil {
+ fmt.Fprintf(os.Stderr, "!%s\n", err)
+ os.Exit(1)
+ }
+ return
+ }
+
+ c, err := imap.NewClient(imap.TLS, "imap.gmail.com", acct.Email, acct.Password, "")
+ if err != nil {
+ log.Fatal(err)
+ }
+ isGmail = c.IsGmail()
+ threaded = isGmail
+
+ inbox = c.Inbox()
+ if err := inbox.Check(); err != nil {
+ log.Fatal(err)
+ }
+
+ msgs = inbox.Msgs()
+ maxfrom = 12
+ for i, m := range msgs {
+ msgNum[m] = i
+ if n := len(from(m.Hdr)); n > maxfrom {
+ maxfrom = n
+ }
+ }
+ if maxfrom > 20 {
+ maxfrom = 20
+ }
+ subjlen = 80 - maxfrom
+
+ rethread()
+
+ go func() {
+ for sig := range signal.Incoming {
+ if sig == os.SIGINT {
+ fmt.Fprintf(os.Stderr, "!interrupt\n")
+ interrupted = true
+ continue
+ }
+ if sig == os.SIGCHLD || sig == os.SIGWINCH {
+ continue
+ }
+ fmt.Fprintf(os.Stderr, "!%s\n", sig)
+ }
+ }()
+
+ for {
+ if dot != nil {
+ fmt.Fprintf(bout, "%d", msgNum[dot.Msg]+1)
+ if dot != &dot.Msg.Root {
+ fmt.Fprintf(bout, ".%s", dot.ID)
+ }
+ }
+ fmt.Fprintf(bout, ": ")
+ bout.Flush()
+
+ line, err := bin.ReadString('\n')
+ if err != nil {
+ break
+ }
+
+ cmd, err := parsecmd(line)
+ if err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ continue
+ }
+
+ if cmd.Targ != nil || cmd.Targs == nil && cmd.A2 == 0 {
+ x := cmd.F(cmd, cmd.Targ)
+ if x != nil {
+ dot = x
+ }
+ } else {
+ targs := cmd.Targs
+ if targs == nil {
+ delta := +1
+ if cmd.A1 > cmd.A2 {
+ delta = -1
+ }
+ for i := cmd.A1; i <= cmd.A2; i += delta {
+ if i < 1 || i > len(msgs) {
+ continue
+ }
+ targs = append(targs, msgs[i-1])
+ }
+ }
+ if cmd.Thread {
+ if !isGmail {
+ fmt.Fprintf(bout, "!need gmail for threaded command\n")
+ continue
+ }
+ byThread := make(map[uint64][]*imap.Msg)
+ for _, m := range msgs {
+ t := m.GmailThread
+ byThread[t] = append(byThread[t], m)
+ }
+ for _, m := range targs {
+ t := m.GmailThread
+ if byThread[t] != nil {
+ if cmd.TF != nil {
+ if x := cmd.TF(cmd, byThread[t]); x != nil {
+ dot = x
+ }
+ } else {
+ for _, mm := range byThread[t] {
+ x := cmd.F(cmd, &mm.Root)
+ if x != nil {
+ dot = x
+ }
+ }
+ }
+ }
+ delete(byThread, t)
+ }
+ continue
+ }
+ for _, m := range targs {
+ if cmd.Delete {
+ dcmd(cmd, &m.Root)
+ if cmd.Name == "p" {
+ // dp is a special case: it advances to the next message before the p.
+ next := nextMsg(m)
+ if next == nil {
+ fmt.Fprintf(bout, "!address\n")
+ dot = &m.Root
+ break
+ }
+ m = next
+ }
+ }
+ x := cmd.F(cmd, &m.Root)
+ if x != nil {
+ dot = x
+ }
+ // TODO: Break loop on interrupt.
+ }
+ }
+ }
+ qcmd(nil, nil)
+}
+
+func parsecmd(line string) (cmd *Cmd, err error) {
+ cmd = &Cmd{}
+ line = strings.TrimSpace(line)
+ if line == "" {
+ // Empty command is a special case: advance and print.
+ cmd.F = pcmd
+ if dot == nil {
+ cmd.A1 = 1
+ cmd.A2 = 1
+ } else {
+ n := msgNum[dot.Msg] + 2
+ if n > len(msgs) {
+ return nil, fmt.Errorf("out of messages")
+ }
+ cmd.A1 = n
+ cmd.A2 = n
+ }
+ return cmd, nil
+ }
+
+ // Global search?
+ if line[0] == 'g' {
+ line = line[1:]
+ if line == "" || line[0] != '/' {
+ // No search string means all messages.
+ cmd.A1 = 1
+ cmd.A2 = len(msgs)
+ } else if line[0] == '/' {
+ re, rest, err := parsere(line)
+ if err != nil {
+ return nil, err
+ }
+ line = rest
+ // Find all messages matching this search string.
+ var targ []*imap.Msg
+ for _, m := range msgs {
+ if re.MatchString(header(m)) {
+ targ = append(targ, m)
+ }
+ }
+ if len(targ) == 0 {
+ return nil, fmt.Errorf("no matches")
+ }
+ cmd.Targs = targ
+ }
+ } else {
+ // Parse an address.
+ a1, targ, rest, err := parseaddr(line, 1)
+ if err != nil {
+ return nil, err
+ }
+ if targ != nil {
+ cmd.Targ = targ
+ line = rest
+ } else {
+ if a1 < 1 || a1 > len(msgs) {
+ return nil, fmt.Errorf("message number %d out of range", a1)
+ }
+ cmd.A1 = a1
+ cmd.A2 = a1
+ a2 := a1
+ if rest != "" && rest[0] == ',' {
+ // This is an address range.
+ a2, targ, rest, err = parseaddr(rest[1:], len(msgs))
+ if err != nil {
+ return nil, err
+ }
+ if a2 < 1 || a2 > len(msgs) {
+ return nil, fmt.Errorf("message number %d out of range", a2)
+ }
+ cmd.A2 = a2
+ } else if rest == line {
+ // There was no address.
+ if dot == nil {
+ cmd.A1 = 1
+ cmd.A2 = 0
+ } else {
+ if dot != nil {
+ if dot == &dot.Msg.Root {
+ // If dot is a plain msg, use a range so that dp works.
+ cmd.A1 = msgNum[dot.Msg] + 1
+ cmd.A2 = cmd.A1
+ } else {
+ cmd.Targ = dot
+ }
+ }
+ }
+ }
+ line = rest
+ }
+ }
+
+ cmd.Line = strings.TrimSpace(line)
+
+ // Insert space after ! or | for tokenization.
+ switch {
+ case strings.HasPrefix(cmd.Line, "||"):
+ cmd.Line = cmd.Line[:2] + " " + cmd.Line[2:]
+ case strings.HasPrefix(cmd.Line, "!"), strings.HasPrefix(cmd.Line, "|"):
+ cmd.Line = cmd.Line[:1] + " " + cmd.Line[1:]
+ }
+
+ av := strings.Fields(cmd.Line)
+ cmd.Args = av
+ if len(av) == 0 || av[0] == "" {
+ // Default is to print.
+ cmd.F = pcmd
+ return cmd, nil
+ }
+
+ name := av[0]
+ cmd.ArgLine = strings.TrimSpace(cmd.Line[len(av[0]):])
+
+ // Hack to allow t prefix on all commands.
+ if len(name) >= 2 && name[0] == 't' {
+ cmd.Thread = true
+ name = name[1:]
+ }
+
+ // Hack to allow d prefix on all commands.
+ if len(name) >= 2 && name[0] == 'd' {
+ cmd.Delete = true
+ name = name[1:]
+ }
+ cmd.Name = name
+
+ // Search command table.
+ for _, ct := range cmdtab {
+ if ct.Name == name {
+ if ct.Args == 0 && len(av) > 1 {
+ return nil, fmt.Errorf("%s doesn't take an argument", name)
+ }
+ cmd.F = ct.F
+ cmd.TF = ct.TF
+ if name == "m" {
+ // mute applies to all thread no matter what
+ cmd.Thread = true
+ }
+ return cmd, nil
+ }
+ }
+ return nil, fmt.Errorf("unknown command %s", name)
+}
+
+func parseaddr(addr string, deflt int) (n int, targ *imap.MsgPart, rest string, err error) {
+ dot := dot
+ n = deflt
+ for {
+ old := addr
+ n, targ, rest, err = parseaddr1(addr, n, dot)
+ if targ != nil || rest == old || err != nil {
+ break
+ }
+ if n < 1 || n > len(msgs) {
+ return 0, nil, "", fmt.Errorf("message number %d out of range", n)
+ }
+ dot = &msgs[n-1].Root
+ addr = rest
+ }
+ return
+}
+
+func parseaddr1(addr string, deflt int, dot *imap.MsgPart) (n int, targ *imap.MsgPart, rest string, err error) {
+ base := 0
+ if dot != nil {
+ base = msgNum[dot.Msg] + 1
+ }
+ if addr == "" {
+ return deflt, nil, addr, nil
+ }
+ var i int
+ sign := 0
+ switch c := addr[0]; c {
+ case '+':
+ sign = +1
+ addr = addr[1:]
+ case '-':
+ sign = -1
+ addr = addr[1:]
+ case '.':
+ if base == 0 {
+ return 0, nil, "", fmt.Errorf("no message selected")
+ }
+ n = base
+ i = 1
+ goto HaveNumber
+ case '$':
+ if len(msgs) == 0 {
+ return 0, nil, "", fmt.Errorf("no messages")
+ }
+ n = len(msgs)
+ i = 1
+ goto HaveNumber
+ case '/', '?':
+ var re *regexp.Regexp
+ re, addr, err = parsere(addr)
+ if err != nil {
+ return
+ }
+ var delta int
+ if c == '/' {
+ delta = +1
+ } else {
+ delta = -1
+ }
+ for j := base + delta; 1 <= j && j <= len(msgs); j += delta {
+ if re.MatchString(header(msgs[j-1])) {
+ n = j
+ i = 0 // already cut addr
+ goto HaveNumber
+ }
+ }
+ err = fmt.Errorf("search")
+ return
+ // TODO case '%'
+ }
+ for i = 0; i < len(addr) && '0' <= addr[i] && addr[i] <= '9'; i++ {
+ n = 10*n + int(addr[i]) - '0'
+ }
+ if sign != 0 {
+ if n == 0 {
+ n = 1
+ }
+ n = base + n*sign
+ goto HaveNumber
+ }
+ if i == 0 {
+ return deflt, nil, addr, nil
+ }
+HaveNumber:
+ rest = addr[i:]
+ if i < len(addr) && addr[i] == '.' {
+ if n < 1 || n > len(msgs) {
+ err = fmt.Errorf("message number %d out of range", n)
+ return
+ }
+ targ = &msgs[n-1].Root
+ for i < len(addr) && addr[i] == '.' {
+ i++
+ var j int
+ n = 0
+ for j = i; j < len(addr) && '0' <= addr[j] && addr[j] <= '9'; j++ {
+ n = 10*n + int(addr[j]) - '0'
+ }
+ if j == i {
+ err = fmt.Errorf("malformed message number %s", addr[:j])
+ return
+ }
+ if n < 1 || n > len(targ.Child) {
+ err = fmt.Errorf("message number %s out of range", addr[:j])
+ return
+ }
+ targ = targ.Child[n-1]
+ i = j
+ }
+ n = 0
+ rest = addr[i:]
+ return
+ }
+ return
+}
+
+func parsere(addr string) (re *regexp.Regexp, rest string, err error) {
+ prog, rest, err := parseprog(addr)
+ if err != nil {
+ return
+ }
+ re, err = regexp.Compile(prog)
+ return
+}
+
+var lastProg string
+
+func parseprog(addr string) (prog string, rest string, err error) {
+ if len(addr) == 1 {
+ if lastProg != "" {
+ return lastProg, "", nil
+ }
+ err = fmt.Errorf("no search")
+ return
+ }
+ i := strings.Index(addr[1:], addr[:1])
+ if i < 0 {
+ prog = addr[1:]
+ rest = ""
+ } else {
+ i += 1 // adjust for slice in IndexByte arg
+ prog, rest = addr[1:i], addr[i+1:]
+ }
+ lastProg = prog
+ return
+}
+
+func bcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ var m *imap.Msg
+ if dot == nil {
+ if len(msgs) == 0 {
+ return nil
+ }
+ m = msgs[0]
+ } else {
+ m = dot.Msg
+ }
+ for i := 0; i < 10; i++ {
+ hcmd(c, &m.Root)
+ next := nextMsg(m)
+ if next == nil {
+ break
+ }
+ m = next
+ }
+ return &m.Root
+}
+
+func dcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ fmt.Fprintf(bout, "!address\n")
+ return nil
+ }
+ deleted[dot.Msg] = true
+ return &dot.Msg.Root
+}
+
+func tdcmd(c *Cmd, msgs []*imap.Msg) *imap.MsgPart {
+ if len(msgs) == 0 {
+ fmt.Fprintf(bout, "!address\n")
+ return nil
+ }
+ for _, m := range msgs {
+ deleted[m] = true
+ }
+ return &msgs[len(msgs)-1].Root
+}
+
+func ucmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ fmt.Fprintf(bout, "!address\n")
+ return nil
+ }
+ delete(deleted, dot.Msg)
+ return &dot.Msg.Root
+}
+
+func eqcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ fmt.Fprintf(bout, "0")
+ } else {
+ fmt.Fprintf(bout, "%d", msgNum[dot.Msg]+1)
+ if dot != &dot.Msg.Root {
+ fmt.Fprintf(bout, ".%s", dot.ID)
+ }
+ }
+ fmt.Fprintf(bout, "\n")
+ return nil
+}
+
+func from(h *imap.MsgHdr) string {
+ if len(h.From) < 1 {
+ return "?"
+ }
+ if name := h.From[0].Name; name != "" {
+ return name
+ }
+ return h.From[0].Email
+}
+
+func header(m *imap.Msg) string {
+ var t string
+ if time.Now().Sub(m.Date) > 365*24*time.Hour {
+ t = m.Date.Format("01/02 15:04")
+ } else {
+ t = m.Date.Format("01/02 2006 ")
+ }
+ ch := ' '
+ if len(m.Root.Child) > 1 || len(m.Root.Child) == 1 && len(m.Root.Child[0].Child) > 0 {
+ ch = 'H'
+ }
+ del := ' '
+ if deleted[m] {
+ del = 'd'
+ }
+ return fmt.Sprintf("%-3d %c%c %s %-*.*s %.*s",
+ msgNum[m]+1, ch, del, t,
+ maxfrom, maxfrom, from(m.Hdr),
+ subjlen, m.Hdr.Subject)
+}
+
+func hcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot != nil {
+ fmt.Fprintf(bout, "%s\n", header(dot.Msg))
+ }
+ return nil
+}
+
+func helpcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ fmt.Fprint(bout, "Commands are of the form [<range>] <command> [args]\n")
+ fmt.Fprint(bout, "<range> := <addr> | <addr>','<addr>| 'g'<search>\n")
+ fmt.Fprint(bout, "<addr> := '.' | '$' | '^' | <number> | <search> | <addr>'+'<addr> | <addr>'-'<addr>\n")
+ fmt.Fprint(bout, "<search> := '/'<gmail search>'/' | '?'<gmail search>'?'\n")
+ fmt.Fprint(bout, "<command> :=\n")
+ for _, ct := range cmdtab {
+ fmt.Fprintf(bout, "%s\n", ct.Help)
+ }
+ return dot
+}
+
+func mimecmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot != nil {
+ mimeH(fmt.Sprint(msgNum[dot.Msg]+1), dot)
+ }
+ return nil
+}
+
+func mimeH(id string, p *imap.MsgPart) {
+ if p.ID != "" {
+ id = id + "." + p.ID
+ }
+ fmt.Fprintf(bout, "%s %s %s %#q %d\n", id, p.Type, p.Encoding+"/"+p.Charset, p.Name, p.Bytes)
+ for _, child := range p.Child {
+ mimeH(id, child)
+ }
+}
+
+func icmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ sync(false)
+ return nil
+}
+
+func ycmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ sync(true)
+ return nil
+}
+
+func tpluscmd(c *Cmd, msgs []*imap.Msg) *imap.MsgPart {
+ if len(msgs) == 0 {
+ return nil
+ }
+ m := nextMsg(msgs[len(msgs)-1])
+ if m == nil {
+ fmt.Fprintf(bout, "!no more messages\n")
+ return nil
+ }
+ return pcmd(c, &m.Root)
+}
+
+func pluscmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ return nil
+ }
+ m := nextMsg(dot.Msg)
+ if m == nil {
+ fmt.Fprintf(bout, "!no more messages\n")
+ return nil
+ }
+ return pcmd(c, &m.Root)
+}
+
+func addrlist(x []imap.Addr) string {
+ var b bytes.Buffer
+ for i, a := range x {
+ if i > 0 {
+ b.WriteString(", ")
+ }
+ b.WriteString(a.String())
+ }
+ return b.String()
+}
+
+func wpcmd(w io.Writer, c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ return nil
+ }
+ if dot == &dot.Msg.Root {
+ h := dot.Msg.Hdr
+ if len(h.From) > 0 {
+ fmt.Fprintf(w, "From: %s\n", addrlist(h.From))
+ }
+ fmt.Fprintf(w, "Date: %s\n", dot.Msg.Date)
+ if len(h.From) > 0 {
+ fmt.Fprintf(w, "To: %s\n", addrlist(h.To))
+ }
+ if len(h.CC) > 0 {
+ fmt.Fprintf(w, "CC: %s\n", addrlist(h.CC))
+ }
+ if len(h.BCC) > 0 {
+ fmt.Fprintf(w, "BCC: %s\n", addrlist(h.BCC))
+ }
+ if len(h.Subject) > 0 {
+ fmt.Fprintf(w, "Subject: %s\n", h.Subject)
+ }
+ fmt.Fprintf(w, "\n")
+ }
+ printMIME(w, dot, true)
+ fmt.Fprintf(w, "\n")
+ return dot
+}
+
+func pcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ defer bout.Flush()
+ return wpcmd(bout, c, dot)
+}
+
+func pipecmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ args := c.Args[1:]
+ if len(args) == 0 {
+ fmt.Fprintf(bout, "!no command\n")
+ return dot
+ }
+ bout.Flush()
+ cmd := exec.Command(args[0], args[1:]...)
+ w, err := cmd.StdinPipe()
+ if err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ return dot
+ }
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ if err := cmd.Start(); err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ return dot
+ }
+ wpcmd(w, c, dot)
+ w.Close()
+ if err := cmd.Wait(); err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ }
+ return dot
+}
+
+func bangcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ args := c.Args[1:]
+ if len(args) == 0 {
+ fmt.Fprintf(bout, "!no command\n")
+ return dot
+ }
+ bout.Flush()
+ cmd := exec.Command(args[0], args[1:]...)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ if err := cmd.Run(); err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ }
+ return nil
+}
+
+func unixfrom(h *imap.MsgHdr) string {
+ if len(h.From) == 0 {
+ return ""
+ }
+ return h.From[0].Email
+}
+
+func unixtime(m *imap.Msg) string {
+ return dot.Msg.Date.Format("Mon Jan _2 15:04:05 MST 2006")
+}
+
+func Pcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ return nil
+ }
+ if dot == &dot.Msg.Root {
+ fmt.Fprintf(bout, "From %s %s\n",
+ unixfrom(dot.Msg.Hdr),
+ unixtime(dot.Msg))
+ }
+ bout.Write(dot.Raw())
+ return dot
+}
+
+func printMIME(w io.Writer, p *imap.MsgPart, top bool) {
+ switch {
+ case top && strings.HasPrefix(p.Type, "text/"):
+ text := p.ShortText()
+ if p.Type == "text/html" {
+ cmd := exec.Command("htmlfmt")
+ cmd.Stdin = bytes.NewBuffer(text)
+ if w == bout {
+ bout.Flush()
+ cmd.Stdout = os.Stdout
+ } else {
+ cmd.Stdout = w
+ }
+ if err := cmd.Run(); err != nil {
+ fmt.Fprintf(w, "%d.%s !%s\n", msgNum[p.Msg]+1, p.ID, err)
+ }
+ return
+ }
+ w.Write(text)
+ case p.Type == "text/plain":
+ if top {
+ panic("printMIME loop")
+ }
+ printMIME(w, p, true)
+ case p.Type == "multipart/alternative":
+ for _, pp := range p.Child {
+ if pp.Type == "text/plain" {
+ printMIME(w, pp, false)
+ return
+ }
+ }
+ if len(p.Child) > 0 {
+ printMIME(w, p.Child[0], false)
+ }
+ case strings.HasPrefix(p.Type, "multipart/"):
+ for _, pp := range p.Child {
+ printMIME(w, pp, false)
+ }
+ default:
+ fmt.Fprintf(w, "%d.%s !%s %s %s\n", msgNum[p.Msg]+1, p.ID, p.Type, p.Desc, p.Name)
+ }
+}
+
+func qcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ flushDeleted()
+ xcmd(c, dot)
+ panic("not reached")
+}
+
+type quoter struct {
+ bol bool
+ w io.Writer
+}
+
+func (q *quoter) Write(b []byte) (n int, err error) {
+ n = len(b)
+ err = nil
+ for len(b) > 0 {
+ if q.bol {
+ q.w.Write([]byte("> "))
+ q.bol = false
+ }
+ i := bytes.IndexByte(b, '\n')
+ if i < 0 {
+ i = len(b)
+ } else {
+ q.bol = true
+ i++
+ }
+ q.w.Write(b[:i])
+ b = b[i:]
+ }
+ return
+}
+
+func quotecmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ return nil
+ }
+ m := dot.Msg
+ if len(m.Hdr.From) != 0 {
+ a := m.Hdr.From[0]
+ name := a.Name
+ if name == "" {
+ name = a.Email
+ }
+ date := m.Date.Format("Jan 2, 2006 at 15:04")
+ fmt.Fprintf(bout, "On %s, %s wrote:\n", date, name)
+ }
+ printMIME(&quoter{true, bout}, dot, true)
+ return dot
+}
+
+func addre(s string) string {
+ if len(s) < 4 || !strings.EqualFold(s[:4], "re: ") {
+ return "Re: " + s
+ }
+ return s
+}
+
+func rcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil || dot.Msg.Hdr == nil {
+ fmt.Fprintf(bout, "!nothing to reply to\n")
+ return nil
+ }
+
+ h := dot.Msg.Hdr
+ replyTo := h.ReplyTo
+ have := make(map[string]bool)
+ if len(replyTo) == 0 {
+ replyTo = h.From
+ }
+ if c.Name[0] == 'a' {
+ for _, a := range replyTo {
+ have[a.Email] = true
+ }
+ for _, a := range append(append(append([]imap.Addr(nil), h.From...), h.To...), h.CC...) {
+ if !have[a.Email] {
+ have[a.Email] = true
+ replyTo = append(replyTo, a)
+ }
+ }
+ }
+ if len(replyTo) == 0 {
+ fmt.Fprintf(bout, "!no one to reply to\n")
+ return dot
+ }
+
+ args := []string{"-a", acct.Email, "-s", addre(h.Subject), "-in-reply-to", h.MessageID}
+ fmt.Fprintf(bout, "replying to:")
+ for _, a := range replyTo {
+ fmt.Fprintf(bout, " %s", a.Email)
+ args = append(args, "-to", a.String())
+ }
+ for _, arg := range c.Args[1:] {
+ fmt.Fprintf(bout, " %s", arg)
+ args = append(args, "-to", arg)
+ }
+ fmt.Fprintf(bout, "\n")
+ bout.Flush()
+ cmd := exec.Command("gmailsend", args...)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ err := cmd.Run()
+ if err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ }
+ return dot
+}
+
+func fcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ fmt.Fprintf(bout, "!nothing to forward\n")
+ return nil
+ }
+
+ return fwd(c, dot, nil)
+}
+
+func tfcmd(c *Cmd, msgs []*imap.Msg) *imap.MsgPart {
+ if len(msgs) == 0 {
+ fmt.Fprintf(bout, "!nothing to forward\n")
+ return nil
+ }
+
+ return fwd(c, &msgs[len(msgs)-1].Root, msgs)
+}
+
+func fwd(c *Cmd, dot *imap.MsgPart, msgs []*imap.Msg) *imap.MsgPart {
+ addrs := c.Args[1:]
+ if len(addrs) == 0 {
+ fmt.Fprintf(bout, "!f command requires address to forward to\n")
+ return dot
+ }
+
+ h := dot.Msg.Hdr
+ args := []string{"-a", acct.Email, "-s", "Fwd: " + h.Subject, "-append", "/dev/fd/3"}
+ fmt.Fprintf(bout, "forwarding to:")
+ for _, arg := range addrs {
+ fmt.Fprintf(bout, " %s", arg)
+ args = append(args, "-to", arg)
+ }
+ fmt.Fprintf(bout, "\n")
+ bout.Flush()
+
+ cmd := exec.Command("gmailsend", args...)
+ r, w, err := os.Pipe()
+ if err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ return dot
+ }
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ cmd.ExtraFiles = []*os.File{r}
+ if err := cmd.Start(); err != nil {
+ r.Close()
+ fmt.Fprintf(bout, "!%s\n", err)
+ return dot
+ }
+ r.Close()
+ what := "message"
+ if len(msgs) > 1 {
+ what = "conversation"
+ }
+ fmt.Fprintf(w, "\n\n--- Forwarded %s ---\n", what)
+ if msgs == nil {
+ wpcmd(w, c, dot)
+ } else {
+ for _, m := range msgs {
+ wpcmd(w, c, &m.Root)
+ fmt.Fprintf(w, "\n\n")
+ }
+ }
+ w.Close()
+ if err := cmd.Wait(); err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ }
+ return dot
+}
+
+func rethread() {
+ if !threaded {
+ sort.Sort(byUIDRev(msgs))
+ } else {
+ byThread := make(map[uint64][]*imap.Msg)
+ for _, m := range msgs {
+ t := m.GmailThread
+ byThread[t] = append(byThread[t], m)
+ }
+
+ var threadList [][]*imap.Msg
+ for _, t := range byThread {
+ sort.Sort(byUID(t))
+ threadList = append(threadList, t)
+ }
+ sort.Sort(byUIDList(threadList))
+
+ msgs = msgs[:0]
+ for _, t := range threadList {
+ msgs = append(msgs, t...)
+ }
+ }
+ for i, m := range msgs {
+ msgNum[m] = i
+ }
+}
+
+type byUID []*imap.Msg
+
+func (l byUID) Less(i, j int) bool { return l[i].UID < l[j].UID }
+func (l byUID) Len() int { return len(l) }
+func (l byUID) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
+
+type byUIDRev []*imap.Msg
+
+func (l byUIDRev) Less(i, j int) bool { return l[i].UID > l[j].UID }
+func (l byUIDRev) Len() int { return len(l) }
+func (l byUIDRev) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
+
+type byUIDList [][]*imap.Msg
+
+func (l byUIDList) Less(i, j int) bool { return l[i][len(l[i])-1].UID > l[j][len(l[j])-1].UID }
+func (l byUIDList) Len() int { return len(l) }
+func (l byUIDList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
+
+func subj(m *imap.Msg) string {
+ s := m.Hdr.Subject
+ for strings.HasPrefix(s, "Re: ") || strings.HasPrefix(s, "RE: ") {
+ s = s[4:]
+ }
+ return s
+}
+
+func mcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ c.ArgLine = "Muted"
+ scmd(c, dot)
+ return dcmd(c, dot)
+}
+
+func tmcmd(c *Cmd, msgs []*imap.Msg) *imap.MsgPart {
+ c.ArgLine = "Muted"
+ tscmd(c, msgs)
+ return tdcmd(c, msgs)
+}
+
+func scmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ return nil
+ }
+ return tscmd(c, []*imap.Msg{dot.Msg})
+}
+
+func tscmd(c *Cmd, msgs []*imap.Msg) *imap.MsgPart {
+ if len(msgs) == 0 {
+ return nil
+ }
+ arg := c.ArgLine
+ dot := &msgs[len(msgs)-1].Root
+ if arg == "" {
+ fmt.Fprintf(bout, "!s needs mailbox (label) name as argument\n")
+ return dot
+ }
+ if strings.EqualFold(arg, "Muted") {
+ if err := dot.Msg.Box.Mute(msgs); err != nil {
+ fmt.Fprintf(bout, "!mute: %s\n", err)
+ }
+ } else {
+ dst := dot.Msg.Box.Client.Box(arg)
+ if dst == nil {
+ fmt.Fprintf(bout, "!unknown mailbox %#q", arg)
+ return dot
+ }
+ if err := dst.Copy(msgs); err != nil {
+ fmt.Fprintf(bout, "!s %#q: %s\n", arg, err)
+ }
+ }
+ return dot
+}
+
+func Wcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ if dot == nil {
+ return nil
+ }
+ if !isGmail {
+ fmt.Fprintf(bout, "!cmd W requires gmail\n")
+ return dot
+ }
+ url := fmt.Sprintf("https://mail.google.com/mail/b/%s/?shva=1#inbox/%x", acct.Email, dot.Msg.GmailThread)
+ if err := exec.Command("open", url).Run(); err != nil {
+ fmt.Fprintf(bout, "!%s\n", err)
+ }
+ return dot
+}
+
+func xcmd(c *Cmd, dot *imap.MsgPart) *imap.MsgPart {
+ // TODO: remove saved attachments?
+ os.Exit(0)
+ panic("not reached")
+}
+
+func flushDeleted() {
+ var toDelete []*imap.Msg
+ for m := range deleted {
+ toDelete = append(toDelete, m)
+ }
+ if len(toDelete) == 0 {
+ return
+ }
+ fmt.Fprintf(os.Stderr, "!deleting %d\n", len(toDelete))
+ err := inbox.Delete(toDelete)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "!deleting: %s\n", err)
+ }
+}
+
+func loadNew() {
+ if err := inbox.Check(); err != nil {
+ fmt.Fprintf(os.Stderr, "!inbox: %s\n", err)
+ }
+
+ old := make(map[*imap.Msg]bool)
+ for _, m := range msgs {
+ old[m] = true
+ }
+
+ nnew := 0
+ new := inbox.Msgs()
+ for _, m := range new {
+ if old[m] {
+ delete(old, m)
+ } else {
+ msgs = append(msgs, m)
+ nnew++
+ }
+ }
+ if nnew > 0 {
+ fmt.Fprintf(os.Stderr, "!%d new messages\n", nnew)
+ }
+ for m := range old {
+ // Deleted
+ m.Flags |= imap.FlagDeleted
+ delete(deleted, m)
+ }
+}
+
+func sync(delete bool) {
+ if delete {
+ flushDeleted()
+ }
+ loadNew()
+ if delete {
+ w := 0
+ for _, m := range msgs {
+ if !m.Deleted() {
+ msgs[w] = m
+ w++
+ }
+ }
+ msgs = msgs[:w]
+ }
+ rethread()
+}
diff --git a/vendor/github.com/mattermost/rsc/google/gmailsend/send.go b/vendor/github.com/mattermost/rsc/google/gmailsend/send.go
new file mode 100644
index 000000000..ace6eb12b
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/gmailsend/send.go
@@ -0,0 +1,370 @@
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "encoding/base64"
+ "flag"
+ "fmt"
+ "io"
+ "net/smtp"
+ "os"
+ "regexp"
+ "strings"
+
+ "github.com/mattermost/rsc/google"
+)
+
+func enc(s string) string {
+ // TODO =? .. ?=
+ return s
+}
+
+type Addr struct {
+ Name string
+ Email string
+}
+
+func (a Addr) enc() string {
+ if a.Name == "" {
+ return "<" + a.Email + ">"
+ }
+ if a.Email == "" {
+ return enc(a.Name) + ":;"
+ }
+ return enc(a.Name) + " <" + a.Email + ">"
+}
+
+type Addrs []Addr
+
+func (a *Addrs) String() string {
+ return "[addrlist]"
+}
+
+func (a Addrs) has(s string) bool {
+ for _, aa := range a {
+ if aa.Email == s {
+ return true
+ }
+ }
+ return false
+}
+
+func (a *Addrs) Set(s string) bool {
+ s = strings.TrimSpace(s)
+ if strings.HasSuffix(s, ">") {
+ j := strings.LastIndex(s, "<")
+ if j >= 0 {
+ *a = append(*a, Addr{strings.TrimSpace(s[:j]), s[j+1 : len(s)-1]})
+ return true
+ }
+ }
+
+ if strings.Contains(s, " ") {
+ fmt.Fprintf(os.Stderr, "invalid address: %s", s)
+ os.Exit(2)
+ }
+ *a = append(*a, Addr{"", s})
+ return true
+}
+
+func (a *Addrs) parseLine(s string) {
+ for _, f := range strings.Split(s, ",") {
+ f = strings.TrimSpace(f)
+ if f != "" {
+ a.Set(f)
+ }
+ }
+}
+
+func (a Addrs) fixDomain() {
+ i := strings.Index(acct.Email, "@")
+ if i < 0 {
+ return
+ }
+ dom := acct.Email[i:]
+ for i := range a {
+ if a[i].Email != "" && !strings.Contains(a[i].Email, "@") {
+ a[i].Email += dom
+ }
+ }
+}
+
+var from, to, cc, bcc, replyTo Addrs
+var inReplyTo, subject string
+var appendFile = flag.String("append", "", "file to append to end of body")
+
+var acct google.Account
+var acctName = flag.String("a", "", "account to use")
+var inputHeader = flag.Bool("i", false, "read additional header lines from stdin")
+
+func holdmode() {
+ if os.Getenv("TERM") == "9term" {
+ // forgive me
+ os.Stdout.WriteString("\x1B];*9term-hold+\x07")
+ }
+}
+
+func match(line, prefix string, arg *string) bool {
+ if len(line) < len(prefix) || !strings.EqualFold(line[:len(prefix)], prefix) {
+ return false
+ }
+ *arg = strings.TrimSpace(line[len(prefix):])
+ return true
+}
+
+func main() {
+ flag.StringVar(&inReplyTo, "in-reply-to", "", "In-Reply-To")
+ flag.StringVar(&subject, "s", "", "Subject")
+ flag.Var(&from, "from", "From (can repeat)")
+ flag.Var(&to, "to", "To (can repeat)")
+ flag.Var(&cc, "cc", "CC (can repeat)")
+ flag.Var(&bcc, "bcc", "BCC (can repeat)")
+ flag.Var(&replyTo, "replyTo", "Reply-To (can repeat)")
+
+ flag.Parse()
+ if flag.NArg() != 0 && !*inputHeader {
+ flag.Usage()
+ }
+
+ var body bytes.Buffer
+ input := bufio.NewReader(os.Stdin)
+ if *inputHeader {
+ holdmode()
+ Loop:
+ for {
+ s, err := input.ReadString('\n')
+ if err != nil {
+ if err == io.EOF {
+ break Loop
+ }
+ fmt.Fprintf(os.Stderr, "reading stdin: %s\n", err)
+ os.Exit(2)
+ }
+ var arg string
+ switch {
+ default:
+ if ok, _ := regexp.MatchString(`^\S+:`, s); ok {
+ fmt.Fprintf(os.Stderr, "unknown header line: %s", s)
+ os.Exit(2)
+ }
+ body.WriteString(s)
+ break Loop
+ case match(s, "from:", &arg):
+ from.parseLine(arg)
+ case match(s, "to:", &arg):
+ to.parseLine(arg)
+ case match(s, "cc:", &arg):
+ cc.parseLine(arg)
+ case match(s, "bcc:", &arg):
+ bcc.parseLine(arg)
+ case match(s, "reply-to:", &arg):
+ replyTo.parseLine(arg)
+ case match(s, "subject:", &arg):
+ subject = arg
+ case match(s, "in-reply-to:", &arg):
+ inReplyTo = arg
+ }
+ }
+ }
+
+ acct = google.Acct(*acctName)
+ from.fixDomain()
+ to.fixDomain()
+ cc.fixDomain()
+ bcc.fixDomain()
+ replyTo.fixDomain()
+
+ smtpTo := append(append(to, cc...), bcc...)
+
+ if len(from) == 0 {
+ // TODO: Much better
+ name := ""
+ email := acct.Email
+ if email == "rsc@swtch.com" || email == "rsc@google.com" {
+ name = "Russ Cox"
+ }
+ if email == "rsc@google.com" && (smtpTo.has("go@googlecode.com") || smtpTo.has("golang-dev@googlegroups.com") || smtpTo.has("golang-nuts@googlegroups.com")) {
+ from = append(from, Addr{name, "rsc@golang.org"})
+ } else {
+ from = append(from, Addr{name, email})
+ }
+ }
+
+ if len(from) > 1 {
+ fmt.Fprintf(os.Stderr, "missing -from\n")
+ os.Exit(2)
+ }
+
+ if len(to)+len(cc)+len(bcc) == 0 {
+ fmt.Fprintf(os.Stderr, "missing destinations\n")
+ os.Exit(2)
+ }
+
+ if !*inputHeader {
+ holdmode()
+ }
+ _, err := io.Copy(&body, input)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "reading stdin: %s\n", err)
+ os.Exit(2)
+ }
+
+ if *appendFile != "" {
+ f, err := os.Open(*appendFile)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "append: %s\n", err)
+ os.Exit(2)
+ }
+ _, err = io.Copy(&body, f)
+ f.Close()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "append: %s\n", err)
+ os.Exit(2)
+ }
+ }
+
+ var msg bytes.Buffer
+ fmt.Fprintf(&msg, "MIME-Version: 1.0\n")
+ if len(from) > 0 {
+ fmt.Fprintf(&msg, "From: ")
+ for i, a := range from {
+ if i > 0 {
+ fmt.Fprintf(&msg, ", ")
+ }
+ fmt.Fprintf(&msg, "%s", a.enc())
+ }
+ fmt.Fprintf(&msg, "\n")
+ }
+ if len(to) > 0 {
+ fmt.Fprintf(&msg, "To: ")
+ for i, a := range to {
+ if i > 0 {
+ fmt.Fprintf(&msg, ", ")
+ }
+ fmt.Fprintf(&msg, "%s", a.enc())
+ }
+ fmt.Fprintf(&msg, "\n")
+ }
+ if len(cc) > 0 {
+ fmt.Fprintf(&msg, "CC: ")
+ for i, a := range cc {
+ if i > 0 {
+ fmt.Fprintf(&msg, ", ")
+ }
+ fmt.Fprintf(&msg, "%s", a.enc())
+ }
+ fmt.Fprintf(&msg, "\n")
+ }
+ if len(replyTo) > 0 {
+ fmt.Fprintf(&msg, "Reply-To: ")
+ for i, a := range replyTo {
+ if i > 0 {
+ fmt.Fprintf(&msg, ", ")
+ }
+ fmt.Fprintf(&msg, "%s", a.enc())
+ }
+ fmt.Fprintf(&msg, "\n")
+ }
+ if inReplyTo != "" {
+ fmt.Fprintf(&msg, "In-Reply-To: %s\n", inReplyTo)
+ }
+ if subject != "" {
+ fmt.Fprintf(&msg, "Subject: %s\n", enc(subject))
+ }
+ fmt.Fprintf(&msg, "Date: xxx\n")
+ fmt.Fprintf(&msg, "Content-Type: text/plain; charset=\"utf-8\"\n")
+ fmt.Fprintf(&msg, "Content-Transfer-Encoding: base64\n")
+ fmt.Fprintf(&msg, "\n")
+ enc64 := base64.StdEncoding.EncodeToString(body.Bytes())
+ for len(enc64) > 72 {
+ fmt.Fprintf(&msg, "%s\n", enc64[:72])
+ enc64 = enc64[72:]
+ }
+ fmt.Fprintf(&msg, "%s\n\n", enc64)
+
+ auth := smtp.PlainAuth(
+ "",
+ acct.Email,
+ acct.Password,
+ "smtp.gmail.com",
+ )
+ var smtpToEmail []string
+ for _, a := range smtpTo {
+ if a.Email != "" {
+ smtpToEmail = append(smtpToEmail, a.Email)
+ }
+ }
+
+ if err := sendMail("smtp.gmail.com:587", auth, from[0].Email, smtpToEmail, msg.Bytes()); err != nil {
+ fmt.Fprintf(os.Stderr, "sending mail: %s\n", err)
+ os.Exit(2)
+ }
+}
+
+/*
+ MIME-Version: 1.0
+Subject: commit/plan9port: rsc: 9term: hold mode back door
+From: Bitbucket <commits-noreply@bitbucket.org>
+To: plan9port-dev@googlegroups.com
+Date: Tue, 11 Oct 2011 13:34:30 -0000
+Message-ID: <20111011133430.31146.55070@bitbucket13.managed.contegix.com>
+Reply-To: commits-noreply@bitbucket.org
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: quoted-printable
+
+1 new changeset in plan9port:
+
+http://bitbucket.org/rsc/plan9port/changeset/8735d7708a1b/
+changeset: 8735d7708a1b
+user: rsc
+date: 2011-10-11 15:34:25
+summary: 9term: hold mode back door
+
+R=3Drsc
+http://codereview.appspot.com/5248056
+affected #: 2 files (-1 bytes)
+
+Repository URL: https://bitbucket.org/rsc/plan9port/
+
+--
+
+This is a commit notification from bitbucket.org. You are receiving
+this because you have the service enabled, addressing the recipient of
+this email.
+
+*/
+
+func sendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
+ c, err := smtp.Dial(addr)
+ if err != nil {
+ return err
+ }
+ if err = c.StartTLS(nil); err != nil {
+ return err
+ }
+ if err = c.Auth(a); err != nil {
+ return err
+ }
+ if err = c.Mail(from); err != nil {
+ return err
+ }
+ for _, addr := range to {
+ if err = c.Rcpt(addr); err != nil {
+ return err
+ }
+ }
+ w, err := c.Data()
+ if err != nil {
+ return err
+ }
+ _, err = w.Write(msg)
+ if err != nil {
+ return err
+ }
+ err = w.Close()
+ if err != nil {
+ return err
+ }
+ return c.Quit()
+}
diff --git a/vendor/github.com/mattermost/rsc/google/googleserver/chat.go b/vendor/github.com/mattermost/rsc/google/googleserver/chat.go
new file mode 100644
index 000000000..8b064dc92
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/googleserver/chat.go
@@ -0,0 +1,80 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TODO: Add ChatHangup.
+// TODO: Auto-hangup chats that are gone.
+
+package main
+
+import (
+ "fmt"
+
+ "github.com/mattermost/rsc/google"
+ "github.com/mattermost/rsc/xmpp"
+)
+
+type chatClient struct {
+ email string
+ id string
+ xmpp *xmpp.Client
+}
+
+var chatClients = map[string]*chatClient{}
+
+func (*Server) chatClient(cid *google.ChatID) (*chatClient, error) {
+ id := cid.ID
+ cc := chatClients[cid.ID]
+ if cc == nil {
+ a := google.Cfg.AccountByEmail(cid.Email)
+ if a == nil {
+ return nil, fmt.Errorf("unknown account %s", cid.Email)
+ }
+ // New client.
+ cli, err := xmpp.NewClient("talk.google.com:443", a.Email, a.Password)
+ if err != nil {
+ return nil, err
+ }
+ cc = &chatClient{email: a.Email, id: id, xmpp: cli}
+ cc.xmpp.Status(cid.Status, cid.StatusMsg)
+ chatClients[id] = cc
+ }
+ return cc, nil
+}
+
+func (srv *Server) ChatRecv(cid *google.ChatID, msg *xmpp.Chat) error {
+ cc, err := srv.chatClient(cid)
+ if err != nil {
+ return err
+ }
+ chat, err := cc.xmpp.Recv()
+ if err != nil {
+ return err
+ }
+ *msg = chat
+ return nil
+}
+
+func (srv *Server) ChatStatus(cid *google.ChatID, _ *Empty) error {
+ cc, err := srv.chatClient(cid)
+ if err != nil {
+ return err
+ }
+ return cc.xmpp.Status(cid.Status, cid.StatusMsg)
+}
+
+func (srv *Server) ChatSend(arg *google.ChatSend, _ *Empty) error {
+ cc, err := srv.chatClient(arg.ID)
+ if err != nil {
+ return err
+ }
+ return cc.xmpp.Send(arg.Msg)
+}
+
+func (srv *Server) ChatRoster(cid *google.ChatID, _ *Empty) error {
+ cc, err := srv.chatClient(cid)
+ if err != nil {
+ return err
+ }
+ return cc.xmpp.Roster()
+}
diff --git a/vendor/github.com/mattermost/rsc/google/googleserver/main.go b/vendor/github.com/mattermost/rsc/google/googleserver/main.go
new file mode 100644
index 000000000..2cd022446
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/googleserver/main.go
@@ -0,0 +1,139 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ // "flag"
+ "bufio"
+ "fmt"
+ "log"
+ "net"
+ "net/rpc"
+ "os"
+ "strings"
+ "syscall"
+
+ "github.com/mattermost/rsc/google"
+ "github.com/mattermost/rsc/xmpp"
+)
+
+func main() {
+ google.ReadConfig()
+ switch os.Args[1] {
+ case "add":
+ google.Cfg.Account = append(google.Cfg.Account, &google.Account{Email: os.Args[2], Password: os.Args[3]})
+ google.WriteConfig()
+ case "serve":
+ serve()
+ case "accounts":
+ c, err := google.Dial()
+ if err != nil {
+ log.Fatal(err)
+ }
+ out, err := c.Accounts()
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, email := range out {
+ fmt.Printf("%s\n", email)
+ }
+ case "ping":
+ c, err := google.Dial()
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := c.Ping(); err != nil {
+ log.Fatal(err)
+ }
+ case "chat":
+ c, err := google.Dial()
+ if err != nil {
+ log.Fatal(err)
+ }
+ cid := &google.ChatID{ID: "1", Email: os.Args[2], Status: xmpp.Available, StatusMsg: ""}
+ go chatRecv(c, cid)
+ c.ChatRoster(cid)
+ b := bufio.NewReader(os.Stdin)
+ for {
+ line, err := b.ReadString('\n')
+ if err != nil {
+ log.Fatal(err)
+ }
+ line = line[:len(line)-1]
+ i := strings.Index(line, ": ")
+ if i < 0 {
+ log.Printf("<who>: <msg>, please")
+ continue
+ }
+ who, msg := line[:i], line[i+2:]
+ if err := c.ChatSend(cid, &xmpp.Chat{Remote: who, Type: "chat", Text: msg}); err != nil {
+ log.Fatal(err)
+ }
+ }
+ }
+}
+
+func chatRecv(c *google.Client, cid *google.ChatID) {
+ for {
+ msg, err := c.ChatRecv(cid)
+ if err != nil {
+ log.Fatal(err)
+ }
+ switch msg.Type {
+ case "roster":
+ for _, contact := range msg.Roster {
+ fmt.Printf("%v\n", contact)
+ }
+ case "presence":
+ fmt.Printf("%v\n", msg.Presence)
+ case "chat":
+ fmt.Printf("%s: %s\n", msg.Remote, msg.Text)
+ default:
+ fmt.Printf("<%s>\n", msg.Type)
+ }
+ }
+}
+
+func listen() net.Listener {
+ socket := google.Dir() + "/socket"
+ os.Remove(socket)
+ l, err := net.Listen("unix", socket)
+ if err != nil {
+ log.Fatal(err)
+ }
+ return l
+}
+
+func serve() {
+ f, err := os.OpenFile(google.Dir()+"/log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
+ if err != nil {
+ log.Fatal(err)
+ }
+ log.SetOutput(f)
+ syscall.Dup2(f.Fd(), 2)
+ os.Stdout = f
+ os.Stderr = f
+ l := listen()
+ rpc.RegisterName("goog", &Server{})
+ rpc.Accept(l)
+ log.Fatal("rpc.Accept finished: server exiting")
+}
+
+type Server struct{}
+
+type Empty google.Empty
+
+func (*Server) Ping(*Empty, *Empty) error {
+ return nil
+}
+
+func (*Server) Accounts(_ *Empty, out *[]string) error {
+ var email []string
+ for _, a := range google.Cfg.Account {
+ email = append(email, a.Email)
+ }
+ *out = email
+ return nil
+}
diff --git a/vendor/github.com/mattermost/rsc/google/main.go b/vendor/github.com/mattermost/rsc/google/main.go
new file mode 100644
index 000000000..b11a6eefb
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/main.go
@@ -0,0 +1,181 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TODO: Something about redialing.
+
+package google
+
+import (
+ // "flag"
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net"
+ "net/rpc"
+ "os"
+ "os/exec"
+ "syscall"
+ "time"
+)
+
+func Dir() string {
+ dir := os.Getenv("HOME") + "/.goog"
+ st, err := os.Stat(dir)
+ if err != nil {
+ if err := os.Mkdir(dir, 0700); err != nil {
+ log.Fatal(err)
+ }
+ st, err = os.Stat(dir)
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+ if !st.IsDir() {
+ log.Fatalf("%s exists but is not a directory", dir)
+ }
+ if st.Mode()&0077 != 0 {
+ log.Fatalf("%s exists but allows group or other permissions: %#o", dir, st.Mode()&0777)
+ }
+ return dir
+}
+
+func Dial() (*Client, error) {
+ socket := Dir() + "/socket"
+ c, err := net.Dial("unix", socket)
+ if err == nil {
+ return &Client{rpc.NewClient(c)}, nil
+ }
+ log.Print("starting server")
+ os.Remove(socket)
+ runServer()
+ for i := 0; i < 50; i++ {
+ c, err = net.Dial("unix", socket)
+ if err == nil {
+ return &Client{rpc.NewClient(c)}, nil
+ }
+ time.Sleep(200e6)
+ if i == 0 {
+ log.Print("waiting for server...")
+ }
+ }
+ return nil, err
+}
+
+type Client struct {
+ client *rpc.Client
+}
+
+type Empty struct{}
+
+func (g *Client) Ping() error {
+ return g.client.Call("goog.Ping", &Empty{}, &Empty{})
+}
+
+func (g *Client) Accounts() ([]string, error) {
+ var out []string
+ if err := g.client.Call("goog.Accounts", &Empty{}, &out); err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
+func runServer() {
+ cmd := exec.Command("googleserver", "serve")
+ cmd.SysProcAttr = &syscall.SysProcAttr{}
+ if err := cmd.Start(); err != nil {
+ log.Fatal(err)
+ }
+}
+
+type Config struct {
+ Account []*Account
+}
+
+type Account struct {
+ Email string
+ Password string
+ Nick string
+}
+
+func (cfg *Config) AccountByEmail(email string) *Account {
+ for _, a := range cfg.Account {
+ if a.Email == email {
+ return a
+ }
+ }
+ return nil
+}
+
+var Cfg Config
+
+func ReadConfig() {
+ file := Dir() + "/config"
+ st, err := os.Stat(file)
+ if err != nil {
+ return
+ }
+ if st.Mode()&0077 != 0 {
+ log.Fatalf("%s exists but allows group or other permissions: %#o", file, st.Mode()&0777)
+ }
+ data, err := ioutil.ReadFile(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ Cfg = Config{}
+ if err := json.Unmarshal(data, &Cfg); err != nil {
+ log.Fatal(err)
+ }
+}
+
+func WriteConfig() {
+ file := Dir() + "/config"
+ st, err := os.Stat(file)
+ if err != nil {
+ if err := ioutil.WriteFile(file, nil, 0600); err != nil {
+ log.Fatal(err)
+ }
+ st, err = os.Stat(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+ if st.Mode()&0077 != 0 {
+ log.Fatalf("%s exists but allows group or other permissions: %#o", file, st.Mode()&0777)
+ }
+ data, err := json.MarshalIndent(&Cfg, "", "\t")
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := ioutil.WriteFile(file, data, 0600); err != nil {
+ log.Fatal(err)
+ }
+ st, err = os.Stat(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if st.Mode()&0077 != 0 {
+ log.Fatalf("%s allows group or other permissions after writing: %#o", file, st.Mode()&0777)
+ }
+}
+
+func Acct(name string) Account {
+ ReadConfig()
+ if name == "" {
+ if len(Cfg.Account) == 0 {
+ fmt.Fprintf(os.Stderr, "no accounts configured\n")
+ os.Exit(2)
+ }
+ return *Cfg.Account[0]
+ }
+
+ for _, a := range Cfg.Account {
+ if a.Email == name || a.Nick == name {
+ return *a
+ }
+ }
+ fmt.Fprintf(os.Stderr, "cannot find account %#q", name)
+ os.Exit(2)
+ panic("not reached")
+}
diff --git a/vendor/github.com/mattermost/rsc/gtfs/gtfs.pb.go b/vendor/github.com/mattermost/rsc/gtfs/gtfs.pb.go
new file mode 100644
index 000000000..7a91a74d5
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/gtfs/gtfs.pb.go
@@ -0,0 +1,440 @@
+// Code generated by protoc-gen-go from "crashme/gtfs.proto"
+// DO NOT EDIT!
+
+package gtfs
+
+import proto "code.google.com/p/goprotobuf/proto"
+import "math"
+
+// Reference proto, math & os imports to suppress error if they are not otherwise used.
+var _ = proto.GetString
+var _ = math.Inf
+var _ error
+
+type FeedHeader_Incrementality int32
+
+const (
+ FeedHeader_FULL_DATASET FeedHeader_Incrementality = 0
+ FeedHeader_DIFFERENTIAL FeedHeader_Incrementality = 1
+)
+
+var FeedHeader_Incrementality_name = map[int32]string{
+ 0: "FULL_DATASET",
+ 1: "DIFFERENTIAL",
+}
+var FeedHeader_Incrementality_value = map[string]int32{
+ "FULL_DATASET": 0,
+ "DIFFERENTIAL": 1,
+}
+
+func NewFeedHeader_Incrementality(x FeedHeader_Incrementality) *FeedHeader_Incrementality {
+ e := FeedHeader_Incrementality(x)
+ return &e
+}
+func (x FeedHeader_Incrementality) String() string {
+ return proto.EnumName(FeedHeader_Incrementality_name, int32(x))
+}
+
+type TripUpdate_StopTimeUpdate_ScheduleRelationship int32
+
+const (
+ TripUpdate_StopTimeUpdate_SCHEDULED TripUpdate_StopTimeUpdate_ScheduleRelationship = 0
+ TripUpdate_StopTimeUpdate_SKIPPED TripUpdate_StopTimeUpdate_ScheduleRelationship = 1
+ TripUpdate_StopTimeUpdate_NO_DATA TripUpdate_StopTimeUpdate_ScheduleRelationship = 2
+)
+
+var TripUpdate_StopTimeUpdate_ScheduleRelationship_name = map[int32]string{
+ 0: "SCHEDULED",
+ 1: "SKIPPED",
+ 2: "NO_DATA",
+}
+var TripUpdate_StopTimeUpdate_ScheduleRelationship_value = map[string]int32{
+ "SCHEDULED": 0,
+ "SKIPPED": 1,
+ "NO_DATA": 2,
+}
+
+func NewTripUpdate_StopTimeUpdate_ScheduleRelationship(x TripUpdate_StopTimeUpdate_ScheduleRelationship) *TripUpdate_StopTimeUpdate_ScheduleRelationship {
+ e := TripUpdate_StopTimeUpdate_ScheduleRelationship(x)
+ return &e
+}
+func (x TripUpdate_StopTimeUpdate_ScheduleRelationship) String() string {
+ return proto.EnumName(TripUpdate_StopTimeUpdate_ScheduleRelationship_name, int32(x))
+}
+
+type VehiclePosition_VehicleStopStatus int32
+
+const (
+ VehiclePosition_INCOMING_AT VehiclePosition_VehicleStopStatus = 0
+ VehiclePosition_STOPPED_AT VehiclePosition_VehicleStopStatus = 1
+ VehiclePosition_IN_TRANSIT_TO VehiclePosition_VehicleStopStatus = 2
+)
+
+var VehiclePosition_VehicleStopStatus_name = map[int32]string{
+ 0: "INCOMING_AT",
+ 1: "STOPPED_AT",
+ 2: "IN_TRANSIT_TO",
+}
+var VehiclePosition_VehicleStopStatus_value = map[string]int32{
+ "INCOMING_AT": 0,
+ "STOPPED_AT": 1,
+ "IN_TRANSIT_TO": 2,
+}
+
+func NewVehiclePosition_VehicleStopStatus(x VehiclePosition_VehicleStopStatus) *VehiclePosition_VehicleStopStatus {
+ e := VehiclePosition_VehicleStopStatus(x)
+ return &e
+}
+func (x VehiclePosition_VehicleStopStatus) String() string {
+ return proto.EnumName(VehiclePosition_VehicleStopStatus_name, int32(x))
+}
+
+type VehiclePosition_CongestionLevel int32
+
+const (
+ VehiclePosition_UNKNOWN_CONGESTION_LEVEL VehiclePosition_CongestionLevel = 0
+ VehiclePosition_RUNNING_SMOOTHLY VehiclePosition_CongestionLevel = 1
+ VehiclePosition_STOP_AND_GO VehiclePosition_CongestionLevel = 2
+ VehiclePosition_CONGESTION VehiclePosition_CongestionLevel = 3
+ VehiclePosition_SEVERE_CONGESTION VehiclePosition_CongestionLevel = 4
+)
+
+var VehiclePosition_CongestionLevel_name = map[int32]string{
+ 0: "UNKNOWN_CONGESTION_LEVEL",
+ 1: "RUNNING_SMOOTHLY",
+ 2: "STOP_AND_GO",
+ 3: "CONGESTION",
+ 4: "SEVERE_CONGESTION",
+}
+var VehiclePosition_CongestionLevel_value = map[string]int32{
+ "UNKNOWN_CONGESTION_LEVEL": 0,
+ "RUNNING_SMOOTHLY": 1,
+ "STOP_AND_GO": 2,
+ "CONGESTION": 3,
+ "SEVERE_CONGESTION": 4,
+}
+
+func NewVehiclePosition_CongestionLevel(x VehiclePosition_CongestionLevel) *VehiclePosition_CongestionLevel {
+ e := VehiclePosition_CongestionLevel(x)
+ return &e
+}
+func (x VehiclePosition_CongestionLevel) String() string {
+ return proto.EnumName(VehiclePosition_CongestionLevel_name, int32(x))
+}
+
+type Alert_Cause int32
+
+const (
+ Alert_UNKNOWN_CAUSE Alert_Cause = 1
+ Alert_OTHER_CAUSE Alert_Cause = 2
+ Alert_TECHNICAL_PROBLEM Alert_Cause = 3
+ Alert_STRIKE Alert_Cause = 4
+ Alert_DEMONSTRATION Alert_Cause = 5
+ Alert_ACCIDENT Alert_Cause = 6
+ Alert_HOLIDAY Alert_Cause = 7
+ Alert_WEATHER Alert_Cause = 8
+ Alert_MAINTENANCE Alert_Cause = 9
+ Alert_CONSTRUCTION Alert_Cause = 10
+ Alert_POLICE_ACTIVITY Alert_Cause = 11
+ Alert_MEDICAL_EMERGENCY Alert_Cause = 12
+)
+
+var Alert_Cause_name = map[int32]string{
+ 1: "UNKNOWN_CAUSE",
+ 2: "OTHER_CAUSE",
+ 3: "TECHNICAL_PROBLEM",
+ 4: "STRIKE",
+ 5: "DEMONSTRATION",
+ 6: "ACCIDENT",
+ 7: "HOLIDAY",
+ 8: "WEATHER",
+ 9: "MAINTENANCE",
+ 10: "CONSTRUCTION",
+ 11: "POLICE_ACTIVITY",
+ 12: "MEDICAL_EMERGENCY",
+}
+var Alert_Cause_value = map[string]int32{
+ "UNKNOWN_CAUSE": 1,
+ "OTHER_CAUSE": 2,
+ "TECHNICAL_PROBLEM": 3,
+ "STRIKE": 4,
+ "DEMONSTRATION": 5,
+ "ACCIDENT": 6,
+ "HOLIDAY": 7,
+ "WEATHER": 8,
+ "MAINTENANCE": 9,
+ "CONSTRUCTION": 10,
+ "POLICE_ACTIVITY": 11,
+ "MEDICAL_EMERGENCY": 12,
+}
+
+func NewAlert_Cause(x Alert_Cause) *Alert_Cause {
+ e := Alert_Cause(x)
+ return &e
+}
+func (x Alert_Cause) String() string {
+ return proto.EnumName(Alert_Cause_name, int32(x))
+}
+
+type Alert_Effect int32
+
+const (
+ Alert_NO_SERVICE Alert_Effect = 1
+ Alert_REDUCED_SERVICE Alert_Effect = 2
+ Alert_SIGNIFICANT_DELAYS Alert_Effect = 3
+ Alert_DETOUR Alert_Effect = 4
+ Alert_ADDITIONAL_SERVICE Alert_Effect = 5
+ Alert_MODIFIED_SERVICE Alert_Effect = 6
+ Alert_OTHER_EFFECT Alert_Effect = 7
+ Alert_UNKNOWN_EFFECT Alert_Effect = 8
+ Alert_STOP_MOVED Alert_Effect = 9
+)
+
+var Alert_Effect_name = map[int32]string{
+ 1: "NO_SERVICE",
+ 2: "REDUCED_SERVICE",
+ 3: "SIGNIFICANT_DELAYS",
+ 4: "DETOUR",
+ 5: "ADDITIONAL_SERVICE",
+ 6: "MODIFIED_SERVICE",
+ 7: "OTHER_EFFECT",
+ 8: "UNKNOWN_EFFECT",
+ 9: "STOP_MOVED",
+}
+var Alert_Effect_value = map[string]int32{
+ "NO_SERVICE": 1,
+ "REDUCED_SERVICE": 2,
+ "SIGNIFICANT_DELAYS": 3,
+ "DETOUR": 4,
+ "ADDITIONAL_SERVICE": 5,
+ "MODIFIED_SERVICE": 6,
+ "OTHER_EFFECT": 7,
+ "UNKNOWN_EFFECT": 8,
+ "STOP_MOVED": 9,
+}
+
+func NewAlert_Effect(x Alert_Effect) *Alert_Effect {
+ e := Alert_Effect(x)
+ return &e
+}
+func (x Alert_Effect) String() string {
+ return proto.EnumName(Alert_Effect_name, int32(x))
+}
+
+type TripDescriptor_ScheduleRelationship int32
+
+const (
+ TripDescriptor_SCHEDULED TripDescriptor_ScheduleRelationship = 0
+ TripDescriptor_ADDED TripDescriptor_ScheduleRelationship = 1
+ TripDescriptor_UNSCHEDULED TripDescriptor_ScheduleRelationship = 2
+ TripDescriptor_CANCELED TripDescriptor_ScheduleRelationship = 3
+ TripDescriptor_REPLACEMENT TripDescriptor_ScheduleRelationship = 5
+)
+
+var TripDescriptor_ScheduleRelationship_name = map[int32]string{
+ 0: "SCHEDULED",
+ 1: "ADDED",
+ 2: "UNSCHEDULED",
+ 3: "CANCELED",
+ 5: "REPLACEMENT",
+}
+var TripDescriptor_ScheduleRelationship_value = map[string]int32{
+ "SCHEDULED": 0,
+ "ADDED": 1,
+ "UNSCHEDULED": 2,
+ "CANCELED": 3,
+ "REPLACEMENT": 5,
+}
+
+func NewTripDescriptor_ScheduleRelationship(x TripDescriptor_ScheduleRelationship) *TripDescriptor_ScheduleRelationship {
+ e := TripDescriptor_ScheduleRelationship(x)
+ return &e
+}
+func (x TripDescriptor_ScheduleRelationship) String() string {
+ return proto.EnumName(TripDescriptor_ScheduleRelationship_name, int32(x))
+}
+
+type FeedMessage struct {
+ Header *FeedHeader `protobuf:"bytes,1,req,name=header" json:"header"`
+ Entity []*FeedEntity `protobuf:"bytes,2,rep,name=entity" json:"entity"`
+ XXX_unrecognized []byte
+}
+
+func (this *FeedMessage) Reset() { *this = FeedMessage{} }
+func (this *FeedMessage) String() string { return proto.CompactTextString(this) }
+
+type FeedHeader struct {
+ GtfsRealtimeVersion *string `protobuf:"bytes,1,req,name=gtfs_realtime_version" json:"gtfs_realtime_version"`
+ Incrementality *FeedHeader_Incrementality `protobuf:"varint,2,opt,name=incrementality,enum=transit_realtime.FeedHeader_Incrementality,def=0" json:"incrementality"`
+ Timestamp *uint64 `protobuf:"varint,3,opt,name=timestamp" json:"timestamp"`
+ XXX_unrecognized []byte
+}
+
+func (this *FeedHeader) Reset() { *this = FeedHeader{} }
+func (this *FeedHeader) String() string { return proto.CompactTextString(this) }
+
+const Default_FeedHeader_Incrementality FeedHeader_Incrementality = FeedHeader_FULL_DATASET
+
+type FeedEntity struct {
+ Id *string `protobuf:"bytes,1,req,name=id" json:"id"`
+ IsDeleted *bool `protobuf:"varint,2,opt,name=is_deleted,def=0" json:"is_deleted"`
+ TripUpdate *TripUpdate `protobuf:"bytes,3,opt,name=trip_update" json:"trip_update"`
+ Vehicle *VehiclePosition `protobuf:"bytes,4,opt,name=vehicle" json:"vehicle"`
+ Alert *Alert `protobuf:"bytes,5,opt,name=alert" json:"alert"`
+ XXX_unrecognized []byte
+}
+
+func (this *FeedEntity) Reset() { *this = FeedEntity{} }
+func (this *FeedEntity) String() string { return proto.CompactTextString(this) }
+
+const Default_FeedEntity_IsDeleted bool = false
+
+type TripUpdate struct {
+ Trip *TripDescriptor `protobuf:"bytes,1,req,name=trip" json:"trip"`
+ Vehicle *VehicleDescriptor `protobuf:"bytes,3,opt,name=vehicle" json:"vehicle"`
+ StopTimeUpdate []*TripUpdate_StopTimeUpdate `protobuf:"bytes,2,rep,name=stop_time_update" json:"stop_time_update"`
+ XXX_unrecognized []byte
+}
+
+func (this *TripUpdate) Reset() { *this = TripUpdate{} }
+func (this *TripUpdate) String() string { return proto.CompactTextString(this) }
+
+type TripUpdate_StopTimeEvent struct {
+ Delay *int32 `protobuf:"varint,1,opt,name=delay" json:"delay"`
+ Time *int64 `protobuf:"varint,2,opt,name=time" json:"time"`
+ Uncertainty *int32 `protobuf:"varint,3,opt,name=uncertainty" json:"uncertainty"`
+ XXX_unrecognized []byte
+}
+
+func (this *TripUpdate_StopTimeEvent) Reset() { *this = TripUpdate_StopTimeEvent{} }
+func (this *TripUpdate_StopTimeEvent) String() string { return proto.CompactTextString(this) }
+
+type TripUpdate_StopTimeUpdate struct {
+ StopSequence *uint32 `protobuf:"varint,1,opt,name=stop_sequence" json:"stop_sequence"`
+ StopId *string `protobuf:"bytes,4,opt,name=stop_id" json:"stop_id"`
+ Arrival *TripUpdate_StopTimeEvent `protobuf:"bytes,2,opt,name=arrival" json:"arrival"`
+ Departure *TripUpdate_StopTimeEvent `protobuf:"bytes,3,opt,name=departure" json:"departure"`
+ ScheduleRelationship *TripUpdate_StopTimeUpdate_ScheduleRelationship `protobuf:"varint,5,opt,name=schedule_relationship,enum=transit_realtime.TripUpdate_StopTimeUpdate_ScheduleRelationship,def=0" json:"schedule_relationship"`
+ XXX_unrecognized []byte
+}
+
+func (this *TripUpdate_StopTimeUpdate) Reset() { *this = TripUpdate_StopTimeUpdate{} }
+func (this *TripUpdate_StopTimeUpdate) String() string { return proto.CompactTextString(this) }
+
+const Default_TripUpdate_StopTimeUpdate_ScheduleRelationship TripUpdate_StopTimeUpdate_ScheduleRelationship = TripUpdate_StopTimeUpdate_SCHEDULED
+
+type VehiclePosition struct {
+ Trip *TripDescriptor `protobuf:"bytes,1,opt,name=trip" json:"trip"`
+ Vehicle *VehicleDescriptor `protobuf:"bytes,8,opt,name=vehicle" json:"vehicle"`
+ Position *Position `protobuf:"bytes,2,opt,name=position" json:"position"`
+ CurrentStopSequence *uint32 `protobuf:"varint,3,opt,name=current_stop_sequence" json:"current_stop_sequence"`
+ StopId *string `protobuf:"bytes,7,opt,name=stop_id" json:"stop_id"`
+ CurrentStatus *VehiclePosition_VehicleStopStatus `protobuf:"varint,4,opt,name=current_status,enum=transit_realtime.VehiclePosition_VehicleStopStatus,def=2" json:"current_status"`
+ Timestamp *uint64 `protobuf:"varint,5,opt,name=timestamp" json:"timestamp"`
+ CongestionLevel *VehiclePosition_CongestionLevel `protobuf:"varint,6,opt,name=congestion_level,enum=transit_realtime.VehiclePosition_CongestionLevel" json:"congestion_level"`
+ XXX_unrecognized []byte
+}
+
+func (this *VehiclePosition) Reset() { *this = VehiclePosition{} }
+func (this *VehiclePosition) String() string { return proto.CompactTextString(this) }
+
+const Default_VehiclePosition_CurrentStatus VehiclePosition_VehicleStopStatus = VehiclePosition_IN_TRANSIT_TO
+
+type Alert struct {
+ ActivePeriod []*TimeRange `protobuf:"bytes,1,rep,name=active_period" json:"active_period"`
+ InformedEntity []*EntitySelector `protobuf:"bytes,5,rep,name=informed_entity" json:"informed_entity"`
+ Cause *Alert_Cause `protobuf:"varint,6,opt,name=cause,enum=transit_realtime.Alert_Cause,def=1" json:"cause"`
+ Effect *Alert_Effect `protobuf:"varint,7,opt,name=effect,enum=transit_realtime.Alert_Effect,def=8" json:"effect"`
+ Url *TranslatedString `protobuf:"bytes,8,opt,name=url" json:"url"`
+ HeaderText *TranslatedString `protobuf:"bytes,10,opt,name=header_text" json:"header_text"`
+ DescriptionText *TranslatedString `protobuf:"bytes,11,opt,name=description_text" json:"description_text"`
+ XXX_unrecognized []byte
+}
+
+func (this *Alert) Reset() { *this = Alert{} }
+func (this *Alert) String() string { return proto.CompactTextString(this) }
+
+const Default_Alert_Cause Alert_Cause = Alert_UNKNOWN_CAUSE
+const Default_Alert_Effect Alert_Effect = Alert_UNKNOWN_EFFECT
+
+type TimeRange struct {
+ Start *uint64 `protobuf:"varint,1,opt,name=start" json:"start"`
+ End *uint64 `protobuf:"varint,2,opt,name=end" json:"end"`
+ XXX_unrecognized []byte
+}
+
+func (this *TimeRange) Reset() { *this = TimeRange{} }
+func (this *TimeRange) String() string { return proto.CompactTextString(this) }
+
+type Position struct {
+ Latitude *float32 `protobuf:"fixed32,1,req,name=latitude" json:"latitude"`
+ Longitude *float32 `protobuf:"fixed32,2,req,name=longitude" json:"longitude"`
+ Bearing *float32 `protobuf:"fixed32,3,opt,name=bearing" json:"bearing"`
+ Odometer *float64 `protobuf:"fixed64,4,opt,name=odometer" json:"odometer"`
+ Speed *float32 `protobuf:"fixed32,5,opt,name=speed" json:"speed"`
+ XXX_unrecognized []byte
+}
+
+func (this *Position) Reset() { *this = Position{} }
+func (this *Position) String() string { return proto.CompactTextString(this) }
+
+type TripDescriptor struct {
+ TripId *string `protobuf:"bytes,1,opt,name=trip_id" json:"trip_id"`
+ RouteId *string `protobuf:"bytes,5,opt,name=route_id" json:"route_id"`
+ StartTime *string `protobuf:"bytes,2,opt,name=start_time" json:"start_time"`
+ StartDate *string `protobuf:"bytes,3,opt,name=start_date" json:"start_date"`
+ ScheduleRelationship *TripDescriptor_ScheduleRelationship `protobuf:"varint,4,opt,name=schedule_relationship,enum=transit_realtime.TripDescriptor_ScheduleRelationship" json:"schedule_relationship"`
+ XXX_unrecognized []byte
+}
+
+func (this *TripDescriptor) Reset() { *this = TripDescriptor{} }
+func (this *TripDescriptor) String() string { return proto.CompactTextString(this) }
+
+type VehicleDescriptor struct {
+ Id *string `protobuf:"bytes,1,opt,name=id" json:"id"`
+ Label *string `protobuf:"bytes,2,opt,name=label" json:"label"`
+ LicensePlate *string `protobuf:"bytes,3,opt,name=license_plate" json:"license_plate"`
+ XXX_unrecognized []byte
+}
+
+func (this *VehicleDescriptor) Reset() { *this = VehicleDescriptor{} }
+func (this *VehicleDescriptor) String() string { return proto.CompactTextString(this) }
+
+type EntitySelector struct {
+ AgencyId *string `protobuf:"bytes,1,opt,name=agency_id" json:"agency_id"`
+ RouteId *string `protobuf:"bytes,2,opt,name=route_id" json:"route_id"`
+ RouteType *int32 `protobuf:"varint,3,opt,name=route_type" json:"route_type"`
+ Trip *TripDescriptor `protobuf:"bytes,4,opt,name=trip" json:"trip"`
+ StopId *string `protobuf:"bytes,5,opt,name=stop_id" json:"stop_id"`
+ XXX_unrecognized []byte
+}
+
+func (this *EntitySelector) Reset() { *this = EntitySelector{} }
+func (this *EntitySelector) String() string { return proto.CompactTextString(this) }
+
+type TranslatedString struct {
+ Translation []*TranslatedString_Translation `protobuf:"bytes,1,rep,name=translation" json:"translation"`
+ XXX_unrecognized []byte
+}
+
+func (this *TranslatedString) Reset() { *this = TranslatedString{} }
+func (this *TranslatedString) String() string { return proto.CompactTextString(this) }
+
+type TranslatedString_Translation struct {
+ Text *string `protobuf:"bytes,1,req,name=text" json:"text"`
+ Language *string `protobuf:"bytes,2,opt,name=language" json:"language"`
+ XXX_unrecognized []byte
+}
+
+func (this *TranslatedString_Translation) Reset() { *this = TranslatedString_Translation{} }
+func (this *TranslatedString_Translation) String() string { return proto.CompactTextString(this) }
+
+func init() {
+ proto.RegisterEnum("transit_realtime.FeedHeader_Incrementality", FeedHeader_Incrementality_name, FeedHeader_Incrementality_value)
+ proto.RegisterEnum("transit_realtime.TripUpdate_StopTimeUpdate_ScheduleRelationship", TripUpdate_StopTimeUpdate_ScheduleRelationship_name, TripUpdate_StopTimeUpdate_ScheduleRelationship_value)
+ proto.RegisterEnum("transit_realtime.VehiclePosition_VehicleStopStatus", VehiclePosition_VehicleStopStatus_name, VehiclePosition_VehicleStopStatus_value)
+ proto.RegisterEnum("transit_realtime.VehiclePosition_CongestionLevel", VehiclePosition_CongestionLevel_name, VehiclePosition_CongestionLevel_value)
+ proto.RegisterEnum("transit_realtime.Alert_Cause", Alert_Cause_name, Alert_Cause_value)
+ proto.RegisterEnum("transit_realtime.Alert_Effect", Alert_Effect_name, Alert_Effect_value)
+ proto.RegisterEnum("transit_realtime.TripDescriptor_ScheduleRelationship", TripDescriptor_ScheduleRelationship_name, TripDescriptor_ScheduleRelationship_value)
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/Makefile b/vendor/github.com/mattermost/rsc/imap/Makefile
new file mode 100644
index 000000000..8dd3d2be4
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/Makefile
@@ -0,0 +1,15 @@
+include $(GOROOT)/src/Make.inc
+
+# TARG=code.google.com/p/rsc/imap
+
+TARG=rsc.googlecode.com/hg/imap
+GOFILES=\
+ decode.go\
+ imap.go\
+ mail.go\
+ sx.go\
+ tcs.go\
+
+GCIMPORTS=-I$(GOPATH)/pkg/$(GOOS)_$(GOARCH)
+
+include $(GOROOT)/src/Make.pkg
diff --git a/vendor/github.com/mattermost/rsc/imap/decode.go b/vendor/github.com/mattermost/rsc/imap/decode.go
new file mode 100644
index 000000000..ebeb1d7d7
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/decode.go
@@ -0,0 +1,227 @@
+package imap
+
+import (
+ "bytes"
+ "encoding/base64"
+ "strings"
+ "unicode"
+)
+
+func decode2047chunk(s string) (conv []byte, rest string, ok bool) {
+ // s is =?...
+ // and should be =?charset?e?text?=
+ j := strings.Index(s[2:], "?")
+ if j < 0 {
+ return
+ }
+ j += 2
+ if j+2 >= len(s) || s[j+2] != '?' {
+ return
+ }
+ k := strings.Index(s[j+3:], "?=")
+ if k < 0 {
+ return
+ }
+ k += j + 3
+
+ charset, enc, text, rest := s[2:j], s[j+1], s[j+3:k], s[k+2:]
+ var encoding string
+ switch enc {
+ default:
+ return
+ case 'q', 'Q':
+ encoding = "quoted-printable"
+ case 'b', 'B':
+ encoding = "base64"
+ }
+
+ dat := decodeText([]byte(text), encoding, charset, true)
+ if dat == nil {
+ return
+ }
+ return dat, rest, true
+}
+
+func decodeQP(dat []byte, underscore bool) []byte {
+ out := make([]byte, len(dat))
+ w := 0
+ for i := 0; i < len(dat); i++ {
+ c := dat[i]
+ if underscore && c == '_' {
+ out[w] = ' '
+ w++
+ continue
+ }
+ if c == '\r' {
+ continue
+ }
+ if c == '=' {
+ if i+1 < len(dat) && dat[i+1] == '\n' {
+ i++
+ continue
+ }
+ if i+2 < len(dat) && dat[i+1] == '\r' && dat[i+2] == '\n' {
+ i += 2
+ continue
+ }
+ if i+2 < len(dat) {
+ v := unhex(dat[i+1])<<4 | unhex(dat[i+2])
+ if v >= 0 {
+ out[w] = byte(v)
+ w++
+ i += 2
+ continue
+ }
+ }
+ }
+ out[w] = c
+ w++
+ }
+ return out[:w]
+}
+
+func nocrnl(dat []byte) []byte {
+ w := 0
+ for _, c := range dat {
+ if c != '\r' && c != '\n' {
+ dat[w] = c
+ w++
+ }
+ }
+ return dat[:w]
+}
+
+func decode64(dat []byte) []byte {
+ out := make([]byte, len(dat))
+ copy(out, dat)
+ out = nocrnl(out)
+ n, err := base64.StdEncoding.Decode(out, out)
+ if err != nil {
+ return nil
+ }
+ return out[:n]
+}
+
+func decodeText(dat []byte, encoding, charset string, underscore bool) []byte {
+ odat := dat
+ switch strlwr(encoding) {
+ case "quoted-printable":
+ dat = decodeQP(dat, underscore)
+ case "base64":
+ dat = decode64(dat)
+ }
+ if dat == nil {
+ return nil
+ }
+ if bytes.IndexByte(dat, '\r') >= 0 {
+ if &odat[0] == &dat[0] {
+ dat = append([]byte(nil), dat...)
+ }
+ dat = nocr(dat)
+ }
+
+ charset = strlwr(charset)
+ if charset == "utf-8" || charset == "us-ascii" {
+ return dat
+ }
+ if charset == "iso-8859-1" {
+ // Avoid allocation for iso-8859-1 that is really just ascii.
+ for _, c := range dat {
+ if c >= 0x80 {
+ goto NeedConv
+ }
+ }
+ return dat
+ NeedConv:
+ }
+
+ // TODO: big5, iso-2022-jp
+
+ tab := convtab[charset]
+ if tab == nil {
+ return dat
+ }
+ var b bytes.Buffer
+ for _, c := range dat {
+ if tab[c] < 0 {
+ b.WriteRune(unicode.ReplacementChar)
+ } else {
+ b.WriteRune(tab[c])
+ }
+ }
+ return b.Bytes()
+}
+
+var convtab = map[string]*[256]rune{
+ "iso-8859-1": &tab_iso8859_1,
+ "iso-8859-2": &tab_iso8859_2,
+ "iso-8859-3": &tab_iso8859_3,
+ "iso-8859-4": &tab_iso8859_4,
+ "iso-8859-5": &tab_iso8859_5,
+ "iso-8859-6": &tab_iso8859_6,
+ "iso-8859-7": &tab_iso8859_7,
+ "iso-8859-8": &tab_iso8859_8,
+ "iso-8859-9": &tab_iso8859_9,
+ "iso-8859-10": &tab_iso8859_10,
+ "iso-8859-15": &tab_iso8859_15,
+ "koi8-r": &tab_koi8,
+ "windows-1250": &tab_cp1250,
+ "windows-1251": &tab_cp1251,
+ "windows-1252": &tab_cp1252,
+ "windows-1253": &tab_cp1253,
+ "windows-1254": &tab_cp1254,
+ "windows-1255": &tab_cp1255,
+ "windows-1256": &tab_cp1256,
+ "windows-1257": &tab_cp1257,
+ "windows-1258": &tab_cp1258,
+}
+
+func unrfc2047(s string) string {
+ if !strings.Contains(s, "=?") {
+ return s
+ }
+ var buf bytes.Buffer
+ for {
+ // =?charset?e?text?=
+ i := strings.Index(s, "=?")
+ if i < 0 {
+ break
+ }
+ conv, rest, ok := decode2047chunk(s[i:])
+ if !ok {
+ buf.WriteString(s[:i+2])
+ s = s[i+2:]
+ continue
+ }
+ buf.WriteString(s[:i])
+ buf.Write(conv)
+ s = rest
+ }
+ buf.WriteString(s)
+ return buf.String()
+}
+
+func lwr(c rune) rune {
+ if 'A' <= c && c <= 'Z' {
+ return c + 'a' - 'A'
+ }
+ return c
+}
+
+func strlwr(s string) string {
+ return strings.Map(lwr, s)
+}
+
+func unhex(c byte) int {
+ switch {
+ case '0' <= c && c <= '9':
+ return int(c) - '0'
+ case 'a' <= c && c <= 'f':
+ return int(c) - 'a' + 10
+ case 'A' <= c && c <= 'F':
+ return int(c) - 'A' + 10
+ }
+ return -1
+}
+
+// TODO: Will need modified UTF-7 eventually.
diff --git a/vendor/github.com/mattermost/rsc/imap/decode_test.go b/vendor/github.com/mattermost/rsc/imap/decode_test.go
new file mode 100644
index 000000000..84c31f63a
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/decode_test.go
@@ -0,0 +1,26 @@
+package imap
+
+import "testing"
+
+var unrfc2047Tests = []struct {
+ in, out string
+}{
+ {"hello world", "hello world"},
+ {"hello =?iso-8859-1?q?this is some text?=", "hello this is some text"},
+ {"=?US-ASCII?Q?Keith_Moore?=", "Keith Moore"},
+ {"=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=", "Keld Jørn Simonsen"},
+ {"=?ISO-8859-1?Q?Andr=E9?= Pirard", "André Pirard"},
+ {"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=", "If you can read this yo"},
+ {"=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=", "u understand the example."},
+ {"=?ISO-8859-1?Q?Olle_J=E4rnefors?=", "Olle Järnefors"},
+ // {"=?iso-2022-jp?B?GyRCTTVKISRKP006SiRyS34kPyQ3JEZKcz03JCIkahsoQg==?=", ""},
+ {"=?UTF-8?B?Ik5pbHMgTy4gU2Vsw6VzZGFsIg==?=", `"Nils O. Selåsdal"`},
+}
+
+func TestUnrfc2047(t *testing.T) {
+ for _, tt := range unrfc2047Tests {
+ if out := unrfc2047(tt.in); out != tt.out {
+ t.Errorf("unrfc2047(%#q) = %#q, want %#q", tt.in, out, tt.out)
+ }
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/imap.go b/vendor/github.com/mattermost/rsc/imap/imap.go
new file mode 100644
index 000000000..6555984d2
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/imap.go
@@ -0,0 +1,1110 @@
+package imap
+
+import (
+ "bufio"
+ "bytes"
+ "crypto/md5"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "os"
+ "os/exec"
+ "strconv"
+ "strings"
+ "sync"
+)
+
+var Debug = false
+
+const tag = "#"
+
+// A Mode specifies the IMAP connection mode.
+type Mode int
+
+const (
+ Unencrypted Mode = iota // unencrypted TCP connection
+ StartTLS // use IMAP STARTTLS command - unimplemented!
+ TLS // direct TLS connection
+ Command // exec shell command (server name)
+)
+
+type lock struct {
+ locked bool
+ mu sync.Mutex
+}
+
+func (l *lock) lock() {
+ l.mu.Lock()
+ l.locked = true
+}
+
+func (l *lock) unlock() {
+ l.mustBeLocked()
+ l.locked = false
+ l.mu.Unlock()
+}
+
+func (l *lock) mustBeLocked() {
+ if !l.locked {
+ panic("not locked")
+ }
+}
+
+type Client struct {
+ server string
+ user string
+ passwd string
+ mode Mode
+ root string
+
+ io lock
+ rw io.ReadWriteCloser // i/o to server
+ b *bufio.Reader // buffered rw
+ autoReconnect bool // reconnect on failure
+ connected bool // rw is active
+
+ data lock
+ capability map[string]bool
+ flags Flags
+ boxByName map[string]*Box // all known boxes
+ allBox []*Box // all known boxes (do we need this?)
+ rootBox *Box // root of box tree
+ inbox *Box // inbox (special, not in tree)
+ box *Box // selected (current) box
+ nextBox *Box // next box to select (do we need this?)
+}
+
+func NewClient(mode Mode, server, user, passwd string, root string) (*Client, error) {
+ c := &Client{
+ server: server,
+ user: user,
+ passwd: passwd,
+ mode: mode,
+ root: root,
+ boxByName: map[string]*Box{},
+ }
+ c.io.lock()
+ if err := c.reconnect(); err != nil {
+ return nil, err
+ }
+ c.autoReconnect = true
+ c.io.unlock()
+
+ return c, nil
+}
+
+func (c *Client) Close() error {
+ c.io.lock()
+ c.autoReconnect = false
+ c.connected = false
+ if c.rw != nil {
+ c.rw.Close()
+ c.rw = nil
+ }
+ c.io.unlock()
+ return nil
+}
+
+func (c *Client) reconnect() error {
+ c.io.mustBeLocked()
+ c.autoReconnect = false
+ if c.rw != nil {
+ c.rw.Close()
+ c.rw = nil
+ }
+
+ if Debug {
+ log.Printf("dial %s...", c.server)
+ }
+ rw, err := dial(c.server, c.mode)
+ if err != nil {
+ return err
+ }
+
+ c.rw = rw
+ c.connected = true
+ c.capability = nil
+ c.box = nil
+ if Debug {
+ c.b = bufio.NewReader(&tee{rw, os.Stderr})
+ } else {
+ c.b = bufio.NewReader(rw)
+ }
+ x, err := c.rdsx()
+ if x == nil {
+ err = fmt.Errorf("no greeting from %s: %v", c.server, err)
+ goto Error
+ }
+ if len(x.sx) < 2 || !x.sx[0].isAtom("*") || !x.sx[1].isAtom("PREAUTH") {
+ if !x.ok() {
+ err = fmt.Errorf("bad greeting - %s", x)
+ goto Error
+ }
+ if err = c.login(); err != nil {
+ goto Error
+ }
+ }
+ if c.capability == nil {
+ if err = c.cmd(nil, "CAPABILITY"); err != nil {
+ goto Error
+ }
+ if c.capability == nil {
+ err = fmt.Errorf("CAPABILITY command did not return capability list")
+ goto Error
+ }
+ }
+ if err := c.getBoxes(); err != nil {
+ goto Error
+ }
+ c.autoReconnect = true
+ return nil
+
+Error:
+ if c.rw != nil {
+ c.rw.Close()
+ c.rw = nil
+ }
+ c.autoReconnect = true
+ c.connected = false
+ return err
+}
+
+var testDial func(string, Mode) (io.ReadWriteCloser, error)
+
+func dial(server string, mode Mode) (io.ReadWriteCloser, error) {
+ if testDial != nil {
+ return testDial(server, mode)
+ }
+ switch mode {
+ default:
+ // also case Unencrypted
+ return net.Dial("tcp", server+":143")
+ case StartTLS:
+ return nil, fmt.Errorf("StartTLS not supported")
+ case TLS:
+ return tls.Dial("tcp", server+":993", nil)
+ case Command:
+ cmd := exec.Command("sh", "-c", server)
+ cmd.Stderr = os.Stderr
+ r, err := cmd.StdoutPipe()
+ if err != nil {
+ return nil, err
+ }
+ w, err := cmd.StdinPipe()
+ if err != nil {
+ r.Close()
+ return nil, err
+ }
+ if err := cmd.Start(); err != nil {
+ r.Close()
+ w.Close()
+ return nil, err
+ }
+ return &pipe2{r, w}, nil
+ }
+ panic("not reached")
+}
+
+type pipe2 struct {
+ io.ReadCloser
+ io.WriteCloser
+}
+
+func (p *pipe2) Close() error {
+ p.ReadCloser.Close()
+ p.WriteCloser.Close()
+ return nil
+}
+
+type tee struct {
+ r io.Reader
+ w io.Writer
+}
+
+func (t tee) Read(p []byte) (n int, err error) {
+ n, err = t.r.Read(p)
+ if n > 0 {
+ t.w.Write(p[0:n])
+ }
+ return
+}
+
+func (c *Client) rdsx() (*sx, error) {
+ c.io.mustBeLocked()
+ return rdsx(c.b)
+}
+
+type sxError struct {
+ x *sx
+}
+
+func (e *sxError) Error() string { return e.x.String() }
+
+func (c *Client) cmd(b *Box, format string, args ...interface{}) error {
+ x, err := c.cmdsx(b, format, args...)
+ if err != nil {
+ return err
+ }
+ if !x.ok() {
+ return &sxError{x}
+ }
+ return nil
+}
+
+// cmdsx0 runs a single command and return the sx. Does not redial.
+func (c *Client) cmdsx0(format string, args ...interface{}) (*sx, error) {
+ c.io.mustBeLocked()
+ if c.rw == nil || !c.connected {
+ return nil, fmt.Errorf("not connected")
+ }
+
+ cmd := fmt.Sprintf(format, args...)
+ if Debug {
+ fmt.Fprintf(os.Stderr, ">>> %s %s\n", tag, cmd)
+ }
+ if _, err := fmt.Fprintf(c.rw, "%s %s\r\n", tag, cmd); err != nil {
+ c.connected = false
+ return nil, err
+ }
+ return c.waitsx()
+}
+
+// cmdsx runs a command on box b. It does redial.
+func (c *Client) cmdsx(b *Box, format string, args ...interface{}) (*sx, error) {
+ c.io.mustBeLocked()
+ c.nextBox = b
+
+Trying:
+ for tries := 0; ; tries++ {
+ if c.rw == nil || !c.connected {
+ if !c.autoReconnect {
+ return nil, fmt.Errorf("not connected")
+ }
+ if err := c.reconnect(); err != nil {
+ return nil, err
+ }
+ if b != nil && c.nextBox == nil {
+ // box disappeared on reconnect
+ return nil, fmt.Errorf("box is gone")
+ }
+ }
+
+ if b != nil && b != c.box {
+ if c.box != nil {
+ // TODO c.box.init = false
+ }
+ c.box = b
+ if _, err := c.cmdsx0("SELECT %s", iquote(b.Name)); err != nil {
+ c.box = nil
+ if tries++; tries == 1 && (c.rw == nil || !c.connected) {
+ continue Trying
+ }
+ return nil, err
+ }
+ }
+
+ x, err := c.cmdsx0(format, args...)
+ if err != nil {
+ if tries++; tries == 1 && (c.rw == nil || !c.connected) {
+ continue Trying
+ }
+ return nil, err
+ }
+ return x, nil
+ }
+ panic("not reached")
+}
+
+func (c *Client) waitsx() (*sx, error) {
+ c.io.mustBeLocked()
+ for {
+ x, err := c.rdsx()
+ if err != nil {
+ c.connected = false
+ return nil, err
+ }
+ if len(x.sx) >= 1 && x.sx[0].kind == sxAtom {
+ if x.sx[0].isAtom(tag) {
+ return x, nil
+ }
+ if x.sx[0].isAtom("*") {
+ c.unexpected(x)
+ }
+ }
+ if x.kind == sxList && len(x.sx) == 0 {
+ c.connected = false
+ return nil, fmt.Errorf("empty response")
+ }
+ }
+ panic("not reached")
+}
+
+func iquote(s string) string {
+ if s == "" {
+ return `""`
+ }
+
+ for i := 0; i < len(s); i++ {
+ if s[i] >= 0x80 || s[i] <= ' ' || s[i] == '\\' || s[i] == '"' {
+ goto Quote
+ }
+ }
+ return s
+
+Quote:
+ var b bytes.Buffer
+ b.WriteByte('"')
+ for i := 0; i < len(s); i++ {
+ if s[i] == '\\' || s[i] == '"' {
+ b.WriteByte('\\')
+ }
+ b.WriteByte(s[i])
+ }
+ b.WriteByte('"')
+ return b.String()
+}
+
+func (c *Client) login() error {
+ c.io.mustBeLocked()
+ x, err := c.cmdsx(nil, "LOGIN %s %s", iquote(c.user), iquote(c.passwd))
+ if err != nil {
+ return err
+ }
+ if !x.ok() {
+ return fmt.Errorf("login rejected: %s", x)
+ }
+ return nil
+}
+
+func (c *Client) getBoxes() error {
+ c.io.mustBeLocked()
+ for _, b := range c.allBox {
+ b.dead = true
+ // b.exists = 0
+ // b.maxSeen = 0
+ }
+ list := "LIST"
+ if c.capability["XLIST"] { // Gmail extension
+ list = "XLIST"
+ }
+ if err := c.cmd(nil, "%s %s *", list, iquote(c.root)); err != nil {
+ return err
+ }
+ if err := c.cmd(nil, "%s %s INBOX", list, iquote(c.root)); err != nil {
+ return err
+ }
+ if c.nextBox != nil && c.nextBox.dead {
+ c.nextBox = nil
+ }
+ for _, b := range c.allBox {
+ if b.dead {
+ delete(c.boxByName, b.Name)
+ }
+ b.firstNum = 0
+ }
+ c.allBox = boxTrim(c.allBox)
+ for _, b := range c.allBox {
+ b.child = boxTrim(b.child)
+ }
+ return nil
+}
+
+func boxTrim(list []*Box) []*Box {
+ w := 0
+ for _, b := range list {
+ if !b.dead {
+ list[w] = b
+ w++
+ }
+ }
+ return list[:w]
+}
+
+const maxFetch = 1000
+
+func (c *Client) setAutoReconnect(b bool) {
+ c.autoReconnect = b
+}
+
+func (c *Client) check(b *Box) error {
+ c.io.mustBeLocked()
+ if b.dead {
+ return fmt.Errorf("box is gone")
+ }
+
+ b.load = true
+
+ // Update exists count.
+ if err := c.cmd(b, "NOOP"); err != nil {
+ return err
+ }
+
+ // Have to get through this in one session.
+ // Caller can call again if we get disconnected
+ // and return an error.
+ c.autoReconnect = false
+ defer c.setAutoReconnect(true)
+
+ // First load after reconnect: figure out what changed.
+ if b.firstNum == 0 && len(b.msgByUID) > 0 {
+ var lo, hi uint32 = 1<<32 - 1, 0
+ for _, m := range b.msgByUID {
+ m.dead = true
+ uid := uint32(m.UID)
+ if lo > uid {
+ lo = uid
+ }
+ if hi < uid {
+ hi = uid
+ }
+ m.num = 0
+ }
+ if err := c.cmd(b, "UID FETCH %d:%d FLAGS", lo, hi); err != nil {
+ return err
+ }
+ for _, m := range b.msgByUID {
+ if m.dead {
+ delete(b.msgByUID, m.UID)
+ }
+ }
+ }
+
+ // First-ever load.
+ if b.firstNum == 0 {
+ if b.exists <= maxFetch {
+ b.firstNum = 1
+ } else {
+ b.firstNum = b.exists - maxFetch + 1
+ }
+ n := b.exists - b.firstNum + 1
+ b.msgByNum = make([]*Msg, n)
+ return c.fetchBox(b, b.firstNum, 0)
+ }
+
+ if b.exists <= b.maxSeen {
+ return nil
+ }
+ return c.fetchBox(b, b.maxSeen, 0)
+}
+
+func (c *Client) fetchBox(b *Box, lo int, hi int) error {
+ c.io.mustBeLocked()
+ if b != c.box {
+ if err := c.cmd(b, "NOOP"); err != nil {
+ return err
+ }
+ }
+ extra := ""
+ if c.IsGmail() {
+ extra = " X-GM-MSGID X-GM-THRID X-GM-LABELS"
+ }
+ slo := fmt.Sprint(lo)
+ shi := "*"
+ if hi > 0 {
+ shi = fmt.Sprint(hi)
+ }
+ return c.cmd(b, "FETCH %s:%s (FLAGS UID INTERNALDATE RFC822.SIZE ENVELOPE BODY%s)", slo, shi, extra)
+}
+
+func (c *Client) IsGmail() bool {
+ return c.capability["X-GM-EXT-1"]
+}
+
+// Table-driven IMAP "unexpected response" parser.
+// All the interesting data is in the unexpected responses.
+
+var unextab = []struct {
+ num int
+ name string
+ fmt string
+ fn func(*Client, *sx)
+}{
+ {0, "BYE", "", xbye},
+ {0, "CAPABILITY", "", xcapability},
+ {0, "FLAGS", "AAL", xflags},
+ {0, "LIST", "AALSS", xlist},
+ {0, "XLIST", "AALSS", xlist},
+ {0, "OK", "", xok},
+ // {0, "SEARCH", "AAN*", xsearch},
+ {1, "EXISTS", "ANA", xexists},
+ {1, "EXPUNGE", "ANA", xexpunge},
+ {1, "FETCH", "ANAL", xfetch},
+ // {1, "RECENT", "ANA", xrecent}, // why do we care?
+}
+
+func (c *Client) unexpected(x *sx) {
+ c.io.mustBeLocked()
+ var num int
+ var name string
+
+ if len(x.sx) >= 3 && x.sx[1].kind == sxNumber && x.sx[2].kind == sxAtom {
+ num = 1
+ name = string(x.sx[2].data)
+ } else if len(x.sx) >= 2 && x.sx[1].kind == sxAtom {
+ num = 0
+ name = string(x.sx[1].data)
+ } else {
+ return
+ }
+
+ c.data.lock()
+ for _, t := range unextab {
+ if t.num == num && strings.EqualFold(t.name, name) {
+ if t.fmt != "" && !x.match(t.fmt) {
+ log.Printf("malformd %s: %s", name, x)
+ continue
+ }
+ t.fn(c, x)
+ }
+ }
+ c.data.unlock()
+}
+
+func xbye(c *Client, x *sx) {
+ c.io.mustBeLocked()
+ c.rw.Close()
+ c.rw = nil
+ c.connected = false
+}
+
+func xflags(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ // This response contains in x.sx[2] the list of flags
+ // that can be validly attached to messages in c.box.
+ if b := c.box; b != nil {
+ c.flags = x.sx[2].parseFlags()
+ }
+}
+
+func xcapability(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ c.capability = make(map[string]bool)
+ for _, xx := range x.sx[2:] {
+ if xx.kind == sxAtom {
+ c.capability[string(xx.data)] = true
+ }
+ }
+}
+
+func xlist(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ s := string(x.sx[4].data)
+ t := string(x.sx[3].data)
+
+ // INBOX is the special name for the main mailbox.
+ // All the other mailbox names have the root prefix removed, if applicable.
+ inbox := strings.EqualFold(s, "inbox")
+ if inbox {
+ s = "inbox"
+ }
+
+ b := c.newBox(s, t, inbox)
+ if b == nil {
+ return
+ }
+ if inbox {
+ c.inbox = b
+ }
+ if s == c.root {
+ c.rootBox = b
+ }
+ b.dead = false
+ b.flags = x.sx[2].parseFlags()
+}
+
+func xexists(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ if b := c.box; b != nil {
+ b.exists = int(x.sx[1].number)
+ if b.exists < b.maxSeen {
+ b.maxSeen = b.exists
+ }
+ }
+}
+
+func xexpunge(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ if b := c.box; b != nil {
+ n := int(x.sx[1].number)
+ bynum := b.msgByNum
+ if bynum != nil {
+ if n < b.firstNum {
+ b.firstNum--
+ } else if n < b.firstNum+len(bynum) {
+ copy(bynum[n-b.firstNum:], bynum[n-b.firstNum+1:])
+ b.msgByNum = bynum[:len(bynum)-1]
+ } else {
+ log.Printf("expunge unexpected message %d %d %d", b.firstNum, b.exists, b.firstNum+len(bynum))
+ }
+ }
+ if n <= b.exists {
+ b.exists--
+ }
+ }
+}
+
+// Table-driven OK info parser.
+
+var oktab = []struct {
+ name string
+ kind sxKind
+ fn func(*Client, *Box, *sx)
+}{
+ {"UIDVALIDITY", sxNumber, xokuidvalidity},
+ {"PERMANENTFLAGS", sxList, xokpermflags},
+ {"UNSEEN", sxNumber, xokunseen},
+ {"READ-WRITE", 0, xokreadwrite},
+ {"READ-ONLY", 0, xokreadonly},
+}
+
+func xok(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ b := c.box
+ if b == nil {
+ return
+ }
+ if len(x.sx) >= 4 && x.sx[2].kind == sxAtom && x.sx[2].data[0] == '[' {
+ var arg *sx
+ if x.sx[3].kind == sxAtom && x.sx[3].data[0] == ']' {
+ arg = nil
+ } else if x.sx[4].kind == sxAtom && x.sx[4].data[0] == ']' {
+ arg = x.sx[3]
+ } else {
+ log.Printf("cannot parse OK: %s", x)
+ return
+ }
+ x.sx[2].data = x.sx[2].data[1:]
+ for _, t := range oktab {
+ if x.sx[2].isAtom(t.name) {
+ if t.kind != 0 && (arg == nil || arg.kind != t.kind) {
+ log.Printf("malformed %s: %s", t.name, arg)
+ continue
+ }
+ t.fn(c, b, arg)
+ }
+ }
+ }
+}
+
+func xokuidvalidity(c *Client, b *Box, x *sx) {
+ c.data.mustBeLocked()
+ n := uint32(x.number)
+ if b.validity != n {
+ if b.msgByUID != nil {
+ log.Printf("imap: UID validity reset for %s", b.Name)
+ }
+ b.validity = n
+ b.maxSeen = 0
+ b.firstNum = 0
+ b.msgByNum = nil
+ b.msgByUID = nil
+ }
+}
+
+func xokpermflags(c *Client, b *Box, x *sx) {
+ c.data.mustBeLocked()
+ b.permFlags = x.parseFlags()
+}
+
+func xokunseen(c *Client, b *Box, x *sx) {
+ c.data.mustBeLocked()
+ b.unseen = int(x.number)
+}
+
+func xokreadwrite(c *Client, b *Box, x *sx) {
+ c.data.mustBeLocked()
+ b.readOnly = false
+}
+
+func xokreadonly(c *Client, b *Box, x *sx) {
+ c.data.mustBeLocked()
+ b.readOnly = true
+}
+
+// Table-driven FETCH message info parser.
+
+var msgtab = []struct {
+ name string
+ fn func(*Msg, *sx, *sx)
+}{
+ {"FLAGS", xmsgflags},
+ {"INTERNALDATE", xmsgdate},
+ {"RFC822.SIZE", xmsgrfc822size},
+ {"ENVELOPE", xmsgenvelope},
+ {"X-GM-MSGID", xmsggmmsgid},
+ {"X-GM-THRID", xmsggmthrid},
+ {"BODY", xmsgbody},
+ {"BODY[", xmsgbodydata},
+}
+
+func xfetch(c *Client, x *sx) {
+ c.data.mustBeLocked()
+ if c.box == nil {
+ log.Printf("FETCH but no open box: %s", x)
+ return
+ }
+
+ // * 152 FETCH (UID 185 FLAGS() ...)
+ n := x.sx[1].number
+ xx := x.sx[3]
+ if len(xx.sx)%2 != 0 {
+ log.Printf("malformed FETCH: %s", x)
+ return
+ }
+ var uid uint64
+ for i := 0; i < len(xx.sx); i += 2 {
+ if xx.sx[i].isAtom("UID") {
+ if xx.sx[i+1].kind == sxNumber {
+ uid = uint64(xx.sx[i+1].number) | uint64(c.box.validity)<<32
+ goto HaveUID
+ }
+ }
+ }
+ // This happens; too bad.
+ // log.Printf("FETCH without UID: %s", x)
+ return
+
+HaveUID:
+ if m := c.box.msgByUID[uid]; m != nil && m.dead {
+ // FETCH during box garbage collection.
+ m.dead = false
+ m.num = int(n)
+ return
+ }
+ m := c.box.newMsg(uid, int(n))
+ for i := 0; i < len(xx.sx); i += 2 {
+ k, v := xx.sx[i], xx.sx[i+1]
+ for _, t := range msgtab {
+ if k.isAtom(t.name) {
+ t.fn(m, k, v)
+ }
+ }
+ }
+}
+
+func xmsggmmsgid(m *Msg, k, v *sx) {
+ m.GmailID = uint64(v.number)
+}
+
+func xmsggmthrid(m *Msg, k, v *sx) {
+ m.GmailThread = uint64(v.number)
+}
+
+func xmsgflags(m *Msg, k, v *sx) {
+ m.Flags = v.parseFlags()
+}
+
+func xmsgrfc822size(m *Msg, k, v *sx) {
+ m.Bytes = v.number
+}
+
+func xmsgdate(m *Msg, k, v *sx) {
+ m.Date = v.parseDate()
+}
+
+func xmsgenvelope(m *Msg, k, v *sx) {
+ m.Hdr = parseEnvelope(v)
+}
+
+func parseEnvelope(v *sx) *MsgHdr {
+ if v.kind != sxList || !v.match("SSLLLLLLSS") {
+ log.Printf("bad envelope: %s", v)
+ return nil
+ }
+
+ hdr := &MsgHdr{
+ Date: v.sx[0].nstring(),
+ Subject: unrfc2047(v.sx[1].nstring()),
+ From: parseAddrs(v.sx[2]),
+ Sender: parseAddrs(v.sx[3]),
+ ReplyTo: parseAddrs(v.sx[4]),
+ To: parseAddrs(v.sx[5]),
+ CC: parseAddrs(v.sx[6]),
+ BCC: parseAddrs(v.sx[7]),
+ InReplyTo: unrfc2047(v.sx[8].nstring()),
+ MessageID: unrfc2047(v.sx[9].nstring()),
+ }
+
+ h := md5.New()
+ fmt.Fprintf(h, "date: %s\n", hdr.Date)
+ fmt.Fprintf(h, "subject: %s\n", hdr.Subject)
+ fmt.Fprintf(h, "from: %s\n", hdr.From)
+ fmt.Fprintf(h, "sender: %s\n", hdr.Sender)
+ fmt.Fprintf(h, "replyto: %s\n", hdr.ReplyTo)
+ fmt.Fprintf(h, "to: %s\n", hdr.To)
+ fmt.Fprintf(h, "cc: %s\n", hdr.CC)
+ fmt.Fprintf(h, "bcc: %s\n", hdr.BCC)
+ fmt.Fprintf(h, "inreplyto: %s\n", hdr.InReplyTo)
+ fmt.Fprintf(h, "messageid: %s\n", hdr.MessageID)
+ hdr.Digest = fmt.Sprintf("%x", h.Sum(nil))
+
+ return hdr
+}
+
+func parseAddrs(x *sx) []Addr {
+ var addr []Addr
+ for _, xx := range x.sx {
+ if !xx.match("SSSS") {
+ log.Printf("bad address: %s", x)
+ continue
+ }
+ name := unrfc2047(xx.sx[0].nstring())
+ // sx[1] is route
+ local := unrfc2047(xx.sx[2].nstring())
+ host := unrfc2047(xx.sx[3].nstring())
+ if local == "" || host == "" {
+ // rfc822 group syntax
+ addr = append(addr, Addr{name, ""})
+ continue
+ }
+ addr = append(addr, Addr{name, local + "@" + host})
+ }
+ return addr
+}
+
+func xmsgbody(m *Msg, k, v *sx) {
+ if v.isNil() {
+ return
+ }
+ if v.kind != sxList {
+ log.Printf("bad body: %s", v)
+ }
+
+ // To follow the structure exactly we should be doing this
+ // to m.NewPart(m.Part[0]) with type message/rfc822,
+ // but the extra layer is redundant - what else would be in
+ // a mailbox?
+ parseStructure(&m.Root, v)
+ n := m.num
+ if m.Box.maxSeen < n {
+ m.Box.maxSeen = n
+ }
+}
+
+func parseStructure(p *MsgPart, x *sx) {
+ if x.isNil() {
+ return
+ }
+ if x.kind != sxList {
+ log.Printf("bad structure: %s", x)
+ return
+ }
+ if x.sx[0].isList() {
+ // multipart
+ var i int
+ for i = 0; i < len(x.sx) && x.sx[i].isList(); i++ {
+ parseStructure(p.newPart(), x.sx[i])
+ }
+ if i != len(x.sx)-1 || !x.sx[i].isString() {
+ log.Printf("bad multipart structure: %s", x)
+ p.Type = "multipart/mixed"
+ return
+ }
+ s := strlwr(x.sx[i].nstring())
+ p.Type = "multipart/" + s
+ return
+ }
+
+ // single type
+ if len(x.sx) < 2 || !x.sx[0].isString() {
+ log.Printf("bad type structure: %s", x)
+ return
+ }
+ s := strlwr(x.sx[0].nstring())
+ t := strlwr(x.sx[1].nstring())
+ p.Type = s + "/" + t
+ if len(x.sx) < 7 || !x.sx[2].isList() || !x.sx[3].isString() || !x.sx[4].isString() || !x.sx[5].isString() || !x.sx[6].isNumber() {
+ log.Printf("bad part structure: %s", x)
+ return
+ }
+ parseParams(p, x.sx[2])
+ p.ContentID = x.sx[3].nstring()
+ p.Desc = x.sx[4].nstring()
+ p.Encoding = x.sx[5].nstring()
+ p.Bytes = x.sx[6].number
+ if p.Type == "message/rfc822" {
+ if len(x.sx) < 10 || !x.sx[7].isList() || !x.sx[8].isList() || !x.sx[9].isNumber() {
+ log.Printf("bad rfc822 structure: %s", x)
+ return
+ }
+ p.Hdr = parseEnvelope(x.sx[7])
+ parseStructure(p.newPart(), x.sx[8])
+ p.Lines = x.sx[9].number
+ }
+ if s == "text" {
+ if len(x.sx) < 8 || !x.sx[7].isNumber() {
+ log.Printf("bad text structure: %s", x)
+ return
+ }
+ p.Lines = x.sx[7].number
+ }
+}
+
+func parseParams(p *MsgPart, x *sx) {
+ if x.isNil() {
+ return
+ }
+ if len(x.sx)%2 != 0 {
+ log.Printf("bad message params: %s", x)
+ return
+ }
+
+ for i := 0; i < len(x.sx); i += 2 {
+ k, v := x.sx[i].nstring(), x.sx[i+1].nstring()
+ k = strlwr(k)
+ switch strlwr(k) {
+ case "charset":
+ p.Charset = strlwr(v)
+ case "name":
+ p.Name = v
+ }
+ }
+}
+
+func (c *Client) fetch(p *MsgPart, what string) {
+ c.io.mustBeLocked()
+ id := p.ID
+ if what != "" {
+ if id != "" {
+ id += "."
+ }
+ id += what
+ }
+ c.cmd(p.Msg.Box, "UID FETCH %d BODY[%s]", p.Msg.UID&(1<<32-1), id)
+}
+
+func xmsgbodydata(m *Msg, k, v *sx) {
+ // k.data is []byte("BODY[...")
+ name := string(k.data[5:])
+ if i := strings.Index(name, "]"); i >= 0 {
+ name = name[:i]
+ }
+
+ p := &m.Root
+ for name != "" && '1' <= name[0] && name[0] <= '9' {
+ var num int
+ num, name = parseNum(name)
+ if num == 0 {
+ log.Printf("unexpected body name: %s", k.data)
+ return
+ }
+ num--
+ if num >= len(p.Child) {
+ log.Printf("invalid body name: %s", k.data)
+ return
+ }
+ p = p.Child[num]
+ }
+
+ switch strlwr(name) {
+ case "":
+ p.raw = v.nbytes()
+ case "mime":
+ p.mimeHeader = nocr(v.nbytes())
+ case "header":
+ p.rawHeader = nocr(v.nbytes())
+ case "text":
+ p.rawBody = nocr(v.nbytes())
+ }
+}
+
+func parseNum(name string) (int, string) {
+ rest := ""
+ i := strings.Index(name, ".")
+ if i >= 0 {
+ name, rest = name[:i], name[i+1:]
+ }
+ n, _ := strconv.Atoi(name)
+ return n, rest
+}
+
+func nocr(b []byte) []byte {
+ w := 0
+ for _, c := range b {
+ if c != '\r' {
+ b[w] = c
+ w++
+ }
+ }
+ return b[:w]
+}
+
+type uidList []*Msg
+
+func (l uidList) String() string {
+ var b bytes.Buffer
+ for i, m := range l {
+ if i > 0 {
+ b.WriteByte(',')
+ }
+ fmt.Fprintf(&b, "%d", m.UID&(1<<32-1))
+ }
+ return b.String()
+}
+
+func (c *Client) deleteList(msgs []*Msg) error {
+ if len(msgs) == 0 {
+ return nil
+ }
+ c.io.mustBeLocked()
+
+ b := msgs[0].Box
+ for _, m := range msgs {
+ if m.Box != b {
+ return fmt.Errorf("messages span boxes: %q and %q", b.Name, m.Box.Name)
+ }
+ if uint32(m.UID>>32) != b.validity {
+ return fmt.Errorf("stale message")
+ }
+ }
+
+ err := c.cmd(b, "UID STORE %s +FLAGS (\\Deleted)", uidList(msgs))
+ if err == nil && c.box == b {
+ err = c.cmd(b, "EXPUNGE")
+ }
+ return err
+}
+
+func (c *Client) copyList(dst, src *Box, msgs []*Msg) error {
+ if len(msgs) == 0 {
+ return nil
+ }
+ c.io.mustBeLocked()
+
+ for _, m := range msgs {
+ if m.Box != src {
+ return fmt.Errorf("messages span boxes: %q and %q", src.Name, m.Box.Name)
+ }
+ if uint32(m.UID>>32) != src.validity {
+ return fmt.Errorf("stale message")
+ }
+ }
+
+ var name string
+ if dst == c.inbox {
+ name = "INBOX"
+ } else {
+ name = iquote(dst.Name)
+ }
+ return c.cmd(src, "UID COPY %s %s", uidList(msgs), name)
+}
+
+func (c *Client) muteList(src *Box, msgs []*Msg) error {
+ if len(msgs) == 0 {
+ return nil
+ }
+ c.io.mustBeLocked()
+
+ for _, m := range msgs {
+ if m.Box != src {
+ return fmt.Errorf("messages span boxes: %q and %q", src.Name, m.Box.Name)
+ }
+ if uint32(m.UID>>32) != src.validity {
+ return fmt.Errorf("stale message")
+ }
+ }
+
+ return c.cmd(src, "UID STORE %s +X-GM-LABELS (\\Muted)", uidList(msgs))
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/imap_test.go b/vendor/github.com/mattermost/rsc/imap/imap_test.go
new file mode 100644
index 000000000..75737fcc8
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/imap_test.go
@@ -0,0 +1,433 @@
+package imap
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/mattermost/rsc/google"
+)
+
+// NOTE: web address is https://mail.google.com/mail/b/rsc@swtch.com/?shva=1#inbox/132e5fd3a6a3c17b
+// where the last is the hex for the thread id.
+// have to have the #inbox part right too. #label/Hello+World/...
+// or #all as a fallback
+
+// TODO: ID command support (RFC 2971)
+
+const mock = true
+
+var user = "rsc@swtch.com"
+var pw, _ = ioutil.ReadFile("/Users/rsc/.swtchpass")
+
+func TestImap(t *testing.T) {
+ var user, pw string
+ if mock {
+ testDial = fakeDial
+ user = "gre@host.com"
+ pw = "password"
+ } else {
+ acct := google.Acct("rsc@swtch.com")
+ user = acct.Email
+ pw = acct.Password
+ }
+ c, err := NewClient(TLS, "imap.gmail.com", user, pw, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ inbox := c.Inbox()
+ msgs := inbox.Msgs()
+
+ for _, m := range msgs {
+ if m.UID == 611764547<<32|57046 {
+ // c.io.lock()
+ // c.cmd(c.boxByName[`[Gmail]/All Mail`], `UID SEARCH X-GM-RAW "label:russcox@gmail.com in:inbox in:unread -in:muted"`)
+ // c.cmd(c.inbox, `UID SEARCH X-GM-RAW "label:russcox@gmail.com in:inbox in:unread -in:muted"`)
+ // c.cmd(c.boxByName[`To Read`], `UID SEARCH X-GM-RAW "label:russcox@gmail.com in:inbox in:unread -in:muted"`)
+ // c.cmd(c.boxByName[`[Gmail]/All Mail`], `UID SEARCH X-GM-RAW "label:russcox@gmail.com in:inbox in:unread -in:muted"`)
+ // c.fetch(m.Root.Child[0], "")
+ // c.io.unlock()
+ fmt.Println("--")
+ fmt.Println("From:", m.Hdr.From)
+ fmt.Println("To:", m.Hdr.To)
+ fmt.Println("Subject:", m.Hdr.Subject)
+ fmt.Println("M-Date:", time.Unix(m.Date, 0))
+ fmt.Println("Date:", m.Hdr.Date)
+ fmt.Println()
+ fmt.Println(string(m.Root.Child[0].Text()))
+ fmt.Println("--")
+ }
+ }
+ c.Close()
+}
+
+func fakeDial(server string, mode Mode) (io.ReadWriteCloser, error) {
+ r1, w1 := io.Pipe()
+ r2, w2 := io.Pipe()
+ go fakeServer(&pipe2{r1, w2})
+ return &pipe2{r2, w1}, nil
+}
+
+func fakeServer(rw io.ReadWriteCloser) {
+ b := bufio.NewReader(rw)
+ rw.Write([]byte(fakeReply[""]))
+ for {
+ line, err := b.ReadString('\n')
+ if err != nil {
+ break
+ }
+ reply := fakeReply[strings.TrimSpace(line)]
+ if reply == "" {
+ rw.Write([]byte("* BYE\r\n"))
+ break
+ }
+ rw.Write([]byte(reply))
+ }
+ rw.Close()
+}
+
+var fakeReply = map[string]string{
+ ``: "* OK Gimap ready for requests from 71.232.17.63 k7if4537693qcx.66\r\n",
+ `# LOGIN gre@host.com password`: "* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE\r\n" +
+ "# OK gre@host.com Grace Emlin authenticated (Success)\r\n",
+ `# XLIST "" INBOX`: `* XLIST (\HasNoChildren \Inbox) "/" "Inbox"` + "\r\n" +
+ "# OK Success\r\n",
+ `# XLIST "" *`: `* XLIST (\HasNoChildren \Inbox) "/" "Inbox"` + "\r\n" +
+ `* XLIST (\HasNoChildren) "/" "Someday"` + "\r\n" +
+ `* XLIST (\HasNoChildren) "/" "To Read"` + "\r\n" +
+ `* XLIST (\HasNoChildren) "/" "Waiting"` + "\r\n" +
+ `* XLIST (\Noselect \HasChildren) "/" "[Gmail]"` + "\r\n" +
+ `* XLIST (\HasNoChildren \AllMail) "/" "[Gmail]/All Mail"` + "\r\n" +
+ `* XLIST (\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"` + "\r\n" +
+ `* XLIST (\HasNoChildren \Important) "/" "[Gmail]/Important"` + "\r\n" +
+ `* XLIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"` + "\r\n" +
+ `* XLIST (\HasNoChildren \Spam) "/" "[Gmail]/Spam"` + "\r\n" +
+ `* XLIST (\HasNoChildren \Starred) "/" "[Gmail]/Starred"` + "\r\n" +
+ `* XLIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash"` + "\r\n" +
+ `* XLIST (\HasNoChildren) "/" "russcox@gmail.com"` + "\r\n" +
+ "# OK Success\r\n",
+ `# LIST "" INBOX`: `* LIST (\HasNoChildren) "/" "INBOX"` + "\r\n" +
+ "# OK Success\r\n",
+ `# LIST "" *`: `* LIST (\HasNoChildren) "/" "INBOX"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "Someday"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "To Read"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "Waiting"` + "\r\n" +
+ `* LIST (\Noselect \HasChildren) "/" "[Gmail]"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/All Mail"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/Drafts"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/Important"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/Sent Mail"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/Spam"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/Starred"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "[Gmail]/Trash"` + "\r\n" +
+ `* LIST (\HasNoChildren) "/" "russcox@gmail.com"` + "\r\n" +
+ "# OK Success\r\n",
+ `# SELECT inbox`: `* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)` + "\r\n" +
+ `* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen \*)] Flags permitted.` + "\r\n" +
+ `* OK [UIDVALIDITY 611764547] UIDs valid.` + "\r\n" +
+ `* 9 EXISTS` + "\r\n" +
+ `* 0 RECENT` + "\r\n" +
+ `* OK [UIDNEXT 57027] Predicted next UID.` + "\r\n" +
+ "# OK [READ-WRITE] inbox selected. (Success)\r\n",
+ `# UID FETCH 1:* (FLAGS)`: `* 1 FETCH (UID 46074 FLAGS (\Seen))` + "\r\n" +
+ `* 2 FETCH (UID 49094 FLAGS (\Seen))` + "\r\n" +
+ `* 3 FETCH (UID 49317 FLAGS (\Seen))` + "\r\n" +
+ `* 4 FETCH (UID 49424 FLAGS (\Flagged \Seen))` + "\r\n" +
+ `* 5 FETCH (UID 49595 FLAGS (\Seen))` + "\r\n" +
+ `* 6 FETCH (UID 49810 FLAGS (\Seen))` + "\r\n" +
+ `* 7 FETCH (UID 50579 FLAGS (\Seen))` + "\r\n" +
+ `* 8 FETCH (UID 50597 FLAGS (\Seen))` + "\r\n" +
+ `* 9 FETCH (UID 50598 FLAGS (\Seen))` + "\r\n" +
+ "# OK Success\r\n",
+ `# FETCH 1:* (UID FLAGS)`: `* 1 FETCH (UID 46074 FLAGS (\Seen))` + "\r\n" +
+ `* 2 FETCH (UID 49094 FLAGS (\Seen))` + "\r\n" +
+ `* 3 FETCH (UID 49317 FLAGS (\Seen))` + "\r\n" +
+ `* 4 FETCH (UID 49424 FLAGS (\Flagged \Seen))` + "\r\n" +
+ `* 5 FETCH (UID 49595 FLAGS (\Seen))` + "\r\n" +
+ `* 6 FETCH (UID 49810 FLAGS (\Seen))` + "\r\n" +
+ `* 7 FETCH (UID 50579 FLAGS (\Seen))` + "\r\n" +
+ `* 8 FETCH (UID 50597 FLAGS (\Seen))` + "\r\n" +
+ `* 9 FETCH (UID 50598 FLAGS (\Seen))` + "\r\n" +
+ "# OK Success\r\n",
+ `# NOOP`: "# OK Success\r\n",
+ `# UID FETCH 1:* (FLAGS X-GM-MSGID X-GM-THRID)`: `* 1 FETCH (X-GM-THRID 1371690017835349492 X-GM-MSGID 1371690017835349492 UID 46074 FLAGS (\Seen))` + "\r\n" +
+ `* 2 FETCH (X-GM-THRID 1370053443095117076 X-GM-MSGID 1374032778063810116 UID 49094 FLAGS (\Seen))` + "\r\n" +
+ `* 3 FETCH (X-GM-THRID 1370053443095117076 X-GM-MSGID 1374171123044094435 UID 49317 FLAGS (\Seen))` + "\r\n" +
+ `* 4 FETCH (X-GM-THRID 1374260005724669308 X-GM-MSGID 1374260005724669308 UID 49424 FLAGS (\Flagged \Seen))` + "\r\n" +
+ `* 5 FETCH (X-GM-THRID 1374399840419707240 X-GM-MSGID 1374399840419707240 UID 49595 FLAGS (\Seen))` + "\r\n" +
+ `* 6 FETCH (X-GM-THRID 1374564698687599195 X-GM-MSGID 1374564698687599195 UID 49810 FLAGS (\Seen))` + "\r\n" +
+ `* 7 FETCH (X-GM-THRID 1353701773219222407 X-GM-MSGID 1375207927094695931 UID 50579 FLAGS (\Seen))` + "\r\n" +
+ `* 8 FETCH (X-GM-THRID 1375017086705541883 X-GM-MSGID 1375220323861690146 UID 50597 FLAGS (\Seen))` + "\r\n" +
+ `* 9 FETCH (X-GM-THRID 1353701773219222407 X-GM-MSGID 1375220551142026521 UID 50598 FLAGS (\Seen))` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 1:* (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE X-GM-MSGID X-GM-THRID)`: `* 1 FETCH (X-GM-THRID 1371690017835349492 X-GM-MSGID 1371690017835349492 UID 46074 RFC822.SIZE 5700 INTERNALDATE "15-Jun-2011 13:45:39 +0000" FLAGS (\Seen) ENVELOPE ("Wed, 15 Jun 2011 13:45:35 +0000" "[re2-dev] Issue 40 in re2: Please make RE2::Rewrite public" ((NIL NIL "re2" "googlecode.com")) ((NIL NIL "re2-dev" "googlegroups.com")) ((NIL NIL "codesite-noreply" "google.com")) ((NIL NIL "re2-dev" "googlegroups.com")) NIL NIL NIL "<0-13244084390050003171-8842966241254494762-re2=googlecode.com@googlecode.com>"))` + "\r\n" +
+ `* 2 FETCH (X-GM-THRID 1370053443095117076 X-GM-MSGID 1374032778063810116 UID 49094 RFC822.SIZE 3558 INTERNALDATE "11-Jul-2011 10:22:49 +0000" FLAGS (\Seen) ENVELOPE ("Mon, 11 Jul 2011 12:22:46 +0200" "Re: [re2-dev] Re: Issue 39 in re2: Eiffel wrapper for RE2" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Russ Cox" NIL "rsc" "swtch.com")) NIL NIL "<CADSkJJWthFb61R1tqJxZP1SxTPuwY_BBW5ToLuzX2UpHSvsy9w@mail.gmail.com>" "<4E1ACEF6.4060609@gmail.com>"))` + "\r\n" +
+ `* 3 FETCH (X-GM-THRID 1370053443095117076 X-GM-MSGID 1374171123044094435 UID 49317 RFC822.SIZE 3323 INTERNALDATE "12-Jul-2011 23:01:46 +0000" FLAGS (\Seen) ENVELOPE ("Wed, 13 Jul 2011 01:01:41 +0200" "Re: [re2-dev] Re: Issue 39 in re2: Eiffel wrapper for RE2" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Russ Cox" NIL "rsc" "swtch.com")) NIL NIL "<CADSkJJV+E-0Xtm=dpiSHLbwkZjZ=zDDoE1t1w0CiGYa+pVz66g@mail.gmail.com>" "<4E1CD255.6060807@gmail.com>"))` + "\r\n" +
+ `* 4 FETCH (X-GM-THRID 1374260005724669308 X-GM-MSGID 1374260005724669308 UID 49424 RFC822.SIZE 2681 INTERNALDATE "13-Jul-2011 22:34:31 +0000" FLAGS (\Flagged \Seen) ENVELOPE ("Wed, 13 Jul 2011 16:33:43 -0600" "Minor correction for venti(8) user manual for running plan9port on Linux" (("Xing" NIL "xinglin" "cs.utah.edu")) (("Xing" NIL "xinglin" "cs.utah.edu")) (("Xing" NIL "xinglin" "cs.utah.edu")) ((NIL NIL "rsc" "swtch.com")) (("Xing Lin" NIL "xinglin" "cs.utah.edu") ("Raghuveer Pullakandam" NIL "rgv" "cs.utah.edu") ("Robert Ricci" NIL "ricci" "cs.utah.edu") ("Eric Eide" NIL "eeide" "cs.utah.edu")) NIL NIL "<1310596423.3866.11.camel@xing-utah-cs>"))` + "\r\n" +
+ `* 5 FETCH (X-GM-THRID 1374399840419707240 X-GM-MSGID 1374399840419707240 UID 49595 RFC822.SIZE 6496 INTERNALDATE "15-Jul-2011 11:37:07 +0000" FLAGS (\Seen) ENVELOPE ("Fri, 15 Jul 2011 13:36:54 +0200" "[re2-dev] MSVC not exporting VariadicFunction2<.. FullMatchN>::operator()(..) but VariadicFunction2<.. PartialMatchN>::operator()(..)" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) NIL NIL NIL "<4E202656.7010408@gmail.com>"))` + "\r\n" +
+ `* 6 FETCH (X-GM-THRID 1374564698687599195 X-GM-MSGID 1374564698687599195 UID 49810 RFC822.SIZE 5485 INTERNALDATE "17-Jul-2011 07:17:29 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 17 Jul 2011 00:17:28 -0700" "Acme IRC client patch" (("Ethan Burns" NIL "burns.ethan" "gmail.com")) (("Ethan Burns" NIL "burns.ethan" "gmail.com")) (("Ethan Burns" NIL "burns.ethan" "gmail.com")) ((NIL NIL "rsc" "swtch.com")) NIL NIL NIL "<CAGE=Ei0bmAjsYYDxCgtDObuxX_tCU18RcWTe6siwemXAuKqDfg@mail.gmail.com>"))` + "\r\n" +
+ `* 7 FETCH (X-GM-THRID 1353701773219222407 X-GM-MSGID 1375207927094695931 UID 50579 RFC822.SIZE 4049 INTERNALDATE "24-Jul-2011 09:41:19 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 24 Jul 2011 02:41:14 -0700 (PDT)" "Re: [re2-dev] Re: MSVC build" ((NIL NIL "talgil" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) ((NIL NIL "re2-dev" "googlegroups.com")) ((NIL NIL "re2-dev" "googlegroups.com")) (("ioannis" NIL "ioannis.e" "gmail.com")) NIL "<AANLkTin8_-yDr8tcb9SosfQ_iAM6RmfzpLQB0gX0vv6w@mail.gmail.com>" "<24718992.6777.1311500475040.JavaMail.geo-discussion-forums@yqyy3>"))` + "\r\n" +
+ `* 8 FETCH (X-GM-THRID 1375017086705541883 X-GM-MSGID 1375220323861690146 UID 50597 RFC822.SIZE 3070 INTERNALDATE "24-Jul-2011 12:58:22 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 24 Jul 2011 14:58:15 +0200" "Re: [re2-dev] Rearranging platform dependant features" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Russ Cox" NIL "rsc" "swtch.com")) NIL NIL "<CADSkJJV+eCPkkhsepo5k0w+dqVo0fQOana2bWp4BexGOrCSSUQ@mail.gmail.com>" "<4E2C16E7.3060500@gmail.com>"))` + "\r\n" +
+ `* 9 FETCH (X-GM-THRID 1353701773219222407 X-GM-MSGID 1375220551142026521 UID 50598 RFC822.SIZE 5744 INTERNALDATE "24-Jul-2011 13:01:59 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 24 Jul 2011 15:01:49 +0200" "Re: [re2-dev] Re: MSVC build" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) NIL NIL "<24718992.6777.1311500475040.JavaMail.geo-discussion-forums@yqyy3>" "<4E2C17BD.6000702@gmail.com>"))` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57047:* (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY X-GM-MSGID X-GM-THRID X-GM-LABELS)`: `* 9 FETCH (X-GM-THRID 1382192619814696847 X-GM-MSGID 1382192619814696847 X-GM-LABELS ("\\Important" russcox@gmail.com) UID 57046 RFC822.SIZE 4170 INTERNALDATE "09-Oct-2011 12:00:02 +0000" FLAGS () ENVELOPE ("Sun, 09 Oct 2011 12:00:02 +0000" "You have no events scheduled today." (("Google Calendar" NIL "calendar-notification" "google.com")) (("Google Calendar" NIL "calendar-notification" "google.com")) (("Russ Cox" NIL "russcox" "gmail.com")) (("Russ Cox" NIL "russcox" "gmail.com")) NIL NIL NIL "<bcaec501c5be15fc7204aedc6af6@google.com>") BODY (("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "DELSP" "yes" "FORMAT" "flowed") NIL NIL "7BIT" 465 11)("TEXT" "HTML" ("CHARSET" "ISO-8859-1") NIL NIL "QUOTED-PRINTABLE" 914 12) "ALTERNATIVE"))` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 1:* (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY X-GM-MSGID X-GM-THRID X-GM-LABELS)`: `* 1 FETCH (X-GM-THRID 1371690017835349492 X-GM-MSGID 1371690017835349492 X-GM-LABELS () UID 46074 RFC822.SIZE 5700 INTERNALDATE "15-Jun-2011 13:45:39 +0000" FLAGS (\Seen) ENVELOPE ("Wed, 15 Jun 2011 13:45:35 +0000" "[re2-dev] Issue 40 in re2: Please make RE2::Rewrite public" ((NIL NIL "re2" "googlecode.com")) ((NIL NIL "re2-dev" "googlegroups.com")) ((NIL NIL "codesite-noreply" "google.com")) ((NIL NIL "re2-dev" "googlegroups.com")) NIL NIL NIL "<0-13244084390050003171-8842966241254494762-re2=googlecode.com@googlecode.com>") BODY ("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "DELSP" "yes" "FORMAT" "flowed") NIL NIL "7BIT" 389 11))` + "\r\n" +
+ `* 2 FETCH (X-GM-THRID 1370053443095117076 X-GM-MSGID 1374032778063810116 X-GM-LABELS ("\\Important") UID 49094 RFC822.SIZE 3558 INTERNALDATE "11-Jul-2011 10:22:49 +0000" FLAGS (\Seen) ENVELOPE ("Mon, 11 Jul 2011 12:22:46 +0200" "Re: [re2-dev] Re: Issue 39 in re2: Eiffel wrapper for RE2" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Russ Cox" NIL "rsc" "swtch.com")) NIL NIL "<CADSkJJWthFb61R1tqJxZP1SxTPuwY_BBW5ToLuzX2UpHSvsy9w@mail.gmail.com>" "<4E1ACEF6.4060609@gmail.com>") BODY ("TEXT" "PLAIN" ("CHARSET" "UTF-8" "FORMAT" "flowed") NIL NIL "7BIT" 766 24))` + "\r\n" +
+ `* 3 FETCH (X-GM-THRID 1370053443095117076 X-GM-MSGID 1374171123044094435 X-GM-LABELS ("\\Important") UID 49317 RFC822.SIZE 3323 INTERNALDATE "12-Jul-2011 23:01:46 +0000" FLAGS (\Seen) ENVELOPE ("Wed, 13 Jul 2011 01:01:41 +0200" "Re: [re2-dev] Re: Issue 39 in re2: Eiffel wrapper for RE2" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Russ Cox" NIL "rsc" "swtch.com")) NIL NIL "<CADSkJJV+E-0Xtm=dpiSHLbwkZjZ=zDDoE1t1w0CiGYa+pVz66g@mail.gmail.com>" "<4E1CD255.6060807@gmail.com>") BODY ("TEXT" "PLAIN" ("CHARSET" "UTF-8" "FORMAT" "flowed") NIL NIL "7BIT" 435 12))` + "\r\n" +
+ `* 4 FETCH (X-GM-THRID 1374260005724669308 X-GM-MSGID 1374260005724669308 X-GM-LABELS ("\\Important" "\\Starred") UID 49424 RFC822.SIZE 2681 INTERNALDATE "13-Jul-2011 22:34:31 +0000" FLAGS (\Flagged \Seen) ENVELOPE ("Wed, 13 Jul 2011 16:33:43 -0600" "Minor correction for venti(8) user manual for running plan9port on Linux" (("Xing" NIL "xinglin" "cs.utah.edu")) (("Xing" NIL "xinglin" "cs.utah.edu")) (("Xing" NIL "xinglin" "cs.utah.edu")) ((NIL NIL "rsc" "swtch.com")) (("Xing Lin" NIL "xinglin" "cs.utah.edu") ("Raghuveer Pullakandam" NIL "rgv" "cs.utah.edu") ("Robert Ricci" NIL "ricci" "cs.utah.edu") ("Eric Eide" NIL "eeide" "cs.utah.edu")) NIL NIL "<1310596423.3866.11.camel@xing-utah-cs>") BODY ("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "8BIT" 789 25))` + "\r\n" +
+ `* 5 FETCH (X-GM-THRID 1374399840419707240 X-GM-MSGID 1374399840419707240 X-GM-LABELS ("\\Important") UID 49595 RFC822.SIZE 6496 INTERNALDATE "15-Jul-2011 11:37:07 +0000" FLAGS (\Seen) ENVELOPE ("Fri, 15 Jul 2011 13:36:54 +0200" "[re2-dev] MSVC not exporting VariadicFunction2<.. FullMatchN>::operator()(..) but VariadicFunction2<.. PartialMatchN>::operator()(..)" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) NIL NIL NIL "<4E202656.7010408@gmail.com>") BODY ("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "FORMAT" "flowed") NIL NIL "7BIT" 1660 34))` + "\r\n" +
+ `* 6 FETCH (X-GM-THRID 1374564698687599195 X-GM-MSGID 1374564698687599195 X-GM-LABELS ("\\Important") UID 49810 RFC822.SIZE 5485 INTERNALDATE "17-Jul-2011 07:17:29 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 17 Jul 2011 00:17:28 -0700" "Acme IRC client patch" (("Ethan Burns" NIL "burns.ethan" "gmail.com")) (("Ethan Burns" NIL "burns.ethan" "gmail.com")) (("Ethan Burns" NIL "burns.ethan" "gmail.com")) ((NIL NIL "rsc" "swtch.com")) NIL NIL NIL "<CAGE=Ei0bmAjsYYDxCgtDObuxX_tCU18RcWTe6siwemXAuKqDfg@mail.gmail.com>") BODY (("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 443 13)("TEXT" "X-PATCH" ("CHARSET" "US-ASCII" "NAME" "emote.patch") NIL NIL "BASE64" 2774 35) "MIXED"))` + "\r\n" +
+ `* 7 FETCH (X-GM-THRID 1353701773219222407 X-GM-MSGID 1375207927094695931 X-GM-LABELS ("\\Important") UID 50579 RFC822.SIZE 4049 INTERNALDATE "24-Jul-2011 09:41:19 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 24 Jul 2011 02:41:14 -0700 (PDT)" "Re: [re2-dev] Re: MSVC build" ((NIL NIL "talgil" "gmail.com")) ((NIL NIL "re2-dev" "googlegroups.com")) ((NIL NIL "re2-dev" "googlegroups.com")) ((NIL NIL "re2-dev" "googlegroups.com")) (("ioannis" NIL "ioannis.e" "gmail.com")) NIL "<AANLkTin8_-yDr8tcb9SosfQ_iAM6RmfzpLQB0gX0vv6w@mail.gmail.com>" "<24718992.6777.1311500475040.JavaMail.geo-discussion-forums@yqyy3>") BODY (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "7BIT" 133 8)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "7BIT" 211 0) "ALTERNATIVE"))` + "\r\n" +
+ `* 8 FETCH (X-GM-THRID 1375017086705541883 X-GM-MSGID 1375220323861690146 X-GM-LABELS ("\\Important") UID 50597 RFC822.SIZE 3070 INTERNALDATE "24-Jul-2011 12:58:22 +0000" FLAGS (\Seen) ENVELOPE ("Sun, 24 Jul 2011 14:58:15 +0200" "Re: [re2-dev] Rearranging platform dependant features" (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Pontus Carlsson" NIL "pontusjoncarlsson" "gmail.com")) (("Russ Cox" NIL "rsc" "swtch.com")) NIL NIL "<CADSkJJV+eCPkkhsepo5k0w+dqVo0fQOana2bWp4BexGOrCSSUQ@mail.gmail.com>" "<4E2C16E7.3060500@gmail.com>") BODY ("TEXT" "PLAIN" ("CHARSET" "UTF-8" "FORMAT" "flowed") NIL NIL "7BIT" 450 10))` + "\r\n" +
+ `* 9 FETCH (X-GM-THRID 1382192619814696847 X-GM-MSGID 1382192619814696847 X-GM-LABELS ("\\Important" russcox@gmail.com) UID 57046 RFC822.SIZE 4170 INTERNALDATE "09-Oct-2011 12:00:02 +0000" FLAGS () ENVELOPE ("Sun, 09 Oct 2011 12:00:02 +0000" "You have no events scheduled today." (("Google Calendar" NIL "calendar-notification" "google.com")) (("Google Calendar" NIL "calendar-notification" "google.com")) (("Russ Cox" NIL "russcox" "gmail.com")) (("Russ Cox" NIL "russcox" "gmail.com")) NIL NIL NIL "<bcaec501c5be15fc7204aedc6af6@google.com>") BODY (("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "DELSP" "yes" "FORMAT" "flowed") NIL NIL "7BIT" 465 11)("TEXT" "HTML" ("CHARSET" "ISO-8859-1") NIL NIL "QUOTED-PRINTABLE" 914 12) "ALTERNATIVE"))` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[1]`: `* 9 FETCH (UID 57046 BODY[1] {465}` + "\r\n" +
+ `russcox@gmail.com, you have no events scheduled today Sun Oct 9, 2011.` + "\r\n" +
+ `` + "\r\n" +
+ `View your calendar at https://www.google.com/calendar/` + "\r\n" +
+ `` + "\r\n" +
+ `You are receiving this email at the account russcox@gmail.com because you ` + "\r\n" +
+ `are subscribed to receive daily agendas for the following calendars: Russ ` + "\r\n" +
+ `Cox.` + "\r\n" +
+ `` + "\r\n" +
+ `To change which calendars you receive daily agendas for, please log in to ` + "\r\n" +
+ `https://www.google.com/calendar/ and change your notification settings for ` + "\r\n" +
+ `each calendar.` + "\r\n" +
+ `)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[1.TEXT]`: `* 9 FETCH (UID 57046 BODY[1.TEXT] NIL)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[1.HEADER]`: `* 9 FETCH (UID 57046 BODY[1.HEADER] NIL)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[1.MIME]`: `* 146 FETCH (UID 57046 BODY[1.MIME] {74}` + "\r\n" +
+ `Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes` + "\r\n" +
+ `` + "\r\n" +
+ `)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[2]`: `* 146 FETCH (UID 57046 BODY[2] {914}` + "\r\n" +
+ `<div style=3D"padding:10px 7px;font-size:14px;line-height:1.4;font-family:A=` + "\r\n" +
+ `rial,Sans-serif;text-align:left;bgcolor=3D#ffffff"><a href=3D"https://www.g=` + "\r\n" +
+ `oogle.com/calendar/"><img style=3D"border-width:0" src=3D"https://www.googl=` + "\r\n" +
+ `e.com/calendar/images/calendar_logo_sm_en.gif" alt=3D"Google Calendar"></a>` + "\r\n" +
+ `<p style=3D"margin:0;color:#0">russcox@gmail.com,&nbsp;you have no events s=` + "\r\n" +
+ `cheduled today <b>Sun Oct 9, 2011</b></p>` + "\r\n" +
+ `<p style=3D"font-family:Arial,Sans-serif;color:#666;font-size:11px">You are=` + "\r\n" +
+ ` receiving this email at the account russcox@gmail.com because you are subs=` + "\r\n" +
+ `cribed to receive daily agendas for the following calendars: Russ Cox.</p>` + "\r\n" +
+ `<p style=3D"font-family:Arial,Sans-serif;color:#666;font-size:11px">To chan=` + "\r\n" +
+ `ge which calendars you receive daily agendas for, please log in to https://=` + "\r\n" +
+ `www.google.com/calendar/ and change your notification settings for each cal=` + "\r\n" +
+ `endar.</p></div>)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[2.TEXT]`: `* 9 FETCH (UID 57046 BODY[2.TEXT] NIL)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[2.HEADER]`: `* 9 FETCH (UID 57046 BODY[2.HEADER] NIL)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[2.MIME]`: `* 146 FETCH (UID 57046 BODY[2.MIME] {92}` + "\r\n" +
+ `Content-Type: text/html; charset=ISO-8859-1` + "\r\n" +
+ `Content-Transfer-Encoding: quoted-printable` + "\r\n" +
+ `` + "\r\n" +
+ `)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[]`: `* 146 FETCH (UID 57046 BODY[] {4170}` + "\r\n" +
+ `Delivered-To: rsc@swtch.com` + "\r\n" +
+ `Received: by 10.216.54.148 with SMTP id i20cs32329wec;` + "\r\n" +
+ ` Sun, 9 Oct 2011 05:00:30 -0700 (PDT)` + "\r\n" +
+ `Received: by 10.227.11.2 with SMTP id r2mr4751812wbr.43.1318161630585;` + "\r\n" +
+ ` Sun, 09 Oct 2011 05:00:30 -0700 (PDT)` + "\r\n" +
+ `DomainKey-Status: good` + "\r\n" +
+ `Received-SPF: softfail (google.com: best guess record for domain of transitioning 3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com does not designate <unknown> as permitted sender)` + "\r\n" +
+ `Received: by 10.241.227.90 with POP3 id 26mf2646912wyj.48;` + "\r\n" +
+ ` Sun, 09 Oct 2011 05:00:29 -0700 (PDT)` + "\r\n" +
+ `X-Gmail-Fetch-Info: russcox@gmail.com 1 smtp.gmail.com 995 russcox` + "\r\n" +
+ `Delivered-To: russcox@gmail.com` + "\r\n" +
+ `Received: by 10.142.76.10 with SMTP id y10cs75487wfa;` + "\r\n" +
+ ` Sun, 9 Oct 2011 05:00:08 -0700 (PDT)` + "\r\n" +
+ `Return-Path: <3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com>` + "\r\n" +
+ `Received-SPF: pass (google.com: domain of 3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com designates 10.52.73.100 as permitted sender) client-ip=10.52.73.100;` + "\r\n" +
+ `Authentication-Results: mr.google.com; spf=pass (google.com: domain of 3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com designates 10.52.73.100 as permitted sender) smtp.mail=3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com; dkim=pass header.i=3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com` + "\r\n" +
+ `Received: from mr.google.com ([10.52.73.100])` + "\r\n" +
+ ` by 10.52.73.100 with SMTP id k4mr8053242vdv.5.1318161606360 (num_hops = 1);` + "\r\n" +
+ ` Sun, 09 Oct 2011 05:00:06 -0700 (PDT)` + "\r\n" +
+ `DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;` + "\r\n" +
+ ` d=google.com; s=beta;` + "\r\n" +
+ ` h=mime-version:reply-to:auto-submitted:message-id:date:subject:from` + "\r\n" +
+ ` :to:content-type;` + "\r\n" +
+ ` bh=SGjz0F4q+eFVkoC4yzLKQKvlxTKiUsYbO/KPI+3KOE8=;` + "\r\n" +
+ ` b=LRBkWBW7ZZ4UJYa7b92zfHa0ZM1K1d0wP/jbgmDw2OZTWtgDICZb30dzhFUfNVdxeN` + "\r\n" +
+ ` kdMFbRhTLP5NpSXWhbDw==` + "\r\n" +
+ `MIME-Version: 1.0` + "\r\n" +
+ `Received: by 10.52.73.100 with SMTP id k4mr5244039vdv.5.1318161602706; Sun, 09` + "\r\n" +
+ ` Oct 2011 05:00:02 -0700 (PDT)` + "\r\n" +
+ `Reply-To: Russ Cox <russcox@gmail.com>` + "\r\n" +
+ `Auto-Submitted: auto-generated` + "\r\n" +
+ `Message-ID: <bcaec501c5be15fc7204aedc6af6@google.com>` + "\r\n" +
+ `Date: Sun, 09 Oct 2011 12:00:02 +0000` + "\r\n" +
+ `Subject: You have no events scheduled today.` + "\r\n" +
+ `From: Google Calendar <calendar-notification@google.com>` + "\r\n" +
+ `To: Russ Cox <russcox@gmail.com>` + "\r\n" +
+ `Content-Type: multipart/alternative; boundary=bcaec501c5be15fc6504aedc6af3` + "\r\n" +
+ `` + "\r\n" +
+ `--bcaec501c5be15fc6504aedc6af3` + "\r\n" +
+ `Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes` + "\r\n" +
+ `` + "\r\n" +
+ `russcox@gmail.com, you have no events scheduled today Sun Oct 9, 2011.` + "\r\n" +
+ `` + "\r\n" +
+ `View your calendar at https://www.google.com/calendar/` + "\r\n" +
+ `` + "\r\n" +
+ `You are receiving this email at the account russcox@gmail.com because you ` + "\r\n" +
+ `are subscribed to receive daily agendas for the following calendars: Russ ` + "\r\n" +
+ `Cox.` + "\r\n" +
+ `` + "\r\n" +
+ `To change which calendars you receive daily agendas for, please log in to ` + "\r\n" +
+ `https://www.google.com/calendar/ and change your notification settings for ` + "\r\n" +
+ `each calendar.` + "\r\n" +
+ `` + "\r\n" +
+ `--bcaec501c5be15fc6504aedc6af3` + "\r\n" +
+ `Content-Type: text/html; charset=ISO-8859-1` + "\r\n" +
+ `Content-Transfer-Encoding: quoted-printable` + "\r\n" +
+ `` + "\r\n" +
+ `<div style=3D"padding:10px 7px;font-size:14px;line-height:1.4;font-family:A=` + "\r\n" +
+ `rial,Sans-serif;text-align:left;bgcolor=3D#ffffff"><a href=3D"https://www.g=` + "\r\n" +
+ `oogle.com/calendar/"><img style=3D"border-width:0" src=3D"https://www.googl=` + "\r\n" +
+ `e.com/calendar/images/calendar_logo_sm_en.gif" alt=3D"Google Calendar"></a>` + "\r\n" +
+ `<p style=3D"margin:0;color:#0">russcox@gmail.com,&nbsp;you have no events s=` + "\r\n" +
+ `cheduled today <b>Sun Oct 9, 2011</b></p>` + "\r\n" +
+ `<p style=3D"font-family:Arial,Sans-serif;color:#666;font-size:11px">You are=` + "\r\n" +
+ ` receiving this email at the account russcox@gmail.com because you are subs=` + "\r\n" +
+ `cribed to receive daily agendas for the following calendars: Russ Cox.</p>` + "\r\n" +
+ `<p style=3D"font-family:Arial,Sans-serif;color:#666;font-size:11px">To chan=` + "\r\n" +
+ `ge which calendars you receive daily agendas for, please log in to https://=` + "\r\n" +
+ `www.google.com/calendar/ and change your notification settings for each cal=` + "\r\n" +
+ `endar.</p></div>` + "\r\n" +
+ `--bcaec501c5be15fc6504aedc6af3--` + "\r\n" +
+ `)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[TEXT]`: `* 146 FETCH (UID 57046 BODY[TEXT] {1647}` + "\r\n" +
+ `--bcaec501c5be15fc6504aedc6af3` + "\r\n" +
+ `Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes` + "\r\n" +
+ `` + "\r\n" +
+ `russcox@gmail.com, you have no events scheduled today Sun Oct 9, 2011.` + "\r\n" +
+ `` + "\r\n" +
+ `View your calendar at https://www.google.com/calendar/` + "\r\n" +
+ `` + "\r\n" +
+ `You are receiving this email at the account russcox@gmail.com because you ` + "\r\n" +
+ `are subscribed to receive daily agendas for the following calendars: Russ ` + "\r\n" +
+ `Cox.` + "\r\n" +
+ `` + "\r\n" +
+ `To change which calendars you receive daily agendas for, please log in to ` + "\r\n" +
+ `https://www.google.com/calendar/ and change your notification settings for ` + "\r\n" +
+ `each calendar.` + "\r\n" +
+ `` + "\r\n" +
+ `--bcaec501c5be15fc6504aedc6af3` + "\r\n" +
+ `Content-Type: text/html; charset=ISO-8859-1` + "\r\n" +
+ `Content-Transfer-Encoding: quoted-printable` + "\r\n" +
+ `` + "\r\n" +
+ `<div style=3D"padding:10px 7px;font-size:14px;line-height:1.4;font-family:A=` + "\r\n" +
+ `rial,Sans-serif;text-align:left;bgcolor=3D#ffffff"><a href=3D"https://www.g=` + "\r\n" +
+ `oogle.com/calendar/"><img style=3D"border-width:0" src=3D"https://www.googl=` + "\r\n" +
+ `e.com/calendar/images/calendar_logo_sm_en.gif" alt=3D"Google Calendar"></a>` + "\r\n" +
+ `<p style=3D"margin:0;color:#0">russcox@gmail.com,&nbsp;you have no events s=` + "\r\n" +
+ `cheduled today <b>Sun Oct 9, 2011</b></p>` + "\r\n" +
+ `<p style=3D"font-family:Arial,Sans-serif;color:#666;font-size:11px">You are=` + "\r\n" +
+ ` receiving this email at the account russcox@gmail.com because you are subs=` + "\r\n" +
+ `cribed to receive daily agendas for the following calendars: Russ Cox.</p>` + "\r\n" +
+ `<p style=3D"font-family:Arial,Sans-serif;color:#666;font-size:11px">To chan=` + "\r\n" +
+ `ge which calendars you receive daily agendas for, please log in to https://=` + "\r\n" +
+ `www.google.com/calendar/ and change your notification settings for each cal=` + "\r\n" +
+ `endar.</p></div>` + "\r\n" +
+ `--bcaec501c5be15fc6504aedc6af3--` + "\r\n" +
+ `)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[HEADER]`: `* 146 FETCH (UID 57046 BODY[HEADER] {2453}` + "\r\n" +
+ `Delivered-To: rsc@swtch.com` + "\r\n" +
+ `Received: by 10.216.54.148 with SMTP id i20cs32329wec; Sun, 9 Oct 2011` + "\r\n" +
+ ` 05:00:30 -0700 (PDT)` + "\r\n" +
+ `Received: by 10.227.11.2 with SMTP id r2mr4751812wbr.43.1318161630585; Sun, 09` + "\r\n" +
+ ` Oct 2011 05:00:30 -0700 (PDT)` + "\r\n" +
+ `DomainKey-Status: good` + "\r\n" +
+ `Received-SPF: softfail (google.com: best guess record for domain of` + "\r\n" +
+ ` transitioning` + "\r\n" +
+ ` 3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com` + "\r\n" +
+ ` does not designate <unknown> as permitted sender)` + "\r\n" +
+ `Received: by 10.241.227.90 with POP3 id 26mf2646912wyj.48; Sun, 09 Oct 2011` + "\r\n" +
+ ` 05:00:29 -0700 (PDT)` + "\r\n" +
+ `X-Gmail-Fetch-Info: russcox@gmail.com 1 smtp.gmail.com 995 russcox` + "\r\n" +
+ `Delivered-To: russcox@gmail.com` + "\r\n" +
+ `Received: by 10.142.76.10 with SMTP id y10cs75487wfa; Sun, 9 Oct 2011 05:00:08` + "\r\n" +
+ ` -0700 (PDT)` + "\r\n" +
+ `Return-Path: <3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com>` + "\r\n" +
+ `Received-SPF: pass (google.com: domain of` + "\r\n" +
+ ` 3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com` + "\r\n" +
+ ` designates 10.52.73.100 as permitted sender) client-ip=10.52.73.100;` + "\r\n" +
+ `Authentication-Results: mr.google.com; spf=pass (google.com: domain of` + "\r\n" +
+ ` 3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com` + "\r\n" +
+ ` designates 10.52.73.100 as permitted sender)` + "\r\n" +
+ ` smtp.mail=3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com;` + "\r\n" +
+ ` dkim=pass` + "\r\n" +
+ ` header.i=3woyRTgcJB5sMPNN7JSBH5DG.7JHMPNN7JSBH5DG.7JH@calendar-server.bounces.google.com` + "\r\n" +
+ `Received: from mr.google.com ([10.52.73.100]) by 10.52.73.100 with SMTP id` + "\r\n" +
+ ` k4mr8053242vdv.5.1318161606360 (num_hops = 1); Sun, 09 Oct 2011 05:00:06` + "\r\n" +
+ ` -0700 (PDT)` + "\r\n" +
+ `DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=beta;` + "\r\n" +
+ ` h=mime-version:reply-to:auto-submitted:message-id:date:subject:from` + "\r\n" +
+ ` :to:content-type; bh=SGjz0F4q+eFVkoC4yzLKQKvlxTKiUsYbO/KPI+3KOE8=;` + "\r\n" +
+ ` b=LRBkWBW7ZZ4UJYa7b92zfHa0ZM1K1d0wP/jbgmDw2OZTWtgDICZb30dzhFUfNVdxeN` + "\r\n" +
+ ` kdMFbRhTLP5NpSXWhbDw==` + "\r\n" +
+ `MIME-Version: 1.0` + "\r\n" +
+ `Received: by 10.52.73.100 with SMTP id k4mr5244039vdv.5.1318161602706; Sun, 09` + "\r\n" +
+ ` Oct 2011 05:00:02 -0700 (PDT)` + "\r\n" +
+ `Reply-To: Russ Cox <russcox@gmail.com>` + "\r\n" +
+ `Auto-Submitted: auto-generated` + "\r\n" +
+ `Message-ID: <bcaec501c5be15fc7204aedc6af6@google.com>` + "\r\n" +
+ `Date: Sun, 09 Oct 2011 12:00:02 +0000` + "\r\n" +
+ `Subject: You have no events scheduled today.` + "\r\n" +
+ `From: Google Calendar <calendar-notification@google.com>` + "\r\n" +
+ `To: Russ Cox <russcox@gmail.com>` + "\r\n" +
+ `Content-Type: multipart/alternative; boundary=bcaec501c5be15fc6504aedc6af3` + "\r\n" +
+ `` + "\r\n" +
+ `)` + "\r\n" +
+ "# OK Success\r\n",
+ `# UID FETCH 57046 BODY[MIME]`: "# BAD Could not parse command\r\n",
+}
+
+/*
+ mail sending
+
+package main
+
+import (
+ "log"
+ "io/ioutil"
+ "smtp"
+ "time"
+)
+var pw, _ = ioutil.ReadFile("/Users/rsc/.swtchpass")
+var msg = `From: "Russ Cox" <rsc@golang.org>
+To: "Russ Cox" <rsc@google.com>
+Subject: test from Go
+
+This is a message sent from Go
+`
+
+BUG: Does not *REQUIRE* auth. Should.
+
+func main() {
+ auth := smtp.PlainAuth(
+ "",
+ "rsc@swtch.com",
+ string(pw),
+ "smtp.gmail.com",
+ )
+ if err := smtp.SendMail("smtp.gmail.com:587", auth, "rsc@swtch.com", []string{"rsc@google.com"}, []byte(msg+time.LocalTime().String())); err != nil {
+ log.Fatal(err)
+ }
+ println("SENT")
+}
+*/
diff --git a/vendor/github.com/mattermost/rsc/imap/mail.go b/vendor/github.com/mattermost/rsc/imap/mail.go
new file mode 100644
index 000000000..365540f82
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/mail.go
@@ -0,0 +1,468 @@
+package imap
+
+import (
+ "bytes"
+ "fmt"
+ "log"
+ "regexp"
+ "sort"
+ "strings"
+ "time"
+)
+
+type Flags uint32
+
+const (
+ FlagJunk Flags = 1 << iota
+ FlagNonJunk
+ FlagReplied
+ FlagFlagged
+ FlagDeleted
+ FlagDraft
+ FlagRecent
+ FlagSeen
+ FlagNoInferiors
+ FlagNoSelect
+ FlagMarked
+ FlagUnMarked
+ FlagHasChildren
+ FlagHasNoChildren
+ FlagInbox // Gmail extension
+ FlagAllMail // Gmail extension
+ FlagDrafts // Gmail extension
+ FlagSent // Gmail extension
+ FlagSpam // Gmail extension
+ FlagStarred // Gmail extension
+ FlagTrash // Gmail extension
+ FlagImportant // Gmail extension
+)
+
+var flagNames = []string{
+ "Junk",
+ "NonJunk",
+ "\\Answered",
+ "\\Flagged",
+ "\\Deleted",
+ "\\Draft",
+ "\\Recent",
+ "\\Seen",
+ "\\NoInferiors",
+ "\\NoSelect",
+ "\\Marked",
+ "\\UnMarked",
+ "\\HasChildren",
+ "\\HasNoChildren",
+ "\\Inbox",
+ "\\AllMail",
+ "\\Drafts",
+ "\\Sent",
+ "\\Spam",
+ "\\Starred",
+ "\\Trash",
+ "\\Important",
+}
+
+// A Box represents an IMAP mailbox.
+type Box struct {
+ Name string // name of mailbox
+ Elem string // last element in name
+ Client *Client
+
+ parent *Box // parent in hierarchy
+ child []*Box // child boxes
+ dead bool // box no longer exists
+ inbox bool // box is inbox
+ flags Flags // allowed flags
+ permFlags Flags // client-modifiable permanent flags
+ readOnly bool // box is read-only
+
+ exists int // number of messages in box (according to server)
+ maxSeen int // maximum message number seen (for polling)
+ unseen int // number of first unseen message
+ validity uint32 // UID validity base number
+ load bool // if false, don't track full set of messages
+ firstNum int // 0 means box not loaded
+ msgByNum []*Msg
+ msgByUID map[uint64]*Msg
+}
+
+func (c *Client) Boxes() []*Box {
+ c.data.lock()
+ defer c.data.unlock()
+
+ box := make([]*Box, len(c.allBox))
+ copy(box, c.allBox)
+ return box
+}
+
+func (c *Client) Box(name string) *Box {
+ c.data.lock()
+ defer c.data.unlock()
+
+ return c.boxByName[name]
+}
+
+func (c *Client) Inbox() *Box {
+ c.data.lock()
+ defer c.data.unlock()
+
+ return c.inbox
+}
+
+func (c *Client) newBox(name, sep string, inbox bool) *Box {
+ c.data.mustBeLocked()
+ if b := c.boxByName[name]; b != nil {
+ return b
+ }
+
+ b := &Box{
+ Name: name,
+ Elem: name,
+ Client: c,
+ inbox: inbox,
+ }
+ if !inbox {
+ b.parent = c.rootBox
+ }
+ if !inbox && sep != "" && name != c.root {
+ if i := strings.LastIndex(name, sep); i >= 0 {
+ b.Elem = name[i+len(sep):]
+ b.parent = c.newBox(name[:i], sep, false)
+ }
+ }
+ c.allBox = append(c.allBox, b)
+ c.boxByName[name] = b
+ if b.parent != nil {
+ b.parent.child = append(b.parent.child, b)
+ }
+ return b
+}
+
+// A Msg represents an IMAP message.
+type Msg struct {
+ Box *Box // box containing message
+ Date time.Time // date
+ Flags Flags // message flags
+ Bytes int64 // size in bytes
+ Lines int64 // number of lines
+ Hdr *MsgHdr // MIME header
+ Root MsgPart // top-level message part
+ GmailID uint64 // Gmail message id
+ GmailThread uint64 // Gmail thread id
+ UID uint64 // unique id for this message
+
+ deleted bool
+ dead bool
+ num int // message number in box (changes)
+}
+
+// TODO: Return os.Error too
+
+type byUID []*Msg
+
+func (x byUID) Len() int { return len(x) }
+func (x byUID) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
+func (x byUID) Less(i, j int) bool { return x[i].UID < x[j].UID }
+
+func (b *Box) Msgs() []*Msg {
+ b.Client.data.lock()
+ defer b.Client.data.unlock()
+
+ msgs := make([]*Msg, len(b.msgByUID))
+ n := 0
+ for _, m := range b.msgByUID {
+ msgs[n] = m
+ n++
+ }
+ sort.Sort(byUID(msgs))
+ return msgs
+}
+
+func (b *Box) newMsg(uid uint64, id int) *Msg {
+ b.Client.data.mustBeLocked()
+ if m := b.msgByUID[uid]; m != nil {
+ return m
+ }
+ if b.msgByUID == nil {
+ b.msgByUID = map[uint64]*Msg{}
+ }
+ m := &Msg{
+ UID: uid,
+ Box: b,
+ num: id,
+ }
+ m.Root.Msg = m
+ if b.load {
+ if b.firstNum == 0 {
+ b.firstNum = id
+ }
+ if id < b.firstNum {
+ log.Printf("warning: unexpected id %d < %d", id, b.firstNum)
+ byNum := make([]*Msg, len(b.msgByNum)+b.firstNum-id)
+ copy(byNum[b.firstNum-id:], b.msgByNum)
+ b.msgByNum = byNum
+ b.firstNum = id
+ }
+ if id-b.firstNum < len(b.msgByNum) {
+ b.msgByNum[id-b.firstNum] = m
+ } else {
+ if id-b.firstNum > len(b.msgByNum) {
+ log.Printf("warning: unexpected id %d > %d", id, b.firstNum+len(b.msgByNum))
+ byNum := make([]*Msg, id-b.firstNum)
+ copy(byNum, b.msgByNum)
+ b.msgByNum = byNum
+ }
+ b.msgByNum = append(b.msgByNum, m)
+ }
+ }
+ b.msgByUID[uid] = m
+ return m
+}
+
+func (b *Box) Delete(msgs []*Msg) error {
+ for _, m := range msgs {
+ if m.Box != b {
+ return fmt.Errorf("messages not from this box")
+ }
+ }
+ b.Client.io.lock()
+ defer b.Client.io.unlock()
+ err := b.Client.deleteList(msgs)
+ if err == nil {
+ b.Client.data.lock()
+ defer b.Client.data.unlock()
+ for _, m := range msgs {
+ if m.Flags&FlagDeleted != 0 {
+ delete(b.msgByUID, m.UID)
+ }
+ }
+ }
+ return err
+}
+
+func (b *Box) Copy(msgs []*Msg) error {
+ if len(msgs) == 0 {
+ return nil
+ }
+ src := msgs[0].Box
+ for _, m := range msgs {
+ if m.Box != src {
+ return fmt.Errorf("messages span boxes: %q and %q", src.Name, m.Box.Name)
+ }
+ }
+ b.Client.io.lock()
+ defer b.Client.io.unlock()
+ return b.Client.copyList(b, src, msgs)
+}
+
+func (b *Box) Mute(msgs []*Msg) error {
+ if len(msgs) == 0 {
+ return nil
+ }
+ for _, m := range msgs {
+ if m.Box != b {
+ return fmt.Errorf("messages not from this box")
+ }
+ }
+ b.Client.io.lock()
+ defer b.Client.io.unlock()
+ return b.Client.muteList(b, msgs)
+}
+
+func (b *Box) Check() error {
+ b.Client.io.lock()
+ defer b.Client.io.unlock()
+
+ return b.Client.check(b)
+}
+
+func (m *Msg) Deleted() bool {
+ // Racy but okay. Can add a lock later if it matters.
+ return m.Flags&FlagDeleted != 0
+}
+
+// A Hdr represents a message header.
+type MsgHdr struct {
+ Date string
+ Subject string
+ From []Addr
+ Sender []Addr
+ ReplyTo []Addr
+ To []Addr
+ CC []Addr
+ BCC []Addr
+ InReplyTo string
+ MessageID string
+ Digest string
+}
+
+// An Addr represents a single, named email address.
+// If Name is empty, only the email address is known.
+// If Email is empty, the Addr represents an unspecified (but named) group.
+type Addr struct {
+ Name string
+ Email string
+}
+
+func (a Addr) String() string {
+ if a.Email == "" {
+ return a.Name
+ }
+ if a.Name == "" {
+ return a.Email
+ }
+ return a.Name + " <" + a.Email + ">"
+}
+
+// A MsgPart represents a single part of a MIME-encoded message.
+type MsgPart struct {
+ Msg *Msg // containing message
+ Type string
+ ContentID string
+ Desc string
+ Encoding string
+ Bytes int64
+ Lines int64
+ Charset string
+ Name string
+ Hdr *MsgHdr
+ ID string
+ Child []*MsgPart
+
+ raw []byte // raw message
+ rawHeader []byte // raw RFC-2822 header, for message/rfc822
+ rawBody []byte // raw RFC-2822 body, for message/rfc822
+ mimeHeader []byte // mime header, for attachments
+}
+
+func (p *MsgPart) newPart() *MsgPart {
+ p.Msg.Box.Client.data.mustBeLocked()
+ dot := "."
+ if p.ID == "" { // no dot at root
+ dot = ""
+ }
+ pp := &MsgPart{
+ Msg: p.Msg,
+ ID: fmt.Sprint(p.ID, dot, 1+len(p.Child)),
+ }
+ p.Child = append(p.Child, pp)
+ return pp
+}
+
+func (p *MsgPart) Text() []byte {
+ c := p.Msg.Box.Client
+ var raw []byte
+ c.data.lock()
+ if p == &p.Msg.Root {
+ raw = p.rawBody
+ c.data.unlock()
+ if raw == nil {
+ c.io.lock()
+ if raw = p.rawBody; raw == nil {
+ c.fetch(p, "TEXT")
+ raw = p.rawBody
+ }
+ c.io.unlock()
+ }
+ } else {
+ raw = p.raw
+ c.data.unlock()
+ if raw == nil {
+ c.io.lock()
+ if raw = p.raw; raw == nil {
+ c.fetch(p, "")
+ raw = p.raw
+ }
+ c.io.unlock()
+ }
+ }
+ return decodeText(raw, p.Encoding, p.Charset, false)
+}
+
+func (p *MsgPart) Raw() []byte {
+ c := p.Msg.Box.Client
+ var raw []byte
+ c.data.lock()
+ raw = p.rawBody
+ c.data.unlock()
+ if raw == nil {
+ c.io.lock()
+ if raw = p.rawBody; raw == nil {
+ c.fetch(p, "")
+ raw = p.rawBody
+ }
+ c.io.unlock()
+ }
+ return raw
+}
+
+var sigDash = []byte("\n--\n")
+var quote = []byte("\n> ")
+var nl = []byte("\n")
+
+var onwrote = regexp.MustCompile(`\A\s*On .* wrote:\s*\z`)
+
+func (p *MsgPart) ShortText() []byte {
+ t := p.Text()
+
+ return shortText(t)
+}
+
+func shortText(t []byte) []byte {
+ if t == nil {
+ return nil
+ }
+
+ // Cut signature.
+ i := bytes.LastIndex(t, sigDash)
+ j := bytes.LastIndex(t, quote)
+ if i > j && bytes.Count(t[i+1:], nl) <= 10 {
+ t = t[:i+1]
+ }
+
+ // Cut trailing quoted text.
+ for {
+ rest, last := lastLine(t)
+ trim := bytes.TrimSpace(last)
+ if len(rest) < len(t) && (len(trim) == 0 || trim[0] == '>') {
+ t = rest
+ continue
+ }
+ break
+ }
+
+ // Cut 'On foo.*wrote:' line.
+ rest, last := lastLine(t)
+ if onwrote.Match(last) {
+ t = rest
+ }
+
+ // Cut trailing blank lines.
+ for {
+ rest, last := lastLine(t)
+ trim := bytes.TrimSpace(last)
+ if len(rest) < len(t) && len(trim) == 0 {
+ t = rest
+ continue
+ }
+ break
+ }
+
+ // Cut signature again.
+ i = bytes.LastIndex(t, sigDash)
+ j = bytes.LastIndex(t, quote)
+ if i > j && bytes.Count(t[i+1:], nl) <= 10 {
+ t = t[:i+1]
+ }
+
+ return t
+}
+
+func lastLine(t []byte) (rest, last []byte) {
+ n := len(t)
+ if n > 0 && t[n-1] == '\n' {
+ n--
+ }
+ j := bytes.LastIndex(t[:n], nl)
+ return t[:j+1], t[j+1:]
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/mail_test.go b/vendor/github.com/mattermost/rsc/imap/mail_test.go
new file mode 100644
index 000000000..3c1aec860
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/mail_test.go
@@ -0,0 +1,335 @@
+package imap
+
+import "testing"
+
+var shortTextTests = []struct {
+ in, out string
+}{
+ {
+ in: `From: Brad Fitzpatrick <bradfitz@golang.org>
+Date: Tue Oct 18 18:23:11 EDT 2011
+To: r@golang.org, golang-dev@googlegroups.com, reply@codereview.appspotmail.com
+Subject: Re: [golang-dev] code review 5307043: rpc: don't panic on write error. (issue 5307043)
+
+Here's a test:
+
+bradfitz@gopher:~/go/src/pkg/rpc$ hg diff
+diff -r b7f9a5e9b87f src/pkg/rpc/server_test.go
+--- a/src/pkg/rpc/server_test.go Tue Oct 18 17:01:42 2011 -0500
++++ b/src/pkg/rpc/server_test.go Tue Oct 18 15:22:19 2011 -0700
+@@ -467,6 +467,27 @@
+ fmt.Printf("mallocs per HTTP rpc round trip: %d\n",
+countMallocs(dialHTTP, t))
+ }
+
++type writeCrasher struct{}
++
++func (writeCrasher) Close() os.Error {
++ return nil
++}
++
++func (writeCrasher) Read(p []byte) (int, os.Error) {
++ return 0, os.EOF
++}
++
++func (writeCrasher) Write(p []byte) (int, os.Error) {
++ return 0, os.NewError("fake write failure")
++}
++
++func TestClientWriteError(t *testing.T) {
++ c := NewClient(writeCrasher{})
++ res := false
++ c.Call("foo", 1, &res)
++}
++
+ func benchmarkEndToEnd(dial func() (*Client, os.Error), b *testing.B) {
+ b.StopTimer()
+ once.Do(startServer)
+
+
+On Tue, Oct 18, 2011 at 3:12 PM, <r@golang.org> wrote:
+
+> Reviewers: golang-dev_googlegroups.com,
+>
+> Message:
+> Hello golang-dev@googlegroups.com,
+>
+> I'd like you to review this change to
+> https://go.googlecode.com/hg/
+>
+>
+> Description:
+> rpc: don't panic on write error.
+> The mechanism to record the error in the call is already in place.
+> Fixes issue 2382.
+>
+> Please review this at http://codereview.appspot.com/**5307043/<http://codereview.appspot.com/5307043/>
+>
+> Affected files:
+> M src/pkg/rpc/client.go
+>
+>
+> Index: src/pkg/rpc/client.go
+> ==============================**==============================**=======
+> --- a/src/pkg/rpc/client.go
+> +++ b/src/pkg/rpc/client.go
+> @@ -85,7 +85,8 @@
+> client.request.Seq = c.seq
+> client.request.ServiceMethod = c.ServiceMethod
+> if err := client.codec.WriteRequest(&**client.request, c.Args); err
+> != nil {
+> - panic("rpc: client encode error: " + err.String())
+> + c.Error = err
+> + c.done()
+> }
+> }
+>
+> @@ -251,10 +252,10 @@
+> // the same Call object. If done is nil, Go will allocate a new channel.
+> // If non-nil, done must be buffered or Go will deliberately crash.
+> func (client *Client) Go(serviceMethod string, args interface{}, reply
+> interface{}, done chan *Call) *Call {
+> - c := new(Call)
+> - c.ServiceMethod = serviceMethod
+> - c.Args = args
+> - c.Reply = reply
+> + call := new(Call)
+> + call.ServiceMethod = serviceMethod
+> + call.Args = args
+> + call.Reply = reply
+> if done == nil {
+> done = make(chan *Call, 10) // buffered.
+> } else {
+> @@ -266,14 +267,14 @@
+> log.Panic("rpc: done channel is unbuffered")
+> }
+> }
+> - c.Done = done
+> + call.Done = done
+> if client.shutdown {
+> - c.Error = ErrShutdown
+> - c.done()
+> - return c
+> + call.Error = ErrShutdown
+> + call.done()
+> + return call
+> }
+> - client.send(c)
+> - return c
+> + client.send(call)
+> + return call
+> }
+>
+> // Call invokes the named function, waits for it to complete, and returns
+> its error status.
+>
+>
+>
+
+`,
+ out: `From: Brad Fitzpatrick <bradfitz@golang.org>
+Date: Tue Oct 18 18:23:11 EDT 2011
+To: r@golang.org, golang-dev@googlegroups.com, reply@codereview.appspotmail.com
+Subject: Re: [golang-dev] code review 5307043: rpc: don't panic on write error. (issue 5307043)
+
+Here's a test:
+
+bradfitz@gopher:~/go/src/pkg/rpc$ hg diff
+diff -r b7f9a5e9b87f src/pkg/rpc/server_test.go
+--- a/src/pkg/rpc/server_test.go Tue Oct 18 17:01:42 2011 -0500
++++ b/src/pkg/rpc/server_test.go Tue Oct 18 15:22:19 2011 -0700
+@@ -467,6 +467,27 @@
+ fmt.Printf("mallocs per HTTP rpc round trip: %d\n",
+countMallocs(dialHTTP, t))
+ }
+
++type writeCrasher struct{}
++
++func (writeCrasher) Close() os.Error {
++ return nil
++}
++
++func (writeCrasher) Read(p []byte) (int, os.Error) {
++ return 0, os.EOF
++}
++
++func (writeCrasher) Write(p []byte) (int, os.Error) {
++ return 0, os.NewError("fake write failure")
++}
++
++func TestClientWriteError(t *testing.T) {
++ c := NewClient(writeCrasher{})
++ res := false
++ c.Call("foo", 1, &res)
++}
++
+ func benchmarkEndToEnd(dial func() (*Client, os.Error), b *testing.B) {
+ b.StopTimer()
+ once.Do(startServer)
+`,
+ },
+ {
+ in: `From: David Symonds <dsymonds@golang.org>
+Date: Tue Oct 18 18:17:52 EDT 2011
+To: reply@codereview.appspotmail.com, r@golang.org, golang-dev@googlegroups.com
+Subject: Re: [golang-dev] code review 5307043: rpc: don't panic on write error. (issue 5307043)
+
+LGTM
+On Oct 19, 2011 9:12 AM, <r@golang.org> wrote:
+
+> Reviewers: golang-dev_googlegroups.com,
+>
+> Message:
+> Hello golang-dev@googlegroups.com,
+>
+> I'd like you to review this change to
+> https://go.googlecode.com/hg/
+>
+>
+> Description:
+> rpc: don't panic on write error.
+> The mechanism to record the error in the call is already in place.
+> Fixes issue 2382.
+>
+> Please review this at http://codereview.appspot.com/**5307043/<http://codereview.appspot.com/5307043/>
+>
+> Affected files:
+> M src/pkg/rpc/client.go
+>
+>
+> Index: src/pkg/rpc/client.go
+> ==============================**==============================**=======
+> --- a/src/pkg/rpc/client.go
+> +++ b/src/pkg/rpc/client.go
+> @@ -85,7 +85,8 @@
+> client.request.Seq = c.seq
+> client.request.ServiceMethod = c.ServiceMethod
+> if err := client.codec.WriteRequest(&**client.request, c.Args); err
+> != nil {
+> - panic("rpc: client encode error: " + err.String())
+> + c.Error = err
+> + c.done()
+> }
+> }
+>
+> @@ -251,10 +252,10 @@
+> // the same Call object. If done is nil, Go will allocate a new channel.
+> // If non-nil, done must be buffered or Go will deliberately crash.
+> func (client *Client) Go(serviceMethod string, args interface{}, reply
+> interface{}, done chan *Call) *Call {
+> - c := new(Call)
+> - c.ServiceMethod = serviceMethod
+> - c.Args = args
+> - c.Reply = reply
+> + call := new(Call)
+> + call.ServiceMethod = serviceMethod
+> + call.Args = args
+> + call.Reply = reply
+> if done == nil {
+> done = make(chan *Call, 10) // buffered.
+> } else {
+> @@ -266,14 +267,14 @@
+> log.Panic("rpc: done channel is unbuffered")
+> }
+> }
+> - c.Done = done
+> + call.Done = done
+> if client.shutdown {
+> - c.Error = ErrShutdown
+> - c.done()
+> - return c
+> + call.Error = ErrShutdown
+> + call.done()
+> + return call
+> }
+> - client.send(c)
+> - return c
+> + client.send(call)
+> + return call
+> }
+>
+> // Call invokes the named function, waits for it to complete, and returns
+> its error status.
+>
+>
+>
+
+`,
+ out: `From: David Symonds <dsymonds@golang.org>
+Date: Tue Oct 18 18:17:52 EDT 2011
+To: reply@codereview.appspotmail.com, r@golang.org, golang-dev@googlegroups.com
+Subject: Re: [golang-dev] code review 5307043: rpc: don't panic on write error. (issue 5307043)
+
+LGTM
+`,
+ },
+ {
+ in: `From: Brad Fitzpatrick <bradfitz@golang.org>
+Date: Tue Oct 18 23:26:07 EDT 2011
+To: rsc@golang.org, golang-dev@googlegroups.com, reply@codereview.appspotmail.com
+Subject: Re: [golang-dev] code review 5297044: gotest: use $GCFLAGS like make does (issue 5297044)
+
+LGTM
+
+On Tue, Oct 18, 2011 at 7:52 PM, <rsc@golang.org> wrote:
+
+> Reviewers: golang-dev_googlegroups.com,
+>
+> Message:
+> Hello golang-dev@googlegroups.com,
+>
+> I'd like you to review this change to
+> https://go.googlecode.com/hg/
+>
+>
+> Description:
+> gotest: use $GCFLAGS like make does
+>
+> Please review this at http://codereview.appspot.com/**5297044/<http://codereview.appspot.com/5297044/>
+>
+> Affected files:
+> M src/cmd/gotest/gotest.go
+>
+>
+> Index: src/cmd/gotest/gotest.go
+> ==============================**==============================**=======
+> --- a/src/cmd/gotest/gotest.go
+> +++ b/src/cmd/gotest/gotest.go
+> @@ -153,8 +153,12 @@
+> if gc == "" {
+> gc = O + "g"
+> }
+> - XGC = []string{gc, "-I", "_test", "-o", "_xtest_." + O}
+> - GC = []string{gc, "-I", "_test", "_testmain.go"}
+> + var gcflags []string
+> + if gf := strings.TrimSpace(os.Getenv("**GCFLAGS")); gf != "" {
+> + gcflags = strings.Fields(gf)
+> + }
+> + XGC = append([]string{gc, "-I", "_test", "-o", "_xtest_." + O},
+> gcflags...)
+> + GC = append(append([]string{gc, "-I", "_test"}, gcflags...),
+> "_testmain.go")
+> gl := os.Getenv("GL")
+> if gl == "" {
+> gl = O + "l"
+>
+>
+>
+`,
+ out: `From: Brad Fitzpatrick <bradfitz@golang.org>
+Date: Tue Oct 18 23:26:07 EDT 2011
+To: rsc@golang.org, golang-dev@googlegroups.com, reply@codereview.appspotmail.com
+Subject: Re: [golang-dev] code review 5297044: gotest: use $GCFLAGS like make does (issue 5297044)
+
+LGTM
+`,
+ },
+}
+
+func TestShortText(t *testing.T) {
+ for i, tt := range shortTextTests {
+ if out := string(shortText([]byte(tt.in))); out != tt.out {
+ t.Errorf("#%d: = %q, want %q\n", i, out, tt.out)
+ }
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/rfc2045.txt b/vendor/github.com/mattermost/rsc/imap/rfc2045.txt
new file mode 100644
index 000000000..9f286b1a9
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/rfc2045.txt
@@ -0,0 +1,1739 @@
+
+
+
+
+
+
+Network Working Group N. Freed
+Request for Comments: 2045 Innosoft
+Obsoletes: 1521, 1522, 1590 N. Borenstein
+Category: Standards Track First Virtual
+ November 1996
+
+
+ Multipurpose Internet Mail Extensions
+ (MIME) Part One:
+ Format of Internet Message Bodies
+
+Status of this Memo
+
+ This document specifies an Internet standards track protocol for the
+ Internet community, and requests discussion and suggestions for
+ improvements. Please refer to the current edition of the "Internet
+ Official Protocol Standards" (STD 1) for the standardization state
+ and status of this protocol. Distribution of this memo is unlimited.
+
+Abstract
+
+ STD 11, RFC 822, defines a message representation protocol specifying
+ considerable detail about US-ASCII message headers, and leaves the
+ message content, or message body, as flat US-ASCII text. This set of
+ documents, collectively called the Multipurpose Internet Mail
+ Extensions, or MIME, redefines the format of messages to allow for
+
+ (1) textual message bodies in character sets other than
+ US-ASCII,
+
+ (2) an extensible set of different formats for non-textual
+ message bodies,
+
+ (3) multi-part message bodies, and
+
+ (4) textual header information in character sets other than
+ US-ASCII.
+
+ These documents are based on earlier work documented in RFC 934, STD
+ 11, and RFC 1049, but extends and revises them. Because RFC 822 said
+ so little about message bodies, these documents are largely
+ orthogonal to (rather than a revision of) RFC 822.
+
+ This initial document specifies the various headers used to describe
+ the structure of MIME messages. The second document, RFC 2046,
+ defines the general structure of the MIME media typing system and
+ defines an initial set of media types. The third document, RFC 2047,
+ describes extensions to RFC 822 to allow non-US-ASCII text data in
+
+
+
+Freed & Borenstein Standards Track [Page 1]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ Internet mail header fields. The fourth document, RFC 2048, specifies
+ various IANA registration procedures for MIME-related facilities. The
+ fifth and final document, RFC 2049, describes MIME conformance
+ criteria as well as providing some illustrative examples of MIME
+ message formats, acknowledgements, and the bibliography.
+
+ These documents are revisions of RFCs 1521, 1522, and 1590, which
+ themselves were revisions of RFCs 1341 and 1342. An appendix in RFC
+ 2049 describes differences and changes from previous versions.
+
+Table of Contents
+
+ 1. Introduction ......................................... 3
+ 2. Definitions, Conventions, and Generic BNF Grammar .... 5
+ 2.1 CRLF ................................................ 5
+ 2.2 Character Set ....................................... 6
+ 2.3 Message ............................................. 6
+ 2.4 Entity .............................................. 6
+ 2.5 Body Part ........................................... 7
+ 2.6 Body ................................................ 7
+ 2.7 7bit Data ........................................... 7
+ 2.8 8bit Data ........................................... 7
+ 2.9 Binary Data ......................................... 7
+ 2.10 Lines .............................................. 7
+ 3. MIME Header Fields ................................... 8
+ 4. MIME-Version Header Field ............................ 8
+ 5. Content-Type Header Field ............................ 10
+ 5.1 Syntax of the Content-Type Header Field ............. 12
+ 5.2 Content-Type Defaults ............................... 14
+ 6. Content-Transfer-Encoding Header Field ............... 14
+ 6.1 Content-Transfer-Encoding Syntax .................... 14
+ 6.2 Content-Transfer-Encodings Semantics ................ 15
+ 6.3 New Content-Transfer-Encodings ...................... 16
+ 6.4 Interpretation and Use .............................. 16
+ 6.5 Translating Encodings ............................... 18
+ 6.6 Canonical Encoding Model ............................ 19
+ 6.7 Quoted-Printable Content-Transfer-Encoding .......... 19
+ 6.8 Base64 Content-Transfer-Encoding .................... 24
+ 7. Content-ID Header Field .............................. 26
+ 8. Content-Description Header Field ..................... 27
+ 9. Additional MIME Header Fields ........................ 27
+ 10. Summary ............................................. 27
+ 11. Security Considerations ............................. 27
+ 12. Authors' Addresses .................................. 28
+ A. Collected Grammar .................................... 29
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 2]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+1. Introduction
+
+ Since its publication in 1982, RFC 822 has defined the standard
+ format of textual mail messages on the Internet. Its success has
+ been such that the RFC 822 format has been adopted, wholly or
+ partially, well beyond the confines of the Internet and the Internet
+ SMTP transport defined by RFC 821. As the format has seen wider use,
+ a number of limitations have proven increasingly restrictive for the
+ user community.
+
+ RFC 822 was intended to specify a format for text messages. As such,
+ non-text messages, such as multimedia messages that might include
+ audio or images, are simply not mentioned. Even in the case of text,
+ however, RFC 822 is inadequate for the needs of mail users whose
+ languages require the use of character sets richer than US-ASCII.
+ Since RFC 822 does not specify mechanisms for mail containing audio,
+ video, Asian language text, or even text in most European languages,
+ additional specifications are needed.
+
+ One of the notable limitations of RFC 821/822 based mail systems is
+ the fact that they limit the contents of electronic mail messages to
+ relatively short lines (e.g. 1000 characters or less [RFC-821]) of
+ 7bit US-ASCII. This forces users to convert any non-textual data
+ that they may wish to send into seven-bit bytes representable as
+ printable US-ASCII characters before invoking a local mail UA (User
+ Agent, a program with which human users send and receive mail).
+ Examples of such encodings currently used in the Internet include
+ pure hexadecimal, uuencode, the 3-in-4 base 64 scheme specified in
+ RFC 1421, the Andrew Toolkit Representation [ATK], and many others.
+
+ The limitations of RFC 822 mail become even more apparent as gateways
+ are designed to allow for the exchange of mail messages between RFC
+ 822 hosts and X.400 hosts. X.400 [X400] specifies mechanisms for the
+ inclusion of non-textual material within electronic mail messages.
+ The current standards for the mapping of X.400 messages to RFC 822
+ messages specify either that X.400 non-textual material must be
+ converted to (not encoded in) IA5Text format, or that they must be
+ discarded, notifying the RFC 822 user that discarding has occurred.
+ This is clearly undesirable, as information that a user may wish to
+ receive is lost. Even though a user agent may not have the
+ capability of dealing with the non-textual material, the user might
+ have some mechanism external to the UA that can extract useful
+ information from the material. Moreover, it does not allow for the
+ fact that the message may eventually be gatewayed back into an X.400
+ message handling system (i.e., the X.400 message is "tunneled"
+ through Internet mail), where the non-textual information would
+ definitely become useful again.
+
+
+
+
+Freed & Borenstein Standards Track [Page 3]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ This document describes several mechanisms that combine to solve most
+ of these problems without introducing any serious incompatibilities
+ with the existing world of RFC 822 mail. In particular, it
+ describes:
+
+ (1) A MIME-Version header field, which uses a version
+ number to declare a message to be conformant with MIME
+ and allows mail processing agents to distinguish
+ between such messages and those generated by older or
+ non-conformant software, which are presumed to lack
+ such a field.
+
+ (2) A Content-Type header field, generalized from RFC 1049,
+ which can be used to specify the media type and subtype
+ of data in the body of a message and to fully specify
+ the native representation (canonical form) of such
+ data.
+
+ (3) A Content-Transfer-Encoding header field, which can be
+ used to specify both the encoding transformation that
+ was applied to the body and the domain of the result.
+ Encoding transformations other than the identity
+ transformation are usually applied to data in order to
+ allow it to pass through mail transport mechanisms
+ which may have data or character set limitations.
+
+ (4) Two additional header fields that can be used to
+ further describe the data in a body, the Content-ID and
+ Content-Description header fields.
+
+ All of the header fields defined in this document are subject to the
+ general syntactic rules for header fields specified in RFC 822. In
+ particular, all of these header fields except for Content-Disposition
+ can include RFC 822 comments, which have no semantic content and
+ should be ignored during MIME processing.
+
+ Finally, to specify and promote interoperability, RFC 2049 provides a
+ basic applicability statement for a subset of the above mechanisms
+ that defines a minimal level of "conformance" with this document.
+
+ HISTORICAL NOTE: Several of the mechanisms described in this set of
+ documents may seem somewhat strange or even baroque at first reading.
+ It is important to note that compatibility with existing standards
+ AND robustness across existing practice were two of the highest
+ priorities of the working group that developed this set of documents.
+ In particular, compatibility was always favored over elegance.
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 4]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ Please refer to the current edition of the "Internet Official
+ Protocol Standards" for the standardization state and status of this
+ protocol. RFC 822 and STD 3, RFC 1123 also provide essential
+ background for MIME since no conforming implementation of MIME can
+ violate them. In addition, several other informational RFC documents
+ will be of interest to the MIME implementor, in particular RFC 1344,
+ RFC 1345, and RFC 1524.
+
+2. Definitions, Conventions, and Generic BNF Grammar
+
+ Although the mechanisms specified in this set of documents are all
+ described in prose, most are also described formally in the augmented
+ BNF notation of RFC 822. Implementors will need to be familiar with
+ this notation in order to understand this set of documents, and are
+ referred to RFC 822 for a complete explanation of the augmented BNF
+ notation.
+
+ Some of the augmented BNF in this set of documents makes named
+ references to syntax rules defined in RFC 822. A complete formal
+ grammar, then, is obtained by combining the collected grammar
+ appendices in each document in this set with the BNF of RFC 822 plus
+ the modifications to RFC 822 defined in RFC 1123 (which specifically
+ changes the syntax for `return', `date' and `mailbox').
+
+ All numeric and octet values are given in decimal notation in this
+ set of documents. All media type values, subtype values, and
+ parameter names as defined are case-insensitive. However, parameter
+ values are case-sensitive unless otherwise specified for the specific
+ parameter.
+
+ FORMATTING NOTE: Notes, such at this one, provide additional
+ nonessential information which may be skipped by the reader without
+ missing anything essential. The primary purpose of these non-
+ essential notes is to convey information about the rationale of this
+ set of documents, or to place these documents in the proper
+ historical or evolutionary context. Such information may in
+ particular be skipped by those who are focused entirely on building a
+ conformant implementation, but may be of use to those who wish to
+ understand why certain design choices were made.
+
+2.1. CRLF
+
+ The term CRLF, in this set of documents, refers to the sequence of
+ octets corresponding to the two US-ASCII characters CR (decimal value
+ 13) and LF (decimal value 10) which, taken together, in this order,
+ denote a line break in RFC 822 mail.
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 5]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+2.2. Character Set
+
+ The term "character set" is used in MIME to refer to a method of
+ converting a sequence of octets into a sequence of characters. Note
+ that unconditional and unambiguous conversion in the other direction
+ is not required, in that not all characters may be representable by a
+ given character set and a character set may provide more than one
+ sequence of octets to represent a particular sequence of characters.
+
+ This definition is intended to allow various kinds of character
+ encodings, from simple single-table mappings such as US-ASCII to
+ complex table switching methods such as those that use ISO 2022's
+ techniques, to be used as character sets. However, the definition
+ associated with a MIME character set name must fully specify the
+ mapping to be performed. In particular, use of external profiling
+ information to determine the exact mapping is not permitted.
+
+ NOTE: The term "character set" was originally to describe such
+ straightforward schemes as US-ASCII and ISO-8859-1 which have a
+ simple one-to-one mapping from single octets to single characters.
+ Multi-octet coded character sets and switching techniques make the
+ situation more complex. For example, some communities use the term
+ "character encoding" for what MIME calls a "character set", while
+ using the phrase "coded character set" to denote an abstract mapping
+ from integers (not octets) to characters.
+
+2.3. Message
+
+ The term "message", when not further qualified, means either a
+ (complete or "top-level") RFC 822 message being transferred on a
+ network, or a message encapsulated in a body of type "message/rfc822"
+ or "message/partial".
+
+2.4. Entity
+
+ The term "entity", refers specifically to the MIME-defined header
+ fields and contents of either a message or one of the parts in the
+ body of a multipart entity. The specification of such entities is
+ the essence of MIME. Since the contents of an entity are often
+ called the "body", it makes sense to speak about the body of an
+ entity. Any sort of field may be present in the header of an entity,
+ but only those fields whose names begin with "content-" actually have
+ any MIME-related meaning. Note that this does NOT imply thay they
+ have no meaning at all -- an entity that is also a message has non-
+ MIME header fields whose meanings are defined by RFC 822.
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 6]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+2.5. Body Part
+
+ The term "body part" refers to an entity inside of a multipart
+ entity.
+
+2.6. Body
+
+ The term "body", when not further qualified, means the body of an
+ entity, that is, the body of either a message or of a body part.
+
+ NOTE: The previous four definitions are clearly circular. This is
+ unavoidable, since the overall structure of a MIME message is indeed
+ recursive.
+
+2.7. 7bit Data
+
+ "7bit data" refers to data that is all represented as relatively
+ short lines with 998 octets or less between CRLF line separation
+ sequences [RFC-821]. No octets with decimal values greater than 127
+ are allowed and neither are NULs (octets with decimal value 0). CR
+ (decimal value 13) and LF (decimal value 10) octets only occur as
+ part of CRLF line separation sequences.
+
+2.8. 8bit Data
+
+ "8bit data" refers to data that is all represented as relatively
+ short lines with 998 octets or less between CRLF line separation
+ sequences [RFC-821]), but octets with decimal values greater than 127
+ may be used. As with "7bit data" CR and LF octets only occur as part
+ of CRLF line separation sequences and no NULs are allowed.
+
+2.9. Binary Data
+
+ "Binary data" refers to data where any sequence of octets whatsoever
+ is allowed.
+
+2.10. Lines
+
+ "Lines" are defined as sequences of octets separated by a CRLF
+ sequences. This is consistent with both RFC 821 and RFC 822.
+ "Lines" only refers to a unit of data in a message, which may or may
+ not correspond to something that is actually displayed by a user
+ agent.
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 7]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+3. MIME Header Fields
+
+ MIME defines a number of new RFC 822 header fields that are used to
+ describe the content of a MIME entity. These header fields occur in
+ at least two contexts:
+
+ (1) As part of a regular RFC 822 message header.
+
+ (2) In a MIME body part header within a multipart
+ construct.
+
+ The formal definition of these header fields is as follows:
+
+ entity-headers := [ content CRLF ]
+ [ encoding CRLF ]
+ [ id CRLF ]
+ [ description CRLF ]
+ *( MIME-extension-field CRLF )
+
+ MIME-message-headers := entity-headers
+ fields
+ version CRLF
+ ; The ordering of the header
+ ; fields implied by this BNF
+ ; definition should be ignored.
+
+ MIME-part-headers := entity-headers
+ [ fields ]
+ ; Any field not beginning with
+ ; "content-" can have no defined
+ ; meaning and may be ignored.
+ ; The ordering of the header
+ ; fields implied by this BNF
+ ; definition should be ignored.
+
+ The syntax of the various specific MIME header fields will be
+ described in the following sections.
+
+4. MIME-Version Header Field
+
+ Since RFC 822 was published in 1982, there has really been only one
+ format standard for Internet messages, and there has been little
+ perceived need to declare the format standard in use. This document
+ is an independent specification that complements RFC 822. Although
+ the extensions in this document have been defined in such a way as to
+ be compatible with RFC 822, there are still circumstances in which it
+ might be desirable for a mail-processing agent to know whether a
+ message was composed with the new standard in mind.
+
+
+
+Freed & Borenstein Standards Track [Page 8]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ Therefore, this document defines a new header field, "MIME-Version",
+ which is to be used to declare the version of the Internet message
+ body format standard in use.
+
+ Messages composed in accordance with this document MUST include such
+ a header field, with the following verbatim text:
+
+ MIME-Version: 1.0
+
+ The presence of this header field is an assertion that the message
+ has been composed in compliance with this document.
+
+ Since it is possible that a future document might extend the message
+ format standard again, a formal BNF is given for the content of the
+ MIME-Version field:
+
+ version := "MIME-Version" ":" 1*DIGIT "." 1*DIGIT
+
+ Thus, future format specifiers, which might replace or extend "1.0",
+ are constrained to be two integer fields, separated by a period. If
+ a message is received with a MIME-version value other than "1.0", it
+ cannot be assumed to conform with this document.
+
+ Note that the MIME-Version header field is required at the top level
+ of a message. It is not required for each body part of a multipart
+ entity. It is required for the embedded headers of a body of type
+ "message/rfc822" or "message/partial" if and only if the embedded
+ message is itself claimed to be MIME-conformant.
+
+ It is not possible to fully specify how a mail reader that conforms
+ with MIME as defined in this document should treat a message that
+ might arrive in the future with some value of MIME-Version other than
+ "1.0".
+
+ It is also worth noting that version control for specific media types
+ is not accomplished using the MIME-Version mechanism. In particular,
+ some formats (such as application/postscript) have version numbering
+ conventions that are internal to the media format. Where such
+ conventions exist, MIME does nothing to supersede them. Where no
+ such conventions exist, a MIME media type might use a "version"
+ parameter in the content-type field if necessary.
+
+
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 9]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ NOTE TO IMPLEMENTORS: When checking MIME-Version values any RFC 822
+ comment strings that are present must be ignored. In particular, the
+ following four MIME-Version fields are equivalent:
+
+ MIME-Version: 1.0
+
+ MIME-Version: 1.0 (produced by MetaSend Vx.x)
+
+ MIME-Version: (produced by MetaSend Vx.x) 1.0
+
+ MIME-Version: 1.(produced by MetaSend Vx.x)0
+
+ In the absence of a MIME-Version field, a receiving mail user agent
+ (whether conforming to MIME requirements or not) may optionally
+ choose to interpret the body of the message according to local
+ conventions. Many such conventions are currently in use and it
+ should be noted that in practice non-MIME messages can contain just
+ about anything.
+
+ It is impossible to be certain that a non-MIME mail message is
+ actually plain text in the US-ASCII character set since it might well
+ be a message that, using some set of nonstandard local conventions
+ that predate MIME, includes text in another character set or non-
+ textual data presented in a manner that cannot be automatically
+ recognized (e.g., a uuencoded compressed UNIX tar file).
+
+5. Content-Type Header Field
+
+ The purpose of the Content-Type field is to describe the data
+ contained in the body fully enough that the receiving user agent can
+ pick an appropriate agent or mechanism to present the data to the
+ user, or otherwise deal with the data in an appropriate manner. The
+ value in this field is called a media type.
+
+ HISTORICAL NOTE: The Content-Type header field was first defined in
+ RFC 1049. RFC 1049 used a simpler and less powerful syntax, but one
+ that is largely compatible with the mechanism given here.
+
+ The Content-Type header field specifies the nature of the data in the
+ body of an entity by giving media type and subtype identifiers, and
+ by providing auxiliary information that may be required for certain
+ media types. After the media type and subtype names, the remainder
+ of the header field is simply a set of parameters, specified in an
+ attribute=value notation. The ordering of parameters is not
+ significant.
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 10]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ In general, the top-level media type is used to declare the general
+ type of data, while the subtype specifies a specific format for that
+ type of data. Thus, a media type of "image/xyz" is enough to tell a
+ user agent that the data is an image, even if the user agent has no
+ knowledge of the specific image format "xyz". Such information can
+ be used, for example, to decide whether or not to show a user the raw
+ data from an unrecognized subtype -- such an action might be
+ reasonable for unrecognized subtypes of text, but not for
+ unrecognized subtypes of image or audio. For this reason, registered
+ subtypes of text, image, audio, and video should not contain embedded
+ information that is really of a different type. Such compound
+ formats should be represented using the "multipart" or "application"
+ types.
+
+ Parameters are modifiers of the media subtype, and as such do not
+ fundamentally affect the nature of the content. The set of
+ meaningful parameters depends on the media type and subtype. Most
+ parameters are associated with a single specific subtype. However, a
+ given top-level media type may define parameters which are applicable
+ to any subtype of that type. Parameters may be required by their
+ defining content type or subtype or they may be optional. MIME
+ implementations must ignore any parameters whose names they do not
+ recognize.
+
+ For example, the "charset" parameter is applicable to any subtype of
+ "text", while the "boundary" parameter is required for any subtype of
+ the "multipart" media type.
+
+ There are NO globally-meaningful parameters that apply to all media
+ types. Truly global mechanisms are best addressed, in the MIME
+ model, by the definition of additional Content-* header fields.
+
+ An initial set of seven top-level media types is defined in RFC 2046.
+ Five of these are discrete types whose content is essentially opaque
+ as far as MIME processing is concerned. The remaining two are
+ composite types whose contents require additional handling by MIME
+ processors.
+
+ This set of top-level media types is intended to be substantially
+ complete. It is expected that additions to the larger set of
+ supported types can generally be accomplished by the creation of new
+ subtypes of these initial types. In the future, more top-level types
+ may be defined only by a standards-track extension to this standard.
+ If another top-level type is to be used for any reason, it must be
+ given a name starting with "X-" to indicate its non-standard status
+ and to avoid a potential conflict with a future official name.
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 11]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+5.1. Syntax of the Content-Type Header Field
+
+ In the Augmented BNF notation of RFC 822, a Content-Type header field
+ value is defined as follows:
+
+ content := "Content-Type" ":" type "/" subtype
+ *(";" parameter)
+ ; Matching of media type and subtype
+ ; is ALWAYS case-insensitive.
+
+ type := discrete-type / composite-type
+
+ discrete-type := "text" / "image" / "audio" / "video" /
+ "application" / extension-token
+
+ composite-type := "message" / "multipart" / extension-token
+
+ extension-token := ietf-token / x-token
+
+ ietf-token := <An extension token defined by a
+ standards-track RFC and registered
+ with IANA.>
+
+ x-token := <The two characters "X-" or "x-" followed, with
+ no intervening white space, by any token>
+
+ subtype := extension-token / iana-token
+
+ iana-token := <A publicly-defined extension token. Tokens
+ of this form must be registered with IANA
+ as specified in RFC 2048.>
+
+ parameter := attribute "=" value
+
+ attribute := token
+ ; Matching of attributes
+ ; is ALWAYS case-insensitive.
+
+ value := token / quoted-string
+
+ token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
+ or tspecials>
+
+ tspecials := "(" / ")" / "<" / ">" / "@" /
+ "," / ";" / ":" / "\" / <">
+ "/" / "[" / "]" / "?" / "="
+ ; Must be in quoted-string,
+ ; to use within parameter values
+
+
+
+Freed & Borenstein Standards Track [Page 12]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ Note that the definition of "tspecials" is the same as the RFC 822
+ definition of "specials" with the addition of the three characters
+ "/", "?", and "=", and the removal of ".".
+
+ Note also that a subtype specification is MANDATORY -- it may not be
+ omitted from a Content-Type header field. As such, there are no
+ default subtypes.
+
+ The type, subtype, and parameter names are not case sensitive. For
+ example, TEXT, Text, and TeXt are all equivalent top-level media
+ types. Parameter values are normally case sensitive, but sometimes
+ are interpreted in a case-insensitive fashion, depending on the
+ intended use. (For example, multipart boundaries are case-sensitive,
+ but the "access-type" parameter for message/External-body is not
+ case-sensitive.)
+
+ Note that the value of a quoted string parameter does not include the
+ quotes. That is, the quotation marks in a quoted-string are not a
+ part of the value of the parameter, but are merely used to delimit
+ that parameter value. In addition, comments are allowed in
+ accordance with RFC 822 rules for structured header fields. Thus the
+ following two forms
+
+ Content-type: text/plain; charset=us-ascii (Plain text)
+
+ Content-type: text/plain; charset="us-ascii"
+
+ are completely equivalent.
+
+ Beyond this syntax, the only syntactic constraint on the definition
+ of subtype names is the desire that their uses must not conflict.
+ That is, it would be undesirable to have two different communities
+ using "Content-Type: application/foobar" to mean two different
+ things. The process of defining new media subtypes, then, is not
+ intended to be a mechanism for imposing restrictions, but simply a
+ mechanism for publicizing their definition and usage. There are,
+ therefore, two acceptable mechanisms for defining new media subtypes:
+
+ (1) Private values (starting with "X-") may be defined
+ bilaterally between two cooperating agents without
+ outside registration or standardization. Such values
+ cannot be registered or standardized.
+
+ (2) New standard values should be registered with IANA as
+ described in RFC 2048.
+
+ The second document in this set, RFC 2046, defines the initial set of
+ media types for MIME.
+
+
+
+Freed & Borenstein Standards Track [Page 13]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+5.2. Content-Type Defaults
+
+ Default RFC 822 messages without a MIME Content-Type header are taken
+ by this protocol to be plain text in the US-ASCII character set,
+ which can be explicitly specified as:
+
+ Content-type: text/plain; charset=us-ascii
+
+ This default is assumed if no Content-Type header field is specified.
+ It is also recommend that this default be assumed when a
+ syntactically invalid Content-Type header field is encountered. In
+ the presence of a MIME-Version header field and the absence of any
+ Content-Type header field, a receiving User Agent can also assume
+ that plain US-ASCII text was the sender's intent. Plain US-ASCII
+ text may still be assumed in the absence of a MIME-Version or the
+ presence of an syntactically invalid Content-Type header field, but
+ the sender's intent might have been otherwise.
+
+6. Content-Transfer-Encoding Header Field
+
+ Many media types which could be usefully transported via email are
+ represented, in their "natural" format, as 8bit character or binary
+ data. Such data cannot be transmitted over some transfer protocols.
+ For example, RFC 821 (SMTP) restricts mail messages to 7bit US-ASCII
+ data with lines no longer than 1000 characters including any trailing
+ CRLF line separator.
+
+ It is necessary, therefore, to define a standard mechanism for
+ encoding such data into a 7bit short line format. Proper labelling
+ of unencoded material in less restrictive formats for direct use over
+ less restrictive transports is also desireable. This document
+ specifies that such encodings will be indicated by a new "Content-
+ Transfer-Encoding" header field. This field has not been defined by
+ any previous standard.
+
+6.1. Content-Transfer-Encoding Syntax
+
+ The Content-Transfer-Encoding field's value is a single token
+ specifying the type of encoding, as enumerated below. Formally:
+
+ encoding := "Content-Transfer-Encoding" ":" mechanism
+
+ mechanism := "7bit" / "8bit" / "binary" /
+ "quoted-printable" / "base64" /
+ ietf-token / x-token
+
+ These values are not case sensitive -- Base64 and BASE64 and bAsE64
+ are all equivalent. An encoding type of 7BIT requires that the body
+
+
+
+Freed & Borenstein Standards Track [Page 14]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ is already in a 7bit mail-ready representation. This is the default
+ value -- that is, "Content-Transfer-Encoding: 7BIT" is assumed if the
+ Content-Transfer-Encoding header field is not present.
+
+6.2. Content-Transfer-Encodings Semantics
+
+ This single Content-Transfer-Encoding token actually provides two
+ pieces of information. It specifies what sort of encoding
+ transformation the body was subjected to and hence what decoding
+ operation must be used to restore it to its original form, and it
+ specifies what the domain of the result is.
+
+ The transformation part of any Content-Transfer-Encodings specifies,
+ either explicitly or implicitly, a single, well-defined decoding
+ algorithm, which for any sequence of encoded octets either transforms
+ it to the original sequence of octets which was encoded, or shows
+ that it is illegal as an encoded sequence. Content-Transfer-
+ Encodings transformations never depend on any additional external
+ profile information for proper operation. Note that while decoders
+ must produce a single, well-defined output for a valid encoding no
+ such restrictions exist for encoders: Encoding a given sequence of
+ octets to different, equivalent encoded sequences is perfectly legal.
+
+ Three transformations are currently defined: identity, the "quoted-
+ printable" encoding, and the "base64" encoding. The domains are
+ "binary", "8bit" and "7bit".
+
+ The Content-Transfer-Encoding values "7bit", "8bit", and "binary" all
+ mean that the identity (i.e. NO) encoding transformation has been
+ performed. As such, they serve simply as indicators of the domain of
+ the body data, and provide useful information about the sort of
+ encoding that might be needed for transmission in a given transport
+ system. The terms "7bit data", "8bit data", and "binary data" are
+ all defined in Section 2.
+
+ The quoted-printable and base64 encodings transform their input from
+ an arbitrary domain into material in the "7bit" range, thus making it
+ safe to carry over restricted transports. The specific definition of
+ the transformations are given below.
+
+ The proper Content-Transfer-Encoding label must always be used.
+ Labelling unencoded data containing 8bit characters as "7bit" is not
+ allowed, nor is labelling unencoded non-line-oriented data as
+ anything other than "binary" allowed.
+
+ Unlike media subtypes, a proliferation of Content-Transfer-Encoding
+ values is both undesirable and unnecessary. However, establishing
+ only a single transformation into the "7bit" domain does not seem
+
+
+
+Freed & Borenstein Standards Track [Page 15]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ possible. There is a tradeoff between the desire for a compact and
+ efficient encoding of largely- binary data and the desire for a
+ somewhat readable encoding of data that is mostly, but not entirely,
+ 7bit. For this reason, at least two encoding mechanisms are
+ necessary: a more or less readable encoding (quoted-printable) and a
+ "dense" or "uniform" encoding (base64).
+
+ Mail transport for unencoded 8bit data is defined in RFC 1652. As of
+ the initial publication of this document, there are no standardized
+ Internet mail transports for which it is legitimate to include
+ unencoded binary data in mail bodies. Thus there are no
+ circumstances in which the "binary" Content-Transfer-Encoding is
+ actually valid in Internet mail. However, in the event that binary
+ mail transport becomes a reality in Internet mail, or when MIME is
+ used in conjunction with any other binary-capable mail transport
+ mechanism, binary bodies must be labelled as such using this
+ mechanism.
+
+ NOTE: The five values defined for the Content-Transfer-Encoding field
+ imply nothing about the media type other than the algorithm by which
+ it was encoded or the transport system requirements if unencoded.
+
+6.3. New Content-Transfer-Encodings
+
+ Implementors may, if necessary, define private Content-Transfer-
+ Encoding values, but must use an x-token, which is a name prefixed by
+ "X-", to indicate its non-standard status, e.g., "Content-Transfer-
+ Encoding: x-my-new-encoding". Additional standardized Content-
+ Transfer-Encoding values must be specified by a standards-track RFC.
+ The requirements such specifications must meet are given in RFC 2048.
+ As such, all content-transfer-encoding namespace except that
+ beginning with "X-" is explicitly reserved to the IETF for future
+ use.
+
+ Unlike media types and subtypes, the creation of new Content-
+ Transfer-Encoding values is STRONGLY discouraged, as it seems likely
+ to hinder interoperability with little potential benefit
+
+6.4. Interpretation and Use
+
+ If a Content-Transfer-Encoding header field appears as part of a
+ message header, it applies to the entire body of that message. If a
+ Content-Transfer-Encoding header field appears as part of an entity's
+ headers, it applies only to the body of that entity. If an entity is
+ of type "multipart" the Content-Transfer-Encoding is not permitted to
+ have any value other than "7bit", "8bit" or "binary". Even more
+ severe restrictions apply to some subtypes of the "message" type.
+
+
+
+
+Freed & Borenstein Standards Track [Page 16]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ It should be noted that most media types are defined in terms of
+ octets rather than bits, so that the mechanisms described here are
+ mechanisms for encoding arbitrary octet streams, not bit streams. If
+ a bit stream is to be encoded via one of these mechanisms, it must
+ first be converted to an 8bit byte stream using the network standard
+ bit order ("big-endian"), in which the earlier bits in a stream
+ become the higher-order bits in a 8bit byte. A bit stream not ending
+ at an 8bit boundary must be padded with zeroes. RFC 2046 provides a
+ mechanism for noting the addition of such padding in the case of the
+ application/octet-stream media type, which has a "padding" parameter.
+
+ The encoding mechanisms defined here explicitly encode all data in
+ US-ASCII. Thus, for example, suppose an entity has header fields
+ such as:
+
+ Content-Type: text/plain; charset=ISO-8859-1
+ Content-transfer-encoding: base64
+
+ This must be interpreted to mean that the body is a base64 US-ASCII
+ encoding of data that was originally in ISO-8859-1, and will be in
+ that character set again after decoding.
+
+ Certain Content-Transfer-Encoding values may only be used on certain
+ media types. In particular, it is EXPRESSLY FORBIDDEN to use any
+ encodings other than "7bit", "8bit", or "binary" with any composite
+ media type, i.e. one that recursively includes other Content-Type
+ fields. Currently the only composite media types are "multipart" and
+ "message". All encodings that are desired for bodies of type
+ multipart or message must be done at the innermost level, by encoding
+ the actual body that needs to be encoded.
+
+ It should also be noted that, by definition, if a composite entity
+ has a transfer-encoding value such as "7bit", but one of the enclosed
+ entities has a less restrictive value such as "8bit", then either the
+ outer "7bit" labelling is in error, because 8bit data are included,
+ or the inner "8bit" labelling placed an unnecessarily high demand on
+ the transport system because the actual included data were actually
+ 7bit-safe.
+
+ NOTE ON ENCODING RESTRICTIONS: Though the prohibition against using
+ content-transfer-encodings on composite body data may seem overly
+ restrictive, it is necessary to prevent nested encodings, in which
+ data are passed through an encoding algorithm multiple times, and
+ must be decoded multiple times in order to be properly viewed.
+ Nested encodings add considerable complexity to user agents: Aside
+ from the obvious efficiency problems with such multiple encodings,
+ they can obscure the basic structure of a message. In particular,
+ they can imply that several decoding operations are necessary simply
+
+
+
+Freed & Borenstein Standards Track [Page 17]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ to find out what types of bodies a message contains. Banning nested
+ encodings may complicate the job of certain mail gateways, but this
+ seems less of a problem than the effect of nested encodings on user
+ agents.
+
+ Any entity with an unrecognized Content-Transfer-Encoding must be
+ treated as if it has a Content-Type of "application/octet-stream",
+ regardless of what the Content-Type header field actually says.
+
+ NOTE ON THE RELATIONSHIP BETWEEN CONTENT-TYPE AND CONTENT-TRANSFER-
+ ENCODING: It may seem that the Content-Transfer-Encoding could be
+ inferred from the characteristics of the media that is to be encoded,
+ or, at the very least, that certain Content-Transfer-Encodings could
+ be mandated for use with specific media types. There are several
+ reasons why this is not the case. First, given the varying types of
+ transports used for mail, some encodings may be appropriate for some
+ combinations of media types and transports but not for others. (For
+ example, in an 8bit transport, no encoding would be required for text
+ in certain character sets, while such encodings are clearly required
+ for 7bit SMTP.)
+
+ Second, certain media types may require different types of transfer
+ encoding under different circumstances. For example, many PostScript
+ bodies might consist entirely of short lines of 7bit data and hence
+ require no encoding at all. Other PostScript bodies (especially
+ those using Level 2 PostScript's binary encoding mechanism) may only
+ be reasonably represented using a binary transport encoding.
+ Finally, since the Content-Type field is intended to be an open-ended
+ specification mechanism, strict specification of an association
+ between media types and encodings effectively couples the
+ specification of an application protocol with a specific lower-level
+ transport. This is not desirable since the developers of a media
+ type should not have to be aware of all the transports in use and
+ what their limitations are.
+
+6.5. Translating Encodings
+
+ The quoted-printable and base64 encodings are designed so that
+ conversion between them is possible. The only issue that arises in
+ such a conversion is the handling of hard line breaks in quoted-
+ printable encoding output. When converting from quoted-printable to
+ base64 a hard line break in the quoted-printable form represents a
+ CRLF sequence in the canonical form of the data. It must therefore be
+ converted to a corresponding encoded CRLF in the base64 form of the
+ data. Similarly, a CRLF sequence in the canonical form of the data
+ obtained after base64 decoding must be converted to a quoted-
+ printable hard line break, but ONLY when converting text data.
+
+
+
+
+Freed & Borenstein Standards Track [Page 18]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+6.6. Canonical Encoding Model
+
+ There was some confusion, in the previous versions of this RFC,
+ regarding the model for when email data was to be converted to
+ canonical form and encoded, and in particular how this process would
+ affect the treatment of CRLFs, given that the representation of
+ newlines varies greatly from system to system, and the relationship
+ between content-transfer-encodings and character sets. A canonical
+ model for encoding is presented in RFC 2049 for this reason.
+
+6.7. Quoted-Printable Content-Transfer-Encoding
+
+ The Quoted-Printable encoding is intended to represent data that
+ largely consists of octets that correspond to printable characters in
+ the US-ASCII character set. It encodes the data in such a way that
+ the resulting octets are unlikely to be modified by mail transport.
+ If the data being encoded are mostly US-ASCII text, the encoded form
+ of the data remains largely recognizable by humans. A body which is
+ entirely US-ASCII may also be encoded in Quoted-Printable to ensure
+ the integrity of the data should the message pass through a
+ character-translating, and/or line-wrapping gateway.
+
+ In this encoding, octets are to be represented as determined by the
+ following rules:
+
+ (1) (General 8bit representation) Any octet, except a CR or
+ LF that is part of a CRLF line break of the canonical
+ (standard) form of the data being encoded, may be
+ represented by an "=" followed by a two digit
+ hexadecimal representation of the octet's value. The
+ digits of the hexadecimal alphabet, for this purpose,
+ are "0123456789ABCDEF". Uppercase letters must be
+ used; lowercase letters are not allowed. Thus, for
+ example, the decimal value 12 (US-ASCII form feed) can
+ be represented by "=0C", and the decimal value 61 (US-
+ ASCII EQUAL SIGN) can be represented by "=3D". This
+ rule must be followed except when the following rules
+ allow an alternative encoding.
+
+ (2) (Literal representation) Octets with decimal values of
+ 33 through 60 inclusive, and 62 through 126, inclusive,
+ MAY be represented as the US-ASCII characters which
+ correspond to those octets (EXCLAMATION POINT through
+ LESS THAN, and GREATER THAN through TILDE,
+ respectively).
+
+ (3) (White Space) Octets with values of 9 and 32 MAY be
+ represented as US-ASCII TAB (HT) and SPACE characters,
+
+
+
+Freed & Borenstein Standards Track [Page 19]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ respectively, but MUST NOT be so represented at the end
+ of an encoded line. Any TAB (HT) or SPACE characters
+ on an encoded line MUST thus be followed on that line
+ by a printable character. In particular, an "=" at the
+ end of an encoded line, indicating a soft line break
+ (see rule #5) may follow one or more TAB (HT) or SPACE
+ characters. It follows that an octet with decimal
+ value 9 or 32 appearing at the end of an encoded line
+ must be represented according to Rule #1. This rule is
+ necessary because some MTAs (Message Transport Agents,
+ programs which transport messages from one user to
+ another, or perform a portion of such transfers) are
+ known to pad lines of text with SPACEs, and others are
+ known to remove "white space" characters from the end
+ of a line. Therefore, when decoding a Quoted-Printable
+ body, any trailing white space on a line must be
+ deleted, as it will necessarily have been added by
+ intermediate transport agents.
+
+ (4) (Line Breaks) A line break in a text body, represented
+ as a CRLF sequence in the text canonical form, must be
+ represented by a (RFC 822) line break, which is also a
+ CRLF sequence, in the Quoted-Printable encoding. Since
+ the canonical representation of media types other than
+ text do not generally include the representation of
+ line breaks as CRLF sequences, no hard line breaks
+ (i.e. line breaks that are intended to be meaningful
+ and to be displayed to the user) can occur in the
+ quoted-printable encoding of such types. Sequences
+ like "=0D", "=0A", "=0A=0D" and "=0D=0A" will routinely
+ appear in non-text data represented in quoted-
+ printable, of course.
+
+ Note that many implementations may elect to encode the
+ local representation of various content types directly
+ rather than converting to canonical form first,
+ encoding, and then converting back to local
+ representation. In particular, this may apply to plain
+ text material on systems that use newline conventions
+ other than a CRLF terminator sequence. Such an
+ implementation optimization is permissible, but only
+ when the combined canonicalization-encoding step is
+ equivalent to performing the three steps separately.
+
+ (5) (Soft Line Breaks) The Quoted-Printable encoding
+ REQUIRES that encoded lines be no more than 76
+ characters long. If longer lines are to be encoded
+ with the Quoted-Printable encoding, "soft" line breaks
+
+
+
+Freed & Borenstein Standards Track [Page 20]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ must be used. An equal sign as the last character on a
+ encoded line indicates such a non-significant ("soft")
+ line break in the encoded text.
+
+ Thus if the "raw" form of the line is a single unencoded line that
+ says:
+
+ Now's the time for all folk to come to the aid of their country.
+
+ This can be represented, in the Quoted-Printable encoding, as:
+
+ Now's the time =
+ for all folk to come=
+ to the aid of their country.
+
+ This provides a mechanism with which long lines are encoded in such a
+ way as to be restored by the user agent. The 76 character limit does
+ not count the trailing CRLF, but counts all other characters,
+ including any equal signs.
+
+ Since the hyphen character ("-") may be represented as itself in the
+ Quoted-Printable encoding, care must be taken, when encapsulating a
+ quoted-printable encoded body inside one or more multipart entities,
+ to ensure that the boundary delimiter does not appear anywhere in the
+ encoded body. (A good strategy is to choose a boundary that includes
+ a character sequence such as "=_" which can never appear in a
+ quoted-printable body. See the definition of multipart messages in
+ RFC 2046.)
+
+ NOTE: The quoted-printable encoding represents something of a
+ compromise between readability and reliability in transport. Bodies
+ encoded with the quoted-printable encoding will work reliably over
+ most mail gateways, but may not work perfectly over a few gateways,
+ notably those involving translation into EBCDIC. A higher level of
+ confidence is offered by the base64 Content-Transfer-Encoding. A way
+ to get reasonably reliable transport through EBCDIC gateways is to
+ also quote the US-ASCII characters
+
+ !"#$@[\]^`{|}~
+
+ according to rule #1.
+
+ Because quoted-printable data is generally assumed to be line-
+ oriented, it is to be expected that the representation of the breaks
+ between the lines of quoted-printable data may be altered in
+ transport, in the same manner that plain text mail has always been
+ altered in Internet mail when passing between systems with differing
+ newline conventions. If such alterations are likely to constitute a
+
+
+
+Freed & Borenstein Standards Track [Page 21]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ corruption of the data, it is probably more sensible to use the
+ base64 encoding rather than the quoted-printable encoding.
+
+ NOTE: Several kinds of substrings cannot be generated according to
+ the encoding rules for the quoted-printable content-transfer-
+ encoding, and hence are formally illegal if they appear in the output
+ of a quoted-printable encoder. This note enumerates these cases and
+ suggests ways to handle such illegal substrings if any are
+ encountered in quoted-printable data that is to be decoded.
+
+ (1) An "=" followed by two hexadecimal digits, one or both
+ of which are lowercase letters in "abcdef", is formally
+ illegal. A robust implementation might choose to
+ recognize them as the corresponding uppercase letters.
+
+ (2) An "=" followed by a character that is neither a
+ hexadecimal digit (including "abcdef") nor the CR
+ character of a CRLF pair is illegal. This case can be
+ the result of US-ASCII text having been included in a
+ quoted-printable part of a message without itself
+ having been subjected to quoted-printable encoding. A
+ reasonable approach by a robust implementation might be
+ to include the "=" character and the following
+ character in the decoded data without any
+ transformation and, if possible, indicate to the user
+ that proper decoding was not possible at this point in
+ the data.
+
+ (3) An "=" cannot be the ultimate or penultimate character
+ in an encoded object. This could be handled as in case
+ (2) above.
+
+ (4) Control characters other than TAB, or CR and LF as
+ parts of CRLF pairs, must not appear. The same is true
+ for octets with decimal values greater than 126. If
+ found in incoming quoted-printable data by a decoder, a
+ robust implementation might exclude them from the
+ decoded data and warn the user that illegal characters
+ were discovered.
+
+ (5) Encoded lines must not be longer than 76 characters,
+ not counting the trailing CRLF. If longer lines are
+ found in incoming, encoded data, a robust
+ implementation might nevertheless decode the lines, and
+ might report the erroneous encoding to the user.
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 22]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ WARNING TO IMPLEMENTORS: If binary data is encoded in quoted-
+ printable, care must be taken to encode CR and LF characters as "=0D"
+ and "=0A", respectively. In particular, a CRLF sequence in binary
+ data should be encoded as "=0D=0A". Otherwise, if CRLF were
+ represented as a hard line break, it might be incorrectly decoded on
+ platforms with different line break conventions.
+
+ For formalists, the syntax of quoted-printable data is described by
+ the following grammar:
+
+ quoted-printable := qp-line *(CRLF qp-line)
+
+ qp-line := *(qp-segment transport-padding CRLF)
+ qp-part transport-padding
+
+ qp-part := qp-section
+ ; Maximum length of 76 characters
+
+ qp-segment := qp-section *(SPACE / TAB) "="
+ ; Maximum length of 76 characters
+
+ qp-section := [*(ptext / SPACE / TAB) ptext]
+
+ ptext := hex-octet / safe-char
+
+ safe-char := <any octet with decimal value of 33 through
+ 60 inclusive, and 62 through 126>
+ ; Characters not listed as "mail-safe" in
+ ; RFC 2049 are also not recommended.
+
+ hex-octet := "=" 2(DIGIT / "A" / "B" / "C" / "D" / "E" / "F")
+ ; Octet must be used for characters > 127, =,
+ ; SPACEs or TABs at the ends of lines, and is
+ ; recommended for any character not listed in
+ ; RFC 2049 as "mail-safe".
+
+ transport-padding := *LWSP-char
+ ; Composers MUST NOT generate
+ ; non-zero length transport
+ ; padding, but receivers MUST
+ ; be able to handle padding
+ ; added by message transports.
+
+ IMPORTANT: The addition of LWSP between the elements shown in this
+ BNF is NOT allowed since this BNF does not specify a structured
+ header field.
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 23]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+6.8. Base64 Content-Transfer-Encoding
+
+ The Base64 Content-Transfer-Encoding is designed to represent
+ arbitrary sequences of octets in a form that need not be humanly
+ readable. The encoding and decoding algorithms are simple, but the
+ encoded data are consistently only about 33 percent larger than the
+ unencoded data. This encoding is virtually identical to the one used
+ in Privacy Enhanced Mail (PEM) applications, as defined in RFC 1421.
+
+ A 65-character subset of US-ASCII is used, enabling 6 bits to be
+ represented per printable character. (The extra 65th character, "=",
+ is used to signify a special processing function.)
+
+ NOTE: This subset has the important property that it is represented
+ identically in all versions of ISO 646, including US-ASCII, and all
+ characters in the subset are also represented identically in all
+ versions of EBCDIC. Other popular encodings, such as the encoding
+ used by the uuencode utility, Macintosh binhex 4.0 [RFC-1741], and
+ the base85 encoding specified as part of Level 2 PostScript, do not
+ share these properties, and thus do not fulfill the portability
+ requirements a binary transport encoding for mail must meet.
+
+ The encoding process represents 24-bit groups of input bits as output
+ strings of 4 encoded characters. Proceeding from left to right, a
+ 24-bit input group is formed by concatenating 3 8bit input groups.
+ These 24 bits are then treated as 4 concatenated 6-bit groups, each
+ of which is translated into a single digit in the base64 alphabet.
+ When encoding a bit stream via the base64 encoding, the bit stream
+ must be presumed to be ordered with the most-significant-bit first.
+ That is, the first bit in the stream will be the high-order bit in
+ the first 8bit byte, and the eighth bit will be the low-order bit in
+ the first 8bit byte, and so on.
+
+ Each 6-bit group is used as an index into an array of 64 printable
+ characters. The character referenced by the index is placed in the
+ output string. These characters, identified in Table 1, below, are
+ selected so as to be universally representable, and the set excludes
+ characters with particular significance to SMTP (e.g., ".", CR, LF)
+ and to the multipart boundary delimiters defined in RFC 2046 (e.g.,
+ "-").
+
+
+
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 24]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ Table 1: The Base64 Alphabet
+
+ Value Encoding Value Encoding Value Encoding Value Encoding
+ 0 A 17 R 34 i 51 z
+ 1 B 18 S 35 j 52 0
+ 2 C 19 T 36 k 53 1
+ 3 D 20 U 37 l 54 2
+ 4 E 21 V 38 m 55 3
+ 5 F 22 W 39 n 56 4
+ 6 G 23 X 40 o 57 5
+ 7 H 24 Y 41 p 58 6
+ 8 I 25 Z 42 q 59 7
+ 9 J 26 a 43 r 60 8
+ 10 K 27 b 44 s 61 9
+ 11 L 28 c 45 t 62 +
+ 12 M 29 d 46 u 63 /
+ 13 N 30 e 47 v
+ 14 O 31 f 48 w (pad) =
+ 15 P 32 g 49 x
+ 16 Q 33 h 50 y
+
+ The encoded output stream must be represented in lines of no more
+ than 76 characters each. All line breaks or other characters not
+ found in Table 1 must be ignored by decoding software. In base64
+ data, characters other than those in Table 1, line breaks, and other
+ white space probably indicate a transmission error, about which a
+ warning message or even a message rejection might be appropriate
+ under some circumstances.
+
+ Special processing is performed if fewer than 24 bits are available
+ at the end of the data being encoded. A full encoding quantum is
+ always completed at the end of a body. When fewer than 24 input bits
+ are available in an input group, zero bits are added (on the right)
+ to form an integral number of 6-bit groups. Padding at the end of
+ the data is performed using the "=" character. Since all base64
+ input is an integral number of octets, only the following cases can
+ arise: (1) the final quantum of encoding input is an integral
+ multiple of 24 bits; here, the final unit of encoded output will be
+ an integral multiple of 4 characters with no "=" padding, (2) the
+ final quantum of encoding input is exactly 8 bits; here, the final
+ unit of encoded output will be two characters followed by two "="
+ padding characters, or (3) the final quantum of encoding input is
+ exactly 16 bits; here, the final unit of encoded output will be three
+ characters followed by one "=" padding character.
+
+ Because it is used only for padding at the end of the data, the
+ occurrence of any "=" characters may be taken as evidence that the
+ end of the data has been reached (without truncation in transit). No
+
+
+
+Freed & Borenstein Standards Track [Page 25]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ such assurance is possible, however, when the number of octets
+ transmitted was a multiple of three and no "=" characters are
+ present.
+
+ Any characters outside of the base64 alphabet are to be ignored in
+ base64-encoded data.
+
+ Care must be taken to use the proper octets for line breaks if base64
+ encoding is applied directly to text material that has not been
+ converted to canonical form. In particular, text line breaks must be
+ converted into CRLF sequences prior to base64 encoding. The
+ important thing to note is that this may be done directly by the
+ encoder rather than in a prior canonicalization step in some
+ implementations.
+
+ NOTE: There is no need to worry about quoting potential boundary
+ delimiters within base64-encoded bodies within multipart entities
+ because no hyphen characters are used in the base64 encoding.
+
+7. Content-ID Header Field
+
+ In constructing a high-level user agent, it may be desirable to allow
+ one body to make reference to another. Accordingly, bodies may be
+ labelled using the "Content-ID" header field, which is syntactically
+ identical to the "Message-ID" header field:
+
+ id := "Content-ID" ":" msg-id
+
+ Like the Message-ID values, Content-ID values must be generated to be
+ world-unique.
+
+ The Content-ID value may be used for uniquely identifying MIME
+ entities in several contexts, particularly for caching data
+ referenced by the message/external-body mechanism. Although the
+ Content-ID header is generally optional, its use is MANDATORY in
+ implementations which generate data of the optional MIME media type
+ "message/external-body". That is, each message/external-body entity
+ must have a Content-ID field to permit caching of such data.
+
+ It is also worth noting that the Content-ID value has special
+ semantics in the case of the multipart/alternative media type. This
+ is explained in the section of RFC 2046 dealing with
+ multipart/alternative.
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 26]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+8. Content-Description Header Field
+
+ The ability to associate some descriptive information with a given
+ body is often desirable. For example, it may be useful to mark an
+ "image" body as "a picture of the Space Shuttle Endeavor." Such text
+ may be placed in the Content-Description header field. This header
+ field is always optional.
+
+ description := "Content-Description" ":" *text
+
+ The description is presumed to be given in the US-ASCII character
+ set, although the mechanism specified in RFC 2047 may be used for
+ non-US-ASCII Content-Description values.
+
+9. Additional MIME Header Fields
+
+ Future documents may elect to define additional MIME header fields
+ for various purposes. Any new header field that further describes
+ the content of a message should begin with the string "Content-" to
+ allow such fields which appear in a message header to be
+ distinguished from ordinary RFC 822 message header fields.
+
+ MIME-extension-field := <Any RFC 822 header field which
+ begins with the string
+ "Content-">
+
+10. Summary
+
+ Using the MIME-Version, Content-Type, and Content-Transfer-Encoding
+ header fields, it is possible to include, in a standardized way,
+ arbitrary types of data with RFC 822 conformant mail messages. No
+ restrictions imposed by either RFC 821 or RFC 822 are violated, and
+ care has been taken to avoid problems caused by additional
+ restrictions imposed by the characteristics of some Internet mail
+ transport mechanisms (see RFC 2049).
+
+ The next document in this set, RFC 2046, specifies the initial set of
+ media types that can be labelled and transported using these headers.
+
+11. Security Considerations
+
+ Security issues are discussed in the second document in this set, RFC
+ 2046.
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 27]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+12. Authors' Addresses
+
+ For more information, the authors of this document are best contacted
+ via Internet mail:
+
+ Ned Freed
+ Innosoft International, Inc.
+ 1050 East Garvey Avenue South
+ West Covina, CA 91790
+ USA
+
+ Phone: +1 818 919 3600
+ Fax: +1 818 919 3614
+ EMail: ned@innosoft.com
+
+
+ Nathaniel S. Borenstein
+ First Virtual Holdings
+ 25 Washington Avenue
+ Morristown, NJ 07960
+ USA
+
+ Phone: +1 201 540 8967
+ Fax: +1 201 993 3032
+ EMail: nsb@nsb.fv.com
+
+
+ MIME is a result of the work of the Internet Engineering Task Force
+ Working Group on RFC 822 Extensions. The chairman of that group,
+ Greg Vaudreuil, may be reached at:
+
+ Gregory M. Vaudreuil
+ Octel Network Services
+ 17080 Dallas Parkway
+ Dallas, TX 75248-1905
+ USA
+
+ EMail: Greg.Vaudreuil@Octel.Com
+
+
+
+
+
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 28]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+Appendix A -- Collected Grammar
+
+ This appendix contains the complete BNF grammar for all the syntax
+ specified by this document.
+
+ By itself, however, this grammar is incomplete. It refers by name to
+ several syntax rules that are defined by RFC 822. Rather than
+ reproduce those definitions here, and risk unintentional differences
+ between the two, this document simply refers the reader to RFC 822
+ for the remaining definitions. Wherever a term is undefined, it
+ refers to the RFC 822 definition.
+
+ attribute := token
+ ; Matching of attributes
+ ; is ALWAYS case-insensitive.
+
+ composite-type := "message" / "multipart" / extension-token
+
+ content := "Content-Type" ":" type "/" subtype
+ *(";" parameter)
+ ; Matching of media type and subtype
+ ; is ALWAYS case-insensitive.
+
+ description := "Content-Description" ":" *text
+
+ discrete-type := "text" / "image" / "audio" / "video" /
+ "application" / extension-token
+
+ encoding := "Content-Transfer-Encoding" ":" mechanism
+
+ entity-headers := [ content CRLF ]
+ [ encoding CRLF ]
+ [ id CRLF ]
+ [ description CRLF ]
+ *( MIME-extension-field CRLF )
+
+ extension-token := ietf-token / x-token
+
+ hex-octet := "=" 2(DIGIT / "A" / "B" / "C" / "D" / "E" / "F")
+ ; Octet must be used for characters > 127, =,
+ ; SPACEs or TABs at the ends of lines, and is
+ ; recommended for any character not listed in
+ ; RFC 2049 as "mail-safe".
+
+ iana-token := <A publicly-defined extension token. Tokens
+ of this form must be registered with IANA
+ as specified in RFC 2048.>
+
+
+
+
+Freed & Borenstein Standards Track [Page 29]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ ietf-token := <An extension token defined by a
+ standards-track RFC and registered
+ with IANA.>
+
+ id := "Content-ID" ":" msg-id
+
+ mechanism := "7bit" / "8bit" / "binary" /
+ "quoted-printable" / "base64" /
+ ietf-token / x-token
+
+ MIME-extension-field := <Any RFC 822 header field which
+ begins with the string
+ "Content-">
+
+ MIME-message-headers := entity-headers
+ fields
+ version CRLF
+ ; The ordering of the header
+ ; fields implied by this BNF
+ ; definition should be ignored.
+
+ MIME-part-headers := entity-headers
+ [fields]
+ ; Any field not beginning with
+ ; "content-" can have no defined
+ ; meaning and may be ignored.
+ ; The ordering of the header
+ ; fields implied by this BNF
+ ; definition should be ignored.
+
+ parameter := attribute "=" value
+
+ ptext := hex-octet / safe-char
+
+ qp-line := *(qp-segment transport-padding CRLF)
+ qp-part transport-padding
+
+ qp-part := qp-section
+ ; Maximum length of 76 characters
+
+ qp-section := [*(ptext / SPACE / TAB) ptext]
+
+ qp-segment := qp-section *(SPACE / TAB) "="
+ ; Maximum length of 76 characters
+
+ quoted-printable := qp-line *(CRLF qp-line)
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 30]
+
+RFC 2045 Internet Message Bodies November 1996
+
+
+ safe-char := <any octet with decimal value of 33 through
+ 60 inclusive, and 62 through 126>
+ ; Characters not listed as "mail-safe" in
+ ; RFC 2049 are also not recommended.
+
+ subtype := extension-token / iana-token
+
+ token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
+ or tspecials>
+
+ transport-padding := *LWSP-char
+ ; Composers MUST NOT generate
+ ; non-zero length transport
+ ; padding, but receivers MUST
+ ; be able to handle padding
+ ; added by message transports.
+
+ tspecials := "(" / ")" / "<" / ">" / "@" /
+ "," / ";" / ":" / "\" / <">
+ "/" / "[" / "]" / "?" / "="
+ ; Must be in quoted-string,
+ ; to use within parameter values
+
+ type := discrete-type / composite-type
+
+ value := token / quoted-string
+
+ version := "MIME-Version" ":" 1*DIGIT "." 1*DIGIT
+
+ x-token := <The two characters "X-" or "x-" followed, with
+ no intervening white space, by any token>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Freed & Borenstein Standards Track [Page 31]
+
diff --git a/vendor/github.com/mattermost/rsc/imap/rfc2971.txt b/vendor/github.com/mattermost/rsc/imap/rfc2971.txt
new file mode 100644
index 000000000..9e7264dcc
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/rfc2971.txt
@@ -0,0 +1,451 @@
+
+
+
+
+
+
+Network Working Group T. Showalter
+Request for Comments: 2971 Mirapoint, Inc.
+Category: Standards Track October 2000
+
+
+ IMAP4 ID extension
+
+Status of this Memo
+
+ This document specifies an Internet standards track protocol for the
+ Internet community, and requests discussion and suggestions for
+ improvements. Please refer to the current edition of the "Internet
+ Official Protocol Standards" (STD 1) for the standardization state
+ and status of this protocol. Distribution of this memo is unlimited.
+
+Copyright Notice
+
+ Copyright (C) The Internet Society (2000). All Rights Reserved.
+
+Abstract
+
+ The ID extension to the Internet Message Access Protocol - Version
+ 4rev1 (IMAP4rev1) protocol allows the server and client to exchange
+ identification information on their implementation in order to make
+ bug reports and usage statistics more complete.
+
+1. Introduction
+
+ The IMAP4rev1 protocol described in [IMAP4rev1] provides a method for
+ accessing remote mail stores, but it provides no facility to
+ advertise what program a client or server uses to provide service.
+ This makes it difficult for implementors to get complete bug reports
+ from users, as it is frequently difficult to know what client or
+ server is in use.
+
+ Additionally, some sites may wish to assemble usage statistics based
+ on what clients are used, but in an an environment where users are
+ permitted to obtain and maintain their own clients this is difficult
+ to accomplish.
+
+ The ID command provides a facility to advertise information on what
+ programs are being used along with contact information (should bugs
+ ever occur).
+
+
+
+
+
+
+
+
+Showalter Standards Track [Page 1]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+2. Conventions Used in this Document
+
+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
+ document are to be interpreted as described in [KEYWORDS].
+
+ The conventions used in this document are the same as specified in
+ [IMAP4rev1]. In examples, "C:" and "S:" indicate lines sent by the
+ client and server respectively. Line breaks have been inserted for
+ readability.
+
+3. Specification
+
+ The sole purpose of the ID extension is to enable clients and servers
+ to exchange information on their implementations for the purposes of
+ statistical analysis and problem determination.
+
+ This information is be submitted to a server by any client wishing to
+ provide information for statistical purposes, provided the server
+ advertises its willingness to take the information with the atom "ID"
+ included in the list of capabilities returned by the CAPABILITY
+ command.
+
+ Implementations MUST NOT make operational changes based on the data
+ sent as part of the ID command or response. The ID command is for
+ human consumption only, and is not to be used in improving the
+ performance of clients or servers.
+
+ This includes, but is not limited to, the following:
+
+ Servers MUST NOT attempt to work around client bugs by using
+ information from the ID command. Clients MUST NOT attempt to work
+ around server bugs based on the ID response.
+
+ Servers MUST NOT provide features to a client or otherwise
+ optimize for a particular client by using information from the ID
+ command. Clients MUST NOT provide features to a server or
+ otherwise optimize for a particular server based on the ID
+ response.
+
+ Servers MUST NOT deny access to or refuse service for a client
+ based on information from the ID command. Clients MUST NOT refuse
+ to operate or limit their operation with a server based on the ID
+ response.
+
+
+
+
+
+
+
+Showalter Standards Track [Page 2]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+ Rationale: It is imperative that this extension not supplant IMAP's
+ CAPABILITY mechanism with a ad-hoc approach where implementations
+ guess each other's features based on who they claim to be.
+
+ Implementations MUST NOT send false information in an ID command.
+
+ Implementations MAY send less information than they have available or
+ no information at all. Such behavior may be useful to preserve user
+ privacy. See Security Considerations, section 7.
+
+3.1. ID Command
+
+ Arguments: client parameter list or NIL
+
+ Responses: OPTIONAL untagged response: ID
+
+ Result: OK identification information accepted
+ BAD command unknown or arguments invalid
+
+ Implementation identification information is sent by the client with
+ the ID command.
+
+ This command is valid in any state.
+
+ The information sent is in the form of a list of field/value pairs.
+ Fields are permitted to be any IMAP4 string, and values are permitted
+ to be any IMAP4 string or NIL. A value of NIL indicates that the
+ client can not or will not specify this information. The client may
+ also send NIL instead of the list, indicating that it wants to send
+ no information, but would still accept a server response.
+
+ The available fields are defined in section 3.3.
+
+ Example: C: a023 ID ("name" "sodr" "version" "19.34" "vendor"
+ "Pink Floyd Music Limited")
+ S: * ID NIL
+ S: a023 OK ID completed
+
+3.2. ID Response
+
+ Contents: server parameter list
+
+ In response to an ID command issued by the client, the server replies
+ with a tagged response containing information on its implementation.
+ The format is the same as the client list.
+
+
+
+
+
+
+Showalter Standards Track [Page 3]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+ Example: C: a042 ID NIL
+ S: * ID ("name" "Cyrus" "version" "1.5" "os" "sunos"
+ "os-version" "5.5" "support-url"
+ "mailto:cyrus-bugs+@andrew.cmu.edu")
+ S: a042 OK ID command completed
+
+ A server MUST send a tagged ID response to an ID command. However, a
+ server MAY send NIL in place of the list.
+
+3.3. Defined Field Values
+
+ Any string may be sent as a field, but the following are defined to
+ describe certain values that might be sent. Implementations are free
+ to send none, any, or all of these. Strings are not case-sensitive.
+ Field strings MUST NOT be longer than 30 octets. Value strings MUST
+ NOT be longer than 1024 octets. Implementations MUST NOT send more
+ than 30 field-value pairs.
+
+ name Name of the program
+ version Version number of the program
+ os Name of the operating system
+ os-version Version of the operating system
+ vendor Vendor of the client/server
+ support-url URL to contact for support
+ address Postal address of contact/vendor
+ date Date program was released, specified as a date-time
+ in IMAP4rev1
+ command Command used to start the program
+ arguments Arguments supplied on the command line, if any
+ if any
+ environment Description of environment, i.e., UNIX environment
+ variables or Windows registry settings
+
+ Implementations MUST NOT use contact information to submit automatic
+ bug reports. Implementations may include information from an ID
+ response in a report automatically prepared, but are prohibited from
+ sending the report without user authorization.
+
+ It is preferable to find the name and version of the underlying
+ operating system at runtime in cases where this is possible.
+
+ Information sent via an ID response may violate user privacy. See
+ Security Considerations, section 7.
+
+ Implementations MUST NOT send the same field name more than once.
+
+
+
+
+
+
+Showalter Standards Track [Page 4]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+4. Formal Syntax
+
+ This syntax is intended to augment the grammar specified in
+ [IMAP4rev1] in order to provide for the ID command. This
+ specification uses the augmented Backus-Naur Form (BNF) notation as
+ used in [IMAP4rev1].
+
+ command_any ::= "CAPABILITY" / "LOGOUT" / "NOOP" / x_command / id
+ ;; adds id command to command_any in [IMAP4rev1]
+
+ id ::= "ID" SPACE id_params_list
+
+ id_response ::= "ID" SPACE id_params_list
+
+ id_params_list ::= "(" #(string SPACE nstring) ")" / nil
+ ;; list of field value pairs
+
+ response_data ::= "*" SPACE (resp_cond_state / resp_cond_bye /
+ mailbox_data / message_data / capability_data / id_response)
+
+5. Use of the ID extension with Firewalls and Other Intermediaries
+
+ There exist proxies, firewalls, and other intermediary systems that
+ can intercept an IMAP session and make changes to the data exchanged
+ in the session. Such intermediaries are not anticipated by the IMAP4
+ protocol design and are not within the scope of the IMAP4 standard.
+ However, in order for the ID command to be useful in the presence of
+ such intermediaries, those intermediaries need to take special note
+ of the ID command and response. In particular, if an intermediary
+ changes any part of the IMAP session it must also change the ID
+ command to advertise its presence.
+
+ A firewall MAY act to block transmission of specific information
+ fields in the ID command and response that it believes reveal
+ information that could expose a security vulnerability. However, a
+ firewall SHOULD NOT disable the extension, when present, entirely,
+ and SHOULD NOT unconditionally remove either the client or server
+ list.
+
+ Finally, it should be noted that a firewall, when handling a
+ CAPABILITY response, MUST NOT allow the names of extensions to be
+ returned to the client that the firewall has no knowledge of.
+
+
+
+
+
+
+
+
+
+Showalter Standards Track [Page 5]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+6. References
+
+ [KEYWORDS] Bradner, S., "Key words for use in RFCs to Indicate
+ Requirement Levels", RFC 2119, March 1997.
+
+ [IMAP4rev1] Crispin, M., "Internet Message Access Protocol - Version
+ 4rev1", RFC 2060, October 1996.
+
+ [RFC-822] Crocker, D., "Standard for the Format of ARPA Internet
+ Text Messages", STD 11, RFC 822, August 1982.
+
+7. Security Considerations
+
+ This extension has the danger of violating the privacy of users if
+ misused. Clients and servers should notify users that they implement
+ and enable the ID command.
+
+ It is highly desirable that implementations provide a method of
+ disabling ID support, perhaps by not sending ID at all, or by sending
+ NIL as the argument to the ID command or response.
+
+ Implementors must exercise extreme care in adding fields sent as part
+ of an ID command or response. Some fields, including a processor ID
+ number, Ethernet address, or other unique (or mostly unique)
+ identifier allow tracking of users in ways that violate user privacy
+ expectations.
+
+ Having implementation information of a given client or server may
+ make it easier for an attacker to gain unauthorized access due to
+ security holes.
+
+ Since this command includes arbitrary data and does not require the
+ user to authenticate, server implementations are cautioned to guard
+ against an attacker sending arbitrary garbage data in order to fill
+ up the ID log. In particular, if a server naively logs each ID
+ command to disk without inspecting it, an attacker can simply fire up
+ thousands of connections and send a few kilobytes of random data.
+ Servers have to guard against this. Methods include truncating
+ abnormally large responses; collating responses by storing only a
+ single copy, then keeping a counter of the number of times that
+ response has been seen; keeping only particularly interesting parts
+ of responses; and only logging responses of users who actually log
+ in.
+
+ Security is affected by firewalls which modify the IMAP protocol
+ stream; see section 5, Use of the ID Extension with Firewalls and
+ Other Intermediaries, for more information.
+
+
+
+
+Showalter Standards Track [Page 6]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+8. Author's Address
+
+ Tim Showalter
+ Mirapoint, Inc.
+ 909 Hermosa Ct.
+ Sunnyvale, CA 94095
+
+ EMail: tjs@mirapoint.com
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Showalter Standards Track [Page 7]
+
+RFC 2971 IMAP4 ID extension October 2000
+
+
+9. Full Copyright Statement
+
+ Copyright (C) The Internet Society (2000). All Rights Reserved.
+
+ This document and translations of it may be copied and furnished to
+ others, and derivative works that comment on or otherwise explain it
+ or assist in its implementation may be prepared, copied, published
+ and distributed, in whole or in part, without restriction of any
+ kind, provided that the above copyright notice and this paragraph are
+ included on all such copies and derivative works. However, this
+ document itself may not be modified in any way, such as by removing
+ the copyright notice or references to the Internet Society or other
+ Internet organizations, except as needed for the purpose of
+ developing Internet standards in which case the procedures for
+ copyrights defined in the Internet Standards process must be
+ followed, or as required to translate it into languages other than
+ English.
+
+ The limited permissions granted above are perpetual and will not be
+ revoked by the Internet Society or its successors or assigns.
+
+ This document and the information contained herein is provided on an
+ "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
+ TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
+ BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
+ HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+
+Acknowledgement
+
+ Funding for the RFC Editor function is currently provided by the
+ Internet Society.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Showalter Standards Track [Page 8]
+
diff --git a/vendor/github.com/mattermost/rsc/imap/rfc3501.txt b/vendor/github.com/mattermost/rsc/imap/rfc3501.txt
new file mode 100644
index 000000000..5ab48e17c
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/rfc3501.txt
@@ -0,0 +1,6051 @@
+
+
+
+
+
+
+Network Working Group M. Crispin
+Request for Comments: 3501 University of Washington
+Obsoletes: 2060 March 2003
+Category: Standards Track
+
+
+ INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1
+
+Status of this Memo
+
+ This document specifies an Internet standards track protocol for the
+ Internet community, and requests discussion and suggestions for
+ improvements. Please refer to the current edition of the "Internet
+ Official Protocol Standards" (STD 1) for the standardization state
+ and status of this protocol. Distribution of this memo is unlimited.
+
+Copyright Notice
+
+ Copyright (C) The Internet Society (2003). All Rights Reserved.
+
+Abstract
+
+ The Internet Message Access Protocol, Version 4rev1 (IMAP4rev1)
+ allows a client to access and manipulate electronic mail messages on
+ a server. IMAP4rev1 permits manipulation of mailboxes (remote
+ message folders) in a way that is functionally equivalent to local
+ folders. IMAP4rev1 also provides the capability for an offline
+ client to resynchronize with the server.
+
+ IMAP4rev1 includes operations for creating, deleting, and renaming
+ mailboxes, checking for new messages, permanently removing messages,
+ setting and clearing flags, RFC 2822 and RFC 2045 parsing, searching,
+ and selective fetching of message attributes, texts, and portions
+ thereof. Messages in IMAP4rev1 are accessed by the use of numbers.
+ These numbers are either message sequence numbers or unique
+ identifiers.
+
+ IMAP4rev1 supports a single server. A mechanism for accessing
+ configuration information to support multiple IMAP4rev1 servers is
+ discussed in RFC 2244.
+
+ IMAP4rev1 does not specify a means of posting mail; this function is
+ handled by a mail transfer protocol such as RFC 2821.
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 1]
+
+RFC 3501 IMAPv4 March 2003
+
+
+Table of Contents
+
+ IMAP4rev1 Protocol Specification ................................ 4
+ 1. How to Read This Document ............................... 4
+ 1.1. Organization of This Document ........................... 4
+ 1.2. Conventions Used in This Document ....................... 4
+ 1.3. Special Notes to Implementors ........................... 5
+ 2. Protocol Overview ....................................... 6
+ 2.1. Link Level .............................................. 6
+ 2.2. Commands and Responses .................................. 6
+ 2.2.1. Client Protocol Sender and Server Protocol Receiver ..... 6
+ 2.2.2. Server Protocol Sender and Client Protocol Receiver ..... 7
+ 2.3. Message Attributes ...................................... 8
+ 2.3.1. Message Numbers ......................................... 8
+ 2.3.1.1. Unique Identifier (UID) Message Attribute ....... 8
+ 2.3.1.2. Message Sequence Number Message Attribute ....... 10
+ 2.3.2. Flags Message Attribute ................................. 11
+ 2.3.3. Internal Date Message Attribute ......................... 12
+ 2.3.4. [RFC-2822] Size Message Attribute ....................... 12
+ 2.3.5. Envelope Structure Message Attribute .................... 12
+ 2.3.6. Body Structure Message Attribute ........................ 12
+ 2.4. Message Texts ........................................... 13
+ 3. State and Flow Diagram .................................. 13
+ 3.1. Not Authenticated State ................................. 13
+ 3.2. Authenticated State ..................................... 13
+ 3.3. Selected State .......................................... 13
+ 3.4. Logout State ............................................ 14
+ 4. Data Formats ............................................ 16
+ 4.1. Atom .................................................... 16
+ 4.2. Number .................................................. 16
+ 4.3. String .................................................. 16
+ 4.3.1. 8-bit and Binary Strings ................................ 17
+ 4.4. Parenthesized List ...................................... 17
+ 4.5. NIL ..................................................... 17
+ 5. Operational Considerations .............................. 18
+ 5.1. Mailbox Naming .......................................... 18
+ 5.1.1. Mailbox Hierarchy Naming ................................ 19
+ 5.1.2. Mailbox Namespace Naming Convention ..................... 19
+ 5.1.3. Mailbox International Naming Convention ................. 19
+ 5.2. Mailbox Size and Message Status Updates ................. 21
+ 5.3. Response when no Command in Progress .................... 21
+ 5.4. Autologout Timer ........................................ 22
+ 5.5. Multiple Commands in Progress ........................... 22
+ 6. Client Commands ........................................ 23
+ 6.1. Client Commands - Any State ............................ 24
+ 6.1.1. CAPABILITY Command ..................................... 24
+ 6.1.2. NOOP Command ........................................... 25
+ 6.1.3. LOGOUT Command ......................................... 26
+
+
+
+Crispin Standards Track [Page 2]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 6.2. Client Commands - Not Authenticated State .............. 26
+ 6.2.1. STARTTLS Command ....................................... 27
+ 6.2.2. AUTHENTICATE Command ................................... 28
+ 6.2.3. LOGIN Command .......................................... 30
+ 6.3. Client Commands - Authenticated State .................. 31
+ 6.3.1. SELECT Command ......................................... 32
+ 6.3.2. EXAMINE Command ........................................ 34
+ 6.3.3. CREATE Command ......................................... 34
+ 6.3.4. DELETE Command ......................................... 35
+ 6.3.5. RENAME Command ......................................... 37
+ 6.3.6. SUBSCRIBE Command ...................................... 39
+ 6.3.7. UNSUBSCRIBE Command .................................... 39
+ 6.3.8. LIST Command ........................................... 40
+ 6.3.9. LSUB Command ........................................... 43
+ 6.3.10. STATUS Command ......................................... 44
+ 6.3.11. APPEND Command ......................................... 46
+ 6.4. Client Commands - Selected State ....................... 47
+ 6.4.1. CHECK Command .......................................... 47
+ 6.4.2. CLOSE Command .......................................... 48
+ 6.4.3. EXPUNGE Command ........................................ 49
+ 6.4.4. SEARCH Command ......................................... 49
+ 6.4.5. FETCH Command .......................................... 54
+ 6.4.6. STORE Command .......................................... 58
+ 6.4.7. COPY Command ........................................... 59
+ 6.4.8. UID Command ............................................ 60
+ 6.5. Client Commands - Experimental/Expansion ............... 62
+ 6.5.1. X<atom> Command ........................................ 62
+ 7. Server Responses ....................................... 62
+ 7.1. Server Responses - Status Responses .................... 63
+ 7.1.1. OK Response ............................................ 65
+ 7.1.2. NO Response ............................................ 66
+ 7.1.3. BAD Response ........................................... 66
+ 7.1.4. PREAUTH Response ....................................... 67
+ 7.1.5. BYE Response ........................................... 67
+ 7.2. Server Responses - Server and Mailbox Status ........... 68
+ 7.2.1. CAPABILITY Response .................................... 68
+ 7.2.2. LIST Response .......................................... 69
+ 7.2.3. LSUB Response .......................................... 70
+ 7.2.4 STATUS Response ........................................ 70
+ 7.2.5. SEARCH Response ........................................ 71
+ 7.2.6. FLAGS Response ......................................... 71
+ 7.3. Server Responses - Mailbox Size ........................ 71
+ 7.3.1. EXISTS Response ........................................ 71
+ 7.3.2. RECENT Response ........................................ 72
+ 7.4. Server Responses - Message Status ...................... 72
+ 7.4.1. EXPUNGE Response ....................................... 72
+ 7.4.2. FETCH Response ......................................... 73
+ 7.5. Server Responses - Command Continuation Request ........ 79
+
+
+
+Crispin Standards Track [Page 3]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 8. Sample IMAP4rev1 connection ............................ 80
+ 9. Formal Syntax .......................................... 81
+ 10. Author's Note .......................................... 92
+ 11. Security Considerations ................................ 92
+ 11.1. STARTTLS Security Considerations ....................... 92
+ 11.2. Other Security Considerations .......................... 93
+ 12. IANA Considerations .................................... 94
+ Appendices ..................................................... 95
+ A. References ............................................. 95
+ B. Changes from RFC 2060 .................................. 97
+ C. Key Word Index ......................................... 103
+ Author's Address ............................................... 107
+ Full Copyright Statement ....................................... 108
+
+IMAP4rev1 Protocol Specification
+
+1. How to Read This Document
+
+1.1. Organization of This Document
+
+ This document is written from the point of view of the implementor of
+ an IMAP4rev1 client or server. Beyond the protocol overview in
+ section 2, it is not optimized for someone trying to understand the
+ operation of the protocol. The material in sections 3 through 5
+ provides the general context and definitions with which IMAP4rev1
+ operates.
+
+ Sections 6, 7, and 9 describe the IMAP commands, responses, and
+ syntax, respectively. The relationships among these are such that it
+ is almost impossible to understand any of them separately. In
+ particular, do not attempt to deduce command syntax from the command
+ section alone; instead refer to the Formal Syntax section.
+
+1.2. Conventions Used in This Document
+
+ "Conventions" are basic principles or procedures. Document
+ conventions are noted in this section.
+
+ In examples, "C:" and "S:" indicate lines sent by the client and
+ server respectively.
+
+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
+ "SHOULD", "SHOULD NOT", "MAY", and "OPTIONAL" in this document are to
+ be interpreted as described in [KEYWORDS].
+
+ The word "can" (not "may") is used to refer to a possible
+ circumstance or situation, as opposed to an optional facility of the
+ protocol.
+
+
+
+Crispin Standards Track [Page 4]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ "User" is used to refer to a human user, whereas "client" refers to
+ the software being run by the user.
+
+ "Connection" refers to the entire sequence of client/server
+ interaction from the initial establishment of the network connection
+ until its termination.
+
+ "Session" refers to the sequence of client/server interaction from
+ the time that a mailbox is selected (SELECT or EXAMINE command) until
+ the time that selection ends (SELECT or EXAMINE of another mailbox,
+ CLOSE command, or connection termination).
+
+ Characters are 7-bit US-ASCII unless otherwise specified. Other
+ character sets are indicated using a "CHARSET", as described in
+ [MIME-IMT] and defined in [CHARSET]. CHARSETs have important
+ additional semantics in addition to defining character set; refer to
+ these documents for more detail.
+
+ There are several protocol conventions in IMAP. These refer to
+ aspects of the specification which are not strictly part of the IMAP
+ protocol, but reflect generally-accepted practice. Implementations
+ need to be aware of these conventions, and avoid conflicts whether or
+ not they implement the convention. For example, "&" may not be used
+ as a hierarchy delimiter since it conflicts with the Mailbox
+ International Naming Convention, and other uses of "&" in mailbox
+ names are impacted as well.
+
+1.3. Special Notes to Implementors
+
+ Implementors of the IMAP protocol are strongly encouraged to read the
+ IMAP implementation recommendations document [IMAP-IMPLEMENTATION] in
+ conjunction with this document, to help understand the intricacies of
+ this protocol and how best to build an interoperable product.
+
+ IMAP4rev1 is designed to be upwards compatible from the [IMAP2] and
+ unpublished IMAP2bis protocols. IMAP4rev1 is largely compatible with
+ the IMAP4 protocol described in RFC 1730; the exception being in
+ certain facilities added in RFC 1730 that proved problematic and were
+ subsequently removed. In the course of the evolution of IMAP4rev1,
+ some aspects in the earlier protocols have become obsolete. Obsolete
+ commands, responses, and data formats which an IMAP4rev1
+ implementation can encounter when used with an earlier implementation
+ are described in [IMAP-OBSOLETE].
+
+ Other compatibility issues with IMAP2bis, the most common variant of
+ the earlier protocol, are discussed in [IMAP-COMPAT]. A full
+ discussion of compatibility issues with rare (and presumed extinct)
+
+
+
+
+Crispin Standards Track [Page 5]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ variants of [IMAP2] is in [IMAP-HISTORICAL]; this document is
+ primarily of historical interest.
+
+ IMAP was originally developed for the older [RFC-822] standard, and
+ as a consequence several fetch items in IMAP incorporate "RFC822" in
+ their name. With the exception of RFC822.SIZE, there are more modern
+ replacements; for example, the modern version of RFC822.HEADER is
+ BODY.PEEK[HEADER]. In all cases, "RFC822" should be interpreted as a
+ reference to the updated [RFC-2822] standard.
+
+2. Protocol Overview
+
+2.1. Link Level
+
+ The IMAP4rev1 protocol assumes a reliable data stream such as that
+ provided by TCP. When TCP is used, an IMAP4rev1 server listens on
+ port 143.
+
+2.2. Commands and Responses
+
+ An IMAP4rev1 connection consists of the establishment of a
+ client/server network connection, an initial greeting from the
+ server, and client/server interactions. These client/server
+ interactions consist of a client command, server data, and a server
+ completion result response.
+
+ All interactions transmitted by client and server are in the form of
+ lines, that is, strings that end with a CRLF. The protocol receiver
+ of an IMAP4rev1 client or server is either reading a line, or is
+ reading a sequence of octets with a known count followed by a line.
+
+2.2.1. Client Protocol Sender and Server Protocol Receiver
+
+ The client command begins an operation. Each client command is
+ prefixed with an identifier (typically a short alphanumeric string,
+ e.g., A0001, A0002, etc.) called a "tag". A different tag is
+ generated by the client for each command.
+
+ Clients MUST follow the syntax outlined in this specification
+ strictly. It is a syntax error to send a command with missing or
+ extraneous spaces or arguments.
+
+ There are two cases in which a line from the client does not
+ represent a complete command. In one case, a command argument is
+ quoted with an octet count (see the description of literal in String
+ under Data Formats); in the other case, the command arguments require
+ server feedback (see the AUTHENTICATE command). In either case, the
+
+
+
+
+Crispin Standards Track [Page 6]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ server sends a command continuation request response if it is ready
+ for the octets (if appropriate) and the remainder of the command.
+ This response is prefixed with the token "+".
+
+ Note: If instead, the server detected an error in the
+ command, it sends a BAD completion response with a tag
+ matching the command (as described below) to reject the
+ command and prevent the client from sending any more of the
+ command.
+
+ It is also possible for the server to send a completion
+ response for some other command (if multiple commands are
+ in progress), or untagged data. In either case, the
+ command continuation request is still pending; the client
+ takes the appropriate action for the response, and reads
+ another response from the server. In all cases, the client
+ MUST send a complete command (including receiving all
+ command continuation request responses and command
+ continuations for the command) before initiating a new
+ command.
+
+ The protocol receiver of an IMAP4rev1 server reads a command line
+ from the client, parses the command and its arguments, and transmits
+ server data and a server command completion result response.
+
+2.2.2. Server Protocol Sender and Client Protocol Receiver
+
+ Data transmitted by the server to the client and status responses
+ that do not indicate command completion are prefixed with the token
+ "*", and are called untagged responses.
+
+ Server data MAY be sent as a result of a client command, or MAY be
+ sent unilaterally by the server. There is no syntactic difference
+ between server data that resulted from a specific command and server
+ data that were sent unilaterally.
+
+ The server completion result response indicates the success or
+ failure of the operation. It is tagged with the same tag as the
+ client command which began the operation. Thus, if more than one
+ command is in progress, the tag in a server completion response
+ identifies the command to which the response applies. There are
+ three possible server completion responses: OK (indicating success),
+ NO (indicating failure), or BAD (indicating a protocol error such as
+ unrecognized command or command syntax error).
+
+ Servers SHOULD enforce the syntax outlined in this specification
+ strictly. Any client command with a protocol syntax error, including
+ (but not limited to) missing or extraneous spaces or arguments,
+
+
+
+Crispin Standards Track [Page 7]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ SHOULD be rejected, and the client given a BAD server completion
+ response.
+
+ The protocol receiver of an IMAP4rev1 client reads a response line
+ from the server. It then takes action on the response based upon the
+ first token of the response, which can be a tag, a "*", or a "+".
+
+ A client MUST be prepared to accept any server response at all times.
+ This includes server data that was not requested. Server data SHOULD
+ be recorded, so that the client can reference its recorded copy
+ rather than sending a command to the server to request the data. In
+ the case of certain server data, the data MUST be recorded.
+
+ This topic is discussed in greater detail in the Server Responses
+ section.
+
+2.3. Message Attributes
+
+ In addition to message text, each message has several attributes
+ associated with it. These attributes can be retrieved individually
+ or in conjunction with other attributes or message texts.
+
+2.3.1. Message Numbers
+
+ Messages in IMAP4rev1 are accessed by one of two numbers; the unique
+ identifier or the message sequence number.
+
+
+2.3.1.1. Unique Identifier (UID) Message Attribute
+
+ A 32-bit value assigned to each message, which when used with the
+ unique identifier validity value (see below) forms a 64-bit value
+ that MUST NOT refer to any other message in the mailbox or any
+ subsequent mailbox with the same name forever. Unique identifiers
+ are assigned in a strictly ascending fashion in the mailbox; as each
+ message is added to the mailbox it is assigned a higher UID than the
+ message(s) which were added previously. Unlike message sequence
+ numbers, unique identifiers are not necessarily contiguous.
+
+ The unique identifier of a message MUST NOT change during the
+ session, and SHOULD NOT change between sessions. Any change of
+ unique identifiers between sessions MUST be detectable using the
+ UIDVALIDITY mechanism discussed below. Persistent unique identifiers
+ are required for a client to resynchronize its state from a previous
+ session with the server (e.g., disconnected or offline access
+ clients); this is discussed further in [IMAP-DISC].
+
+
+
+
+
+Crispin Standards Track [Page 8]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Associated with every mailbox are two values which aid in unique
+ identifier handling: the next unique identifier value and the unique
+ identifier validity value.
+
+ The next unique identifier value is the predicted value that will be
+ assigned to a new message in the mailbox. Unless the unique
+ identifier validity also changes (see below), the next unique
+ identifier value MUST have the following two characteristics. First,
+ the next unique identifier value MUST NOT change unless new messages
+ are added to the mailbox; and second, the next unique identifier
+ value MUST change whenever new messages are added to the mailbox,
+ even if those new messages are subsequently expunged.
+
+ Note: The next unique identifier value is intended to
+ provide a means for a client to determine whether any
+ messages have been delivered to the mailbox since the
+ previous time it checked this value. It is not intended to
+ provide any guarantee that any message will have this
+ unique identifier. A client can only assume, at the time
+ that it obtains the next unique identifier value, that
+ messages arriving after that time will have a UID greater
+ than or equal to that value.
+
+ The unique identifier validity value is sent in a UIDVALIDITY
+ response code in an OK untagged response at mailbox selection time.
+ If unique identifiers from an earlier session fail to persist in this
+ session, the unique identifier validity value MUST be greater than
+ the one used in the earlier session.
+
+ Note: Ideally, unique identifiers SHOULD persist at all
+ times. Although this specification recognizes that failure
+ to persist can be unavoidable in certain server
+ environments, it STRONGLY ENCOURAGES message store
+ implementation techniques that avoid this problem. For
+ example:
+
+ 1) Unique identifiers MUST be strictly ascending in the
+ mailbox at all times. If the physical message store is
+ re-ordered by a non-IMAP agent, this requires that the
+ unique identifiers in the mailbox be regenerated, since
+ the former unique identifiers are no longer strictly
+ ascending as a result of the re-ordering.
+
+ 2) If the message store has no mechanism to store unique
+ identifiers, it must regenerate unique identifiers at
+ each session, and each session must have a unique
+ UIDVALIDITY value.
+
+
+
+
+Crispin Standards Track [Page 9]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 3) If the mailbox is deleted and a new mailbox with the
+ same name is created at a later date, the server must
+ either keep track of unique identifiers from the
+ previous instance of the mailbox, or it must assign a
+ new UIDVALIDITY value to the new instance of the
+ mailbox. A good UIDVALIDITY value to use in this case
+ is a 32-bit representation of the creation date/time of
+ the mailbox. It is alright to use a constant such as
+ 1, but only if it guaranteed that unique identifiers
+ will never be reused, even in the case of a mailbox
+ being deleted (or renamed) and a new mailbox by the
+ same name created at some future time.
+
+ 4) The combination of mailbox name, UIDVALIDITY, and UID
+ must refer to a single immutable message on that server
+ forever. In particular, the internal date, [RFC-2822]
+ size, envelope, body structure, and message texts
+ (RFC822, RFC822.HEADER, RFC822.TEXT, and all BODY[...]
+ fetch data items) must never change. This does not
+ include message numbers, nor does it include attributes
+ that can be set by a STORE command (e.g., FLAGS).
+
+
+2.3.1.2. Message Sequence Number Message Attribute
+
+ A relative position from 1 to the number of messages in the mailbox.
+ This position MUST be ordered by ascending unique identifier. As
+ each new message is added, it is assigned a message sequence number
+ that is 1 higher than the number of messages in the mailbox before
+ that new message was added.
+
+ Message sequence numbers can be reassigned during the session. For
+ example, when a message is permanently removed (expunged) from the
+ mailbox, the message sequence number for all subsequent messages is
+ decremented. The number of messages in the mailbox is also
+ decremented. Similarly, a new message can be assigned a message
+ sequence number that was once held by some other message prior to an
+ expunge.
+
+ In addition to accessing messages by relative position in the
+ mailbox, message sequence numbers can be used in mathematical
+ calculations. For example, if an untagged "11 EXISTS" is received,
+ and previously an untagged "8 EXISTS" was received, three new
+ messages have arrived with message sequence numbers of 9, 10, and 11.
+ Another example, if message 287 in a 523 message mailbox has UID
+ 12345, there are exactly 286 messages which have lesser UIDs and 236
+ messages which have greater UIDs.
+
+
+
+
+Crispin Standards Track [Page 10]
+
+RFC 3501 IMAPv4 March 2003
+
+
+2.3.2. Flags Message Attribute
+
+ A list of zero or more named tokens associated with the message. A
+ flag is set by its addition to this list, and is cleared by its
+ removal. There are two types of flags in IMAP4rev1. A flag of
+ either type can be permanent or session-only.
+
+ A system flag is a flag name that is pre-defined in this
+ specification. All system flags begin with "\". Certain system
+ flags (\Deleted and \Seen) have special semantics described
+ elsewhere. The currently-defined system flags are:
+
+ \Seen
+ Message has been read
+
+ \Answered
+ Message has been answered
+
+ \Flagged
+ Message is "flagged" for urgent/special attention
+
+ \Deleted
+ Message is "deleted" for removal by later EXPUNGE
+
+ \Draft
+ Message has not completed composition (marked as a draft).
+
+ \Recent
+ Message is "recently" arrived in this mailbox. This session
+ is the first session to have been notified about this
+ message; if the session is read-write, subsequent sessions
+ will not see \Recent set for this message. This flag can not
+ be altered by the client.
+
+ If it is not possible to determine whether or not this
+ session is the first session to be notified about a message,
+ then that message SHOULD be considered recent.
+
+ If multiple connections have the same mailbox selected
+ simultaneously, it is undefined which of these connections
+ will see newly-arrived messages with \Recent set and which
+ will see it without \Recent set.
+
+ A keyword is defined by the server implementation. Keywords do not
+ begin with "\". Servers MAY permit the client to define new keywords
+ in the mailbox (see the description of the PERMANENTFLAGS response
+ code for more information).
+
+
+
+
+Crispin Standards Track [Page 11]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ A flag can be permanent or session-only on a per-flag basis.
+ Permanent flags are those which the client can add or remove from the
+ message flags permanently; that is, concurrent and subsequent
+ sessions will see any change in permanent flags. Changes to session
+ flags are valid only in that session.
+
+ Note: The \Recent system flag is a special case of a
+ session flag. \Recent can not be used as an argument in a
+ STORE or APPEND command, and thus can not be changed at
+ all.
+
+2.3.3. Internal Date Message Attribute
+
+ The internal date and time of the message on the server. This
+ is not the date and time in the [RFC-2822] header, but rather a
+ date and time which reflects when the message was received. In
+ the case of messages delivered via [SMTP], this SHOULD be the
+ date and time of final delivery of the message as defined by
+ [SMTP]. In the case of messages delivered by the IMAP4rev1 COPY
+ command, this SHOULD be the internal date and time of the source
+ message. In the case of messages delivered by the IMAP4rev1
+ APPEND command, this SHOULD be the date and time as specified in
+ the APPEND command description. All other cases are
+ implementation defined.
+
+2.3.4. [RFC-2822] Size Message Attribute
+
+ The number of octets in the message, as expressed in [RFC-2822]
+ format.
+
+2.3.5. Envelope Structure Message Attribute
+
+ A parsed representation of the [RFC-2822] header of the message.
+ Note that the IMAP Envelope structure is not the same as an
+ [SMTP] envelope.
+
+2.3.6. Body Structure Message Attribute
+
+ A parsed representation of the [MIME-IMB] body structure
+ information of the message.
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 12]
+
+RFC 3501 IMAPv4 March 2003
+
+
+2.4. Message Texts
+
+ In addition to being able to fetch the full [RFC-2822] text of a
+ message, IMAP4rev1 permits the fetching of portions of the full
+ message text. Specifically, it is possible to fetch the
+ [RFC-2822] message header, [RFC-2822] message body, a [MIME-IMB]
+ body part, or a [MIME-IMB] header.
+
+3. State and Flow Diagram
+
+ Once the connection between client and server is established, an
+ IMAP4rev1 connection is in one of four states. The initial
+ state is identified in the server greeting. Most commands are
+ only valid in certain states. It is a protocol error for the
+ client to attempt a command while the connection is in an
+ inappropriate state, and the server will respond with a BAD or
+ NO (depending upon server implementation) command completion
+ result.
+
+3.1. Not Authenticated State
+
+ In the not authenticated state, the client MUST supply
+ authentication credentials before most commands will be
+ permitted. This state is entered when a connection starts
+ unless the connection has been pre-authenticated.
+
+3.2. Authenticated State
+
+ In the authenticated state, the client is authenticated and MUST
+ select a mailbox to access before commands that affect messages
+ will be permitted. This state is entered when a
+ pre-authenticated connection starts, when acceptable
+ authentication credentials have been provided, after an error in
+ selecting a mailbox, or after a successful CLOSE command.
+
+3.3. Selected State
+
+ In a selected state, a mailbox has been selected to access.
+ This state is entered when a mailbox has been successfully
+ selected.
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 13]
+
+RFC 3501 IMAPv4 March 2003
+
+
+3.4. Logout State
+
+ In the logout state, the connection is being terminated. This
+ state can be entered as a result of a client request (via the
+ LOGOUT command) or by unilateral action on the part of either
+ the client or server.
+
+ If the client requests the logout state, the server MUST send an
+ untagged BYE response and a tagged OK response to the LOGOUT
+ command before the server closes the connection; and the client
+ MUST read the tagged OK response to the LOGOUT command before
+ the client closes the connection.
+
+ A server MUST NOT unilaterally close the connection without
+ sending an untagged BYE response that contains the reason for
+ having done so. A client SHOULD NOT unilaterally close the
+ connection, and instead SHOULD issue a LOGOUT command. If the
+ server detects that the client has unilaterally closed the
+ connection, the server MAY omit the untagged BYE response and
+ simply close its connection.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 14]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ +----------------------+
+ |connection established|
+ +----------------------+
+ ||
+ \/
+ +--------------------------------------+
+ | server greeting |
+ +--------------------------------------+
+ || (1) || (2) || (3)
+ \/ || ||
+ +-----------------+ || ||
+ |Not Authenticated| || ||
+ +-----------------+ || ||
+ || (7) || (4) || ||
+ || \/ \/ ||
+ || +----------------+ ||
+ || | Authenticated |<=++ ||
+ || +----------------+ || ||
+ || || (7) || (5) || (6) ||
+ || || \/ || ||
+ || || +--------+ || ||
+ || || |Selected|==++ ||
+ || || +--------+ ||
+ || || || (7) ||
+ \/ \/ \/ \/
+ +--------------------------------------+
+ | Logout |
+ +--------------------------------------+
+ ||
+ \/
+ +-------------------------------+
+ |both sides close the connection|
+ +-------------------------------+
+
+ (1) connection without pre-authentication (OK greeting)
+ (2) pre-authenticated connection (PREAUTH greeting)
+ (3) rejected connection (BYE greeting)
+ (4) successful LOGIN or AUTHENTICATE command
+ (5) successful SELECT or EXAMINE command
+ (6) CLOSE command, or failed SELECT or EXAMINE command
+ (7) LOGOUT command, server shutdown, or connection closed
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 15]
+
+RFC 3501 IMAPv4 March 2003
+
+
+4. Data Formats
+
+ IMAP4rev1 uses textual commands and responses. Data in
+ IMAP4rev1 can be in one of several forms: atom, number, string,
+ parenthesized list, or NIL. Note that a particular data item
+ may take more than one form; for example, a data item defined as
+ using "astring" syntax may be either an atom or a string.
+
+4.1. Atom
+
+ An atom consists of one or more non-special characters.
+
+4.2. Number
+
+ A number consists of one or more digit characters, and
+ represents a numeric value.
+
+4.3. String
+
+ A string is in one of two forms: either literal or quoted
+ string. The literal form is the general form of string. The
+ quoted string form is an alternative that avoids the overhead of
+ processing a literal at the cost of limitations of characters
+ which may be used.
+
+ A literal is a sequence of zero or more octets (including CR and
+ LF), prefix-quoted with an octet count in the form of an open
+ brace ("{"), the number of octets, close brace ("}"), and CRLF.
+ In the case of literals transmitted from server to client, the
+ CRLF is immediately followed by the octet data. In the case of
+ literals transmitted from client to server, the client MUST wait
+ to receive a command continuation request (described later in
+ this document) before sending the octet data (and the remainder
+ of the command).
+
+ A quoted string is a sequence of zero or more 7-bit characters,
+ excluding CR and LF, with double quote (<">) characters at each
+ end.
+
+ The empty string is represented as either "" (a quoted string
+ with zero characters between double quotes) or as {0} followed
+ by CRLF (a literal with an octet count of 0).
+
+ Note: Even if the octet count is 0, a client transmitting a
+ literal MUST wait to receive a command continuation request.
+
+
+
+
+
+
+Crispin Standards Track [Page 16]
+
+RFC 3501 IMAPv4 March 2003
+
+
+4.3.1. 8-bit and Binary Strings
+
+ 8-bit textual and binary mail is supported through the use of a
+ [MIME-IMB] content transfer encoding. IMAP4rev1 implementations MAY
+ transmit 8-bit or multi-octet characters in literals, but SHOULD do
+ so only when the [CHARSET] is identified.
+
+ Although a BINARY body encoding is defined, unencoded binary strings
+ are not permitted. A "binary string" is any string with NUL
+ characters. Implementations MUST encode binary data into a textual
+ form, such as BASE64, before transmitting the data. A string with an
+ excessive amount of CTL characters MAY also be considered to be
+ binary.
+
+4.4. Parenthesized List
+
+ Data structures are represented as a "parenthesized list"; a sequence
+ of data items, delimited by space, and bounded at each end by
+ parentheses. A parenthesized list can contain other parenthesized
+ lists, using multiple levels of parentheses to indicate nesting.
+
+ The empty list is represented as () -- a parenthesized list with no
+ members.
+
+4.5. NIL
+
+ The special form "NIL" represents the non-existence of a particular
+ data item that is represented as a string or parenthesized list, as
+ distinct from the empty string "" or the empty parenthesized list ().
+
+ Note: NIL is never used for any data item which takes the
+ form of an atom. For example, a mailbox name of "NIL" is a
+ mailbox named NIL as opposed to a non-existent mailbox
+ name. This is because mailbox uses "astring" syntax which
+ is an atom or a string. Conversely, an addr-name of NIL is
+ a non-existent personal name, because addr-name uses
+ "nstring" syntax which is NIL or a string, but never an
+ atom.
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 17]
+
+RFC 3501 IMAPv4 March 2003
+
+
+5. Operational Considerations
+
+ The following rules are listed here to ensure that all IMAP4rev1
+ implementations interoperate properly.
+
+5.1. Mailbox Naming
+
+ Mailbox names are 7-bit. Client implementations MUST NOT attempt to
+ create 8-bit mailbox names, and SHOULD interpret any 8-bit mailbox
+ names returned by LIST or LSUB as UTF-8. Server implementations
+ SHOULD prohibit the creation of 8-bit mailbox names, and SHOULD NOT
+ return 8-bit mailbox names in LIST or LSUB. See section 5.1.3 for
+ more information on how to represent non-ASCII mailbox names.
+
+ Note: 8-bit mailbox names were undefined in earlier
+ versions of this protocol. Some sites used a local 8-bit
+ character set to represent non-ASCII mailbox names. Such
+ usage is not interoperable, and is now formally deprecated.
+
+ The case-insensitive mailbox name INBOX is a special name reserved to
+ mean "the primary mailbox for this user on this server". The
+ interpretation of all other names is implementation-dependent.
+
+ In particular, this specification takes no position on case
+ sensitivity in non-INBOX mailbox names. Some server implementations
+ are fully case-sensitive; others preserve case of a newly-created
+ name but otherwise are case-insensitive; and yet others coerce names
+ to a particular case. Client implementations MUST interact with any
+ of these. If a server implementation interprets non-INBOX mailbox
+ names as case-insensitive, it MUST treat names using the
+ international naming convention specially as described in section
+ 5.1.3.
+
+ There are certain client considerations when creating a new mailbox
+ name:
+
+ 1) Any character which is one of the atom-specials (see the Formal
+ Syntax) will require that the mailbox name be represented as a
+ quoted string or literal.
+
+ 2) CTL and other non-graphic characters are difficult to represent
+ in a user interface and are best avoided.
+
+ 3) Although the list-wildcard characters ("%" and "*") are valid
+ in a mailbox name, it is difficult to use such mailbox names
+ with the LIST and LSUB commands due to the conflict with
+ wildcard interpretation.
+
+
+
+
+Crispin Standards Track [Page 18]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 4) Usually, a character (determined by the server implementation)
+ is reserved to delimit levels of hierarchy.
+
+ 5) Two characters, "#" and "&", have meanings by convention, and
+ should be avoided except when used in that convention.
+
+5.1.1. Mailbox Hierarchy Naming
+
+ If it is desired to export hierarchical mailbox names, mailbox names
+ MUST be left-to-right hierarchical using a single character to
+ separate levels of hierarchy. The same hierarchy separator character
+ is used for all levels of hierarchy within a single name.
+
+5.1.2. Mailbox Namespace Naming Convention
+
+ By convention, the first hierarchical element of any mailbox name
+ which begins with "#" identifies the "namespace" of the remainder of
+ the name. This makes it possible to disambiguate between different
+ types of mailbox stores, each of which have their own namespaces.
+
+ For example, implementations which offer access to USENET
+ newsgroups MAY use the "#news" namespace to partition the
+ USENET newsgroup namespace from that of other mailboxes.
+ Thus, the comp.mail.misc newsgroup would have a mailbox
+ name of "#news.comp.mail.misc", and the name
+ "comp.mail.misc" can refer to a different object (e.g., a
+ user's private mailbox).
+
+5.1.3. Mailbox International Naming Convention
+
+ By convention, international mailbox names in IMAP4rev1 are specified
+ using a modified version of the UTF-7 encoding described in [UTF-7].
+ Modified UTF-7 may also be usable in servers that implement an
+ earlier version of this protocol.
+
+ In modified UTF-7, printable US-ASCII characters, except for "&",
+ represent themselves; that is, characters with octet values 0x20-0x25
+ and 0x27-0x7e. The character "&" (0x26) is represented by the
+ two-octet sequence "&-".
+
+ All other characters (octet values 0x00-0x1f and 0x7f-0xff) are
+ represented in modified BASE64, with a further modification from
+ [UTF-7] that "," is used instead of "/". Modified BASE64 MUST NOT be
+ used to represent any printing US-ASCII character which can represent
+ itself.
+
+
+
+
+
+
+Crispin Standards Track [Page 19]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ "&" is used to shift to modified BASE64 and "-" to shift back to
+ US-ASCII. There is no implicit shift from BASE64 to US-ASCII, and
+ null shifts ("-&" while in BASE64; note that "&-" while in US-ASCII
+ means "&") are not permitted. However, all names start in US-ASCII,
+ and MUST end in US-ASCII; that is, a name that ends with a non-ASCII
+ ISO-10646 character MUST end with a "-").
+
+ The purpose of these modifications is to correct the following
+ problems with UTF-7:
+
+ 1) UTF-7 uses the "+" character for shifting; this conflicts with
+ the common use of "+" in mailbox names, in particular USENET
+ newsgroup names.
+
+ 2) UTF-7's encoding is BASE64 which uses the "/" character; this
+ conflicts with the use of "/" as a popular hierarchy delimiter.
+
+ 3) UTF-7 prohibits the unencoded usage of "\"; this conflicts with
+ the use of "\" as a popular hierarchy delimiter.
+
+ 4) UTF-7 prohibits the unencoded usage of "~"; this conflicts with
+ the use of "~" in some servers as a home directory indicator.
+
+ 5) UTF-7 permits multiple alternate forms to represent the same
+ string; in particular, printable US-ASCII characters can be
+ represented in encoded form.
+
+ Although modified UTF-7 is a convention, it establishes certain
+ requirements on server handling of any mailbox name with an
+ embedded "&" character. In particular, server implementations
+ MUST preserve the exact form of the modified BASE64 portion of a
+ modified UTF-7 name and treat that text as case-sensitive, even if
+ names are otherwise case-insensitive or case-folded.
+
+ Server implementations SHOULD verify that any mailbox name with an
+ embedded "&" character, used as an argument to CREATE, is: in the
+ correctly modified UTF-7 syntax, has no superfluous shifts, and
+ has no encoding in modified BASE64 of any printing US-ASCII
+ character which can represent itself. However, client
+ implementations MUST NOT depend upon the server doing this, and
+ SHOULD NOT attempt to create a mailbox name with an embedded "&"
+ character unless it complies with the modified UTF-7 syntax.
+
+ Server implementations which export a mail store that does not
+ follow the modified UTF-7 convention MUST convert to modified
+ UTF-7 any mailbox name that contains either non-ASCII characters
+ or the "&" character.
+
+
+
+
+Crispin Standards Track [Page 20]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ For example, here is a mailbox name which mixes English,
+ Chinese, and Japanese text:
+ ~peter/mail/&U,BTFw-/&ZeVnLIqe-
+
+ For example, the string "&Jjo!" is not a valid mailbox
+ name because it does not contain a shift to US-ASCII
+ before the "!". The correct form is "&Jjo-!". The
+ string "&U,BTFw-&ZeVnLIqe-" is not permitted because it
+ contains a superfluous shift. The correct form is
+ "&U,BTF2XlZyyKng-".
+
+5.2. Mailbox Size and Message Status Updates
+
+ At any time, a server can send data that the client did not request.
+ Sometimes, such behavior is REQUIRED. For example, agents other than
+ the server MAY add messages to the mailbox (e.g., new message
+ delivery), change the flags of the messages in the mailbox (e.g.,
+ simultaneous access to the same mailbox by multiple agents), or even
+ remove messages from the mailbox. A server MUST send mailbox size
+ updates automatically if a mailbox size change is observed during the
+ processing of a command. A server SHOULD send message flag updates
+ automatically, without requiring the client to request such updates
+ explicitly.
+
+ Special rules exist for server notification of a client about the
+ removal of messages to prevent synchronization errors; see the
+ description of the EXPUNGE response for more detail. In particular,
+ it is NOT permitted to send an EXISTS response that would reduce the
+ number of messages in the mailbox; only the EXPUNGE response can do
+ this.
+
+ Regardless of what implementation decisions a client makes on
+ remembering data from the server, a client implementation MUST record
+ mailbox size updates. It MUST NOT assume that any command after the
+ initial mailbox selection will return the size of the mailbox.
+
+5.3. Response when no Command in Progress
+
+ Server implementations are permitted to send an untagged response
+ (except for EXPUNGE) while there is no command in progress. Server
+ implementations that send such responses MUST deal with flow control
+ considerations. Specifically, they MUST either (1) verify that the
+ size of the data does not exceed the underlying transport's available
+ window size, or (2) use non-blocking writes.
+
+
+
+
+
+
+
+Crispin Standards Track [Page 21]
+
+RFC 3501 IMAPv4 March 2003
+
+
+5.4. Autologout Timer
+
+ If a server has an inactivity autologout timer, the duration of that
+ timer MUST be at least 30 minutes. The receipt of ANY command from
+ the client during that interval SHOULD suffice to reset the
+ autologout timer.
+
+5.5. Multiple Commands in Progress
+
+ The client MAY send another command without waiting for the
+ completion result response of a command, subject to ambiguity rules
+ (see below) and flow control constraints on the underlying data
+ stream. Similarly, a server MAY begin processing another command
+ before processing the current command to completion, subject to
+ ambiguity rules. However, any command continuation request responses
+ and command continuations MUST be negotiated before any subsequent
+ command is initiated.
+
+ The exception is if an ambiguity would result because of a command
+ that would affect the results of other commands. Clients MUST NOT
+ send multiple commands without waiting if an ambiguity would result.
+ If the server detects a possible ambiguity, it MUST execute commands
+ to completion in the order given by the client.
+
+ The most obvious example of ambiguity is when a command would affect
+ the results of another command, e.g., a FETCH of a message's flags
+ and a STORE of that same message's flags.
+
+ A non-obvious ambiguity occurs with commands that permit an untagged
+ EXPUNGE response (commands other than FETCH, STORE, and SEARCH),
+ since an untagged EXPUNGE response can invalidate sequence numbers in
+ a subsequent command. This is not a problem for FETCH, STORE, or
+ SEARCH commands because servers are prohibited from sending EXPUNGE
+ responses while any of those commands are in progress. Therefore, if
+ the client sends any command other than FETCH, STORE, or SEARCH, it
+ MUST wait for the completion result response before sending a command
+ with message sequence numbers.
+
+ Note: UID FETCH, UID STORE, and UID SEARCH are different
+ commands from FETCH, STORE, and SEARCH. If the client
+ sends a UID command, it must wait for a completion result
+ response before sending a command with message sequence
+ numbers.
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 22]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ For example, the following non-waiting command sequences are invalid:
+
+ FETCH + NOOP + STORE
+ STORE + COPY + FETCH
+ COPY + COPY
+ CHECK + FETCH
+
+ The following are examples of valid non-waiting command sequences:
+
+ FETCH + STORE + SEARCH + CHECK
+ STORE + COPY + EXPUNGE
+
+ UID SEARCH + UID SEARCH may be valid or invalid as a non-waiting
+ command sequence, depending upon whether or not the second UID
+ SEARCH contains message sequence numbers.
+
+6. Client Commands
+
+ IMAP4rev1 commands are described in this section. Commands are
+ organized by the state in which the command is permitted. Commands
+ which are permitted in multiple states are listed in the minimum
+ permitted state (for example, commands valid in authenticated and
+ selected state are listed in the authenticated state commands).
+
+ Command arguments, identified by "Arguments:" in the command
+ descriptions below, are described by function, not by syntax. The
+ precise syntax of command arguments is described in the Formal Syntax
+ section.
+
+ Some commands cause specific server responses to be returned; these
+ are identified by "Responses:" in the command descriptions below.
+ See the response descriptions in the Responses section for
+ information on these responses, and the Formal Syntax section for the
+ precise syntax of these responses. It is possible for server data to
+ be transmitted as a result of any command. Thus, commands that do
+ not specifically require server data specify "no specific responses
+ for this command" instead of "none".
+
+ The "Result:" in the command description refers to the possible
+ tagged status responses to a command, and any special interpretation
+ of these status responses.
+
+ The state of a connection is only changed by successful commands
+ which are documented as changing state. A rejected command (BAD
+ response) never changes the state of the connection or of the
+ selected mailbox. A failed command (NO response) generally does not
+ change the state of the connection or of the selected mailbox; the
+ exception being the SELECT and EXAMINE commands.
+
+
+
+Crispin Standards Track [Page 23]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.1. Client Commands - Any State
+
+ The following commands are valid in any state: CAPABILITY, NOOP, and
+ LOGOUT.
+
+6.1.1. CAPABILITY Command
+
+ Arguments: none
+
+ Responses: REQUIRED untagged response: CAPABILITY
+
+ Result: OK - capability completed
+ BAD - command unknown or arguments invalid
+
+ The CAPABILITY command requests a listing of capabilities that the
+ server supports. The server MUST send a single untagged
+ CAPABILITY response with "IMAP4rev1" as one of the listed
+ capabilities before the (tagged) OK response.
+
+ A capability name which begins with "AUTH=" indicates that the
+ server supports that particular authentication mechanism. All
+ such names are, by definition, part of this specification. For
+ example, the authorization capability for an experimental
+ "blurdybloop" authenticator would be "AUTH=XBLURDYBLOOP" and not
+ "XAUTH=BLURDYBLOOP" or "XAUTH=XBLURDYBLOOP".
+
+ Other capability names refer to extensions, revisions, or
+ amendments to this specification. See the documentation of the
+ CAPABILITY response for additional information. No capabilities,
+ beyond the base IMAP4rev1 set defined in this specification, are
+ enabled without explicit client action to invoke the capability.
+
+ Client and server implementations MUST implement the STARTTLS,
+ LOGINDISABLED, and AUTH=PLAIN (described in [IMAP-TLS])
+ capabilities. See the Security Considerations section for
+ important information.
+
+ See the section entitled "Client Commands -
+ Experimental/Expansion" for information about the form of site or
+ implementation-specific capabilities.
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 24]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Example: C: abcd CAPABILITY
+ S: * CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI
+ LOGINDISABLED
+ S: abcd OK CAPABILITY completed
+ C: efgh STARTTLS
+ S: efgh OK STARTLS completed
+ <TLS negotiation, further commands are under [TLS] layer>
+ C: ijkl CAPABILITY
+ S: * CAPABILITY IMAP4rev1 AUTH=GSSAPI AUTH=PLAIN
+ S: ijkl OK CAPABILITY completed
+
+
+6.1.2. NOOP Command
+
+ Arguments: none
+
+ Responses: no specific responses for this command (but see below)
+
+ Result: OK - noop completed
+ BAD - command unknown or arguments invalid
+
+ The NOOP command always succeeds. It does nothing.
+
+ Since any command can return a status update as untagged data, the
+ NOOP command can be used as a periodic poll for new messages or
+ message status updates during a period of inactivity (this is the
+ preferred method to do this). The NOOP command can also be used
+ to reset any inactivity autologout timer on the server.
+
+ Example: C: a002 NOOP
+ S: a002 OK NOOP completed
+ . . .
+ C: a047 NOOP
+ S: * 22 EXPUNGE
+ S: * 23 EXISTS
+ S: * 3 RECENT
+ S: * 14 FETCH (FLAGS (\Seen \Deleted))
+ S: a047 OK NOOP completed
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 25]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.1.3. LOGOUT Command
+
+ Arguments: none
+
+ Responses: REQUIRED untagged response: BYE
+
+ Result: OK - logout completed
+ BAD - command unknown or arguments invalid
+
+ The LOGOUT command informs the server that the client is done with
+ the connection. The server MUST send a BYE untagged response
+ before the (tagged) OK response, and then close the network
+ connection.
+
+ Example: C: A023 LOGOUT
+ S: * BYE IMAP4rev1 Server logging out
+ S: A023 OK LOGOUT completed
+ (Server and client then close the connection)
+
+6.2. Client Commands - Not Authenticated State
+
+ In the not authenticated state, the AUTHENTICATE or LOGIN command
+ establishes authentication and enters the authenticated state. The
+ AUTHENTICATE command provides a general mechanism for a variety of
+ authentication techniques, privacy protection, and integrity
+ checking; whereas the LOGIN command uses a traditional user name and
+ plaintext password pair and has no means of establishing privacy
+ protection or integrity checking.
+
+ The STARTTLS command is an alternate form of establishing session
+ privacy protection and integrity checking, but does not establish
+ authentication or enter the authenticated state.
+
+ Server implementations MAY allow access to certain mailboxes without
+ establishing authentication. This can be done by means of the
+ ANONYMOUS [SASL] authenticator described in [ANONYMOUS]. An older
+ convention is a LOGIN command using the userid "anonymous"; in this
+ case, a password is required although the server may choose to accept
+ any password. The restrictions placed on anonymous users are
+ implementation-dependent.
+
+ Once authenticated (including as anonymous), it is not possible to
+ re-enter not authenticated state.
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 26]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ In addition to the universal commands (CAPABILITY, NOOP, and LOGOUT),
+ the following commands are valid in the not authenticated state:
+ STARTTLS, AUTHENTICATE and LOGIN. See the Security Considerations
+ section for important information about these commands.
+
+6.2.1. STARTTLS Command
+
+ Arguments: none
+
+ Responses: no specific response for this command
+
+ Result: OK - starttls completed, begin TLS negotiation
+ BAD - command unknown or arguments invalid
+
+ A [TLS] negotiation begins immediately after the CRLF at the end
+ of the tagged OK response from the server. Once a client issues a
+ STARTTLS command, it MUST NOT issue further commands until a
+ server response is seen and the [TLS] negotiation is complete.
+
+ The server remains in the non-authenticated state, even if client
+ credentials are supplied during the [TLS] negotiation. This does
+ not preclude an authentication mechanism such as EXTERNAL (defined
+ in [SASL]) from using client identity determined by the [TLS]
+ negotiation.
+
+ Once [TLS] has been started, the client MUST discard cached
+ information about server capabilities and SHOULD re-issue the
+ CAPABILITY command. This is necessary to protect against man-in-
+ the-middle attacks which alter the capabilities list prior to
+ STARTTLS. The server MAY advertise different capabilities after
+ STARTTLS.
+
+ Example: C: a001 CAPABILITY
+ S: * CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED
+ S: a001 OK CAPABILITY completed
+ C: a002 STARTTLS
+ S: a002 OK Begin TLS negotiation now
+ <TLS negotiation, further commands are under [TLS] layer>
+ C: a003 CAPABILITY
+ S: * CAPABILITY IMAP4rev1 AUTH=PLAIN
+ S: a003 OK CAPABILITY completed
+ C: a004 LOGIN joe password
+ S: a004 OK LOGIN completed
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 27]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.2.2. AUTHENTICATE Command
+
+ Arguments: authentication mechanism name
+
+ Responses: continuation data can be requested
+
+ Result: OK - authenticate completed, now in authenticated state
+ NO - authenticate failure: unsupported authentication
+ mechanism, credentials rejected
+ BAD - command unknown or arguments invalid,
+ authentication exchange cancelled
+
+ The AUTHENTICATE command indicates a [SASL] authentication
+ mechanism to the server. If the server supports the requested
+ authentication mechanism, it performs an authentication protocol
+ exchange to authenticate and identify the client. It MAY also
+ negotiate an OPTIONAL security layer for subsequent protocol
+ interactions. If the requested authentication mechanism is not
+ supported, the server SHOULD reject the AUTHENTICATE command by
+ sending a tagged NO response.
+
+ The AUTHENTICATE command does not support the optional "initial
+ response" feature of [SASL]. Section 5.1 of [SASL] specifies how
+ to handle an authentication mechanism which uses an initial
+ response.
+
+ The service name specified by this protocol's profile of [SASL] is
+ "imap".
+
+ The authentication protocol exchange consists of a series of
+ server challenges and client responses that are specific to the
+ authentication mechanism. A server challenge consists of a
+ command continuation request response with the "+" token followed
+ by a BASE64 encoded string. The client response consists of a
+ single line consisting of a BASE64 encoded string. If the client
+ wishes to cancel an authentication exchange, it issues a line
+ consisting of a single "*". If the server receives such a
+ response, it MUST reject the AUTHENTICATE command by sending a
+ tagged BAD response.
+
+ If a security layer is negotiated through the [SASL]
+ authentication exchange, it takes effect immediately following the
+ CRLF that concludes the authentication exchange for the client,
+ and the CRLF of the tagged OK response for the server.
+
+ While client and server implementations MUST implement the
+ AUTHENTICATE command itself, it is not required to implement any
+ authentication mechanisms other than the PLAIN mechanism described
+
+
+
+Crispin Standards Track [Page 28]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ in [IMAP-TLS]. Also, an authentication mechanism is not required
+ to support any security layers.
+
+ Note: a server implementation MUST implement a
+ configuration in which it does NOT permit any plaintext
+ password mechanisms, unless either the STARTTLS command
+ has been negotiated or some other mechanism that
+ protects the session from password snooping has been
+ provided. Server sites SHOULD NOT use any configuration
+ which permits a plaintext password mechanism without
+ such a protection mechanism against password snooping.
+ Client and server implementations SHOULD implement
+ additional [SASL] mechanisms that do not use plaintext
+ passwords, such the GSSAPI mechanism described in [SASL]
+ and/or the [DIGEST-MD5] mechanism.
+
+ Servers and clients can support multiple authentication
+ mechanisms. The server SHOULD list its supported authentication
+ mechanisms in the response to the CAPABILITY command so that the
+ client knows which authentication mechanisms to use.
+
+ A server MAY include a CAPABILITY response code in the tagged OK
+ response of a successful AUTHENTICATE command in order to send
+ capabilities automatically. It is unnecessary for a client to
+ send a separate CAPABILITY command if it recognizes these
+ automatic capabilities. This should only be done if a security
+ layer was not negotiated by the AUTHENTICATE command, because the
+ tagged OK response as part of an AUTHENTICATE command is not
+ protected by encryption/integrity checking. [SASL] requires the
+ client to re-issue a CAPABILITY command in this case.
+
+ If an AUTHENTICATE command fails with a NO response, the client
+ MAY try another authentication mechanism by issuing another
+ AUTHENTICATE command. It MAY also attempt to authenticate by
+ using the LOGIN command (see section 6.2.3 for more detail). In
+ other words, the client MAY request authentication types in
+ decreasing order of preference, with the LOGIN command as a last
+ resort.
+
+ The authorization identity passed from the client to the server
+ during the authentication exchange is interpreted by the server as
+ the user name whose privileges the client is requesting.
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 29]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Example: S: * OK IMAP4rev1 Server
+ C: A001 AUTHENTICATE GSSAPI
+ S: +
+ C: YIIB+wYJKoZIhvcSAQICAQBuggHqMIIB5qADAgEFoQMCAQ6iBw
+ MFACAAAACjggEmYYIBIjCCAR6gAwIBBaESGxB1Lndhc2hpbmd0
+ b24uZWR1oi0wK6ADAgEDoSQwIhsEaW1hcBsac2hpdmFtcy5jYW
+ Mud2FzaGluZ3Rvbi5lZHWjgdMwgdCgAwIBAaEDAgEDooHDBIHA
+ cS1GSa5b+fXnPZNmXB9SjL8Ollj2SKyb+3S0iXMljen/jNkpJX
+ AleKTz6BQPzj8duz8EtoOuNfKgweViyn/9B9bccy1uuAE2HI0y
+ C/PHXNNU9ZrBziJ8Lm0tTNc98kUpjXnHZhsMcz5Mx2GR6dGknb
+ I0iaGcRerMUsWOuBmKKKRmVMMdR9T3EZdpqsBd7jZCNMWotjhi
+ vd5zovQlFqQ2Wjc2+y46vKP/iXxWIuQJuDiisyXF0Y8+5GTpAL
+ pHDc1/pIGmMIGjoAMCAQGigZsEgZg2on5mSuxoDHEA1w9bcW9n
+ FdFxDKpdrQhVGVRDIzcCMCTzvUboqb5KjY1NJKJsfjRQiBYBdE
+ NKfzK+g5DlV8nrw81uOcP8NOQCLR5XkoMHC0Dr/80ziQzbNqhx
+ O6652Npft0LQwJvenwDI13YxpwOdMXzkWZN/XrEqOWp6GCgXTB
+ vCyLWLlWnbaUkZdEYbKHBPjd8t/1x5Yg==
+ S: + YGgGCSqGSIb3EgECAgIAb1kwV6ADAgEFoQMCAQ+iSzBJoAMC
+ AQGiQgRAtHTEuOP2BXb9sBYFR4SJlDZxmg39IxmRBOhXRKdDA0
+ uHTCOT9Bq3OsUTXUlk0CsFLoa8j+gvGDlgHuqzWHPSQg==
+ C:
+ S: + YDMGCSqGSIb3EgECAgIBAAD/////6jcyG4GE3KkTzBeBiVHe
+ ceP2CWY0SR0fAQAgAAQEBAQ=
+ C: YDMGCSqGSIb3EgECAgIBAAD/////3LQBHXTpFfZgrejpLlLImP
+ wkhbfa2QteAQAgAG1yYwE=
+ S: A001 OK GSSAPI authentication successful
+
+ Note: The line breaks within server challenges and client
+ responses are for editorial clarity and are not in real
+ authenticators.
+
+
+6.2.3. LOGIN Command
+
+ Arguments: user name
+ password
+
+ Responses: no specific responses for this command
+
+ Result: OK - login completed, now in authenticated state
+ NO - login failure: user name or password rejected
+ BAD - command unknown or arguments invalid
+
+ The LOGIN command identifies the client to the server and carries
+ the plaintext password authenticating this user.
+
+
+
+
+
+
+Crispin Standards Track [Page 30]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ A server MAY include a CAPABILITY response code in the tagged OK
+ response to a successful LOGIN command in order to send
+ capabilities automatically. It is unnecessary for a client to
+ send a separate CAPABILITY command if it recognizes these
+ automatic capabilities.
+
+ Example: C: a001 LOGIN SMITH SESAME
+ S: a001 OK LOGIN completed
+
+ Note: Use of the LOGIN command over an insecure network
+ (such as the Internet) is a security risk, because anyone
+ monitoring network traffic can obtain plaintext passwords.
+ The LOGIN command SHOULD NOT be used except as a last
+ resort, and it is recommended that client implementations
+ have a means to disable any automatic use of the LOGIN
+ command.
+
+ Unless either the STARTTLS command has been negotiated or
+ some other mechanism that protects the session from
+ password snooping has been provided, a server
+ implementation MUST implement a configuration in which it
+ advertises the LOGINDISABLED capability and does NOT permit
+ the LOGIN command. Server sites SHOULD NOT use any
+ configuration which permits the LOGIN command without such
+ a protection mechanism against password snooping. A client
+ implementation MUST NOT send a LOGIN command if the
+ LOGINDISABLED capability is advertised.
+
+6.3. Client Commands - Authenticated State
+
+ In the authenticated state, commands that manipulate mailboxes as
+ atomic entities are permitted. Of these commands, the SELECT and
+ EXAMINE commands will select a mailbox for access and enter the
+ selected state.
+
+ In addition to the universal commands (CAPABILITY, NOOP, and LOGOUT),
+ the following commands are valid in the authenticated state: SELECT,
+ EXAMINE, CREATE, DELETE, RENAME, SUBSCRIBE, UNSUBSCRIBE, LIST, LSUB,
+ STATUS, and APPEND.
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 31]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.3.1. SELECT Command
+
+ Arguments: mailbox name
+
+ Responses: REQUIRED untagged responses: FLAGS, EXISTS, RECENT
+ REQUIRED OK untagged responses: UNSEEN, PERMANENTFLAGS,
+ UIDNEXT, UIDVALIDITY
+
+ Result: OK - select completed, now in selected state
+ NO - select failure, now in authenticated state: no
+ such mailbox, can't access mailbox
+ BAD - command unknown or arguments invalid
+
+ The SELECT command selects a mailbox so that messages in the
+ mailbox can be accessed. Before returning an OK to the client,
+ the server MUST send the following untagged data to the client.
+ Note that earlier versions of this protocol only required the
+ FLAGS, EXISTS, and RECENT untagged data; consequently, client
+ implementations SHOULD implement default behavior for missing data
+ as discussed with the individual item.
+
+ FLAGS Defined flags in the mailbox. See the description
+ of the FLAGS response for more detail.
+
+ <n> EXISTS The number of messages in the mailbox. See the
+ description of the EXISTS response for more detail.
+
+ <n> RECENT The number of messages with the \Recent flag set.
+ See the description of the RECENT response for more
+ detail.
+
+ OK [UNSEEN <n>]
+ The message sequence number of the first unseen
+ message in the mailbox. If this is missing, the
+ client can not make any assumptions about the first
+ unseen message in the mailbox, and needs to issue a
+ SEARCH command if it wants to find it.
+
+ OK [PERMANENTFLAGS (<list of flags>)]
+ A list of message flags that the client can change
+ permanently. If this is missing, the client should
+ assume that all flags can be changed permanently.
+
+ OK [UIDNEXT <n>]
+ The next unique identifier value. Refer to section
+ 2.3.1.1 for more information. If this is missing,
+ the client can not make any assumptions about the
+ next unique identifier value.
+
+
+
+Crispin Standards Track [Page 32]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ OK [UIDVALIDITY <n>]
+ The unique identifier validity value. Refer to
+ section 2.3.1.1 for more information. If this is
+ missing, the server does not support unique
+ identifiers.
+
+ Only one mailbox can be selected at a time in a connection;
+ simultaneous access to multiple mailboxes requires multiple
+ connections. The SELECT command automatically deselects any
+ currently selected mailbox before attempting the new selection.
+ Consequently, if a mailbox is selected and a SELECT command that
+ fails is attempted, no mailbox is selected.
+
+ If the client is permitted to modify the mailbox, the server
+ SHOULD prefix the text of the tagged OK response with the
+ "[READ-WRITE]" response code.
+
+ If the client is not permitted to modify the mailbox but is
+ permitted read access, the mailbox is selected as read-only, and
+ the server MUST prefix the text of the tagged OK response to
+ SELECT with the "[READ-ONLY]" response code. Read-only access
+ through SELECT differs from the EXAMINE command in that certain
+ read-only mailboxes MAY permit the change of permanent state on a
+ per-user (as opposed to global) basis. Netnews messages marked in
+ a server-based .newsrc file are an example of such per-user
+ permanent state that can be modified with read-only mailboxes.
+
+ Example: C: A142 SELECT INBOX
+ S: * 172 EXISTS
+ S: * 1 RECENT
+ S: * OK [UNSEEN 12] Message 12 is first unseen
+ S: * OK [UIDVALIDITY 3857529045] UIDs valid
+ S: * OK [UIDNEXT 4392] Predicted next UID
+ S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
+ S: * OK [PERMANENTFLAGS (\Deleted \Seen \*)] Limited
+ S: A142 OK [READ-WRITE] SELECT completed
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 33]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.3.2. EXAMINE Command
+
+ Arguments: mailbox name
+
+ Responses: REQUIRED untagged responses: FLAGS, EXISTS, RECENT
+ REQUIRED OK untagged responses: UNSEEN, PERMANENTFLAGS,
+ UIDNEXT, UIDVALIDITY
+
+ Result: OK - examine completed, now in selected state
+ NO - examine failure, now in authenticated state: no
+ such mailbox, can't access mailbox
+ BAD - command unknown or arguments invalid
+
+ The EXAMINE command is identical to SELECT and returns the same
+ output; however, the selected mailbox is identified as read-only.
+ No changes to the permanent state of the mailbox, including
+ per-user state, are permitted; in particular, EXAMINE MUST NOT
+ cause messages to lose the \Recent flag.
+
+ The text of the tagged OK response to the EXAMINE command MUST
+ begin with the "[READ-ONLY]" response code.
+
+ Example: C: A932 EXAMINE blurdybloop
+ S: * 17 EXISTS
+ S: * 2 RECENT
+ S: * OK [UNSEEN 8] Message 8 is first unseen
+ S: * OK [UIDVALIDITY 3857529045] UIDs valid
+ S: * OK [UIDNEXT 4392] Predicted next UID
+ S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
+ S: * OK [PERMANENTFLAGS ()] No permanent flags permitted
+ S: A932 OK [READ-ONLY] EXAMINE completed
+
+
+6.3.3. CREATE Command
+
+ Arguments: mailbox name
+
+ Responses: no specific responses for this command
+
+ Result: OK - create completed
+ NO - create failure: can't create mailbox with that name
+ BAD - command unknown or arguments invalid
+
+ The CREATE command creates a mailbox with the given name. An OK
+ response is returned only if a new mailbox with that name has been
+ created. It is an error to attempt to create INBOX or a mailbox
+ with a name that refers to an extant mailbox. Any error in
+ creation will return a tagged NO response.
+
+
+
+Crispin Standards Track [Page 34]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ If the mailbox name is suffixed with the server's hierarchy
+ separator character (as returned from the server by a LIST
+ command), this is a declaration that the client intends to create
+ mailbox names under this name in the hierarchy. Server
+ implementations that do not require this declaration MUST ignore
+ the declaration. In any case, the name created is without the
+ trailing hierarchy delimiter.
+
+ If the server's hierarchy separator character appears elsewhere in
+ the name, the server SHOULD create any superior hierarchical names
+ that are needed for the CREATE command to be successfully
+ completed. In other words, an attempt to create "foo/bar/zap" on
+ a server in which "/" is the hierarchy separator character SHOULD
+ create foo/ and foo/bar/ if they do not already exist.
+
+ If a new mailbox is created with the same name as a mailbox which
+ was deleted, its unique identifiers MUST be greater than any
+ unique identifiers used in the previous incarnation of the mailbox
+ UNLESS the new incarnation has a different unique identifier
+ validity value. See the description of the UID command for more
+ detail.
+
+ Example: C: A003 CREATE owatagusiam/
+ S: A003 OK CREATE completed
+ C: A004 CREATE owatagusiam/blurdybloop
+ S: A004 OK CREATE completed
+
+ Note: The interpretation of this example depends on whether
+ "/" was returned as the hierarchy separator from LIST. If
+ "/" is the hierarchy separator, a new level of hierarchy
+ named "owatagusiam" with a member called "blurdybloop" is
+ created. Otherwise, two mailboxes at the same hierarchy
+ level are created.
+
+
+6.3.4. DELETE Command
+
+ Arguments: mailbox name
+
+ Responses: no specific responses for this command
+
+ Result: OK - delete completed
+ NO - delete failure: can't delete mailbox with that name
+ BAD - command unknown or arguments invalid
+
+
+
+
+
+
+
+Crispin Standards Track [Page 35]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The DELETE command permanently removes the mailbox with the given
+ name. A tagged OK response is returned only if the mailbox has
+ been deleted. It is an error to attempt to delete INBOX or a
+ mailbox name that does not exist.
+
+ The DELETE command MUST NOT remove inferior hierarchical names.
+ For example, if a mailbox "foo" has an inferior "foo.bar"
+ (assuming "." is the hierarchy delimiter character), removing
+ "foo" MUST NOT remove "foo.bar". It is an error to attempt to
+ delete a name that has inferior hierarchical names and also has
+ the \Noselect mailbox name attribute (see the description of the
+ LIST response for more details).
+
+ It is permitted to delete a name that has inferior hierarchical
+ names and does not have the \Noselect mailbox name attribute. In
+ this case, all messages in that mailbox are removed, and the name
+ will acquire the \Noselect mailbox name attribute.
+
+ The value of the highest-used unique identifier of the deleted
+ mailbox MUST be preserved so that a new mailbox created with the
+ same name will not reuse the identifiers of the former
+ incarnation, UNLESS the new incarnation has a different unique
+ identifier validity value. See the description of the UID command
+ for more detail.
+
+ Examples: C: A682 LIST "" *
+ S: * LIST () "/" blurdybloop
+ S: * LIST (\Noselect) "/" foo
+ S: * LIST () "/" foo/bar
+ S: A682 OK LIST completed
+ C: A683 DELETE blurdybloop
+ S: A683 OK DELETE completed
+ C: A684 DELETE foo
+ S: A684 NO Name "foo" has inferior hierarchical names
+ C: A685 DELETE foo/bar
+ S: A685 OK DELETE Completed
+ C: A686 LIST "" *
+ S: * LIST (\Noselect) "/" foo
+ S: A686 OK LIST completed
+ C: A687 DELETE foo
+ S: A687 OK DELETE Completed
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 36]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ C: A82 LIST "" *
+ S: * LIST () "." blurdybloop
+ S: * LIST () "." foo
+ S: * LIST () "." foo.bar
+ S: A82 OK LIST completed
+ C: A83 DELETE blurdybloop
+ S: A83 OK DELETE completed
+ C: A84 DELETE foo
+ S: A84 OK DELETE Completed
+ C: A85 LIST "" *
+ S: * LIST () "." foo.bar
+ S: A85 OK LIST completed
+ C: A86 LIST "" %
+ S: * LIST (\Noselect) "." foo
+ S: A86 OK LIST completed
+
+
+6.3.5. RENAME Command
+
+ Arguments: existing mailbox name
+ new mailbox name
+
+ Responses: no specific responses for this command
+
+ Result: OK - rename completed
+ NO - rename failure: can't rename mailbox with that name,
+ can't rename to mailbox with that name
+ BAD - command unknown or arguments invalid
+
+ The RENAME command changes the name of a mailbox. A tagged OK
+ response is returned only if the mailbox has been renamed. It is
+ an error to attempt to rename from a mailbox name that does not
+ exist or to a mailbox name that already exists. Any error in
+ renaming will return a tagged NO response.
+
+ If the name has inferior hierarchical names, then the inferior
+ hierarchical names MUST also be renamed. For example, a rename of
+ "foo" to "zap" will rename "foo/bar" (assuming "/" is the
+ hierarchy delimiter character) to "zap/bar".
+
+ If the server's hierarchy separator character appears in the name,
+ the server SHOULD create any superior hierarchical names that are
+ needed for the RENAME command to complete successfully. In other
+ words, an attempt to rename "foo/bar/zap" to baz/rag/zowie on a
+ server in which "/" is the hierarchy separator character SHOULD
+ create baz/ and baz/rag/ if they do not already exist.
+
+
+
+
+
+Crispin Standards Track [Page 37]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The value of the highest-used unique identifier of the old mailbox
+ name MUST be preserved so that a new mailbox created with the same
+ name will not reuse the identifiers of the former incarnation,
+ UNLESS the new incarnation has a different unique identifier
+ validity value. See the description of the UID command for more
+ detail.
+
+ Renaming INBOX is permitted, and has special behavior. It moves
+ all messages in INBOX to a new mailbox with the given name,
+ leaving INBOX empty. If the server implementation supports
+ inferior hierarchical names of INBOX, these are unaffected by a
+ rename of INBOX.
+
+ Examples: C: A682 LIST "" *
+ S: * LIST () "/" blurdybloop
+ S: * LIST (\Noselect) "/" foo
+ S: * LIST () "/" foo/bar
+ S: A682 OK LIST completed
+ C: A683 RENAME blurdybloop sarasoop
+ S: A683 OK RENAME completed
+ C: A684 RENAME foo zowie
+ S: A684 OK RENAME Completed
+ C: A685 LIST "" *
+ S: * LIST () "/" sarasoop
+ S: * LIST (\Noselect) "/" zowie
+ S: * LIST () "/" zowie/bar
+ S: A685 OK LIST completed
+
+ C: Z432 LIST "" *
+ S: * LIST () "." INBOX
+ S: * LIST () "." INBOX.bar
+ S: Z432 OK LIST completed
+ C: Z433 RENAME INBOX old-mail
+ S: Z433 OK RENAME completed
+ C: Z434 LIST "" *
+ S: * LIST () "." INBOX
+ S: * LIST () "." INBOX.bar
+ S: * LIST () "." old-mail
+ S: Z434 OK LIST completed
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 38]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.3.6. SUBSCRIBE Command
+
+ Arguments: mailbox
+
+ Responses: no specific responses for this command
+
+ Result: OK - subscribe completed
+ NO - subscribe failure: can't subscribe to that name
+ BAD - command unknown or arguments invalid
+
+ The SUBSCRIBE command adds the specified mailbox name to the
+ server's set of "active" or "subscribed" mailboxes as returned by
+ the LSUB command. This command returns a tagged OK response only
+ if the subscription is successful.
+
+ A server MAY validate the mailbox argument to SUBSCRIBE to verify
+ that it exists. However, it MUST NOT unilaterally remove an
+ existing mailbox name from the subscription list even if a mailbox
+ by that name no longer exists.
+
+ Note: This requirement is because a server site can
+ choose to routinely remove a mailbox with a well-known
+ name (e.g., "system-alerts") after its contents expire,
+ with the intention of recreating it when new contents
+ are appropriate.
+
+
+ Example: C: A002 SUBSCRIBE #news.comp.mail.mime
+ S: A002 OK SUBSCRIBE completed
+
+
+6.3.7. UNSUBSCRIBE Command
+
+ Arguments: mailbox name
+
+ Responses: no specific responses for this command
+
+ Result: OK - unsubscribe completed
+ NO - unsubscribe failure: can't unsubscribe that name
+ BAD - command unknown or arguments invalid
+
+ The UNSUBSCRIBE command removes the specified mailbox name from
+ the server's set of "active" or "subscribed" mailboxes as returned
+ by the LSUB command. This command returns a tagged OK response
+ only if the unsubscription is successful.
+
+ Example: C: A002 UNSUBSCRIBE #news.comp.mail.mime
+ S: A002 OK UNSUBSCRIBE completed
+
+
+
+Crispin Standards Track [Page 39]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.3.8. LIST Command
+
+ Arguments: reference name
+ mailbox name with possible wildcards
+
+ Responses: untagged responses: LIST
+
+ Result: OK - list completed
+ NO - list failure: can't list that reference or name
+ BAD - command unknown or arguments invalid
+
+ The LIST command returns a subset of names from the complete set
+ of all names available to the client. Zero or more untagged LIST
+ replies are returned, containing the name attributes, hierarchy
+ delimiter, and name; see the description of the LIST reply for
+ more detail.
+
+ The LIST command SHOULD return its data quickly, without undue
+ delay. For example, it SHOULD NOT go to excess trouble to
+ calculate the \Marked or \Unmarked status or perform other
+ processing; if each name requires 1 second of processing, then a
+ list of 1200 names would take 20 minutes!
+
+ An empty ("" string) reference name argument indicates that the
+ mailbox name is interpreted as by SELECT. The returned mailbox
+ names MUST match the supplied mailbox name pattern. A non-empty
+ reference name argument is the name of a mailbox or a level of
+ mailbox hierarchy, and indicates the context in which the mailbox
+ name is interpreted.
+
+ An empty ("" string) mailbox name argument is a special request to
+ return the hierarchy delimiter and the root name of the name given
+ in the reference. The value returned as the root MAY be the empty
+ string if the reference is non-rooted or is an empty string. In
+ all cases, a hierarchy delimiter (or NIL if there is no hierarchy)
+ is returned. This permits a client to get the hierarchy delimiter
+ (or find out that the mailbox names are flat) even when no
+ mailboxes by that name currently exist.
+
+ The reference and mailbox name arguments are interpreted into a
+ canonical form that represents an unambiguous left-to-right
+ hierarchy. The returned mailbox names will be in the interpreted
+ form.
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 40]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Note: The interpretation of the reference argument is
+ implementation-defined. It depends upon whether the
+ server implementation has a concept of the "current
+ working directory" and leading "break out characters",
+ which override the current working directory.
+
+ For example, on a server which exports a UNIX or NT
+ filesystem, the reference argument contains the current
+ working directory, and the mailbox name argument would
+ contain the name as interpreted in the current working
+ directory.
+
+ If a server implementation has no concept of break out
+ characters, the canonical form is normally the reference
+ name appended with the mailbox name. Note that if the
+ server implements the namespace convention (section
+ 5.1.2), "#" is a break out character and must be treated
+ as such.
+
+ If the reference argument is not a level of mailbox
+ hierarchy (that is, it is a \NoInferiors name), and/or
+ the reference argument does not end with the hierarchy
+ delimiter, it is implementation-dependent how this is
+ interpreted. For example, a reference of "foo/bar" and
+ mailbox name of "rag/baz" could be interpreted as
+ "foo/bar/rag/baz", "foo/barrag/baz", or "foo/rag/baz".
+ A client SHOULD NOT use such a reference argument except
+ at the explicit request of the user. A hierarchical
+ browser MUST NOT make any assumptions about server
+ interpretation of the reference unless the reference is
+ a level of mailbox hierarchy AND ends with the hierarchy
+ delimiter.
+
+ Any part of the reference argument that is included in the
+ interpreted form SHOULD prefix the interpreted form. It SHOULD
+ also be in the same form as the reference name argument. This
+ rule permits the client to determine if the returned mailbox name
+ is in the context of the reference argument, or if something about
+ the mailbox argument overrode the reference argument. Without
+ this rule, the client would have to have knowledge of the server's
+ naming semantics including what characters are "breakouts" that
+ override a naming context.
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 41]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ For example, here are some examples of how references
+ and mailbox names might be interpreted on a UNIX-based
+ server:
+
+ Reference Mailbox Name Interpretation
+ ------------ ------------ --------------
+ ~smith/Mail/ foo.* ~smith/Mail/foo.*
+ archive/ % archive/%
+ #news. comp.mail.* #news.comp.mail.*
+ ~smith/Mail/ /usr/doc/foo /usr/doc/foo
+ archive/ ~fred/Mail/* ~fred/Mail/*
+
+ The first three examples demonstrate interpretations in
+ the context of the reference argument. Note that
+ "~smith/Mail" SHOULD NOT be transformed into something
+ like "/u2/users/smith/Mail", or it would be impossible
+ for the client to determine that the interpretation was
+ in the context of the reference.
+
+ The character "*" is a wildcard, and matches zero or more
+ characters at this position. The character "%" is similar to "*",
+ but it does not match a hierarchy delimiter. If the "%" wildcard
+ is the last character of a mailbox name argument, matching levels
+ of hierarchy are also returned. If these levels of hierarchy are
+ not also selectable mailboxes, they are returned with the
+ \Noselect mailbox name attribute (see the description of the LIST
+ response for more details).
+
+ Server implementations are permitted to "hide" otherwise
+ accessible mailboxes from the wildcard characters, by preventing
+ certain characters or names from matching a wildcard in certain
+ situations. For example, a UNIX-based server might restrict the
+ interpretation of "*" so that an initial "/" character does not
+ match.
+
+ The special name INBOX is included in the output from LIST, if
+ INBOX is supported by this server for this user and if the
+ uppercase string "INBOX" matches the interpreted reference and
+ mailbox name arguments with wildcards as described above. The
+ criteria for omitting INBOX is whether SELECT INBOX will return
+ failure; it is not relevant whether the user's real INBOX resides
+ on this or some other server.
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 42]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Example: C: A101 LIST "" ""
+ S: * LIST (\Noselect) "/" ""
+ S: A101 OK LIST Completed
+ C: A102 LIST #news.comp.mail.misc ""
+ S: * LIST (\Noselect) "." #news.
+ S: A102 OK LIST Completed
+ C: A103 LIST /usr/staff/jones ""
+ S: * LIST (\Noselect) "/" /
+ S: A103 OK LIST Completed
+ C: A202 LIST ~/Mail/ %
+ S: * LIST (\Noselect) "/" ~/Mail/foo
+ S: * LIST () "/" ~/Mail/meetings
+ S: A202 OK LIST completed
+
+
+6.3.9. LSUB Command
+
+ Arguments: reference name
+ mailbox name with possible wildcards
+
+ Responses: untagged responses: LSUB
+
+ Result: OK - lsub completed
+ NO - lsub failure: can't list that reference or name
+ BAD - command unknown or arguments invalid
+
+ The LSUB command returns a subset of names from the set of names
+ that the user has declared as being "active" or "subscribed".
+ Zero or more untagged LSUB replies are returned. The arguments to
+ LSUB are in the same form as those for LIST.
+
+ The returned untagged LSUB response MAY contain different mailbox
+ flags from a LIST untagged response. If this should happen, the
+ flags in the untagged LIST are considered more authoritative.
+
+ A special situation occurs when using LSUB with the % wildcard.
+ Consider what happens if "foo/bar" (with a hierarchy delimiter of
+ "/") is subscribed but "foo" is not. A "%" wildcard to LSUB must
+ return foo, not foo/bar, in the LSUB response, and it MUST be
+ flagged with the \Noselect attribute.
+
+ The server MUST NOT unilaterally remove an existing mailbox name
+ from the subscription list even if a mailbox by that name no
+ longer exists.
+
+
+
+
+
+
+
+Crispin Standards Track [Page 43]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Example: C: A002 LSUB "#news." "comp.mail.*"
+ S: * LSUB () "." #news.comp.mail.mime
+ S: * LSUB () "." #news.comp.mail.misc
+ S: A002 OK LSUB completed
+ C: A003 LSUB "#news." "comp.%"
+ S: * LSUB (\NoSelect) "." #news.comp.mail
+ S: A003 OK LSUB completed
+
+
+6.3.10. STATUS Command
+
+ Arguments: mailbox name
+ status data item names
+
+ Responses: untagged responses: STATUS
+
+ Result: OK - status completed
+ NO - status failure: no status for that name
+ BAD - command unknown or arguments invalid
+
+ The STATUS command requests the status of the indicated mailbox.
+ It does not change the currently selected mailbox, nor does it
+ affect the state of any messages in the queried mailbox (in
+ particular, STATUS MUST NOT cause messages to lose the \Recent
+ flag).
+
+ The STATUS command provides an alternative to opening a second
+ IMAP4rev1 connection and doing an EXAMINE command on a mailbox to
+ query that mailbox's status without deselecting the current
+ mailbox in the first IMAP4rev1 connection.
+
+ Unlike the LIST command, the STATUS command is not guaranteed to
+ be fast in its response. Under certain circumstances, it can be
+ quite slow. In some implementations, the server is obliged to
+ open the mailbox read-only internally to obtain certain status
+ information. Also unlike the LIST command, the STATUS command
+ does not accept wildcards.
+
+ Note: The STATUS command is intended to access the
+ status of mailboxes other than the currently selected
+ mailbox. Because the STATUS command can cause the
+ mailbox to be opened internally, and because this
+ information is available by other means on the selected
+ mailbox, the STATUS command SHOULD NOT be used on the
+ currently selected mailbox.
+
+
+
+
+
+
+Crispin Standards Track [Page 44]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The STATUS command MUST NOT be used as a "check for new
+ messages in the selected mailbox" operation (refer to
+ sections 7, 7.3.1, and 7.3.2 for more information about
+ the proper method for new message checking).
+
+ Because the STATUS command is not guaranteed to be fast
+ in its results, clients SHOULD NOT expect to be able to
+ issue many consecutive STATUS commands and obtain
+ reasonable performance.
+
+ The currently defined status data items that can be requested are:
+
+ MESSAGES
+ The number of messages in the mailbox.
+
+ RECENT
+ The number of messages with the \Recent flag set.
+
+ UIDNEXT
+ The next unique identifier value of the mailbox. Refer to
+ section 2.3.1.1 for more information.
+
+ UIDVALIDITY
+ The unique identifier validity value of the mailbox. Refer to
+ section 2.3.1.1 for more information.
+
+ UNSEEN
+ The number of messages which do not have the \Seen flag set.
+
+
+ Example: C: A042 STATUS blurdybloop (UIDNEXT MESSAGES)
+ S: * STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292)
+ S: A042 OK STATUS completed
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 45]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.3.11. APPEND Command
+
+ Arguments: mailbox name
+ OPTIONAL flag parenthesized list
+ OPTIONAL date/time string
+ message literal
+
+ Responses: no specific responses for this command
+
+ Result: OK - append completed
+ NO - append error: can't append to that mailbox, error
+ in flags or date/time or message text
+ BAD - command unknown or arguments invalid
+
+ The APPEND command appends the literal argument as a new message
+ to the end of the specified destination mailbox. This argument
+ SHOULD be in the format of an [RFC-2822] message. 8-bit
+ characters are permitted in the message. A server implementation
+ that is unable to preserve 8-bit data properly MUST be able to
+ reversibly convert 8-bit APPEND data to 7-bit using a [MIME-IMB]
+ content transfer encoding.
+
+ Note: There MAY be exceptions, e.g., draft messages, in
+ which required [RFC-2822] header lines are omitted in
+ the message literal argument to APPEND. The full
+ implications of doing so MUST be understood and
+ carefully weighed.
+
+ If a flag parenthesized list is specified, the flags SHOULD be set
+ in the resulting message; otherwise, the flag list of the
+ resulting message is set to empty by default. In either case, the
+ Recent flag is also set.
+
+ If a date-time is specified, the internal date SHOULD be set in
+ the resulting message; otherwise, the internal date of the
+ resulting message is set to the current date and time by default.
+
+ If the append is unsuccessful for any reason, the mailbox MUST be
+ restored to its state before the APPEND attempt; no partial
+ appending is permitted.
+
+ If the destination mailbox does not exist, a server MUST return an
+ error, and MUST NOT automatically create the mailbox. Unless it
+ is certain that the destination mailbox can not be created, the
+ server MUST send the response code "[TRYCREATE]" as the prefix of
+ the text of the tagged NO response. This gives a hint to the
+ client that it can attempt a CREATE command and retry the APPEND
+ if the CREATE is successful.
+
+
+
+Crispin Standards Track [Page 46]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ If the mailbox is currently selected, the normal new message
+ actions SHOULD occur. Specifically, the server SHOULD notify the
+ client immediately via an untagged EXISTS response. If the server
+ does not do so, the client MAY issue a NOOP command (or failing
+ that, a CHECK command) after one or more APPEND commands.
+
+ Example: C: A003 APPEND saved-messages (\Seen) {310}
+ S: + Ready for literal data
+ C: Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
+ C: From: Fred Foobar <foobar@Blurdybloop.COM>
+ C: Subject: afternoon meeting
+ C: To: mooch@owatagu.siam.edu
+ C: Message-Id: <B27397-0100000@Blurdybloop.COM>
+ C: MIME-Version: 1.0
+ C: Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
+ C:
+ C: Hello Joe, do you think we can meet at 3:30 tomorrow?
+ C:
+ S: A003 OK APPEND completed
+
+ Note: The APPEND command is not used for message delivery,
+ because it does not provide a mechanism to transfer [SMTP]
+ envelope information.
+
+6.4. Client Commands - Selected State
+
+ In the selected state, commands that manipulate messages in a mailbox
+ are permitted.
+
+ In addition to the universal commands (CAPABILITY, NOOP, and LOGOUT),
+ and the authenticated state commands (SELECT, EXAMINE, CREATE,
+ DELETE, RENAME, SUBSCRIBE, UNSUBSCRIBE, LIST, LSUB, STATUS, and
+ APPEND), the following commands are valid in the selected state:
+ CHECK, CLOSE, EXPUNGE, SEARCH, FETCH, STORE, COPY, and UID.
+
+6.4.1. CHECK Command
+
+ Arguments: none
+
+ Responses: no specific responses for this command
+
+ Result: OK - check completed
+ BAD - command unknown or arguments invalid
+
+ The CHECK command requests a checkpoint of the currently selected
+ mailbox. A checkpoint refers to any implementation-dependent
+ housekeeping associated with the mailbox (e.g., resolving the
+ server's in-memory state of the mailbox with the state on its
+
+
+
+Crispin Standards Track [Page 47]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ disk) that is not normally executed as part of each command. A
+ checkpoint MAY take a non-instantaneous amount of real time to
+ complete. If a server implementation has no such housekeeping
+ considerations, CHECK is equivalent to NOOP.
+
+ There is no guarantee that an EXISTS untagged response will happen
+ as a result of CHECK. NOOP, not CHECK, SHOULD be used for new
+ message polling.
+
+ Example: C: FXXZ CHECK
+ S: FXXZ OK CHECK Completed
+
+
+6.4.2. CLOSE Command
+
+ Arguments: none
+
+ Responses: no specific responses for this command
+
+ Result: OK - close completed, now in authenticated state
+ BAD - command unknown or arguments invalid
+
+ The CLOSE command permanently removes all messages that have the
+ \Deleted flag set from the currently selected mailbox, and returns
+ to the authenticated state from the selected state. No untagged
+ EXPUNGE responses are sent.
+
+ No messages are removed, and no error is given, if the mailbox is
+ selected by an EXAMINE command or is otherwise selected read-only.
+
+ Even if a mailbox is selected, a SELECT, EXAMINE, or LOGOUT
+ command MAY be issued without previously issuing a CLOSE command.
+ The SELECT, EXAMINE, and LOGOUT commands implicitly close the
+ currently selected mailbox without doing an expunge. However,
+ when many messages are deleted, a CLOSE-LOGOUT or CLOSE-SELECT
+ sequence is considerably faster than an EXPUNGE-LOGOUT or
+ EXPUNGE-SELECT because no untagged EXPUNGE responses (which the
+ client would probably ignore) are sent.
+
+ Example: C: A341 CLOSE
+ S: A341 OK CLOSE completed
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 48]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.4.3. EXPUNGE Command
+
+ Arguments: none
+
+ Responses: untagged responses: EXPUNGE
+
+ Result: OK - expunge completed
+ NO - expunge failure: can't expunge (e.g., permission
+ denied)
+ BAD - command unknown or arguments invalid
+
+ The EXPUNGE command permanently removes all messages that have the
+ \Deleted flag set from the currently selected mailbox. Before
+ returning an OK to the client, an untagged EXPUNGE response is
+ sent for each message that is removed.
+
+ Example: C: A202 EXPUNGE
+ S: * 3 EXPUNGE
+ S: * 3 EXPUNGE
+ S: * 5 EXPUNGE
+ S: * 8 EXPUNGE
+ S: A202 OK EXPUNGE completed
+
+ Note: In this example, messages 3, 4, 7, and 11 had the
+ \Deleted flag set. See the description of the EXPUNGE
+ response for further explanation.
+
+
+6.4.4. SEARCH Command
+
+ Arguments: OPTIONAL [CHARSET] specification
+ searching criteria (one or more)
+
+ Responses: REQUIRED untagged response: SEARCH
+
+ Result: OK - search completed
+ NO - search error: can't search that [CHARSET] or
+ criteria
+ BAD - command unknown or arguments invalid
+
+ The SEARCH command searches the mailbox for messages that match
+ the given searching criteria. Searching criteria consist of one
+ or more search keys. The untagged SEARCH response from the server
+ contains a listing of message sequence numbers corresponding to
+ those messages that match the searching criteria.
+
+
+
+
+
+
+Crispin Standards Track [Page 49]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ When multiple keys are specified, the result is the intersection
+ (AND function) of all the messages that match those keys. For
+ example, the criteria DELETED FROM "SMITH" SINCE 1-Feb-1994 refers
+ to all deleted messages from Smith that were placed in the mailbox
+ since February 1, 1994. A search key can also be a parenthesized
+ list of one or more search keys (e.g., for use with the OR and NOT
+ keys).
+
+ Server implementations MAY exclude [MIME-IMB] body parts with
+ terminal content media types other than TEXT and MESSAGE from
+ consideration in SEARCH matching.
+
+ The OPTIONAL [CHARSET] specification consists of the word
+ "CHARSET" followed by a registered [CHARSET]. It indicates the
+ [CHARSET] of the strings that appear in the search criteria.
+ [MIME-IMB] content transfer encodings, and [MIME-HDRS] strings in
+ [RFC-2822]/[MIME-IMB] headers, MUST be decoded before comparing
+ text in a [CHARSET] other than US-ASCII. US-ASCII MUST be
+ supported; other [CHARSET]s MAY be supported.
+
+ If the server does not support the specified [CHARSET], it MUST
+ return a tagged NO response (not a BAD). This response SHOULD
+ contain the BADCHARSET response code, which MAY list the
+ [CHARSET]s supported by the server.
+
+ In all search keys that use strings, a message matches the key if
+ the string is a substring of the field. The matching is
+ case-insensitive.
+
+ The defined search keys are as follows. Refer to the Formal
+ Syntax section for the precise syntactic definitions of the
+ arguments.
+
+ <sequence set>
+ Messages with message sequence numbers corresponding to the
+ specified message sequence number set.
+
+ ALL
+ All messages in the mailbox; the default initial key for
+ ANDing.
+
+ ANSWERED
+ Messages with the \Answered flag set.
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 50]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ BCC <string>
+ Messages that contain the specified string in the envelope
+ structure's BCC field.
+
+ BEFORE <date>
+ Messages whose internal date (disregarding time and timezone)
+ is earlier than the specified date.
+
+ BODY <string>
+ Messages that contain the specified string in the body of the
+ message.
+
+ CC <string>
+ Messages that contain the specified string in the envelope
+ structure's CC field.
+
+ DELETED
+ Messages with the \Deleted flag set.
+
+ DRAFT
+ Messages with the \Draft flag set.
+
+ FLAGGED
+ Messages with the \Flagged flag set.
+
+ FROM <string>
+ Messages that contain the specified string in the envelope
+ structure's FROM field.
+
+ HEADER <field-name> <string>
+ Messages that have a header with the specified field-name (as
+ defined in [RFC-2822]) and that contains the specified string
+ in the text of the header (what comes after the colon). If the
+ string to search is zero-length, this matches all messages that
+ have a header line with the specified field-name regardless of
+ the contents.
+
+ KEYWORD <flag>
+ Messages with the specified keyword flag set.
+
+ LARGER <n>
+ Messages with an [RFC-2822] size larger than the specified
+ number of octets.
+
+ NEW
+ Messages that have the \Recent flag set but not the \Seen flag.
+ This is functionally equivalent to "(RECENT UNSEEN)".
+
+
+
+
+Crispin Standards Track [Page 51]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ NOT <search-key>
+ Messages that do not match the specified search key.
+
+ OLD
+ Messages that do not have the \Recent flag set. This is
+ functionally equivalent to "NOT RECENT" (as opposed to "NOT
+ NEW").
+
+ ON <date>
+ Messages whose internal date (disregarding time and timezone)
+ is within the specified date.
+
+ OR <search-key1> <search-key2>
+ Messages that match either search key.
+
+ RECENT
+ Messages that have the \Recent flag set.
+
+ SEEN
+ Messages that have the \Seen flag set.
+
+ SENTBEFORE <date>
+ Messages whose [RFC-2822] Date: header (disregarding time and
+ timezone) is earlier than the specified date.
+
+ SENTON <date>
+ Messages whose [RFC-2822] Date: header (disregarding time and
+ timezone) is within the specified date.
+
+ SENTSINCE <date>
+ Messages whose [RFC-2822] Date: header (disregarding time and
+ timezone) is within or later than the specified date.
+
+ SINCE <date>
+ Messages whose internal date (disregarding time and timezone)
+ is within or later than the specified date.
+
+ SMALLER <n>
+ Messages with an [RFC-2822] size smaller than the specified
+ number of octets.
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 52]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ SUBJECT <string>
+ Messages that contain the specified string in the envelope
+ structure's SUBJECT field.
+
+ TEXT <string>
+ Messages that contain the specified string in the header or
+ body of the message.
+
+ TO <string>
+ Messages that contain the specified string in the envelope
+ structure's TO field.
+
+ UID <sequence set>
+ Messages with unique identifiers corresponding to the specified
+ unique identifier set. Sequence set ranges are permitted.
+
+ UNANSWERED
+ Messages that do not have the \Answered flag set.
+
+ UNDELETED
+ Messages that do not have the \Deleted flag set.
+
+ UNDRAFT
+ Messages that do not have the \Draft flag set.
+
+ UNFLAGGED
+ Messages that do not have the \Flagged flag set.
+
+ UNKEYWORD <flag>
+ Messages that do not have the specified keyword flag set.
+
+ UNSEEN
+ Messages that do not have the \Seen flag set.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 53]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Example: C: A282 SEARCH FLAGGED SINCE 1-Feb-1994 NOT FROM "Smith"
+ S: * SEARCH 2 84 882
+ S: A282 OK SEARCH completed
+ C: A283 SEARCH TEXT "string not in mailbox"
+ S: * SEARCH
+ S: A283 OK SEARCH completed
+ C: A284 SEARCH CHARSET UTF-8 TEXT {6}
+ C: XXXXXX
+ S: * SEARCH 43
+ S: A284 OK SEARCH completed
+
+ Note: Since this document is restricted to 7-bit ASCII
+ text, it is not possible to show actual UTF-8 data. The
+ "XXXXXX" is a placeholder for what would be 6 octets of
+ 8-bit data in an actual transaction.
+
+
+6.4.5. FETCH Command
+
+ Arguments: sequence set
+ message data item names or macro
+
+ Responses: untagged responses: FETCH
+
+ Result: OK - fetch completed
+ NO - fetch error: can't fetch that data
+ BAD - command unknown or arguments invalid
+
+ The FETCH command retrieves data associated with a message in the
+ mailbox. The data items to be fetched can be either a single atom
+ or a parenthesized list.
+
+ Most data items, identified in the formal syntax under the
+ msg-att-static rule, are static and MUST NOT change for any
+ particular message. Other data items, identified in the formal
+ syntax under the msg-att-dynamic rule, MAY change, either as a
+ result of a STORE command or due to external events.
+
+ For example, if a client receives an ENVELOPE for a
+ message when it already knows the envelope, it can
+ safely ignore the newly transmitted envelope.
+
+ There are three macros which specify commonly-used sets of data
+ items, and can be used instead of data items. A macro must be
+ used by itself, and not in conjunction with other macros or data
+ items.
+
+
+
+
+
+Crispin Standards Track [Page 54]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ ALL
+ Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE)
+
+ FAST
+ Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE)
+
+ FULL
+ Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE
+ BODY)
+
+ The currently defined data items that can be fetched are:
+
+ BODY
+ Non-extensible form of BODYSTRUCTURE.
+
+ BODY[<section>]<<partial>>
+ The text of a particular body section. The section
+ specification is a set of zero or more part specifiers
+ delimited by periods. A part specifier is either a part number
+ or one of the following: HEADER, HEADER.FIELDS,
+ HEADER.FIELDS.NOT, MIME, and TEXT. An empty section
+ specification refers to the entire message, including the
+ header.
+
+ Every message has at least one part number. Non-[MIME-IMB]
+ messages, and non-multipart [MIME-IMB] messages with no
+ encapsulated message, only have a part 1.
+
+ Multipart messages are assigned consecutive part numbers, as
+ they occur in the message. If a particular part is of type
+ message or multipart, its parts MUST be indicated by a period
+ followed by the part number within that nested multipart part.
+
+ A part of type MESSAGE/RFC822 also has nested part numbers,
+ referring to parts of the MESSAGE part's body.
+
+ The HEADER, HEADER.FIELDS, HEADER.FIELDS.NOT, and TEXT part
+ specifiers can be the sole part specifier or can be prefixed by
+ one or more numeric part specifiers, provided that the numeric
+ part specifier refers to a part of type MESSAGE/RFC822. The
+ MIME part specifier MUST be prefixed by one or more numeric
+ part specifiers.
+
+ The HEADER, HEADER.FIELDS, and HEADER.FIELDS.NOT part
+ specifiers refer to the [RFC-2822] header of the message or of
+ an encapsulated [MIME-IMT] MESSAGE/RFC822 message.
+ HEADER.FIELDS and HEADER.FIELDS.NOT are followed by a list of
+ field-name (as defined in [RFC-2822]) names, and return a
+
+
+
+Crispin Standards Track [Page 55]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ subset of the header. The subset returned by HEADER.FIELDS
+ contains only those header fields with a field-name that
+ matches one of the names in the list; similarly, the subset
+ returned by HEADER.FIELDS.NOT contains only the header fields
+ with a non-matching field-name. The field-matching is
+ case-insensitive but otherwise exact. Subsetting does not
+ exclude the [RFC-2822] delimiting blank line between the header
+ and the body; the blank line is included in all header fetches,
+ except in the case of a message which has no body and no blank
+ line.
+
+ The MIME part specifier refers to the [MIME-IMB] header for
+ this part.
+
+ The TEXT part specifier refers to the text body of the message,
+ omitting the [RFC-2822] header.
+
+ Here is an example of a complex message with some of its
+ part specifiers:
+
+ HEADER ([RFC-2822] header of the message)
+ TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED
+ 1 TEXT/PLAIN
+ 2 APPLICATION/OCTET-STREAM
+ 3 MESSAGE/RFC822
+ 3.HEADER ([RFC-2822] header of the message)
+ 3.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED
+ 3.1 TEXT/PLAIN
+ 3.2 APPLICATION/OCTET-STREAM
+ 4 MULTIPART/MIXED
+ 4.1 IMAGE/GIF
+ 4.1.MIME ([MIME-IMB] header for the IMAGE/GIF)
+ 4.2 MESSAGE/RFC822
+ 4.2.HEADER ([RFC-2822] header of the message)
+ 4.2.TEXT ([RFC-2822] text body of the message) MULTIPART/MIXED
+ 4.2.1 TEXT/PLAIN
+ 4.2.2 MULTIPART/ALTERNATIVE
+ 4.2.2.1 TEXT/PLAIN
+ 4.2.2.2 TEXT/RICHTEXT
+
+
+ It is possible to fetch a substring of the designated text.
+ This is done by appending an open angle bracket ("<"), the
+ octet position of the first desired octet, a period, the
+ maximum number of octets desired, and a close angle bracket
+ (">") to the part specifier. If the starting octet is beyond
+ the end of the text, an empty string is returned.
+
+
+
+
+Crispin Standards Track [Page 56]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Any partial fetch that attempts to read beyond the end of the
+ text is truncated as appropriate. A partial fetch that starts
+ at octet 0 is returned as a partial fetch, even if this
+ truncation happened.
+
+ Note: This means that BODY[]<0.2048> of a 1500-octet message
+ will return BODY[]<0> with a literal of size 1500, not
+ BODY[].
+
+ Note: A substring fetch of a HEADER.FIELDS or
+ HEADER.FIELDS.NOT part specifier is calculated after
+ subsetting the header.
+
+ The \Seen flag is implicitly set; if this causes the flags to
+ change, they SHOULD be included as part of the FETCH responses.
+
+ BODY.PEEK[<section>]<<partial>>
+ An alternate form of BODY[<section>] that does not implicitly
+ set the \Seen flag.
+
+ BODYSTRUCTURE
+ The [MIME-IMB] body structure of the message. This is computed
+ by the server by parsing the [MIME-IMB] header fields in the
+ [RFC-2822] header and [MIME-IMB] headers.
+
+ ENVELOPE
+ The envelope structure of the message. This is computed by the
+ server by parsing the [RFC-2822] header into the component
+ parts, defaulting various fields as necessary.
+
+ FLAGS
+ The flags that are set for this message.
+
+ INTERNALDATE
+ The internal date of the message.
+
+ RFC822
+ Functionally equivalent to BODY[], differing in the syntax of
+ the resulting untagged FETCH data (RFC822 is returned).
+
+ RFC822.HEADER
+ Functionally equivalent to BODY.PEEK[HEADER], differing in the
+ syntax of the resulting untagged FETCH data (RFC822.HEADER is
+ returned).
+
+ RFC822.SIZE
+ The [RFC-2822] size of the message.
+
+
+
+
+Crispin Standards Track [Page 57]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ RFC822.TEXT
+ Functionally equivalent to BODY[TEXT], differing in the syntax
+ of the resulting untagged FETCH data (RFC822.TEXT is returned).
+
+ UID
+ The unique identifier for the message.
+
+
+ Example: C: A654 FETCH 2:4 (FLAGS BODY[HEADER.FIELDS (DATE FROM)])
+ S: * 2 FETCH ....
+ S: * 3 FETCH ....
+ S: * 4 FETCH ....
+ S: A654 OK FETCH completed
+
+
+6.4.6. STORE Command
+
+ Arguments: sequence set
+ message data item name
+ value for message data item
+
+ Responses: untagged responses: FETCH
+
+ Result: OK - store completed
+ NO - store error: can't store that data
+ BAD - command unknown or arguments invalid
+
+ The STORE command alters data associated with a message in the
+ mailbox. Normally, STORE will return the updated value of the
+ data with an untagged FETCH response. A suffix of ".SILENT" in
+ the data item name prevents the untagged FETCH, and the server
+ SHOULD assume that the client has determined the updated value
+ itself or does not care about the updated value.
+
+ Note: Regardless of whether or not the ".SILENT" suffix
+ was used, the server SHOULD send an untagged FETCH
+ response if a change to a message's flags from an
+ external source is observed. The intent is that the
+ status of the flags is determinate without a race
+ condition.
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 58]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The currently defined data items that can be stored are:
+
+ FLAGS <flag list>
+ Replace the flags for the message (other than \Recent) with the
+ argument. The new value of the flags is returned as if a FETCH
+ of those flags was done.
+
+ FLAGS.SILENT <flag list>
+ Equivalent to FLAGS, but without returning a new value.
+
+ +FLAGS <flag list>
+ Add the argument to the flags for the message. The new value
+ of the flags is returned as if a FETCH of those flags was done.
+
+ +FLAGS.SILENT <flag list>
+ Equivalent to +FLAGS, but without returning a new value.
+
+ -FLAGS <flag list>
+ Remove the argument from the flags for the message. The new
+ value of the flags is returned as if a FETCH of those flags was
+ done.
+
+ -FLAGS.SILENT <flag list>
+ Equivalent to -FLAGS, but without returning a new value.
+
+
+ Example: C: A003 STORE 2:4 +FLAGS (\Deleted)
+ S: * 2 FETCH (FLAGS (\Deleted \Seen))
+ S: * 3 FETCH (FLAGS (\Deleted))
+ S: * 4 FETCH (FLAGS (\Deleted \Flagged \Seen))
+ S: A003 OK STORE completed
+
+
+6.4.7. COPY Command
+
+ Arguments: sequence set
+ mailbox name
+
+ Responses: no specific responses for this command
+
+ Result: OK - copy completed
+ NO - copy error: can't copy those messages or to that
+ name
+ BAD - command unknown or arguments invalid
+
+
+
+
+
+
+
+Crispin Standards Track [Page 59]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The COPY command copies the specified message(s) to the end of the
+ specified destination mailbox. The flags and internal date of the
+ message(s) SHOULD be preserved, and the Recent flag SHOULD be set,
+ in the copy.
+
+ If the destination mailbox does not exist, a server SHOULD return
+ an error. It SHOULD NOT automatically create the mailbox. Unless
+ it is certain that the destination mailbox can not be created, the
+ server MUST send the response code "[TRYCREATE]" as the prefix of
+ the text of the tagged NO response. This gives a hint to the
+ client that it can attempt a CREATE command and retry the COPY if
+ the CREATE is successful.
+
+ If the COPY command is unsuccessful for any reason, server
+ implementations MUST restore the destination mailbox to its state
+ before the COPY attempt.
+
+ Example: C: A003 COPY 2:4 MEETING
+ S: A003 OK COPY completed
+
+
+6.4.8. UID Command
+
+ Arguments: command name
+ command arguments
+
+ Responses: untagged responses: FETCH, SEARCH
+
+ Result: OK - UID command completed
+ NO - UID command error
+ BAD - command unknown or arguments invalid
+
+ The UID command has two forms. In the first form, it takes as its
+ arguments a COPY, FETCH, or STORE command with arguments
+ appropriate for the associated command. However, the numbers in
+ the sequence set argument are unique identifiers instead of
+ message sequence numbers. Sequence set ranges are permitted, but
+ there is no guarantee that unique identifiers will be contiguous.
+
+ A non-existent unique identifier is ignored without any error
+ message generated. Thus, it is possible for a UID FETCH command
+ to return an OK without any data or a UID COPY or UID STORE to
+ return an OK without performing any operations.
+
+ In the second form, the UID command takes a SEARCH command with
+ SEARCH command arguments. The interpretation of the arguments is
+ the same as with SEARCH; however, the numbers returned in a SEARCH
+ response for a UID SEARCH command are unique identifiers instead
+
+
+
+Crispin Standards Track [Page 60]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ of message sequence numbers. For example, the command UID SEARCH
+ 1:100 UID 443:557 returns the unique identifiers corresponding to
+ the intersection of two sequence sets, the message sequence number
+ range 1:100 and the UID range 443:557.
+
+ Note: in the above example, the UID range 443:557
+ appears. The same comment about a non-existent unique
+ identifier being ignored without any error message also
+ applies here. Hence, even if neither UID 443 or 557
+ exist, this range is valid and would include an existing
+ UID 495.
+
+ Also note that a UID range of 559:* always includes the
+ UID of the last message in the mailbox, even if 559 is
+ higher than any assigned UID value. This is because the
+ contents of a range are independent of the order of the
+ range endpoints. Thus, any UID range with * as one of
+ the endpoints indicates at least one message (the
+ message with the highest numbered UID), unless the
+ mailbox is empty.
+
+ The number after the "*" in an untagged FETCH response is always a
+ message sequence number, not a unique identifier, even for a UID
+ command response. However, server implementations MUST implicitly
+ include the UID message data item as part of any FETCH response
+ caused by a UID command, regardless of whether a UID was specified
+ as a message data item to the FETCH.
+
+
+ Note: The rule about including the UID message data item as part
+ of a FETCH response primarily applies to the UID FETCH and UID
+ STORE commands, including a UID FETCH command that does not
+ include UID as a message data item. Although it is unlikely that
+ the other UID commands will cause an untagged FETCH, this rule
+ applies to these commands as well.
+
+ Example: C: A999 UID FETCH 4827313:4828442 FLAGS
+ S: * 23 FETCH (FLAGS (\Seen) UID 4827313)
+ S: * 24 FETCH (FLAGS (\Seen) UID 4827943)
+ S: * 25 FETCH (FLAGS (\Seen) UID 4828442)
+ S: A999 OK UID FETCH completed
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 61]
+
+RFC 3501 IMAPv4 March 2003
+
+
+6.5. Client Commands - Experimental/Expansion
+
+
+6.5.1. X<atom> Command
+
+ Arguments: implementation defined
+
+ Responses: implementation defined
+
+ Result: OK - command completed
+ NO - failure
+ BAD - command unknown or arguments invalid
+
+ Any command prefixed with an X is an experimental command.
+ Commands which are not part of this specification, a standard or
+ standards-track revision of this specification, or an
+ IESG-approved experimental protocol, MUST use the X prefix.
+
+ Any added untagged responses issued by an experimental command
+ MUST also be prefixed with an X. Server implementations MUST NOT
+ send any such untagged responses, unless the client requested it
+ by issuing the associated experimental command.
+
+ Example: C: a441 CAPABILITY
+ S: * CAPABILITY IMAP4rev1 XPIG-LATIN
+ S: a441 OK CAPABILITY completed
+ C: A442 XPIG-LATIN
+ S: * XPIG-LATIN ow-nay eaking-spay ig-pay atin-lay
+ S: A442 OK XPIG-LATIN ompleted-cay
+
+7. Server Responses
+
+ Server responses are in three forms: status responses, server data,
+ and command continuation request. The information contained in a
+ server response, identified by "Contents:" in the response
+ descriptions below, is described by function, not by syntax. The
+ precise syntax of server responses is described in the Formal Syntax
+ section.
+
+ The client MUST be prepared to accept any response at all times.
+
+ Status responses can be tagged or untagged. Tagged status responses
+ indicate the completion result (OK, NO, or BAD status) of a client
+ command, and have a tag matching the command.
+
+ Some status responses, and all server data, are untagged. An
+ untagged response is indicated by the token "*" instead of a tag.
+ Untagged status responses indicate server greeting, or server status
+
+
+
+Crispin Standards Track [Page 62]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ that does not indicate the completion of a command (for example, an
+ impending system shutdown alert). For historical reasons, untagged
+ server data responses are also called "unsolicited data", although
+ strictly speaking, only unilateral server data is truly
+ "unsolicited".
+
+ Certain server data MUST be recorded by the client when it is
+ received; this is noted in the description of that data. Such data
+ conveys critical information which affects the interpretation of all
+ subsequent commands and responses (e.g., updates reflecting the
+ creation or destruction of messages).
+
+ Other server data SHOULD be recorded for later reference; if the
+ client does not need to record the data, or if recording the data has
+ no obvious purpose (e.g., a SEARCH response when no SEARCH command is
+ in progress), the data SHOULD be ignored.
+
+ An example of unilateral untagged server data occurs when the IMAP
+ connection is in the selected state. In the selected state, the
+ server checks the mailbox for new messages as part of command
+ execution. Normally, this is part of the execution of every command;
+ hence, a NOOP command suffices to check for new messages. If new
+ messages are found, the server sends untagged EXISTS and RECENT
+ responses reflecting the new size of the mailbox. Server
+ implementations that offer multiple simultaneous access to the same
+ mailbox SHOULD also send appropriate unilateral untagged FETCH and
+ EXPUNGE responses if another agent changes the state of any message
+ flags or expunges any messages.
+
+ Command continuation request responses use the token "+" instead of a
+ tag. These responses are sent by the server to indicate acceptance
+ of an incomplete client command and readiness for the remainder of
+ the command.
+
+7.1. Server Responses - Status Responses
+
+ Status responses are OK, NO, BAD, PREAUTH and BYE. OK, NO, and BAD
+ can be tagged or untagged. PREAUTH and BYE are always untagged.
+
+ Status responses MAY include an OPTIONAL "response code". A response
+ code consists of data inside square brackets in the form of an atom,
+ possibly followed by a space and arguments. The response code
+ contains additional information or status codes for client software
+ beyond the OK/NO/BAD condition, and are defined when there is a
+ specific action that a client can take based upon the additional
+ information.
+
+
+
+
+
+Crispin Standards Track [Page 63]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The currently defined response codes are:
+
+ ALERT
+
+ The human-readable text contains a special alert that MUST be
+ presented to the user in a fashion that calls the user's
+ attention to the message.
+
+ BADCHARSET
+
+ Optionally followed by a parenthesized list of charsets. A
+ SEARCH failed because the given charset is not supported by
+ this implementation. If the optional list of charsets is
+ given, this lists the charsets that are supported by this
+ implementation.
+
+ CAPABILITY
+
+ Followed by a list of capabilities. This can appear in the
+ initial OK or PREAUTH response to transmit an initial
+ capabilities list. This makes it unnecessary for a client to
+ send a separate CAPABILITY command if it recognizes this
+ response.
+
+ PARSE
+
+ The human-readable text represents an error in parsing the
+ [RFC-2822] header or [MIME-IMB] headers of a message in the
+ mailbox.
+
+ PERMANENTFLAGS
+
+ Followed by a parenthesized list of flags, indicates which of
+ the known flags the client can change permanently. Any flags
+ that are in the FLAGS untagged response, but not the
+ PERMANENTFLAGS list, can not be set permanently. If the client
+ attempts to STORE a flag that is not in the PERMANENTFLAGS
+ list, the server will either ignore the change or store the
+ state change for the remainder of the current session only.
+ The PERMANENTFLAGS list can also include the special flag \*,
+ which indicates that it is possible to create new keywords by
+ attempting to store those flags in the mailbox.
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 64]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ READ-ONLY
+
+ The mailbox is selected read-only, or its access while selected
+ has changed from read-write to read-only.
+
+ READ-WRITE
+
+ The mailbox is selected read-write, or its access while
+ selected has changed from read-only to read-write.
+
+ TRYCREATE
+
+ An APPEND or COPY attempt is failing because the target mailbox
+ does not exist (as opposed to some other reason). This is a
+ hint to the client that the operation can succeed if the
+ mailbox is first created by the CREATE command.
+
+ UIDNEXT
+
+ Followed by a decimal number, indicates the next unique
+ identifier value. Refer to section 2.3.1.1 for more
+ information.
+
+ UIDVALIDITY
+
+ Followed by a decimal number, indicates the unique identifier
+ validity value. Refer to section 2.3.1.1 for more information.
+
+ UNSEEN
+
+ Followed by a decimal number, indicates the number of the first
+ message without the \Seen flag set.
+
+ Additional response codes defined by particular client or server
+ implementations SHOULD be prefixed with an "X" until they are
+ added to a revision of this protocol. Client implementations
+ SHOULD ignore response codes that they do not recognize.
+
+7.1.1. OK Response
+
+ Contents: OPTIONAL response code
+ human-readable text
+
+ The OK response indicates an information message from the server.
+ When tagged, it indicates successful completion of the associated
+ command. The human-readable text MAY be presented to the user as
+ an information message. The untagged form indicates an
+
+
+
+
+Crispin Standards Track [Page 65]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ information-only message; the nature of the information MAY be
+ indicated by a response code.
+
+ The untagged form is also used as one of three possible greetings
+ at connection startup. It indicates that the connection is not
+ yet authenticated and that a LOGIN command is needed.
+
+ Example: S: * OK IMAP4rev1 server ready
+ C: A001 LOGIN fred blurdybloop
+ S: * OK [ALERT] System shutdown in 10 minutes
+ S: A001 OK LOGIN Completed
+
+
+7.1.2. NO Response
+
+ Contents: OPTIONAL response code
+ human-readable text
+
+ The NO response indicates an operational error message from the
+ server. When tagged, it indicates unsuccessful completion of the
+ associated command. The untagged form indicates a warning; the
+ command can still complete successfully. The human-readable text
+ describes the condition.
+
+ Example: C: A222 COPY 1:2 owatagusiam
+ S: * NO Disk is 98% full, please delete unnecessary data
+ S: A222 OK COPY completed
+ C: A223 COPY 3:200 blurdybloop
+ S: * NO Disk is 98% full, please delete unnecessary data
+ S: * NO Disk is 99% full, please delete unnecessary data
+ S: A223 NO COPY failed: disk is full
+
+
+7.1.3. BAD Response
+
+ Contents: OPTIONAL response code
+ human-readable text
+
+ The BAD response indicates an error message from the server. When
+ tagged, it reports a protocol-level error in the client's command;
+ the tag indicates the command that caused the error. The untagged
+ form indicates a protocol-level error for which the associated
+ command can not be determined; it can also indicate an internal
+ server failure. The human-readable text describes the condition.
+
+
+
+
+
+
+
+Crispin Standards Track [Page 66]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Example: C: ...very long command line...
+ S: * BAD Command line too long
+ C: ...empty line...
+ S: * BAD Empty command line
+ C: A443 EXPUNGE
+ S: * BAD Disk crash, attempting salvage to a new disk!
+ S: * OK Salvage successful, no data lost
+ S: A443 OK Expunge completed
+
+
+7.1.4. PREAUTH Response
+
+ Contents: OPTIONAL response code
+ human-readable text
+
+ The PREAUTH response is always untagged, and is one of three
+ possible greetings at connection startup. It indicates that the
+ connection has already been authenticated by external means; thus
+ no LOGIN command is needed.
+
+ Example: S: * PREAUTH IMAP4rev1 server logged in as Smith
+
+
+7.1.5. BYE Response
+
+ Contents: OPTIONAL response code
+ human-readable text
+
+ The BYE response is always untagged, and indicates that the server
+ is about to close the connection. The human-readable text MAY be
+ displayed to the user in a status report by the client. The BYE
+ response is sent under one of four conditions:
+
+ 1) as part of a normal logout sequence. The server will close
+ the connection after sending the tagged OK response to the
+ LOGOUT command.
+
+ 2) as a panic shutdown announcement. The server closes the
+ connection immediately.
+
+ 3) as an announcement of an inactivity autologout. The server
+ closes the connection immediately.
+
+ 4) as one of three possible greetings at connection startup,
+ indicating that the server is not willing to accept a
+ connection from this client. The server closes the
+ connection immediately.
+
+
+
+
+Crispin Standards Track [Page 67]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The difference between a BYE that occurs as part of a normal
+ LOGOUT sequence (the first case) and a BYE that occurs because of
+ a failure (the other three cases) is that the connection closes
+ immediately in the failure case. In all cases the client SHOULD
+ continue to read response data from the server until the
+ connection is closed; this will ensure that any pending untagged
+ or completion responses are read and processed.
+
+ Example: S: * BYE Autologout; idle for too long
+
+7.2. Server Responses - Server and Mailbox Status
+
+ These responses are always untagged. This is how server and mailbox
+ status data are transmitted from the server to the client. Many of
+ these responses typically result from a command with the same name.
+
+7.2.1. CAPABILITY Response
+
+ Contents: capability listing
+
+ The CAPABILITY response occurs as a result of a CAPABILITY
+ command. The capability listing contains a space-separated
+ listing of capability names that the server supports. The
+ capability listing MUST include the atom "IMAP4rev1".
+
+ In addition, client and server implementations MUST implement the
+ STARTTLS, LOGINDISABLED, and AUTH=PLAIN (described in [IMAP-TLS])
+ capabilities. See the Security Considerations section for
+ important information.
+
+ A capability name which begins with "AUTH=" indicates that the
+ server supports that particular authentication mechanism.
+
+ The LOGINDISABLED capability indicates that the LOGIN command is
+ disabled, and that the server will respond with a tagged NO
+ response to any attempt to use the LOGIN command even if the user
+ name and password are valid. An IMAP client MUST NOT issue the
+ LOGIN command if the server advertises the LOGINDISABLED
+ capability.
+
+ Other capability names indicate that the server supports an
+ extension, revision, or amendment to the IMAP4rev1 protocol.
+ Server responses MUST conform to this document until the client
+ issues a command that uses the associated capability.
+
+ Capability names MUST either begin with "X" or be standard or
+ standards-track IMAP4rev1 extensions, revisions, or amendments
+ registered with IANA. A server MUST NOT offer unregistered or
+
+
+
+Crispin Standards Track [Page 68]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ non-standard capability names, unless such names are prefixed with
+ an "X".
+
+ Client implementations SHOULD NOT require any capability name
+ other than "IMAP4rev1", and MUST ignore any unknown capability
+ names.
+
+ A server MAY send capabilities automatically, by using the
+ CAPABILITY response code in the initial PREAUTH or OK responses,
+ and by sending an updated CAPABILITY response code in the tagged
+ OK response as part of a successful authentication. It is
+ unnecessary for a client to send a separate CAPABILITY command if
+ it recognizes these automatic capabilities.
+
+ Example: S: * CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI XPIG-LATIN
+
+
+7.2.2. LIST Response
+
+ Contents: name attributes
+ hierarchy delimiter
+ name
+
+ The LIST response occurs as a result of a LIST command. It
+ returns a single name that matches the LIST specification. There
+ can be multiple LIST responses for a single LIST command.
+
+ Four name attributes are defined:
+
+ \Noinferiors
+ It is not possible for any child levels of hierarchy to exist
+ under this name; no child levels exist now and none can be
+ created in the future.
+
+ \Noselect
+ It is not possible to use this name as a selectable mailbox.
+
+ \Marked
+ The mailbox has been marked "interesting" by the server; the
+ mailbox probably contains messages that have been added since
+ the last time the mailbox was selected.
+
+ \Unmarked
+ The mailbox does not contain any additional messages since the
+ last time the mailbox was selected.
+
+
+
+
+
+
+Crispin Standards Track [Page 69]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ If it is not feasible for the server to determine whether or not
+ the mailbox is "interesting", or if the name is a \Noselect name,
+ the server SHOULD NOT send either \Marked or \Unmarked.
+
+ The hierarchy delimiter is a character used to delimit levels of
+ hierarchy in a mailbox name. A client can use it to create child
+ mailboxes, and to search higher or lower levels of naming
+ hierarchy. All children of a top-level hierarchy node MUST use
+ the same separator character. A NIL hierarchy delimiter means
+ that no hierarchy exists; the name is a "flat" name.
+
+ The name represents an unambiguous left-to-right hierarchy, and
+ MUST be valid for use as a reference in LIST and LSUB commands.
+ Unless \Noselect is indicated, the name MUST also be valid as an
+ argument for commands, such as SELECT, that accept mailbox names.
+
+ Example: S: * LIST (\Noselect) "/" ~/Mail/foo
+
+
+7.2.3. LSUB Response
+
+ Contents: name attributes
+ hierarchy delimiter
+ name
+
+ The LSUB response occurs as a result of an LSUB command. It
+ returns a single name that matches the LSUB specification. There
+ can be multiple LSUB responses for a single LSUB command. The
+ data is identical in format to the LIST response.
+
+ Example: S: * LSUB () "." #news.comp.mail.misc
+
+
+7.2.4 STATUS Response
+
+ Contents: name
+ status parenthesized list
+
+ The STATUS response occurs as a result of an STATUS command. It
+ returns the mailbox name that matches the STATUS specification and
+ the requested mailbox status information.
+
+ Example: S: * STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292)
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 70]
+
+RFC 3501 IMAPv4 March 2003
+
+
+7.2.5. SEARCH Response
+
+ Contents: zero or more numbers
+
+ The SEARCH response occurs as a result of a SEARCH or UID SEARCH
+ command. The number(s) refer to those messages that match the
+ search criteria. For SEARCH, these are message sequence numbers;
+ for UID SEARCH, these are unique identifiers. Each number is
+ delimited by a space.
+
+ Example: S: * SEARCH 2 3 6
+
+
+7.2.6. FLAGS Response
+
+ Contents: flag parenthesized list
+
+ The FLAGS response occurs as a result of a SELECT or EXAMINE
+ command. The flag parenthesized list identifies the flags (at a
+ minimum, the system-defined flags) that are applicable for this
+ mailbox. Flags other than the system flags can also exist,
+ depending on server implementation.
+
+ The update from the FLAGS response MUST be recorded by the client.
+
+ Example: S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
+
+
+7.3. Server Responses - Mailbox Size
+
+ These responses are always untagged. This is how changes in the size
+ of the mailbox are transmitted from the server to the client.
+ Immediately following the "*" token is a number that represents a
+ message count.
+
+7.3.1. EXISTS Response
+
+ Contents: none
+
+ The EXISTS response reports the number of messages in the mailbox.
+ This response occurs as a result of a SELECT or EXAMINE command,
+ and if the size of the mailbox changes (e.g., new messages).
+
+ The update from the EXISTS response MUST be recorded by the
+ client.
+
+ Example: S: * 23 EXISTS
+
+
+
+
+Crispin Standards Track [Page 71]
+
+RFC 3501 IMAPv4 March 2003
+
+
+7.3.2. RECENT Response
+
+ Contents: none
+
+ The RECENT response reports the number of messages with the
+ \Recent flag set. This response occurs as a result of a SELECT or
+ EXAMINE command, and if the size of the mailbox changes (e.g., new
+ messages).
+
+ Note: It is not guaranteed that the message sequence
+ numbers of recent messages will be a contiguous range of
+ the highest n messages in the mailbox (where n is the
+ value reported by the RECENT response). Examples of
+ situations in which this is not the case are: multiple
+ clients having the same mailbox open (the first session
+ to be notified will see it as recent, others will
+ probably see it as non-recent), and when the mailbox is
+ re-ordered by a non-IMAP agent.
+
+ The only reliable way to identify recent messages is to
+ look at message flags to see which have the \Recent flag
+ set, or to do a SEARCH RECENT.
+
+ The update from the RECENT response MUST be recorded by the
+ client.
+
+ Example: S: * 5 RECENT
+
+
+7.4. Server Responses - Message Status
+
+ These responses are always untagged. This is how message data are
+ transmitted from the server to the client, often as a result of a
+ command with the same name. Immediately following the "*" token is a
+ number that represents a message sequence number.
+
+7.4.1. EXPUNGE Response
+
+ Contents: none
+
+ The EXPUNGE response reports that the specified message sequence
+ number has been permanently removed from the mailbox. The message
+ sequence number for each successive message in the mailbox is
+ immediately decremented by 1, and this decrement is reflected in
+ message sequence numbers in subsequent responses (including other
+ untagged EXPUNGE responses).
+
+
+
+
+
+Crispin Standards Track [Page 72]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ The EXPUNGE response also decrements the number of messages in the
+ mailbox; it is not necessary to send an EXISTS response with the
+ new value.
+
+ As a result of the immediate decrement rule, message sequence
+ numbers that appear in a set of successive EXPUNGE responses
+ depend upon whether the messages are removed starting from lower
+ numbers to higher numbers, or from higher numbers to lower
+ numbers. For example, if the last 5 messages in a 9-message
+ mailbox are expunged, a "lower to higher" server will send five
+ untagged EXPUNGE responses for message sequence number 5, whereas
+ a "higher to lower server" will send successive untagged EXPUNGE
+ responses for message sequence numbers 9, 8, 7, 6, and 5.
+
+ An EXPUNGE response MUST NOT be sent when no command is in
+ progress, nor while responding to a FETCH, STORE, or SEARCH
+ command. This rule is necessary to prevent a loss of
+ synchronization of message sequence numbers between client and
+ server. A command is not "in progress" until the complete command
+ has been received; in particular, a command is not "in progress"
+ during the negotiation of command continuation.
+
+ Note: UID FETCH, UID STORE, and UID SEARCH are different
+ commands from FETCH, STORE, and SEARCH. An EXPUNGE
+ response MAY be sent during a UID command.
+
+ The update from the EXPUNGE response MUST be recorded by the
+ client.
+
+ Example: S: * 44 EXPUNGE
+
+
+7.4.2. FETCH Response
+
+ Contents: message data
+
+ The FETCH response returns data about a message to the client.
+ The data are pairs of data item names and their values in
+ parentheses. This response occurs as the result of a FETCH or
+ STORE command, as well as by unilateral server decision (e.g.,
+ flag updates).
+
+ The current data items are:
+
+ BODY
+ A form of BODYSTRUCTURE without extension data.
+
+
+
+
+
+Crispin Standards Track [Page 73]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ BODY[<section>]<<origin octet>>
+ A string expressing the body contents of the specified section.
+ The string SHOULD be interpreted by the client according to the
+ content transfer encoding, body type, and subtype.
+
+ If the origin octet is specified, this string is a substring of
+ the entire body contents, starting at that origin octet. This
+ means that BODY[]<0> MAY be truncated, but BODY[] is NEVER
+ truncated.
+
+ Note: The origin octet facility MUST NOT be used by a server
+ in a FETCH response unless the client specifically requested
+ it by means of a FETCH of a BODY[<section>]<<partial>> data
+ item.
+
+ 8-bit textual data is permitted if a [CHARSET] identifier is
+ part of the body parameter parenthesized list for this section.
+ Note that headers (part specifiers HEADER or MIME, or the
+ header portion of a MESSAGE/RFC822 part), MUST be 7-bit; 8-bit
+ characters are not permitted in headers. Note also that the
+ [RFC-2822] delimiting blank line between the header and the
+ body is not affected by header line subsetting; the blank line
+ is always included as part of header data, except in the case
+ of a message which has no body and no blank line.
+
+ Non-textual data such as binary data MUST be transfer encoded
+ into a textual form, such as BASE64, prior to being sent to the
+ client. To derive the original binary data, the client MUST
+ decode the transfer encoded string.
+
+ BODYSTRUCTURE
+ A parenthesized list that describes the [MIME-IMB] body
+ structure of a message. This is computed by the server by
+ parsing the [MIME-IMB] header fields, defaulting various fields
+ as necessary.
+
+ For example, a simple text message of 48 lines and 2279 octets
+ can have a body structure of: ("TEXT" "PLAIN" ("CHARSET"
+ "US-ASCII") NIL NIL "7BIT" 2279 48)
+
+ Multiple parts are indicated by parenthesis nesting. Instead
+ of a body type as the first element of the parenthesized list,
+ there is a sequence of one or more nested body structures. The
+ second element of the parenthesized list is the multipart
+ subtype (mixed, digest, parallel, alternative, etc.).
+
+
+
+
+
+
+Crispin Standards Track [Page 74]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ For example, a two part message consisting of a text and a
+ BASE64-encoded text attachment can have a body structure of:
+ (("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152
+ 23)("TEXT" "PLAIN" ("CHARSET" "US-ASCII" "NAME" "cc.diff")
+ "<960723163407.20117h@cac.washington.edu>" "Compiler diff"
+ "BASE64" 4554 73) "MIXED")
+
+ Extension data follows the multipart subtype. Extension data
+ is never returned with the BODY fetch, but can be returned with
+ a BODYSTRUCTURE fetch. Extension data, if present, MUST be in
+ the defined order. The extension data of a multipart body part
+ are in the following order:
+
+ body parameter parenthesized list
+ A parenthesized list of attribute/value pairs [e.g., ("foo"
+ "bar" "baz" "rag") where "bar" is the value of "foo", and
+ "rag" is the value of "baz"] as defined in [MIME-IMB].
+
+ body disposition
+ A parenthesized list, consisting of a disposition type
+ string, followed by a parenthesized list of disposition
+ attribute/value pairs as defined in [DISPOSITION].
+
+ body language
+ A string or parenthesized list giving the body language
+ value as defined in [LANGUAGE-TAGS].
+
+ body location
+ A string list giving the body content URI as defined in
+ [LOCATION].
+
+ Any following extension data are not yet defined in this
+ version of the protocol. Such extension data can consist of
+ zero or more NILs, strings, numbers, or potentially nested
+ parenthesized lists of such data. Client implementations that
+ do a BODYSTRUCTURE fetch MUST be prepared to accept such
+ extension data. Server implementations MUST NOT send such
+ extension data until it has been defined by a revision of this
+ protocol.
+
+ The basic fields of a non-multipart body part are in the
+ following order:
+
+ body type
+ A string giving the content media type name as defined in
+ [MIME-IMB].
+
+
+
+
+
+Crispin Standards Track [Page 75]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ body subtype
+ A string giving the content subtype name as defined in
+ [MIME-IMB].
+
+ body parameter parenthesized list
+ A parenthesized list of attribute/value pairs [e.g., ("foo"
+ "bar" "baz" "rag") where "bar" is the value of "foo" and
+ "rag" is the value of "baz"] as defined in [MIME-IMB].
+
+ body id
+ A string giving the content id as defined in [MIME-IMB].
+
+ body description
+ A string giving the content description as defined in
+ [MIME-IMB].
+
+ body encoding
+ A string giving the content transfer encoding as defined in
+ [MIME-IMB].
+
+ body size
+ A number giving the size of the body in octets. Note that
+ this size is the size in its transfer encoding and not the
+ resulting size after any decoding.
+
+ A body type of type MESSAGE and subtype RFC822 contains,
+ immediately after the basic fields, the envelope structure,
+ body structure, and size in text lines of the encapsulated
+ message.
+
+ A body type of type TEXT contains, immediately after the basic
+ fields, the size of the body in text lines. Note that this
+ size is the size in its content transfer encoding and not the
+ resulting size after any decoding.
+
+ Extension data follows the basic fields and the type-specific
+ fields listed above. Extension data is never returned with the
+ BODY fetch, but can be returned with a BODYSTRUCTURE fetch.
+ Extension data, if present, MUST be in the defined order.
+
+ The extension data of a non-multipart body part are in the
+ following order:
+
+ body MD5
+ A string giving the body MD5 value as defined in [MD5].
+
+
+
+
+
+
+Crispin Standards Track [Page 76]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ body disposition
+ A parenthesized list with the same content and function as
+ the body disposition for a multipart body part.
+
+ body language
+ A string or parenthesized list giving the body language
+ value as defined in [LANGUAGE-TAGS].
+
+ body location
+ A string list giving the body content URI as defined in
+ [LOCATION].
+
+ Any following extension data are not yet defined in this
+ version of the protocol, and would be as described above under
+ multipart extension data.
+
+ ENVELOPE
+ A parenthesized list that describes the envelope structure of a
+ message. This is computed by the server by parsing the
+ [RFC-2822] header into the component parts, defaulting various
+ fields as necessary.
+
+ The fields of the envelope structure are in the following
+ order: date, subject, from, sender, reply-to, to, cc, bcc,
+ in-reply-to, and message-id. The date, subject, in-reply-to,
+ and message-id fields are strings. The from, sender, reply-to,
+ to, cc, and bcc fields are parenthesized lists of address
+ structures.
+
+ An address structure is a parenthesized list that describes an
+ electronic mail address. The fields of an address structure
+ are in the following order: personal name, [SMTP]
+ at-domain-list (source route), mailbox name, and host name.
+
+ [RFC-2822] group syntax is indicated by a special form of
+ address structure in which the host name field is NIL. If the
+ mailbox name field is also NIL, this is an end of group marker
+ (semi-colon in RFC 822 syntax). If the mailbox name field is
+ non-NIL, this is a start of group marker, and the mailbox name
+ field holds the group name phrase.
+
+ If the Date, Subject, In-Reply-To, and Message-ID header lines
+ are absent in the [RFC-2822] header, the corresponding member
+ of the envelope is NIL; if these header lines are present but
+ empty the corresponding member of the envelope is the empty
+ string.
+
+
+
+
+
+Crispin Standards Track [Page 77]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Note: some servers may return a NIL envelope member in the
+ "present but empty" case. Clients SHOULD treat NIL and
+ empty string as identical.
+
+ Note: [RFC-2822] requires that all messages have a valid
+ Date header. Therefore, the date member in the envelope can
+ not be NIL or the empty string.
+
+ Note: [RFC-2822] requires that the In-Reply-To and
+ Message-ID headers, if present, have non-empty content.
+ Therefore, the in-reply-to and message-id members in the
+ envelope can not be the empty string.
+
+ If the From, To, cc, and bcc header lines are absent in the
+ [RFC-2822] header, or are present but empty, the corresponding
+ member of the envelope is NIL.
+
+ If the Sender or Reply-To lines are absent in the [RFC-2822]
+ header, or are present but empty, the server sets the
+ corresponding member of the envelope to be the same value as
+ the from member (the client is not expected to know to do
+ this).
+
+ Note: [RFC-2822] requires that all messages have a valid
+ From header. Therefore, the from, sender, and reply-to
+ members in the envelope can not be NIL.
+
+ FLAGS
+ A parenthesized list of flags that are set for this message.
+
+ INTERNALDATE
+ A string representing the internal date of the message.
+
+ RFC822
+ Equivalent to BODY[].
+
+ RFC822.HEADER
+ Equivalent to BODY[HEADER]. Note that this did not result in
+ \Seen being set, because RFC822.HEADER response data occurs as
+ a result of a FETCH of RFC822.HEADER. BODY[HEADER] response
+ data occurs as a result of a FETCH of BODY[HEADER] (which sets
+ \Seen) or BODY.PEEK[HEADER] (which does not set \Seen).
+
+ RFC822.SIZE
+ A number expressing the [RFC-2822] size of the message.
+
+
+
+
+
+
+Crispin Standards Track [Page 78]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ RFC822.TEXT
+ Equivalent to BODY[TEXT].
+
+ UID
+ A number expressing the unique identifier of the message.
+
+
+ Example: S: * 23 FETCH (FLAGS (\Seen) RFC822.SIZE 44827)
+
+
+7.5. Server Responses - Command Continuation Request
+
+ The command continuation request response is indicated by a "+" token
+ instead of a tag. This form of response indicates that the server is
+ ready to accept the continuation of a command from the client. The
+ remainder of this response is a line of text.
+
+ This response is used in the AUTHENTICATE command to transmit server
+ data to the client, and request additional client data. This
+ response is also used if an argument to any command is a literal.
+
+ The client is not permitted to send the octets of the literal unless
+ the server indicates that it is expected. This permits the server to
+ process commands and reject errors on a line-by-line basis. The
+ remainder of the command, including the CRLF that terminates a
+ command, follows the octets of the literal. If there are any
+ additional command arguments, the literal octets are followed by a
+ space and those arguments.
+
+ Example: C: A001 LOGIN {11}
+ S: + Ready for additional command text
+ C: FRED FOOBAR {7}
+ S: + Ready for additional command text
+ C: fat man
+ S: A001 OK LOGIN completed
+ C: A044 BLURDYBLOOP {102856}
+ S: A044 BAD No such command as "BLURDYBLOOP"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 79]
+
+RFC 3501 IMAPv4 March 2003
+
+
+8. Sample IMAP4rev1 connection
+
+ The following is a transcript of an IMAP4rev1 connection. A long
+ line in this sample is broken for editorial clarity.
+
+S: * OK IMAP4rev1 Service Ready
+C: a001 login mrc secret
+S: a001 OK LOGIN completed
+C: a002 select inbox
+S: * 18 EXISTS
+S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
+S: * 2 RECENT
+S: * OK [UNSEEN 17] Message 17 is the first unseen message
+S: * OK [UIDVALIDITY 3857529045] UIDs valid
+S: a002 OK [READ-WRITE] SELECT completed
+C: a003 fetch 12 full
+S: * 12 FETCH (FLAGS (\Seen) INTERNALDATE "17-Jul-1996 02:44:25 -0700"
+ RFC822.SIZE 4286 ENVELOPE ("Wed, 17 Jul 1996 02:23:25 -0700 (PDT)"
+ "IMAP4rev1 WG mtg summary and minutes"
+ (("Terry Gray" NIL "gray" "cac.washington.edu"))
+ (("Terry Gray" NIL "gray" "cac.washington.edu"))
+ (("Terry Gray" NIL "gray" "cac.washington.edu"))
+ ((NIL NIL "imap" "cac.washington.edu"))
+ ((NIL NIL "minutes" "CNRI.Reston.VA.US")
+ ("John Klensin" NIL "KLENSIN" "MIT.EDU")) NIL NIL
+ "<B27397-0100000@cac.washington.edu>")
+ BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028
+ 92))
+S: a003 OK FETCH completed
+C: a004 fetch 12 body[header]
+S: * 12 FETCH (BODY[HEADER] {342}
+S: Date: Wed, 17 Jul 1996 02:23:25 -0700 (PDT)
+S: From: Terry Gray <gray@cac.washington.edu>
+S: Subject: IMAP4rev1 WG mtg summary and minutes
+S: To: imap@cac.washington.edu
+S: cc: minutes@CNRI.Reston.VA.US, John Klensin <KLENSIN@MIT.EDU>
+S: Message-Id: <B27397-0100000@cac.washington.edu>
+S: MIME-Version: 1.0
+S: Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
+S:
+S: )
+S: a004 OK FETCH completed
+C: a005 store 12 +flags \deleted
+S: * 12 FETCH (FLAGS (\Seen \Deleted))
+S: a005 OK +FLAGS completed
+C: a006 logout
+S: * BYE IMAP4rev1 server terminating connection
+S: a006 OK LOGOUT completed
+
+
+
+Crispin Standards Track [Page 80]
+
+RFC 3501 IMAPv4 March 2003
+
+
+9. Formal Syntax
+
+ The following syntax specification uses the Augmented Backus-Naur
+ Form (ABNF) notation as specified in [ABNF].
+
+ In the case of alternative or optional rules in which a later rule
+ overlaps an earlier rule, the rule which is listed earlier MUST take
+ priority. For example, "\Seen" when parsed as a flag is the \Seen
+ flag name and not a flag-extension, even though "\Seen" can be parsed
+ as a flag-extension. Some, but not all, instances of this rule are
+ noted below.
+
+ Note: [ABNF] rules MUST be followed strictly; in
+ particular:
+
+ (1) Except as noted otherwise, all alphabetic characters
+ are case-insensitive. The use of upper or lower case
+ characters to define token strings is for editorial clarity
+ only. Implementations MUST accept these strings in a
+ case-insensitive fashion.
+
+ (2) In all cases, SP refers to exactly one space. It is
+ NOT permitted to substitute TAB, insert additional spaces,
+ or otherwise treat SP as being equivalent to LWSP.
+
+ (3) The ASCII NUL character, %x00, MUST NOT be used at any
+ time.
+
+address = "(" addr-name SP addr-adl SP addr-mailbox SP
+ addr-host ")"
+
+addr-adl = nstring
+ ; Holds route from [RFC-2822] route-addr if
+ ; non-NIL
+
+addr-host = nstring
+ ; NIL indicates [RFC-2822] group syntax.
+ ; Otherwise, holds [RFC-2822] domain name
+
+addr-mailbox = nstring
+ ; NIL indicates end of [RFC-2822] group; if
+ ; non-NIL and addr-host is NIL, holds
+ ; [RFC-2822] group name.
+ ; Otherwise, holds [RFC-2822] local-part
+ ; after removing [RFC-2822] quoting
+
+
+
+
+
+
+Crispin Standards Track [Page 81]
+
+RFC 3501 IMAPv4 March 2003
+
+
+addr-name = nstring
+ ; If non-NIL, holds phrase from [RFC-2822]
+ ; mailbox after removing [RFC-2822] quoting
+
+append = "APPEND" SP mailbox [SP flag-list] [SP date-time] SP
+ literal
+
+astring = 1*ASTRING-CHAR / string
+
+ASTRING-CHAR = ATOM-CHAR / resp-specials
+
+atom = 1*ATOM-CHAR
+
+ATOM-CHAR = <any CHAR except atom-specials>
+
+atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
+ quoted-specials / resp-specials
+
+authenticate = "AUTHENTICATE" SP auth-type *(CRLF base64)
+
+auth-type = atom
+ ; Defined by [SASL]
+
+base64 = *(4base64-char) [base64-terminal]
+
+base64-char = ALPHA / DIGIT / "+" / "/"
+ ; Case-sensitive
+
+base64-terminal = (2base64-char "==") / (3base64-char "=")
+
+body = "(" (body-type-1part / body-type-mpart) ")"
+
+body-extension = nstring / number /
+ "(" body-extension *(SP body-extension) ")"
+ ; Future expansion. Client implementations
+ ; MUST accept body-extension fields. Server
+ ; implementations MUST NOT generate
+ ; body-extension fields except as defined by
+ ; future standard or standards-track
+ ; revisions of this specification.
+
+body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
+ [SP body-fld-loc *(SP body-extension)]]]
+ ; MUST NOT be returned on non-extensible
+ ; "BODY" fetch
+
+
+
+
+
+
+Crispin Standards Track [Page 82]
+
+RFC 3501 IMAPv4 March 2003
+
+
+body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
+ [SP body-fld-loc *(SP body-extension)]]]
+ ; MUST NOT be returned on non-extensible
+ ; "BODY" fetch
+
+body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
+ body-fld-enc SP body-fld-octets
+
+body-fld-desc = nstring
+
+body-fld-dsp = "(" string SP body-fld-param ")" / nil
+
+body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
+ "QUOTED-PRINTABLE") DQUOTE) / string
+
+body-fld-id = nstring
+
+body-fld-lang = nstring / "(" string *(SP string) ")"
+
+body-fld-loc = nstring
+
+body-fld-lines = number
+
+body-fld-md5 = nstring
+
+body-fld-octets = number
+
+body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
+
+body-type-1part = (body-type-basic / body-type-msg / body-type-text)
+ [SP body-ext-1part]
+
+body-type-basic = media-basic SP body-fields
+ ; MESSAGE subtype MUST NOT be "RFC822"
+
+body-type-mpart = 1*body SP media-subtype
+ [SP body-ext-mpart]
+
+body-type-msg = media-message SP body-fields SP envelope
+ SP body SP body-fld-lines
+
+body-type-text = media-text SP body-fields SP body-fld-lines
+
+capability = ("AUTH=" auth-type) / atom
+ ; New capabilities MUST begin with "X" or be
+ ; registered with IANA as standard or
+ ; standards-track
+
+
+
+
+Crispin Standards Track [Page 83]
+
+RFC 3501 IMAPv4 March 2003
+
+
+capability-data = "CAPABILITY" *(SP capability) SP "IMAP4rev1"
+ *(SP capability)
+ ; Servers MUST implement the STARTTLS, AUTH=PLAIN,
+ ; and LOGINDISABLED capabilities
+ ; Servers which offer RFC 1730 compatibility MUST
+ ; list "IMAP4" as the first capability.
+
+CHAR8 = %x01-ff
+ ; any OCTET except NUL, %x00
+
+command = tag SP (command-any / command-auth / command-nonauth /
+ command-select) CRLF
+ ; Modal based on state
+
+command-any = "CAPABILITY" / "LOGOUT" / "NOOP" / x-command
+ ; Valid in all states
+
+command-auth = append / create / delete / examine / list / lsub /
+ rename / select / status / subscribe / unsubscribe
+ ; Valid only in Authenticated or Selected state
+
+command-nonauth = login / authenticate / "STARTTLS"
+ ; Valid only when in Not Authenticated state
+
+command-select = "CHECK" / "CLOSE" / "EXPUNGE" / copy / fetch / store /
+ uid / search
+ ; Valid only when in Selected state
+
+continue-req = "+" SP (resp-text / base64) CRLF
+
+copy = "COPY" SP sequence-set SP mailbox
+
+create = "CREATE" SP mailbox
+ ; Use of INBOX gives a NO error
+
+date = date-text / DQUOTE date-text DQUOTE
+
+date-day = 1*2DIGIT
+ ; Day of month
+
+date-day-fixed = (SP DIGIT) / 2DIGIT
+ ; Fixed-format version of date-day
+
+date-month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
+ "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
+
+date-text = date-day "-" date-month "-" date-year
+
+
+
+
+Crispin Standards Track [Page 84]
+
+RFC 3501 IMAPv4 March 2003
+
+
+date-year = 4DIGIT
+
+date-time = DQUOTE date-day-fixed "-" date-month "-" date-year
+ SP time SP zone DQUOTE
+
+delete = "DELETE" SP mailbox
+ ; Use of INBOX gives a NO error
+
+digit-nz = %x31-39
+ ; 1-9
+
+envelope = "(" env-date SP env-subject SP env-from SP
+ env-sender SP env-reply-to SP env-to SP env-cc SP
+ env-bcc SP env-in-reply-to SP env-message-id ")"
+
+env-bcc = "(" 1*address ")" / nil
+
+env-cc = "(" 1*address ")" / nil
+
+env-date = nstring
+
+env-from = "(" 1*address ")" / nil
+
+env-in-reply-to = nstring
+
+env-message-id = nstring
+
+env-reply-to = "(" 1*address ")" / nil
+
+env-sender = "(" 1*address ")" / nil
+
+env-subject = nstring
+
+env-to = "(" 1*address ")" / nil
+
+examine = "EXAMINE" SP mailbox
+
+fetch = "FETCH" SP sequence-set SP ("ALL" / "FULL" / "FAST" /
+ fetch-att / "(" fetch-att *(SP fetch-att) ")")
+
+fetch-att = "ENVELOPE" / "FLAGS" / "INTERNALDATE" /
+ "RFC822" [".HEADER" / ".SIZE" / ".TEXT"] /
+ "BODY" ["STRUCTURE"] / "UID" /
+ "BODY" section ["<" number "." nz-number ">"] /
+ "BODY.PEEK" section ["<" number "." nz-number ">"]
+
+
+
+
+
+
+Crispin Standards Track [Page 85]
+
+RFC 3501 IMAPv4 March 2003
+
+
+flag = "\Answered" / "\Flagged" / "\Deleted" /
+ "\Seen" / "\Draft" / flag-keyword / flag-extension
+ ; Does not include "\Recent"
+
+flag-extension = "\" atom
+ ; Future expansion. Client implementations
+ ; MUST accept flag-extension flags. Server
+ ; implementations MUST NOT generate
+ ; flag-extension flags except as defined by
+ ; future standard or standards-track
+ ; revisions of this specification.
+
+flag-fetch = flag / "\Recent"
+
+flag-keyword = atom
+
+flag-list = "(" [flag *(SP flag)] ")"
+
+flag-perm = flag / "\*"
+
+greeting = "*" SP (resp-cond-auth / resp-cond-bye) CRLF
+
+header-fld-name = astring
+
+header-list = "(" header-fld-name *(SP header-fld-name) ")"
+
+list = "LIST" SP mailbox SP list-mailbox
+
+list-mailbox = 1*list-char / string
+
+list-char = ATOM-CHAR / list-wildcards / resp-specials
+
+list-wildcards = "%" / "*"
+
+literal = "{" number "}" CRLF *CHAR8
+ ; Number represents the number of CHAR8s
+
+login = "LOGIN" SP userid SP password
+
+lsub = "LSUB" SP mailbox SP list-mailbox
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 86]
+
+RFC 3501 IMAPv4 March 2003
+
+
+mailbox = "INBOX" / astring
+ ; INBOX is case-insensitive. All case variants of
+ ; INBOX (e.g., "iNbOx") MUST be interpreted as INBOX
+ ; not as an astring. An astring which consists of
+ ; the case-insensitive sequence "I" "N" "B" "O" "X"
+ ; is considered to be INBOX and not an astring.
+ ; Refer to section 5.1 for further
+ ; semantic details of mailbox names.
+
+mailbox-data = "FLAGS" SP flag-list / "LIST" SP mailbox-list /
+ "LSUB" SP mailbox-list / "SEARCH" *(SP nz-number) /
+ "STATUS" SP mailbox SP "(" [status-att-list] ")" /
+ number SP "EXISTS" / number SP "RECENT"
+
+mailbox-list = "(" [mbx-list-flags] ")" SP
+ (DQUOTE QUOTED-CHAR DQUOTE / nil) SP mailbox
+
+mbx-list-flags = *(mbx-list-oflag SP) mbx-list-sflag
+ *(SP mbx-list-oflag) /
+ mbx-list-oflag *(SP mbx-list-oflag)
+
+mbx-list-oflag = "\Noinferiors" / flag-extension
+ ; Other flags; multiple possible per LIST response
+
+mbx-list-sflag = "\Noselect" / "\Marked" / "\Unmarked"
+ ; Selectability flags; only one per LIST response
+
+media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
+ "MESSAGE" / "VIDEO") DQUOTE) / string) SP
+ media-subtype
+ ; Defined in [MIME-IMT]
+
+media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
+ ; Defined in [MIME-IMT]
+
+media-subtype = string
+ ; Defined in [MIME-IMT]
+
+media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
+ ; Defined in [MIME-IMT]
+
+message-data = nz-number SP ("EXPUNGE" / ("FETCH" SP msg-att))
+
+msg-att = "(" (msg-att-dynamic / msg-att-static)
+ *(SP (msg-att-dynamic / msg-att-static)) ")"
+
+msg-att-dynamic = "FLAGS" SP "(" [flag-fetch *(SP flag-fetch)] ")"
+ ; MAY change for a message
+
+
+
+Crispin Standards Track [Page 87]
+
+RFC 3501 IMAPv4 March 2003
+
+
+msg-att-static = "ENVELOPE" SP envelope / "INTERNALDATE" SP date-time /
+ "RFC822" [".HEADER" / ".TEXT"] SP nstring /
+ "RFC822.SIZE" SP number /
+ "BODY" ["STRUCTURE"] SP body /
+ "BODY" section ["<" number ">"] SP nstring /
+ "UID" SP uniqueid
+ ; MUST NOT change for a message
+
+nil = "NIL"
+
+nstring = string / nil
+
+number = 1*DIGIT
+ ; Unsigned 32-bit integer
+ ; (0 <= n < 4,294,967,296)
+
+nz-number = digit-nz *DIGIT
+ ; Non-zero unsigned 32-bit integer
+ ; (0 < n < 4,294,967,296)
+
+password = astring
+
+quoted = DQUOTE *QUOTED-CHAR DQUOTE
+
+QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
+ "\" quoted-specials
+
+quoted-specials = DQUOTE / "\"
+
+rename = "RENAME" SP mailbox SP mailbox
+ ; Use of INBOX as a destination gives a NO error
+
+response = *(continue-req / response-data) response-done
+
+response-data = "*" SP (resp-cond-state / resp-cond-bye /
+ mailbox-data / message-data / capability-data) CRLF
+
+response-done = response-tagged / response-fatal
+
+response-fatal = "*" SP resp-cond-bye CRLF
+ ; Server closes connection immediately
+
+response-tagged = tag SP resp-cond-state CRLF
+
+resp-cond-auth = ("OK" / "PREAUTH") SP resp-text
+ ; Authentication condition
+
+
+
+
+
+Crispin Standards Track [Page 88]
+
+RFC 3501 IMAPv4 March 2003
+
+
+resp-cond-bye = "BYE" SP resp-text
+
+resp-cond-state = ("OK" / "NO" / "BAD") SP resp-text
+ ; Status condition
+
+resp-specials = "]"
+
+resp-text = ["[" resp-text-code "]" SP] text
+
+resp-text-code = "ALERT" /
+ "BADCHARSET" [SP "(" astring *(SP astring) ")" ] /
+ capability-data / "PARSE" /
+ "PERMANENTFLAGS" SP "("
+ [flag-perm *(SP flag-perm)] ")" /
+ "READ-ONLY" / "READ-WRITE" / "TRYCREATE" /
+ "UIDNEXT" SP nz-number / "UIDVALIDITY" SP nz-number /
+ "UNSEEN" SP nz-number /
+ atom [SP 1*<any TEXT-CHAR except "]">]
+
+search = "SEARCH" [SP "CHARSET" SP astring] 1*(SP search-key)
+ ; CHARSET argument to MUST be registered with IANA
+
+search-key = "ALL" / "ANSWERED" / "BCC" SP astring /
+ "BEFORE" SP date / "BODY" SP astring /
+ "CC" SP astring / "DELETED" / "FLAGGED" /
+ "FROM" SP astring / "KEYWORD" SP flag-keyword /
+ "NEW" / "OLD" / "ON" SP date / "RECENT" / "SEEN" /
+ "SINCE" SP date / "SUBJECT" SP astring /
+ "TEXT" SP astring / "TO" SP astring /
+ "UNANSWERED" / "UNDELETED" / "UNFLAGGED" /
+ "UNKEYWORD" SP flag-keyword / "UNSEEN" /
+ ; Above this line were in [IMAP2]
+ "DRAFT" / "HEADER" SP header-fld-name SP astring /
+ "LARGER" SP number / "NOT" SP search-key /
+ "OR" SP search-key SP search-key /
+ "SENTBEFORE" SP date / "SENTON" SP date /
+ "SENTSINCE" SP date / "SMALLER" SP number /
+ "UID" SP sequence-set / "UNDRAFT" / sequence-set /
+ "(" search-key *(SP search-key) ")"
+
+section = "[" [section-spec] "]"
+
+section-msgtext = "HEADER" / "HEADER.FIELDS" [".NOT"] SP header-list /
+ "TEXT"
+ ; top-level or MESSAGE/RFC822 part
+
+section-part = nz-number *("." nz-number)
+ ; body part nesting
+
+
+
+Crispin Standards Track [Page 89]
+
+RFC 3501 IMAPv4 March 2003
+
+
+section-spec = section-msgtext / (section-part ["." section-text])
+
+section-text = section-msgtext / "MIME"
+ ; text other than actual body part (headers, etc.)
+
+select = "SELECT" SP mailbox
+
+seq-number = nz-number / "*"
+ ; message sequence number (COPY, FETCH, STORE
+ ; commands) or unique identifier (UID COPY,
+ ; UID FETCH, UID STORE commands).
+ ; * represents the largest number in use. In
+ ; the case of message sequence numbers, it is
+ ; the number of messages in a non-empty mailbox.
+ ; In the case of unique identifiers, it is the
+ ; unique identifier of the last message in the
+ ; mailbox or, if the mailbox is empty, the
+ ; mailbox's current UIDNEXT value.
+ ; The server should respond with a tagged BAD
+ ; response to a command that uses a message
+ ; sequence number greater than the number of
+ ; messages in the selected mailbox. This
+ ; includes "*" if the selected mailbox is empty.
+
+seq-range = seq-number ":" seq-number
+ ; two seq-number values and all values between
+ ; these two regardless of order.
+ ; Example: 2:4 and 4:2 are equivalent and indicate
+ ; values 2, 3, and 4.
+ ; Example: a unique identifier sequence range of
+ ; 3291:* includes the UID of the last message in
+ ; the mailbox, even if that value is less than 3291.
+
+sequence-set = (seq-number / seq-range) *("," sequence-set)
+ ; set of seq-number values, regardless of order.
+ ; Servers MAY coalesce overlaps and/or execute the
+ ; sequence in any order.
+ ; Example: a message sequence number set of
+ ; 2,4:7,9,12:* for a mailbox with 15 messages is
+ ; equivalent to 2,4,5,6,7,9,12,13,14,15
+ ; Example: a message sequence number set of *:4,5:7
+ ; for a mailbox with 10 messages is equivalent to
+ ; 10,9,8,7,6,5,4,5,6,7 and MAY be reordered and
+ ; overlap coalesced to be 4,5,6,7,8,9,10.
+
+status = "STATUS" SP mailbox SP
+ "(" status-att *(SP status-att) ")"
+
+
+
+
+Crispin Standards Track [Page 90]
+
+RFC 3501 IMAPv4 March 2003
+
+
+status-att = "MESSAGES" / "RECENT" / "UIDNEXT" / "UIDVALIDITY" /
+ "UNSEEN"
+
+status-att-list = status-att SP number *(SP status-att SP number)
+
+store = "STORE" SP sequence-set SP store-att-flags
+
+store-att-flags = (["+" / "-"] "FLAGS" [".SILENT"]) SP
+ (flag-list / (flag *(SP flag)))
+
+string = quoted / literal
+
+subscribe = "SUBSCRIBE" SP mailbox
+
+tag = 1*<any ASTRING-CHAR except "+">
+
+text = 1*TEXT-CHAR
+
+TEXT-CHAR = <any CHAR except CR and LF>
+
+time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ ; Hours minutes seconds
+
+uid = "UID" SP (copy / fetch / search / store)
+ ; Unique identifiers used instead of message
+ ; sequence numbers
+
+uniqueid = nz-number
+ ; Strictly ascending
+
+unsubscribe = "UNSUBSCRIBE" SP mailbox
+
+userid = astring
+
+x-command = "X" atom <experimental command arguments>
+
+zone = ("+" / "-") 4DIGIT
+ ; Signed four-digit value of hhmm representing
+ ; hours and minutes east of Greenwich (that is,
+ ; the amount that the given time differs from
+ ; Universal Time). Subtracting the timezone
+ ; from the given time will give the UT form.
+ ; The Universal Time zone is "+0000".
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 91]
+
+RFC 3501 IMAPv4 March 2003
+
+
+10. Author's Note
+
+ This document is a revision or rewrite of earlier documents, and
+ supercedes the protocol specification in those documents: RFC 2060,
+ RFC 1730, unpublished IMAP2bis.TXT document, RFC 1176, and RFC 1064.
+
+11. Security Considerations
+
+ IMAP4rev1 protocol transactions, including electronic mail data, are
+ sent in the clear over the network unless protection from snooping is
+ negotiated. This can be accomplished either by the use of STARTTLS,
+ negotiated privacy protection in the AUTHENTICATE command, or some
+ other protection mechanism.
+
+11.1. STARTTLS Security Considerations
+
+ The specification of the STARTTLS command and LOGINDISABLED
+ capability in this document replaces that in [IMAP-TLS]. [IMAP-TLS]
+ remains normative for the PLAIN [SASL] authenticator.
+
+ IMAP client and server implementations MUST implement the
+ TLS_RSA_WITH_RC4_128_MD5 [TLS] cipher suite, and SHOULD implement the
+ TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA [TLS] cipher suite. This is
+ important as it assures that any two compliant implementations can be
+ configured to interoperate. All other cipher suites are OPTIONAL.
+ Note that this is a change from section 2.1 of [IMAP-TLS].
+
+ During the [TLS] negotiation, the client MUST check its understanding
+ of the server hostname against the server's identity as presented in
+ the server Certificate message, in order to prevent man-in-the-middle
+ attacks. If the match fails, the client SHOULD either ask for
+ explicit user confirmation, or terminate the connection and indicate
+ that the server's identity is suspect. Matching is performed
+ according to these rules:
+
+ The client MUST use the server hostname it used to open the
+ connection as the value to compare against the server name
+ as expressed in the server certificate. The client MUST
+ NOT use any form of the server hostname derived from an
+ insecure remote source (e.g., insecure DNS lookup). CNAME
+ canonicalization is not done.
+
+ If a subjectAltName extension of type dNSName is present in
+ the certificate, it SHOULD be used as the source of the
+ server's identity.
+
+ Matching is case-insensitive.
+
+
+
+
+Crispin Standards Track [Page 92]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ A "*" wildcard character MAY be used as the left-most name
+ component in the certificate. For example, *.example.com
+ would match a.example.com, foo.example.com, etc. but would
+ not match example.com.
+
+ If the certificate contains multiple names (e.g., more than
+ one dNSName field), then a match with any one of the fields
+ is considered acceptable.
+
+ Both the client and server MUST check the result of the STARTTLS
+ command and subsequent [TLS] negotiation to see whether acceptable
+ authentication or privacy was achieved.
+
+11.2. Other Security Considerations
+
+ A server error message for an AUTHENTICATE command which fails due to
+ invalid credentials SHOULD NOT detail why the credentials are
+ invalid.
+
+ Use of the LOGIN command sends passwords in the clear. This can be
+ avoided by using the AUTHENTICATE command with a [SASL] mechanism
+ that does not use plaintext passwords, by first negotiating
+ encryption via STARTTLS or some other protection mechanism.
+
+ A server implementation MUST implement a configuration that, at the
+ time of authentication, requires:
+ (1) The STARTTLS command has been negotiated.
+ OR
+ (2) Some other mechanism that protects the session from password
+ snooping has been provided.
+ OR
+ (3) The following measures are in place:
+ (a) The LOGINDISABLED capability is advertised, and [SASL]
+ mechanisms (such as PLAIN) using plaintext passwords are NOT
+ advertised in the CAPABILITY list.
+ AND
+ (b) The LOGIN command returns an error even if the password is
+ correct.
+ AND
+ (c) The AUTHENTICATE command returns an error with all [SASL]
+ mechanisms that use plaintext passwords, even if the password
+ is correct.
+
+ A server error message for a failing LOGIN command SHOULD NOT specify
+ that the user name, as opposed to the password, is invalid.
+
+ A server SHOULD have mechanisms in place to limit or delay failed
+ AUTHENTICATE/LOGIN attempts.
+
+
+
+Crispin Standards Track [Page 93]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ Additional security considerations are discussed in the section
+ discussing the AUTHENTICATE and LOGIN commands.
+
+12. IANA Considerations
+
+ IMAP4 capabilities are registered by publishing a standards track or
+ IESG approved experimental RFC. The registry is currently located
+ at:
+
+ http://www.iana.org/assignments/imap4-capabilities
+
+ As this specification revises the STARTTLS and LOGINDISABLED
+ extensions previously defined in [IMAP-TLS], the registry will be
+ updated accordingly.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 94]
+
+RFC 3501 IMAPv4 March 2003
+
+
+Appendices
+
+A. Normative References
+
+ The following documents contain definitions or specifications that
+ are necessary to understand this document properly:
+ [ABNF] Crocker, D. and P. Overell, "Augmented BNF for
+ Syntax Specifications: ABNF", RFC 2234,
+ November 1997.
+
+ [ANONYMOUS] Newman, C., "Anonymous SASL Mechanism", RFC
+ 2245, November 1997.
+
+ [CHARSET] Freed, N. and J. Postel, "IANA Character Set
+ Registration Procedures", RFC 2978, October
+ 2000.
+
+ [DIGEST-MD5] Leach, P. and C. Newman, "Using Digest
+ Authentication as a SASL Mechanism", RFC 2831,
+ May 2000.
+
+ [DISPOSITION] Troost, R., Dorner, S. and K. Moore,
+ "Communicating Presentation Information in
+ Internet Messages: The Content-Disposition
+ Header", RFC 2183, August 1997.
+
+ [IMAP-TLS] Newman, C., "Using TLS with IMAP, POP3 and
+ ACAP", RFC 2595, June 1999.
+
+ [KEYWORDS] Bradner, S., "Key words for use in RFCs to
+ Indicate Requirement Levels", BCP 14, RFC 2119,
+ March 1997.
+
+ [LANGUAGE-TAGS] Alvestrand, H., "Tags for the Identification of
+ Languages", BCP 47, RFC 3066, January 2001.
+
+ [LOCATION] Palme, J., Hopmann, A. and N. Shelness, "MIME
+ Encapsulation of Aggregate Documents, such as
+ HTML (MHTML)", RFC 2557, March 1999.
+
+ [MD5] Myers, J. and M. Rose, "The Content-MD5 Header
+ Field", RFC 1864, October 1995.
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 95]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ [MIME-HDRS] Moore, K., "MIME (Multipurpose Internet Mail
+ Extensions) Part Three: Message Header
+ Extensions for Non-ASCII Text", RFC 2047,
+ November 1996.
+
+ [MIME-IMB] Freed, N. and N. Borenstein, "MIME
+ (Multipurpose Internet Mail Extensions) Part
+ One: Format of Internet Message Bodies", RFC
+ 2045, November 1996.
+
+ [MIME-IMT] Freed, N. and N. Borenstein, "MIME
+ (Multipurpose Internet Mail Extensions) Part
+ Two: Media Types", RFC 2046, November 1996.
+
+ [RFC-2822] Resnick, P., "Internet Message Format", RFC
+ 2822, April 2001.
+
+ [SASL] Myers, J., "Simple Authentication and Security
+ Layer (SASL)", RFC 2222, October 1997.
+
+ [TLS] Dierks, T. and C. Allen, "The TLS Protocol
+ Version 1.0", RFC 2246, January 1999.
+
+ [UTF-7] Goldsmith, D. and M. Davis, "UTF-7: A Mail-Safe
+ Transformation Format of Unicode", RFC 2152,
+ May 1997.
+
+ The following documents describe quality-of-implementation issues
+ that should be carefully considered when implementing this protocol:
+
+ [IMAP-IMPLEMENTATION] Leiba, B., "IMAP Implementation
+ Recommendations", RFC 2683, September 1999.
+
+ [IMAP-MULTIACCESS] Gahrns, M., "IMAP4 Multi-Accessed Mailbox
+ Practice", RFC 2180, July 1997.
+
+A.1 Informative References
+
+ The following documents describe related protocols:
+
+ [IMAP-DISC] Austein, R., "Synchronization Operations for
+ Disconnected IMAP4 Clients", Work in Progress.
+
+ [IMAP-MODEL] Crispin, M., "Distributed Electronic Mail
+ Models in IMAP4", RFC 1733, December 1994.
+
+
+
+
+
+
+Crispin Standards Track [Page 96]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ [ACAP] Newman, C. and J. Myers, "ACAP -- Application
+ Configuration Access Protocol", RFC 2244,
+ November 1997.
+
+ [SMTP] Klensin, J., "Simple Mail Transfer Protocol",
+ STD 10, RFC 2821, April 2001.
+
+ The following documents are historical or describe historical aspects
+ of this protocol:
+
+ [IMAP-COMPAT] Crispin, M., "IMAP4 Compatibility with
+ IMAP2bis", RFC 2061, December 1996.
+
+ [IMAP-HISTORICAL] Crispin, M., "IMAP4 Compatibility with IMAP2
+ and IMAP2bis", RFC 1732, December 1994.
+
+ [IMAP-OBSOLETE] Crispin, M., "Internet Message Access Protocol
+ - Obsolete Syntax", RFC 2062, December 1996.
+
+ [IMAP2] Crispin, M., "Interactive Mail Access Protocol
+ - Version 2", RFC 1176, August 1990.
+
+ [RFC-822] Crocker, D., "Standard for the Format of ARPA
+ Internet Text Messages", STD 11, RFC 822,
+ August 1982.
+
+ [RFC-821] Postel, J., "Simple Mail Transfer Protocol",
+ STD 10, RFC 821, August 1982.
+
+B. Changes from RFC 2060
+
+ 1) Clarify description of unique identifiers and their semantics.
+
+ 2) Fix the SELECT description to clarify that UIDVALIDITY is required
+ in the SELECT and EXAMINE responses.
+
+ 3) Added an example of a failing search.
+
+ 4) Correct store-att-flags: "#flag" should be "1#flag".
+
+ 5) Made search and section rules clearer.
+
+ 6) Correct the STORE example.
+
+ 7) Correct "BASE645" misspelling.
+
+ 8) Remove extraneous close parenthesis in example of two-part message
+ with text and BASE64 attachment.
+
+
+
+Crispin Standards Track [Page 97]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 9) Remove obsolete "MAILBOX" response from mailbox-data.
+
+ 10) A spurious "<" in the rule for mailbox-data was removed.
+
+ 11) Add CRLF to continue-req.
+
+ 12) Specifically exclude "]" from the atom in resp-text-code.
+
+ 13) Clarify that clients and servers should adhere strictly to the
+ protocol syntax.
+
+ 14) Emphasize in 5.2 that EXISTS can not be used to shrink a mailbox.
+
+ 15) Add NEWNAME to resp-text-code.
+
+ 16) Clarify that the empty string, not NIL, is used as arguments to
+ LIST.
+
+ 17) Clarify that NIL can be returned as a hierarchy delimiter for the
+ empty string mailbox name argument if the mailbox namespace is flat.
+
+ 18) Clarify that addr-mailbox and addr-name have RFC-2822 quoting
+ removed.
+
+ 19) Update UTF-7 reference.
+
+ 20) Fix example in 6.3.11.
+
+ 21) Clarify that non-existent UIDs are ignored.
+
+ 22) Update DISPOSITION reference.
+
+ 23) Expand state diagram.
+
+ 24) Clarify that partial fetch responses are only returned in
+ response to a partial fetch command.
+
+ 25) Add UIDNEXT response code. Correct UIDVALIDITY definition
+ reference.
+
+ 26) Further clarification of "can" vs. "MAY".
+
+ 27) Reference RFC-2119.
+
+ 28) Clarify that superfluous shifts are not permitted in modified
+ UTF-7.
+
+ 29) Clarify that there are no implicit shifts in modified UTF-7.
+
+
+
+Crispin Standards Track [Page 98]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 30) Clarify that "INBOX" in a mailbox name is always INBOX, even if
+ it is given as a string.
+
+ 31) Add missing open parenthesis in media-basic grammar rule.
+
+ 32) Correct attribute syntax in mailbox-data.
+
+ 33) Add UIDNEXT to EXAMINE responses.
+
+ 34) Clarify UNSEEN, PERMANENTFLAGS, UIDVALIDITY, and UIDNEXT
+ responses in SELECT and EXAMINE. They are required now, but weren't
+ in older versions.
+
+ 35) Update references with RFC numbers.
+
+ 36) Flush text-mime2.
+
+ 37) Clarify that modified UTF-7 names must be case-sensitive and that
+ violating the convention should be avoided.
+
+ 38) Correct UID FETCH example.
+
+ 39) Clarify UID FETCH, UID STORE, and UID SEARCH vs. untagged EXPUNGE
+ responses.
+
+ 40) Clarify the use of the word "convention".
+
+ 41) Clarify that a command is not "in progress" until it has been
+ fully received (specifically, that a command is not "in progress"
+ during command continuation negotiation).
+
+ 42) Clarify envelope defaulting.
+
+ 43) Clarify that SP means one and only one space character.
+
+ 44) Forbid silly states in LIST response.
+
+ 45) Clarify that the ENVELOPE, INTERNALDATE, RFC822*, BODY*, and UID
+ for a message is static.
+
+ 46) Add BADCHARSET response code.
+
+ 47) Update formal syntax to [ABNF] conventions.
+
+ 48) Clarify trailing hierarchy delimiter in CREATE semantics.
+
+ 49) Clarify that the "blank line" is the [RFC-2822] delimiting blank
+ line.
+
+
+
+Crispin Standards Track [Page 99]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 50) Clarify that RENAME should also create hierarchy as needed for
+ the command to complete.
+
+ 51) Fix body-ext-mpart to not require language if disposition
+ present.
+
+ 52) Clarify the RFC822.HEADER response.
+
+ 53) Correct missing space after charset astring in search.
+
+ 54) Correct missing quote for BADCHARSET in resp-text-code.
+
+ 55) Clarify that ALL, FAST, and FULL preclude any other data items
+ appearing.
+
+ 56) Clarify semantics of reference argument in LIST.
+
+ 57) Clarify that a null string for SEARCH HEADER X-FOO means any
+ message with a header line with a field-name of X-FOO regardless of
+ the text of the header.
+
+ 58) Specifically reserve 8-bit mailbox names for future use as UTF-8.
+
+ 59) It is not an error for the client to store a flag that is not in
+ the PERMANENTFLAGS list; however, the server will either ignore the
+ change or make the change in the session only.
+
+ 60) Correct/clarify the text regarding superfluous shifts.
+
+ 61) Correct typographic errors in the "Changes" section.
+
+ 62) Clarify that STATUS must not be used to check for new messages in
+ the selected mailbox
+
+ 63) Clarify LSUB behavior with "%" wildcard.
+
+ 64) Change AUTHORIZATION to AUTHENTICATE in section 7.5.
+
+ 65) Clarify description of multipart body type.
+
+ 66) Clarify that STORE FLAGS does not affect \Recent.
+
+ 67) Change "west" to "east" in description of timezone.
+
+ 68) Clarify that commands which break command pipelining must wait
+ for a completion result response.
+
+ 69) Clarify that EXAMINE does not affect \Recent.
+
+
+
+Crispin Standards Track [Page 100]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 70) Make description of MIME structure consistent.
+
+ 71) Clarify that date searches disregard the time and timezone of the
+ INTERNALDATE or Date: header. In other words, "ON 13-APR-2000" means
+ messages with an INTERNALDATE text which starts with "13-APR-2000",
+ even if timezone differential from the local timezone is sufficient
+ to move that INTERNALDATE into the previous or next day.
+
+ 72) Clarify that the header fetches don't add a blank line if one
+ isn't in the [RFC-2822] message.
+
+ 73) Clarify (in discussion of UIDs) that messages are immutable.
+
+ 74) Add an example of CHARSET searching.
+
+ 75) Clarify in SEARCH that keywords are a type of flag.
+
+ 76) Clarify the mandatory nature of the SELECT data responses.
+
+ 77) Add optional CAPABILITY response code in the initial OK or
+ PREAUTH.
+
+ 78) Add note that server can send an untagged CAPABILITY command as
+ part of the responses to AUTHENTICATE and LOGIN.
+
+ 79) Remove statement about it being unnecessary to issue a CAPABILITY
+ command more than once in a connection. That statement is no longer
+ true.
+
+ 80) Clarify that untagged EXPUNGE decrements the number of messages
+ in the mailbox.
+
+ 81) Fix definition of "body" (concatenation has tighter binding than
+ alternation).
+
+ 82) Add a new "Special Notes to Implementors" section with reference
+ to [IMAP-IMPLEMENTATION].
+
+ 83) Clarify that an untagged CAPABILITY response to an AUTHENTICATE
+ command should only be done if a security layer was not negotiated.
+
+ 84) Change the definition of atom to exclude "]". Update astring to
+ include "]" for compatibility with the past. Remove resp-text-atom.
+
+ 85) Remove NEWNAME. It can't work because mailbox names can be
+ literals and can include "]". Functionality can be addressed via
+ referrals.
+
+
+
+
+Crispin Standards Track [Page 101]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 86) Move modified UTF-7 rationale in order to have more logical
+ paragraph flow.
+
+ 87) Clarify UID uniqueness guarantees with the use of MUST.
+
+ 88) Note that clients should read response data until the connection
+ is closed instead of immediately closing on a BYE.
+
+ 89) Change RFC-822 references to RFC-2822.
+
+ 90) Clarify that RFC-2822 should be followed instead of RFC-822.
+
+ 91) Change recommendation of optional automatic capabilities in LOGIN
+ and AUTHENTICATE to use the CAPABILITY response code in the tagged
+ OK. This is more interoperable than an unsolicited untagged
+ CAPABILITY response.
+
+ 92) STARTTLS and AUTH=PLAIN are mandatory to implement; add
+ recommendations for other [SASL] mechanisms.
+
+ 93) Clarify that a "connection" (as opposed to "server" or "command")
+ is in one of the four states.
+
+ 94) Clarify that a failed or rejected command does not change state.
+
+ 95) Split references between normative and informative.
+
+ 96) Discuss authentication failure issues in security section.
+
+ 97) Clarify that a data item is not necessarily of only one data
+ type.
+
+ 98) Clarify that sequence ranges are independent of order.
+
+ 99) Change an example to clarify that superfluous shifts in
+ Modified-UTF7 can not be fixed just by omitting the shift. The
+ entire string must be recalculated.
+
+ 100) Change Envelope Structure definition since [RFC-2822] uses
+ "envelope" to refer to the [SMTP] envelope and not the envelope data
+ that appears in the [RFC-2822] header.
+
+ 101) Expand on RFC822.HEADER response data vs. BODY[HEADER].
+
+ 102) Clarify Logout state semantics, change ASCII art.
+
+ 103) Security changes to comply with IESG requirements.
+
+
+
+
+Crispin Standards Track [Page 102]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ 104) Add definition for body URI.
+
+ 105) Break sequence range definition into three rules, with rewritten
+ descriptions for each.
+
+ 106) Move STARTTLS and LOGINDISABLED here from [IMAP-TLS].
+
+ 107) Add IANA Considerations section.
+
+ 108) Clarify valid client assumptions for new message UIDs vs.
+ UIDNEXT.
+
+ 109) Clarify that changes to permanentflags affect concurrent
+ sessions as well as subsequent sessions.
+
+ 110) Clarify that authenticated state can be entered by the CLOSE
+ command.
+
+ 111) Emphasize that SELECT and EXAMINE are the exceptions to the rule
+ that a failing command does not change state.
+
+ 112) Clarify that newly-appended messages have the Recent flag set.
+
+ 113) Clarify that newly-copied messages SHOULD have the Recent flag
+ set.
+
+ 114) Clarify that UID commands always return the UID in FETCH
+ responses.
+
+C. Key Word Index
+
+ +FLAGS <flag list> (store command data item) ............... 59
+ +FLAGS.SILENT <flag list> (store command data item) ........ 59
+ -FLAGS <flag list> (store command data item) ............... 59
+ -FLAGS.SILENT <flag list> (store command data item) ........ 59
+ ALERT (response code) ...................................... 64
+ ALL (fetch item) ........................................... 55
+ ALL (search key) ........................................... 50
+ ANSWERED (search key) ...................................... 50
+ APPEND (command) ........................................... 45
+ AUTHENTICATE (command) ..................................... 27
+ BAD (response) ............................................. 66
+ BADCHARSET (response code) ................................. 64
+ BCC <string> (search key) .................................. 51
+ BEFORE <date> (search key) ................................. 51
+ BODY (fetch item) .......................................... 55
+ BODY (fetch result) ........................................ 73
+ BODY <string> (search key) ................................. 51
+
+
+
+Crispin Standards Track [Page 103]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ BODY.PEEK[<section>]<<partial>> (fetch item) ............... 57
+ BODYSTRUCTURE (fetch item) ................................. 57
+ BODYSTRUCTURE (fetch result) ............................... 74
+ BODY[<section>]<<origin octet>> (fetch result) ............. 74
+ BODY[<section>]<<partial>> (fetch item) .................... 55
+ BYE (response) ............................................. 67
+ Body Structure (message attribute) ......................... 12
+ CAPABILITY (command) ....................................... 24
+ CAPABILITY (response code) ................................. 64
+ CAPABILITY (response) ...................................... 68
+ CC <string> (search key) ................................... 51
+ CHECK (command) ............................................ 47
+ CLOSE (command) ............................................ 48
+ COPY (command) ............................................. 59
+ CREATE (command) ........................................... 34
+ DELETE (command) ........................................... 35
+ DELETED (search key) ....................................... 51
+ DRAFT (search key) ......................................... 51
+ ENVELOPE (fetch item) ...................................... 57
+ ENVELOPE (fetch result) .................................... 77
+ EXAMINE (command) .......................................... 33
+ EXISTS (response) .......................................... 71
+ EXPUNGE (command) .......................................... 48
+ EXPUNGE (response) ......................................... 72
+ Envelope Structure (message attribute) ..................... 12
+ FAST (fetch item) .......................................... 55
+ FETCH (command) ............................................ 54
+ FETCH (response) ........................................... 73
+ FLAGGED (search key) ....................................... 51
+ FLAGS (fetch item) ......................................... 57
+ FLAGS (fetch result) ....................................... 78
+ FLAGS (response) ........................................... 71
+ FLAGS <flag list> (store command data item) ................ 59
+ FLAGS.SILENT <flag list> (store command data item) ......... 59
+ FROM <string> (search key) ................................. 51
+ FULL (fetch item) .......................................... 55
+ Flags (message attribute) .................................. 11
+ HEADER (part specifier) .................................... 55
+ HEADER <field-name> <string> (search key) .................. 51
+ HEADER.FIELDS <header-list> (part specifier) ............... 55
+ HEADER.FIELDS.NOT <header-list> (part specifier) ........... 55
+ INTERNALDATE (fetch item) .................................. 57
+ INTERNALDATE (fetch result) ................................ 78
+ Internal Date (message attribute) .......................... 12
+ KEYWORD <flag> (search key) ................................ 51
+ Keyword (type of flag) ..................................... 11
+ LARGER <n> (search key) .................................... 51
+ LIST (command) ............................................. 40
+
+
+
+Crispin Standards Track [Page 104]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ LIST (response) ............................................ 69
+ LOGIN (command) ............................................ 30
+ LOGOUT (command) ........................................... 25
+ LSUB (command) ............................................. 43
+ LSUB (response) ............................................ 70
+ MAY (specification requirement term) ....................... 4
+ MESSAGES (status item) ..................................... 45
+ MIME (part specifier) ...................................... 56
+ MUST (specification requirement term) ...................... 4
+ MUST NOT (specification requirement term) .................. 4
+ Message Sequence Number (message attribute) ................ 10
+ NEW (search key) ........................................... 51
+ NO (response) .............................................. 66
+ NOOP (command) ............................................. 25
+ NOT <search-key> (search key) .............................. 52
+ OK (response) .............................................. 65
+ OLD (search key) ........................................... 52
+ ON <date> (search key) ..................................... 52
+ OPTIONAL (specification requirement term) .................. 4
+ OR <search-key1> <search-key2> (search key) ................ 52
+ PARSE (response code) ...................................... 64
+ PERMANENTFLAGS (response code) ............................. 64
+ PREAUTH (response) ......................................... 67
+ Permanent Flag (class of flag) ............................. 12
+ READ-ONLY (response code) .................................. 65
+ READ-WRITE (response code) ................................. 65
+ RECENT (response) .......................................... 72
+ RECENT (search key) ........................................ 52
+ RECENT (status item) ....................................... 45
+ RENAME (command) ........................................... 37
+ REQUIRED (specification requirement term) .................. 4
+ RFC822 (fetch item) ........................................ 57
+ RFC822 (fetch result) ...................................... 78
+ RFC822.HEADER (fetch item) ................................. 57
+ RFC822.HEADER (fetch result) ............................... 78
+ RFC822.SIZE (fetch item) ................................... 57
+ RFC822.SIZE (fetch result) ................................. 78
+ RFC822.TEXT (fetch item) ................................... 58
+ RFC822.TEXT (fetch result) ................................. 79
+ SEARCH (command) ........................................... 49
+ SEARCH (response) .......................................... 71
+ SEEN (search key) .......................................... 52
+ SELECT (command) ........................................... 31
+ SENTBEFORE <date> (search key) ............................. 52
+ SENTON <date> (search key) ................................. 52
+ SENTSINCE <date> (search key) .............................. 52
+ SHOULD (specification requirement term) .................... 4
+ SHOULD NOT (specification requirement term) ................ 4
+
+
+
+Crispin Standards Track [Page 105]
+
+RFC 3501 IMAPv4 March 2003
+
+
+ SINCE <date> (search key) .................................. 52
+ SMALLER <n> (search key) ................................... 52
+ STARTTLS (command) ......................................... 27
+ STATUS (command) ........................................... 44
+ STATUS (response) .......................................... 70
+ STORE (command) ............................................ 58
+ SUBJECT <string> (search key) .............................. 53
+ SUBSCRIBE (command) ........................................ 38
+ Session Flag (class of flag) ............................... 12
+ System Flag (type of flag) ................................. 11
+ TEXT (part specifier) ...................................... 56
+ TEXT <string> (search key) ................................. 53
+ TO <string> (search key) ................................... 53
+ TRYCREATE (response code) .................................. 65
+ UID (command) .............................................. 60
+ UID (fetch item) ........................................... 58
+ UID (fetch result) ......................................... 79
+ UID <sequence set> (search key) ............................ 53
+ UIDNEXT (response code) .................................... 65
+ UIDNEXT (status item) ...................................... 45
+ UIDVALIDITY (response code) ................................ 65
+ UIDVALIDITY (status item) .................................. 45
+ UNANSWERED (search key) .................................... 53
+ UNDELETED (search key) ..................................... 53
+ UNDRAFT (search key) ....................................... 53
+ UNFLAGGED (search key) ..................................... 53
+ UNKEYWORD <flag> (search key) .............................. 53
+ UNSEEN (response code) ..................................... 65
+ UNSEEN (search key) ........................................ 53
+ UNSEEN (status item) ....................................... 45
+ UNSUBSCRIBE (command) ...................................... 39
+ Unique Identifier (UID) (message attribute) ................ 8
+ X<atom> (command) .......................................... 62
+ [RFC-2822] Size (message attribute) ........................ 12
+ \Answered (system flag) .................................... 11
+ \Deleted (system flag) ..................................... 11
+ \Draft (system flag) ....................................... 11
+ \Flagged (system flag) ..................................... 11
+ \Marked (mailbox name attribute) ........................... 69
+ \Noinferiors (mailbox name attribute) ...................... 69
+ \Noselect (mailbox name attribute) ......................... 69
+ \Recent (system flag) ...................................... 11
+ \Seen (system flag) ........................................ 11
+ \Unmarked (mailbox name attribute) ......................... 69
+
+
+
+
+
+
+
+Crispin Standards Track [Page 106]
+
+RFC 3501 IMAPv4 March 2003
+
+
+Author's Address
+
+ Mark R. Crispin
+ Networks and Distributed Computing
+ University of Washington
+ 4545 15th Avenue NE
+ Seattle, WA 98105-4527
+
+ Phone: (206) 543-5762
+
+ EMail: MRC@CAC.Washington.EDU
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 107]
+
+RFC 3501 IMAPv4 March 2003
+
+
+Full Copyright Statement
+
+ Copyright (C) The Internet Society (2003). All Rights Reserved.
+
+ This document and translations of it may be copied and furnished to
+ others, and derivative works that comment on or otherwise explain it
+ or assist in its implementation may be prepared, copied, published
+ and distributed, in whole or in part, without restriction of any
+ kind, provided that the above copyright notice and this paragraph are
+ included on all such copies and derivative works. However, this
+ document itself may not be modified in any way, such as by removing
+ the copyright notice or references to the Internet Society or other
+ Internet organizations, except as needed for the purpose of
+ developing Internet standards in which case the procedures for
+ copyrights defined in the Internet Standards process must be
+ followed, or as required to translate it into languages other than
+ English.
+
+ The limited permissions granted above are perpetual and will not be
+ revoked by the Internet Society or its successors or assigns. v This
+ document and the information contained herein is provided on an "AS
+ IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK
+ FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+ LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL
+ NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY
+ OR FITNESS FOR A PARTICULAR PURPOSE.
+
+Acknowledgement
+
+ Funding for the RFC Editor function is currently provided by the
+ Internet Society.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Crispin Standards Track [Page 108]
+
diff --git a/vendor/github.com/mattermost/rsc/imap/sx.go b/vendor/github.com/mattermost/rsc/imap/sx.go
new file mode 100644
index 000000000..8b0e361fa
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/sx.go
@@ -0,0 +1,350 @@
+package imap
+
+import (
+ "bufio"
+ "bytes"
+ "fmt"
+ "io"
+ "log"
+ "strings"
+ "time"
+)
+
+type sxKind int
+
+const (
+ sxNone sxKind = iota
+ sxAtom
+ sxString
+ sxNumber
+ sxList
+)
+
+type sx struct {
+ kind sxKind
+ data []byte
+ number int64
+ sx []*sx
+}
+
+func rdsx(b *bufio.Reader) (*sx, error) {
+ x := &sx{kind: sxList}
+ for {
+ xx, err := rdsx1(b)
+ if err != nil {
+ return nil, err
+ }
+ if xx == nil {
+ break
+ }
+ x.sx = append(x.sx, xx)
+ }
+ return x, nil
+}
+
+func rdsx1(b *bufio.Reader) (*sx, error) {
+ c, err := b.ReadByte()
+ if c == ' ' {
+ c, err = b.ReadByte()
+ }
+ if c == '\r' {
+ c, err = b.ReadByte()
+ }
+ if err != nil {
+ return nil, err
+ }
+ if c == '\n' {
+ return nil, nil
+ }
+ if c == ')' { // end of list
+ b.UnreadByte()
+ return nil, nil
+ }
+ if c == '(' { // parenthesized list
+ x, err := rdsx(b)
+ if err != nil {
+ return nil, err
+ }
+ c, err = b.ReadByte()
+ if err != nil {
+ return nil, err
+ }
+ if c != ')' {
+ // oops! not good
+ b.UnreadByte()
+ }
+ return x, nil
+ }
+ if c == '{' { // length-prefixed string
+ n := 0
+ for {
+ c, _ = b.ReadByte()
+ if c < '0' || c > '9' {
+ break
+ }
+ n = n*10 + int(c) - '0'
+ }
+ if c != '}' {
+ // oops! not good
+ b.UnreadByte()
+ }
+ c, err = b.ReadByte()
+ if c != '\r' {
+ // oops! not good
+ }
+ c, err = b.ReadByte()
+ if c != '\n' {
+ // oops! not good
+ }
+ data := make([]byte, n)
+ if _, err := io.ReadFull(b, data); err != nil {
+ return nil, err
+ }
+ return &sx{kind: sxString, data: data}, nil
+ }
+ if c == '"' { // quoted string
+ var data []byte
+ for {
+ c, err = b.ReadByte()
+ if err != nil {
+ return nil, err
+ }
+ if c == '"' {
+ break
+ }
+ if c == '\\' {
+ c, _ = b.ReadByte()
+ }
+ data = append(data, c)
+ }
+ return &sx{kind: sxString, data: data}, nil
+ }
+ if '0' <= c && c <= '9' { // number
+ n := int64(c) - '0'
+ for {
+ c, err := b.ReadByte()
+ if err != nil {
+ return nil, err
+ }
+ if c < '0' || c > '9' {
+ break
+ }
+ n = n*10 + int64(c) - '0'
+ }
+ b.UnreadByte()
+ return &sx{kind: sxNumber, number: n}, nil
+ }
+
+ // atom
+ nbr := 0
+ var data []byte
+ data = append(data, c)
+ for {
+ c, err = b.ReadByte()
+ if err != nil {
+ return nil, err
+ }
+ if c <= ' ' || c == '(' || c == ')' || c == '{' || c == '}' {
+ break
+ }
+ if c == '[' {
+ // allow embedded brackets as in BODY[]
+ if data[0] == '[' {
+ break
+ }
+ nbr++
+ }
+ if c == ']' {
+ if nbr <= 0 {
+ break
+ }
+ nbr--
+ }
+ data = append(data, c)
+ }
+ if c != ' ' {
+ b.UnreadByte()
+ }
+ return &sx{kind: sxAtom, data: data}, nil
+}
+
+func (x *sx) ok() bool {
+ return len(x.sx) >= 2 && x.sx[1].kind == sxAtom && strings.EqualFold(string(x.sx[1].data), "ok")
+}
+
+func (x *sx) String() string {
+ var b bytes.Buffer
+ x.fmt(&b, true)
+ return b.String()
+}
+
+func (x *sx) fmt(b *bytes.Buffer, paren bool) {
+ if x == nil {
+ return
+ }
+ switch x.kind {
+ case sxAtom, sxString:
+ fmt.Fprintf(b, "%q", x.data)
+ case sxNumber:
+ fmt.Fprintf(b, "%d", x.number)
+ case sxList:
+ if paren {
+ b.WriteByte('(')
+ }
+ for i, xx := range x.sx {
+ if i > 0 {
+ b.WriteByte(' ')
+ }
+ xx.fmt(b, paren)
+ }
+ if paren {
+ b.WriteByte(')')
+ }
+ default:
+ b.WriteByte('?')
+ }
+}
+
+var bytesNIL = []byte("NIL")
+
+var fmtKind = []sxKind{
+ 'L': sxList,
+ 'S': sxString,
+ 'N': sxNumber,
+ 'A': sxAtom,
+}
+
+func (x *sx) match(format string) bool {
+ done := false
+ c := format[0]
+ for i := 0; i < len(x.sx); i++ {
+ if !done {
+ if i >= len(format) {
+ log.Printf("sxmatch: too short")
+ return false
+ }
+ if format[i] == '*' {
+ done = true
+ } else {
+ c = format[i]
+ }
+ }
+ xx := x.sx[i]
+ if xx.kind == sxAtom && xx.isNil() {
+ if c == 'L' {
+ xx.kind = sxList
+ xx.data = nil
+ } else if c == 'S' {
+ xx.kind = sxString
+ xx.data = nil
+ }
+ }
+ if xx.kind == sxAtom && c == 'S' {
+ xx.kind = sxString
+ }
+ if xx.kind != fmtKind[c] {
+ log.Printf("sxmatch: %s not %c", xx, c)
+ return false
+ }
+ }
+ if len(format) > len(x.sx) {
+ log.Printf("sxmatch: too long")
+ return false
+ }
+ return true
+}
+
+func (x *sx) isAtom(name string) bool {
+ if x == nil || x.kind != sxAtom {
+ return false
+ }
+ data := x.data
+ n := len(name)
+ if n > 0 && name[n-1] == '[' {
+ i := bytes.IndexByte(data, '[')
+ if i < 0 {
+ return false
+ }
+ data = data[:i]
+ name = name[:n-1]
+ }
+ for i := 0; i < len(name); i++ {
+ if i >= len(data) || lwr(rune(data[i])) != lwr(rune(name[i])) {
+ return false
+ }
+ }
+ return len(name) == len(data)
+}
+
+func (x *sx) isString() bool {
+ if x.isNil() {
+ return true
+ }
+ if x.kind == sxAtom {
+ x.kind = sxString
+ }
+ return x.kind == sxString
+}
+
+func (x *sx) isNumber() bool {
+ return x.kind == sxNumber
+}
+
+func (x *sx) isNil() bool {
+ return x == nil ||
+ x.kind == sxList && len(x.sx) == 0 ||
+ x.kind == sxAtom && bytes.Equal(x.data, bytesNIL)
+}
+
+func (x *sx) isList() bool {
+ return x.isNil() || x.kind == sxList
+}
+
+func (x *sx) parseFlags() Flags {
+ if x.kind != sxList {
+ log.Printf("malformed flags: %s", x)
+ return 0
+ }
+
+ f := Flags(0)
+SX:
+ for _, xx := range x.sx {
+ if xx.kind != sxAtom {
+ continue
+ }
+ for i, name := range flagNames {
+ if xx.isAtom(name) {
+ f |= 1 << uint(i)
+ continue SX
+ }
+ }
+ if Debug {
+ log.Printf("unknown flag: %v", xx)
+ }
+ }
+ return f
+}
+
+func (x *sx) parseDate() time.Time {
+ if x.kind != sxString {
+ log.Printf("malformed date: %s", x)
+ return time.Time{}
+ }
+
+ t, err := time.Parse("02-Jan-2006 15:04:05 -0700", string(x.data))
+ if err != nil {
+ log.Printf("malformed date: %s (%s)", x, err)
+ }
+ return t
+}
+
+func (x *sx) nstring() string {
+ return string(x.nbytes())
+}
+
+func (x *sx) nbytes() []byte {
+ if x.isNil() {
+ return nil
+ }
+ return x.data
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/sx_test.go b/vendor/github.com/mattermost/rsc/imap/sx_test.go
new file mode 100644
index 000000000..9644209ca
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/sx_test.go
@@ -0,0 +1,60 @@
+package imap
+
+import (
+ "bufio"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+var sxTests = []struct {
+ in string
+ out *sx
+}{
+ {"1234", &sx{kind: sxNumber, number: 1234}},
+ {"hello", &sx{kind: sxAtom, data: []byte("hello")}},
+ {"hello[world]", &sx{kind: sxAtom, data: []byte("hello[world]")}},
+ {`"h\\ello"`, &sx{kind: sxString, data: []byte(`h\ello`)}},
+ {"{6}\r\nh\\ello", &sx{kind: sxString, data: []byte(`h\ello`)}},
+ {`(hello "world" (again) ())`,
+ &sx{
+ kind: sxList,
+ sx: []*sx{
+ &sx{
+ kind: sxAtom,
+ data: []byte("hello"),
+ },
+ &sx{
+ kind: sxString,
+ data: []byte("world"),
+ },
+ &sx{
+ kind: sxList,
+ sx: []*sx{
+ &sx{
+ kind: sxAtom,
+ data: []byte("again"),
+ },
+ },
+ },
+ &sx{
+ kind: sxList,
+ },
+ },
+ },
+ },
+}
+
+func TestSx(t *testing.T) {
+ for _, tt := range sxTests {
+ b := bufio.NewReader(strings.NewReader(tt.in + "\n"))
+ sx, err := rdsx1(b)
+ if err != nil {
+ t.Errorf("parse %s: %v", tt.in, err)
+ continue
+ }
+ if !reflect.DeepEqual(sx, tt.out) {
+ t.Errorf("rdsx1(%s) = %v, want %v", tt.in, sx, tt.out)
+ }
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/imap/tcs.go b/vendor/github.com/mattermost/rsc/imap/tcs.go
new file mode 100644
index 000000000..6a0939e01
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/imap/tcs.go
@@ -0,0 +1,602 @@
+package imap
+
+// NOTE(rsc): These belong elsewhere but the existing charset
+// packages seem too complicated.
+
+var tab_iso8859_1 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
+}
+
+var tab_iso8859_2 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x0104, 0x02d8, 0x0141, 0x00a4, 0x013d, 0x015a, 0x00a7,
+ 0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b,
+ 0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7,
+ 0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c,
+ 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7,
+ 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e,
+ 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7,
+ 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df,
+ 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7,
+ 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f,
+ 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7,
+ 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9,
+}
+
+var tab_iso8859_3 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x0126, 0x02d8, 0x00a3, 0x00a4, -1, 0x0124, 0x00a7,
+ 0x00a8, 0x0130, 0x015e, 0x011e, 0x0134, 0x00ad, -1, 0x017b,
+ 0x00b0, 0x0127, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x0125, 0x00b7,
+ 0x00b8, 0x0131, 0x015f, 0x011f, 0x0135, 0x00bd, -1, 0x017c,
+ 0x00c0, 0x00c1, 0x00c2, -1, 0x00c4, 0x010a, 0x0108, 0x00c7,
+ 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
+ -1, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x0120, 0x00d6, 0x00d7,
+ 0x011c, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x016c, 0x015c, 0x00df,
+ 0x00e0, 0x00e1, 0x00e2, -1, 0x00e4, 0x010b, 0x0109, 0x00e7,
+ 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
+ -1, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x0121, 0x00f6, 0x00f7,
+ 0x011d, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x016d, 0x015d, 0x02d9,
+}
+
+var tab_iso8859_4 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x0104, 0x0138, 0x0156, 0x00a4, 0x0128, 0x013b, 0x00a7,
+ 0x00a8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00ad, 0x017d, 0x00af,
+ 0x00b0, 0x0105, 0x02db, 0x0157, 0x00b4, 0x0129, 0x013c, 0x02c7,
+ 0x00b8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014a, 0x017e, 0x014b,
+ 0x0100, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x012e,
+ 0x010c, 0x00c9, 0x0118, 0x00cb, 0x0116, 0x00cd, 0x00ce, 0x012a,
+ 0x0110, 0x0145, 0x014c, 0x0136, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
+ 0x00d8, 0x0172, 0x00da, 0x00db, 0x00dc, 0x0168, 0x016a, 0x00df,
+ 0x0101, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x012f,
+ 0x010d, 0x00e9, 0x0119, 0x00eb, 0x0117, 0x00ed, 0x00ee, 0x012b,
+ 0x0111, 0x0146, 0x014d, 0x0137, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
+ 0x00f8, 0x0173, 0x00fa, 0x00fb, 0x00fc, 0x0169, 0x016b, 0x02d9,
+}
+
+var tab_iso8859_5 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407,
+ 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x00ad, 0x040e, 0x040f,
+ 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
+ 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
+ 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
+ 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f,
+ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
+ 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
+ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
+ 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f,
+ 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457,
+ 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x00a7, 0x045e, 0x045f,
+}
+
+var tab_iso8859_6 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, -1, -1, -1, 0x00a4, -1, -1, -1,
+ -1, -1, -1, -1, 0x060c, 0x00ad, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, 0x061b, -1, -1, -1, 0x061f,
+ -1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
+ 0x0628, 0x0629, 0x062a, 0x062b, 0x062c, 0x062d, 0x062e, 0x062f,
+ 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x0637,
+ 0x0638, 0x0639, 0x063a, -1, -1, -1, -1, -1,
+ 0x0640, 0x0641, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647,
+ 0x0648, 0x0649, 0x064a, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f,
+ 0x0650, 0x0651, 0x0652, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+}
+
+var tab_iso8859_7 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x2018, 0x2019, 0x00a3, -1, -1, 0x00a6, 0x00a7,
+ 0x00a8, 0x00a9, -1, 0x00ab, 0x00ac, 0x00ad, -1, 0x2015,
+ 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x0384, 0x0385, 0x0386, 0x00b7,
+ 0x0388, 0x0389, 0x038a, 0x00bb, 0x038c, 0x00bd, 0x038e, 0x038f,
+ 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
+ 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f,
+ 0x03a0, 0x03a1, -1, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7,
+ 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
+ 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7,
+ 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
+ 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7,
+ 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, -1,
+}
+
+var tab_iso8859_8 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, -1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
+ 0x00a8, 0x00a9, 0x00d7, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x203e,
+ 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
+ 0x00b8, 0x00b9, 0x00f7, 0x00bb, 0x00bc, 0x00bd, 0x00be, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, 0x2017,
+ 0x05d0, 0x05d1, 0x05d2, 0x05d3, 0x05d4, 0x05d5, 0x05d6, 0x05d7,
+ 0x05d8, 0x05d9, 0x05da, 0x05db, 0x05dc, 0x05dd, 0x05de, 0x05df,
+ 0x05e0, 0x05e1, 0x05e2, 0x05e3, 0x05e4, 0x05e5, 0x05e6, 0x05e7,
+ 0x05e8, 0x05e9, 0x05ea, -1, -1, -1, -1, -1,
+}
+
+var tab_iso8859_9 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
+ 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
+ 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
+ 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
+ 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
+ 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
+ 0x011e, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
+ 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0130, 0x015e, 0x00df,
+ 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
+ 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
+ 0x011f, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
+ 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0131, 0x015f, 0x00ff,
+}
+
+var tab_iso8859_10 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0x00a0, 0x0104, 0x0112, 0x0122, 0x012a, 0x0128, 0x0136, 0x00a7,
+ 0x013b, 0x0110, 0x0160, 0x0166, 0x017d, 0x00ad, 0x016a, 0x014a,
+ 0x00b0, 0x0105, 0x0113, 0x0123, 0x012b, 0x0129, 0x0137, 0x00b7,
+ 0x013c, 0x0110, 0x0161, 0x0167, 0x017e, 0x2014, 0x016b, 0x014b,
+ 0x0100, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x012e,
+ 0x010c, 0x00c9, 0x0118, 0x00cb, 0x0116, 0x00cd, 0x00ce, 0x00cf,
+ 0x00d0, 0x0145, 0x014c, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x0168,
+ 0x00d8, 0x0172, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
+ 0x0101, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x012f,
+ 0x010d, 0x00e9, 0x0119, 0x00eb, 0x0117, 0x00ed, 0x00ee, 0x00ef,
+ 0x00f0, 0x0146, 0x014d, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x0169,
+ 0x00f8, 0x0173, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x0138,
+}
+
+var tab_iso8859_15 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0x20ac, 0xa5, 0x0160, 0xa7, 0x0161, 0xa9, 0xaa, 0xab, 0xac, 0xad,
+ 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0x017d, 0xb5, 0xb6, 0xb7, 0x017e, 0xb9, 0xba, 0xbb,
+ 0x0152, 0x0153, 0x0178, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
+ 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
+ 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
+ 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
+}
+
+var tab_koi8 = [256]rune{
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+ 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
+ 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e,
+ 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
+ 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a,
+ 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
+ 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
+ 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
+ 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a,
+}
+
+var tab_cp1250 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, -1, 0x201E, 0x2026, 0x2020, 0x2021,
+ -1, 0x2030, 0x0160, 0x2039, 0x015A, 0x0164, 0x017D, 0x0179,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ -1, 0x2122, 0x0161, 0x203A, 0x015B, 0x0165, 0x017E, 0x017A,
+ 0x00A0, 0x02C7, 0x02D8, 0x0141, 0x00A4, 0x0104, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, 0x015E, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x017B,
+ 0x00B0, 0x00B1, 0x02DB, 0x0142, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00B8, 0x0105, 0x015F, 0x00BB, 0x013D, 0x02DD, 0x013E, 0x017C,
+ 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
+ 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
+ 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
+ 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
+ 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
+ 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
+ 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
+ 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9,
+}
+var tab_cp1251 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
+ 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ -1, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
+ 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7,
+ 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
+ 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7,
+ 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,
+ 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
+ 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
+ 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
+ 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
+ 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
+ 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
+ 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
+ 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
+}
+var tab_cp1252 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, -1, 0x017D, -1,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, -1, 0x017E, 0x0178,
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
+ 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
+ 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
+ 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
+ 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
+ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
+ 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
+ 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF,
+}
+var tab_cp1253 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ -1, 0x2030, -1, 0x2039, -1, -1, -1, -1,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ -1, 0x2122, -1, 0x203A, -1, -1, -1, -1,
+ 0x00A0, 0x0385, 0x0386, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, -1, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x2015,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x00B5, 0x00B6, 0x00B7,
+ 0x0388, 0x0389, 0x038A, 0x00BB, 0x038C, 0x00BD, 0x038E, 0x038F,
+ 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397,
+ 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F,
+ 0x03A0, 0x03A1, -1, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7,
+ 0x03A8, 0x03A9, 0x03AA, 0x03AB, 0x03AC, 0x03AD, 0x03AE, 0x03AF,
+ 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7,
+ 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF,
+ 0x03C0, 0x03C1, 0x03C2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7,
+ 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, -1,
+}
+var tab_cp1254 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, -1, -1, -1,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, -1, -1, 0x0178,
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
+ 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
+ 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
+ 0x011E, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
+ 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0130, 0x015E, 0x00DF,
+ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
+ 0x011F, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
+ 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0131, 0x015F, 0x00FF,
+}
+var tab_cp1255 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x02C6, 0x2030, -1, 0x2039, -1, -1, -1, -1,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ 0x02DC, 0x2122, -1, 0x203A, -1, -1, -1, -1,
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AA, 0x00A5, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00B8, 0x00B9, 0x00F7, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
+ 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
+ 0x05B8, 0x05B9, -1, 0x05BB, 0x05BC, 0x05BD, 0x05BE, 0x05BF,
+ 0x05C0, 0x05C1, 0x05C2, 0x05C3, 0x05F0, 0x05F1, 0x05F2, 0x05F3,
+ 0x05F4, -1, -1, -1, -1, -1, -1, -1,
+ 0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7,
+ 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
+ 0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7,
+ 0x05E8, 0x05E9, 0x05EA, -1, -1, 0x200E, 0x200F, -1,
+}
+var tab_cp1256 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, 0x067E, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x02C6, 0x2030, 0x0679, 0x2039, 0x0152, 0x0686, 0x0698, 0x0688,
+ 0x06AF, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ 0x06A9, 0x2122, 0x0691, 0x203A, 0x0153, 0x200C, 0x200D, 0x06BA,
+ 0x00A0, 0x060C, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, 0x06BE, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00B8, 0x00B9, 0x061B, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x061F,
+ 0x06C1, 0x0621, 0x0622, 0x0623, 0x0624, 0x0625, 0x0626, 0x0627,
+ 0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F,
+ 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x0636, 0x00D7,
+ 0x0637, 0x0638, 0x0639, 0x063A, 0x0640, 0x0641, 0x0642, 0x0643,
+ 0x00E0, 0x0644, 0x00E2, 0x0645, 0x0646, 0x0647, 0x0648, 0x00E7,
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0649, 0x064A, 0x00EE, 0x00EF,
+ 0x064B, 0x064C, 0x064D, 0x064E, 0x00F4, 0x064F, 0x0650, 0x00F7,
+ 0x0651, 0x00F9, 0x0652, 0x00FB, 0x00FC, 0x200E, 0x200F, 0x06D2,
+}
+var tab_cp1257 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, -1, 0x201E, 0x2026, 0x2020, 0x2021,
+ -1, 0x2030, -1, 0x2039, -1, 0x00A8, 0x02C7, 0x00B8,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ -1, 0x2122, -1, 0x203A, -1, 0x00AF, 0x02DB, -1,
+ 0x00A0, -1, 0x00A2, 0x00A3, 0x00A4, -1, 0x00A6, 0x00A7,
+ 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00F8, 0x00B9, 0x0157, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00E6,
+ 0x0104, 0x012E, 0x0100, 0x0106, 0x00C4, 0x00C5, 0x0118, 0x0112,
+ 0x010C, 0x00C9, 0x0179, 0x0116, 0x0122, 0x0136, 0x012A, 0x013B,
+ 0x0160, 0x0143, 0x0145, 0x00D3, 0x014C, 0x00D5, 0x00D6, 0x00D7,
+ 0x0172, 0x0141, 0x015A, 0x016A, 0x00DC, 0x017B, 0x017D, 0x00DF,
+ 0x0105, 0x012F, 0x0101, 0x0107, 0x00E4, 0x00E5, 0x0119, 0x0113,
+ 0x010D, 0x00E9, 0x017A, 0x0117, 0x0123, 0x0137, 0x012B, 0x013C,
+ 0x0161, 0x0144, 0x0146, 0x00F3, 0x014D, 0x00F5, 0x00F6, 0x00F7,
+ 0x0173, 0x0142, 0x015B, 0x016B, 0x00FC, 0x017C, 0x017E, 0x02D9,
+}
+var tab_cp1258 = [256]rune{
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
+ 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
+ 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
+ 0x20AC, -1, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x02C6, 0x2030, -1, 0x2039, 0x0152, -1, -1, -1,
+ -1, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ 0x02DC, 0x2122, -1, 0x203A, 0x0153, -1, -1, 0x0178,
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
+ 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
+ 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
+ 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
+ 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x0300, 0x00CD, 0x00CE, 0x00CF,
+ 0x0110, 0x00D1, 0x0309, 0x00D3, 0x00D4, 0x01A0, 0x00D6, 0x00D7,
+ 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x01AF, 0x0303, 0x00DF,
+ 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x0301, 0x00ED, 0x00EE, 0x00EF,
+ 0x0111, 0x00F1, 0x0323, 0x00F3, 0x00F4, 0x01A1, 0x00F6, 0x00F7,
+ 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x01B0, 0x20AB, 0x00FF,
+}
diff --git a/vendor/github.com/mattermost/rsc/keychain/doc.go b/vendor/github.com/mattermost/rsc/keychain/doc.go
new file mode 100644
index 000000000..e7e21fd2e
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/keychain/doc.go
@@ -0,0 +1,28 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package keychain implements access to the passwords and other keys
+// stored in the system-provided keychain.
+package keychain
+
+// BUG(rsc): Package keychain is only implemented on OS X.
+
+import (
+ "fmt"
+)
+
+// UserPasswd returns the user name and password for authenticating
+// to the named server. If the user argument is non-empty, UserPasswd
+// restricts its search to passwords for the named user.
+func UserPasswd(server, preferredUser string) (user, passwd string, err error) {
+ user, passwd, err = userPasswd(server, preferredUser)
+ if err != nil {
+ if preferredUser != "" {
+ err = fmt.Errorf("loading password for %s@%s: %v", preferredUser, server, err)
+ } else {
+ err = fmt.Errorf("loading password for %s: %v", server, err)
+ }
+ }
+ return
+}
diff --git a/vendor/github.com/mattermost/rsc/keychain/mac.go b/vendor/github.com/mattermost/rsc/keychain/mac.go
new file mode 100644
index 000000000..523579169
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/keychain/mac.go
@@ -0,0 +1,107 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package keychain
+
+/*
+#include <CoreFoundation/CoreFoundation.h>
+#include <Security/Security.h>
+#include <CoreServices/CoreServices.h>
+
+#cgo LDFLAGS: -framework CoreFoundation -framework Security
+
+static char*
+mac2c(CFStringRef s)
+{
+ char *p;
+ int n;
+
+ n = CFStringGetLength(s)*8;
+ p = malloc(n);
+ CFStringGetCString(s, p, n, kCFStringEncodingUTF8);
+ return p;
+}
+
+void
+keychain_getpasswd(char *user0, char *server, char **user, char **passwd, char **error)
+{
+ OSStatus st;
+ UInt32 len;
+ void *data;
+ SecKeychainItemRef it;
+ CFStringRef str;
+
+ *user = NULL;
+ *passwd = NULL;
+ *error = NULL;
+
+ st = SecKeychainFindInternetPassword(
+ NULL, // default keychain
+ strlen(server), server,
+ 0, NULL, // security domain
+ strlen(user0), user0, // account name
+ 0, NULL, // path
+ 0, // port
+ 0, // protocol type
+ kSecAuthenticationTypeDefault,
+ &len,
+ &data,
+ &it);
+ if(st != 0) {
+ str = SecCopyErrorMessageString(st, NULL);
+ *error = mac2c(str);
+ CFRelease(str);
+ return;
+ }
+ *passwd = malloc(len+1);
+ memmove(*passwd, data, len);
+ (*passwd)[len] = '\0';
+ SecKeychainItemFreeContent(NULL, data);
+
+ SecKeychainAttribute attr = {kSecAccountItemAttr, 0, NULL};
+ SecKeychainAttributeList attrl = {1, &attr};
+ st = SecKeychainItemCopyContent(
+ it,
+ NULL,
+ &attrl,
+ 0, NULL);
+ if(st != 0) {
+ str = SecCopyErrorMessageString(st, NULL);
+ *error = mac2c(str);
+ CFRelease(str);
+ return;
+ }
+ data = attr.data;
+ len = attr.length;
+ *user = malloc(len+1);
+ memmove(*user, data, len);
+ (*user)[len] = '\0';
+ SecKeychainItemFreeContent(&attrl, NULL);
+}
+*/
+import "C"
+
+import (
+ "errors"
+ "unsafe"
+)
+
+func userPasswd(server, user string) (user1, passwd string, err error) {
+ cServer := C.CString(server)
+ cUser := C.CString(user)
+ defer C.free(unsafe.Pointer(cServer))
+ defer C.free(unsafe.Pointer(cUser))
+
+ var cPasswd, cError *C.char
+ C.keychain_getpasswd(cUser, cServer, &cUser, &cPasswd, &cError)
+ defer C.free(unsafe.Pointer(cUser))
+ defer C.free(unsafe.Pointer(cPasswd))
+ defer C.free(unsafe.Pointer(cError))
+
+ if cError != nil {
+ return "", "", errors.New(C.GoString(cError))
+ }
+
+ return C.GoString(cUser), C.GoString(cPasswd), nil
+}
diff --git a/vendor/github.com/mattermost/rsc/mbta/Passages.pb b/vendor/github.com/mattermost/rsc/mbta/Passages.pb
new file mode 100644
index 000000000..5eb9e900e
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/mbta/Passages.pb
Binary files differ
diff --git a/vendor/github.com/mattermost/rsc/mbta/Vehicles.pb b/vendor/github.com/mattermost/rsc/mbta/Vehicles.pb
new file mode 100644
index 000000000..98d90c800
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/mbta/Vehicles.pb
Binary files differ
diff --git a/vendor/github.com/mattermost/rsc/mbta/mbta.go b/vendor/github.com/mattermost/rsc/mbta/mbta.go
new file mode 100644
index 000000000..c8c68e6e2
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/mbta/mbta.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "os"
+
+ "code.google.com/p/goprotobuf/proto"
+ "github.com/mattermost/rsc/gtfs"
+)
+
+func main() {
+ log.SetFlags(0)
+ if len(os.Args) != 2 {
+ log.Fatal("usage: mbta file.pb")
+ }
+ pb, err := ioutil.ReadFile(os.Args[1])
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var feed gtfs.FeedMessage
+ if err := proto.Unmarshal(pb, &feed); err != nil {
+ log.Fatal(err)
+ }
+
+ proto.MarshalText(os.Stdout, &feed)
+}
diff --git a/vendor/github.com/mattermost/rsc/mkapp b/vendor/github.com/mattermost/rsc/mkapp
new file mode 100755
index 000000000..1a52931f1
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/mkapp
@@ -0,0 +1,51 @@
+#!/bin/bash
+# Copyright 2012 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+# Run this script in your app root directory, the one containing
+# the app.yaml file. Then point appcfg.py or dev_appserver.py
+# at './tmp' instead of '.'.
+#
+# It creates a symlink forest in a subdirectory named 'tmp' that
+# includes all the files from packages under your app root as
+# well as any packages from your $GOPATH that are needed by
+# the app packages. This makes deployment to App Engine
+# bring in just the packages you need, without manual chasing
+# of dependencies. Perhaps some day appcfg.py and dev_appserver.py
+# will work this way by default.
+
+set -e
+rm -rf tmp
+dirs=$(find . -type d)
+mkdir tmp
+
+for i in *
+do
+ case "$i" in
+ tmp | *.go)
+ ;;
+ *)
+ ln -s ../$i tmp/$i
+ esac
+done
+
+# Like App Engine
+export GOOS=linux
+export GOARCH=amd64
+
+mkdir tmp/_go_top
+cp $(go list -f '{{range .GoFiles}}{{.}} {{end}}') tmp/_go_top
+
+dirs=$(go list -e -f '{{if not .Standard}}{{.ImportPath}} {{end}}' $(go list -f '{{range .Deps}}{{.}} {{end}}' $dirs))
+for import in $dirs
+do
+ case "$import" in
+ appengine | appengine/*)
+ ;;
+ *)
+ mkdir -p tmp/$import
+ files=$(go list -f '{{range .GoFiles}}{{$.Dir}}/{{.}} {{end}}' $import)
+ ln -s $files tmp/$import 2>&1 | grep -v 'is a directory' || true # ignore subdirectory warnings
+ esac
+done
diff --git a/vendor/github.com/mattermost/rsc/plist/plist.go b/vendor/github.com/mattermost/rsc/plist/plist.go
new file mode 100644
index 000000000..1c76bface
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/plist/plist.go
@@ -0,0 +1,179 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package plist implements parsing of Apple plist files.
+package plist
+
+import (
+ "bytes"
+ "fmt"
+ "reflect"
+ "strconv"
+)
+
+func next(data []byte) (skip, tag, rest []byte) {
+ i := bytes.IndexByte(data, '<')
+ if i < 0 {
+ return data, nil, nil
+ }
+ j := bytes.IndexByte(data[i:], '>')
+ if j < 0 {
+ return data, nil, nil
+ }
+ j += i + 1
+ return data[:i], data[i:j], data[j:]
+}
+
+func Unmarshal(data []byte, v interface{}) error {
+ _, tag, data := next(data)
+ if !bytes.HasPrefix(tag, []byte("<plist")) {
+ return fmt.Errorf("not a plist")
+ }
+
+ data, err := unmarshalValue(data, reflect.ValueOf(v))
+ if err != nil {
+ return err
+ }
+ _, tag, data = next(data)
+ if !bytes.Equal(tag, []byte("</plist>")) {
+ return fmt.Errorf("junk on end of plist")
+ }
+ return nil
+}
+
+func unmarshalValue(data []byte, v reflect.Value) (rest []byte, err error) {
+ _, tag, data := next(data)
+ if tag == nil {
+ return nil, fmt.Errorf("unexpected end of data")
+ }
+
+ if v.Kind() == reflect.Ptr {
+ if v.IsNil() {
+ v.Set(reflect.New(v.Type().Elem()))
+ }
+ v = v.Elem()
+ }
+
+ switch string(tag) {
+ case "<dict>":
+ t := v.Type()
+ if v.Kind() != reflect.Struct {
+ return nil, fmt.Errorf("cannot unmarshal <dict> into non-struct %s", v.Type())
+ }
+ Dict:
+ for {
+ _, tag, data = next(data)
+ if len(tag) == 0 {
+ return nil, fmt.Errorf("eof inside <dict>")
+ }
+ if string(tag) == "</dict>" {
+ break
+ }
+ if string(tag) != "<key>" {
+ return nil, fmt.Errorf("unexpected tag %s inside <dict>", tag)
+ }
+ var body []byte
+ body, tag, data = next(data)
+ if len(tag) == 0 {
+ return nil, fmt.Errorf("eof inside <dict>")
+ }
+ if string(tag) != "</key>" {
+ return nil, fmt.Errorf("unexpected tag %s inside <dict>", tag)
+ }
+ name := string(body)
+ var i int
+ for i = 0; i < t.NumField(); i++ {
+ f := t.Field(i)
+ if f.Name == name || f.Tag.Get("plist") == name {
+ data, err = unmarshalValue(data, v.Field(i))
+ continue Dict
+ }
+ }
+ data, err = skipValue(data)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return data, nil
+
+ case "<array>":
+ t := v.Type()
+ if v.Kind() != reflect.Slice {
+ return nil, fmt.Errorf("cannot unmarshal <array> into non-slice %s", v.Type())
+ }
+ for {
+ _, tag, rest := next(data)
+ if len(tag) == 0 {
+ return nil, fmt.Errorf("eof inside <array>")
+ }
+ if string(tag) == "</array>" {
+ data = rest
+ break
+ }
+ elem := reflect.New(t.Elem()).Elem()
+ data, err = unmarshalValue(data, elem)
+ if err != nil {
+ return nil, err
+ }
+ v.Set(reflect.Append(v, elem))
+ }
+ return data, nil
+
+ case "<string>":
+ if v.Kind() != reflect.String {
+ return nil, fmt.Errorf("cannot unmarshal <string> into non-string %s", v.Type())
+ }
+ body, etag, data := next(data)
+ if len(etag) == 0 {
+ return nil, fmt.Errorf("eof inside <string>")
+ }
+ if string(etag) != "</string>" {
+ return nil, fmt.Errorf("expected </string> but got %s", etag)
+ }
+ v.SetString(string(body)) // TODO: unescape
+ return data, nil
+
+ case "<integer>":
+ if v.Kind() != reflect.Int {
+ return nil, fmt.Errorf("cannot unmarshal <integer> into non-int %s", v.Type())
+ }
+ body, etag, data := next(data)
+ if len(etag) == 0 {
+ return nil, fmt.Errorf("eof inside <integer>")
+ }
+ if string(etag) != "</integer>" {
+ return nil, fmt.Errorf("expected </integer> but got %s", etag)
+ }
+ i, err := strconv.Atoi(string(body))
+ if err != nil {
+ return nil, fmt.Errorf("non-integer in <integer> tag: %s", body)
+ }
+ v.SetInt(int64(i))
+ return data, nil
+ }
+ return nil, fmt.Errorf("unexpected tag %s", tag)
+}
+
+func skipValue(data []byte) (rest []byte, err error) {
+ n := 0
+ for {
+ var tag []byte
+ _, tag, data = next(data)
+ if len(tag) == 0 {
+ return nil, fmt.Errorf("unexpected eof")
+ }
+ if tag[1] == '/' {
+ if n == 0 {
+ return nil, fmt.Errorf("unexpected closing tag")
+ }
+ n--
+ if n == 0 {
+ break
+ }
+ } else {
+ n++
+ }
+ }
+ return data, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/plist/plist_test.go b/vendor/github.com/mattermost/rsc/plist/plist_test.go
new file mode 100644
index 000000000..42f496c67
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/plist/plist_test.go
@@ -0,0 +1,110 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package plist
+
+import (
+ "reflect"
+ "testing"
+)
+
+var thePlist = `<plist version="1.0">
+ <dict>
+ <key>BucketUUID</key>
+ <string>C218A47D-DAFB-4476-9C67-597E556D7D8A</string>
+ <key>BucketName</key>
+ <string>rsc</string>
+ <key>ComputerUUID</key>
+ <string>E7859547-BB9C-41C0-871E-858A0526BAE7</string>
+ <key>LocalPath</key>
+ <string>/Users/rsc</string>
+ <key>LocalMountPoint</key>
+ <string>/Users</string>
+ <key>IgnoredRelativePaths</key>
+ <array>
+ <string>/.Trash</string>
+ <string>/go/pkg</string>
+ <string>/go1/pkg</string>
+ <string>/Library/Caches</string>
+ </array>
+ <key>Excludes</key>
+ <dict>
+ <key>excludes</key>
+ <array>
+ <dict>
+ <key>type</key>
+ <integer>2</integer>
+ <key>text</key>
+ <string>.unison.</string>
+ </dict>
+ </array>
+ </dict>
+ </dict>
+</plist>
+`
+
+var plistTests = []struct {
+ in string
+ out interface{}
+}{
+ {
+ thePlist,
+ &MyStruct{
+ BucketUUID: "C218A47D-DAFB-4476-9C67-597E556D7D8A",
+ BucketName: "rsc",
+ ComputerUUID: "E7859547-BB9C-41C0-871E-858A0526BAE7",
+ LocalPath: "/Users/rsc",
+ LocalMountPoint: "/Users",
+ IgnoredRelativePaths: []string{
+ "/.Trash",
+ "/go/pkg",
+ "/go1/pkg",
+ "/Library/Caches",
+ },
+ Excludes: Exclude1{
+ Excludes: []Exclude2{
+ {Type: 2,
+ Text: ".unison.",
+ },
+ },
+ },
+ },
+ },
+ {
+ thePlist,
+ &struct{}{},
+ },
+}
+
+type MyStruct struct {
+ BucketUUID string
+ BucketName string
+ ComputerUUID string
+ LocalPath string
+ LocalMountPoint string
+ IgnoredRelativePaths []string
+ Excludes Exclude1
+}
+
+type Exclude1 struct {
+ Excludes []Exclude2 `plist:"excludes"`
+}
+
+type Exclude2 struct {
+ Type int `plist:"type"`
+ Text string `plist:"text"`
+}
+
+func TestUnmarshal(t *testing.T) {
+ for _, tt := range plistTests {
+ v := reflect.New(reflect.ValueOf(tt.out).Type().Elem()).Interface()
+ if err := Unmarshal([]byte(tt.in), v); err != nil {
+ t.Errorf("%s", err)
+ continue
+ }
+ if !reflect.DeepEqual(tt.out, v) {
+ t.Errorf("unmarshal not equal")
+ }
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/qr/coding/qr_test.go b/vendor/github.com/mattermost/rsc/qr/coding/qr_test.go
new file mode 100644
index 000000000..b8199bb51
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/coding/qr_test.go
@@ -0,0 +1,133 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package coding
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/mattermost/rsc/gf256"
+ "github.com/mattermost/rsc/qr/libqrencode"
+)
+
+func test(t *testing.T, v Version, l Level, text ...Encoding) bool {
+ s := ""
+ ty := libqrencode.EightBit
+ switch x := text[0].(type) {
+ case String:
+ s = string(x)
+ case Alpha:
+ s = string(x)
+ ty = libqrencode.Alphanumeric
+ case Num:
+ s = string(x)
+ ty = libqrencode.Numeric
+ }
+ key, err := libqrencode.Encode(libqrencode.Version(v), libqrencode.Level(l), ty, s)
+ if err != nil {
+ t.Errorf("libqrencode.Encode(%v, %v, %d, %#q): %v", v, l, ty, s, err)
+ return false
+ }
+ mask := (^key.Pixel[8][2]&1)<<2 | (key.Pixel[8][3]&1)<<1 | (^key.Pixel[8][4] & 1)
+ p, err := NewPlan(v, l, Mask(mask))
+ if err != nil {
+ t.Errorf("NewPlan(%v, L, %d): %v", v, err, mask)
+ return false
+ }
+ if len(p.Pixel) != len(key.Pixel) {
+ t.Errorf("%v: NewPlan uses %dx%d, libqrencode uses %dx%d", v, len(p.Pixel), len(p.Pixel), len(key.Pixel), len(key.Pixel))
+ return false
+ }
+ c, err := p.Encode(text...)
+ if err != nil {
+ t.Errorf("Encode: %v", err)
+ return false
+ }
+ badpix := 0
+Pixel:
+ for y, prow := range p.Pixel {
+ for x, pix := range prow {
+ pix &^= Black
+ if c.Black(x, y) {
+ pix |= Black
+ }
+
+ keypix := key.Pixel[y][x]
+ want := Pixel(0)
+ switch {
+ case keypix&libqrencode.Finder != 0:
+ want = Position.Pixel()
+ case keypix&libqrencode.Alignment != 0:
+ want = Alignment.Pixel()
+ case keypix&libqrencode.Timing != 0:
+ want = Timing.Pixel()
+ case keypix&libqrencode.Format != 0:
+ want = Format.Pixel()
+ want |= OffsetPixel(pix.Offset()) // sic
+ want |= pix & Invert
+ case keypix&libqrencode.PVersion != 0:
+ want = PVersion.Pixel()
+ case keypix&libqrencode.DataECC != 0:
+ if pix.Role() == Check || pix.Role() == Extra {
+ want = pix.Role().Pixel()
+ } else {
+ want = Data.Pixel()
+ }
+ want |= OffsetPixel(pix.Offset())
+ want |= pix & Invert
+ default:
+ want = Unused.Pixel()
+ }
+ if keypix&libqrencode.Black != 0 {
+ want |= Black
+ }
+ if pix != want {
+ t.Errorf("%v/%v: Pixel[%d][%d] = %v, want %v %#x", v, mask, y, x, pix, want, keypix)
+ if badpix++; badpix >= 100 {
+ t.Errorf("stopping after %d bad pixels", badpix)
+ break Pixel
+ }
+ }
+ }
+ }
+ return badpix == 0
+}
+
+var input = []Encoding{
+ String("hello"),
+ Num("1"),
+ Num("12"),
+ Num("123"),
+ Alpha("AB"),
+ Alpha("ABC"),
+}
+
+func TestVersion(t *testing.T) {
+ badvers := 0
+Version:
+ for v := Version(1); v <= 40; v++ {
+ for l := L; l <= H; l++ {
+ for _, in := range input {
+ if !test(t, v, l, in) {
+ if badvers++; badvers >= 10 {
+ t.Errorf("stopping after %d bad versions", badvers)
+ break Version
+ }
+ }
+ }
+ }
+ }
+}
+
+func TestEncode(t *testing.T) {
+ data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
+ check := []byte{0xa5, 0x24, 0xd4, 0xc1, 0xed, 0x36, 0xc7, 0x87, 0x2c, 0x55}
+ rs := gf256.NewRSEncoder(Field, len(check))
+ out := make([]byte, len(check))
+ rs.ECC(data, out)
+ if !bytes.Equal(out, check) {
+ t.Errorf("have %x want %x", out, check)
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/qr/libqrencode/Makefile b/vendor/github.com/mattermost/rsc/qr/libqrencode/Makefile
new file mode 100644
index 000000000..4c9591462
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/libqrencode/Makefile
@@ -0,0 +1,4 @@
+include $(GOROOT)/src/Make.inc
+TARG=rsc.googlecode.com/hg/qr/libqrencode
+CGOFILES=qrencode.go
+include $(GOROOT)/src/Make.pkg
diff --git a/vendor/github.com/mattermost/rsc/qr/libqrencode/qrencode.go b/vendor/github.com/mattermost/rsc/qr/libqrencode/qrencode.go
new file mode 100644
index 000000000..f4ce3ffb6
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/libqrencode/qrencode.go
@@ -0,0 +1,149 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package libqrencode wraps the C libqrencode library.
+// The qr package (in this package's parent directory)
+// does not use any C wrapping. This code is here only
+// for use during that package's tests.
+package libqrencode
+
+/*
+#cgo LDFLAGS: -lqrencode
+#include <qrencode.h>
+*/
+import "C"
+
+import (
+ "fmt"
+ "image"
+ "image/color"
+ "unsafe"
+)
+
+type Version int
+
+type Mode int
+
+const (
+ Numeric Mode = C.QR_MODE_NUM
+ Alphanumeric Mode = C.QR_MODE_AN
+ EightBit Mode = C.QR_MODE_8
+)
+
+type Level int
+
+const (
+ L Level = C.QR_ECLEVEL_L
+ M Level = C.QR_ECLEVEL_M
+ Q Level = C.QR_ECLEVEL_Q
+ H Level = C.QR_ECLEVEL_H
+)
+
+type Pixel int
+
+const (
+ Black Pixel = 1 << iota
+ DataECC
+ Format
+ PVersion
+ Timing
+ Alignment
+ Finder
+ NonData
+)
+
+type Code struct {
+ Version int
+ Width int
+ Pixel [][]Pixel
+ Scale int
+}
+
+func (*Code) ColorModel() color.Model {
+ return color.RGBAModel
+}
+
+func (c *Code) Bounds() image.Rectangle {
+ d := (c.Width + 8) * c.Scale
+ return image.Rect(0, 0, d, d)
+}
+
+var (
+ white color.Color = color.RGBA{0xFF, 0xFF, 0xFF, 0xFF}
+ black color.Color = color.RGBA{0x00, 0x00, 0x00, 0xFF}
+ blue color.Color = color.RGBA{0x00, 0x00, 0x80, 0xFF}
+ red color.Color = color.RGBA{0xFF, 0x40, 0x40, 0xFF}
+ yellow color.Color = color.RGBA{0xFF, 0xFF, 0x00, 0xFF}
+ gray color.Color = color.RGBA{0x80, 0x80, 0x80, 0xFF}
+ green color.Color = color.RGBA{0x22, 0x8B, 0x22, 0xFF}
+)
+
+func (c *Code) At(x, y int) color.Color {
+ x = x/c.Scale - 4
+ y = y/c.Scale - 4
+ if 0 <= x && x < c.Width && 0 <= y && y < c.Width {
+ switch p := c.Pixel[y][x]; {
+ case p&Black == 0:
+ // nothing
+ case p&DataECC != 0:
+ return black
+ case p&Format != 0:
+ return blue
+ case p&PVersion != 0:
+ return red
+ case p&Timing != 0:
+ return yellow
+ case p&Alignment != 0:
+ return gray
+ case p&Finder != 0:
+ return green
+ }
+ }
+ return white
+}
+
+type Chunk struct {
+ Mode Mode
+ Text string
+}
+
+func Encode(version Version, level Level, mode Mode, text string) (*Code, error) {
+ return EncodeChunk(version, level, Chunk{mode, text})
+}
+
+func EncodeChunk(version Version, level Level, chunk ...Chunk) (*Code, error) {
+ qi, err := C.QRinput_new2(C.int(version), C.QRecLevel(level))
+ if qi == nil {
+ return nil, fmt.Errorf("QRinput_new2: %v", err)
+ }
+ defer C.QRinput_free(qi)
+ for _, ch := range chunk {
+ data := []byte(ch.Text)
+ n, err := C.QRinput_append(qi, C.QRencodeMode(ch.Mode), C.int(len(data)), (*C.uchar)(&data[0]))
+ if n < 0 {
+ return nil, fmt.Errorf("QRinput_append %q: %v", data, err)
+ }
+ }
+
+ qc, err := C.QRcode_encodeInput(qi)
+ if qc == nil {
+ return nil, fmt.Errorf("QRinput_encodeInput: %v", err)
+ }
+
+ c := &Code{
+ Version: int(qc.version),
+ Width: int(qc.width),
+ Scale: 16,
+ }
+ pix := make([]Pixel, c.Width*c.Width)
+ cdat := (*[1000 * 1000]byte)(unsafe.Pointer(qc.data))[:len(pix)]
+ for i := range pix {
+ pix[i] = Pixel(cdat[i])
+ }
+ c.Pixel = make([][]Pixel, c.Width)
+ for i := range c.Pixel {
+ c.Pixel[i] = pix[i*c.Width : (i+1)*c.Width]
+ }
+ return c, nil
+}
diff --git a/vendor/github.com/mattermost/rsc/qr/png_test.go b/vendor/github.com/mattermost/rsc/qr/png_test.go
new file mode 100644
index 000000000..27a622924
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/png_test.go
@@ -0,0 +1,73 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package qr
+
+import (
+ "bytes"
+ "image"
+ "image/color"
+ "image/png"
+ "io/ioutil"
+ "testing"
+)
+
+func TestPNG(t *testing.T) {
+ c, err := Encode("hello, world", L)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pngdat := c.PNG()
+ if true {
+ ioutil.WriteFile("x.png", pngdat, 0666)
+ }
+ m, err := png.Decode(bytes.NewBuffer(pngdat))
+ if err != nil {
+ t.Fatal(err)
+ }
+ gm := m.(*image.Gray)
+
+ scale := c.Scale
+ siz := c.Size
+ nbad := 0
+ for y := 0; y < scale*(8+siz); y++ {
+ for x := 0; x < scale*(8+siz); x++ {
+ v := byte(255)
+ if c.Black(x/scale-4, y/scale-4) {
+ v = 0
+ }
+ if gv := gm.At(x, y).(color.Gray).Y; gv != v {
+ t.Errorf("%d,%d = %d, want %d", x, y, gv, v)
+ if nbad++; nbad >= 20 {
+ t.Fatalf("too many bad pixels")
+ }
+ }
+ }
+ }
+}
+
+func BenchmarkPNG(b *testing.B) {
+ c, err := Encode("0123456789012345678901234567890123456789", L)
+ if err != nil {
+ panic(err)
+ }
+ var bytes []byte
+ for i := 0; i < b.N; i++ {
+ bytes = c.PNG()
+ }
+ b.SetBytes(int64(len(bytes)))
+}
+
+func BenchmarkImagePNG(b *testing.B) {
+ c, err := Encode("0123456789012345678901234567890123456789", L)
+ if err != nil {
+ panic(err)
+ }
+ var buf bytes.Buffer
+ for i := 0; i < b.N; i++ {
+ buf.Reset()
+ png.Encode(&buf, c.Image())
+ }
+ b.SetBytes(int64(buf.Len()))
+}
diff --git a/vendor/github.com/mattermost/rsc/qr/web/pic.go b/vendor/github.com/mattermost/rsc/qr/web/pic.go
new file mode 100644
index 000000000..6baef94d2
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/web/pic.go
@@ -0,0 +1,506 @@
+package web
+
+import (
+ "bytes"
+ "fmt"
+ "image"
+ "image/color"
+ "image/draw"
+ "image/png"
+ "net/http"
+ "strconv"
+ "strings"
+
+ "code.google.com/p/freetype-go/freetype"
+ "github.com/mattermost/rsc/appfs/fs"
+ "github.com/mattermost/rsc/qr"
+ "github.com/mattermost/rsc/qr/coding"
+)
+
+func makeImage(req *http.Request, caption, font string, pt, size, border, scale int, f func(x, y int) uint32) *image.RGBA {
+ d := (size + 2*border) * scale
+ csize := 0
+ if caption != "" {
+ if pt == 0 {
+ pt = 11
+ }
+ csize = pt * 2
+ }
+ c := image.NewRGBA(image.Rect(0, 0, d, d+csize))
+
+ // white
+ u := &image.Uniform{C: color.White}
+ draw.Draw(c, c.Bounds(), u, image.ZP, draw.Src)
+
+ for y := 0; y < size; y++ {
+ for x := 0; x < size; x++ {
+ r := image.Rect((x+border)*scale, (y+border)*scale, (x+border+1)*scale, (y+border+1)*scale)
+ rgba := f(x, y)
+ u.C = color.RGBA{byte(rgba >> 24), byte(rgba >> 16), byte(rgba >> 8), byte(rgba)}
+ draw.Draw(c, r, u, image.ZP, draw.Src)
+ }
+ }
+
+ if csize != 0 {
+ if font == "" {
+ font = "data/luxisr.ttf"
+ }
+ ctxt := fs.NewContext(req)
+ dat, _, err := ctxt.Read(font)
+ if err != nil {
+ panic(err)
+ }
+ tfont, err := freetype.ParseFont(dat)
+ if err != nil {
+ panic(err)
+ }
+ ft := freetype.NewContext()
+ ft.SetDst(c)
+ ft.SetDPI(100)
+ ft.SetFont(tfont)
+ ft.SetFontSize(float64(pt))
+ ft.SetSrc(image.NewUniform(color.Black))
+ ft.SetClip(image.Rect(0, 0, 0, 0))
+ wid, err := ft.DrawString(caption, freetype.Pt(0, 0))
+ if err != nil {
+ panic(err)
+ }
+ p := freetype.Pt(d, d+3*pt/2)
+ p.X -= wid.X
+ p.X /= 2
+ ft.SetClip(c.Bounds())
+ ft.DrawString(caption, p)
+ }
+
+ return c
+}
+
+func makeFrame(req *http.Request, font string, pt, vers, l, scale, dots int) image.Image {
+ lev := coding.Level(l)
+ p, err := coding.NewPlan(coding.Version(vers), lev, 0)
+ if err != nil {
+ panic(err)
+ }
+
+ nd := p.DataBytes / p.Blocks
+ nc := p.CheckBytes / p.Blocks
+ extra := p.DataBytes - nd*p.Blocks
+
+ cap := fmt.Sprintf("QR v%d, %s", vers, lev)
+ if dots > 0 {
+ cap = fmt.Sprintf("QR v%d order, from bottom right", vers)
+ }
+ m := makeImage(req, cap, font, pt, len(p.Pixel), 0, scale, func(x, y int) uint32 {
+ pix := p.Pixel[y][x]
+ switch pix.Role() {
+ case coding.Data:
+ if dots > 0 {
+ return 0xffffffff
+ }
+ off := int(pix.Offset() / 8)
+ nd := nd
+ var i int
+ for i = 0; i < p.Blocks; i++ {
+ if i == extra {
+ nd++
+ }
+ if off < nd {
+ break
+ }
+ off -= nd
+ }
+ return blockColors[i%len(blockColors)]
+ case coding.Check:
+ if dots > 0 {
+ return 0xffffffff
+ }
+ i := (int(pix.Offset()/8) - p.DataBytes) / nc
+ return dark(blockColors[i%len(blockColors)])
+ }
+ if pix&coding.Black != 0 {
+ return 0x000000ff
+ }
+ return 0xffffffff
+ })
+
+ if dots > 0 {
+ b := m.Bounds()
+ for y := 0; y <= len(p.Pixel); y++ {
+ for x := 0; x < b.Dx(); x++ {
+ m.SetRGBA(x, y*scale-(y/len(p.Pixel)), color.RGBA{127, 127, 127, 255})
+ }
+ }
+ for x := 0; x <= len(p.Pixel); x++ {
+ for y := 0; y < b.Dx(); y++ {
+ m.SetRGBA(x*scale-(x/len(p.Pixel)), y, color.RGBA{127, 127, 127, 255})
+ }
+ }
+ order := make([]image.Point, (p.DataBytes+p.CheckBytes)*8+1)
+ for y, row := range p.Pixel {
+ for x, pix := range row {
+ if r := pix.Role(); r != coding.Data && r != coding.Check {
+ continue
+ }
+ // draw.Draw(m, m.Bounds().Add(image.Pt(x*scale, y*scale)), dot, image.ZP, draw.Over)
+ order[pix.Offset()] = image.Point{x*scale + scale/2, y*scale + scale/2}
+ }
+ }
+
+ for mode := 0; mode < 2; mode++ {
+ for i, p := range order {
+ q := order[i+1]
+ if q.X == 0 {
+ break
+ }
+ line(m, p, q, mode)
+ }
+ }
+ }
+ return m
+}
+
+func line(m *image.RGBA, p, q image.Point, mode int) {
+ x := 0
+ y := 0
+ dx := q.X - p.X
+ dy := q.Y - p.Y
+ xsign := +1
+ ysign := +1
+ if dx < 0 {
+ xsign = -1
+ dx = -dx
+ }
+ if dy < 0 {
+ ysign = -1
+ dy = -dy
+ }
+ pt := func() {
+ switch mode {
+ case 0:
+ for dx := -2; dx <= 2; dx++ {
+ for dy := -2; dy <= 2; dy++ {
+ if dy*dx <= -4 || dy*dx >= 4 {
+ continue
+ }
+ m.SetRGBA(p.X+x*xsign+dx, p.Y+y*ysign+dy, color.RGBA{255, 192, 192, 255})
+ }
+ }
+
+ case 1:
+ m.SetRGBA(p.X+x*xsign, p.Y+y*ysign, color.RGBA{128, 0, 0, 255})
+ }
+ }
+ if dx > dy {
+ for x < dx || y < dy {
+ pt()
+ x++
+ if float64(x)*float64(dy)/float64(dx)-float64(y) > 0.5 {
+ y++
+ }
+ }
+ } else {
+ for x < dx || y < dy {
+ pt()
+ y++
+ if float64(y)*float64(dx)/float64(dy)-float64(x) > 0.5 {
+ x++
+ }
+ }
+ }
+ pt()
+}
+
+func pngEncode(c image.Image) []byte {
+ var b bytes.Buffer
+ png.Encode(&b, c)
+ return b.Bytes()
+}
+
+// Frame handles a request for a single QR frame.
+func Frame(w http.ResponseWriter, req *http.Request) {
+ arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
+ v := arg("v")
+ scale := arg("scale")
+ if scale == 0 {
+ scale = 8
+ }
+
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(pngEncode(makeFrame(req, req.FormValue("font"), arg("pt"), v, arg("l"), scale, arg("dots"))))
+}
+
+// Frames handles a request for multiple QR frames.
+func Frames(w http.ResponseWriter, req *http.Request) {
+ vs := strings.Split(req.FormValue("v"), ",")
+
+ arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
+ scale := arg("scale")
+ if scale == 0 {
+ scale = 8
+ }
+ font := req.FormValue("font")
+ pt := arg("pt")
+ dots := arg("dots")
+
+ var images []image.Image
+ l := arg("l")
+ for _, v := range vs {
+ l := l
+ if i := strings.Index(v, "."); i >= 0 {
+ l, _ = strconv.Atoi(v[i+1:])
+ v = v[:i]
+ }
+ vv, _ := strconv.Atoi(v)
+ images = append(images, makeFrame(req, font, pt, vv, l, scale, dots))
+ }
+
+ b := images[len(images)-1].Bounds()
+
+ dx := arg("dx")
+ if dx == 0 {
+ dx = b.Dx()
+ }
+ x, y := 0, 0
+ xmax := 0
+ sep := arg("sep")
+ if sep == 0 {
+ sep = 10
+ }
+ var points []image.Point
+ for i, m := range images {
+ if x > 0 {
+ x += sep
+ }
+ if x > 0 && x+m.Bounds().Dx() > dx {
+ y += sep + images[i-1].Bounds().Dy()
+ x = 0
+ }
+ points = append(points, image.Point{x, y})
+ x += m.Bounds().Dx()
+ if x > xmax {
+ xmax = x
+ }
+
+ }
+
+ c := image.NewRGBA(image.Rect(0, 0, xmax, y+b.Dy()))
+ for i, m := range images {
+ draw.Draw(c, c.Bounds().Add(points[i]), m, image.ZP, draw.Src)
+ }
+
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(pngEncode(c))
+}
+
+// Mask handles a request for a single QR mask.
+func Mask(w http.ResponseWriter, req *http.Request) {
+ arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
+ v := arg("v")
+ m := arg("m")
+ scale := arg("scale")
+ if scale == 0 {
+ scale = 8
+ }
+
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(pngEncode(makeMask(req, req.FormValue("font"), arg("pt"), v, m, scale)))
+}
+
+// Masks handles a request for multiple QR masks.
+func Masks(w http.ResponseWriter, req *http.Request) {
+ arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
+ v := arg("v")
+ scale := arg("scale")
+ if scale == 0 {
+ scale = 8
+ }
+ font := req.FormValue("font")
+ pt := arg("pt")
+ var mm []image.Image
+ for m := 0; m < 8; m++ {
+ mm = append(mm, makeMask(req, font, pt, v, m, scale))
+ }
+ dx := mm[0].Bounds().Dx()
+ dy := mm[0].Bounds().Dy()
+
+ sep := arg("sep")
+ if sep == 0 {
+ sep = 10
+ }
+ c := image.NewRGBA(image.Rect(0, 0, (dx+sep)*4-sep, (dy+sep)*2-sep))
+ for m := 0; m < 8; m++ {
+ x := (m % 4) * (dx + sep)
+ y := (m / 4) * (dy + sep)
+ draw.Draw(c, c.Bounds().Add(image.Pt(x, y)), mm[m], image.ZP, draw.Src)
+ }
+
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(pngEncode(c))
+}
+
+var maskName = []string{
+ "(x+y) % 2",
+ "y % 2",
+ "x % 3",
+ "(x+y) % 3",
+ "(y/2 + x/3) % 2",
+ "xy%2 + xy%3",
+ "(xy%2 + xy%3) % 2",
+ "(xy%3 + (x+y)%2) % 2",
+}
+
+func makeMask(req *http.Request, font string, pt int, vers, mask, scale int) image.Image {
+ p, err := coding.NewPlan(coding.Version(vers), coding.L, coding.Mask(mask))
+ if err != nil {
+ panic(err)
+ }
+ m := makeImage(req, maskName[mask], font, pt, len(p.Pixel), 0, scale, func(x, y int) uint32 {
+ pix := p.Pixel[y][x]
+ switch pix.Role() {
+ case coding.Data, coding.Check:
+ if pix&coding.Invert != 0 {
+ return 0x000000ff
+ }
+ }
+ return 0xffffffff
+ })
+ return m
+}
+
+var blockColors = []uint32{
+ 0x7777ffff,
+ 0xffff77ff,
+ 0xff7777ff,
+ 0x77ffffff,
+ 0x1e90ffff,
+ 0xffffe0ff,
+ 0x8b6969ff,
+ 0x77ff77ff,
+ 0x9b30ffff,
+ 0x00bfffff,
+ 0x90e890ff,
+ 0xfff68fff,
+ 0xffec8bff,
+ 0xffa07aff,
+ 0xffa54fff,
+ 0xeee8aaff,
+ 0x98fb98ff,
+ 0xbfbfbfff,
+ 0x54ff9fff,
+ 0xffaeb9ff,
+ 0xb23aeeff,
+ 0xbbffffff,
+ 0x7fffd4ff,
+ 0xff7a7aff,
+ 0x00007fff,
+}
+
+func dark(x uint32) uint32 {
+ r, g, b, a := byte(x>>24), byte(x>>16), byte(x>>8), byte(x)
+ r = r/2 + r/4
+ g = g/2 + g/4
+ b = b/2 + b/4
+ return uint32(r)<<24 | uint32(g)<<16 | uint32(b)<<8 | uint32(a)
+}
+
+func clamp(x int) byte {
+ if x < 0 {
+ return 0
+ }
+ if x > 255 {
+ return 255
+ }
+ return byte(x)
+}
+
+func max(x, y int) int {
+ if x > y {
+ return x
+ }
+ return y
+}
+
+// Arrow handles a request for an arrow pointing in a given direction.
+func Arrow(w http.ResponseWriter, req *http.Request) {
+ arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
+ dir := arg("dir")
+ size := arg("size")
+ if size == 0 {
+ size = 50
+ }
+ del := size / 10
+
+ m := image.NewRGBA(image.Rect(0, 0, size, size))
+
+ if dir == 4 {
+ draw.Draw(m, m.Bounds(), image.Black, image.ZP, draw.Src)
+ draw.Draw(m, image.Rect(5, 5, size-5, size-5), image.White, image.ZP, draw.Src)
+ }
+
+ pt := func(x, y int, c color.RGBA) {
+ switch dir {
+ case 0:
+ m.SetRGBA(x, y, c)
+ case 1:
+ m.SetRGBA(y, size-1-x, c)
+ case 2:
+ m.SetRGBA(size-1-x, size-1-y, c)
+ case 3:
+ m.SetRGBA(size-1-y, x, c)
+ }
+ }
+
+ for y := 0; y < size/2; y++ {
+ for x := 0; x < del && x < y; x++ {
+ pt(x, y, color.RGBA{0, 0, 0, 255})
+ }
+ for x := del; x < y-del; x++ {
+ pt(x, y, color.RGBA{128, 128, 255, 255})
+ }
+ for x := max(y-del, 0); x <= y; x++ {
+ pt(x, y, color.RGBA{0, 0, 0, 255})
+ }
+ }
+ for y := size / 2; y < size; y++ {
+ for x := 0; x < del && x < size-1-y; x++ {
+ pt(x, y, color.RGBA{0, 0, 0, 255})
+ }
+ for x := del; x < size-1-y-del; x++ {
+ pt(x, y, color.RGBA{128, 128, 192, 255})
+ }
+ for x := max(size-1-y-del, 0); x <= size-1-y; x++ {
+ pt(x, y, color.RGBA{0, 0, 0, 255})
+ }
+ }
+
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(pngEncode(m))
+}
+
+// Encode encodes a string using the given version, level, and mask.
+func Encode(w http.ResponseWriter, req *http.Request) {
+ val := func(s string) int {
+ v, _ := strconv.Atoi(req.FormValue(s))
+ return v
+ }
+
+ l := coding.Level(val("l"))
+ v := coding.Version(val("v"))
+ enc := coding.String(req.FormValue("t"))
+ m := coding.Mask(val("m"))
+
+ p, err := coding.NewPlan(v, l, m)
+ if err != nil {
+ panic(err)
+ }
+ cc, err := p.Encode(enc)
+ if err != nil {
+ panic(err)
+ }
+
+ c := &qr.Code{Bitmap: cc.Bitmap, Size: cc.Size, Stride: cc.Stride, Scale: 8}
+ w.Header().Set("Content-Type", "image/png")
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(c.PNG())
+}
+
diff --git a/vendor/github.com/mattermost/rsc/qr/web/play.go b/vendor/github.com/mattermost/rsc/qr/web/play.go
new file mode 100644
index 000000000..120f50b81
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/web/play.go
@@ -0,0 +1,1118 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+/*
+QR data layout
+
+qr/
+ upload/
+ id.png
+ id.fix
+ flag/
+ id
+
+*/
+// TODO: Random seed taken from GET for caching, repeatability.
+// TODO: Flag for abuse button + some kind of dashboard.
+// TODO: +1 button on web page? permalink?
+// TODO: Flag for abuse button on permalinks too?
+// TODO: Make the page prettier.
+// TODO: Cache headers.
+
+package web
+
+import (
+ "bytes"
+ "crypto/md5"
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "html/template"
+ "image"
+ "image/color"
+ _ "image/gif"
+ _ "image/jpeg"
+ "image/png"
+ "io"
+ "math/rand"
+ "net/http"
+ "net/url"
+ "os"
+ "sort"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/mattermost/rsc/appfs/fs"
+ "github.com/mattermost/rsc/gf256"
+ "github.com/mattermost/rsc/qr"
+ "github.com/mattermost/rsc/qr/coding"
+ "github.com/mattermost/rsc/qr/web/resize"
+)
+
+func runTemplate(c *fs.Context, w http.ResponseWriter, name string, data interface{}) {
+ t := template.New("main")
+
+ main, _, err := c.Read(name)
+ if err != nil {
+ panic(err)
+ }
+ style, _, _ := c.Read("style.html")
+ main = append(main, style...)
+ _, err = t.Parse(string(main))
+ if err != nil {
+ panic(err)
+ }
+
+ var buf bytes.Buffer
+ if err := t.Execute(&buf, &data); err != nil {
+ panic(err)
+ }
+ w.Write(buf.Bytes())
+}
+
+func isImgName(s string) bool {
+ if len(s) != 32 {
+ return false
+ }
+ for i := 0; i < len(s); i++ {
+ if '0' <= s[i] && s[i] <= '9' || 'a' <= s[i] && s[i] <= 'f' {
+ continue
+ }
+ return false
+ }
+ return true
+}
+
+func isTagName(s string) bool {
+ if len(s) != 16 {
+ return false
+ }
+ for i := 0; i < len(s); i++ {
+ if '0' <= s[i] && s[i] <= '9' || 'a' <= s[i] && s[i] <= 'f' {
+ continue
+ }
+ return false
+ }
+ return true
+}
+
+// Draw is the handler for drawing a QR code.
+func Draw(w http.ResponseWriter, req *http.Request) {
+ ctxt := fs.NewContext(req)
+
+ url := req.FormValue("url")
+ if url == "" {
+ url = "http://swtch.com/qr"
+ }
+ if req.FormValue("upload") == "1" {
+ upload(w, req, url)
+ return
+ }
+
+ t0 := time.Now()
+ img := req.FormValue("i")
+ if !isImgName(img) {
+ img = "pjw"
+ }
+ if req.FormValue("show") == "png" {
+ i := loadSize(ctxt, img, 48)
+ var buf bytes.Buffer
+ png.Encode(&buf, i)
+ w.Write(buf.Bytes())
+ return
+ }
+ if req.FormValue("flag") == "1" {
+ flag(w, req, img, ctxt)
+ return
+ }
+ if req.FormValue("x") == "" {
+ var data = struct {
+ Name string
+ URL string
+ }{
+ Name: img,
+ URL: url,
+ }
+ runTemplate(ctxt, w, "qr/main.html", &data)
+ return
+ }
+
+ arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
+ targ := makeTarg(ctxt, img, 17+4*arg("v")+arg("z"))
+
+ m := &Image{
+ Name: img,
+ Dx: arg("x"),
+ Dy: arg("y"),
+ URL: req.FormValue("u"),
+ Version: arg("v"),
+ Mask: arg("m"),
+ RandControl: arg("r") > 0,
+ Dither: arg("i") > 0,
+ OnlyDataBits: arg("d") > 0,
+ SaveControl: arg("c") > 0,
+ Scale: arg("scale"),
+ Target: targ,
+ Seed: int64(arg("s")),
+ Rotation: arg("o"),
+ Size: arg("z"),
+ }
+ if m.Version > 8 {
+ m.Version = 8
+ }
+
+ if m.Scale == 0 {
+ if arg("l") > 1 {
+ m.Scale = 8
+ } else {
+ m.Scale = 4
+ }
+ }
+ if m.Version >= 12 && m.Scale >= 4 {
+ m.Scale /= 2
+ }
+
+ if arg("l") == 1 {
+ data, err := json.Marshal(m)
+ if err != nil {
+ panic(err)
+ }
+ h := md5.New()
+ h.Write(data)
+ tag := fmt.Sprintf("%x", h.Sum(nil))[:16]
+ if err := ctxt.Write("qrsave/"+tag, data); err != nil {
+ panic(err)
+ }
+ http.Redirect(w, req, "/qr/show/" + tag, http.StatusTemporaryRedirect)
+ return
+ }
+
+ if err := m.Encode(req); err != nil {
+ fmt.Fprintf(w, "%s\n", err)
+ return
+ }
+
+ var dat []byte
+ switch {
+ case m.SaveControl:
+ dat = m.Control
+ default:
+ dat = m.Code.PNG()
+ }
+
+ if arg("l") > 0 {
+ w.Header().Set("Content-Type", "image/png")
+ w.Write(dat)
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ fmt.Fprint(w, "<center><img src=\"data:image/png;base64,")
+ io.WriteString(w, base64.StdEncoding.EncodeToString(dat))
+ fmt.Fprint(w, "\" /><br>")
+ fmt.Fprintf(w, "<form method=\"POST\" action=\"%s&l=1\"><input type=\"submit\" value=\"Save this QR code\"></form>\n", m.Link())
+ fmt.Fprintf(w, "</center>\n")
+ fmt.Fprintf(w, "<br><center><font size=-1>%v</font></center>\n", time.Now().Sub(t0))
+}
+
+func (m *Image) Small() bool {
+ return 8*(17+4*int(m.Version)) < 512
+}
+
+func (m *Image) Link() string {
+ s := fmt.Sprint
+ b := func(v bool) string {
+ if v {
+ return "1"
+ }
+ return "0"
+ }
+ val := url.Values{
+ "i": {m.Name},
+ "x": {s(m.Dx)},
+ "y": {s(m.Dy)},
+ "z": {s(m.Size)},
+ "u": {m.URL},
+ "v": {s(m.Version)},
+ "m": {s(m.Mask)},
+ "r": {b(m.RandControl)},
+ "t": {b(m.Dither)},
+ "d": {b(m.OnlyDataBits)},
+ "c": {b(m.SaveControl)},
+ "s": {s(m.Seed)},
+ }
+ return "/qr/draw?" + val.Encode()
+}
+
+// Show is the handler for showing a stored QR code.
+func Show(w http.ResponseWriter, req *http.Request) {
+ ctxt := fs.NewContext(req)
+ tag := req.URL.Path[len("/qr/show/"):]
+ png := strings.HasSuffix(tag, ".png")
+ if png {
+ tag = tag[:len(tag)-len(".png")]
+ }
+ if !isTagName(tag) {
+ fmt.Fprintf(w, "Sorry, QR code not found\n")
+ return
+ }
+ if req.FormValue("flag") == "1" {
+ flag(w, req, tag, ctxt)
+ return
+ }
+ data, _, err := ctxt.Read("qrsave/" + tag)
+ if err != nil {
+ fmt.Fprintf(w, "Sorry, QR code not found.\n")
+ return
+ }
+
+ var m Image
+ if err := json.Unmarshal(data, &m); err != nil {
+ panic(err)
+ }
+ m.Tag = tag
+
+ switch req.FormValue("size") {
+ case "big":
+ m.Scale *= 2
+ case "small":
+ m.Scale /= 2
+ }
+
+ if png {
+ if err := m.Encode(req); err != nil {
+ panic(err)
+ return
+ }
+ w.Header().Set("Cache-Control", "public, max-age=3600")
+ w.Write(m.Code.PNG())
+ return
+ }
+
+ w.Header().Set("Cache-Control", "public, max-age=300")
+ runTemplate(ctxt, w, "qr/permalink.html", &m)
+}
+
+func upload(w http.ResponseWriter, req *http.Request, link string) {
+ // Upload of a new image.
+ // Copied from Moustachio demo.
+ f, _, err := req.FormFile("image")
+ if err != nil {
+ fmt.Fprintf(w, "You need to select an image to upload.\n")
+ return
+ }
+ defer f.Close()
+
+ i, _, err := image.Decode(f)
+ if err != nil {
+ panic(err)
+ }
+
+ // Convert image to 128x128 gray+alpha.
+ b := i.Bounds()
+ const max = 128
+ // If it's gigantic, it's more efficient to downsample first
+ // and then resize; resizing will smooth out the roughness.
+ var i1 *image.RGBA
+ if b.Dx() > 4*max || b.Dy() > 4*max {
+ w, h := 2*max, 2*max
+ if b.Dx() > b.Dy() {
+ h = b.Dy() * h / b.Dx()
+ } else {
+ w = b.Dx() * w / b.Dy()
+ }
+ i1 = resize.Resample(i, b, w, h)
+ } else {
+ // "Resample" to same size, just to convert to RGBA.
+ i1 = resize.Resample(i, b, b.Dx(), b.Dy())
+ }
+ b = i1.Bounds()
+
+ // Encode to PNG.
+ dx, dy := 128, 128
+ if b.Dx() > b.Dy() {
+ dy = b.Dy() * dx / b.Dx()
+ } else {
+ dx = b.Dx() * dy / b.Dy()
+ }
+ i128 := resize.ResizeRGBA(i1, i1.Bounds(), dx, dy)
+
+ var buf bytes.Buffer
+ if err := png.Encode(&buf, i128); err != nil {
+ panic(err)
+ }
+
+ h := md5.New()
+ h.Write(buf.Bytes())
+ tag := fmt.Sprintf("%x", h.Sum(nil))[:32]
+
+ ctxt := fs.NewContext(req)
+ if err := ctxt.Write("qr/upload/"+tag+".png", buf.Bytes()); err != nil {
+ panic(err)
+ }
+
+ // Redirect with new image tag.
+ // Redirect to draw with new image tag.
+ http.Redirect(w, req, req.URL.Path+"?"+url.Values{"i": {tag}, "url": {link}}.Encode(), 302)
+}
+
+func flag(w http.ResponseWriter, req *http.Request, img string, ctxt *fs.Context) {
+ if !isImgName(img) && !isTagName(img) {
+ fmt.Fprintf(w, "Invalid image.\n")
+ return
+ }
+ data, _, _ := ctxt.Read("qr/flag/" + img)
+ data = append(data, '!')
+ ctxt.Write("qr/flag/" + img, data)
+
+ fmt.Fprintf(w, "Thank you. The image has been reported.\n")
+}
+
+func loadSize(ctxt *fs.Context, name string, max int) *image.RGBA {
+ data, _, err := ctxt.Read("qr/upload/" + name + ".png")
+ if err != nil {
+ panic(err)
+ }
+ i, _, err := image.Decode(bytes.NewBuffer(data))
+ if err != nil {
+ panic(err)
+ }
+ b := i.Bounds()
+ dx, dy := max, max
+ if b.Dx() > b.Dy() {
+ dy = b.Dy() * dx / b.Dx()
+ } else {
+ dx = b.Dx() * dy / b.Dy()
+ }
+ var irgba *image.RGBA
+ switch i := i.(type) {
+ case *image.RGBA:
+ irgba = resize.ResizeRGBA(i, i.Bounds(), dx, dy)
+ case *image.NRGBA:
+ irgba = resize.ResizeNRGBA(i, i.Bounds(), dx, dy)
+ }
+ return irgba
+}
+
+func makeTarg(ctxt *fs.Context, name string, max int) [][]int {
+ i := loadSize(ctxt, name, max)
+ b := i.Bounds()
+ dx, dy := b.Dx(), b.Dy()
+ targ := make([][]int, dy)
+ arr := make([]int, dx*dy)
+ for y := 0; y < dy; y++ {
+ targ[y], arr = arr[:dx], arr[dx:]
+ row := targ[y]
+ for x := 0; x < dx; x++ {
+ p := i.Pix[y*i.Stride+4*x:]
+ r, g, b, a := p[0], p[1], p[2], p[3]
+ if a == 0 {
+ row[x] = -1
+ } else {
+ row[x] = int((299*uint32(r) + 587*uint32(g) + 114*uint32(b) + 500) / 1000)
+ }
+ }
+ }
+ return targ
+}
+
+type Image struct {
+ Name string
+ Target [][]int
+ Dx int
+ Dy int
+ URL string
+ Tag string
+ Version int
+ Mask int
+ Scale int
+ Rotation int
+ Size int
+
+ // RandControl says to pick the pixels randomly.
+ RandControl bool
+ Seed int64
+
+ // Dither says to dither instead of using threshold pixel layout.
+ Dither bool
+
+ // OnlyDataBits says to use only data bits, not check bits.
+ OnlyDataBits bool
+
+ // Code is the final QR code.
+ Code *qr.Code
+
+ // Control is a PNG showing the pixels that we controlled.
+ // Pixels we don't control are grayed out.
+ SaveControl bool
+ Control []byte
+}
+
+type Pixinfo struct {
+ X int
+ Y int
+ Pix coding.Pixel
+ Targ byte
+ DTarg int
+ Contrast int
+ HardZero bool
+ Block *BitBlock
+ Bit uint
+}
+
+type Pixorder struct {
+ Off int
+ Priority int
+}
+
+type byPriority []Pixorder
+
+func (x byPriority) Len() int { return len(x) }
+func (x byPriority) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
+func (x byPriority) Less(i, j int) bool { return x[i].Priority > x[j].Priority }
+
+func (m *Image) target(x, y int) (targ byte, contrast int) {
+ tx := x + m.Dx
+ ty := y + m.Dy
+ if ty < 0 || ty >= len(m.Target) || tx < 0 || tx >= len(m.Target[ty]) {
+ return 255, -1
+ }
+
+ v0 := m.Target[ty][tx]
+ if v0 < 0 {
+ return 255, -1
+ }
+ targ = byte(v0)
+
+ n := 0
+ sum := 0
+ sumsq := 0
+ const del = 5
+ for dy := -del; dy <= del; dy++ {
+ for dx := -del; dx <= del; dx++ {
+ if 0 <= ty+dy && ty+dy < len(m.Target) && 0 <= tx+dx && tx+dx < len(m.Target[ty+dy]) {
+ v := m.Target[ty+dy][tx+dx]
+ sum += v
+ sumsq += v * v
+ n++
+ }
+ }
+ }
+
+ avg := sum / n
+ contrast = sumsq/n - avg*avg
+ return
+}
+
+func (m *Image) rotate(p *coding.Plan, rot int) {
+ if rot == 0 {
+ return
+ }
+
+ N := len(p.Pixel)
+ pix := make([][]coding.Pixel, N)
+ apix := make([]coding.Pixel, N*N)
+ for i := range pix {
+ pix[i], apix = apix[:N], apix[N:]
+ }
+
+ switch rot {
+ case 0:
+ // ok
+ case 1:
+ for y := 0; y < N; y++ {
+ for x := 0; x < N; x++ {
+ pix[y][x] = p.Pixel[x][N-1-y]
+ }
+ }
+ case 2:
+ for y := 0; y < N; y++ {
+ for x := 0; x < N; x++ {
+ pix[y][x] = p.Pixel[N-1-y][N-1-x]
+ }
+ }
+ case 3:
+ for y := 0; y < N; y++ {
+ for x := 0; x < N; x++ {
+ pix[y][x] = p.Pixel[N-1-x][y]
+ }
+ }
+ }
+
+ p.Pixel = pix
+}
+
+func (m *Image) Encode(req *http.Request) error {
+ p, err := coding.NewPlan(coding.Version(m.Version), coding.L, coding.Mask(m.Mask))
+ if err != nil {
+ return err
+ }
+
+ m.rotate(p, m.Rotation)
+
+ rand := rand.New(rand.NewSource(m.Seed))
+
+ // QR parameters.
+ nd := p.DataBytes / p.Blocks
+ nc := p.CheckBytes / p.Blocks
+ extra := p.DataBytes - nd*p.Blocks
+ rs := gf256.NewRSEncoder(coding.Field, nc)
+
+ // Build information about pixels, indexed by data/check bit number.
+ pixByOff := make([]Pixinfo, (p.DataBytes+p.CheckBytes)*8)
+ expect := make([][]bool, len(p.Pixel))
+ for y, row := range p.Pixel {
+ expect[y] = make([]bool, len(row))
+ for x, pix := range row {
+ targ, contrast := m.target(x, y)
+ if m.RandControl && contrast >= 0 {
+ contrast = rand.Intn(128) + 64*((x+y)%2) + 64*((x+y)%3%2)
+ }
+ expect[y][x] = pix&coding.Black != 0
+ if r := pix.Role(); r == coding.Data || r == coding.Check {
+ pixByOff[pix.Offset()] = Pixinfo{X: x, Y: y, Pix: pix, Targ: targ, Contrast: contrast}
+ }
+ }
+ }
+
+Again:
+ // Count fixed initial data bits, prepare template URL.
+ url := m.URL + "#"
+ var b coding.Bits
+ coding.String(url).Encode(&b, p.Version)
+ coding.Num("").Encode(&b, p.Version)
+ bbit := b.Bits()
+ dbit := p.DataBytes*8 - bbit
+ if dbit < 0 {
+ return fmt.Errorf("cannot encode URL into available bits")
+ }
+ num := make([]byte, dbit/10*3)
+ for i := range num {
+ num[i] = '0'
+ }
+ b.Pad(dbit)
+ b.Reset()
+ coding.String(url).Encode(&b, p.Version)
+ coding.Num(num).Encode(&b, p.Version)
+ b.AddCheckBytes(p.Version, p.Level)
+ data := b.Bytes()
+
+ doff := 0 // data offset
+ coff := 0 // checksum offset
+ mbit := bbit + dbit/10*10
+
+ // Choose pixels.
+ bitblocks := make([]*BitBlock, p.Blocks)
+ for blocknum := 0; blocknum < p.Blocks; blocknum++ {
+ if blocknum == p.Blocks-extra {
+ nd++
+ }
+
+ bdata := data[doff/8 : doff/8+nd]
+ cdata := data[p.DataBytes+coff/8 : p.DataBytes+coff/8+nc]
+ bb := newBlock(nd, nc, rs, bdata, cdata)
+ bitblocks[blocknum] = bb
+
+ // Determine which bits in this block we can try to edit.
+ lo, hi := 0, nd*8
+ if lo < bbit-doff {
+ lo = bbit - doff
+ if lo > hi {
+ lo = hi
+ }
+ }
+ if hi > mbit-doff {
+ hi = mbit - doff
+ if hi < lo {
+ hi = lo
+ }
+ }
+
+ // Preserve [0, lo) and [hi, nd*8).
+ for i := 0; i < lo; i++ {
+ if !bb.canSet(uint(i), (bdata[i/8]>>uint(7-i&7))&1) {
+ return fmt.Errorf("cannot preserve required bits")
+ }
+ }
+ for i := hi; i < nd*8; i++ {
+ if !bb.canSet(uint(i), (bdata[i/8]>>uint(7-i&7))&1) {
+ return fmt.Errorf("cannot preserve required bits")
+ }
+ }
+
+ // Can edit [lo, hi) and checksum bits to hit target.
+ // Determine which ones to try first.
+ order := make([]Pixorder, (hi-lo)+nc*8)
+ for i := lo; i < hi; i++ {
+ order[i-lo].Off = doff + i
+ }
+ for i := 0; i < nc*8; i++ {
+ order[hi-lo+i].Off = p.DataBytes*8 + coff + i
+ }
+ if m.OnlyDataBits {
+ order = order[:hi-lo]
+ }
+ for i := range order {
+ po := &order[i]
+ po.Priority = pixByOff[po.Off].Contrast<<8 | rand.Intn(256)
+ }
+ sort.Sort(byPriority(order))
+
+ const mark = false
+ for i := range order {
+ po := &order[i]
+ pinfo := &pixByOff[po.Off]
+ bval := pinfo.Targ
+ if bval < 128 {
+ bval = 1
+ } else {
+ bval = 0
+ }
+ pix := pinfo.Pix
+ if pix&coding.Invert != 0 {
+ bval ^= 1
+ }
+ if pinfo.HardZero {
+ bval = 0
+ }
+
+ var bi int
+ if pix.Role() == coding.Data {
+ bi = po.Off - doff
+ } else {
+ bi = po.Off - p.DataBytes*8 - coff + nd*8
+ }
+ if bb.canSet(uint(bi), bval) {
+ pinfo.Block = bb
+ pinfo.Bit = uint(bi)
+ if mark {
+ p.Pixel[pinfo.Y][pinfo.X] = coding.Black
+ }
+ } else {
+ if pinfo.HardZero {
+ panic("hard zero")
+ }
+ if mark {
+ p.Pixel[pinfo.Y][pinfo.X] = 0
+ }
+ }
+ }
+ bb.copyOut()
+
+ const cheat = false
+ for i := 0; i < nd*8; i++ {
+ pinfo := &pixByOff[doff+i]
+ pix := p.Pixel[pinfo.Y][pinfo.X]
+ if bb.B[i/8]&(1<<uint(7-i&7)) != 0 {
+ pix ^= coding.Black
+ }
+ expect[pinfo.Y][pinfo.X] = pix&coding.Black != 0
+ if cheat {
+ p.Pixel[pinfo.Y][pinfo.X] = pix & coding.Black
+ }
+ }
+ for i := 0; i < nc*8; i++ {
+ pinfo := &pixByOff[p.DataBytes*8+coff+i]
+ pix := p.Pixel[pinfo.Y][pinfo.X]
+ if bb.B[nd+i/8]&(1<<uint(7-i&7)) != 0 {
+ pix ^= coding.Black
+ }
+ expect[pinfo.Y][pinfo.X] = pix&coding.Black != 0
+ if cheat {
+ p.Pixel[pinfo.Y][pinfo.X] = pix & coding.Black
+ }
+ }
+ doff += nd * 8
+ coff += nc * 8
+ }
+
+ // Pass over all pixels again, dithering.
+ if m.Dither {
+ for i := range pixByOff {
+ pinfo := &pixByOff[i]
+ pinfo.DTarg = int(pinfo.Targ)
+ }
+ for y, row := range p.Pixel {
+ for x, pix := range row {
+ if pix.Role() != coding.Data && pix.Role() != coding.Check {
+ continue
+ }
+ pinfo := &pixByOff[pix.Offset()]
+ if pinfo.Block == nil {
+ // did not choose this pixel
+ continue
+ }
+
+ pix := pinfo.Pix
+
+ pval := byte(1) // pixel value (black)
+ v := 0 // gray value (black)
+ targ := pinfo.DTarg
+ if targ >= 128 {
+ // want white
+ pval = 0
+ v = 255
+ }
+
+ bval := pval // bit value
+ if pix&coding.Invert != 0 {
+ bval ^= 1
+ }
+ if pinfo.HardZero && bval != 0 {
+ bval ^= 1
+ pval ^= 1
+ v ^= 255
+ }
+
+ // Set pixel value as we want it.
+ pinfo.Block.reset(pinfo.Bit, bval)
+
+ _, _ = x, y
+
+ err := targ - v
+ if x+1 < len(row) {
+ addDither(pixByOff, row[x+1], err*7/16)
+ }
+ if false && y+1 < len(p.Pixel) {
+ if x > 0 {
+ addDither(pixByOff, p.Pixel[y+1][x-1], err*3/16)
+ }
+ addDither(pixByOff, p.Pixel[y+1][x], err*5/16)
+ if x+1 < len(row) {
+ addDither(pixByOff, p.Pixel[y+1][x+1], err*1/16)
+ }
+ }
+ }
+ }
+
+ for _, bb := range bitblocks {
+ bb.copyOut()
+ }
+ }
+
+ noops := 0
+ // Copy numbers back out.
+ for i := 0; i < dbit/10; i++ {
+ // Pull out 10 bits.
+ v := 0
+ for j := 0; j < 10; j++ {
+ bi := uint(bbit + 10*i + j)
+ v <<= 1
+ v |= int((data[bi/8] >> (7 - bi&7)) & 1)
+ }
+ // Turn into 3 digits.
+ if v >= 1000 {
+ // Oops - too many 1 bits.
+ // We know the 512, 256, 128, 64, 32 bits are all set.
+ // Pick one at random to clear. This will break some
+ // checksum bits, but so be it.
+ println("oops", i, v)
+ pinfo := &pixByOff[bbit+10*i+3] // TODO random
+ pinfo.Contrast = 1e9 >> 8
+ pinfo.HardZero = true
+ noops++
+ }
+ num[i*3+0] = byte(v/100 + '0')
+ num[i*3+1] = byte(v/10%10 + '0')
+ num[i*3+2] = byte(v%10 + '0')
+ }
+ if noops > 0 {
+ goto Again
+ }
+
+ var b1 coding.Bits
+ coding.String(url).Encode(&b1, p.Version)
+ coding.Num(num).Encode(&b1, p.Version)
+ b1.AddCheckBytes(p.Version, p.Level)
+ if !bytes.Equal(b.Bytes(), b1.Bytes()) {
+ fmt.Printf("mismatch\n%d %x\n%d %x\n", len(b.Bytes()), b.Bytes(), len(b1.Bytes()), b1.Bytes())
+ panic("byte mismatch")
+ }
+
+ cc, err := p.Encode(coding.String(url), coding.Num(num))
+ if err != nil {
+ return err
+ }
+
+ if !m.Dither {
+ for y, row := range expect {
+ for x, pix := range row {
+ if cc.Black(x, y) != pix {
+ println("mismatch", x, y, p.Pixel[y][x].String())
+ }
+ }
+ }
+ }
+
+ m.Code = &qr.Code{Bitmap: cc.Bitmap, Size: cc.Size, Stride: cc.Stride, Scale: m.Scale}
+
+ if m.SaveControl {
+ m.Control = pngEncode(makeImage(req, "", "", 0, cc.Size, 4, m.Scale, func(x, y int) (rgba uint32) {
+ pix := p.Pixel[y][x]
+ if pix.Role() == coding.Data || pix.Role() == coding.Check {
+ pinfo := &pixByOff[pix.Offset()]
+ if pinfo.Block != nil {
+ if cc.Black(x, y) {
+ return 0x000000ff
+ }
+ return 0xffffffff
+ }
+ }
+ if cc.Black(x, y) {
+ return 0x3f3f3fff
+ }
+ return 0xbfbfbfff
+ }))
+ }
+
+ return nil
+}
+
+func addDither(pixByOff []Pixinfo, pix coding.Pixel, err int) {
+ if pix.Role() != coding.Data && pix.Role() != coding.Check {
+ return
+ }
+ pinfo := &pixByOff[pix.Offset()]
+ println("add", pinfo.X, pinfo.Y, pinfo.DTarg, err)
+ pinfo.DTarg += err
+}
+
+func readTarget(name string) ([][]int, error) {
+ f, err := os.Open(name)
+ if err != nil {
+ return nil, err
+ }
+ m, err := png.Decode(f)
+ if err != nil {
+ return nil, fmt.Errorf("decode %s: %v", name, err)
+ }
+ rect := m.Bounds()
+ target := make([][]int, rect.Dy())
+ for i := range target {
+ target[i] = make([]int, rect.Dx())
+ }
+ for y, row := range target {
+ for x := range row {
+ a := int(color.RGBAModel.Convert(m.At(x, y)).(color.RGBA).A)
+ t := int(color.GrayModel.Convert(m.At(x, y)).(color.Gray).Y)
+ if a == 0 {
+ t = -1
+ }
+ row[x] = t
+ }
+ }
+ return target, nil
+}
+
+type BitBlock struct {
+ DataBytes int
+ CheckBytes int
+ B []byte
+ M [][]byte
+ Tmp []byte
+ RS *gf256.RSEncoder
+ bdata []byte
+ cdata []byte
+}
+
+func newBlock(nd, nc int, rs *gf256.RSEncoder, dat, cdata []byte) *BitBlock {
+ b := &BitBlock{
+ DataBytes: nd,
+ CheckBytes: nc,
+ B: make([]byte, nd+nc),
+ Tmp: make([]byte, nc),
+ RS: rs,
+ bdata: dat,
+ cdata: cdata,
+ }
+ copy(b.B, dat)
+ rs.ECC(b.B[:nd], b.B[nd:])
+ b.check()
+ if !bytes.Equal(b.Tmp, cdata) {
+ panic("cdata")
+ }
+
+ b.M = make([][]byte, nd*8)
+ for i := range b.M {
+ row := make([]byte, nd+nc)
+ b.M[i] = row
+ for j := range row {
+ row[j] = 0
+ }
+ row[i/8] = 1 << (7 - uint(i%8))
+ rs.ECC(row[:nd], row[nd:])
+ }
+ return b
+}
+
+func (b *BitBlock) check() {
+ b.RS.ECC(b.B[:b.DataBytes], b.Tmp)
+ if !bytes.Equal(b.B[b.DataBytes:], b.Tmp) {
+ fmt.Printf("ecc mismatch\n%x\n%x\n", b.B[b.DataBytes:], b.Tmp)
+ panic("mismatch")
+ }
+}
+
+func (b *BitBlock) reset(bi uint, bval byte) {
+ if (b.B[bi/8]>>(7-bi&7))&1 == bval {
+ // already has desired bit
+ return
+ }
+ // rows that have already been set
+ m := b.M[len(b.M):cap(b.M)]
+ for _, row := range m {
+ if row[bi/8]&(1<<(7-bi&7)) != 0 {
+ // Found it.
+ for j, v := range row {
+ b.B[j] ^= v
+ }
+ return
+ }
+ }
+ panic("reset of unset bit")
+}
+
+func (b *BitBlock) canSet(bi uint, bval byte) bool {
+ found := false
+ m := b.M
+ for j, row := range m {
+ if row[bi/8]&(1<<(7-bi&7)) == 0 {
+ continue
+ }
+ if !found {
+ found = true
+ if j != 0 {
+ m[0], m[j] = m[j], m[0]
+ }
+ continue
+ }
+ for k := range row {
+ row[k] ^= m[0][k]
+ }
+ }
+ if !found {
+ return false
+ }
+
+ targ := m[0]
+
+ // Subtract from saved-away rows too.
+ for _, row := range m[len(m):cap(m)] {
+ if row[bi/8]&(1<<(7-bi&7)) == 0 {
+ continue
+ }
+ for k := range row {
+ row[k] ^= targ[k]
+ }
+ }
+
+ // Found a row with bit #bi == 1 and cut that bit from all the others.
+ // Apply to data and remove from m.
+ if (b.B[bi/8]>>(7-bi&7))&1 != bval {
+ for j, v := range targ {
+ b.B[j] ^= v
+ }
+ }
+ b.check()
+ n := len(m) - 1
+ m[0], m[n] = m[n], m[0]
+ b.M = m[:n]
+
+ for _, row := range b.M {
+ if row[bi/8]&(1<<(7-bi&7)) != 0 {
+ panic("did not reduce")
+ }
+ }
+
+ return true
+}
+
+func (b *BitBlock) copyOut() {
+ b.check()
+ copy(b.bdata, b.B[:b.DataBytes])
+ copy(b.cdata, b.B[b.DataBytes:])
+}
+
+func showtable(w http.ResponseWriter, b *BitBlock, gray func(int) bool) {
+ nd := b.DataBytes
+ nc := b.CheckBytes
+
+ fmt.Fprintf(w, "<table class='matrix' cellspacing=0 cellpadding=0 border=0>\n")
+ line := func() {
+ fmt.Fprintf(w, "<tr height=1 bgcolor='#bbbbbb'><td colspan=%d>\n", (nd+nc)*8)
+ }
+ line()
+ dorow := func(row []byte) {
+ fmt.Fprintf(w, "<tr>\n")
+ for i := 0; i < (nd+nc)*8; i++ {
+ fmt.Fprintf(w, "<td")
+ v := row[i/8] >> uint(7-i&7) & 1
+ if gray(i) {
+ fmt.Fprintf(w, " class='gray'")
+ }
+ fmt.Fprintf(w, ">")
+ if v == 1 {
+ fmt.Fprintf(w, "1")
+ }
+ }
+ line()
+ }
+
+ m := b.M[len(b.M):cap(b.M)]
+ for i := len(m) - 1; i >= 0; i-- {
+ dorow(m[i])
+ }
+ m = b.M
+ for _, row := range b.M {
+ dorow(row)
+ }
+
+ fmt.Fprintf(w, "</table>\n")
+}
+
+func BitsTable(w http.ResponseWriter, req *http.Request) {
+ nd := 2
+ nc := 2
+ fmt.Fprintf(w, `<html>
+ <style type='text/css'>
+ .matrix {
+ font-family: sans-serif;
+ font-size: 0.8em;
+ }
+ table.matrix {
+ padding-left: 1em;
+ padding-right: 1em;
+ padding-top: 1em;
+ padding-bottom: 1em;
+ }
+ .matrix td {
+ padding-left: 0.3em;
+ padding-right: 0.3em;
+ border-left: 2px solid white;
+ border-right: 2px solid white;
+ text-align: center;
+ color: #aaa;
+ }
+ .matrix td.gray {
+ color: black;
+ background-color: #ddd;
+ }
+ </style>
+ `)
+ rs := gf256.NewRSEncoder(coding.Field, nc)
+ dat := make([]byte, nd+nc)
+ b := newBlock(nd, nc, rs, dat[:nd], dat[nd:])
+ for i := 0; i < nd*8; i++ {
+ b.canSet(uint(i), 0)
+ }
+ showtable(w, b, func(i int) bool { return i < nd*8 })
+
+ b = newBlock(nd, nc, rs, dat[:nd], dat[nd:])
+ for j := 0; j < (nd+nc)*8; j += 2 {
+ b.canSet(uint(j), 0)
+ }
+ showtable(w, b, func(i int) bool { return i%2 == 0 })
+
+}
diff --git a/vendor/github.com/mattermost/rsc/qr/web/resize/resize.go b/vendor/github.com/mattermost/rsc/qr/web/resize/resize.go
new file mode 100644
index 000000000..02c8b0040
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/qr/web/resize/resize.go
@@ -0,0 +1,152 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package resize
+
+import (
+ "image"
+ "image/color"
+)
+
+// average convert the sums to averages and returns the result.
+func average(sum []uint64, w, h int, n uint64) *image.RGBA {
+ ret := image.NewRGBA(image.Rect(0, 0, w, h))
+ for y := 0; y < h; y++ {
+ for x := 0; x < w; x++ {
+ index := 4 * (y*w + x)
+ pix := ret.Pix[y*ret.Stride+x*4:]
+ pix[0] = uint8(sum[index+0] / n)
+ pix[1] = uint8(sum[index+1] / n)
+ pix[2] = uint8(sum[index+2] / n)
+ pix[3] = uint8(sum[index+3] / n)
+ }
+ }
+ return ret
+}
+
+// ResizeRGBA returns a scaled copy of the RGBA image slice r of m.
+// The returned image has width w and height h.
+func ResizeRGBA(m *image.RGBA, r image.Rectangle, w, h int) *image.RGBA {
+ ww, hh := uint64(w), uint64(h)
+ dx, dy := uint64(r.Dx()), uint64(r.Dy())
+ // See comment in Resize.
+ n, sum := dx*dy, make([]uint64, 4*w*h)
+ for y := r.Min.Y; y < r.Max.Y; y++ {
+ pix := m.Pix[(y-r.Min.Y)*m.Stride:]
+ for x := r.Min.X; x < r.Max.X; x++ {
+ // Get the source pixel.
+ p := pix[(x-r.Min.X)*4:]
+ r64 := uint64(p[0])
+ g64 := uint64(p[1])
+ b64 := uint64(p[2])
+ a64 := uint64(p[3])
+ // Spread the source pixel over 1 or more destination rows.
+ py := uint64(y) * hh
+ for remy := hh; remy > 0; {
+ qy := dy - (py % dy)
+ if qy > remy {
+ qy = remy
+ }
+ // Spread the source pixel over 1 or more destination columns.
+ px := uint64(x) * ww
+ index := 4 * ((py/dy)*ww + (px / dx))
+ for remx := ww; remx > 0; {
+ qx := dx - (px % dx)
+ if qx > remx {
+ qx = remx
+ }
+ qxy := qx * qy
+ sum[index+0] += r64 * qxy
+ sum[index+1] += g64 * qxy
+ sum[index+2] += b64 * qxy
+ sum[index+3] += a64 * qxy
+ index += 4
+ px += qx
+ remx -= qx
+ }
+ py += qy
+ remy -= qy
+ }
+ }
+ }
+ return average(sum, w, h, n)
+}
+
+// ResizeNRGBA returns a scaled copy of the RGBA image slice r of m.
+// The returned image has width w and height h.
+func ResizeNRGBA(m *image.NRGBA, r image.Rectangle, w, h int) *image.RGBA {
+ ww, hh := uint64(w), uint64(h)
+ dx, dy := uint64(r.Dx()), uint64(r.Dy())
+ // See comment in Resize.
+ n, sum := dx*dy, make([]uint64, 4*w*h)
+ for y := r.Min.Y; y < r.Max.Y; y++ {
+ pix := m.Pix[(y-r.Min.Y)*m.Stride:]
+ for x := r.Min.X; x < r.Max.X; x++ {
+ // Get the source pixel.
+ p := pix[(x-r.Min.X)*4:]
+ r64 := uint64(p[0])
+ g64 := uint64(p[1])
+ b64 := uint64(p[2])
+ a64 := uint64(p[3])
+ r64 = (r64 * a64) / 255
+ g64 = (g64 * a64) / 255
+ b64 = (b64 * a64) / 255
+ // Spread the source pixel over 1 or more destination rows.
+ py := uint64(y) * hh
+ for remy := hh; remy > 0; {
+ qy := dy - (py % dy)
+ if qy > remy {
+ qy = remy
+ }
+ // Spread the source pixel over 1 or more destination columns.
+ px := uint64(x) * ww
+ index := 4 * ((py/dy)*ww + (px / dx))
+ for remx := ww; remx > 0; {
+ qx := dx - (px % dx)
+ if qx > remx {
+ qx = remx
+ }
+ qxy := qx * qy
+ sum[index+0] += r64 * qxy
+ sum[index+1] += g64 * qxy
+ sum[index+2] += b64 * qxy
+ sum[index+3] += a64 * qxy
+ index += 4
+ px += qx
+ remx -= qx
+ }
+ py += qy
+ remy -= qy
+ }
+ }
+ }
+ return average(sum, w, h, n)
+}
+
+// Resample returns a resampled copy of the image slice r of m.
+// The returned image has width w and height h.
+func Resample(m image.Image, r image.Rectangle, w, h int) *image.RGBA {
+ if w < 0 || h < 0 {
+ return nil
+ }
+ if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
+ return image.NewRGBA(image.Rect(0, 0, w, h))
+ }
+ curw, curh := r.Dx(), r.Dy()
+ img := image.NewRGBA(image.Rect(0, 0, w, h))
+ for y := 0; y < h; y++ {
+ for x := 0; x < w; x++ {
+ // Get a source pixel.
+ subx := x * curw / w
+ suby := y * curh / h
+ r32, g32, b32, a32 := m.At(subx, suby).RGBA()
+ r := uint8(r32 >> 8)
+ g := uint8(g32 >> 8)
+ b := uint8(b32 >> 8)
+ a := uint8(a32 >> 8)
+ img.SetRGBA(x, y, color.RGBA{r, g, b, a})
+ }
+ }
+ return img
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/copy.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/copy.go
new file mode 100644
index 000000000..4e723260b
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/copy.go
@@ -0,0 +1,225 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Copied from code.google.com/p/codesearch/regexp/copy.go.
+
+// Copied from Go's regexp/syntax.
+// Formatters edited to handle instByteRange.
+
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "regexp/syntax"
+ "sort"
+ "strconv"
+ "unicode"
+)
+
+// cleanClass sorts the ranges (pairs of elements of r),
+// merges them, and eliminates duplicates.
+func cleanClass(rp *[]rune) []rune {
+
+ // Sort by lo increasing, hi decreasing to break ties.
+ sort.Sort(ranges{rp})
+
+ r := *rp
+ if len(r) < 2 {
+ return r
+ }
+
+ // Merge abutting, overlapping.
+ w := 2 // write index
+ for i := 2; i < len(r); i += 2 {
+ lo, hi := r[i], r[i+1]
+ if lo <= r[w-1]+1 {
+ // merge with previous range
+ if hi > r[w-1] {
+ r[w-1] = hi
+ }
+ continue
+ }
+ // new disjoint range
+ r[w] = lo
+ r[w+1] = hi
+ w += 2
+ }
+
+ return r[:w]
+}
+
+// appendRange returns the result of appending the range lo-hi to the class r.
+func appendRange(r []rune, lo, hi rune) []rune {
+ // Expand last range or next to last range if it overlaps or abuts.
+ // Checking two ranges helps when appending case-folded
+ // alphabets, so that one range can be expanding A-Z and the
+ // other expanding a-z.
+ n := len(r)
+ for i := 2; i <= 4; i += 2 { // twice, using i=2, i=4
+ if n >= i {
+ rlo, rhi := r[n-i], r[n-i+1]
+ if lo <= rhi+1 && rlo <= hi+1 {
+ if lo < rlo {
+ r[n-i] = lo
+ }
+ if hi > rhi {
+ r[n-i+1] = hi
+ }
+ return r
+ }
+ }
+ }
+
+ return append(r, lo, hi)
+}
+
+const (
+ // minimum and maximum runes involved in folding.
+ // checked during test.
+ minFold = 0x0041
+ maxFold = 0x1044f
+)
+
+// appendFoldedRange returns the result of appending the range lo-hi
+// and its case folding-equivalent runes to the class r.
+func appendFoldedRange(r []rune, lo, hi rune) []rune {
+ // Optimizations.
+ if lo <= minFold && hi >= maxFold {
+ // Range is full: folding can't add more.
+ return appendRange(r, lo, hi)
+ }
+ if hi < minFold || lo > maxFold {
+ // Range is outside folding possibilities.
+ return appendRange(r, lo, hi)
+ }
+ if lo < minFold {
+ // [lo, minFold-1] needs no folding.
+ r = appendRange(r, lo, minFold-1)
+ lo = minFold
+ }
+ if hi > maxFold {
+ // [maxFold+1, hi] needs no folding.
+ r = appendRange(r, maxFold+1, hi)
+ hi = maxFold
+ }
+
+ // Brute force. Depend on appendRange to coalesce ranges on the fly.
+ for c := lo; c <= hi; c++ {
+ r = appendRange(r, c, c)
+ f := unicode.SimpleFold(c)
+ for f != c {
+ r = appendRange(r, f, f)
+ f = unicode.SimpleFold(f)
+ }
+ }
+ return r
+}
+
+// ranges implements sort.Interface on a []rune.
+// The choice of receiver type definition is strange
+// but avoids an allocation since we already have
+// a *[]rune.
+type ranges struct {
+ p *[]rune
+}
+
+func (ra ranges) Less(i, j int) bool {
+ p := *ra.p
+ i *= 2
+ j *= 2
+ return p[i] < p[j] || p[i] == p[j] && p[i+1] > p[j+1]
+}
+
+func (ra ranges) Len() int {
+ return len(*ra.p) / 2
+}
+
+func (ra ranges) Swap(i, j int) {
+ p := *ra.p
+ i *= 2
+ j *= 2
+ p[i], p[i+1], p[j], p[j+1] = p[j], p[j+1], p[i], p[i+1]
+}
+
+func progString(p *syntax.Prog) string {
+ var b bytes.Buffer
+ dumpProg(&b, p)
+ return b.String()
+}
+
+func instString(i *syntax.Inst) string {
+ var b bytes.Buffer
+ dumpInst(&b, i)
+ return b.String()
+}
+
+func bw(b *bytes.Buffer, args ...string) {
+ for _, s := range args {
+ b.WriteString(s)
+ }
+}
+
+func dumpProg(b *bytes.Buffer, p *syntax.Prog) {
+ for j := range p.Inst {
+ i := &p.Inst[j]
+ pc := strconv.Itoa(j)
+ if len(pc) < 3 {
+ b.WriteString(" "[len(pc):])
+ }
+ if j == p.Start {
+ pc += "*"
+ }
+ bw(b, pc, "\t")
+ dumpInst(b, i)
+ bw(b, "\n")
+ }
+}
+
+func u32(i uint32) string {
+ return strconv.FormatUint(uint64(i), 10)
+}
+
+func dumpInst(b *bytes.Buffer, i *syntax.Inst) {
+ switch i.Op {
+ case syntax.InstAlt:
+ bw(b, "alt -> ", u32(i.Out), ", ", u32(i.Arg))
+ case syntax.InstAltMatch:
+ bw(b, "altmatch -> ", u32(i.Out), ", ", u32(i.Arg))
+ case syntax.InstCapture:
+ bw(b, "cap ", u32(i.Arg), " -> ", u32(i.Out))
+ case syntax.InstEmptyWidth:
+ bw(b, "empty ", u32(i.Arg), " -> ", u32(i.Out))
+ case syntax.InstMatch:
+ bw(b, "match")
+ case syntax.InstFail:
+ bw(b, "fail")
+ case syntax.InstNop:
+ bw(b, "nop -> ", u32(i.Out))
+ case instByteRange:
+ fmt.Fprintf(b, "byte %02x-%02x", (i.Arg>>8)&0xFF, i.Arg&0xFF)
+ if i.Arg&argFold != 0 {
+ bw(b, "/i")
+ }
+ bw(b, " -> ", u32(i.Out))
+
+ // Should not happen
+ case syntax.InstRune:
+ if i.Rune == nil {
+ // shouldn't happen
+ bw(b, "rune <nil>")
+ }
+ bw(b, "rune ", strconv.QuoteToASCII(string(i.Rune)))
+ if syntax.Flags(i.Arg)&syntax.FoldCase != 0 {
+ bw(b, "/i")
+ }
+ bw(b, " -> ", u32(i.Out))
+ case syntax.InstRune1:
+ bw(b, "rune1 ", strconv.QuoteToASCII(string(i.Rune)), " -> ", u32(i.Out))
+ case syntax.InstRuneAny:
+ bw(b, "any -> ", u32(i.Out))
+ case syntax.InstRuneAnyNotNL:
+ bw(b, "anynotnl -> ", u32(i.Out))
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/main.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/main.go
new file mode 100644
index 000000000..672337a04
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/main.go
@@ -0,0 +1,96 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "regexp/syntax"
+ "runtime/pprof"
+)
+
+var maxState = flag.Int("m", 1e5, "maximum number of states to explore")
+var cpuprof = flag.String("cpuprofile", "", "cpu profile file")
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, "usage: regmerge [-m maxstate] regexp [regexp2 regexp3....]\n")
+ os.Exit(2)
+ }
+ flag.Parse()
+
+ if len(flag.Args()) < 1 {
+ flag.Usage()
+ }
+
+ os.Exit(run(flag.Args()))
+}
+
+func run(args []string) int {
+ if *cpuprof != "" {
+ f, err := os.Create(*cpuprof)
+ if err != nil {
+ log.Fatal(err)
+ }
+ pprof.StartCPUProfile(f)
+ defer pprof.StopCPUProfile()
+ }
+
+ m, err := compile(flag.Args()...)
+ if err != nil {
+ log.Fatal(err)
+ }
+ n := 100
+ for ;; n *= 2 {
+ if n >= *maxState {
+ if n >= 2* *maxState {
+ fmt.Printf("reached state limit\n")
+ return 1
+ }
+ n = *maxState
+ }
+ log.Printf("try %d states...\n", n)
+ s, err := m.findMatch(n)
+ if err == nil {
+ fmt.Printf("%q\n", s)
+ return 0
+ }
+ if err != ErrMemory {
+ fmt.Printf("failed: %s\n", err)
+ return 3
+ }
+ }
+ panic("unreachable")
+}
+
+func compile(exprs ...string) (*matcher, error) {
+ var progs []*syntax.Prog
+ for _, expr := range exprs {
+ re, err := syntax.Parse(expr, syntax.Perl)
+ if err != nil {
+ return nil, err
+ }
+ sre := re.Simplify()
+ prog, err := syntax.Compile(sre)
+ if err != nil {
+ return nil, err
+ }
+ if err := toByteProg(prog); err != nil {
+ return nil, err
+ }
+ progs = append(progs, prog)
+ }
+ m := &matcher{}
+ if err := m.init(joinProgs(progs), len(progs)); err != nil {
+ return nil, err
+ }
+ return m, nil
+}
+
+func bug() {
+ panic("regmerge: internal error")
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/match.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/match.go
new file mode 100644
index 000000000..6a4b1f967
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/match.go
@@ -0,0 +1,406 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Copied from code.google.com/p/codesearch/regexp/copy.go
+// and adapted for the problem of finding a matching string, not
+// testing whether a particular string matches.
+
+package main
+
+import (
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "hash/fnv"
+ "hash"
+ "log"
+ "regexp/syntax"
+)
+
+// A matcher holds the state for running regular expression search.
+type matcher struct {
+ buf []byte
+ prog *syntax.Prog // compiled program
+ dstate map[uint32]*dstate // dstate cache
+ start *dstate // start state
+ startLine *dstate // start state for beginning of line
+ z1, z2, z3 nstate // three temporary nstates
+ ids []int
+ numState int
+ maxState int
+ numByte int
+ numMatch int
+ undo [256]byte
+ all *dstate
+ allTail **dstate
+ h hash.Hash32
+}
+
+// An nstate corresponds to an NFA state.
+type nstate struct {
+ q Set // queue of program instructions
+ flag flags // flags (TODO)
+ needFlag syntax.EmptyOp
+}
+
+// The flags record state about a position between bytes in the text.
+type flags uint32
+
+const (
+ flagBOL flags = 1 << iota // beginning of line
+ flagEOL // end of line
+ flagBOT // beginning of text
+ flagEOT // end of text
+ flagWord // last byte was word byte
+)
+
+// A dstate corresponds to a DFA state.
+type dstate struct {
+ enc string // encoded nstate
+ nextAll *dstate
+ nextHash *dstate
+ prev *dstate
+ prevByte int
+ done bool
+}
+
+func (z *nstate) String() string {
+ return fmt.Sprintf("%v/%#x+%#x", z.q.Dense(), z.flag, z.needFlag)
+}
+
+// enc encodes z as a string.
+func (m *matcher) enc(z *nstate) []byte {
+ buf := m.buf[:0]
+ buf = append(buf, byte(z.needFlag), byte(z.flag))
+ ids := m.ids[:0]
+ for _, id := range z.q.Dense() {
+ ids = append(ids, int(id))
+ }
+ sortInts(ids)
+ last := ^uint32(0)
+ for _, id := range ids {
+ x := uint32(id)-last
+ last = uint32(id)
+ for x >= 0x80 {
+ buf = append(buf, byte(x)|0x80)
+ x >>= 7
+ }
+ buf = append(buf, byte(x))
+ }
+ m.buf = buf
+ return buf
+}
+
+// dec decodes the encoding s into z.
+func (m *matcher) dec(z *nstate, s string) {
+ b := append(m.buf[:0], s...)
+ m.buf = b
+ z.needFlag = syntax.EmptyOp(b[0])
+ b = b[1:]
+ i, n := binary.Uvarint(b)
+ if n <= 0 {
+ bug()
+ }
+ b = b[n:]
+ z.flag = flags(i)
+ z.q.Reset()
+ last := ^uint32(0)
+ for len(b) > 0 {
+ i, n = binary.Uvarint(b)
+ if n <= 0 {
+ bug()
+ }
+ b = b[n:]
+ last += uint32(i)
+ z.q.Add(last, 0, 0)
+ }
+}
+
+// init initializes the matcher.
+func (m *matcher) init(prog *syntax.Prog, n int) error {
+ m.prog = prog
+ m.dstate = make(map[uint32]*dstate)
+ m.numMatch = n
+ m.maxState = 10
+ m.allTail = &m.all
+ m.numByte = 256
+ for i := range m.undo {
+ m.undo[i] = byte(i)
+ }
+ m.h = fnv.New32()
+
+ m.z1.q.Init(uint32(len(prog.Inst)))
+ m.z2.q.Init(uint32(len(prog.Inst)))
+ m.z3.q.Init(uint32(len(prog.Inst)))
+ m.ids = make([]int, 0, len(prog.Inst))
+
+ m.addq(&m.z1.q, uint32(prog.Start), syntax.EmptyBeginLine|syntax.EmptyBeginText)
+ m.z1.flag = flagBOL | flagBOT
+ m.start = m.cache(&m.z1, nil, 0)
+
+ m.z1.q.Reset()
+ m.addq(&m.z1.q, uint32(prog.Start), syntax.EmptyBeginLine)
+ m.z1.flag = flagBOL
+ m.startLine = m.cache(&m.z1, nil, 0)
+
+ m.crunchProg()
+
+ return nil
+}
+
+// stepEmpty steps runq to nextq expanding according to flag.
+func (m *matcher) stepEmpty(runq, nextq *Set, flag syntax.EmptyOp) {
+ nextq.Reset()
+ for _, id := range runq.Dense() {
+ m.addq(nextq, id, flag)
+ }
+}
+
+// stepByte steps runq to nextq consuming c and then expanding according to flag.
+// It returns true if a match ends immediately before c.
+// c is either an input byte or endText.
+func (m *matcher) stepByte(runq, nextq *Set, c int, flag syntax.EmptyOp) (match bool) {
+ nextq.Reset()
+ m.addq(nextq, uint32(m.prog.Start), flag)
+
+ nmatch := 0
+ for _, id := range runq.Dense() {
+ i := &m.prog.Inst[id]
+ switch i.Op {
+ default:
+ continue
+ case syntax.InstMatch:
+ nmatch++
+ continue
+ case instByteRange:
+ if c == endText {
+ break
+ }
+ lo := int((i.Arg >> 8) & 0xFF)
+ hi := int(i.Arg & 0xFF)
+ if i.Arg&argFold != 0 && 'a' <= c && c <= 'z' {
+ c += 'A' - 'a'
+ }
+ if lo <= c && c <= hi {
+ m.addq(nextq, i.Out, flag)
+ }
+ }
+ }
+ return nmatch == m.numMatch
+}
+
+// addq adds id to the queue, expanding according to flag.
+func (m *matcher) addq(q *Set, id uint32, flag syntax.EmptyOp) {
+ if q.Has(id, 0) {
+ return
+ }
+ q.MustAdd(id)
+ i := &m.prog.Inst[id]
+ switch i.Op {
+ case syntax.InstCapture, syntax.InstNop:
+ m.addq(q, i.Out, flag)
+ case syntax.InstAlt, syntax.InstAltMatch:
+ m.addq(q, i.Out, flag)
+ m.addq(q, i.Arg, flag)
+ case syntax.InstEmptyWidth:
+ if syntax.EmptyOp(i.Arg)&^flag == 0 {
+ m.addq(q, i.Out, flag)
+ }
+ }
+}
+
+const endText = -1
+
+// computeNext computes the next DFA state if we're in d reading c (an input byte or endText).
+func (m *matcher) computeNext(this, next *nstate, d *dstate, c int) bool {
+ // compute flags in effect before c
+ flag := syntax.EmptyOp(0)
+ if this.flag&flagBOL != 0 {
+ flag |= syntax.EmptyBeginLine
+ }
+ if this.flag&flagBOT != 0 {
+ flag |= syntax.EmptyBeginText
+ }
+ if this.flag&flagWord != 0 {
+ if !isWordByte(c) {
+ flag |= syntax.EmptyWordBoundary
+ } else {
+ flag |= syntax.EmptyNoWordBoundary
+ }
+ } else {
+ if isWordByte(c) {
+ flag |= syntax.EmptyWordBoundary
+ } else {
+ flag |= syntax.EmptyNoWordBoundary
+ }
+ }
+ if c == '\n' {
+ flag |= syntax.EmptyEndLine
+ }
+ if c == endText {
+ flag |= syntax.EmptyEndLine | syntax.EmptyEndText
+ }
+
+ if flag &= this.needFlag; flag != 0 {
+ // re-expand queue using new flags.
+ // TODO: only do this when it matters
+ // (something is gating on word boundaries).
+ m.stepEmpty(&this.q, &next.q, flag)
+ this, next = next, &m.z3
+ }
+
+ // now compute flags after c.
+ flag = 0
+ next.flag = 0
+ if c == '\n' {
+ flag |= syntax.EmptyBeginLine
+ next.flag |= flagBOL
+ }
+ if isWordByte(c) {
+ next.flag |= flagWord
+ }
+
+ // re-add start, process rune + expand according to flags.
+ if m.stepByte(&this.q, &next.q, c, flag) {
+ return true
+ }
+ next.needFlag = m.queueFlag(&next.q)
+ if next.needFlag&syntax.EmptyBeginLine == 0 {
+ next.flag &^= flagBOL
+ }
+ if next.needFlag&(syntax.EmptyWordBoundary|syntax.EmptyNoWordBoundary) == 0{
+ next.flag &^= flagWord
+ }
+
+ m.cache(next, d, c)
+ return false
+}
+
+func (m *matcher) queueFlag(runq *Set) syntax.EmptyOp {
+ var e uint32
+ for _, id := range runq.Dense() {
+ i := &m.prog.Inst[id]
+ if i.Op == syntax.InstEmptyWidth {
+ e |= i.Arg
+ }
+ }
+ return syntax.EmptyOp(e)
+}
+
+func (m *matcher) hash(enc []byte) uint32 {
+ m.h.Reset()
+ m.h.Write(enc)
+ return m.h.Sum32()
+}
+
+func (m *matcher) find(h uint32, enc []byte) *dstate {
+Search:
+ for d := m.dstate[h]; d!=nil; d=d.nextHash {
+ s := d.enc
+ if len(s) != len(enc) {
+ continue Search
+ }
+ for i, b := range enc {
+ if s[i] != b {
+ continue Search
+ }
+ }
+ return d
+ }
+ return nil
+}
+
+func (m *matcher) cache(z *nstate, prev *dstate, prevByte int) *dstate {
+ enc := m.enc(z)
+ h := m.hash(enc)
+ d := m.find(h, enc)
+ if d != nil {
+ return d
+ }
+
+ if m.numState >= m.maxState {
+ panic(ErrMemory)
+ }
+ m.numState++
+ d = &dstate{
+ enc: string(enc),
+ prev: prev,
+ prevByte: prevByte,
+ nextHash: m.dstate[h],
+ }
+ m.dstate[h] = d
+ *m.allTail = d
+ m.allTail = &d.nextAll
+
+ return d
+}
+
+// isWordByte reports whether the byte c is a word character: ASCII only.
+// This is used to implement \b and \B. This is not right for Unicode, but:
+// - it's hard to get right in a byte-at-a-time matching world
+// (the DFA has only one-byte lookahead)
+// - this crude approximation is the same one PCRE uses
+func isWordByte(c int) bool {
+ return 'A' <= c && c <= 'Z' ||
+ 'a' <= c && c <= 'z' ||
+ '0' <= c && c <= '9' ||
+ c == '_'
+}
+
+var ErrNoMatch = errors.New("no matching strings")
+var ErrMemory = errors.New("exhausted memory")
+
+func (m *matcher) findMatch(maxState int) (s string, err error) {
+ defer func() {
+ switch r := recover().(type) {
+ case nil:
+ return
+ case error:
+ err = r
+ return
+ default:
+ panic(r)
+ }
+ }()
+
+ m.maxState = maxState
+ numState := 0
+ var d *dstate
+ var c int
+ for d = m.all; d != nil; d = d.nextAll {
+ numState++
+ if d.done {
+ continue
+ }
+ this, next := &m.z1, &m.z2
+ m.dec(this, d.enc)
+ if m.computeNext(this, next, d, endText) {
+ c = endText
+ goto Found
+ }
+ for _, cb := range m.undo[:m.numByte] {
+ if m.computeNext(this, next, d, int(cb)) {
+ c = int(cb)
+ goto Found
+ }
+ }
+ d.done = true
+ }
+ log.Printf("searched %d states; queued %d states", numState, m.numState)
+ return "", ErrNoMatch
+
+Found:
+ var buf []byte
+ if c >= 0 {
+ buf = append(buf, byte(c))
+ }
+ for d1 := d; d1.prev != nil; d1= d1.prev {
+ buf = append(buf, byte(d1.prevByte))
+ }
+ for i, j := 0, len(buf)-1; i < j; i, j = i+1, j-1 {
+ buf[i], buf[j] = buf[j], buf[i]
+ }
+ log.Printf("searched %d states; queued %d states", numState, m.numState)
+ return string(buf), nil
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/merge.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/merge.go
new file mode 100644
index 000000000..15f51cfa7
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/merge.go
@@ -0,0 +1,103 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Code to merge (join) multiple regexp progs into a single prog.
+// New code; not copied from anywhere.
+
+package main
+
+import "regexp/syntax"
+
+func joinProgs(progs []*syntax.Prog) *syntax.Prog {
+ all := &syntax.Prog{}
+ for i, p := range progs {
+ n := len(all.Inst)
+ all.Inst = append(all.Inst, p.Inst...)
+ match := shiftInst(all.Inst[n:], n)
+ if match < 0 {
+ // no match instruction; give up
+ all.Inst = []syntax.Inst{{Op: syntax.InstFail}}
+ all.Start = 0
+ return all
+ }
+ match += n
+ m := len(all.Inst)
+ all.Inst = append(all.Inst,
+ syntax.Inst{Op: syntax.InstAlt, Out: uint32(p.Start+n), Arg: uint32(m+1)},
+ syntax.Inst{Op: instByteRange, Arg: 0x00FF, Out: uint32(m)},
+ syntax.Inst{Op: instByteRange, Arg: 0x00FF, Out: uint32(match)},
+ syntax.Inst{Op: syntax.InstMatch},
+ )
+ all.Inst[match] = syntax.Inst{Op: syntax.InstAlt, Out: uint32(m+2), Arg: uint32(m+3)}
+
+ if i == 0 {
+ all.Start = m
+ } else {
+ old := all.Start
+ all.Start = len(all.Inst)
+ all.Inst = append(all.Inst, syntax.Inst{Op: syntax.InstAlt, Out: uint32(old), Arg: uint32(m)})
+ }
+ }
+
+ return all
+}
+
+func shiftInst(inst []syntax.Inst, n int) int {
+ match := -1
+ for i := range inst {
+ ip := &inst[i]
+ ip.Out += uint32(n)
+ if ip.Op == syntax.InstMatch {
+ if match >= 0 {
+ panic("double match")
+ }
+ match = i
+ }
+ if ip.Op == syntax.InstAlt || ip.Op == syntax.InstAltMatch {
+ ip.Arg += uint32(n)
+ }
+ }
+ return match
+}
+
+func (m *matcher) crunchProg() {
+ var rewrite [256]byte
+
+ for i := range m.prog.Inst {
+ ip := &m.prog.Inst[i]
+ switch ip.Op {
+ case instByteRange:
+ lo, hi := byte(ip.Arg>>8), byte(ip.Arg)
+ rewrite[lo] = 1
+ if hi < 255 {
+ rewrite[hi+1] = 1
+ }
+ case syntax.InstEmptyWidth:
+ switch op := syntax.EmptyOp(ip.Arg); {
+ case op&(syntax.EmptyBeginLine|syntax.EmptyEndLine) != 0:
+ rewrite['\n'] = 1
+ rewrite['\n'+1] = 1
+ case op&(syntax.EmptyWordBoundary|syntax.EmptyNoWordBoundary) != 0:
+ rewrite['A'] = 1
+ rewrite['Z'+1] = 1
+ rewrite['a'] = 1
+ rewrite['z'+1] = 1
+ rewrite['0'] = 1
+ rewrite['9'+1] = 1
+ rewrite['_'] = 1
+ rewrite['_'+1] = 1
+ }
+ }
+ }
+
+ rewrite[0] = 0
+ for i := 1; i < 256; i++ {
+ rewrite[i] += rewrite[i-1]
+ }
+ m.numByte = int(rewrite[255]) + 1
+
+ for i := 255; i >= 0; i-- {
+ m.undo[rewrite[i]] = byte(i)
+ }
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/sort.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/sort.go
new file mode 100644
index 000000000..e40f87714
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/sort.go
@@ -0,0 +1,199 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Copy of go/src/pkg/sort/sort.go, specialized for []int
+// and to remove some array indexing.
+
+package main
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
+
+// Insertion sort
+func insertionSort(data []int, a, b int) {
+ for i := a + 1; i < b; i++ {
+ for j := i; j > a && data[j] < data[j-1]; j-- {
+ data[j], data[j-1] = data[j-1], data[j]
+ }
+ }
+}
+
+// siftDown implements the heap property on data[lo, hi).
+// first is an offset into the array where the root of the heap lies.
+func siftDown(data []int, lo, hi, first int) {
+ root := lo
+ for {
+ child := 2*root + 1
+ if child >= hi {
+ break
+ }
+ if child+1 < hi && data[first+child] < data[first+child+1] {
+ child++
+ }
+ if !(data[first+root] < data[first+child]) {
+ return
+ }
+ data[first+root], data[first+child] = data[first+child], data[first+root]
+ root = child
+ }
+}
+
+func heapSort(data []int, a, b int) {
+ first := a
+ lo := 0
+ hi := b - a
+
+ // Build heap with greatest element at top.
+ for i := (hi - 1) / 2; i >= 0; i-- {
+ siftDown(data, i, hi, first)
+ }
+
+ // Pop elements, largest first, into end of data.
+ for i := hi - 1; i >= 0; i-- {
+ data[first], data[first+i] = data[first+i], data[first]
+ siftDown(data, lo, i, first)
+ }
+}
+
+// Quicksort, following Bentley and McIlroy,
+// ``Engineering a Sort Function,'' SP&E November 1993.
+
+// medianOfThree moves the median of the three values data[a], data[b], data[c] into data[a].
+func medianOfThree(data []int, a, b, c int) {
+ m0 := b
+ m1 := a
+ m2 := c
+ // bubble sort on 3 elements
+ if data[m1] < data[m0] {
+ data[m1], data[m0] = data[m0], data[m1]
+ }
+ if data[m2] < data[m1] {
+ data[m2], data[m1] = data[m1], data[m2]
+ }
+ if data[m1] < data[m0] {
+ data[m1], data[m0] = data[m0], data[m1]
+ }
+ // now data[m0] <= data[m1] <= data[m2]
+}
+
+func swapRange(data []int, a, b, n int) {
+ for i := 0; i < n; i++ {
+ data[a+i], data[b+i] = data[b+i], data[a+i]
+ }
+}
+
+func doPivot(data []int, lo, hi int) (midlo, midhi int) {
+ m := lo + (hi-lo)/2 // Written like this to avoid integer overflow.
+ if hi-lo > 40 {
+ // Tukey's ``Ninther,'' median of three medians of three.
+ s := (hi - lo) / 8
+ medianOfThree(data, lo, lo+s, lo+2*s)
+ medianOfThree(data, m, m-s, m+s)
+ medianOfThree(data, hi-1, hi-1-s, hi-1-2*s)
+ }
+ medianOfThree(data, lo, m, hi-1)
+
+ // Invariants are:
+ // data[lo] = pivot (set up by ChoosePivot)
+ // data[lo <= i < a] = pivot
+ // data[a <= i < b] < pivot
+ // data[b <= i < c] is unexamined
+ // data[c <= i < d] > pivot
+ // data[d <= i < hi] = pivot
+ //
+ // Once b meets c, can swap the "= pivot" sections
+ // into the middle of the slice.
+ pivot := lo
+ a, b, c, d := lo+1, lo+1, hi, hi
+ dpivot := data[pivot]
+ db, dc1 := data[b], data[c-1]
+ for b < c {
+ if db < dpivot { // data[b] < pivot
+ b++
+ if b < c {
+ db = data[b]
+ }
+ continue
+ }
+ if !(dpivot < db) { // data[b] = pivot
+ data[a], data[b] = db, data[a]
+ a++
+ b++
+ if b < c {
+ db = data[b]
+ }
+ continue
+ }
+ if dpivot < dc1 { // data[c-1] > pivot
+ c--
+ if c > 0 {
+ dc1 = data[c-1]
+ }
+ continue
+ }
+ if !(dc1 < dpivot) { // data[c-1] = pivot
+ data[c-1], data[d-1] = data[d-1], dc1
+ c--
+ d--
+ if c > 0 {
+ dc1 = data[c-1]
+ }
+ continue
+ }
+ // data[b] > pivot; data[c-1] < pivot
+ data[b], data[c-1] = dc1, db
+ b++
+ c--
+ if b < c {
+ db = data[b]
+ dc1 = data[c-1]
+ }
+ }
+
+ n := min(b-a, a-lo)
+ swapRange(data, lo, b-n, n)
+
+ n = min(hi-d, d-c)
+ swapRange(data, c, hi-n, n)
+
+ return lo + b - a, hi - (d - c)
+}
+
+func quickSort(data []int, a, b, maxDepth int) {
+ for b-a > 7 {
+ if maxDepth == 0 {
+ heapSort(data, a, b)
+ return
+ }
+ maxDepth--
+ mlo, mhi := doPivot(data, a, b)
+ // Avoiding recursion on the larger subproblem guarantees
+ // a stack depth of at most lg(b-a).
+ if mlo-a < b-mhi {
+ quickSort(data, a, mlo, maxDepth)
+ a = mhi // i.e., quickSort(data, mhi, b)
+ } else {
+ quickSort(data, mhi, b, maxDepth)
+ b = mlo // i.e., quickSort(data, a, mlo)
+ }
+ }
+ if b-a > 1 {
+ insertionSort(data, a, b)
+ }
+}
+
+func sortInts(data []int) {
+ // Switch to heapsort if depth of 2*ceil(lg(n)) is reached.
+ n := len(data)
+ maxDepth := 0
+ for 1<<uint(maxDepth) < n {
+ maxDepth++
+ }
+ maxDepth *= 2
+ quickSort(data, 0, n, maxDepth)
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/sparse.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/sparse.go
new file mode 100644
index 000000000..461027080
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/sparse.go
@@ -0,0 +1,80 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Copied from code.google.com/p/codesearch/sparse/set.go,
+// tweaked for better inlinability.
+
+package main
+
+// For comparison: running cindex over the Linux 2.6 kernel with this
+// implementation of trigram sets takes 11 seconds. If I change it to
+// a bitmap (which must be cleared between files) it takes 25 seconds.
+
+// A Set is a sparse set of uint32 values.
+// http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html
+type Set struct {
+ dense []uint32
+ sparse []uint32
+}
+
+// NewSet returns a new Set with a given maximum size.
+// The set can contain numbers in [0, max-1].
+func NewSet(max uint32) *Set {
+ return &Set{
+ sparse: make([]uint32, max),
+ }
+}
+
+// Init initializes a Set to have a given maximum size.
+// The set can contain numbers in [0, max-1].
+func (s *Set) Init(max uint32) {
+ s.sparse = make([]uint32, max)
+}
+
+// Reset clears (empties) the set.
+func (s *Set) Reset() {
+ s.dense = s.dense[:0]
+}
+
+// Add adds x to the set if it is not already there.
+//
+// TODO: The ugly additional variables v and n make the
+// function inlinable. When the 6g inliner gets better
+// they will not be necessary.
+func (s *Set) Add(x uint32, v uint32, n int) {
+ v = s.sparse[x]
+ if v < uint32(len(s.dense)) && s.dense[v] == x {
+ return
+ }
+ n = len(s.dense)
+ s.sparse[x] = uint32(n)
+ s.dense = append(s.dense, x)
+}
+
+func (s *Set) MustAdd(x uint32) {
+ s.sparse[x] = uint32(len(s.dense))
+ s.dense = append(s.dense, x)
+}
+
+// Has reports whether x is in the set.
+//
+// TODO: The ugly additional variables v makes
+// function inlinable. When the 6g inliner gets better
+// it will not be necessary.
+func (s *Set) Has(x uint32, v uint32) bool {
+ v = s.sparse[x]
+ return v < uint32(len(s.dense)) && s.dense[v] == x
+}
+
+// Dense returns the values in the set.
+// The values are listed in the order in which they
+// were inserted.
+func (s *Set) Dense() []uint32 {
+ return s.dense
+}
+
+// Len returns the number of values in the set.
+func (s *Set) Len() int {
+ return len(s.dense)
+}
diff --git a/vendor/github.com/mattermost/rsc/regexp/regmerge/utf.go b/vendor/github.com/mattermost/rsc/regexp/regmerge/utf.go
new file mode 100644
index 000000000..605e01c10
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/regexp/regmerge/utf.go
@@ -0,0 +1,270 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Copy of code.google.com/p/codesearch/regexp/utf.go.
+
+package main
+
+import (
+ "regexp/syntax"
+ "unicode"
+ "unicode/utf8"
+)
+
+const (
+ instFail = syntax.InstFail
+ instAlt = syntax.InstAlt
+ instByteRange = syntax.InstRune | 0x80 // local opcode
+
+ argFold = 1 << 16
+)
+
+func toByteProg(prog *syntax.Prog) error {
+ var b runeBuilder
+ for pc := range prog.Inst {
+ i := &prog.Inst[pc]
+ switch i.Op {
+ case syntax.InstRune, syntax.InstRune1:
+ // General rune range. PIA.
+ // TODO: Pick off single-byte case.
+ if lo, hi, fold, ok := oneByteRange(i); ok {
+ i.Op = instByteRange
+ i.Arg = uint32(lo)<<8 | uint32(hi)
+ if fold {
+ i.Arg |= argFold
+ }
+ break
+ }
+
+ r := i.Rune
+ if syntax.Flags(i.Arg)&syntax.FoldCase != 0 {
+ // Build folded list.
+ var rr []rune
+ if len(r) == 1 {
+ rr = appendFoldedRange(rr, r[0], r[0])
+ } else {
+ for j := 0; j < len(r); j += 2 {
+ rr = appendFoldedRange(rr, r[j], r[j+1])
+ }
+ }
+ r = rr
+ }
+
+ b.init(prog, uint32(pc), i.Out)
+ if len(r) == 1 {
+ b.addRange(r[0], r[0], false)
+ } else {
+ for j := 0; j < len(r); j += 2 {
+ b.addRange(r[j], r[j+1], false)
+ }
+ }
+
+ case syntax.InstRuneAny, syntax.InstRuneAnyNotNL:
+ // All runes.
+ // AnyNotNL should exclude \n but the line-at-a-time
+ // execution takes care of that for us.
+ b.init(prog, uint32(pc), i.Out)
+ b.addRange(0, unicode.MaxRune, false)
+ }
+ }
+ return nil
+}
+
+func oneByteRange(i *syntax.Inst) (lo, hi byte, fold, ok bool) {
+ if i.Op == syntax.InstRune1 {
+ r := i.Rune[0]
+ if r < utf8.RuneSelf {
+ return byte(r), byte(r), false, true
+ }
+ }
+ if i.Op != syntax.InstRune {
+ return
+ }
+ fold = syntax.Flags(i.Arg)&syntax.FoldCase != 0
+ if len(i.Rune) == 1 || len(i.Rune) == 2 && i.Rune[0] == i.Rune[1] {
+ r := i.Rune[0]
+ if r >= utf8.RuneSelf {
+ return
+ }
+ if fold && !asciiFold(r) {
+ return
+ }
+ return byte(r), byte(r), fold, true
+ }
+ if len(i.Rune) == 2 && i.Rune[1] < utf8.RuneSelf {
+ if fold {
+ for r := i.Rune[0]; r <= i.Rune[1]; r++ {
+ if asciiFold(r) {
+ return
+ }
+ }
+ }
+ return byte(i.Rune[0]), byte(i.Rune[1]), fold, true
+ }
+ if len(i.Rune) == 4 && i.Rune[0] == i.Rune[1] && i.Rune[2] == i.Rune[3] && unicode.SimpleFold(i.Rune[0]) == i.Rune[2] && unicode.SimpleFold(i.Rune[2]) == i.Rune[0] {
+ return byte(i.Rune[0]), byte(i.Rune[0]), true, true
+ }
+
+ return
+}
+
+func asciiFold(r rune) bool {
+ if r >= utf8.RuneSelf {
+ return false
+ }
+ r1 := unicode.SimpleFold(r)
+ if r1 >= utf8.RuneSelf {
+ return false
+ }
+ if r1 == r {
+ return true
+ }
+ return unicode.SimpleFold(r1) == r
+}
+
+func maxRune(n int) rune {
+ b := 0
+ if n == 1 {
+ b = 7
+ } else {
+ b = 8 - (n + 1) + 6*(n-1)
+ }
+ return 1<<uint(b) - 1
+}
+
+type cacheKey struct {
+ lo, hi uint8
+ fold bool
+ next uint32
+}
+
+type runeBuilder struct {
+ begin uint32
+ out uint32
+ cache map[cacheKey]uint32
+ p *syntax.Prog
+}
+
+func (b *runeBuilder) init(p *syntax.Prog, begin, out uint32) {
+ // We will rewrite p.Inst[begin] to hold the accumulated
+ // machine. For now, there is no match.
+ p.Inst[begin].Op = instFail
+
+ b.begin = begin
+ b.out = out
+ if b.cache == nil {
+ b.cache = make(map[cacheKey]uint32)
+ }
+ for k := range b.cache {
+ delete(b.cache, k)
+ }
+ b.p = p
+}
+
+func (b *runeBuilder) uncachedSuffix(lo, hi byte, fold bool, next uint32) uint32 {
+ if next == 0 {
+ next = b.out
+ }
+ pc := len(b.p.Inst)
+ i := syntax.Inst{Op: instByteRange, Arg: uint32(lo)<<8 | uint32(hi), Out: next}
+ if fold {
+ i.Arg |= argFold
+ }
+ b.p.Inst = append(b.p.Inst, i)
+ return uint32(pc)
+}
+
+func (b *runeBuilder) suffix(lo, hi byte, fold bool, next uint32) uint32 {
+ if lo < 0x80 || hi > 0xbf {
+ // Not a continuation byte, no need to cache.
+ return b.uncachedSuffix(lo, hi, fold, next)
+ }
+
+ key := cacheKey{lo, hi, fold, next}
+ if pc, ok := b.cache[key]; ok {
+ return pc
+ }
+
+ pc := b.uncachedSuffix(lo, hi, fold, next)
+ b.cache[key] = pc
+ return pc
+}
+
+func (b *runeBuilder) addBranch(pc uint32) {
+ // Add pc to the branch at the beginning.
+ i := &b.p.Inst[b.begin]
+ switch i.Op {
+ case syntax.InstFail:
+ i.Op = syntax.InstNop
+ i.Out = pc
+ return
+ case syntax.InstNop:
+ i.Op = syntax.InstAlt
+ i.Arg = pc
+ return
+ case syntax.InstAlt:
+ apc := uint32(len(b.p.Inst))
+ b.p.Inst = append(b.p.Inst, syntax.Inst{Op: instAlt, Out: i.Arg, Arg: pc})
+ i = &b.p.Inst[b.begin]
+ i.Arg = apc
+ b.begin = apc
+ }
+}
+
+func (b *runeBuilder) addRange(lo, hi rune, fold bool) {
+ if lo > hi {
+ return
+ }
+
+ // TODO: Pick off 80-10FFFF for special handling?
+ if lo == 0x80 && hi == 0x10FFFF {
+ }
+
+ // Split range into same-length sized ranges.
+ for i := 1; i < utf8.UTFMax; i++ {
+ max := maxRune(i)
+ if lo <= max && max < hi {
+ b.addRange(lo, max, fold)
+ b.addRange(max+1, hi, fold)
+ return
+ }
+ }
+
+ // ASCII range is special.
+ if hi < utf8.RuneSelf {
+ b.addBranch(b.suffix(byte(lo), byte(hi), fold, 0))
+ return
+ }
+
+ // Split range into sections that agree on leading bytes.
+ for i := 1; i < utf8.UTFMax; i++ {
+ m := rune(1)<<uint(6*i) - 1 // last i bytes of UTF-8 sequence
+ if lo&^m != hi&^m {
+ if lo&m != 0 {
+ b.addRange(lo, lo|m, fold)
+ b.addRange((lo|m)+1, hi, fold)
+ return
+ }
+ if hi&m != m {
+ b.addRange(lo, hi&^m-1, fold)
+ b.addRange(hi&^m, hi, fold)
+ return
+ }
+ }
+ }
+
+ // Finally. Generate byte matching equivalent for lo-hi.
+ var ulo, uhi [utf8.UTFMax]byte
+ n := utf8.EncodeRune(ulo[:], lo)
+ m := utf8.EncodeRune(uhi[:], hi)
+ if n != m {
+ panic("codesearch/regexp: bad utf-8 math")
+ }
+
+ pc := uint32(0)
+ for i := n - 1; i >= 0; i-- {
+ pc = b.suffix(ulo[i], uhi[i], false, pc)
+ }
+ b.addBranch(pc)
+}
diff --git a/vendor/github.com/mattermost/rsc/rosetta/graph/Makefile b/vendor/github.com/mattermost/rsc/rosetta/graph/Makefile
new file mode 100644
index 000000000..1cb49f788
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/rosetta/graph/Makefile
@@ -0,0 +1,7 @@
+include $(GOROOT)/src/Make.inc
+
+TARG=rsc.googlecode.com/hg/rosetta/graph
+GOFILES=\
+ graph.go\
+
+include $(GOROOT)/src/Make.pkg
diff --git a/vendor/github.com/mattermost/rsc/rosetta/graph/graph.go b/vendor/github.com/mattermost/rsc/rosetta/graph/graph.go
new file mode 100644
index 000000000..359617d41
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/rosetta/graph/graph.go
@@ -0,0 +1,133 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Simple demonstration of a graph interface and
+// Dijkstra's algorithm built on top of that interface,
+// without using inheritance.
+
+package graph
+
+import "container/heap"
+
+// A Graph is the interface implemented by graphs that
+// this package can run algorithms on.
+type Graph interface {
+ // NumVertex returns the number of vertices in the graph.
+ NumVertex() int
+
+ // VertexID returns a vertex ID, 0 <= ID < NumVertex(), for v.
+ VertexID(v Vertex) int
+
+ // Neighbors returns a slice of vertices that are adjacent
+ // to v in the graph.
+ Neighbors(v Vertex) []Vertex
+}
+
+type Vertex interface {
+ String() string
+}
+
+// ShortestPath uses Dijkstra's algorithm to find the shortest
+// path from start to end in g. It returns the path as a slice of
+// vertices, with start first and end last. If there is no path,
+// ShortestPath returns nil.
+func ShortestPath(g Graph, start, end Vertex) []Vertex {
+ d := newDijkstra(g)
+
+ d.visit(start, 1, nil)
+ for !d.empty() {
+ p := d.next()
+ if g.VertexID(p.v) == g.VertexID(end) {
+ break
+ }
+ for _, v := range g.Neighbors(p.v) {
+ d.visit(v, p.depth+1, p)
+ }
+ }
+
+ p := d.pos(end)
+ if p.depth == 0 {
+ // unvisited - no path
+ return nil
+ }
+ path := make([]Vertex, p.depth)
+ for ; p != nil; p = p.parent {
+ path[p.depth-1] = p.v
+ }
+ return path
+}
+
+// A dpos is a position in the Dijkstra traversal.
+type dpos struct {
+ depth int
+ heapIndex int
+ v Vertex
+ parent *dpos
+}
+
+// A dijkstra is the Dijkstra traversal's work state.
+// It contains the heap queue and per-vertex information.
+type dijkstra struct {
+ g Graph
+ q []*dpos
+ byID []dpos
+}
+
+func newDijkstra(g Graph) *dijkstra {
+ d := &dijkstra{g: g}
+ d.byID = make([]dpos, g.NumVertex())
+ return d
+}
+
+func (d *dijkstra) pos(v Vertex) *dpos {
+ p := &d.byID[d.g.VertexID(v)]
+ p.v = v // in case this is the first time we've seen it
+ return p
+}
+
+func (d *dijkstra) visit(v Vertex, depth int, parent *dpos) {
+ p := d.pos(v)
+ if p.depth == 0 {
+ p.parent = parent
+ p.depth = depth
+ heap.Push(d, p)
+ }
+}
+
+func (d *dijkstra) empty() bool {
+ return len(d.q) == 0
+}
+
+func (d *dijkstra) next() *dpos {
+ return heap.Pop(d).(*dpos)
+}
+
+// Implementation of heap.Interface
+func (d *dijkstra) Len() int {
+ return len(d.q)
+}
+
+func (d *dijkstra) Less(i, j int) bool {
+ return d.q[i].depth < d.q[j].depth
+}
+
+func (d *dijkstra) Swap(i, j int) {
+ d.q[i], d.q[j] = d.q[j], d.q[i]
+ d.q[i].heapIndex = i
+ d.q[j].heapIndex = j
+}
+
+func (d *dijkstra) Push(x interface{}) {
+ p := x.(*dpos)
+ p.heapIndex = len(d.q)
+ d.q = append(d.q, p)
+}
+
+func (d *dijkstra) Pop() interface{} {
+ n := len(d.q)
+ x := d.q[n-1]
+ d.q = d.q[:n-1]
+ x.heapIndex = -1
+ return x
+}
diff --git a/vendor/github.com/mattermost/rsc/rosetta/maze/Makefile b/vendor/github.com/mattermost/rsc/rosetta/maze/Makefile
new file mode 100644
index 000000000..8a83abd05
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/rosetta/maze/Makefile
@@ -0,0 +1,8 @@
+include $(GOROOT)/src/Make.inc
+
+TARG=maze
+GOFILES=\
+ maze.go\
+
+include $(GOROOT)/src/Make.cmd
+
diff --git a/vendor/github.com/mattermost/rsc/rosetta/maze/maze.go b/vendor/github.com/mattermost/rsc/rosetta/maze/maze.go
new file mode 100644
index 000000000..3cd15d04f
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/rosetta/maze/maze.go
@@ -0,0 +1,191 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Rosetta Code-inspired maze generator and solver.
+// Demonstrates use of interfaces to separate algorithm
+// implementations (graph.ShortestPath, heap.*) from data.
+// (In contrast, multiple inheritance approaches require
+// you to store their data in your data structures as part
+// of the inheritance.)
+
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "math/rand"
+ "time"
+
+ "github.com/mattermost/rsc/rosetta/graph"
+)
+
+type Maze struct {
+ w, h int
+ grid [][]walls
+}
+
+type Dir uint
+
+const (
+ North Dir = iota
+ East
+ West
+ South
+)
+
+type walls uint8
+
+const allWalls walls = 1<<North | 1<<East | 1<<South | 1<<West
+
+var dirs = []struct {
+ δx, δy int
+}{
+ {0, -1},
+ {1, 0},
+ {-1, 0},
+ {0, 1},
+}
+
+// move returns the cell in the direction dir from position r, c.
+// It returns ok==false if there is no cell in that direction.
+func (m *Maze) move(x, y int, dir Dir) (nx, ny int, ok bool) {
+ nx = x + dirs[dir].δx
+ ny = y + dirs[dir].δy
+ ok = 0 <= nx && nx < m.w && 0 <= ny && ny < m.h
+ return
+}
+
+// Move returns the cell in the direction dir from position x, y
+// It returns ok==false if there is no cell in that direction
+// or if a wall blocks movement in that direction.
+func (m *Maze) Move(x, y int, dir Dir) (nx, ny int, ok bool) {
+ nx, ny, ok = m.move(x, y, dir)
+ ok = ok && m.grid[y][x]&(1<<dir) == 0
+ return
+}
+
+// NewMaze returns a new, randomly generated maze
+// of width w and height h.
+func NewMaze(w, h int) *Maze {
+ // Allocate one slice for the whole 2-d cell grid and break up into rows.
+ all := make([]walls, w*h)
+ for i := range all {
+ all[i] = allWalls
+ }
+ m := &Maze{w: w, h: h, grid: make([][]walls, h)}
+ for i := range m.grid {
+ m.grid[i], all = all[:w], all[w:]
+ }
+
+ // All cells start with all walls.
+ m.generate(rand.Intn(w), rand.Intn(h))
+
+ return m
+}
+
+func (m *Maze) generate(x, y int) {
+ i := rand.Intn(4)
+ for j := 0; j < 4; j++ {
+ dir := Dir(i+j) % 4
+ if nx, ny, ok := m.move(x, y, dir); ok && m.grid[ny][nx] == allWalls {
+ // break down wall
+ m.grid[y][x] &^= 1 << dir
+ m.grid[ny][nx] &^= 1 << (3 - dir)
+ m.generate(nx, ny)
+ }
+ }
+}
+
+// String returns a multi-line string representation of the maze.
+func (m *Maze) String() string {
+ return m.PathString(nil)
+}
+
+// PathString returns the multi-line string representation of the
+// maze with the path marked on it.
+func (m *Maze) PathString(path []graph.Vertex) string {
+ var b bytes.Buffer
+ wall := func(w, m walls, ch byte) {
+ if w&m != 0 {
+ b.WriteByte(ch)
+ } else {
+ b.WriteByte(' ')
+ }
+ }
+ for _, row := range m.grid {
+ b.WriteByte('+')
+ for _, cell := range row {
+ wall(cell, 1<<North, '-')
+ b.WriteByte('+')
+ }
+ b.WriteString("\n")
+ for _, cell := range row {
+ wall(cell, 1<<West, '|')
+ b.WriteByte(' ')
+ }
+ b.WriteString("|\n")
+ }
+ for i := 0; i < m.w; i++ {
+ b.WriteString("++")
+ }
+ b.WriteString("+")
+ grid := b.Bytes()
+
+ // Overlay path.
+ last := -1
+ for _, v := range path {
+ p := v.(pos)
+ i := (2*m.w+2)*(2*p.y+1) + 2*p.x + 1
+ grid[i] = '#'
+ if last != -1 {
+ grid[(i+last)/2] = '#'
+ }
+ last = i
+ }
+
+ return string(grid)
+}
+
+// Implement graph.Graph.
+
+type pos struct {
+ x, y int
+}
+
+func (p pos) String() string {
+ return fmt.Sprintf("%d,%d", p.x, p.y)
+}
+
+func (m *Maze) Neighbors(v graph.Vertex) []graph.Vertex {
+ p := v.(pos)
+ var neighbors []graph.Vertex
+ for dir := North; dir <= South; dir++ {
+ if nx, ny, ok := m.Move(p.x, p.y, dir); ok {
+ neighbors = append(neighbors, pos{nx, ny})
+ }
+ }
+ return neighbors
+}
+
+func (m *Maze) NumVertex() int {
+ return m.w * m.h
+}
+
+func (m *Maze) VertexID(v graph.Vertex) int {
+ p := v.(pos)
+ return p.y*m.w + p.x
+}
+
+func (m *Maze) Vertex(x, y int) graph.Vertex {
+ return pos{x, y}
+}
+
+func main() {
+ const w, h = 30, 10
+ rand.Seed(time.Now().UnixNano())
+
+ m := NewMaze(w, h)
+ path := graph.ShortestPath(m, m.Vertex(0, 0), m.Vertex(w-1, h-1))
+ fmt.Println(m.PathString(path))
+}
diff --git a/vendor/github.com/mattermost/rsc/s3get/main.go b/vendor/github.com/mattermost/rsc/s3get/main.go
new file mode 100644
index 000000000..579e8bb5f
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/s3get/main.go
@@ -0,0 +1,85 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// S3get fetches a single object from or lists the objects in an S3 bucket.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+
+ "github.com/mattermost/rsc/keychain"
+ "launchpad.net/goamz/aws"
+ "launchpad.net/goamz/s3"
+)
+
+var list = flag.Bool("l", false, "list buckets")
+var delim = flag.String("d", "", "list delimiter")
+
+func usage() {
+ fmt.Fprintf(os.Stderr, `usage: s3get [-l] bucket path
+
+s3get fetches a single object from or lists the objects
+in an S3 bucket.
+
+The -l flag causes s3get to list available paths.
+When using -l, path may be omitted.
+Otherwise the listing begins at path.
+
+s3get uses the user name and password in the local
+keychain for the server 's3.amazonaws.com' as the S3
+access key (user name) and secret key (password).
+`)
+ os.Exit(2)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ var buck, obj string
+ args := flag.Args()
+ switch len(args) {
+ case 1:
+ buck = args[0]
+ if !*list {
+ fmt.Fprintf(os.Stderr, "must specify path when not using -l")
+ os.Exit(2)
+ }
+ case 2:
+ buck = args[0]
+ obj = args[1]
+ default:
+ usage()
+ }
+
+ access, secret, err := keychain.UserPasswd("s3.amazonaws.com", "")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ auth := aws.Auth{AccessKey: access, SecretKey: secret}
+
+ b := s3.New(auth, aws.USEast).Bucket(buck)
+ if *list {
+ objs, prefixes, err := b.List("", *delim, obj, 0)
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, p := range prefixes {
+ fmt.Printf("%s\n", p)
+ }
+ for _, obj := range objs {
+ fmt.Printf("%s\n", obj.Key)
+ }
+ return
+ }
+
+ data, err := b.Get(obj)
+ if err != nil {
+ log.Fatal(err)
+ }
+ os.Stdout.Write(data)
+}
diff --git a/vendor/github.com/mattermost/rsc/smugmug/smug.go b/vendor/github.com/mattermost/rsc/smugmug/smug.go
new file mode 100644
index 000000000..8cb662223
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/smugmug/smug.go
@@ -0,0 +1,542 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package smugmug uses the SmugMug API to manipulate photo albums
+// stored on smugmug.com.
+package smugmug
+
+import (
+ "bytes"
+ "crypto/md5"
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "strconv"
+ "strings"
+)
+
+const smugUploadHost = "upload.smugmug.com"
+const smugAPI = "1.2.2"
+const smugURL = "https://secure.smugmug.com/services/api/json/" + smugAPI + "/"
+const smugURLUnencrypted = "http://api.smugmug.com/services/api/json/" + smugAPI + "/"
+
+// A Conn represents an authenticated connection to the SmugMug server.
+type Conn struct {
+ sessid string
+ apiKey string
+
+ NickName string
+}
+
+// A Category represents a single album category.
+type Category struct {
+ ID int `json:"id"`
+ Name string
+}
+
+type smugResult struct {
+ Stat string `json:"stat"`
+ Message string `json:"message"`
+}
+
+type loginResult struct {
+ Login struct {
+ Session struct {
+ ID string `json:"id"`
+ }
+ User struct {
+ ID int `json:"id"`
+ NickName string
+ DisplayName string
+ }
+ }
+}
+
+// Login logs into the SmugMug server with the given email address and password.
+// The apikey argument is the API Key for your application.
+// To obtain an API Key, see http://www.smugmug.com/hack/apikeys.
+func Login(email, passwd, apikey string) (*Conn, error) {
+ c := &Conn{}
+ var out loginResult
+ if err := c.do("smugmug.login.withPassword", &out, "APIKey", apikey, "EmailAddress", email, "Password", passwd); err != nil {
+ return nil, err
+ }
+
+ c.sessid = out.Login.Session.ID
+ if c.sessid == "" {
+ return nil, fmt.Errorf("SmugMug login appeared to succeed but did not return session ID")
+ }
+
+ c.NickName = out.Login.User.NickName
+ if c.NickName == "" {
+ return nil, fmt.Errorf("SmugMug login appeared to succeed but did not return User NickName")
+ }
+
+ return c, nil
+}
+
+// Categories returns the album categories for the user identified by the nick name.
+func (c *Conn) Categories(nick string) ([]*Category, error) {
+ var out struct {
+ Categories []*Category
+ }
+ if err := c.do("smugmug.categories.get", &out, "NickName", nick); err != nil {
+ return nil, err
+ }
+ return out.Categories, nil
+}
+
+// CreateCategory creates a category with the given name.
+func (c *Conn) CreateCategory(name string) (*Category, error) {
+ var out struct {
+ Category *Category
+ }
+ if err := c.do("smugmug.categories.create", &out, "Name", name); err != nil {
+ return nil, err
+ }
+ return out.Category, nil
+}
+
+// DeleteCategory deletes the category.
+func (c *Conn) DeleteCategory(cat *Category) error {
+ return c.do("smugmug.categories.delete", nil, "CategoryID", strconv.Itoa(cat.ID))
+}
+
+// An Album represents a single photo album.
+type Album struct {
+ ID int `json:"id"`
+ Key string
+ Title string
+ URL string
+}
+
+// Albums returns the albums for the user identified by the nick name.
+// Use c.NickName for the logged-in user.
+func (c *Conn) Albums(nick string) ([]*Album, error) {
+ var out struct {
+ Albums []*Album
+ }
+ if err := c.do("smugmug.albums.get", &out, "NickName", nick); err != nil {
+ return nil, err
+ }
+ return out.Albums, nil
+}
+
+// CreateAlbum creates a new album.
+func (c *Conn) CreateAlbum(title string) (*Album, error) {
+ var out struct {
+ Album *Album
+ }
+ if err := c.do("smugmug.albums.create", &out,
+ "Title", title,
+ "Public", "0",
+ "WorldSearchable", "0",
+ "SmugSearchable", "0",
+ ); err != nil {
+ return nil, err
+ }
+
+ if out.Album == nil || out.Album.Key == "" {
+ return nil, fmt.Errorf("unable to parse SmugMug result")
+ }
+ return out.Album, nil
+}
+
+// AlbumInfo returns detailed metadata about an album.
+func (c *Conn) AlbumInfo(album *Album) (*AlbumInfo, error) {
+ var out struct {
+ Album *AlbumInfo
+ }
+ if err := c.do("smugmug.albums.getInfo", &out,
+ "AlbumID", strconv.Itoa(album.ID),
+ "AlbumKey", album.Key,
+ ); err != nil {
+ return nil, err
+ }
+
+ if out.Album == nil || out.Album.ID == 0 {
+ return nil, fmt.Errorf("unable to parse SmugMug result")
+ }
+ return out.Album, nil
+}
+
+// An AlbumInfo lists the metadata for an album.
+type AlbumInfo struct {
+ ID int `json:"id"`
+ Key string
+ Title string
+
+ Backprinting string
+ BoutiquePackaging int
+ CanRank bool
+ Category *Category
+ Clean bool
+ ColorCorrection int
+ Comments bool
+ Community struct {
+ ID int `json:"id"`
+ Name string
+ }
+ Description string
+ EXIF bool
+ External bool
+ FamilyEdit bool
+ Filenames bool
+ FriendEdit bool
+ Geography bool
+ Header bool
+ HideOwner bool
+ Highlight struct {
+ ID int `json:"id"`
+ Key string
+ Type string
+ }
+ ImageCount int
+ InterceptShipping int
+ Keywords string
+ Larges bool
+ LastUpdated string
+ NiceName string
+ Originals bool
+ PackagingBranding bool
+ Password string
+ PasswordHint string
+ Passworded bool
+ Position int
+ Printable bool
+ Printmark struct {
+ ID int `json:"id"`
+ Name string
+ }
+ ProofDays int
+ Protected bool
+ Public bool
+ Share bool
+ SmugSearchable bool
+ SortDirection bool
+ SortMethod string
+ SquareThumbs bool
+ SubCategory *Category
+ Template struct {
+ ID int `json:"id"`
+ }
+ Theme struct {
+ ID int `json:"id"`
+ Key string
+ Type string
+ }
+ URL string
+ UnsharpAmount float64
+ UnsharpRadius float64
+ UnsharpSigma float64
+ Watermark struct {
+ ID int `json:"id"`
+ Name string
+ }
+ Watermarking bool
+ WorldSearchable bool
+ X2Larges bool
+ X3Larges bool
+ XLarges bool
+}
+
+// ChangeAlbum changes an album's settings.
+// The argument list is a sequence of key, value pairs.
+// The keys are the names of AlbumInfo struct fields,
+// and the values are string values. For a boolean field,
+// use "0" for false and "1" for true.
+//
+// Example:
+// c.ChangeAlbum(a, "Larges", "1", "Title", "My Album")
+//
+func (c *Conn) ChangeAlbum(album *Album, args ...string) error {
+ callArgs := append([]string{"AlbumID", strconv.Itoa(album.ID)}, args...)
+ return c.do("smugmug.albums.changeSettings", nil, callArgs...)
+}
+
+// DeleteAlbum deletes an album.
+func (c *Conn) DeleteAlbum(album *Album) error {
+ return c.do("smugmug.albums.delete", nil, "AlbumID", strconv.Itoa(album.ID))
+}
+
+// An Image represents a single SmugMug image.
+type Image struct {
+ ID int `json:"id"`
+ Key string
+ URL string
+}
+
+// Images returns a list of images for an album.
+func (c *Conn) Images(album *Album) ([]*Image, error) {
+ var out struct {
+ Album struct {
+ Images []*Image
+ }
+ }
+
+ if err := c.do("smugmug.images.get", &out,
+ "AlbumID", strconv.Itoa(album.ID),
+ "AlbumKey", album.Key,
+ "Heavy", "1",
+ ); err != nil {
+ return nil, err
+ }
+
+ return out.Album.Images, nil
+}
+
+// An ImageInfo lists the metadata for an image.
+type ImageInfo struct {
+ ID int `json:"id"`
+ Key string
+ Album *Album
+ Altitude int
+ Caption string
+ Date string
+ FileName string
+ Duration int
+ Format string
+ Height int
+ Hidden bool
+ Keywords string
+ LargeURL string
+ LastUpdated string
+ Latitude float64
+ LightboxURL string
+ Longitude float64
+ MD5Sum string
+ MediumURL string
+ OriginalURL string
+ Position int
+ Serial int
+ Size int
+ SmallURL string
+ ThumbURL string
+ TinyURL string
+ Video320URL string
+ Video640URL string
+ Video960URL string
+ Video1280URL string
+ Video1920URL string
+ Width int
+ X2LargeURL string
+ X3LargeURL string
+ XLargeURL string
+}
+
+// ImageInfo returns detailed metadata about an image.
+func (c *Conn) ImageInfo(image *Image) (*ImageInfo, error) {
+ var out struct {
+ Image *ImageInfo
+ }
+ if err := c.do("smugmug.images.getInfo", &out,
+ "ImageID", strconv.Itoa(image.ID),
+ "ImageKey", image.Key,
+ ); err != nil {
+ return nil, err
+ }
+
+ if out.Image == nil || out.Image.ID == 0 {
+ return nil, fmt.Errorf("unable to parse SmugMug result")
+ }
+ return out.Image, nil
+}
+
+// ChangeImage changes an image's settings.
+// The argument list is a sequence of key, value pairs.
+// The keys are the names of ImageInfo struct fields,
+// and the values are string values. For a boolean field,
+// use "0" for false and "1" for true.
+//
+// Example:
+// c.ChangeImage(a, "Caption", "me!", "Hidden", "0")
+//
+func (c *Conn) ChangeImage(image *Image, args ...string) error {
+ callArgs := append([]string{"ImageID", strconv.Itoa(image.ID)}, args...)
+ return c.do("smugmug.images.changeSettings", nil, callArgs...)
+}
+
+// An ImageEXIF lists the EXIF data associated with an image.
+type ImageEXIF struct {
+ ID int `json:"id"`
+ Key string
+ Aperture string
+ Brightness string
+ CCDWidth string
+ ColorSpace int
+ CompressedBitsPerPixel string
+ Contrast int
+ DateTime string
+ DateTimeDigitized string
+ DateTimeOriginal string
+ DigitalZoomRatio string
+ ExposureBiasValue string
+ ExposureMode int
+ ExposureProgram int
+ ExposureTime string
+ Flash int
+ FocalLength string
+ FocalLengthIn35mmFilm string
+ ISO int
+ LightSource int
+ Make string
+ Metering int
+ Model string
+ Saturation int
+ SensingMethod int
+ Sharpness int
+ SubjectDistance string
+ SubjectDistanceRange int
+ WhiteBalance int
+}
+
+// ImageInfo returns the EXIF data for an image.
+func (c *Conn) ImageEXIF(image *Image) (*ImageEXIF, error) {
+ var out struct {
+ Image *ImageEXIF
+ }
+ if err := c.do("smugmug.images.getEXIF", &out,
+ "ImageID", strconv.Itoa(image.ID),
+ "ImageKey", image.Key,
+ ); err != nil {
+ return nil, err
+ }
+
+ if out.Image == nil || out.Image.ID == 0 {
+ return nil, fmt.Errorf("unable to parse SmugMug result")
+ }
+ return out.Image, nil
+}
+
+// DeleteImage deletes an image.
+func (c *Conn) DeleteImage(image *Image) error {
+ return c.do("smugmug.images.delete", nil, "ImageID", strconv.Itoa(image.ID))
+}
+
+// AddImage uploads a new image to an album.
+// The name is the file name that will be displayed on SmugMug.
+// The data is the raw image data.
+func (c *Conn) AddImage(name string, data []byte, a *Album) (*Image, error) {
+ return c.upload(name, data, "AlbumID", a.ID)
+}
+
+// ReplaceImage replaces an image.
+// The name is the file name that will be displayed on SmugMug.
+// The data is the raw image data.
+func (c *Conn) ReplaceImage(name string, data []byte, image *Image) (*Image, error) {
+ return c.upload(name, data, "ImageID", image.ID)
+}
+
+func (c *Conn) upload(name string, data []byte, idkind string, id int) (*Image, error) {
+ h := md5.New()
+ h.Write(data)
+ digest := fmt.Sprintf("%x", h.Sum(nil))
+
+ req := &http.Request{
+ Method: "PUT",
+ URL: &url.URL{
+ Scheme: "http",
+ Host: smugUploadHost,
+ Path: "/" + name,
+ },
+ ContentLength: int64(len(data)),
+ Header: http.Header{
+ "Content-MD5": {digest},
+ "X-Smug-SessionID": {c.sessid},
+ "X-Smug-Version": {smugAPI},
+ "X-Smug-ResponseType": {"JSON"},
+ "X-Smug-" + idkind: {strconv.Itoa(id)},
+ "X-Smug-FileName": {name},
+ },
+ Body: ioutil.NopCloser(bytes.NewBuffer(data)),
+ }
+
+ r, err := http.DefaultTransport.RoundTrip(req)
+ if err != nil {
+ return nil, fmt.Errorf("upload %s: %s", name, err)
+ }
+
+ var out struct {
+ Image *Image
+ }
+ if err := c.parseResult("upload", r, &out); err != nil {
+ return nil, fmt.Errorf("upload %s: %s", name, err)
+ }
+ return out.Image, nil
+}
+
+func (c *Conn) do(method string, dst interface{}, args ...string) (err error) {
+ defer func() {
+ if err != nil {
+ err = fmt.Errorf("%s: %s", method, err)
+ }
+ }()
+
+ form := url.Values{
+ "method": {method},
+ "APIKey": {c.apiKey},
+ "Pretty": {"1"}, // nice-looking JSON
+ }
+ if c.sessid != "" {
+ form["SessionID"] = []string{c.sessid}
+ }
+ for i := 0; i < len(args); i += 2 {
+ key, val := args[i], args[i+1]
+ form[key] = []string{val}
+ }
+
+ url := smugURL
+ if !strings.Contains(method, "login") {
+ // I'd really prefer to use HTTPS for everything,
+ // but I get "invalid API key" if I do.
+ url = smugURLUnencrypted
+ }
+ r, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()))
+ if err != nil {
+ return err
+ }
+
+ return c.parseResult(method, r, dst)
+}
+
+func (c *Conn) parseResult(method string, r *http.Response, dst interface{}) error {
+ defer r.Body.Close()
+ if r.StatusCode != 200 {
+ return fmt.Errorf("HTTP %s", r.Status)
+ }
+ data, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return fmt.Errorf("reading body: %s", err)
+ }
+
+ var res smugResult
+ if err := json.Unmarshal(data, &res); err != nil {
+ return fmt.Errorf("parsing JSON result: %s", err)
+ }
+
+ // If there are no images, that's not an error.
+ // But SmugMug says it is.
+ if res.Stat == "fail" && method == "smugmug.images.get" && res.Message == "empty set - no images found" {
+ res.Stat = "ok"
+ data = []byte(`{"Images": []}`)
+ }
+
+ if res.Stat != "ok" {
+ msg := res.Stat
+ if res.Message != "" {
+ msg = res.Message
+ }
+ return fmt.Errorf("%s", msg)
+ }
+
+ if dst != nil {
+ if err := json.Unmarshal(data, dst); err != nil {
+ return fmt.Errorf("parsing JSON result: %s", err)
+ }
+ }
+
+ return nil
+}
diff --git a/vendor/github.com/mattermost/rsc/smugmug/smugup/main.go b/vendor/github.com/mattermost/rsc/smugmug/smugup/main.go
new file mode 100644
index 000000000..0b40e9db6
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/smugmug/smugup/main.go
@@ -0,0 +1,158 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Smugup uploads a collection of photos to SmugMug.
+//
+// Run 'smugup -help' for details.
+package main
+
+import (
+ "crypto/md5"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os"
+ "path/filepath"
+
+ "github.com/mattermost/rsc/keychain"
+ "github.com/mattermost/rsc/smugmug"
+)
+
+const apiKey = "8qH4UgiunBKpsYpvcBXftbCYNEreAZ0m"
+
+var usageMessage = `usage: smugup [options] 'album title' [photo.jpg ...]
+
+Smugup creates a new album with the given title if one does not already exist.
+Then uploads the list of images to the album. If a particular image
+already exists in the album, the JPG replaces the album image if the
+contents differ.
+
+Smugup fetches the SmugMug user name and password from the
+user's keychain.
+
+By default, new albums are created as private as possible: not public,
+not world searchable, and not SmugMug-searchable.
+
+Smugup prints the URL for the album when finished.
+
+The options are:
+
+ -u user
+ SmugMug user account name (email address).
+ This is not typically needed, as the user name found in the keychain will be used.
+`
+
+var smugUser = flag.String("u", "", "SmugMug user name")
+
+func usage() {
+ fmt.Fprint(os.Stderr, usageMessage)
+ os.Exit(2)
+}
+
+func main() {
+ flag.Usage = usage
+ flag.Parse()
+ log.SetFlags(0)
+
+ args := flag.Args()
+ if len(args) < 1 {
+ usage()
+ }
+ title, files := args[0], args[1:]
+
+ user, passwd, err := keychain.UserPasswd("smugmug.com", *smugUser)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ smug, err := smugmug.Login(user, passwd, apiKey)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ albums, err := smug.Albums(smug.NickName)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var a *smugmug.Album
+ for _, a = range albums {
+ if a.Title == title {
+ goto HaveAlbum
+ }
+ }
+
+ a, err = smug.CreateAlbum(title)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+HaveAlbum:
+ imageFiles := map[string]*smugmug.ImageInfo{}
+ if len(files) > 0 {
+ images, err := smug.Images(a)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ n := 0
+ c := make(chan *smugmug.ImageInfo)
+ rate := make(chan bool, 4)
+ for _, image := range images {
+ go func(image *smugmug.Image) {
+ rate <- true
+ info, err := smug.ImageInfo(image)
+ <-rate
+ if err != nil {
+ log.Print(err)
+ c <- nil
+ return
+ }
+ c <- info
+ }(image)
+ n++
+ }
+
+ for i := 0; i < n; i++ {
+ info := <-c
+ if info == nil {
+ continue
+ }
+ imageFiles[info.FileName] = info
+ }
+ }
+
+ for _, file := range files {
+ data, err := ioutil.ReadFile(file)
+ if err != nil {
+ log.Print(err)
+ continue
+ }
+ _, elem := filepath.Split(file)
+ info := imageFiles[elem]
+ if info != nil {
+ h := md5.New()
+ h.Write(data)
+ digest := fmt.Sprintf("%x", h.Sum(nil))
+ if digest == info.MD5Sum {
+ // Already have that image.
+ continue
+ }
+ _, err = smug.ReplaceImage(file, data, &smugmug.Image{ID: info.ID})
+ } else {
+ _, err = smug.AddImage(file, data, a)
+ }
+ if err != nil {
+ log.Print(err)
+ }
+ }
+
+ info, err := smug.AlbumInfo(a)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ fmt.Printf("%s\n", info.URL)
+}
diff --git a/vendor/github.com/mattermost/rsc/tmp/app b/vendor/github.com/mattermost/rsc/tmp/app
new file mode 120000
index 000000000..5df94d993
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/app
@@ -0,0 +1 @@
+../app \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/appfs b/vendor/github.com/mattermost/rsc/tmp/appfs
new file mode 120000
index 000000000..d38e0b442
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/appfs
@@ -0,0 +1 @@
+../appfs \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/arq b/vendor/github.com/mattermost/rsc/tmp/arq
new file mode 120000
index 000000000..25b25add9
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/arq
@@ -0,0 +1 @@
+../arq \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/blog b/vendor/github.com/mattermost/rsc/tmp/blog
new file mode 120000
index 000000000..c9e36e109
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/blog
@@ -0,0 +1 @@
+../blog \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/cmd b/vendor/github.com/mattermost/rsc/tmp/cmd
new file mode 120000
index 000000000..9287aae2a
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/cmd
@@ -0,0 +1 @@
+../cmd \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/crypt b/vendor/github.com/mattermost/rsc/tmp/crypt
new file mode 120000
index 000000000..22e80df6e
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/crypt
@@ -0,0 +1 @@
+../crypt \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/devweb b/vendor/github.com/mattermost/rsc/tmp/devweb
new file mode 120000
index 000000000..27bf20aa9
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/devweb
@@ -0,0 +1 @@
+../devweb \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/fuse b/vendor/github.com/mattermost/rsc/tmp/fuse
new file mode 120000
index 000000000..dad1b521b
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/fuse
@@ -0,0 +1 @@
+../fuse \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/gf256 b/vendor/github.com/mattermost/rsc/tmp/gf256
new file mode 120000
index 000000000..386a8217c
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/gf256
@@ -0,0 +1 @@
+../gf256 \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/google b/vendor/github.com/mattermost/rsc/tmp/google
new file mode 120000
index 000000000..cb899d262
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/google
@@ -0,0 +1 @@
+../google \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/gtfs b/vendor/github.com/mattermost/rsc/tmp/gtfs
new file mode 120000
index 000000000..a1dc5a7b6
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/gtfs
@@ -0,0 +1 @@
+../gtfs \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/imap b/vendor/github.com/mattermost/rsc/tmp/imap
new file mode 120000
index 000000000..006bdcfdf
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/imap
@@ -0,0 +1 @@
+../imap \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/keychain b/vendor/github.com/mattermost/rsc/tmp/keychain
new file mode 120000
index 000000000..4f18ef52d
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/keychain
@@ -0,0 +1 @@
+../keychain \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/mbta b/vendor/github.com/mattermost/rsc/tmp/mbta
new file mode 120000
index 000000000..858cac3c2
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/mbta
@@ -0,0 +1 @@
+../mbta \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/mkapp b/vendor/github.com/mattermost/rsc/tmp/mkapp
new file mode 120000
index 000000000..a60312f7d
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/mkapp
@@ -0,0 +1 @@
+../mkapp \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/plist b/vendor/github.com/mattermost/rsc/tmp/plist
new file mode 120000
index 000000000..9dc5f2029
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/plist
@@ -0,0 +1 @@
+../plist \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/qr b/vendor/github.com/mattermost/rsc/tmp/qr
new file mode 120000
index 000000000..78b229bd3
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/qr
@@ -0,0 +1 @@
+../qr \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/regexp b/vendor/github.com/mattermost/rsc/tmp/regexp
new file mode 120000
index 000000000..be8f6fd71
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/regexp
@@ -0,0 +1 @@
+../regexp \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/rosetta b/vendor/github.com/mattermost/rsc/tmp/rosetta
new file mode 120000
index 000000000..deb14d791
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/rosetta
@@ -0,0 +1 @@
+../rosetta \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/s3get b/vendor/github.com/mattermost/rsc/tmp/s3get
new file mode 120000
index 000000000..1bb5ed3a3
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/s3get
@@ -0,0 +1 @@
+../s3get \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/smugmug b/vendor/github.com/mattermost/rsc/tmp/smugmug
new file mode 120000
index 000000000..97f9b8229
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/smugmug
@@ -0,0 +1 @@
+../smugmug \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/tmp/xmpp b/vendor/github.com/mattermost/rsc/tmp/xmpp
new file mode 120000
index 000000000..f8abedc7c
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/tmp/xmpp
@@ -0,0 +1 @@
+../xmpp \ No newline at end of file
diff --git a/vendor/github.com/mattermost/rsc/xmpp/xmpp.go b/vendor/github.com/mattermost/rsc/xmpp/xmpp.go
new file mode 100644
index 000000000..6fa7e43cb
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/xmpp/xmpp.go
@@ -0,0 +1,572 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TODO(rsc):
+// More precise error handling.
+// Presence functionality.
+// TODO(mattn):
+// Add proxy authentication.
+
+// Package xmpp implements a simple Google Talk client
+// using the XMPP protocol described in RFC 3920 and RFC 3921.
+package xmpp
+
+import (
+ "bufio"
+ "bytes"
+ "crypto/tls"
+ "encoding/base64"
+ "encoding/xml"
+ "errors"
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "net/http"
+ "net/url"
+ "os"
+ "strconv"
+ "strings"
+)
+
+const (
+ nsStream = "http://etherx.jabber.org/streams"
+ nsTLS = "urn:ietf:params:xml:ns:xmpp-tls"
+ nsSASL = "urn:ietf:params:xml:ns:xmpp-sasl"
+ nsBind = "urn:ietf:params:xml:ns:xmpp-bind"
+ nsClient = "jabber:client"
+)
+
+var DefaultConfig tls.Config
+
+type Client struct {
+ tls *tls.Conn // connection to server
+ jid string // Jabber ID for our connection
+ p *xml.Decoder
+}
+
+// NewClient creates a new connection to a host given as "hostname" or "hostname:port".
+// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID.
+// Default the port to 5222.
+func NewClient(host, user, passwd string) (*Client, error) {
+ addr := host
+
+ if strings.TrimSpace(host) == "" {
+ a := strings.SplitN(user, "@", 2)
+ if len(a) == 2 {
+ host = a[1]
+ }
+ }
+ a := strings.SplitN(host, ":", 2)
+ if len(a) == 1 {
+ host += ":5222"
+ }
+ proxy := os.Getenv("HTTP_PROXY")
+ if proxy == "" {
+ proxy = os.Getenv("http_proxy")
+ }
+ if proxy != "" {
+ url, err := url.Parse(proxy)
+ if err == nil {
+ addr = url.Host
+ }
+ }
+ c, err := net.Dial("tcp", addr)
+ if err != nil {
+ return nil, err
+ }
+
+ if proxy != "" {
+ fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\n", host)
+ fmt.Fprintf(c, "Host: %s\r\n", host)
+ fmt.Fprintf(c, "\r\n")
+ br := bufio.NewReader(c)
+ req, _ := http.NewRequest("CONNECT", host, nil)
+ resp, err := http.ReadResponse(br, req)
+ if err != nil {
+ return nil, err
+ }
+ if resp.StatusCode != 200 {
+ f := strings.SplitN(resp.Status, " ", 2)
+ return nil, errors.New(f[1])
+ }
+ }
+
+ tlsconn := tls.Client(c, &DefaultConfig)
+ if err = tlsconn.Handshake(); err != nil {
+ return nil, err
+ }
+
+ if strings.LastIndex(host, ":") > 0 {
+ host = host[:strings.LastIndex(host, ":")]
+ }
+ if err = tlsconn.VerifyHostname(host); err != nil {
+ return nil, err
+ }
+
+ client := new(Client)
+ client.tls = tlsconn
+ if err := client.init(user, passwd); err != nil {
+ client.Close()
+ return nil, err
+ }
+ return client, nil
+}
+
+func (c *Client) Close() error {
+ return c.tls.Close()
+}
+
+func (c *Client) init(user, passwd string) error {
+ // For debugging: the following causes the plaintext of the connection to be duplicated to stdout.
+ // c.p = xml.NewParser(tee{c.tls, os.Stdout});
+ c.p = xml.NewDecoder(c.tls)
+
+ a := strings.SplitN(user, "@", 2)
+ if len(a) != 2 {
+ return errors.New("xmpp: invalid username (want user@domain): " + user)
+ }
+ user = a[0]
+ domain := a[1]
+
+ // Declare intent to be a jabber client.
+ fmt.Fprintf(c.tls, "<?xml version='1.0'?>\n"+
+ "<stream:stream to='%s' xmlns='%s'\n"+
+ " xmlns:stream='%s' version='1.0'>\n",
+ xmlEscape(domain), nsClient, nsStream)
+
+ // Server should respond with a stream opening.
+ se, err := nextStart(c.p)
+ if err != nil {
+ return err
+ }
+ if se.Name.Space != nsStream || se.Name.Local != "stream" {
+ return errors.New("xmpp: expected <stream> but got <" + se.Name.Local + "> in " + se.Name.Space)
+ }
+
+ // Now we're in the stream and can use Unmarshal.
+ // Next message should be <features> to tell us authentication options.
+ // See section 4.6 in RFC 3920.
+ var f streamFeatures
+ if err = c.p.Decode(&f); err != nil {
+ return errors.New("unmarshal <features>: " + err.Error())
+ }
+ havePlain := false
+ for _, m := range f.Mechanisms.Mechanism {
+ if m == "PLAIN" {
+ havePlain = true
+ break
+ }
+ }
+ if !havePlain {
+ return errors.New(fmt.Sprintf("PLAIN authentication is not an option: %v", f.Mechanisms.Mechanism))
+ }
+
+ // Plain authentication: send base64-encoded \x00 user \x00 password.
+ raw := "\x00" + user + "\x00" + passwd
+ enc := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
+ base64.StdEncoding.Encode(enc, []byte(raw))
+ fmt.Fprintf(c.tls, "<auth xmlns='%s' mechanism='PLAIN'>%s</auth>\n",
+ nsSASL, enc)
+
+ // Next message should be either success or failure.
+ name, val, err := next(c.p)
+ switch v := val.(type) {
+ case *saslSuccess:
+ case *saslFailure:
+ // v.Any is type of sub-element in failure,
+ // which gives a description of what failed.
+ return errors.New("auth failure: " + v.Any.Local)
+ default:
+ return errors.New("expected <success> or <failure>, got <" + name.Local + "> in " + name.Space)
+ }
+
+ // Now that we're authenticated, we're supposed to start the stream over again.
+ // Declare intent to be a jabber client.
+ fmt.Fprintf(c.tls, "<stream:stream to='%s' xmlns='%s'\n"+
+ " xmlns:stream='%s' version='1.0'>\n",
+ xmlEscape(domain), nsClient, nsStream)
+
+ // Here comes another <stream> and <features>.
+ se, err = nextStart(c.p)
+ if err != nil {
+ return err
+ }
+ if se.Name.Space != nsStream || se.Name.Local != "stream" {
+ return errors.New("expected <stream>, got <" + se.Name.Local + "> in " + se.Name.Space)
+ }
+ if err = c.p.Decode(&f); err != nil {
+ // TODO: often stream stop.
+ //return os.NewError("unmarshal <features>: " + err.String())
+ }
+
+ // Send IQ message asking to bind to the local user name.
+ fmt.Fprintf(c.tls, "<iq type='set' id='x'><bind xmlns='%s'/></iq>\n", nsBind)
+ var iq clientIQ
+ if err = c.p.Decode(&iq); err != nil {
+ return errors.New("unmarshal <iq>: " + err.Error())
+ }
+ if &iq.Bind == nil {
+ return errors.New("<iq> result missing <bind>")
+ }
+ c.jid = iq.Bind.Jid // our local id
+
+ // We're connected and can now receive and send messages.
+ c.Status(Away, "")
+ return nil
+}
+
+type Chat struct {
+ Remote string
+ Type string
+ Text string
+ Roster Roster
+ Presence *Presence
+}
+
+type Roster []Contact
+
+type Contact struct {
+ Remote string
+ Name string
+ Group []string
+}
+
+type Presence struct {
+ Remote string
+ Status Status
+ StatusMsg string
+ Priority int
+}
+
+func atoi(s string) int {
+ if s == "" {
+ return 0
+ }
+ n, err := strconv.Atoi(s)
+ if err != nil {
+ n = -1
+ }
+ return n
+}
+
+func statusCode(s string) Status {
+ for i, ss := range statusName {
+ if s == ss {
+ return Status(i)
+ }
+ }
+ return Available
+}
+
+// Recv wait next token of chat.
+func (c *Client) Recv() (chat Chat, err error) {
+ for {
+ _, val, err := next(c.p)
+ if err != nil {
+ return Chat{}, err
+ }
+ switch val := val.(type) {
+ case *clientMessage:
+ return Chat{Remote: val.From, Type: val.Type, Text: val.Body}, nil
+ case *clientQuery:
+ var r Roster
+ for _, item := range val.Item {
+ r = append(r, Contact{item.Jid, item.Name, item.Group})
+ }
+ return Chat{Type: "roster", Roster: r}, nil
+ case *clientPresence:
+ pr := &Presence{Remote: val.From, Status: statusCode(val.Show), StatusMsg: val.Status, Priority: atoi(val.Priority)}
+ if val.Type == "unavailable" {
+ pr.Status = Unavailable
+ }
+ return Chat{Remote: val.From, Type: "presence", Presence: pr}, nil
+ default:
+ //log.Printf("ignoring %T", val)
+ }
+ }
+ panic("unreachable")
+}
+
+// Send sends message text.
+func (c *Client) Send(chat Chat) error {
+ fmt.Fprintf(c.tls, "<message to='%s' from='%s' type='chat' xml:lang='en'>"+
+ "<body>%s</body></message>",
+ xmlEscape(chat.Remote), xmlEscape(c.jid),
+ xmlEscape(chat.Text))
+ return nil
+}
+
+// Roster asks for the chat roster.
+func (c *Client) Roster() error {
+ fmt.Fprintf(c.tls, "<iq from='%s' type='get' id='roster1'><query xmlns='jabber:iq:roster'/></iq>\n", xmlEscape(c.jid))
+ return nil
+}
+
+type Status int
+
+const (
+ Unavailable Status = iota
+ DoNotDisturb
+ ExtendedAway
+ Away
+ Available
+)
+
+var statusName = []string{
+ Unavailable: "unavailable",
+ DoNotDisturb: "dnd",
+ ExtendedAway: "xa",
+ Away: "away",
+ Available: "chat",
+}
+
+func (s Status) String() string {
+ return statusName[s]
+}
+
+func (c *Client) Status(status Status, msg string) error {
+ fmt.Fprintf(c.tls, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", status, xmlEscape(msg))
+ return nil
+}
+
+// RFC 3920 C.1 Streams name space
+
+type streamFeatures struct {
+ XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
+ StartTLS tlsStartTLS
+ Mechanisms saslMechanisms
+ Bind bindBind
+ Session bool
+}
+
+type streamError struct {
+ XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"`
+ Any xml.Name
+ Text string
+}
+
+// RFC 3920 C.3 TLS name space
+
+type tlsStartTLS struct {
+ XMLName xml.Name `xml:":ietf:params:xml:ns:xmpp-tls starttls"`
+ Required bool
+}
+
+type tlsProceed struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls proceed"`
+}
+
+type tlsFailure struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls failure"`
+}
+
+// RFC 3920 C.4 SASL name space
+
+type saslMechanisms struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"`
+ Mechanism []string
+}
+
+type saslAuth struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
+ Mechanism string `xml:"attr"`
+}
+
+type saslChallenge string
+
+type saslResponse string
+
+type saslAbort struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl abort"`
+}
+
+type saslSuccess struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"`
+}
+
+type saslFailure struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"`
+ Any xml.Name
+}
+
+// RFC 3920 C.5 Resource binding name space
+
+type bindBind struct {
+ XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
+ Resource string
+ Jid string
+}
+
+// RFC 3921 B.1 jabber:client
+
+type clientMessage struct {
+ XMLName xml.Name `xml:"jabber:client message"`
+ From string `xml:"attr"`
+ Id string `xml:"attr"`
+ To string `xml:"attr"`
+ Type string `xml:"attr"` // chat, error, groupchat, headline, or normal
+
+ // These should technically be []clientText,
+ // but string is much more convenient.
+ Subject string
+ Body string
+ Thread string
+}
+
+type clientText struct {
+ Lang string `xml:"attr"`
+ Body string `xml:"chardata"`
+}
+
+type clientPresence struct {
+ XMLName xml.Name `xml:"jabber:client presence"`
+ From string `xml:"attr"`
+ Id string `xml:"attr"`
+ To string `xml:"attr"`
+ Type string `xml:"attr"` // error, probe, subscribe, subscribed, unavailable, unsubscribe, unsubscribed
+ Lang string `xml:"attr"`
+
+ Show string // away, chat, dnd, xa
+ Status string // sb []clientText
+ Priority string
+ Error *clientError
+}
+
+type clientIQ struct { // info/query
+ XMLName xml.Name `xml:"jabber:client iq"`
+ From string `xml:"attr"`
+ Id string `xml:"attr"`
+ To string `xml:"attr"`
+ Type string `xml:"attr"` // error, get, result, set
+ Error clientError
+ Bind bindBind
+ Query clientQuery
+}
+
+type clientError struct {
+ XMLName xml.Name `xml:"jabber:client error"`
+ Code string `xml:"attr"`
+ Type string `xml:"attr"`
+ Any xml.Name
+ Text string
+}
+
+type clientQuery struct {
+ Item []rosterItem
+}
+
+type rosterItem struct {
+ XMLName xml.Name `xml:"jabber:iq:roster item"`
+ Jid string `xml:"attr"`
+ Name string `xml:"attr"`
+ Subscription string `xml:"attr"`
+ Group []string
+}
+
+// Scan XML token stream to find next StartElement.
+func nextStart(p *xml.Decoder) (xml.StartElement, error) {
+ for {
+ t, err := p.Token()
+ if err != nil {
+ log.Fatal("token", err)
+ }
+ switch t := t.(type) {
+ case xml.StartElement:
+ return t, nil
+ }
+ }
+ panic("unreachable")
+}
+
+// Scan XML token stream for next element and save into val.
+// If val == nil, allocate new element based on proto map.
+// Either way, return val.
+func next(p *xml.Decoder) (xml.Name, interface{}, error) {
+ // Read start element to find out what type we want.
+ se, err := nextStart(p)
+ if err != nil {
+ return xml.Name{}, nil, err
+ }
+
+ // Put it in an interface and allocate one.
+ var nv interface{}
+ switch se.Name.Space + " " + se.Name.Local {
+ case nsStream + " features":
+ nv = &streamFeatures{}
+ case nsStream + " error":
+ nv = &streamError{}
+ case nsTLS + " starttls":
+ nv = &tlsStartTLS{}
+ case nsTLS + " proceed":
+ nv = &tlsProceed{}
+ case nsTLS + " failure":
+ nv = &tlsFailure{}
+ case nsSASL + " mechanisms":
+ nv = &saslMechanisms{}
+ case nsSASL + " challenge":
+ nv = ""
+ case nsSASL + " response":
+ nv = ""
+ case nsSASL + " abort":
+ nv = &saslAbort{}
+ case nsSASL + " success":
+ nv = &saslSuccess{}
+ case nsSASL + " failure":
+ nv = &saslFailure{}
+ case nsBind + " bind":
+ nv = &bindBind{}
+ case nsClient + " message":
+ nv = &clientMessage{}
+ case nsClient + " presence":
+ nv = &clientPresence{}
+ case nsClient + " iq":
+ nv = &clientIQ{}
+ case nsClient + " error":
+ nv = &clientError{}
+ default:
+ return xml.Name{}, nil, errors.New("unexpected XMPP message " +
+ se.Name.Space + " <" + se.Name.Local + "/>")
+ }
+
+ // Unmarshal into that storage.
+ if err = p.DecodeElement(nv, &se); err != nil {
+ return xml.Name{}, nil, err
+ }
+ return se.Name, nv, err
+}
+
+var xmlSpecial = map[byte]string{
+ '<': "&lt;",
+ '>': "&gt;",
+ '"': "&quot;",
+ '\'': "&apos;",
+ '&': "&amp;",
+}
+
+func xmlEscape(s string) string {
+ var b bytes.Buffer
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if s, ok := xmlSpecial[c]; ok {
+ b.WriteString(s)
+ } else {
+ b.WriteByte(c)
+ }
+ }
+ return b.String()
+}
+
+type tee struct {
+ r io.Reader
+ w io.Writer
+}
+
+func (t tee) Read(p []byte) (n int, err error) {
+ n, err = t.r.Read(p)
+ if n > 0 {
+ t.w.Write(p[0:n])
+ }
+ return
+}