Merge remote-tracking branch 'origin/master' into cap-protocol

Conflicts:
	irc/server.go
This commit is contained in:
Jeremy Latt
2014-03-06 10:34:50 -08:00
12 changed files with 194 additions and 222 deletions
+1 -2
View File
@@ -464,9 +464,8 @@ func (channel *Channel) Invite(invitee *Client, inviter *Client) {
return
}
// TODO Modify channel masks
inviter.RplInviting(invitee, channel.name)
invitee.Reply(RplInviteMsg(inviter, channel.name))
invitee.Reply(RplInviteMsg(inviter, invitee, channel.name))
if invitee.flags[Away] {
inviter.RplAway(invitee)
}
+1 -2
View File
@@ -1,7 +1,6 @@
package irc
import (
"code.google.com/p/go.crypto/bcrypt"
"code.google.com/p/go.text/unicode/norm"
"errors"
"fmt"
@@ -214,7 +213,7 @@ func (cmd *PassCommand) CheckPassword() {
if cmd.hash == nil {
return
}
cmd.err = bcrypt.CompareHashAndPassword(cmd.hash, cmd.password)
cmd.err = ComparePassword(cmd.hash, cmd.password)
}
func NewPassCommand(args []string) (editableCommand, error) {
+38 -62
View File
@@ -1,91 +1,67 @@
package irc
import (
"encoding/base64"
"encoding/json"
"code.google.com/p/gcfg"
"errors"
"log"
"os"
"path/filepath"
)
func decodePassword(password string) []byte {
if password == "" {
return nil
}
bytes, err := base64.StdEncoding.DecodeString(password)
type PassConfig struct {
Password string
}
func (conf *PassConfig) PasswordBytes() []byte {
bytes, err := DecodePassword(conf.Password)
if err != nil {
log.Fatal(err)
log.Fatal("decode password error: ", err)
}
return bytes
}
type Config struct {
Debug map[string]bool
Listeners []ListenerConfig
MOTD string
Name string
Operators []OperatorConfig
Password string
directory string
Server struct {
PassConfig
Database string
Listen []string
MOTD string
Name string
}
Operator map[string]*PassConfig
Debug struct {
Net bool
Client bool
Channel bool
Server bool
}
}
func (conf *Config) Database() string {
return filepath.Join(conf.directory, "ergonomadic.db")
}
func (conf *Config) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
func (conf *Config) OperatorsMap() map[string][]byte {
func (conf *Config) Operators() map[string][]byte {
operators := make(map[string][]byte)
for _, opConf := range conf.Operators {
operators[opConf.Name] = opConf.PasswordBytes()
for name, opConf := range conf.Operator {
operators[name] = opConf.PasswordBytes()
}
return operators
}
type OperatorConfig struct {
Name string
Password string
}
func (conf *OperatorConfig) PasswordBytes() []byte {
return decodePassword(conf.Password)
}
type ListenerConfig struct {
Net string
Address string
Key string
Certificate string
}
func (config *ListenerConfig) IsTLS() bool {
return (config.Key != "") && (config.Certificate != "")
}
func LoadConfig(filename string) (config *Config, err error) {
config = &Config{}
file, err := os.Open(filename)
err = gcfg.ReadFileInto(config, filename)
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
if config.Server.Name == "" {
err = errors.New("server.name missing")
return
}
config.directory = filepath.Dir(filename)
config.MOTD = filepath.Join(config.directory, config.MOTD)
for _, lconf := range config.Listeners {
if lconf.Net == "" {
lconf.Net = "tcp"
}
if config.Server.Database == "" {
err = errors.New("server.database missing")
return
}
if len(config.Server.Listen) == 0 {
err = errors.New("server.listen missing")
return
}
return
}
+2 -2
View File
@@ -19,11 +19,11 @@ var (
// regexps
ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`)
NicknameExpr = regexp.MustCompile(
"^[\\pL\\[\\]{}^`][\\pL\\pN\\[\\]{}^`]{1,31}$")
"^[\\pL\\[\\]{}^`_][\\pL\\pN\\[\\]{}^`_|]{1,31}$")
)
const (
SEM_VER = "ergonomadic-1.2.11"
SEM_VER = "ergonomadic-1.2.13"
CRLF = "\r\n"
MAX_REPLY_LEN = 512 - len(CRLF)
+32
View File
@@ -0,0 +1,32 @@
package irc
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
func InitDB(path string) {
os.Remove(path)
db := OpenDB(path)
defer db.Close()
_, err := db.Exec(`
CREATE TABLE channel (
name TEXT NOT NULL UNIQUE,
flags TEXT NOT NULL,
key TEXT NOT NULL,
topic TEXT NOT NULL,
user_limit INTEGER DEFAULT 0)`)
if err != nil {
log.Fatal("initdb error: ", err)
}
}
func OpenDB(path string) *sql.DB {
db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal("open db error: ", err)
}
return db
}
+37
View File
@@ -0,0 +1,37 @@
package irc
import (
"code.google.com/p/go.crypto/bcrypt"
"encoding/base64"
"errors"
)
var (
EmptyPasswordError = errors.New("empty password")
)
func GenerateEncodedPassword(passwd string) (encoded string, err error) {
if passwd == "" {
err = EmptyPasswordError
return
}
bcrypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
return
}
encoded = base64.StdEncoding.EncodeToString(bcrypted)
return
}
func DecodePassword(encoded string) (decoded []byte, err error) {
if encoded == "" {
err = EmptyPasswordError
return
}
decoded, err = base64.StdEncoding.DecodeString(encoded)
return
}
func ComparePassword(hash, password []byte) error {
return bcrypt.CompareHashAndPassword(hash, password)
}
+2 -2
View File
@@ -128,8 +128,8 @@ func RplError(message string) string {
return NewStringReply(nil, ERROR, ":%s", message)
}
func RplInviteMsg(inviter *Client, channel string) string {
return NewStringReply(inviter, INVITE, channel)
func RplInviteMsg(inviter *Client, invitee *Client, channel string) string {
return NewStringReply(inviter, INVITE, "%s :%s", invitee.Nick(), channel)
}
func RplKick(channel *Channel, client *Client, target *Client, comment string) string {
+25 -60
View File
@@ -2,12 +2,8 @@ package irc
import (
"bufio"
"crypto/rand"
"crypto/tls"
"database/sql"
"encoding/binary"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"net"
"os"
@@ -16,6 +12,7 @@ import (
"runtime/debug"
"runtime/pprof"
"strings"
"syscall"
"time"
)
@@ -35,34 +32,30 @@ type Server struct {
}
func NewServer(config *Config) *Server {
db, err := sql.Open("sqlite3", config.Database())
if err != nil {
log.Fatal(err)
}
server := &Server{
channels: make(ChannelNameMap),
clients: make(ClientNameMap),
commands: make(chan Command, 16),
ctime: time.Now(),
db: db,
db: OpenDB(config.Server.Database),
idle: make(chan *Client, 16),
motdFile: config.MOTD,
name: config.Name,
motdFile: config.Server.MOTD,
name: config.Server.Name,
newConns: make(chan net.Conn, 16),
operators: config.OperatorsMap(),
password: config.PasswordBytes(),
operators: config.Operators(),
password: config.Server.PasswordBytes(),
signals: make(chan os.Signal, 1),
}
signal.Notify(server.signals, os.Interrupt, os.Kill)
server.loadChannels()
for _, listenerConf := range config.Listeners {
go server.listen(listenerConf)
for _, addr := range config.Server.Listen {
go server.listen(addr)
}
signal.Notify(server.signals, syscall.SIGINT, syscall.SIGHUP,
syscall.SIGTERM, syscall.SIGQUIT)
return server
}
@@ -71,7 +64,7 @@ func (server *Server) loadChannels() {
SELECT name, flags, key, topic, user_limit
FROM channel`)
if err != nil {
log.Fatal(err)
log.Fatal("error loading channels: ", err)
}
for rows.Next() {
var name, flags, key, topic string
@@ -128,14 +121,20 @@ func (server *Server) processCommand(cmd Command) {
}
}
func (server *Server) Shutdown() {
server.db.Close()
for _, client := range server.clients {
client.Reply(RplNotice(server, client, "shutting down"))
}
}
func (server *Server) Run() {
done := false
for !done {
select {
case <-server.signals:
server.db.Close()
server.Shutdown()
done = true
continue
case conn := <-server.newConns:
NewClient(server, conn)
@@ -149,33 +148,18 @@ func (server *Server) Run() {
}
}
func newListener(config ListenerConfig) (net.Listener, error) {
if config.IsTLS() {
certificate, err := tls.LoadX509KeyPair(config.Certificate, config.Key)
if err != nil {
return nil, err
}
return tls.Listen("tcp", config.Address, &tls.Config{
Certificates: []tls.Certificate{certificate},
PreferServerCipherSuites: true,
})
}
return net.Listen("tcp", config.Address)
}
//
// listen goroutine
//
func (s *Server) listen(config ListenerConfig) {
listener, err := newListener(config)
func (s *Server) listen(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal(s, "listen error: ", err)
}
if DEBUG_SERVER {
log.Printf("%s listening on %s", s, config.Address)
log.Printf("%s listening on %s", s, addr)
}
for {
@@ -194,24 +178,6 @@ func (s *Server) listen(config ListenerConfig) {
}
}
func (s *Server) GenerateGuestNick() string {
bytes := make([]byte, 8)
for {
_, err := rand.Read(bytes)
if err != nil {
panic(err)
}
randInt, n := binary.Uvarint(bytes)
if n <= 0 {
continue // TODO handle error
}
nick := fmt.Sprintf("guest%d", randInt)
if s.clients.Get(nick) == nil {
return nick
}
}
}
//
// server functionality
//
@@ -880,9 +846,8 @@ func (msg *InviteCommand) HandleServer(server *Server) {
channel := server.channels.Get(msg.channel)
if channel == nil {
name := strings.ToLower(msg.channel)
client.RplInviting(target, name)
target.Reply(RplInviteMsg(client, name))
client.RplInviting(target, msg.channel)
target.Reply(RplInviteMsg(client, target, msg.channel))
return
}