mtfosbot/module/twitch-irc/twitch-irc.go

212 lines
3.4 KiB
Go
Raw Normal View History

2018-09-12 10:07:49 +00:00
package twitchirc
import (
2018-09-15 15:48:40 +00:00
"crypto/tls"
2018-09-12 10:07:49 +00:00
"fmt"
2018-09-12 15:34:28 +00:00
"time"
2018-09-12 10:07:49 +00:00
2018-09-17 13:46:59 +00:00
"git.trj.tw/golang/mtfosbot/model"
2018-11-16 03:10:19 +00:00
"github.com/go-irc/irc"
2018-09-12 10:07:49 +00:00
"git.trj.tw/golang/mtfosbot/module/config"
)
var client *irc.Client
2018-09-12 15:34:28 +00:00
var queue *QueueList
var channels []string
2019-01-31 09:19:12 +00:00
var queueRunning bool
func init() {
queueRunning = false
}
2018-09-12 10:07:49 +00:00
// InitIRC -
2018-09-15 15:48:40 +00:00
func InitIRC() {
2018-09-12 10:07:49 +00:00
conf := config.GetConf()
2018-09-15 15:48:40 +00:00
tlsConf := &tls.Config{}
conn, err := tls.Dial("tcp", conf.Twitch.ChatHost, tlsConf)
// conn, err := net.Dial("tcp", conf.Twitch.ChatHost)
2018-09-12 10:07:49 +00:00
if err != nil {
2018-09-24 12:28:24 +00:00
fmt.Println("create irc connect fail ", err)
time.Sleep(time.Second * 3)
go InitIRC()
2018-09-12 10:07:49 +00:00
return
}
2018-09-15 15:48:40 +00:00
defer conn.Close()
channels = make([]string, 0)
queue = NewQueue()
runQueue()
2018-09-17 13:46:59 +00:00
ReJoin()
2018-09-15 15:48:40 +00:00
2018-09-12 10:07:49 +00:00
config := irc.ClientConfig{
Nick: conf.Twitch.BotUser,
2018-09-15 15:48:40 +00:00
Pass: conf.Twitch.BotOauth,
2018-09-12 10:07:49 +00:00
Handler: irc.HandlerFunc(ircHandle),
}
client = irc.NewClient(conn, config)
err = client.Run()
2018-09-15 15:48:40 +00:00
if err != nil {
fmt.Println("twitch chat connect fail")
2018-09-21 15:33:52 +00:00
// reconnect after 3sec
time.Sleep(time.Second * 3)
client = nil
channels = channels[:0]
queue.Clear()
go InitIRC()
2018-09-24 12:28:24 +00:00
return
2018-09-15 15:48:40 +00:00
}
2018-09-12 10:07:49 +00:00
}
2018-09-13 10:18:59 +00:00
// SendMessage -
func SendMessage(ch, msg string) {
if len(ch) == 0 {
return
}
if indexOf(channels, ch) == -1 {
return
}
m := &MsgObj{
Command: "PRIVMSG",
Params: []string{
fmt.Sprintf("#%s", ch),
2018-09-17 13:46:59 +00:00
fmt.Sprintf("%s", msg),
2018-09-13 10:18:59 +00:00
},
}
queue.Add(m)
}
2018-09-17 13:46:59 +00:00
// ReJoin -
func ReJoin() {
ch, err := model.GetAllTwitchChannel()
if err != nil {
return
}
LeaveAllChannel()
for _, v := range ch {
if v.Join {
JoinChannel(v.Name)
}
}
}
2018-09-12 15:34:28 +00:00
// JoinChannel -
func JoinChannel(ch string) {
if len(ch) == 0 {
return
}
m := &MsgObj{
Command: "JOIN",
Params: []string{
fmt.Sprintf("#%s", ch),
},
}
queue.Add(m)
}
// LeaveChannel -
func LeaveChannel(ch string) {
if len(ch) == 0 {
return
}
if indexOf(channels, ch) == -1 {
return
}
m := &MsgObj{
Command: "PART",
Params: []string{
fmt.Sprintf("#%s", ch),
},
}
queue.Add(m)
}
2018-09-15 15:48:40 +00:00
// LeaveAllChannel -
func LeaveAllChannel() {
2018-09-17 13:46:59 +00:00
if len(channels) == 0 {
return
}
2018-09-15 15:48:40 +00:00
for _, v := range channels {
m := &MsgObj{
Command: "PART",
Params: []string{
fmt.Sprintf("#%s", v),
},
}
queue.Add(m)
}
}
2018-09-12 15:34:28 +00:00
func runQueue() {
2019-01-31 09:19:12 +00:00
if queueRunning == true {
return
}
queueRunning = true
2018-09-15 15:48:40 +00:00
go func() {
cnt := 0
for {
2018-09-17 13:46:59 +00:00
if !queue.IsEmpty() && client != nil {
2018-09-15 15:48:40 +00:00
m := queue.Get()
msg := &irc.Message{}
msg.Command = m.Command
msg.Params = m.Params
if m.Command == "JOIN" {
if indexOf(channels, m.Params[0][1:]) != -1 {
continue
}
channels = append(channels, m.Params[0][1:])
} else if m.Command == "PART" {
if indexOf(channels, m.Params[0][1:]) == -1 {
continue
}
idx := indexOf(channels, m.Params[0][1:])
channels = append(channels[:idx], channels[idx+1:]...)
2018-09-13 10:18:59 +00:00
}
2018-09-15 15:48:40 +00:00
fmt.Println("< ", msg.String())
client.WriteMessage(msg)
}
cnt++
2018-09-21 15:33:52 +00:00
if cnt > 3600 {
2018-09-15 15:48:40 +00:00
// call rejoin
2018-09-17 13:46:59 +00:00
ReJoin()
cnt = 0
2018-09-12 15:34:28 +00:00
}
2018-09-15 15:48:40 +00:00
time.Sleep(time.Second * 1)
2018-09-12 15:34:28 +00:00
}
2018-09-15 15:48:40 +00:00
}()
2018-09-12 15:34:28 +00:00
}
2018-09-12 10:07:49 +00:00
func ircHandle(c *irc.Client, m *irc.Message) {
2018-09-13 10:18:59 +00:00
fmt.Println("> ", m.String())
2018-09-20 17:14:08 +00:00
if m.Command == "PING" {
tmp := &irc.Message{
Command: "PONG",
Params: []string{
m.Params[0],
},
}
2018-09-21 05:37:39 +00:00
fmt.Println("< ", tmp.String())
2018-09-20 17:14:08 +00:00
client.WriteMessage(tmp)
}
2018-09-12 15:34:28 +00:00
}
func indexOf(c []string, data string) int {
if len(c) == 0 {
return -1
}
for k, v := range c {
if v == data {
return k
}
}
return -1
2018-09-12 10:07:49 +00:00
}