new nicklist shit
This commit is contained in:
parent
24590994f2
commit
dbfd78d4e1
55
irc.go
55
irc.go
@ -8,6 +8,12 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
//IrcVersion exports the current version of the irc lib
|
||||||
|
IrcVersion = "0.1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Connection holds the callbacks for the client
|
//Connection holds the callbacks for the client
|
||||||
@ -123,14 +129,48 @@ func hasNick(nick string, names []string) bool {
|
|||||||
return false
|
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) {
|
func (c *Connection) updateNicks(channel string, names []string) {
|
||||||
log.Printf("updating channel %s with %d nicks\n", channel, len(names))
|
log.Printf("updating channel %s with %d nicks\n", channel, len(names))
|
||||||
for _, i := range names {
|
for _, i := range names {
|
||||||
if !hasNick(i, c.userList[channel]) {
|
c.addNick(channel, i)
|
||||||
c.userList[channel] = append(c.userList[channel], i)
|
|
||||||
log.Printf("added nick: %s to channel %s\n", i, channel)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: simplify this to pass gocyclo
|
//TODO: simplify this to pass gocyclo
|
||||||
@ -154,10 +194,10 @@ func (c *Connection) parseMessage(line string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cmd == "353" {
|
if cmd == "353" {
|
||||||
idx := strings.Index(args, "=")
|
idx := strings.Index(args, "=") + 2
|
||||||
idx += 2
|
|
||||||
s := strings.Split(args[idx:], ":")
|
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 {
|
if c.NumericCallback != nil {
|
||||||
@ -184,14 +224,17 @@ func (c *Connection) parseMessage(line string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "join":
|
case "join":
|
||||||
|
c.addNick(target, GetNick(from))
|
||||||
if c.JoinCallback != nil {
|
if c.JoinCallback != nil {
|
||||||
c.JoinCallback(from, target)
|
c.JoinCallback(from, target)
|
||||||
}
|
}
|
||||||
case "quit":
|
case "quit":
|
||||||
|
c.removeNickAllChans(GetNick(from))
|
||||||
if c.QuitCallback != nil {
|
if c.QuitCallback != nil {
|
||||||
c.QuitCallback(from, msg)
|
c.QuitCallback(from, msg)
|
||||||
}
|
}
|
||||||
case "part":
|
case "part":
|
||||||
|
c.removeNick(target, GetNick(from))
|
||||||
if c.PartCallback != nil {
|
if c.PartCallback != nil {
|
||||||
c.PartCallback(from, target, msg)
|
c.PartCallback(from, target, msg)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package irc
|
package irc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -11,9 +12,13 @@ func TestSimpleIrc(t *testing.T) {
|
|||||||
|
|
||||||
go con.Run()
|
go con.Run()
|
||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(30 * time.Second)
|
||||||
|
|
||||||
con.SendPrivmsg("#freedom", "\x0356 FUCK YALL BITCHES")
|
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) {
|
func TestParser(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user