From 4e68012b4798cec8079c1fc5e531f2b5a879f9e0 Mon Sep 17 00:00:00 2001 From: blackbeard420 Date: Wed, 26 Feb 2025 22:48:32 -0500 Subject: [PATCH] initial commit --- go.mod | 5 ++++ go.sum | 2 ++ relay.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 relay.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5ca11aa --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module ouch-relay + +go 1.23.6 + +require git.thc420.dev/blackbeard420/irc v0.0.0-20250227032148-dbfee4e42d67 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5d6c10f --- /dev/null +++ b/go.sum @@ -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= diff --git a/relay.go b/relay.go new file mode 100644 index 0000000..d10861b --- /dev/null +++ b/relay.go @@ -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 "" +}