Hi… I am well aware that this diff view is very suboptimal. It will be fixed when the refactored server comes along!
IRC sending queues
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileContributor: Runxi Yu <https://runxiyu.org>
package main
import (
"bufio"
"context"
"errors"
"os"
"github.com/jackc/pgx/v5/pgxpool"
"go.lindenii.runxiyu.org/lindenii-common/scfg"
)
var database *pgxpool.Pool
var config struct {
HTTP struct {
Net string `scfg:"net"`
Addr string `scfg:"addr"`
CookieExpiry int `scfg:"cookie_expiry"`
Root string `scfg:"root"`
} `scfg:"http"`
Hooks struct {
Socket string `scfg:"socket"`
Execs string `scfg:"execs"`
} `scfg:"hooks"`
Git struct {
RepoDir string `scfg:"repo_dir"`
} `scfg:"git"`
SSH struct {
Net string `scfg:"net"`
Addr string `scfg:"addr"`
Key string `scfg:"key"`
Root string `scfg:"root"`
} `scfg:"ssh"`
IRC struct {
Net string `scfg:"net"` Addr string `scfg:"addr"` TLS bool `scfg:"tls"`
Net string `scfg:"net"` Addr string `scfg:"addr"` TLS bool `scfg:"tls"` SendQ uint `scfg:"sendq"` Nick string `scfg:"nick"` User string `scfg:"user"` Gecos string `scfg:"gecos"`
} `scfg:"irc"`
General struct {
Title string `scfg:"title"`
} `scfg:"general"`
DB struct {
Type string `scfg:"type"`
Conn string `scfg:"conn"`
} `scfg:"db"`
}
func loadConfig(path string) (err error) {
var configFile *os.File
var decoder *scfg.Decoder
if configFile, err = os.Open(path); err != nil {
return err
}
defer configFile.Close()
decoder = scfg.NewDecoder(bufio.NewReader(configFile))
if err = decoder.Decode(&config); err != nil {
return err
}
if config.DB.Type != "postgres" {
return errors.New("unsupported database type")
}
if database, err = pgxpool.New(context.Background(), config.DB.Conn); err != nil {
return err
}
globalData["forge_title"] = config.General.Title
return nil
}
http {
# What network transport should we listen on?
# Examples: tcp tcp4 tcp6 unix
net tcp
# What address to listen on?
# Examples for net tcp*: 127.0.0.1:8080 :80
# Example for unix: /var/run/lindenii/forge/http.sock
addr /var/run/lindenii/forge/http.sock
# How many seconds should cookies be remembered before they are purged?
cookie_expiry 604800
# What is the canonical URL of the web root?
root https://forge.example.org
}
irc {
tls true
net tcp
addr irc.runxiyu.org:6697
sendq 6000
nick forge-test
user forge
gecos "Lindenii Forge Test"
}
git {
repo_dir /var/lib/lindenii/forge/repos
}
ssh {
# What network transport should we listen on?
# This should be "tcp" in almost all cases.
net tcp
# What address to listen on?
addr :22
# What is the path to the SSH host key? Generate it with ssh-keygen.
# The key must have an empty password.
key /etc/lindenii/ssh_host_ed25519_key
# What is the canonical SSH URL?
root ssh://forge.example.org
}
general {
title "Test Forge"
}
db {
# What type of database are we connecting to?
# Currently only "postgres" is supported.
type postgres
# What is the connection string?
conn postgresql:///lindenii-forge?host=/var/run/postgresql
}
hooks {
# On which UNIX domain socket should we listen for hook callbacks on?
socket /var/run/lindenii/forge/hooks.sock
# Where should hook executables be put?
execs /usr/libexec/lindenii/forge/hooks
}
package main import ( "crypto/tls" "net"
"go.lindenii.runxiyu.org/lindenii-common/clog"
"go.lindenii.runxiyu.org/lindenii-irc" )
var (
ircSendBuffered chan string
ircSendDirectChan chan errorBack[string]
)
type errorBack[T any] struct {
content T
errorBack chan error
}
func ircBotSession() error {
var err error
var underlyingConn net.Conn
if config.IRC.TLS {
underlyingConn, err = tls.Dial(config.IRC.Net, config.IRC.Addr, nil)
} else {
underlyingConn, err = net.Dial(config.IRC.Net, config.IRC.Addr)
}
if err != nil {
return (err)
return err
}
defer underlyingConn.Close()
conn := irc.NewConn(underlyingConn)
conn.WriteString("NICK forge\r\nUSER forge 0 * :Forge\r\n")
conn.WriteString(
"NICK " + config.IRC.Nick + "\r\n" +
"USER " + config.IRC.User + " 0 * :" + config.IRC.Gecos + "\r\n",
)
readLoopError := make(chan error)
writeLoopAbort := make(chan struct{})
go func() {
for {
select {
case <-writeLoopAbort:
return
default:
}
msg, err := conn.ReadMessage()
if err != nil {
readLoopError <- err
return
}
switch msg.Command {
case "001":
_, err = conn.WriteString("JOIN #chat\r\n")
if err != nil {
readLoopError <- err
return
}
case "PING":
_, err = conn.WriteString("PONG :" + msg.Args[0] + "\r\n")
if err != nil {
readLoopError <- err
return
}
case "JOIN":
_, err = conn.WriteString("PRIVMSG #chat :test\r\n")
if err != nil {
readLoopError <- err
return
}
default:
}
}
}()
for {
msg, err := conn.ReadMessage()
if err != nil {
return (err)
select {
case err = <-readLoopError:
return err
case s := <-ircSendBuffered:
_, err = conn.WriteString(s)
if err != nil {
select {
case ircSendBuffered <- s:
default:
clog.Error("unable to requeue IRC message: " + s)
}
writeLoopAbort <- struct{}{}
return err
}
case se := <-ircSendDirectChan:
_, err = conn.WriteString(se.content)
se.errorBack <- err
if err != nil {
writeLoopAbort <- struct{}{}
return err
}
}
switch msg.Command {
case "001":
conn.WriteString("JOIN #chat\r\n")
case "PING":
conn.WriteString("PONG :")
conn.WriteString(msg.Args[0])
conn.WriteString("\r\n")
case "JOIN":
conn.WriteString("PRIVMSG #chat :test\r\n")
default:
}
}
}
func ircSendDirect(s string) error {
ech := make(chan error, 1)
ircSendDirectChan <- errorBack[string]{
content: s,
errorBack: ech,
}
return <-ech
}
func ircBotLoop() {
ircSendBuffered = make(chan string, config.IRC.SendQ) ircSendDirectChan = make(chan errorBack[string])
for {
_ = ircBotSession()
err := ircBotSession()
clog.Error("IRC error: " + err.Error())
} }