summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/client.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/internal/client.go b/internal/client.go
new file mode 100644
index 0000000..092504e
--- /dev/null
+++ b/internal/client.go
@@ -0,0 +1,61 @@
+package irclogs
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "net"
+ "net/textproto"
+)
+
+type IRCClient struct {
+ Host string
+ port int
+ nick string
+ pass string
+ caps []string
+
+ conn net.Conn
+}
+
+func NewIRCClient(host string, port int, nick string, pass string, capabilities []string) IRCClient {
+ return IRCClient{
+ Host: host,
+ port: port,
+ nick: nick,
+ pass: pass,
+ caps: capabilities,
+ }
+}
+
+func (c *IRCClient) Connect() (err error) {
+ conn, err := net.Dial("tcp", net.JoinHostPort(c.Host, fmt.Sprintf("%d", c.port)))
+ if err != nil {
+ return
+ }
+
+ c.conn = conn
+ c.authorize()
+
+ tp := textproto.NewReader(bufio.NewReader(c.conn))
+
+ for {
+ message, err := tp.ReadLine()
+ if err != nil {
+ log.Panicf("Failed to read a line: %v\n", err)
+ }
+ log.Printf("IRC message: %s\n", message)
+ }
+}
+
+func (c *IRCClient) authorize() {
+ c.SendRaw(fmt.Sprintf("NICK %s", c.nick))
+ c.SendRaw(fmt.Sprintf("PASS %s", c.pass))
+ for _, cap := range c.caps {
+ c.SendRaw(fmt.Sprintf("CAP REQ :%s", cap))
+ }
+}
+
+func (c *IRCClient) SendRaw(message string) {
+ fmt.Fprintf(c.conn, "%s\r\n", message)
+}