source: code/trunk/vendor/gopkg.in/irc.v3/conn.go@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 2.7 KB
Line 
1package irc
2
3import (
4 "bufio"
5 "fmt"
6 "io"
7)
8
9// Conn represents a simple IRC client. It embeds an irc.Reader and an
10// irc.Writer.
11type Conn struct {
12 *Reader
13 *Writer
14}
15
16// NewConn creates a new Conn
17func NewConn(rw io.ReadWriter) *Conn {
18 return &Conn{
19 NewReader(rw),
20 NewWriter(rw),
21 }
22}
23
24// Writer is the outgoing side of a connection.
25type Writer struct {
26 // DebugCallback is called for each outgoing message. The name of this may
27 // not be stable.
28 DebugCallback func(line string)
29
30 // Internal fields
31 writer io.Writer
32 writeCallback func(w *Writer, line string) error
33}
34
35func defaultWriteCallback(w *Writer, line string) error {
36 _, err := w.writer.Write([]byte(line + "\r\n"))
37 return err
38}
39
40// NewWriter creates an irc.Writer from an io.Writer.
41func NewWriter(w io.Writer) *Writer {
42 return &Writer{nil, w, defaultWriteCallback}
43}
44
45// Write is a simple function which will write the given line to the
46// underlying connection.
47func (w *Writer) Write(line string) error {
48 if w.DebugCallback != nil {
49 w.DebugCallback(line)
50 }
51
52 return w.writeCallback(w, line)
53}
54
55// Writef is a wrapper around the connection's Write method and
56// fmt.Sprintf. Simply use it to send a message as you would normally
57// use fmt.Printf.
58func (w *Writer) Writef(format string, args ...interface{}) error {
59 return w.Write(fmt.Sprintf(format, args...))
60}
61
62// WriteMessage writes the given message to the stream
63func (w *Writer) WriteMessage(m *Message) error {
64 return w.Write(m.String())
65}
66
67// Reader is the incoming side of a connection. The data will be
68// buffered, so do not re-use the io.Reader used to create the
69// irc.Reader.
70type Reader struct {
71 // DebugCallback is called for each incoming message. The name of this may
72 // not be stable.
73 DebugCallback func(string)
74
75 // Internal fields
76 reader *bufio.Reader
77}
78
79// NewReader creates an irc.Reader from an io.Reader. Note that once a reader is
80// passed into this function, you should no longer use it as it is being used
81// inside a bufio.Reader so you cannot rely on only the amount of data for a
82// Message being read when you call ReadMessage.
83func NewReader(r io.Reader) *Reader {
84 return &Reader{
85 nil,
86 bufio.NewReader(r),
87 }
88}
89
90// ReadMessage returns the next message from the stream or an error.
91// It ignores empty messages.
92func (r *Reader) ReadMessage() (msg *Message, err error) {
93 // It's valid for a message to be empty. Clients should ignore these,
94 // so we do to be good citizens.
95 err = ErrZeroLengthMessage
96 for err == ErrZeroLengthMessage {
97 var line string
98 line, err = r.reader.ReadString('\n')
99 if err != nil {
100 return nil, err
101 }
102
103 if r.DebugCallback != nil {
104 r.DebugCallback(line)
105 }
106
107 // Parse the message from our line
108 msg, err = ParseMessage(line)
109 }
110 return msg, err
111}
Note: See TracBrowser for help on using the repository browser.