Exemple #1
0
// newIMAPClient will initiate a new IMAP connection with the given creds.
func newIMAPClient(info MailboxInfo) (*imap.Client, error) {
	var client *imap.Client
	var err error
	if info.TLS {
		client, err = imap.DialTLS(info.Host, new(tls.Config))
		if err != nil {
			return client, err
		}
	} else {
		client, err = imap.Dial(info.Host)
		if err != nil {
			return client, err
		}
	}

	_, err = client.Login(info.User, info.Pwd)
	if err != nil {
		return client, err
	}

	_, err = imap.Wait(client.Select(info.Folder, info.ReadOnly))
	if err != nil {
		return client, err
	}

	return client, nil
}
func connect(user, authToken string) (*imap.Client, error) {
	c, err := imap.DialTLS("imap.gmail.com:993", nil)
	if err != nil {
		return nil, err
	}
	if _, err = c.Auth(oauthSASL{user, authToken}); err != nil {
		c.Logout(2 * time.Second)
		return nil, err
	}
	return c, nil
}
Exemple #3
0
func dialImap(addr string) (c *imap.Client, err error) {
	if strings.HasSuffix(addr, ":993") {
		c, err = imap.DialTLS(addr, tlsConfig)
	} else {
		c, err = imap.Dial(addr)
	}
	if err != nil {
		return nil, e.New(err)
	}
	return c, nil
}
Exemple #4
0
func Dial(addr string) (c *imap.Client) {
	var err error
	if strings.HasSuffix(addr, ":993") {
		c, err = imap.DialTLS(addr, nil)
	} else {
		c, err = imap.Dial(addr)
	}
	if err != nil {
		panic(err)
	}
	return c
}
Exemple #5
0
// Dial connects to the given address.
func (c *IMAPConnection) Dial(address string) error {
	logger.Debugf("connecting to %s", address)
	tlsConfig := &tls.Config{}
	var err error
	c.conn, err = imap.DialTLS(address, tlsConfig)
	if err != nil {
		logger.Errorf("failed to connect: %s", err)
		return err
	}

	return nil
}
Exemple #6
0
func Dial(addr string) (c *imap.Client) {
	var err error
	if strings.HasSuffix(addr, ":993") {
		log.Println("Dialing with tls: ", addr)
		c, err = imap.DialTLS(addr, nil)
	} else {
		log.Println("Dialing without tls: ", addr)
		c, err = imap.Dial(addr)
	}
	if err != nil {
		panic(err)
	}
	return c
}
// CheckImapConnection if is connected return nil or try to connect
func (m *MatterMail) CheckImapConnection() error {
	if m.imapClient != nil && (m.imapClient.State() == imap.Auth || m.imapClient.State() == imap.Selected) {
		m.debg.Println("CheckImapConnection: Connection alive")
		return nil
	}

	var err error

	//Start connection with server
	if strings.HasSuffix(m.cfg.ImapServer, ":993") {
		m.debg.Println("CheckImapConnection: DialTLS")
		m.imapClient, err = imap.DialTLS(m.cfg.ImapServer, nil)
	} else {
		m.debg.Println("CheckImapConnection: Dial")
		m.imapClient, err = imap.Dial(m.cfg.ImapServer)
	}

	if err != nil {
		m.eror.Println("Unable to connect:", err)
		return err
	}

	if m.cfg.StartTLS && m.imapClient.Caps["STARTTLS"] {
		m.debg.Println("CheckImapConnection:StartTLS")
		var tconfig tls.Config
		if m.cfg.TLSAcceptAllCerts {
			tconfig.InsecureSkipVerify = true
		}
		_, err = m.imapClient.StartTLS(&tconfig)
		if err != nil {
			return err
		}
	}

	//Check if server support IDLE mode
	/*
		if !m.imapClient.Caps["IDLE"] {
			return fmt.Errorf("The server %q does not support IDLE\n", m.cfg.ImapServer)
		}
	*/
	m.info.Printf("Connected with %q\n", m.cfg.ImapServer)

	_, err = m.imapClient.Login(m.cfg.Email, m.cfg.EmailPass)
	if err != nil {
		m.eror.Println("Unable to login:", m.cfg.Email)
		return err
	}

	return nil
}
Exemple #8
0
func imapConnect(server string) *imap.Client {
	user, password := getCredentials(server)
	c, err := imap.DialTLS(server, nil)
	if err != nil {
		log.Fatal(err)
	}
	if c.State() == imap.Login {
		if _, err := c.Login(user, password); err != nil {
			log.Fatal(err)
		}
	} else {
		log.Fatal("Connection not in Login state. Cannot login.")
	}
	return c
}
Exemple #9
0
// dial open imap socket
func dial(host string) (*imap.Client, error) {
	var client *imap.Client
	var err error

	if strings.HasSuffix(host, ":993") {
		client, err = imap.DialTLS(host, nil)
	} else {
		client, err = imap.Dial(host)
	}

	if err != nil {
		return nil, err
	}

	return client, nil
}
Exemple #10
0
// newIMAPClient will initiate a new IMAP connection with the given creds.
func newIMAPClient(host, username, password, mailbox string) (*imap.Client, error) {
	client, err := imap.DialTLS(host, new(tls.Config))
	if err != nil {
		return client, err
	}

	_, err = client.Login(username, password)
	if err != nil {
		return client, err
	}

	_, err = imap.Wait(client.Select(mailbox, false))
	if err != nil {
		return client, err
	}

	return client, nil
}
Exemple #11
0
func (mc *MailCon) dial() (c *imap.Client, err error) {
	// Decide what method to use for dialing into the server
	var serverAddr string = fmt.Sprintf("%s:%d", mc.conf.Hostname, mc.conf.Port)
	if 993 == mc.conf.Port {
		c, err = imap.DialTLS(serverAddr, nil)
	} else {
		c, err = imap.Dial(serverAddr)
	}
	// If dialing went wrong, return the error
	if err != nil {
		return nil, err
	}
	// Check for STARTTLS and use appropriate method and config object if need be
	if c.Caps["STARTTLS"] {
		_, err = mc.waitFor(c.StartTLS(&tls.Config{
			InsecureSkipVerify: mc.conf.SkipCertificateVerification,
		}))
	}
	// Identify this client at the IMAP server
	if c.Caps["ID"] {
		_, err = mc.waitFor(c.ID("name", "watney"))
	}
	return c, err
}
Exemple #12
0
// connect sets up a new IMAPS connection to the given host using the given
// credentials. The name parameter should be a human-readable name for this
// mailbox.
func Connect(notify chan bool,
	name string,
	address string,
	username string,
	password string,
	folder string,
	poll time.Duration) {

	// Connect to the server
	fmt.Printf("%s: connecting to server...\n", name)
	c, err := imap.DialTLS(address, nil)

	if err != nil {
		fmt.Printf("%s: connection failed, retrying in 3 minutes\n", name)
		fmt.Println(" ", err)
		Errs[name] = 1
		notify <- false
		time.Sleep(3 * time.Minute)
		go Connect(notify, name, address, username, password, folder, poll)
		return
	}

	// Connected successfully!
	Errs[name] = 0

	// Remember to log out and close the connection when finished
	defer c.Logout(30 * time.Second)

	// Authenticate
	if c.State() == imap.Login {
		fmt.Printf("login %s '%s' '%s'\n", address, username, password)
		_, err = c.Login(username, password)
	} else {
		fmt.Printf("%s: no login presented, exiting...\n", name)
		Errs[name] = 3
		notify <- false
		return
	}

	if err != nil {
		fmt.Printf("%s: login failed (%s), exiting...\n", name, err)
		Errs[name] = 4
		notify <- false
		return
	}

	// If IDLE isn't supported, we're not going to fall back on polling
	// Time to abandon ship!
	if !c.Caps["IDLE"] {
		fmt.Printf("%s: server does not support IMAP IDLE, exiting...\n", name)
		Errs[name] = 2
		notify <- false
		return
	}

	_, err = c.Select(folder, true)

	if err != nil {
		fmt.Printf("%s: could not open folder %s (%s), exiting...\n", name, folder, err)
	}

	// Get initial unread messages count
	fmt.Printf("%s initial state: ", name)
	UpdateTray(c, notify, name)

	// And go to IMAP IDLE mode
	cmd, err := c.Idle()

	if err != nil {
		// Fast reconnect
		fmt.Printf("%s: connection failed, reconnecting...\n", name)
		fmt.Println("  ", err)
		Errs[name] = 5
		go Connect(notify, name, address, username, password, folder, poll)
		return
	}

	// Process responses while idling
	for cmd.InProgress() {
		// Wait for server messages
		// Refresh every `poll` to avoid disconnection
		// Defaults to 29 minutes (see spec)
		err = c.Recv(poll)

		if err == io.EOF {
			fmt.Printf("%s: connection closed, reconnecting...\n", name)
			fmt.Println("  ", err)
			Errs[name] = 5
			go Connect(notify, name, address, username, password, folder, poll)
			return
		}

		if err != nil && err != imap.ErrTimeout {
			fmt.Printf("%s: error during receive: %s\n", name, err)
		}

		// We don't really care about the data, just that there *is* data
		cmd.Data = nil
		c.Data = nil

		// Update our view of the inbox
		c.IdleTerm()
		fmt.Printf("%s state: ", name)
		UpdateTray(c, notify, name)
		cmd, err = c.Idle()

		if err != nil {
			fmt.Printf("%s: connection failed (%s), reconnecting...\n", name, err)
			fmt.Println("  ", err)
			Errs[name] = 5
			go Connect(notify, name, address, username, password, folder, poll)
			return
		}
	}
}