1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-05-12 18:44:50 +00:00

Initial commit of vague hackery for a go IRCd.

This commit is contained in:
Alex Bramley 2010-11-21 20:07:31 +00:00
parent 83e1f9ca74
commit b2539c0063
22 changed files with 1104 additions and 0 deletions

11
server/Makefile Normal file
View file

@ -0,0 +1,11 @@
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
include $(GOROOT)/src/Make.inc
TARG=irc/server
GOFILES=\
config.go\
include $(GOROOT)/src/Make.pkg

17
server/config/Makefile Normal file
View file

@ -0,0 +1,17 @@
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
include $(GOROOT)/src/Make.inc
TARG=irc/server/config
GOFILES=\
config.go\
port.go\
oper.go\
link.go\
ban.go\
info.go\
settings.go\
include $(GOROOT)/src/Make.pkg

33
server/config/ban.go Normal file
View file

@ -0,0 +1,33 @@
package config
type cBan interface {
Match(string) bool
Reason() string
}
// G-Line etc;
type cBanNick struct {
NickMask string // nick!ident@host
Reason string
}
// Z-Line
type cBanIP struct {
Address string // ip (or hostname), plus optional CIDR netmask
Reason string
ip string // parsed into these
cidr int
}
// CTCP version ban
type cBanVersion struct {
VersionRegex string // regex to match against version reply
Reason string
}
// Ban server from linking to network
type cBanServer struct {
ServerMask string // matched against name of linked server
Reason string
}

167
server/config/config.go Normal file
View file

@ -0,0 +1,167 @@
package config
import (
"io"
"os"
"fmt"
"strconv"
"scanner"
)
type Config struct {
fn string
scan *scanner.Scanner
// Ports we listen on.
Ports map[int]*cPort
// People with teh p0wer.
Opers map[string]*cOper
// Servers we link to on the network.
Links map[string]*cLink
// Servers/nickmasks/IPs that are unwanted.
Bans []*cBan
// Server info (name, admins, etc.)
Info *cInfo
// Server settings
Settings *cSettings
// Parse errors
Errors []os.Error
}
type configMap map[string]func(*Config)
type keywordMap map[string]func(*Config, interface{})
var configKeywords = configMap{
"port": (*Config).parsePort,
// "oper": (*Config).parseOper,
// "link": (*Config).parseLink,
// "ban": (*Config).parseBan,
// "info": (*Config).parseInfo,
// "set": (*Config).parseSettings,
}
func LoadConfig(filename string) *Config {
conf := &Config{fn: filename}
conf.initialise()
if fh, err := os.Open(conf.fn, os.O_RDONLY, 0644); err == nil {
conf.Parse(fh)
fh.Close()
} else {
conf.Errors = append(conf.Errors, err)
}
return conf
}
func (conf *Config) initialise() {
conf.Ports = make(map[int]*cPort)
conf.Opers = make(map[string]*cOper)
conf.Links = make(map[string]*cLink)
conf.Bans = make([]*cBan, 0)
conf.Info = &cInfo{}
conf.Settings = &cSettings{}
conf.Errors = make([]os.Error, 0)
}
func (conf *Config) Rehash() {
neu := LoadConfig(conf.fn)
if len(neu.Errors) > 0 {
conf.Errors = neu.Errors
} else {
conf = neu
}
}
func (conf *Config) Parse(io io.Reader) {
s := &scanner.Scanner{}
s.Init(io)
s.Filename = conf.fn
conf.scan = s
tok, text := conf.next()
for tok != scanner.EOF {
// This external loop should only parse Config things
if f, ok := configKeywords[text]; ok {
f(conf)
} else {
conf.parseError("Invalid top-level keyword '%s'", text)
}
fmt.Printf("Token: '%s', type %s\n", s.TokenText(), scanner.TokenString(tok))
tok, text = conf.next()
}
}
func (conf *Config) parseKwBlock(dst interface{}, bt string, kw keywordMap) {
if ok := conf.expect("{"); !ok {
conf.parseError("Expected %s configuration block.", bt)
return
}
tok, text := conf.next()
for tok != scanner.EOF {
if f, ok := kw[text]; ok {
if ok = conf.expect("="); ok {
f(conf, dst)
}
} else if text == "}" {
break
} else {
conf.parseError("Invalid %s keyword '%s'", bt, text)
}
tok, text = conf.next()
}
}
var booleans = map[string]bool {
"true": true,
"yes": true,
"on": true,
"1": true,
"false": false,
"no": false,
"off": false,
"0": false,
}
func (conf *Config) expectBool() (bool, bool) {
tok, text := conf.next()
if val, ok := booleans[text]; tok == scanner.Ident && ok {
return val, ok
}
conf.parseError("Expected boolean, got '%s'", text)
return false, false
}
func (conf *Config) expectInt() (int, bool) {
tok, text := conf.next()
num, err := strconv.Atoi(text)
if tok != scanner.Int || err != nil {
conf.parseError("Expected integer, got '%s'", text)
return 0, false
}
return num, true
}
func (conf *Config) expect(str string) bool {
_, text := conf.next()
if text != str {
conf.parseError("Expected '%s', got '%s'", str, text)
return false
}
return true
}
func (conf *Config) next() (int, string) {
tok := conf.scan.Scan()
text := conf.scan.TokenText()
if tok == scanner.String {
// drop "quotes" -> quotes
text = text[1:len(text)-1]
}
return tok, text
}
func (conf *Config) parseError(err string, args ...interface{}) {
err = conf.scan.Pos().String() + ": " + err
conf.Errors = append(conf.Errors, os.NewError(fmt.Sprintf(err, args...)))
}

