Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
6d8815a0c3 | |||
0fc25eb941 | |||
1e10152db4 | |||
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=
|
||||
|
31
readme.md
31
readme.md
@ -2,6 +2,14 @@
|
||||
|
||||
ouchnets official relay client
|
||||
|
||||
## usage
|
||||
|
||||
`./ouch-relay` will load config.json from current directory
|
||||
|
||||
To specify config path use `./ouch-relay -cfg path/to/config`
|
||||
|
||||
`./ouch-relay -h` to print the usage message
|
||||
|
||||
## config
|
||||
|
||||
```
|
||||
@ -9,15 +17,24 @@ ouchnets official relay client
|
||||
"networks": [
|
||||
{
|
||||
"url": "irc.ouch.chat:6667",
|
||||
"nick": "relay1",
|
||||
"nick": "Alpha",
|
||||
"channel": "#testing",
|
||||
"name": "ouchNET",
|
||||
"name": "TestNet",
|
||||
"monitor": true
|
||||
},
|
||||
{
|
||||
"url": "irc.whatever.chat:6667",
|
||||
"nick": "relay2",
|
||||
"url": "irc.ouch.chat:6667",
|
||||
"nick": "Beta",
|
||||
"channel": "#test",
|
||||
"name": "whateverNET",
|
||||
"name": "OuchNet",
|
||||
"monitor": false
|
||||
},
|
||||
{
|
||||
"url": "irc.ouch.chat:6667",
|
||||
"nick": "Charlie",
|
||||
"channel": "#taliban",
|
||||
"name": "TalibanNet",
|
||||
"monitor": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -27,6 +44,6 @@ ouchnets official relay client
|
||||
- `nick` is the nickname you want the relay to use
|
||||
- `channel` is the channel you want the relay to occupy
|
||||
- `name` is the network Identifier for the relay
|
||||
- `recvonly` set to `true` to make connection only recieve relays, not send
|
||||
- `monitor` set to `true` to make connection only recieve messages from relay chain, not send
|
||||
|
||||
you may specify as many networks as you want under the json `"netoworks"` array. All networks will then have specified channels relayed across all networks
|
||||
you may specify as many networks as you want under the json `"networks"` array. All networks will then have specified channels relayed across all networks
|
97
relay.go
97
relay.go
@ -2,10 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.thc420.dev/blackbeard420/gomirc.git"
|
||||
"git.thc420.dev/blackbeard420/irc"
|
||||
)
|
||||
|
||||
@ -14,7 +18,7 @@ type Network struct {
|
||||
Nick string `json:"nick"`
|
||||
Channel string `json:"channel"`
|
||||
Name string `json:"name"`
|
||||
RecvOnly bool `json:"recvonly"`
|
||||
Monitor bool `json:"monitor"`
|
||||
Connection irc.Connection
|
||||
}
|
||||
|
||||
@ -26,7 +30,16 @@ var netmap map[string]*Network
|
||||
var netmutex sync.Mutex
|
||||
|
||||
func main() {
|
||||
buf, _ := os.ReadFile("config.json")
|
||||
cfgpath := flag.String("cfg", "config.json", "path to config file")
|
||||
flag.Parse()
|
||||
|
||||
log.Printf("Loading config: %s", *cfgpath)
|
||||
|
||||
buf, err := os.ReadFile(*cfgpath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("FATAL: %s", err.Error())
|
||||
}
|
||||
|
||||
conf := Config{}
|
||||
|
||||
@ -36,75 +49,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 = Monitor Mode: %t", n.Name, n.Channel, n.Monitor)
|
||||
|
||||
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.Monitor {
|
||||
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()
|
||||
log.Printf("Network connection lost [%s]", n.Name)
|
||||
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