<commit_before>// An IMAP client.
package client

import (
	"crypto/tls"
	"io"
	"log"
	"net"
	"sync"

	imap "github.com/emersion/go-imap/common"
)

// An IMAP client.
type Client struct {
	conn *imap.Conn
	isTLS bool

	handlers []imap.RespHandler
	handlersLocker sync.Locker

	// The server capabilities.
	Caps map[string]bool
	// The current connection state.
	State imap.ConnState
	// The selected mailbox, if there is one.
	Mailbox *imap.MailboxStatus

	// A channel where info messages from the server will be sent.
	Infos chan *imap.StatusResp
	// A channel where warning messages from the server will be sent.
	Warnings chan *imap.StatusResp
	// A channel where error messages from the server will be sent.
	Errors chan *imap.StatusResp
	// A channel where bye messages from the server will be sent.
	Byes chan *imap.StatusResp
	// A channel where mailbox updates from the server will be sent.
	MailboxUpdates chan *imap.MailboxStatus
	// A channel where deleted message IDs will be sent.
	Expunges chan uint32

	// TODO: support unilateral message updates
	// A channel where messages updates from the server will be sent.
	//MessageUpdates chan *imap.Message
}

func (c *Client) read() error {
	r := c.conn.Reader

	defer (func () {
		c.handlersLocker.Lock()
		defer c.handlersLocker.Unlock()

		for _, hdlr := range c.handlers {
			close(hdlr)
		}
		c.handlers = nil
	})()

	for {
		if c.State == imap.LogoutState {
			return nil
		}

		c.conn.Wait()

		res, err := imap.ReadResp(r)
		if err == io.EOF {
			return nil
		}
		if err != nil {
			log.Println("Error reading response:", err)
			continue
		}

		c.handlersLocker.Lock()

		var accepted bool
		for _, hdlr := range c.handlers {
			if hdlr == nil {
				continue
			}

			h := &imap.RespHandling{
				Resp: res,
				Accepts: make(chan bool),
			}

			hdlr <- h

			accepted = <-h.Accepts
			if accepted {
				break
			}
		}

		c.handlersLocker.Unlock()

		if !accepted {<commit_msg>client: fixes closed conn warnings<commit_after>// An IMAP client.
package client

import (
	"crypto/tls"
	"io"
	"log"
	"net"
	"sync"

	imap "github.com/emersion/go-imap/common"
)

// An IMAP client.
type Client struct {
	conn *imap.Conn
	isTLS bool

	handlers []imap.RespHandler
	handlersLocker sync.Locker

	// The server capabilities.
	Caps map[string]bool
	// The current connection state.
	State imap.ConnState
	// The selected mailbox, if there is one.
	Mailbox *imap.MailboxStatus

	// A channel where info messages from the server will be sent.
	Infos chan *imap.StatusResp
	// A channel where warning messages from the server will be sent.
	Warnings chan *imap.StatusResp
	// A channel where error messages from the server will be sent.
	Errors chan *imap.StatusResp
	// A channel where bye messages from the server will be sent.
	Byes chan *imap.StatusResp
	// A channel where mailbox updates from the server will be sent.
	MailboxUpdates chan *imap.MailboxStatus
	// A channel where deleted message IDs will be sent.
	Expunges chan uint32

	// TODO: support unilateral message updates
	// A channel where messages updates from the server will be sent.
	//MessageUpdates chan *imap.Message
}

func (c *Client) read() error {
	r := c.conn.Reader

	defer (func () {
		c.handlersLocker.Lock()
		defer c.handlersLocker.Unlock()

		for _, hdlr := range c.handlers {
			close(hdlr)
		}
		c.handlers = nil
	})()

	for {
		if c.State == imap.LogoutState {
			return nil
		}

		c.conn.Wait()

		res, err := imap.ReadResp(r)
		if err == io.EOF || c.State == imap.LogoutState {
			return nil
		}
		if err != nil {
			log.Println("Error reading response:", err)
			continue
		}

		c.handlersLocker.Lock()

		var accepted bool
		for _, hdlr := range c.handlers {
			if hdlr == nil {
				continue
			}

			h := &imap.RespHandling{
				Resp: res,
				Accepts: make(chan bool),
			}

			hdlr <- h

			accepted = <-h.Accepts
			if accepted {
				break
			}
		}

		c.handlersLocker.Unlock()

		if !accepted {
{'fraction_non_alphanumeric': 0.10258358662613981, 'fraction_numerical': 0.0010131712259371835, 'mean_word_length': 2.9137760158572843, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '299295', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1346, 'n_words': 446}