85 lines
1.9 KiB
Go
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()
|
||
|
}
|