Make timestamp format twiddleable; cosmetic re-arrangement of *Conn struct.

This commit is contained in:
Alex Bramley 2010-11-23 22:18:08 +00:00
parent 623247c995
commit bea2395160
1 changed files with 20 additions and 18 deletions

View File

@ -18,6 +18,13 @@ type Conn struct {
Me *Nick Me *Nick
Network string Network string
// Event handler mapping
events map[string][]func(*Conn, *Line)
// Map of channels we're on
chans map[string]*Channel
// Map of nicks we know about
nicks map[string]*Nick
// I/O stuff to server // I/O stuff to server
sock net.Conn sock net.Conn
io *bufio.ReadWriter io *bufio.ReadWriter
@ -25,29 +32,23 @@ type Conn struct {
out chan string out chan string
connected bool connected bool
// Error channel to transmit any fail back to the user
Err chan os.Error
// Misc knobs to tweak client behaviour:
// Are we connecting via SSL? Do we care about certificate validity? // Are we connecting via SSL? Do we care about certificate validity?
SSL bool SSL bool
SSLConfig *tls.Config SSLConfig *tls.Config
// Error channel to transmit any fail back to the user
Err chan os.Error
// Set this to true to disable flood protection and false to re-enable // Set this to true to disable flood protection and false to re-enable
Flood bool Flood bool
Debug bool
// Function which returns a *time.Time for use as a timestamp // Function which returns a *time.Time for use as a timestamp
Timestamp func() *time.Time Timestamp func() *time.Time
// Event handler mapping // Enable debugging? Set format for timestamps on debug output.
events map[string][]func(*Conn, *Line) Debug bool
TSFormat string
// Map of channels we're on
chans map[string]*Channel
// Map of nicks we know about
nicks map[string]*Nick
} }
// We parse an incoming line into this struct. Line.Cmd is used as the trigger // We parse an incoming line into this struct. Line.Cmd is used as the trigger
@ -66,9 +67,12 @@ type Line struct {
// that you can add event handlers to it. See AddHandler() for details. // that you can add event handlers to it. See AddHandler() for details.
func New(nick, user, name string) *Conn { func New(nick, user, name string) *Conn {
conn := new(Conn) conn := new(Conn)
conn.Timestamp = time.LocalTime
conn.initialise() conn.initialise()
conn.SSL = false
conn.SSLConfig = nil
conn.Me = conn.NewNick(nick, user, name, "") conn.Me = conn.NewNick(nick, user, name, "")
conn.Timestamp = time.LocalTime
conn.Format = "15:04:05"
conn.setupEvents() conn.setupEvents()
return conn return conn
} }
@ -80,8 +84,6 @@ func (conn *Conn) initialise() {
conn.in = make(chan *Line, 32) conn.in = make(chan *Line, 32)
conn.out = make(chan string, 32) conn.out = make(chan string, 32)
conn.Err = make(chan os.Error, 4) conn.Err = make(chan os.Error, 4)
conn.SSL = false
conn.SSLConfig = nil
conn.io = nil conn.io = nil
conn.sock = nil conn.sock = nil
@ -192,7 +194,7 @@ func (conn *Conn) send() {
} }
conn.io.Flush() conn.io.Flush()
if conn.Debug { if conn.Debug {
fmt.Println(conn.Timestamp().Format("[15:04:05]") + " -> " + line) fmt.Println(conn.Timestamp().Format(conn.Format) + " -> " + line)
} }
} }
} }
@ -209,7 +211,7 @@ func (conn *Conn) recv() {
} }
s = strings.Trim(s, "\r\n") s = strings.Trim(s, "\r\n")
if conn.Debug { if conn.Debug {
fmt.Println(t.Format("[15:04:05]") + " <- " + s) fmt.Println(t.Format(conn.Format) + " <- " + s)
} }
line := &Line{Raw: s, Time: t} line := &Line{Raw: s, Time: t}