// accepts a post to create a new server func (c Servers) Add(hostname, username, password string, useTLS bool) revel.Result { // make sure we have the big-3 data we need to connect to a server // tls we will assume is False if it is unspecified c.Validation.Required(hostname).Message("You must specify a server to add.") c.Validation.Required(username).Message("You must supply an email address to add a server.") c.Validation.Required(password) // redirect on error if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(Servers.Index) } // create the server server := models.NewServer(hostname, username, password) server.UseTLS = useTLS // test connection _, err := server.Connect() if err != nil { c.Flash.Error("Connection to %s failed: %v", hostname, err) return c.Redirect(Servers.Index) } // cool, save the server in the session session := models.GetSession(c.Session.Id()) session[hostname] = server // also make it the new current server session[CurrentServerKey] = server c.Flash.Success("Added server %s!", hostname) return c.Redirect(Servers.Index) }
func (c Servers) Remove(hostname string) revel.Result { c.Validation.Required(hostname).Message("You must specify a host to remove.") if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(Servers.Index) } session := models.GetSession(c.Session.Id()) if server, ok := session[hostname]; ok { // make sure to delete second reference if this is the current // server if session[CurrentServerKey] == server { delete(session, CurrentServerKey) } // always close connections server.Close() delete(session, hostname) c.Flash.Success("Removed server %s and disconnected.", hostname) return c.Redirect(Servers.Index) } c.Flash.Error("Server %s not found for this session.", hostname) return c.Redirect(Servers.Index) }
// get the current server or redirect the user to the server config page func (c Mailboxes) getCurrentServer() (*models.Server, revel.Result) { session := models.GetSession(c.Session.Id()) current_server, ok := session[CurrentServerKey] // no current server, which means the user has no servers // send them to the server list to pick and/or add one if !ok { c.Flash.Error("You must configure a server") return nil, c.Redirect(Servers.Index) } return current_server, nil }
func (c Servers) Select(hostname string) revel.Result { // needs hostname desperatley c.Validation.Required(hostname).Message("You must select a host.") if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(Servers.Index) } session := models.GetSession(c.Session.Id()) if server, ok := session[hostname]; ok { session[CurrentServerKey] = server c.Flash.Success("Selected server %s.", hostname) return c.Redirect(Servers.Index) } // base case -- we don't have this server c.Flash.Error("Server %s cannot be selected because no connection to it exists.", hostname) return c.Redirect(Servers.Index) }
// list the currently-added servers // // allow the user to activate one of the servers and store it in // the session under CurrentServerKey // // allow the user to add a new server by posting a message to // /servers/add with hostname, username, password, and UseTLS(bool) func (c Servers) Index() revel.Result { session := models.GetSession(c.Session.Id()) server_count := len(session) cur, ok := session[CurrentServerKey] if ok { // duplicate server_count -= 1 } // enumerate servers into a nice list servers := make([]*models.Server, server_count) for hn, server := range session { if hn == CurrentServerKey { continue } servers = append(servers, server) } return c.Render(servers, cur) }
func main() { // create our models sess := models.GetSession("jake") password, err := gopass.GetPass("IMAP password> ") fatal("get password", err) server := models.NewServer( Hostname, "*****@*****.**", password) sess[Hostname] = server // connect - to test _, err = server.Connect() fatal("server.Connect", err) // get mailboxes _, err = server.GetMailboxes() fatal("get mailboxes", err) // messages in spam box spam, ok := server.Mailboxes["INBOX"] if !ok { fmt.Println("Couldn't get mailbox 'spam'") os.Exit(1) } msgs, err := spam.Update() fatal("update spam", err) lastMsg := msgs[len(msgs)-1] // download and parse body _, err = lastMsg.Body(false) fatal("download last message", err) // fmt.Printf("body:::::::\n%v\n", string(body)) msg_tree, err := lastMsg.ParseBody() fatal("parse body", err) ///var recurseNode func(node *models.MessageNode) ///recurseNode = func (node *models.MessageNode) { ////fmt.Printf("node %v\n", node) ////if node.Children != nil { ////for _, child := range node.Children { ////recurseNode(child) ////} ////} ///} ///recurseNode(msg_tree) fmt.Printf("Message tree --\n%v\n", msg_tree) indented, err := json.MarshalIndent(msg_tree, "derp", " ") fmt.Println(string(indented)) // investigate all the different FETCH types // see http://tools.ietf.org/html/rfc3501#section-6.4.5 // fetch_types := []string{ // "BODYSTRUCTURE", // // "FLAGS", "INTERNALDATE", "RFC822.SIZE", "ENVELOPE", "BODY", // // "BODYSTRUCTURE", "RFC822", "RFC822.HEADER", // // "ENVELOPE", "INTERNALDATE", // } // req_type := strings.Join(fetch_types, " ") // cmd, err := lastMsg.RetrieveRaw(req_type) // fatal("request lots of things async", err) // cmd, err = imap.Wait(cmd, err) // fatal("request lots of things sync", err) // info := cmd.Data[0].MessageInfo() // for fetch_type, response := range info.Attrs { // fmt.Printf("\n\n___FETCHTYPE[ %s ]___\n%s", fetch_type, imap.AsString(response)) // } // retrieve and print body of final msg //_, err = lastMsg.RetrieveBody(false) //fatal("get body", err) //parts, err := lastMsg.GetParts() //fatal("get parts", err) //for i, p := range parts { // fmt.Printf("__PART___ Number: %d\nContent-Type: %s\n\n%s", i, p.MimeType, string(p.Data)) //} // close connection server.Close() }