3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

Merge pull request #10 from jlatt/cleanup

cleanup
This commit is contained in:
Jeremy Latt 2014-03-02 11:37:06 -08:00
commit 2340464265
6 changed files with 79 additions and 51 deletions

View File

@ -1,47 +1,12 @@
package main
import (
"code.google.com/p/go.crypto/bcrypt"
"database/sql"
"encoding/base64"
"flag"
"fmt"
"github.com/jlatt/ergonomadic/irc"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
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.Database())
db, err := sql.Open("sqlite3", config.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() {
conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file")
initdb := flag.Bool("initdb", false, "initialize database")
@ -49,7 +14,11 @@ func main() {
flag.Parse()
if *passwd != "" {
genPasswd(*passwd)
encoded, err := irc.GenerateEncodedPassword(*passwd)
if err != nil {
log.Fatal(err)
}
fmt.Println(encoded)
return
}
@ -59,7 +28,8 @@ func main() {
}
if *initdb {
initDB(config)
irc.InitDB(config.Database())
log.Println("database initialized: " + config.Database())
return
}

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) {

View File

@ -1,7 +1,6 @@
package irc
import (
"encoding/base64"
"encoding/json"
"log"
"os"
@ -9,10 +8,7 @@ import (
)
func decodePassword(password string) []byte {
if password == "" {
return nil
}
bytes, err := base64.StdEncoding.DecodeString(password)
bytes, err := DecodePassword(password)
if err != nil {
log.Fatal(err)
}

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

@ -7,7 +7,6 @@ import (
"database/sql"
"encoding/binary"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"net"
"os"
@ -37,17 +36,12 @@ 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.Database()),
idle: make(chan *Client, 16),
motdFile: config.MOTD,
name: config.Name,