Skip to content

Instantly share code, notes, and snippets.

@cweagans
Last active August 16, 2017 21:29
Show Gist options
  • Save cweagans/833973fb59b3b36ee4ec82b1e4e5ac04 to your computer and use it in GitHub Desktop.
Save cweagans/833973fb59b3b36ee4ec82b1e4e5ac04 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
)
type Client struct {
incoming chan string
outgoing chan string
reader *bufio.Reader
writer *bufio.Writer
}
func (client *Client) Read() {
for {
line, err := client.reader.ReadString('\n')
if err != nil {
client.incoming <- err.Error()
break
}
client.incoming <- line
}
}
func (client *Client) Write() {
for data := range client.outgoing {
client.writer.WriteString(data)
client.writer.Flush()
}
}
func (client *Client) Listen() {
go client.Read()
go client.Write()
}
func NewClient(connection net.Conn) *Client {
writer := bufio.NewWriter(connection)
reader := bufio.NewReader(connection)
client := &Client{
incoming: make(chan string),
outgoing: make(chan string),
reader: reader,
writer: writer,
}
client.Listen()
return client
}
func WriteServerMessagesToScreen(client *Client) {
for {
message := <-client.incoming
log.Println(message)
}
}
/**
Next steps:
* Parse message out into a nice struct
* Set up a plugin system
* Write a plugin to automatically PONG on PING commands.
* Write a plugin to automatically say hello back to a user when greeted.
*/
func main() {
log.Println("Local: Connecting to freenode...")
conn, err := net.Dial("tcp", "irc.freenode.net:6667")
defer conn.Close()
if err != nil {
log.Fatalln(err)
}
client := NewClient(conn)
go WriteServerMessagesToScreen(client)
cmd := "NICK cweagansbot\n"
log.Println("Sending NICK cmd")
client.outgoing <- cmd
cmd = "USER cweagansbot cweagans cweagans :Cameron Eagans (bot)\n"
log.Println("Sending USER cmd")
client.outgoing <- cmd
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("IRC> ")
text, _ := reader.ReadString('\n')
client.outgoing <- text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment