diff options
| author | ilotterytea <iltsu@alright.party> | 2025-08-03 17:56:12 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-08-03 17:56:12 +0500 |
| commit | 9a20de85770f2f5ba09ad492aa2fb06782966096 (patch) | |
| tree | 9c5ef003093c6db54b44eb0c56b98f7b7c8cd9f5 /internal/client.go | |
| parent | c2432518c6ae67afa40c68c2a466ba34728e5de6 (diff) | |
feat: save messages into the database
Diffstat (limited to 'internal/client.go')
| -rw-r--r-- | internal/client.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/internal/client.go b/internal/client.go index 8adc12c..5099074 100644 --- a/internal/client.go +++ b/internal/client.go @@ -12,6 +12,7 @@ import ( ) type IRCClient struct { + db *sql.DB dbid int Host string @@ -25,8 +26,9 @@ type IRCClient struct { conn net.Conn } -func NewIRCClient(dbid int, host string, port int, nick string, pass string, capabilities []string) IRCClient { +func NewIRCClient(db *sql.DB, dbid int, host string, port int, nick string, pass string, capabilities []string) IRCClient { return IRCClient{ + db: db, dbid: dbid, Host: host, port: port, @@ -58,7 +60,13 @@ func (c *IRCClient) Connect() (err error) { for _, line := range strings.Split(raw, "\r\n") { message := ParseIRCMessage(line) - log.Printf("IRC message: %s\n", message) + + switch message.Command { + case "PRIVMSG", "JOIN", "PART": + message.Save(c.db, c.dbid) + case "PING": + c.SendRaw(fmt.Sprintf("PONG :%s", strings.Join(message.Params, " "))) + } } } } @@ -77,10 +85,10 @@ func (c *IRCClient) SendRaw(message string) { } } -func (c *IRCClient) JoinChannels(db *sql.DB) { +func (c *IRCClient) JoinChannels() { for { rooms := []string{} - rows, err := db.Query("SELECT name FROM rooms WHERE server_id = ? AND departed_at IS NULL", c.dbid) + rows, err := c.db.Query("SELECT name FROM rooms WHERE server_id = ? AND departed_at IS NULL", c.dbid) if err != nil { log.Fatalf("Failed to get rooms: %v\n", err) continue |
