Refactor codebase and added:
- auto reconnect - cleaned up messages - gomirc for mirc colors
This commit is contained in:
parent
837ee02156
commit
acc5d051ff
2
go.mod
2
go.mod
@ -3,3 +3,5 @@ module ouch-relay
|
||||
go 1.23.6
|
||||
|
||||
require git.thc420.dev/blackbeard420/irc v0.0.0-20250228000710-2e8d1a50847a
|
||||
|
||||
require git.thc420.dev/blackbeard420/gomirc.git v0.0.0-20220305053047-90f01584fe5e // indirect
|
||||
|
2
go.sum
2
go.sum
@ -1,2 +1,4 @@
|
||||
git.thc420.dev/blackbeard420/gomirc.git v0.0.0-20220305053047-90f01584fe5e h1:m0WOq0V/hHKjVRUMAkljEb9i2NiLKQHoZBSI62D1l44=
|
||||
git.thc420.dev/blackbeard420/gomirc.git v0.0.0-20220305053047-90f01584fe5e/go.mod h1:I+euw3FxkDKk4pPiAGR0xDXYPPO7LzhgqS/0wL5N1ng=
|
||||
git.thc420.dev/blackbeard420/irc v0.0.0-20250228000710-2e8d1a50847a h1:bLa800VrICquajubOeRImr3UOer5DPTjrhv4Adf7S4A=
|
||||
git.thc420.dev/blackbeard420/irc v0.0.0-20250228000710-2e8d1a50847a/go.mod h1:RtacSCxgLYXrVQBbcU9apo9ZvGckdTbn0BKCQ7HDWK0=
|
||||
|
83
relay.go
83
relay.go
@ -3,9 +3,12 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.thc420.dev/blackbeard420/gomirc.git"
|
||||
"git.thc420.dev/blackbeard420/irc"
|
||||
)
|
||||
|
||||
@ -36,75 +39,71 @@ func main() {
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
for i, n := range conf.Networks {
|
||||
fmt.Printf("%d = [%s] %s\n", i, n.Name, n.Channel)
|
||||
netmap[n.Name] = &n
|
||||
n.Connection = *irc.NewConnection(n.Url, n.Nick, n.Nick, nil, []string{n.Channel})
|
||||
if !n.RecvOnly {
|
||||
n.Connection.PrivmsgCallbackEx = handleMessage
|
||||
n.Connection.JoinCallbackEx = handleJoin
|
||||
n.Connection.PartCallbackEx = handlePart
|
||||
n.Connection.QuitCallbackEx = handleQuit
|
||||
}
|
||||
for _, n := range conf.Networks {
|
||||
wg.Add(1)
|
||||
go runRelay(&n)
|
||||
initRelay(&n)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func initRelay(n *Network) {
|
||||
log.Printf("Connecting to [%s] %s\n", n.Name, n.Channel)
|
||||
|
||||
netmutex.Lock()
|
||||
defer netmutex.Unlock()
|
||||
|
||||
netmap[n.Name] = n
|
||||
n.Connection = *irc.NewConnection(n.Url, n.Nick, n.Nick, nil, []string{n.Channel})
|
||||
if !n.RecvOnly {
|
||||
n.Connection.PrivmsgCallbackEx = handleMessage
|
||||
n.Connection.JoinCallbackEx = handleJoin
|
||||
n.Connection.PartCallbackEx = handlePart
|
||||
n.Connection.QuitCallbackEx = handleQuit
|
||||
}
|
||||
|
||||
go runRelay(n)
|
||||
}
|
||||
|
||||
func deleteRelay(n string) {
|
||||
netmutex.Lock()
|
||||
defer netmutex.Unlock()
|
||||
delete(netmap, n)
|
||||
}
|
||||
|
||||
func runRelay(n *Network) {
|
||||
n.Connection.Run()
|
||||
relay(&n.Connection, "Network connection lost, retrying in 15 seconds")
|
||||
time.Sleep(time.Second * 15)
|
||||
initRelay(n)
|
||||
}
|
||||
|
||||
func handleMessage(c *irc.Connection, channel, nick, msg string) {
|
||||
netmutex.Lock()
|
||||
defer netmutex.Unlock()
|
||||
|
||||
us := getByConn(c)
|
||||
|
||||
for _, n := range netmap {
|
||||
if n.Name != us {
|
||||
n.Connection.SendPrivmsg(n.Channel, fmt.Sprintf("[%s] {%s} => %s", netmap[us].Name, irc.GetNick(nick), msg))
|
||||
}
|
||||
}
|
||||
relay(c, fmt.Sprintf("<%s> %s", irc.GetNick(nick), msg))
|
||||
}
|
||||
|
||||
func handleJoin(c *irc.Connection, from, target string) {
|
||||
netmutex.Lock()
|
||||
defer netmutex.Unlock()
|
||||
|
||||
us := getByConn(c)
|
||||
|
||||
for _, n := range netmap {
|
||||
if n.Name != us {
|
||||
n.Connection.SendPrivmsg(n.Channel, fmt.Sprintf("[%s] {%s} joined %s", netmap[us].Name, irc.GetNick(from), target))
|
||||
}
|
||||
}
|
||||
relay(c, fmt.Sprintf("<%s> joined %s", irc.GetNick(from), target))
|
||||
}
|
||||
|
||||
func handlePart(c *irc.Connection, from, target, msg string) {
|
||||
netmutex.Lock()
|
||||
defer netmutex.Unlock()
|
||||
|
||||
us := getByConn(c)
|
||||
|
||||
for _, n := range netmap {
|
||||
if n.Name != us {
|
||||
n.Connection.SendPrivmsg(n.Channel, fmt.Sprintf("[%s] {%s} parted %s %s", netmap[us].Name, irc.GetNick(from), target, msg))
|
||||
}
|
||||
}
|
||||
relay(c, fmt.Sprintf("<%s> parted %s %s", irc.GetNick(from), target, msg))
|
||||
}
|
||||
|
||||
func handleQuit(c *irc.Connection, from, msg string) {
|
||||
relay(c, fmt.Sprintf("<%s> has quit [%s]", irc.GetNick(from), msg))
|
||||
}
|
||||
|
||||
func relay(c *irc.Connection, msg string) {
|
||||
netmutex.Lock()
|
||||
defer netmutex.Unlock()
|
||||
|
||||
us := getByConn(c)
|
||||
networkid := gomirc.Grey().Sprintf("[%s]", netmap[us].Name)
|
||||
|
||||
for _, n := range netmap {
|
||||
if n.Name != us {
|
||||
n.Connection.SendPrivmsg(n.Channel, fmt.Sprintf("[%s] {%s} has quit [%s]", netmap[us].Name, irc.GetNick(from), msg))
|
||||
n.Connection.SendPrivmsg(n.Channel, fmt.Sprintf("%s %s", networkid, msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user