Browse Source

organize like a proper go package

tags/v0.1.0
Jeremy Latt 10 years ago
parent
commit
b9cb539219
13 changed files with 57 additions and 123 deletions
  1. 0
    2
      .gitignore
  2. 0
    5
      build.sh
  3. 17
    0
      ergonomadic.go
  4. 1
    8
      irc/channel.go
  5. 1
    31
      irc/client.go
  6. 0
    0
      irc/commands.go
  7. 8
    1
      irc/constants.go
  8. 0
    4
      irc/net.go
  9. 17
    10
      irc/reply.go
  10. 13
    18
      irc/server.go
  11. 0
    13
      src/ergonomadic/ergonomadic.go
  12. 0
    13
      src/ergonomadicdb/ergonomadicdb.go
  13. 0
    18
      src/genpasswd/genpasswd.go

+ 0
- 2
.gitignore View File

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

+ 0
- 5
build.sh View File

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

+ 17
- 0
ergonomadic.go View File

@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import (
4
+	"flag"
5
+	"github.com/jlatt/ergonomadic/irc"
6
+)
7
+
8
+func main() {
9
+	name := flag.String("name", "localhost", "A name for the server")
10
+	listen := flag.String("listen", ":6667", "interface to listen on")
11
+	flag.BoolVar(&irc.DEBUG_NET, "dnet", false, "debug net")
12
+	flag.BoolVar(&irc.DEBUG_CLIENT, "dclient", false, "debug client")
13
+	flag.BoolVar(&irc.DEBUG_CHANNEL, "dchannel", false, "debug channel")
14
+	flag.BoolVar(&irc.DEBUG_SERVER, "dserver", false, "debug server")
15
+	flag.Parse()
16
+	irc.NewServer(*name).Listen(*listen)
17
+}

src/irc/channel.go → irc/channel.go View File

@@ -4,10 +4,6 @@ import (
4 4
 	"log"
5 5
 )
6 6
 
7
-const (
8
-	DEBUG_CHANNEL = true
9
-)
10
-
11 7
 type Channel struct {
12 8
 	commands  chan<- ChannelCommand
13 9
 	key       string
@@ -59,9 +55,7 @@ func (channel *Channel) receiveReplies(replies <-chan Reply) {
59 55
 			log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
60 56
 		}
61 57
 		for client := range channel.members {
62
-			if client != reply.Source() {
63
-				client.replies <- reply
64
-			}
58
+			client.replies <- reply
65 59
 		}
66 60
 	}
67 61
 }
