new nicklist shit

This commit is contained in:
blackbeard420 2020-09-01 22:18:43 -04:00
parent 24590994f2
commit dbfd78d4e1
2 changed files with 55 additions and 7 deletions

55
irc.go
View File

@ -8,6 +8,12 @@ import (
"strconv"
"strings"
"time"
"unicode"
)
const (
//IrcVersion exports the current version of the irc lib
IrcVersion = "0.1.0"
)
//Connection holds the callbacks for the client
@ -123,13 +129,47 @@ func hasNick(nick string, names []string) bool {
return false
}
func hasOpSymbol(nick string) bool {
s := rune(nick[0])
if !unicode.IsLetter(s) && !unicode.IsNumber(s) {
return true
}
return false
}
func (c *Connection) updateNicks(channel string, names []string) {
log.Printf("updating channel %s with %d nicks\n", channel, len(names))
for _, i := range names {
if !hasNick(i, c.userList[channel]) {
c.userList[channel] = append(c.userList[channel], i)
log.Printf("added nick: %s to channel %s\n", i, channel)
c.addNick(channel, i)
}
}
func (c *Connection) addNick(channel string, nick string) {
if !hasNick(nick, c.userList[channel]) {
if hasOpSymbol(nick) {
nick = nick[1:]
}
log.Printf("added nick: %s to channel %s\n", nick, channel)
c.userList[channel] = append(c.userList[channel], nick)
}
}
func (c *Connection) removeNick(channel string, nick string) {
nicks := []string{}
for _, i := range c.userList[channel] {
if i != nick {
nicks = append(nicks, i)
}
}
log.Printf("removed nick: %s from channel %s\n", nick, channel)
c.userList[channel] = nicks
}
func (c *Connection) removeNickAllChans(nick string) {
for k := range c.userList {
c.removeNick(k, nick)
}
}
@ -154,10 +194,10 @@ func (c *Connection) parseMessage(line string) {
}
if cmd == "353" {
idx := strings.Index(args, "=")
idx += 2
idx := strings.Index(args, "=") + 2
s := strings.Split(args[idx:], ":")
c.updateNicks(strings.ToLower(s[0]), strings.Split(s[1], " "))
target := strings.TrimSpace(strings.ToLower(s[0]))
c.updateNicks(target, strings.Split(s[1], " "))
}
if c.NumericCallback != nil {
@ -184,14 +224,17 @@ func (c *Connection) parseMessage(line string) {
}
}
case "join":
c.addNick(target, GetNick(from))
if c.JoinCallback != nil {
c.JoinCallback(from, target)
}
case "quit":
c.removeNickAllChans(GetNick(from))
if c.QuitCallback != nil {
c.QuitCallback(from, msg)
}
case "part":
c.removeNick(target, GetNick(from))
if c.PartCallback != nil {
c.PartCallback(from, target, msg)
}

View File

@ -1,6 +1,7 @@
package irc
import (
"fmt"
"log"
"testing"
"time"
@ -11,9 +12,13 @@ func TestSimpleIrc(t *testing.T) {
go con.Run()
time.Sleep(10 * time.Second)
time.Sleep(30 * time.Second)
con.SendPrivmsg("#freedom", "\x0356 FUCK YALL BITCHES")
for _, v := range con.userList["#freedom"] {
con.SendPrivmsg("#freedom", fmt.Sprintf("yo %s fuck you", v))
}
}
func TestParser(t *testing.T) {