8
server/config/info.go Normal file
View file

@ -0,0 +1,8 @@
package config
type cInfo struct {
Name, Network, Info, MOTDFile string
Admins []string
Numeric int
}

12
server/config/link.go Normal file
View file

@ -0,0 +1,12 @@
package config
type cLink struct {
Server string // Server name for link
Address string // {ip,ip6,host}:port
ReceivePass string // Password when server connects to us
ConnectPass string // Password when we connect to server
// Do we use tls.Dial? or compression (no)? Do we auto-connect on start?
SSL, Zip, Auto bool
}

16
server/config/oper.go Normal file
View file

@ -0,0 +1,16 @@
package config
type cOper struct {
Username, Password string
HostMask []string
// Permissions for oper
CanKill, CanBan, CanNick, CanLink bool
}
var cOperDefaults = &cOper{
HostMask: []string{"*@*"},
CanKill: true, CanBan: true,
CanNick: false, CanLink: false,
}

86
server/config/port.go Normal file
View file

@ -0,0 +1,86 @@
package config
import (
"fmt"
"strings"
"scanner"
)
type cPort struct {
Port int
BindIP, Family, Class string
// Is port a tls.Listener? Does it support compression (no)?
SSL, Zip bool
// address == "<BindIP>:<Port>"
address string
}
var portKeywords = keywordMap{
// "bind_ip": (*Config).parsePortBindIP,
// "family": (*Config).parsePortFamily,
"class": (*Config).parsePortClass,
"ssl": (*Config).parsePortSSL,
// "zip": (*Config).parsePortZip,
}
var cPortDefaults = cPort{
BindIP: "", Family: "tcp", Class: "client",
SSL: false, Zip: false,
}
func defaultPort() *cPort {
p := cPortDefaults
return &p
}
func (p *cPort) String() string {
str := []string{fmt.Sprintf("port %d {", p.Port)}
if p.BindIP != "" {
str = append(str, "\tbind_ip = " + p.BindIP)
}
str = append(str,
fmt.Sprintf("\tfamily = \"%s\"",p.Family),
fmt.Sprintf("\tclass = \"%s\"", p.Class),
fmt.Sprintf("\tssl = %t", p.SSL),
fmt.Sprintf("\tzip = %t", p.Zip),
"}",
)
return strings.Join(str, "\n")
}
func (conf *Config) parsePort() {
port := defaultPort()
portnum, ok := conf.expectInt()
if !ok || portnum > 65535 || portnum < 1024 {
conf.parseError("Invalid port '%s'", portnum)
port = nil
} else {
port.Port = portnum
conf.Ports[portnum] = port
}
if conf.scan.Peek() != '\n' {
conf.parseKwBlock(port, "port", portKeywords)
}
fmt.Println(port.String())
}
func (conf *Config) parsePortClass(pi interface{}) {
port := pi.(*cPort)
tok, text := conf.next()
if tok == scanner.String && (text == "server" || text == "client") {
port.Class = text
} else {
conf.parseError(
"Port class must be \"server\" or \"client\", got '%s'", text)
}
}
func (conf *Config) parsePortSSL(pi interface{}) {
port := pi.(*cPort)
if ssl, ok := conf.expectBool(); ok {
port.SSL = ssl
}
}

View file

@ -0,0 +1,8 @@
package config
type cSettings struct {
SSLKey, SSLCert, SSLCACert string
MaxChans, MaxConnsPerIP int
LogFile string
}

58
server/connmux.go Normal file
View file

@ -0,0 +1,58 @@
package server
import (
"bufio"
"net"
"crypto/tls"
"compress/zlib"
)
type Mux struct {
// Sockets we've created
listening []net.Listener
serving []net.Conn
// send and recieve channels to sockets
send map[*Node]chan string
recv chan string
// input/output/error channels to daemon
// Msg interface defined in parser.go
In chan *Msg
Out chan *Msg
Err chan os.Error
}
func (m *Mux) Serve(addr string, client bool, conf *tls.Config) os.Error {
var l net.Listener, s net.Conn, e os.Error
if conf == nil {
if l, e = net.Listen("tcp", addr); e != nil {
return e
}
} else {
if l, e = tls.Listen("tcp", addr, conf); e != nil {
return e
}
}
append(m.listening, l)
go func() {
for {
if s, e = l.Accept(); e != nil {
m.Err <- e
}
append(m.serving, s)
io := bufio.NewReadWriter(bufio.NewReader(s), bufio.NewWriter(s))
if client {
go m.clientSync(io)
} else {
// TODO(abramley): zlib support
go m.serverSync(io)
}
}
}
}
func (m *Mux) clientSync(in bufio.ReadWriter) {
}

1
server/handlers.go Normal file
View file

@ -0,0 +1 @@
package server

15
server/netmap.go Normal file
View file

@ -0,0 +1,15 @@
package server
type NetMap struct {
// To get to a specific Node, go via..
via map[*Node]*Node
// Network links, from server's perspective
links *Link
}
type Link struct {
node *Node
hops int
links []*Link
}

9
server/network.go Normal file
View file

@ -0,0 +1,9 @@
package server
type Network struct {
nodes map[string]*Node
chans map[string]*Channel
nicks map[string]*Nick
tree *NetMap
}

1
server/nickchan.go Normal file
View file

@ -0,0 +1 @@
package server

6
server/node.go Normal file
View file

@ -0,0 +1,6 @@
package server
type Node struct {
Name, Host string
}

1
server/parser.go Normal file
View file

@ -0,0 +1 @@
package server

1
server/server.go Normal file
View file

@ -0,0 +1 @@
package server