Merge remote-tracking branch 'origin/master' into gcfg

Conflicts:
	ergonomadic.go
	irc/config.go
	irc/server.go
This commit is contained in:
Jeremy Latt 2014-03-02 11:41:24 -08:00
commit fa165a9d74
7 changed files with 91 additions and 55 deletions

View File

@ -1,48 +1,14 @@
package main package main
import ( import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"github.com/jlatt/ergonomadic/irc" "github.com/jlatt/ergonomadic/irc"
_ "github.com/mattn/go-sqlite3"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
) )
func genPasswd(passwd string) {
crypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
log.Fatal(err)
}
encoded := base64.StdEncoding.EncodeToString(crypted)
fmt.Println(encoded)
}
func initDB(config *irc.Config) {
os.Remove(config.Server.Database)
db, err := sql.Open("sqlite3", config.Server.Database)
if err != nil {
log.Fatal(err)
}
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(err)
}
}
func main() { func main() {
conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file") conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file")
initdb := flag.Bool("initdb", false, "initialize database") initdb := flag.Bool("initdb", false, "initialize database")
@ -50,7 +16,11 @@ func main() {
flag.Parse() flag.Parse()
if *passwd != "" { if *passwd != "" {
genPasswd(*passwd) encoded, err := irc.GenerateEncodedPassword(*passwd)
if err != nil {
log.Fatal(err)
}
fmt.Println(encoded)
return return
} }
@ -64,7 +34,8 @@ func main() {
} }
if *initdb { if *initdb {
initDB(config) irc.InitDB(config.Server.Database)
log.Println("database initialized: " + config.Server.Database)
return return
} }

View File

@ -1,7 +1,6 @@
package irc package irc
import ( import (
"code.google.com/p/go.crypto/bcrypt"
"code.google.com/p/go.text/unicode/norm" "code.google.com/p/go.text/unicode/norm"
"errors" "errors"
"fmt" "fmt"
@ -214,7 +213,7 @@ func (cmd *PassCommand) CheckPassword() {
if cmd.hash == nil { if cmd.hash == nil {
return return
} }
cmd.err = bcrypt.CompareHashAndPassword(cmd.hash, cmd.password) cmd.err = ComparePassword(cmd.hash, cmd.password)
} }
func NewPassCommand(args []string) (editableCommand, error) { func NewPassCommand(args []string) (editableCommand, error) {

View File

@ -2,7 +2,6 @@ package irc
import ( import (
"code.google.com/p/gcfg" "code.google.com/p/gcfg"
"encoding/base64"
"errors" "errors"
"log" "log"
) )
@ -12,10 +11,7 @@ type PassConfig struct {
} }
func (conf *PassConfig) PasswordBytes() []byte { func (conf *PassConfig) PasswordBytes() []byte {
if conf.Password == "" { bytes, err := DecodePassword(conf.Password)
return nil
}
bytes, err := base64.StdEncoding.DecodeString(conf.Password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -19,11 +19,11 @@ var (
// regexps // regexps
ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`) ChannelNameExpr = regexp.MustCompile(`^[&!#+][\pL\pN]{1,63}$`)
NicknameExpr = regexp.MustCompile( NicknameExpr = regexp.MustCompile(
"^[\\pL\\[\\]{}^`][\\pL\\pN\\[\\]{}^`]{1,31}$") "^[\\pL\\[\\]{}^`_][\\pL\\pN\\[\\]{}^`_]{1,31}$")
) )
const ( const (
SEM_VER = "ergonomadic-1.2.11" SEM_VER = "ergonomadic-1.2.12"
CRLF = "\r\n" CRLF = "\r\n"
MAX_REPLY_LEN = 512 - len(CRLF) MAX_REPLY_LEN = 512 - len(CRLF)

32
irc/database.go Normal file
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(err)
}
}
func OpenDB(path string) *sql.DB {
db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal(err)
}
return db
}

37
irc/password.go Normal file
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)
}

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"database/sql" "database/sql"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3"
"log" "log"
"net" "net"
"os" "os"
@ -13,6 +12,7 @@ import (
"runtime/debug" "runtime/debug"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"syscall"
"time" "time"
) )
@ -33,17 +33,12 @@ type Server struct {
} }
func NewServer(config *Config) *Server { func NewServer(config *Config) *Server {
db, err := sql.Open("sqlite3", config.Server.Database)
if err != nil {
log.Fatal(err)
}
server := &Server{ server := &Server{
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command, 16), commands: make(chan Command, 16),
ctime: time.Now(), ctime: time.Now(),
db: db, db: OpenDB(config.Server.Database),
idle: make(chan *Client, 16), idle: make(chan *Client, 16),
motdFile: config.Server.MOTD, motdFile: config.Server.MOTD,
name: config.Server.Name, name: config.Server.Name,
@ -54,7 +49,7 @@ func NewServer(config *Config) *Server {
timeout: make(chan *Client, 16), timeout: make(chan *Client, 16),
} }
signal.Notify(server.signals, os.Interrupt, os.Kill) signal.Notify(server.signals, syscall.SIGINT, syscall.SIGHUP)
server.loadChannels() server.loadChannels()
@ -135,14 +130,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() { func (server *Server) Run() {
done := false done := false
for !done { for !done {
select { select {
case <-server.signals: case <-server.signals:
server.db.Close() server.Shutdown()
done = true done = true
continue
case conn := <-server.newConns: case conn := <-server.newConns:
NewClient(server, conn) NewClient(server, conn)