imap/client/connection.go
H0llyW00dzZ 464fdc1bbd
First Commit
- [+] feat: add initial implementation of IMAP client package with support for single and multiple user management
- [+] chore: add .gitignore file to exclude binaries, IDE files, and environment configurations
- [+] docs: add README file with project description, features, installation, usage examples, and license information
- [+] docs: add BSD 3-Clause License file
- [+] feat(client): implement configuration and connection handling for IMAP clients
- [+] feat(client): implement message listing functionality for single and multiple users
- [+] feat(client): add support for secure TLS connections in IMAP client
- [+] feat(client): create helper functions for initializing single and multi-user IMAP clients
- [+] chore: initialize Go module and add dependencies for IMAP and byte buffer pooling
2025-01-24 11:26:48 +07:00

85 lines
1.9 KiB
Go

// Copyright (c) 2025 H0llyW00dzZ All rights reserved.
//
// By accessing or using this software, you agree to be bound by the terms
// of the License Agreement, which you can find at LICENSE files.
package client
import (
"crypto/tls"
"fmt"
"github.com/emersion/go-imap/client"
)
// Connect establishes a connection to the IMAP server
func (c *IMAPClient) Connect() error {
tlsConfig := &tls.Config{
InsecureSkipVerify: c.config.InsecureSkipVerify,
}
cl, err := client.DialTLS(c.config.Server, tlsConfig)
if err != nil {
return fmt.Errorf("failed to connect to server: %v", err)
}
if err := cl.Login(c.config.Username, c.config.Password); err != nil {
return fmt.Errorf("failed to login: %v", err)
}
c.client = cl
return nil
}
// Disconnect logs out and closes the connection
func (c *IMAPClient) Disconnect() error {
if c.client != nil {
if err := c.client.Logout(); err != nil {
return fmt.Errorf("failed to logout: %v", err)
}
}
return nil
}
// AddUser adds a new user to the MultiUserIMAP
func (m *MultiUserIMAP) AddUser(username, password, server string, insecureSkipVerify bool) {
m.mu.Lock()
defer m.mu.Unlock()
config := &Config{
Username: username,
Password: password,
Server: server,
InsecureSkipVerify: insecureSkipVerify,
}
client := NewIMAP(config)
m.clients[username] = client
}
// ConnectUser connects a specific user
func (m *MultiUserIMAP) ConnectUser(username string) error {
m.mu.Lock()
defer m.mu.Unlock()
client, exists := m.clients[username]
if !exists {
return fmt.Errorf("user not found: %s", username)
}
return client.Connect()
}
// DisconnectUser disconnects a specific user
func (m *MultiUserIMAP) DisconnectUser(username string) error {
m.mu.Lock()
defer m.mu.Unlock()
client, exists := m.clients[username]
if !exists {
return fmt.Errorf("user not found: %s", username)
}
return client.Disconnect()
}