Pārlūkot izejas kodu

organize like a proper go package

tags/v0.1.0
Jeremy Latt 10 gadus atpakaļ
vecāks
revīzija
b9cb539219

+ 0
- 2
.gitignore Parādīt failu

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

+ 0
- 5
build.sh Parādīt failu

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 Parādīt failu

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 Parādīt failu

4
 	"log"
4
 	"log"
5
 )
5
 )
6
 
6
 
7
-const (
8
-	DEBUG_CHANNEL = true
9
-)
10
-
11
 type Channel struct {
7
 type Channel struct {
12
 	commands  chan<- ChannelCommand
8
 	commands  chan<- ChannelCommand
13
 	key       string
9
 	key       string
59
 			log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
55
 			log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
60
 		}
56
 		}
61
 		for client := range channel.members {
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
 	channel.members[client] = true
111
 	channel.members[client] = true
118
 	client.channels[channel] = true
112
 	client.channels[channel] = true
119
 	reply := RplJoin(channel, client)
113
 	reply := RplJoin(channel, client)
120
-	client.replies <- reply
121
 	channel.replies <- reply
114
 	channel.replies <- reply
122
 	channel.GetTopic(client)
115
 	channel.GetTopic(client)
123
 	channel.GetUsers(client)
116
 	channel.GetUsers(client)

src/irc/client.go → irc/client.go Parādīt failu

7
 	"time"
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
 type Client struct {
10
 type Client struct {
20
 	atime      time.Time
11
 	atime      time.Time
21
 	away       bool
12
 	away       bool
22
 	channels   ChannelSet
13
 	channels   ChannelSet
23
-	commands   chan<- ClientCommand
24
 	conn       net.Conn
14
 	conn       net.Conn
25
 	hostname   string
15
 	hostname   string
26
 	nick       string
16
 	nick       string
37
 func NewClient(server *Server, conn net.Conn) *Client {
27
 func NewClient(server *Server, conn net.Conn) *Client {
38
 	read := StringReadChan(conn)
28
 	read := StringReadChan(conn)
39
 	write := StringWriteChan(conn)
29
 	write := StringWriteChan(conn)
40
-	commands := make(chan ClientCommand)
41
 	replies := make(chan Reply)
30
 	replies := make(chan Reply)
42
 
31
 
43
 	client := &Client{
32
 	client := &Client{
44
 		channels: make(ChannelSet),
33
 		channels: make(ChannelSet),
45
-		commands: commands,
46
 		conn:     conn,
34
 		conn:     conn,
47
 		hostname: LookupHostname(conn.RemoteAddr()),
35
 		hostname: LookupHostname(conn.RemoteAddr()),
48
 		replies:  replies,
36
 		replies:  replies,
51
 
39
 
52
 	go client.readConn(read)
40
 	go client.readConn(read)
53
 	go client.writeConn(write, replies)
41
 	go client.writeConn(write, replies)
54
-	go client.receiveCommands(commands)
55
 
42
 
56
 	return client
43
 	return client
57
 }
44
 }
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
 func (c *Client) Replies() chan<- Reply {
72
 func (c *Client) Replies() chan<- Reply {
95
 	return c.replies
73
 	return c.replies
96
 }
74
 }
135
 }
113
 }
136
 
114
 
137
 func (c *Client) String() string {
115
 func (c *Client) String() string {
138
-	return c.hostname
116
+	return c.UserHost()
139
 }
117
 }
140
 
118
 
141
 func (c *Client) PublicId() string {
119
 func (c *Client) PublicId() string {
142
 	return fmt.Sprintf("%s!%s@%s", c.Nick(), c.Nick(), c.server.Id())
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 Parādīt failu


src/irc/constants.go → irc/constants.go Parādīt failu

1
 package irc
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
 const (
10
 const (
4
-	VERSION = "ergonomadic-1"
11
+	VERSION = "irc-1"
5
 )
12
 )
6
 
13
 
7
 const (
14
 const (

src/irc/net.go → irc/net.go Parādīt failu

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

src/irc/reply.go → irc/reply.go Parādīt failu

115
 	tooLong := func(names []string) bool {
115
 	tooLong := func(names []string) bool {
116
 		return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
116
 		return (baseLen + joinedLen(names)) > MAX_REPLY_LEN
117
 	}
117
 	}
118
-	var start = 0
118
+	from, to := 0, 1
119
 	nicks := reply.channel.Nicks()
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
 	RplEndOfNames(reply.channel).Format(client, write)
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
 // messaging replies
139
 // messaging replies
133
 
140
 
134
 func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
141
 func RplPrivMsg(source Identifier, target Identifier, message string) Reply {
209
 		channel.name, strings.Join(names, " "))
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
 func RplYoureOper(server *Server) Reply {
224
 func RplYoureOper(server *Server) Reply {

src/irc/server.go → irc/server.go Parādīt failu

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

+ 0
- 13
src/ergonomadic/ergonomadic.go Parādīt failu

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 Parādīt failu

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 Parādīt failu

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
-}

Notiek ielāde…
Atcelt
Saglabāt