| 1 | # go-jmap |
| 2 | |
| 3 | A JMAP client library. Includes support for all core functionality (including |
| 4 | PushSubscription and EventSource event streams), mail, smime-verify, and MDN |
| 5 | specifications |
| 6 | |
| 7 | Note: this library started as a fork of [github.com/foxcpp/go-jmap](https://github.com/foxcpp/go-jmap) |
| 8 | It has since undergone massive refactoring and probably doesn't look very |
| 9 | similar anymore, but many thanks to foxcpp for the initial work. |
| 10 | |
| 11 | ## Usage |
| 12 | |
| 13 | ```go |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | |
| 19 | "git.sr.ht/~rockorager/go-jmap" |
| 20 | "git.sr.ht/~rockorager/go-jmap/mail" |
| 21 | "git.sr.ht/~rockorager/go-jmap/mail/email" |
| 22 | "git.sr.ht/~rockorager/go-jmap/mail/mailbox" |
| 23 | ) |
| 24 | |
| 25 | func main() { |
| 26 | // Create a new client. The SessionEndpoint must be specified for |
| 27 | // initial connections. |
| 28 | client := &jmap.Client{ |
| 29 | SessionEndpoint: "https://api.fastmail.com/jmap/session", |
| 30 | } |
| 31 | // Set the authentication mechanism. This also sets the HttpClient of |
| 32 | // the jmap client |
| 33 | client.WithAccessToken("my-access-token") |
| 34 | |
| 35 | // Authenticate the client. This gets a Session object. Session objects |
| 36 | // are cacheable, and have their own state string clients can use to |
| 37 | // decide when to refresh. The client can be initialized with a cached |
| 38 | // Session object. If one isn't available, the first request will also |
| 39 | // authenticate the client |
| 40 | if err := client.Authenticate(); err != nil { |
| 41 | // Handle the error |
| 42 | } |
| 43 | |
| 44 | // Get the account ID of the primary mail account |
| 45 | id := client.Session.PrimaryAccounts[mail.URI] |
| 46 | |
| 47 | // Create a new request |
| 48 | req := &jmap.Request{} |
| 49 | |
| 50 | // Invoke a method. The CallID of this method will be returned to be |
| 51 | // used when chaining calls |
| 52 | req.Invoke(&mailbox.Get{ |
| 53 | Account: id, |
| 54 | }) |
| 55 | |
| 56 | // Invoke a changes call, let's save the callID and pass it to a Get |
| 57 | // method |
| 58 | callID := req.Invoke(&email.Changes{ |
| 59 | Account: id, |
| 60 | SinceState: "some-known-state", |
| 61 | }) |
| 62 | |
| 63 | // Invoke a result reference call |
| 64 | req.Invoke(&email.Get{ |
| 65 | Account: id, |
| 66 | ReferenceIDs: &jmap.ResultReference{ |
| 67 | ResultOf: callID, // The CallID of the referenced method |
| 68 | Name: "Email/changes", // The name of the referenced method |
| 69 | Path: "/created", // JSON pointer to the location of the reference |
| 70 | }, |
| 71 | }) |
| 72 | |
| 73 | // Make the request |
| 74 | resp, err := client.Do(req) |
| 75 | if err != nil { |
| 76 | // Handle the error |
| 77 | } |
| 78 | |
| 79 | // Loop through the responses to invidividual invocations |
| 80 | for _, inv := range resp.Responses { |
| 81 | // Our result to individual calls is in the Args field of the |
| 82 | // invocation |
| 83 | switch r := inv.Args.(type) { |
| 84 | case *mailbox.GetResponse: |
| 85 | // A GetResponse contains a List of the objects |
| 86 | // retrieved |
| 87 | for _, mbox := range r.List { |
| 88 | fmt.Printf("Mailbox name: %s", mbox.Name) |
| 89 | fmt.Printf("Total email: %d", mbox.TotalEmails) |
| 90 | fmt.Printf("Unread email: %d", mbox.UnreadEmails) |
| 91 | } |
| 92 | case *email.GetResponse: |
| 93 | for _, eml := range r.List { |
| 94 | fmt.Printf("Email subject: %s", eml.Subject) |
| 95 | } |
| 96 | } |
| 97 | // There is a response in here to the Email/changes call, but we |
| 98 | // don't care about the results since we passed them to the |
| 99 | // Email/get call |
| 100 | } |
| 101 | } |
| 102 | ``` |
| 103 | |
| 104 | ## Status |
| 105 | |
| 106 | ### Core ([RFC 8620](https://tools.ietf.org/html/rfc8620)) |
| 107 | |
| 108 | Complete |
| 109 | |
| 110 | ### Mail ([RFC 8621](https://tools.ietf.org/html/rfc8621)) |
| 111 | |
| 112 | Complete |
| 113 | |
| 114 | ### MDN ([RFC 9007](https://tools.ietf.org/html/rfc9007)) |
| 115 | |
| 116 | Complete |
| 117 | |
| 118 | ### WebSocket ([RFC 8887](https://tools.ietf.org/html/rfc8887)) |
| 119 | |
| 120 | Not started |
| 121 | |
| 122 | ### S/MIME ([RFC 9219](https://tools.ietf.org/html/rfc9219)) |
| 123 | |
| 124 | Complete |