3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-25 12:12:35 +01:00

organize like a proper go package

This commit is contained in:
Jeremy Latt 2014-02-08 13:18:11 -08:00
parent f04dd7c5d5
commit b9cb539219
13 changed files with 57 additions and 123 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
pkg
bin
src/code.google.com/
src/github.com/
ergonomadic.db

View File

@ -1,5 +0,0 @@
#!/bin/bash
export GOPATH="$PWD"
#go get "code.google.com/p/go.crypto/bcrypt"
#go get "github.com/mattn/go-sqlite3"
go install ergonomadic genpasswd

17
ergonomadic.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"flag"
"github.com/jlatt/ergonomadic/irc"
)
func main() {
name := flag.String("name", "localhost", "A name for the server")
listen := flag.String("listen", ":6667", "interface to listen on")
flag.BoolVar(&irc.DEBUG_NET, "dnet", false, "debug net")
flag.BoolVar(&irc.DEBUG_CLIENT, "dclient", false, "debug client")
flag.BoolVar(&irc.DEBUG_CHANNEL, "dchannel", false, "debug channel")
flag.BoolVar(&irc.DEBUG_SERVER, "dserver", false, "debug server")
flag.Parse()
irc.NewServer(*name).Listen(*listen)
}

View File

@ -4,10 +4,6 @@ import (
"log"
)
const (
DEBUG_CHANNEL = true
)
type Channel struct {
commands chan<- ChannelCommand
key string
@ -59,9 +55,7 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
}
for client := range channel.members {
if client != reply.Source() {
client.replies <- reply
}
client.replies <- reply
}
}
}
@ -117,7 +111,6 @@ func (channel *Channel) Join(client *Client) {
channel.members[client] = true
client.channels[channel] = true
reply := RplJoin(channel, client)
client.replies <- reply
channel.replies <- reply
channel.GetTopic(client)
channel.GetUsers(client)

View File

@ -7,20 +7,10 @@ import (
"time"
)
const (
DEBUG_CLIENT = false
)
type ClientCommand interface {
Command
HandleClient(*Client)
}
type Client struct {
atime time.Time
away bool
channels ChannelSet
commands chan<- ClientCommand
conn net.Conn
hostname string
nick string
@ -37,12 +27,10 @@ type ClientSet map[*Client]bool
func NewClient(server *Server, conn net.Conn) *Client {
read := StringReadChan(conn)
write := StringWriteChan(conn)
commands := make(chan ClientCommand)
replies := make(chan Reply)
client := &Client{
channels: make(ChannelSet),
commands: commands,
conn: conn,
hostname: LookupHostname(conn.RemoteAddr()),
replies: replies,
@ -51,7 +39,6 @@ func NewClient(server *Server, conn net.Conn) *Client {
go client.readConn(read)
go client.writeConn(write, replies)
go client.receiveCommands(commands)
return client
}
@ -82,15 +69,6 @@ func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
}
}
func (client *Client) receiveCommands(commands <-chan ClientCommand) {
for command := range commands {
if DEBUG_CLIENT {
log.Printf("%s → %s : %s", command.Client(), client, command)
}
command.HandleClient(client)
}
}
func (c *Client) Replies() chan<- Reply {
return c.replies
}
@ -135,17 +113,9 @@ func (c *Client) Id() string {
}
func (c *Client) String() string {
return c.hostname
return c.UserHost()
}
func (c *Client) PublicId() string {
return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
}
//
// commands
//
func (m *PrivMsgCommand) HandleClient(client *Client) {
client.replies <- RplPrivMsg(m.Client(), client, m.message)
}

View File

@ -1,7 +1,14 @@
package irc
var (
DEBUG_NET = false
DEBUG_CLIENT = false
DEBUG_CHANNEL = false
DEBUG_SERVER = false
)
const (
VERSION = "ergonomadic-1"
VERSION = "irc-1"
)
const (

View File

@ -7,10 +7,6 @@ import (
"strings"
)
const (
DEBUG_NET = false
)
func readTrimmedLine(reader *bufio.Reader) (string, error) {
line, err := reader.ReadString('\n')
if err != nil {

View File

@ -115,20 +115,27 @@ func (reply *NamesReply) Format(client *Client, write chan<- string) {
tooLong := func(names []string) bool {
return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
}
var start = 0
from, to := 0, 1
nicks := reply.channel.Nicks()
for i := range nicks {
if (i > start) && tooLong(nicks[start:i]) {
RplNamReply(reply.channel, nicks[start:i-1]).Format(client, write)
start = i - 1
for to < len(nicks) {
if (from < (to - 1)) && tooLong(nicks[from:to]) {
RplNamReply(reply.channel, nicks[from:to-1]).Format(client, write)
from, to = to-1, to
} else {
to += 1
}
}
if start < (len(nicks) - 1) {
RplNamReply(reply.channel, nicks[start:]).Format(client, write)
if from < len(nicks) {
RplNamReply(reply.channel, nicks[from:]).Format(client, write)
}
RplEndOfNames(reply.channel).Format(client, write)
}
func (reply *NamesReply) String() string {
return fmt.Sprintf("NamesReply(channel=%s, names=%s)",
reply.channel, reply.channel.Nicks())
}
// messaging replies
func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
@ -209,9 +216,9 @@ func RplNamReply(channel *Channel, names []string) *NumericReply {
channel.name, strings.Join(names, " "))
}
func RplEndOfNames(source Identifier) Reply {
return NewNumericReply(source, RPL_ENDOFNAMES,
":End of NAMES list")
func RplEndOfNames(channel *Channel) Reply {
return NewNumericReply(channel, RPL_ENDOFNAMES,
"%s :End of NAMES list", channel.name)
}
func RplYoureOper(server *Server) Reply {

View File

@ -1,16 +1,11 @@
package irc
import (
"code.google.com/p/go.crypto/bcrypt"
"log"
"net"
"time"
)
const (
DEBUG_SERVER = true
)
type ChannelNameMap map[string]*Channel
type ClientNameMap map[string]*Client
@ -20,7 +15,7 @@ type Server struct {
ctime time.Time
hostname string
name string
password []byte
password string
clients ClientNameMap
}
@ -54,7 +49,9 @@ func (s *Server) Listen(addr string) {
}
s.hostname = LookupHostname(listener.Addr())
log.Print("Server.Listen: listening on ", addr)
if DEBUG_SERVER {
log.Print("Server.Listen: listening on ", addr)
}
for {
conn, err := listener.Accept()
@ -62,7 +59,9 @@ func (s *Server) Listen(addr string) {
log.Print("Server.Listen: ", err)
continue
}
log.Print("Server.Listen: accepted ", conn.RemoteAddr())
if DEBUG_SERVER {
log.Print("Server.Listen: accepted ", conn.RemoteAddr())
}
NewClient(s, conn)
}
}
@ -94,7 +93,7 @@ func (s *Server) interestedClients(fromClient *Client) ClientSet {
// server functionality
func (s *Server) tryRegister(c *Client) {
if !c.registered && c.HasNick() && c.HasUsername() && s.CheckPassword(c) {
if !c.registered && c.HasNick() && c.HasUsername() && c.serverPass {
c.registered = true
replies := []Reply{
RplWelcome(s, c),
@ -108,10 +107,6 @@ func (s *Server) tryRegister(c *Client) {
}
}
func (s *Server) CheckPassword(c *Client) bool {
return (s.password == nil) || c.serverPass
}
func (s *Server) Id() string {
return s.name
}
@ -149,9 +144,9 @@ func (m *PongCommand) HandleServer(s *Server) {
}
func (m *PassCommand) HandleServer(s *Server) {
err := bcrypt.CompareHashAndPassword(s.password, []byte(m.password))
if err != nil {
if s.password != m.password {
m.Client().Replies() <- ErrPasswdMismatch(s)
// TODO disconnect
return
}
@ -199,13 +194,14 @@ func (m *QuitCommand) HandleServer(s *Server) {
for client := range s.interestedClients(c) {
client.replies <- reply
}
c.conn.Close()
cmd := &PartCommand{
BaseCommand: BaseCommand{c},
}
for channel := range c.channels {
channel.commands <- cmd
}
c.conn.Close()
delete(s.clients, c.nick)
}
func (m *JoinCommand) HandleServer(s *Server) {
@ -266,8 +262,7 @@ func (m *PrivMsgCommand) HandleServer(s *Server) {
m.Client().replies <- ErrNoSuchNick(s, m.target)
return
}
target.commands <- m
target.replies <- RplPrivMsg(m.Client(), target, m.message)
}
func (m *ModeCommand) HandleServer(s *Server) {

View File

@ -1,13 +0,0 @@
package main
import (
"flag"
"irc"
)
func main() {
name := flag.String("name", "localhost", "A name for the server")
listen := flag.String("listen", ":6667", "interface to listen on")
flag.Parse()
irc.NewServer(*name).Listen(*listen)
}

View File

@ -1,13 +0,0 @@
package main
import (
"flag"
"irc"
)
func main() {
flag.Parse()
db := irc.NewDatabase()
defer db.Close()
irc.ExecSqlFile(db, flag.Arg(0)+".sql")
}

View File

@ -1,18 +0,0 @@
package main
import (
"code.google.com/p/go.crypto/bcrypt"
"encoding/base64"
"flag"
"fmt"
)
func main() {
flag.Parse()
password := flag.Arg(0)
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
panic(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(hash))
}