Пример #1
0
// 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)
}
Пример #2
0
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()
}