From b9cb53921907ce6ffb7ea0da86ccfbb880cee7a8 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 8 Feb 2014 13:18:11 -0800 Subject: [PATCH] organize like a proper go package --- .gitignore | 2 -- build.sh | 5 ----- ergonomadic.go | 17 ++++++++++++++++ {src/irc => irc}/channel.go | 9 +-------- {src/irc => irc}/client.go | 32 +----------------------------- {src/irc => irc}/commands.go | 0 {src/irc => irc}/constants.go | 9 ++++++++- {src/irc => irc}/net.go | 4 ---- {src/irc => irc}/reply.go | 27 +++++++++++++++---------- {src/irc => irc}/server.go | 31 ++++++++++++----------------- src/ergonomadic/ergonomadic.go | 13 ------------ src/ergonomadicdb/ergonomadicdb.go | 13 ------------ src/genpasswd/genpasswd.go | 18 ----------------- 13 files changed, 57 insertions(+), 123 deletions(-) delete mode 100755 build.sh create mode 100644 ergonomadic.go rename {src/irc => irc}/channel.go (96%) rename {src/irc => irc}/client.go (77%) rename {src/irc => irc}/commands.go (100%) rename {src/irc => irc}/constants.go (97%) rename {src/irc => irc}/net.go (97%) rename {src/irc => irc}/reply.go (92%) rename {src/irc => irc}/server.go (90%) delete mode 100644 src/ergonomadic/ergonomadic.go delete mode 100644 src/ergonomadicdb/ergonomadicdb.go delete mode 100644 src/genpasswd/genpasswd.go diff --git a/.gitignore b/.gitignore index b90e3dfc..859b7a20 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ pkg bin -src/code.google.com/ -src/github.com/ ergonomadic.db diff --git a/build.sh b/build.sh deleted file mode 100755 index 9cbb1205..00000000 --- a/build.sh +++ /dev/null @@ -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 diff --git a/ergonomadic.go b/ergonomadic.go new file mode 100644 index 00000000..f2d5c1e3 --- /dev/null +++ b/ergonomadic.go @@ -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) +} diff --git a/src/irc/channel.go b/irc/channel.go similarity index 96% rename from src/irc/channel.go rename to irc/channel.go index ad824d18..36ee3831 100644 --- a/src/irc/channel.go +++ b/irc/channel.go @@ -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) diff --git a/src/irc/client.go b/irc/client.go similarity index 77% rename from src/irc/client.go rename to irc/client.go index 6e938456..ff1b0d8f 100644 --- a/src/irc/client.go +++ b/irc/client.go @@ -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) -} diff --git a/src/irc/commands.go b/irc/commands.go similarity index 100% rename from src/irc/commands.go rename to irc/commands.go diff --git a/src/irc/constants.go b/irc/constants.go similarity index 97% rename from src/irc/constants.go rename to irc/constants.go index 6633d379..f8e94b19 100644 --- a/src/irc/constants.go +++ b/irc/constants.go @@ -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 ( diff --git a/src/irc/net.go b/irc/net.go similarity index 97% rename from src/irc/net.go rename to irc/net.go index 043fc658..1f5ce209 100644 --- a/src/irc/net.go +++ b/irc/net.go @@ -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 { diff --git a/src/irc/reply.go b/irc/reply.go similarity index 92% rename from src/irc/reply.go rename to irc/reply.go index 036ba5d8..463c2ae8 100644 --- a/src/irc/reply.go +++ b/irc/reply.go @@ -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 { diff --git a/src/irc/server.go b/irc/server.go similarity index 90% rename from src/irc/server.go rename to irc/server.go index f0a14876..8e4cc604 100644 --- a/src/irc/server.go +++ b/irc/server.go @@ -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) { diff --git a/src/ergonomadic/ergonomadic.go b/src/ergonomadic/ergonomadic.go deleted file mode 100644 index 46e108fa..00000000 --- a/src/ergonomadic/ergonomadic.go +++ /dev/null @@ -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) -} diff --git a/src/ergonomadicdb/ergonomadicdb.go b/src/ergonomadicdb/ergonomadicdb.go deleted file mode 100644 index b724d7be..00000000 --- a/src/ergonomadicdb/ergonomadicdb.go +++ /dev/null @@ -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") -} diff --git a/src/genpasswd/genpasswd.go b/src/genpasswd/genpasswd.go deleted file mode 100644 index 093bf021..00000000 --- a/src/genpasswd/genpasswd.go +++ /dev/null @@ -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)) -}