-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Nick tracker #79
base: master
Are you sure you want to change the base?
Add Nick tracker #79
Changes from all commits
86d5996
c1a39c1
bf21258
d6d3f2e
a875f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/thoja/go-ircevent" | ||
"sort" | ||
"time" | ||
) | ||
|
||
const channel = "#ggnet" | ||
const serverssl = "irc.homelien.no:6667" | ||
|
||
func main() { | ||
ircnick1 := "blatibalt1" | ||
irccon := irc.IRC(ircnick1, "blatiblat") | ||
irccon.VerboseCallbackHandler = true | ||
irccon.Debug = true | ||
irccon.AddCallback("001", func(e *irc.Event) { irccon.Join(channel) }) | ||
irccon.AddCallback("366", func(e *irc.Event) {}) | ||
irccon.SetupNickTrack() | ||
err := irccon.Connect(serverssl) | ||
if err != nil { | ||
fmt.Printf("Err %s", err) | ||
return | ||
} | ||
go func() { | ||
t := time.NewTicker(30 * time.Second) | ||
for { | ||
<-t.C | ||
var keys []string | ||
if _, ok := irccon.Channels[channel]; ok == true { | ||
for k, _ := range irccon.Channels[channel].Users { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
fmt.Printf("%d: ", len(keys)) | ||
for _, k := range keys { | ||
fmt.Printf("(%s)%s ", irccon.Channels[channel].Users[k].Mode, k) | ||
} | ||
fmt.Printf("\n") | ||
} | ||
} | ||
}() | ||
irccon.Loop() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package irc | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
//Struct to store Channel Info | ||
type Channel struct { | ||
Topic string | ||
Mode string | ||
Users map[string]User | ||
} | ||
|
||
type User struct { | ||
Host string | ||
Mode string | ||
} | ||
|
||
var mode_split = regexp.MustCompile("([%@+]{0,1})(.+)") //Half-Op, //Op, //Voice | ||
|
||
func (irc *Connection) SetupNickTrack() { | ||
// 353: RPL_NAMEREPLY per RFC1459 | ||
// will typically receive this on channel joins and when NAMES is | ||
// called via GetNicksOnCHan | ||
irc.AddCallback("353", func(e *Event) { | ||
// get chan | ||
channelName := e.Arguments[2] | ||
// check if chan exists in map | ||
_, ok := irc.Channels[channelName] | ||
|
||
// if not make one | ||
if ok != true { | ||
irc.Channels[channelName] = Channel{Users: make(map[string]User)} | ||
} | ||
// split the datat into a slice | ||
for _, modenick := range strings.Split(e.Message(), " ") { | ||
nickandmode := mode_split.FindStringSubmatch(modenick) | ||
u := User{} | ||
if len(nickandmode) == 3 { | ||
if nickandmode[1] == "@" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned above, these should probably be pulled from isupport, if possible, rather than hard coded. |
||
u.Mode = "+o" // Ooof should be mode struct? | ||
} else if nickandmode[1] == "+" { | ||
u.Mode = "+v" // Ooof should be mode struct? | ||
} else if nickandmode[1] == "%" { | ||
u.Mode = "+h" | ||
} | ||
irc.Channels[channelName].Users[nickandmode[2]] = u | ||
} else { | ||
irc.Channels[channelName].Users[modenick] = u | ||
} | ||
} | ||
}) | ||
|
||
irc.AddCallback("MODE", func(e *Event) { | ||
channelName := e.Arguments[0] | ||
if len(e.Arguments) == 3 { // 3 == for channel 2 == for user on server | ||
if _, ok := irc.Channels[channelName]; ok != true { | ||
irc.Channels[channelName] = Channel{Users: make(map[string]User)} | ||
} | ||
if _, ok := irc.Channels[channelName].Users[e.Arguments[2]]; ok != true { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only seems to handle setting all modes, however Additionally, this will probably end up being the most complicated portion. Either IRCv3 multi-prefix needs to be enabled (and handled) or you will need to query for the user's modes in a channel using |
||
irc.Channels[channelName].Users[e.Arguments[2]] = User{Mode: e.Arguments[1]} | ||
} else { | ||
u := irc.Channels[channelName].Users[e.Arguments[2]] | ||
u.Mode = e.Arguments[1] | ||
irc.Channels[channelName].Users[e.Arguments[2]] = u | ||
} | ||
} | ||
}) | ||
|
||
//Really hacky since the message from the server does not include the channel | ||
irc.AddCallback("NICK", func(e *Event) { | ||
if len(e.Arguments) == 1 { // Sanity check | ||
for k, _ := range irc.Channels { | ||
if _, ok := irc.Channels[k].Users[e.Nick]; ok { | ||
u := irc.Channels[k].Users[e.Nick] | ||
u.Host = e.Host | ||
irc.Channels[k].Users[e.Arguments[0]] = u //New nick | ||
delete(irc.Channels[k].Users, e.Nick) //Delete old | ||
} | ||
} | ||
} | ||
}) | ||
|
||
irc.AddCallback("JOIN", func(e *Event) { | ||
channelName := e.Arguments[0] | ||
if _, ok := irc.Channels[channelName]; ok != true { | ||
irc.Channels[channelName] = Channel{Users: make(map[string]User)} | ||
} | ||
irc.Channels[channelName].Users[e.Nick] = User{Host: e.Source} | ||
}) | ||
|
||
irc.AddCallback("PART", func(e *Event) { | ||
channelName := e.Arguments[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the bot leaves a channel, the whole channel should be removed as it won't be tracked any more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try and work on this, it's something that will probably break things for me. |
||
delete(irc.Channels[channelName].Users, e.Nick) | ||
}) | ||
|
||
irc.AddCallback("QUIT", func(e *Event) { | ||
for k, _ := range irc.Channels { | ||
delete(irc.Channels[k].Users, e.Nick) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fairly limited and will break on any servers that support other modes (I've seen & used for admin). This should be pulled from the
PREFIX
isupport value which comes in the format like this:(qaohv)~&@%+