initial commit
This commit is contained in:
commit
4e68012b47
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module ouch-relay
|
||||
|
||||
go 1.23.6
|
||||
|
||||
require git.thc420.dev/blackbeard420/irc v0.0.0-20250227032148-dbfee4e42d67
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
git.thc420.dev/blackbeard420/irc v0.0.0-20250227032148-dbfee4e42d67 h1:JjfZtOe6VaN72YNYrVzLL1LfynF+zbL3hIyTz065U4A=
|
||||
git.thc420.dev/blackbeard420/irc v0.0.0-20250227032148-dbfee4e42d67/go.mod h1:RtacSCxgLYXrVQBbcU9apo9ZvGckdTbn0BKCQ7HDWK0=
|
74
relay.go
Normal file
74
relay.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"git.thc420.dev/blackbeard420/irc"
|
||||
)
|
||||
|
||||
type Network struct {
|
||||
Url string `json:"url"`
|
||||
Nick string `json:"nick"`
|
||||
Channel string `json:"channel"`
|
||||
Name string `json:"name"`
|
||||
Connection irc.Connection
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Networks []Network `json:"networks"`
|
||||
}
|
||||
|
||||
var netmap map[string]*Network
|
||||
var netmutex sync.Mutex
|
||||
|
||||
func main() {
|
||||
buf, _ := os.ReadFile("config.json")
|
||||
|
||||
conf := Config{}
|
||||
|
||||
json.Unmarshal(buf, &conf)
|
||||
|
||||
netmap = make(map[string]*Network)
|
||||
|
||||
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})
|
||||
n.Connection.PrivmsgCallbackEx = handleMessage
|
||||
wg.Add(1)
|
||||
go runRelay(&n)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func runRelay(n *Network) {
|
||||
n.Connection.Run()
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getByConn(c *irc.Connection) string {
|
||||
for _, n := range netmap {
|
||||
if n.Connection.Sock == c.Sock {
|
||||
return n.Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user