Skip to content

Commit

Permalink
DRY server link creation, don't crash on Juped server squit
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
Xe committed Aug 29, 2015
1 parent 976d5a6 commit bbc58cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
42 changes: 23 additions & 19 deletions bot/basehandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tetra

import (
"fmt"
"log"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -558,22 +559,7 @@ func handleSID(line *r1459.RawLine) {
Log.Panic("No server by ID " + line.Source + ", desync")
}

server := &Server{
Name: line.Args[0],
Gecos: line.Args[3],
Sid: line.Args[2],
Links: []*Server{parent},
Counter: metrics.NewGauge(),
}

var err error
server.Hops, err = strconv.Atoi(line.Args[1])
if err != nil {
return
}

parent.Links = append(parent.Links, server)

server := NewServer(parent, line.Args[0], line.Args[3], line.Args[2], line.Args[1])
Servers[server.Sid] = server

metrics.Register(server.Name+"_clients", server.Counter)
Expand All @@ -587,10 +573,28 @@ func handlePASS(line *r1459.RawLine) {

func handleSERVER(line *r1459.RawLine) {
// <<< SERVER fluttershy.yolo-swag.com 1 :shadowircd test server
Uplink.Name = line.Args[0]
Uplink.Gecos = line.Args[2]
if line.Source == "" {
Uplink.Name = line.Args[0]
Uplink.Gecos = line.Args[2]

metrics.Register(Uplink.Name+"_clients", Uplink.Counter)
return
}

// :services.int SERVER foo.bar 3 :(H) [Xena] test
var parent *Server

for _, server := range Servers {
if server.Name == line.Source {
parent = server
}
}

if parent == nil {
log.Fatalf("Unknown server %s, desync", line.Source)
}

metrics.Register(Uplink.Name+"_clients", Uplink.Counter)
Servers[line.Source] = NewServer(parent, line.Args[0], line.Args[2], line.Args[0], line.Args[1])
}

func handleWHOIS(line *r1459.RawLine) {
Expand Down
18 changes: 18 additions & 0 deletions bot/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tetra

import (
"strconv"

"github.com/rcrowley/go-metrics"
)

Expand All @@ -25,3 +27,19 @@ func (s *Server) AddClient() {
func (s *Server) DelClient() {
s.Count--
}

// NewServer allocates a new server struct, fitting it into the network.
func NewServer(parent *Server, name, gecos, id, hops string) *Server {
s := &Server{
Sid: id,
Name: name,
Gecos: gecos,
Links: []*Server{parent},
Counter: metrics.NewGauge(),
}

s.Hops, _ = strconv.Atoi(hops)
parent.Links = append(parent.Links, s)

return s
}

0 comments on commit bbc58cc

Please sign in to comment.