@@ -117,7 +111,6 @@ func (channel *Channel) Join(client *Client) {
117 111
 	channel.members[client] = true
118 112
 	client.channels[channel] = true
119 113
 	reply := RplJoin(channel, client)
120
-	client.replies <- reply
121 114
 	channel.replies <- reply
122 115
 	channel.GetTopic(client)
123 116
 	channel.GetUsers(client)

src/irc/client.go → irc/client.go View File

@@ -7,20 +7,10 @@ import (
7 7
 	"time"
8 8
 )
9 9
 
10
-const (
11
-	DEBUG_CLIENT = false
12
-)
13
-
14
-type ClientCommand interface {
15
-	Command
16
-	HandleClient(*Client)
17
-}
18
-
19 10
 type Client struct {
20 11
 	atime      time.Time
21 12
 	away       bool
22 13
 	channels   ChannelSet
23
-	commands   chan<- ClientCommand
24 14
 	conn       net.Conn
25 15
 	hostname   string
26 16
 	nick       string
@@ -37,12 +27,10 @@ type ClientSet map[*Client]bool
37 27
 func NewClient(server *Server, conn net.Conn) *Client {
38 28
 	read := StringReadChan(conn)
39 29
 	write := StringWriteChan(conn)
40
-	commands := make(chan ClientCommand)
41 30
 	replies := make(chan Reply)
42 31
 
43 32
 	client := &Client{
44 33
 		channels: make(ChannelSet),
45
-		commands: commands,
46 34
 		conn:     conn,
47 35
 		hostname: LookupHostname(conn.RemoteAddr()),
48 36
 		replies:  replies,
@@ -51,7 +39,6 @@ func NewClient(server *Server, conn net.Conn) *Client {
51 39
 
52 40
 	go client.readConn(read)
53 41
 	go client.writeConn(write, replies)
54
-	go client.receiveCommands(commands)
55 42
 
56 43
 	return client
57 44
 }
@@ -82,15 +69,6 @@ func (c *Client) writeConn(write chan<- string, replies <-chan Reply) {
82 69
 	}
83 70
 }
84 71
 
85
-func (client *Client) receiveCommands(commands <-chan ClientCommand) {
86
-	for command := range commands {
87
-		if DEBUG_CLIENT {
88
-			log.Printf("%s → %s : %s", command.Client(), client, command)
89
-		}
90
-		command.HandleClient(client)
91
-	}
92
-}
93
-
94 72
 func (c *Client) Replies() chan<- Reply {
95 73
 	return c.replies
96 74
 }
@@ -135,17 +113,9 @@ func (c *Client) Id() string {
135 113
 }
136 114
 
137 115
 func (c *Client) String() string {
138
-	return c.hostname
116
+	return c.UserHost()
139 117
 }
140 118
 
141 119
 func (c *Client) PublicId() string {
142 120
 	return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
143 121
 }
144
-
145
-//
146
-// commands
147
-//
148
-
149
-func (m *PrivMsgCommand) HandleClient(client *Client) {
150
-	client.replies <- RplPrivMsg(m.Client(), client, m.message)
151
-}

src/irc/commands.go → irc/commands.go View File


src/irc/constants.go → irc/constants.go View File

@@ -1,7 +1,14 @@
1 1
 package irc
2 2
 
3
+var (
4
+	DEBUG_NET     = false
5
+	DEBUG_CLIENT  = false
6
+	DEBUG_CHANNEL = false
7
+	DEBUG_SERVER  = false
8
+)
9
+
3 10
 const (
4
-	VERSION = "ergonomadic-1"
11
+	VERSION = "irc-1"
5 12
 )
6 13
 
7 14
 const (

src/irc/net.go → irc/net.go View File

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

src/irc/reply.go → irc/reply.go View File

@@ -115,20 +115,27 @@ func (reply *NamesReply) Format(client *Client, write chan<- string) {
115 115
 	tooLong := func(names []string) bool {
116 116
 		return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
117 117
 	}
118
-	var start = 0
118
+	from, to := 0, 1
119 119
 	nicks := reply.channel.Nicks()
120
-	for i := range nicks {
121
-		if (i > start) && tooLong(nicks[start:i]) {
122
-			RplNamReply(reply.channel, nicks[start:i-1]).Format(client, write)
123
-			start = i - 1
120
+	for to < len(nicks) {
121
+		if (from < (to - 1)) && tooLong(nicks[from:to]) {
122
+			RplNamReply(reply.channel, nicks[from:to-1]).Format(client, write)
123
+			from, to = to-1, to
124
+		} else {
125
+			to += 1
124 126
 		}
125 127
 	}
126
-	if start < (len(nicks) - 1) {
127
-		RplNamReply(reply.channel, nicks[start:]).Format(client, write)
128
+	if from < len(nicks) {
129
+		RplNamReply(reply.channel, nicks[from:]).Format(client, write)
128 130
 	}
129 131
 	RplEndOfNames(reply.channel).Format(client, write)
130 132
 }
131 133
 
134
+func (reply *NamesReply) String() string {
135
+	return fmt.Sprintf("NamesReply(channel=%s, names=%s)",
136
+		reply.channel, reply.channel.Nicks())
137
+}
138
+
132 139
 // messaging replies
133 140
 
134 141
 func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
@@ -209,9 +216,9 @@ func RplNamReply(channel *Channel, names []string) *NumericReply {
209 216
 		channel.name, strings.Join(names, " "))
210 217
 }
211 218
 
212
-func RplEndOfNames(source Identifier) Reply {
213
-	return NewNumericReply(source, RPL_ENDOFNAMES,
214
-		":End of NAMES list")
219
+func RplEndOfNames(channel *Channel) Reply {
220
+	return NewNumericReply(channel, RPL_ENDOFNAMES,
221
+		"%s :End of NAMES list", channel.name)
215 222
 }
216 223
 
217 224
 func RplYoureOper(server *Server) Reply {

src/irc/server.go → irc/server.go View File

@@ -1,16 +1,11 @@
1 1
 package irc
2 2
 
3 3
 import (
4
-	"code.google.com/p/go.crypto/bcrypt"
5 4
 	"log"
6 5
 	"net"
7 6
 	"time"
8 7
 )
9 8
 
10
-const (
11
-	DEBUG_SERVER = true
12
-)
13
-
14 9
 type ChannelNameMap map[string]*Channel
15 10
 type ClientNameMap map[string]*Client
16 11
 
@@ -20,7 +15,7 @@ type Server struct {
20 15
 	ctime    time.Time
21 16
 	hostname string
22 17
 	name     string
23
-	password []byte
18
+	password string
24 19
 	clients  ClientNameMap
25 20
 }
26 21
 
@@ -54,7 +49,9 @@ func (s *Server) Listen(addr string) {
54 49
 	}
55 50
 
56 51
 	s.hostname = LookupHostname(listener.Addr())
57
-	log.Print("Server.Listen: listening on ", addr)
52
+	if DEBUG_SERVER {
53
+		log.Print("Server.Listen: listening on ", addr)
54
+	}
58 55
 
59 56
 	for {
60 57
 		conn, err := listener.Accept()
@@ -62,7 +59,9 @@ func (s *Server) Listen(addr string) {
62 59
 			log.Print("Server.Listen: ", err)
63 60
 			continue
64 61
 		}
65
-		log.Print("Server.Listen: accepted ", conn.RemoteAddr())
62
+		if DEBUG_SERVER {
63
+			log.Print("Server.Listen: accepted ", conn.RemoteAddr())
64
+		}
66 65
 		NewClient(s, conn)
67 66
 	}
68 67
 }
@@ -94,7 +93,7 @@ func (s *Server) interestedClients(fromClient *Client) ClientSet {
94 93
 // server functionality
95 94
 
96 95
 func (s *Server) tryRegister(c *Client) {
97
-	if !c.registered && c.HasNick() && c.HasUsername() && s.CheckPassword(c) {
96
+	if !c.registered && c.HasNick() && c.HasUsername() && c.serverPass {
98 97
 		c.registered = true
99 98
 		replies := []Reply{
100 99
 			RplWelcome(s, c),
@@ -108,10 +107,6 @@ func (s *Server) tryRegister(c *Client) {
108 107
 	}
109 108
 }
110 109
 
111
-func (s *Server) CheckPassword(c *Client) bool {
112
-	return (s.password == nil) || c.serverPass
113
-}
114
-
115 110
 func (s *Server) Id() string {
116 111
 	return s.name
117 112
 }
@@ -149,9 +144,9 @@ func (m *PongCommand) HandleServer(s *Server) {
149 144
 }
150 145
 
151 146
 func (m *PassCommand) HandleServer(s *Server) {
152
-	err := bcrypt.CompareHashAndPassword(s.password, []byte(m.password))
153
-	if err != nil {
147
+	if s.password != m.password {
154 148
 		m.Client().Replies() <- ErrPasswdMismatch(s)
149
+		// TODO disconnect
155 150
 		return
156 151
 	}
157 152
 
@@ -199,13 +194,14 @@ func (m *QuitCommand) HandleServer(s *Server) {
199 194
 	for client := range s.interestedClients(c) {
200 195
 		client.replies <- reply
201 196
 	}
202
-	c.conn.Close()
203 197
 	cmd := &PartCommand{
204 198
 		BaseCommand: BaseCommand{c},
205 199
 	}
206 200
 	for channel := range c.channels {
207 201
 		channel.commands <- cmd
208 202
 	}
203
+	c.conn.Close()
204
+	delete(s.clients, c.nick)
209 205
 }
210 206
 
211 207
 func (m *JoinCommand) HandleServer(s *Server) {
@@ -266,8 +262,7 @@ func (m *PrivMsgCommand) HandleServer(s *Server) {
266 262
 		m.Client().replies <- ErrNoSuchNick(s, m.target)
267 263
 		return
268 264
 	}
269
-
270
-	target.commands <- m
265
+	target.replies <- RplPrivMsg(m.Client(), target, m.message)
271 266
 }
272 267
 
273 268
 func (m *ModeCommand) HandleServer(s *Server) {

+ 0
- 13
src/ergonomadic/ergonomadic.go View File

@@ -1,13 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"flag"
5
-	"irc"
6
-)
7
-
8
-func main() {
9
-	name := flag.String("name", "localhost", "A name for the server")
10
-	listen := flag.String("listen", ":6667", "interface to listen on")
11
-	flag.Parse()
12
-	irc.NewServer(*name).Listen(*listen)
13
-}

+ 0
- 13
src/ergonomadicdb/ergonomadicdb.go View File

@@ -1,13 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"flag"
5
-	"irc"
6
-)
7
-
8
-func main() {
9
-	flag.Parse()
10
-	db := irc.NewDatabase()
11
-	defer db.Close()
12
-	irc.ExecSqlFile(db, flag.Arg(0)+".sql")
13
-}

+ 0
- 18
src/genpasswd/genpasswd.go View File

@@ -1,18 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"code.google.com/p/go.crypto/bcrypt"
5
-	"encoding/base64"
6
-	"flag"
7
-	"fmt"
8
-)
9
-
10
-func main() {
11
-	flag.Parse()
12
-	password := flag.Arg(0)
13
-	hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
14
-	if err != nil {
15
-		panic(err)
16
-	}
17
-	fmt.Println(base64.StdEncoding.EncodeToString(hash))
18
-}

Loading…
Cancel
Save