// tell func tell(ctx *bot.Context) { // s == <target> <stuff> txt := ctx.Text() idx := strings.Index(txt, " ") if idx == -1 { ctx.ReplyN("Tell who what?") return } tell := txt[idx+1:] n, c := ctx.Storable() t := bot.Nick(txt[:idx]) if t.Lower() == strings.ToLower(ctx.Nick) || t.Lower() == "me" { ctx.ReplyN("You're a dick. Oh, wait, that wasn't *quite* it...") return } r := reminders.NewTell(tell, t, n, c) if err := rc.Insert(r); err != nil { ctx.ReplyN("Error saving tell: %v", err) return } if s := pc.GetByNick(txt[:idx]); s.CanPush() { push.Push(s, fmt.Sprintf("%s in %s asked me to tell you:", ctx.Nick, ctx.Target()), tell) } // Any previously-generated list of reminders is now obsolete. delete(listed, ctx.Nick) ctx.ReplyN("%s", r.Acknowledge()) }
func githubWatcher(ctx *bot.Context) { // Watch #sp0rklf for IRC messages about issues coming from github. if ctx.Nick != "fluffle\\sp0rkle" || ctx.Target() != "#sp0rklf" || !strings.Contains(ctx.Text(), "issue #") { return } text := util.RemoveColours(ctx.Text()) // srsly github why colours :( l := &util.Lexer{Input: text} l.Find(' ') text = text[l.Pos()+1:] l.Find('#') l.Next() issue := int(l.Number()) labels, _, err := gh.Issues.ListLabelsByIssue( githubUser, githubRepo, issue, &github.ListOptions{}) if err != nil { logging.Error("Error getting labels for issue %d: %v", issue, err) return } for _, l := range labels { kv := strings.Split(*l.Name, ":") if len(kv) == 2 && kv[0] == "nick" { logging.Debug("Recording tell for %s about issue %d.", kv[1], issue) r := reminders.NewTell("that "+text, bot.Nick(kv[1]), "github", "") if err := rc.Insert(r); err != nil { logging.Error("Error inserting github tell: %v", err) } } } }
func tellCheck(ctx *bot.Context) { nick := ctx.Nick if ctx.Cmd == client.NICK { // We want the destination nick, not the source. nick = ctx.Target() } r := rc.TellsFor(nick) for i := range r { if ctx.Cmd == client.NICK { if r[i].Chan != "" { ctx.Privmsg(string(r[i].Chan), nick+": "+r[i].Reply()) } ctx.Reply("%s", r[i].Reply()) } else { ctx.Privmsg(ctx.Nick, r[i].Reply()) if r[i].Chan != "" { ctx.ReplyN("%s", r[i].Reply()) } } rc.RemoveId(r[i].Id) } if len(r) > 0 { delete(listed, ctx.Nick) } }
func githubCreateIssue(ctx *bot.Context) { s := strings.SplitN(ctx.Text(), ". ", 2) if s[0] == "" { ctx.ReplyN("I'm not going to create an empty issue.") return } req := &github.IssueRequest{ Title: &s[0], Labels: &[]string{ "from:IRC", "nick:" + ctx.Nick, "chan:" + ctx.Target(), }, } if len(s) == 2 { req.Body = &s[1] } issue, _, err := gh.Issues.Create(githubUser, githubRepo, req) if err != nil { ctx.ReplyN("Error creating issue: %v", err) return } ctx.ReplyN("Issue #%d created at %s/%d", *issue.Number, githubIssuesURL, *issue.Number) }
func urlScan(ctx *bot.Context) { words := strings.Split(ctx.Text(), " ") n, c := ctx.Storable() for _, w := range words { if util.LooksURLish(w) { if u := uc.GetByUrl(w); u != nil { if u.Nick != bot.Nick(ctx.Nick) && time.Since(u.Timestamp) > 2*time.Hour { ctx.Reply("that URL first mentioned by %s %s ago", u.Nick, util.TimeSince(u.Timestamp)) } continue } u := urls.NewUrl(w, n, c) if len(w) > autoShortenLimit && ctx.Public() { u.Shortened = Encode(w) } if err := uc.Insert(u); err != nil { ctx.ReplyN("Couldn't insert url '%s': %s", w, err) continue } if u.Shortened != "" { ctx.Reply("%s's URL shortened as %s%s%s", ctx.Nick, bot.HttpHost(), shortenPath, u.Shortened) } lastseen[ctx.Target()] = u.Id } } }
// Factoid add: 'key := value' or 'key :is value' func insert(ctx *bot.Context) { if !ctx.Addressed || !util.IsFactoidAddition(ctx.Text()) { return } var key, val string if strings.Index(ctx.Text(), ":=") != -1 { kv := strings.SplitN(ctx.Text(), ":=", 2) key = ToKey(kv[0], false) val = strings.TrimSpace(kv[1]) } else { // we use :is to add val = "key is val" kv := strings.SplitN(ctx.Text(), ":is", 2) key = ToKey(kv[0], false) val = strings.Join([]string{strings.TrimSpace(kv[0]), "is", strings.TrimSpace(kv[1])}, " ") } n, c := ctx.Storable() fact := factoids.NewFactoid(key, val, n, c) // The "randomwoot" factoid contains random positive phrases for success. joy := "Woo" if rand := fc.GetPseudoRand("randomwoot"); rand != nil { joy = rand.Value } if err := fc.Insert(fact); err == nil { count := fc.GetCount(key) LastSeen(ctx.Target(), fact.Id) ctx.ReplyN("%s, I now know %d things about '%s'.", joy, count, key) } else { ctx.ReplyN("Error storing factoid: %s.", err) } }
func topten(ctx *bot.Context) { top := sc.TopTen(ctx.Target()) s := make([]string, 0, 10) for i, n := range top { s = append(s, fmt.Sprintf("#%d: %s - %d", i+1, n.Nick, n.Lines)) } ctx.Reply("%s", strings.Join(s, ", ")) }
func recordNick(ctx *bot.Context) { sn := seenNickFromLine(ctx) sn.Chan = "" sn.Text = ctx.Target() if _, err := sc.Upsert(sn.Id(), sn); err != nil { // We don't have anyone to reply to in this case, so log instead. logging.Warn("Failed to store seen data: %v", err) } }
// Split this out so we can inject a deterministic time for testing. func id_replacer(val string, ctx *bot.Context, ts time.Time) string { val = strings.Replace(val, "$nick", ctx.Nick, -1) val = strings.Replace(val, "$chan", ctx.Target(), -1) val = strings.Replace(val, "$username", ctx.Ident, -1) val = strings.Replace(val, "$user", ctx.Ident, -1) val = strings.Replace(val, "$host", ctx.Host, -1) val = strings.Replace(val, "$date", datetime.Format(ts), -1) val = strings.Replace(val, "$time", datetime.Format(ts, "15:04:05"), -1) return val }
func statsCmd(ctx *bot.Context) { n := ctx.Nick if len(ctx.Text()) > 0 { n = ctx.Text() } ns := sc.StatsFor(n, ctx.Target()) if ns != nil { ctx.ReplyN("%s", ns) } }
// Factoid chance: 'chance of that is' => sets chance of lastSeen[chan] func chance(ctx *bot.Context) { str := ctx.Text() var chance float64 if strings.HasSuffix(str, "%") { // Handle 'chance of that is \d+%' if i, err := strconv.Atoi(str[:len(str)-1]); err != nil { ctx.ReplyN("'%s' didn't look like a % chance to me.", str) return } else { chance = float64(i) / 100 } } else { // Assume the chance is a floating point number. if c, err := strconv.ParseFloat(str, 64); err != nil { ctx.ReplyN("'%s' didn't look like a chance to me.", str) return } else { chance = c } } // Make sure the chance we've parsed lies in (0.0,1.0] if chance > 1.0 || chance <= 0.0 { ctx.ReplyN("'%s' was outside possible chance ranges.", str) return } // Retrieve last seen ObjectId, replace with "" ls := LastSeen(ctx.Target(), "") // ok, we're good to update the chance. if fact := fc.GetById(ls); fact != nil { // Store the old chance, update with the new old := fact.Chance fact.Chance = chance // Update the Modified field fact.Modify(ctx.Storable()) // And store the new factoid data if err := fc.Update(bson.M{"_id": ls}, fact); err == nil { ctx.ReplyN("'%s' was at %.0f%% chance, now is at %.0f%%.", fact.Key, old*100, chance*100) } else { ctx.ReplyN("I failed to replace '%s': %s", fact.Key, err) } } else { ctx.ReplyN("Whatever that was, I've already forgotten it.") } }
// Factoid delete: 'forget|delete that' => deletes lastSeen[chan] func forget(ctx *bot.Context) { // Get fresh state on the last seen factoid. ls := LastSeen(ctx.Target(), "") if fact := fc.GetById(ls); fact != nil { if err := fc.Remove(bson.M{"_id": ls}); err == nil { ctx.ReplyN("I forgot that '%s' was '%s'.", fact.Key, fact.Value) } else { ctx.ReplyN("I failed to forget '%s': %s", fact.Key, err) } } else { ctx.ReplyN("Whatever that was, I've already forgotten it.") } }
func recordStats(ctx *bot.Context) { ns := sc.StatsFor(ctx.Nick, ctx.Target()) if ns == nil { n, c := ctx.Storable() ns = stats.NewStat(n, c) } ns.Update(ctx.Text()) if ns.Lines%10000 == 0 { ctx.Reply("%s has said %d lines in this channel and "+ "should now shut the f**k up and do something useful", ctx.Nick, ns.Lines) } if _, err := sc.Upsert(ns.Id(), ns); err != nil { ctx.Reply("Failed to store stats data: %v", err) } }
func lookup(ctx *bot.Context) { // Only perform extra prefix removal if we weren't addressed directly key := ToKey(ctx.Text(), !ctx.Addressed) var fact *factoids.Factoid if fact = fc.GetPseudoRand(key); fact == nil && ctx.Cmd == client.ACTION { // Support sp0rkle's habit of stripping off it's own nick // but only for actions, not privmsgs. if strings.HasSuffix(key, ctx.Me()) { key = strings.TrimSpace(key[:len(key)-len(ctx.Me())]) fact = fc.GetPseudoRand(key) } } if fact == nil { return } // Chance is used to limit the rate of factoid replies for things // people say a lot, like smilies, or 'lol', or 'i love the peen'. chance := fact.Chance if key == "" { // This is doing a "random" lookup, triggered by someone typing in // something entirely composed of the chars stripped by ToKey(). // To avoid making this too spammy, forcibly limit the chance to 40%. chance = 0.4 } if rand.Float64() < chance { // Store this as the last seen factoid LastSeen(ctx.Target(), fact.Id) // Update the Accessed field // TODO(fluffle): fd should take care of updating Accessed internally fact.Access(ctx.Storable()) // And store the new factoid data if err := fc.Update(bson.M{"_id": fact.Id}, fact); err != nil { ctx.ReplyN("I failed to update '%s' (%s): %s ", fact.Key, fact.Id, err) } recurse(fact, map[string]bool{key: true}) switch fact.Type { case factoids.F_ACTION: ctx.Do("%s", fact.Value) default: ctx.Reply("%s", fact.Value) } } }
func cache(ctx *bot.Context) { var u *urls.Url if ctx.Text() == "" { // assume we have been given "cache that" if u = uc.GetById(lastseen[ctx.Target()]); u == nil { ctx.ReplyN("I seem to have forgotten what to cache") return } if u.CachedAs != "" { ctx.ReplyN("That was already cached as %s%s%s at %s", bot.HttpHost(), cachePath, u.CachedAs, datetime.Format(u.CacheTime)) return } } else { url := strings.TrimSpace(ctx.Text()) if idx := strings.Index(url, " "); idx != -1 { url = url[:idx] } if !util.LooksURLish(url) { ctx.ReplyN("'%s' doesn't look URLish", url) return } if u = uc.GetByUrl(url); u == nil { n, c := ctx.Storable() u = urls.NewUrl(url, n, c) } else if u.CachedAs != "" { ctx.ReplyN("That was already cached as %s%s%s at %s", bot.HttpHost(), cachePath, u.CachedAs, datetime.Format(u.CacheTime)) return } } if err := Cache(u); err != nil { ctx.ReplyN("Failed to store cached url: %s", err) return } ctx.ReplyN("%s cached as %s%s%s", u.Url, bot.HttpHost(), cachePath, u.CachedAs) }
func shorten(ctx *bot.Context) { var u *urls.Url if ctx.Text() == "" { // assume we have been given "shorten that" if u = uc.GetById(lastseen[ctx.Target()]); u == nil { ctx.ReplyN("I seem to have forgotten what to shorten") return } if u.Shortened != "" { ctx.ReplyN("That was already shortened as %s%s%s", bot.HttpHost(), shortenPath, u.Shortened) return } } else { url := strings.TrimSpace(ctx.Text()) if idx := strings.Index(url, " "); idx != -1 { url = url[:idx] } if !util.LooksURLish(url) { ctx.ReplyN("'%s' doesn't look URLish", url) return } if u = uc.GetByUrl(url); u == nil { n, c := ctx.Storable() u = urls.NewUrl(url, n, c) } else if u.Shortened != "" { ctx.ReplyN("That was already shortened as %s%s%s", bot.HttpHost(), shortenPath, u.Shortened) return } } if err := Shorten(u); err != nil { ctx.ReplyN("Failed to store shortened url: %s", err) return } ctx.ReplyN("%s shortened to %s%s%s", u.Url, bot.HttpHost(), shortenPath, u.Shortened) }
// Factoid replace: 'replace that with' => updates lastSeen[chan] func replace(ctx *bot.Context) { ls := LastSeen(ctx.Target(), "") if fact := fc.GetById(ls); fact != nil { // Store the old factoid value old := fact.Value // Replace the value with the new one fact.Value = ctx.Text() // Update the Modified field fact.Modify(ctx.Storable()) // And store the new factoid data if err := fc.Update(bson.M{"_id": ls}, fact); err == nil { ctx.ReplyN("'%s' was '%s', now is '%s'.", fact.Key, old, fact.Value) } else { ctx.ReplyN("I failed to replace '%s': %s", fact.Key, err) } } else { ctx.ReplyN("Whatever that was, I've already forgotten it.") } }
// Factoid literal: 'literal key' => info about factoid func literal(ctx *bot.Context) { key := ToKey(ctx.Text(), false) if count := fc.GetCount(key); count == 0 { ctx.ReplyN("I don't know anything about '%s'.", key) return } else if count > 10 && ctx.Public() { ctx.ReplyN("I know too much about '%s', ask me privately.", key) return } if facts := fc.GetAll(key); facts != nil { for _, fact := range facts { // Use Privmsg directly here so that the results aren't output // via the plugin system and contain the literal data. ctx.Privmsg(ctx.Target(), fmt.Sprintf( "[%3.0f%%] %s", fact.Chance*100, fact.Value)) } } else { ctx.ReplyN("Something literally went wrong :-(") } }
// Factoid edit: that =~ s/<regex>/<replacement>/ func edit(ctx *bot.Context) { // extract regexp and replacement l := &util.Lexer{Input: ctx.Text()} if l.Next() != "s" { ctx.ReplyN("It's 'that =~ s/<regex>/<replacement>/', fool.") return } delim := l.Peek() // Identify delimiting character l.Next() // Skip past that delimiter re := extractRx(l, delim) // Extract regex from string l.Next() // Skip past next delimiter rp := extractRx(l, delim) // Extract replacement from string if l.Next() != string(delim) { ctx.ReplyN("Couldn't parse regex: re='%s', rp='%s'.", re, rp) return } rx, err := regexp.Compile(re) if err != nil { ctx.ReplyN("Couldn't compile regex '%s': %s", re, err) return } // Retrieve last seen ObjectId, replace with "" ls := LastSeen(ctx.Target(), "") fact := fc.GetById(ls) if fact == nil { ctx.ReplyN("I've forgotten what we were talking about, sorry!") return } old := fact.Value fact.Value = rx.ReplaceAllString(old, rp) fact.Modify(ctx.Storable()) if err := fc.UpdateId(ls, fact); err == nil { ctx.ReplyN("'%s' was '%s', is now '%s'.", fact.Key, old, fact.Value) } else { ctx.ReplyN("I failed to replace '%s': %s", fact.Key, err) } }
func githubUpdateIssue(ctx *bot.Context) { l := &util.Lexer{Input: ctx.Text()} issue := int(l.Number()) if issue == 0 { ctx.ReplyN("Not sure what issue you're talking about?") return } text := strings.TrimSpace(l.Find(0)) if text == "" { ctx.ReplyN("Don't you have anything to say?") return } text = fmt.Sprintf("<%s/%s> %s", ctx.Nick, ctx.Target(), text) comm, _, err := gh.Issues.CreateComment( githubUser, githubRepo, issue, &github.IssueComment{Body: &text}) if err != nil { ctx.ReplyN("Error creating issue comment: %v", err) return } ctx.ReplyN("Created comment %s/%d#issuecomment-%d", githubIssuesURL, issue, *comm.ID) }