<commit_before>package mailman

import (
	"bytes"
	"fmt"
	"net/mail"
	"strconv"
	"strings"
	"time"

	"github.com/muesli/gomail"
	"github.com/mxk/go-imap/imap"

	"github.com/muesli/polly/api/db"
)

var (
	context *db.PollyContext
)

// SetupMailmanContext sets the context
func SetupMailmanContext(ctx *db.PollyContext) {
	context = ctx
}

func sendMail(tos []string, from, subject, body, mid, contenttype, contenttypetransfer string) {
	m := gomail.NewMessage()
	m.SetHeader("From", from)
	m.SetHeader("Subject", subject)
	m.SetHeader("Message-ID", mid)
	m.SetHeader("Errors-To", context.Config.Connections.Email.Mailman.BounceAddress)
	m.SetHeader("X-BeenThere", context.Config.Connections.Email.Mailman.Address)
	m.SetAddressHeader("To", context.Config.Connections.Email.Mailman.Address, context.Config.Connections.Email.Mailman.Name)
	m.SetAddressHeader("Envelope-Sender", context.Config.Connections.Email.Mailman.BounceAddress, context.Config.Connections.Email.Mailman.Name)
	m.SetAddressHeader("List-Id", context.Config.Connections.Email.Mailman.Address, context.Config.Connections.Email.Mailman.Name)

	if len(contenttype) > 0 {
		m.SetHeader("Content-Type", contenttype)
	}
	if len(contenttypetransfer) > 0 {
		m.SetHeader("Content-Transfer-Encoding", contenttypetransfer)
	}

	m.SetRawBody(body)

	d := gomail.NewDialer(context.Config.Connections.Email.SMTP.Server, context.Config.Connections.Email.SMTP.Port,
		context.Config.Connections.Email.SMTP.User, context.Config.Connections.Email.SMTP.Password)
	s, err := d.Dial()
	if err != nil {
		panic(err)
	}
	defer s.Close()

	err = s.Send(context.Config.Connections.Email.Mailman.Address, tos, m)
	if err != nil {
		panic(err)
	}
}

// RunLoop fetches mail and delivers them to recipients - forever
func RunLoop() {
	var (
		c   *imap.Client
		cmd *imap.Command
		rsp *imap.Response
	)

	// Connect to the server
	c, _ = imap.DialTLS(context.Config.Connections.Email.IMAP.Server+":"+strconv.FormatInt(int64(context.Config.Connections.Email.IMAP.Port), 10), nil)

	// Print server greeting (first response in the unilateral server data queue)
	fmt.Println("IMAP Server says hello:", c.Data[0].Info)
	c.Data = nil

	// Enable encryption, if supported by the server<commit_msg>Panic when IMAP login failed<commit_after>package mailman

import (
	"bytes"
	"fmt"
	"net/mail"
	"strconv"
	"strings"
	"time"

	"github.com/muesli/gomail"
	"github.com/mxk/go-imap/imap"

	"github.com/muesli/polly/api/db"
)

var (
	context *db.PollyContext
)

// SetupMailmanContext sets the context
func SetupMailmanContext(ctx *db.PollyContext) {
	context = ctx
}

func sendMail(tos []string, from, subject, body, mid, contenttype, contenttypetransfer string) {
	m := gomail.NewMessage()
	m.SetHeader("From", from)
	m.SetHeader("Subject", subject)
	m.SetHeader("Message-ID", mid)
	m.SetHeader("Errors-To", context.Config.Connections.Email.Mailman.BounceAddress)
	m.SetHeader("X-BeenThere", context.Config.Connections.Email.Mailman.Address)
	m.SetAddressHeader("To", context.Config.Connections.Email.Mailman.Address, context.Config.Connections.Email.Mailman.Name)
	m.SetAddressHeader("Envelope-Sender", context.Config.Connections.Email.Mailman.BounceAddress, context.Config.Connections.Email.Mailman.Name)
	m.SetAddressHeader("List-Id", context.Config.Connections.Email.Mailman.Address, context.Config.Connections.Email.Mailman.Name)

	if len(contenttype) > 0 {
		m.SetHeader("Content-Type", contenttype)
	}
	if len(contenttypetransfer) > 0 {
		m.SetHeader("Content-Transfer-Encoding", contenttypetransfer)
	}

	m.SetRawBody(body)

	d := gomail.NewDialer(context.Config.Connections.Email.SMTP.Server, context.Config.Connections.Email.SMTP.Port,
		context.Config.Connections.Email.SMTP.User, context.Config.Connections.Email.SMTP.Password)
	s, err := d.Dial()
	if err != nil {
		panic(err)
	}
	defer s.Close()

	err = s.Send(context.Config.Connections.Email.Mailman.Address, tos, m)
	if err != nil {
		panic(err)
	}
}

// RunLoop fetches mail and delivers them to recipients - forever
func RunLoop() {
	var (
		c   *imap.Client
		cmd *imap.Command
		rsp *imap.Response
		err error
	)

	// Connect to the server
	c, err = imap.DialTLS(context.Config.Connections.Email.IMAP.Server+":"+strconv.FormatInt(int64(context.Config.Connections.Email.IMAP.Port), 10), nil)
	if err != nil || c == nil || c.Data == nil {
		panic(err)
	}

	// Print server greeting (first response in the unilateral server data queue)
	fmt.Println("IMAP Server says hello:", c.Data[0].Info)
	c.Data = nil

	// Enable encryption, if supported by the server
{'fraction_non_alphanumeric': 0.1411635565312843, 'fraction_numerical': 0.0030735455543358945, 'mean_word_length': 4.730817610062893, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, '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': '316770', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1624, 'n_words': 292}