// 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 (n *Nick) String() string { if act, ok := actionMap[n.Action]; ok { return fmt.Sprintf("I last saw %s on %s (%s ago), %s.", n.Nick, datetime.Format(n.Timestamp), util.TimeSince(n.Timestamp), act(n)) } // No specific message format for the action seen. return fmt.Sprintf("I last saw %s at %s (%s ago).", n.Nick, datetime.Format(n.Timestamp), util.TimeSince(n.Timestamp)) }
func (k *Karma) String() string { s := fmt.Sprintf("'%s' has a karma of %d after %d votes.", k.Subject, k.Score, k.Votes) if k.Upvoter != "" { s += fmt.Sprintf(" Last upvoted by %s at %s.", k.Upvoter, datetime.Format(k.Upvtime)) } if k.Downvoter != "" { s += fmt.Sprintf(" Last downvoted by %s at %s.", k.Downvoter, datetime.Format(k.Downvtime)) } return s }
// Factoid info: 'fact info key' => some information about key func info(ctx *bot.Context) { key := ToKey(ctx.Text(), false) count := fc.GetCount(key) if count == 0 { ctx.ReplyN("I don't know anything about '%s'.", key) return } msgs := make([]string, 0, 10) if key == "" { msgs = append(msgs, fmt.Sprintf("In total, I know %d things.", count)) } else { msgs = append(msgs, fmt.Sprintf("I know %d things about '%s'.", count, key)) } if created := fc.GetLast("created", key); created != nil { c := created.Created msgs = append(msgs, "A factoid") if key != "" { msgs = append(msgs, fmt.Sprintf("for '%s'", key)) } msgs = append(msgs, fmt.Sprintf("was last created on %s by %s,", datetime.Format(c.Timestamp), c.Nick)) } if modified := fc.GetLast("modified", key); modified != nil { m := modified.Modified msgs = append(msgs, fmt.Sprintf("modified on %s by %s,", datetime.Format(m.Timestamp), m.Nick)) } if accessed := fc.GetLast("accessed", key); accessed != nil { a := accessed.Accessed msgs = append(msgs, fmt.Sprintf("and accessed on %s by %s.", datetime.Format(a.Timestamp), a.Nick)) } if info := fc.InfoMR(key); info != nil { if key == "" { msgs = append(msgs, "These factoids have") } else { msgs = append(msgs, fmt.Sprintf("'%s' has", key)) } msgs = append(msgs, fmt.Sprintf( "been modified %d times and accessed %d times.", info.Modified, info.Accessed)) } ctx.ReplyN("%s", strings.Join(msgs, " ")) }
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 date(ctx *bot.Context) { tstr, zone := ctx.Text(), "" if idx := strings.Index(tstr, "in "); idx != -1 { tstr, zone = tstr[:idx], strings.TrimSpace(tstr[idx+3:]) } tm, ok := time.Now(), true if tstr != "" { if tm, ok = datetime.Parse(tstr); !ok { ctx.ReplyN("Couldn't parse time string '%s'.", tstr) return } } if loc := datetime.Zone(zone); zone != "" && loc != nil { tm = tm.In(loc) ctx.ReplyN("%s", tm.Format(datetime.TimeFormat)) } else { ctx.ReplyN("%s", datetime.Format(tm)) } }
func urbanDictionary(ctx *bot.Context) { entry, ok, err := cache.fetch(strings.ToLower(ctx.Text())) if err != nil { ctx.ReplyN("ud request failed: %s", err) return } cached, r := "", entry.result if ok { cached = fmt.Sprintf(", result cached at %s", datetime.Format(entry.stamp)) } if r.Total == 0 || r.Type == "no_results" { ctx.ReplyN("%s isn't defined yet%s.", ctx.Text(), cached) return } // Cycle through all the definitions on repeated calls for the same term r.Pages = (r.Pages + 1) % r.Total def := r.List[r.Pages] ctx.Reply("[%d/%d] %s (%d up, %d down%s)", r.Pages+1, r.Total, strings.Replace(def.Definition, "\r\n", " ", -1), def.Upvotes, def.Downvotes, cached) }
func (r *Reminder) At() string { return datetime.Format(r.RemindAt) }