package main

import (
	"crypto/tls"
	"fmt"
	"time"

	"github.com/thoj/go-ircevent"
)

const serverssl = "testnet.oragono.io:6697"

func main() {
	stamp := time.Now().UnixNano() % int64(time.Second)
	nick := fmt.Sprintf("deadlock-%d", stamp)
	channel := fmt.Sprintf("#ircevent-%d", stamp)
	irccon := irc.IRC(nick, "IRCTestSSL")
	irccon.Debug = true
	irccon.UseTLS = true
	irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
	irccon.AddCallback("001", func(e *irc.Event) { irccon.Join(channel) })
	irccon.AddCallback("366", func(e *irc.Event) {
		// simulate being disconnected in the middle of a callback:
		// send a raw QUIT command, outside of irccon.Quit(), which will
		// cause the server to disconnect us:
		irccon.SendRaw("QUIT")
		// fill up the local send queue:
		for i := 0; i < 10; i++ {
			irccon.Privmsg(channel, "hi")
		}
		// give the server time to disconnect us:
		time.Sleep(1 * time.Second)
		// fill up the send queue again:
		for i := 0; i < 20; i++ {
			irccon.Privmsg(channel, "hi")
		}
	})
	err := irccon.Connect(serverssl)
	if err != nil {
		fmt.Printf("Err %s", err)
		return
	}
	irccon.Loop()
}