示例#1
0
文件: handlers.go 项目: pzsz/sp0rkle
func recordKick(line *base.Line) {
	n, c := line.Storable()
	kn := base.Nick(line.Args[1])
	// seenNickFromLine doesn't work with the hacks for KICKING and KICKED
	// First, handle KICKING
	kr := sc.LastSeenDoing(line.Nick, "KICKING")
	if kr == nil {
		kr = seen.SawNick(n, c, "KICKING", line.Args[2])
	} else {
		kr.Nick, kr.Chan = n, c
		kr.Timestamp, kr.Text = time.Now(), line.Args[2]
	}
	kr.OtherNick = kn
	_, err := sc.Upsert(kr.Id(), kr)
	if err != nil {
		bot.Reply(line, "Failed to store seen data: %v", err)
	}
	// Now, handle KICKED
	ke := sc.LastSeenDoing(line.Args[1], "KICKED")
	if ke == nil {
		ke = seen.SawNick(kn, c, "KICKED", line.Args[2])
	} else {
		ke.Nick, ke.Chan = kn, c
		ke.Timestamp, ke.Text = time.Now(), line.Args[2]
	}
	ke.OtherNick = n
	_, err = sc.Upsert(ke.Id(), ke)
	if err != nil {
		bot.Reply(line, "Failed to store seen data: %v", err)
	}
}
示例#2
0
文件: handlers.go 项目: pzsz/sp0rkle
// Factoid add: 'key := value' or 'key :is value'
func insert(line *base.Line) {
	if !line.Addressed || !util.IsFactoidAddition(line.Args[1]) {
		return
	}

	var key, val string
	if strings.Index(line.Args[1], ":=") != -1 {
		kv := strings.SplitN(line.Args[1], ":=", 2)
		key = ToKey(kv[0], false)
		val = strings.TrimSpace(kv[1])
	} else {
		// we use :is to add val = "key is val"
		kv := strings.SplitN(line.Args[1], ":is", 2)
		key = ToKey(kv[0], false)
		val = strings.Join([]string{strings.TrimSpace(kv[0]),
			"is", strings.TrimSpace(kv[1])}, " ")
	}
	n, c := line.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)
		bot.ReplyN(line, "%s, I now know %d things about '%s'.", joy, count, key)
	} else {
		bot.ReplyN(line, "Error storing factoid: %s.", err)
	}
}
示例#3
0
文件: handlers.go 项目: pzsz/sp0rkle
func urlScan(line *base.Line) {
	words := strings.Split(line.Args[1], " ")
	n, c := line.Storable()
	for _, w := range words {
		if util.LooksURLish(w) {
			if u := uc.GetByUrl(w); u != nil {
				bot.Reply(line, "%s first mentioned by %s at %s",
					w, u.Nick, u.Timestamp.Format(time.RFC1123))
				continue
			}
			u := urls.NewUrl(w, n, c)
			if len(w) > autoShortenLimit {
				u.Shortened = Encode(w)
			}
			if err := uc.Insert(u); err != nil {
				bot.ReplyN(line, "Couldn't insert url '%s': %s", w, err)
				continue
			}
			if u.Shortened != "" {
				bot.Reply(line, "%s's URL shortened as %s%s%s",
					line.Nick, bot.HttpHost(), shortenPath, u.Shortened)
			}
			lastseen[line.Args[0]] = u.Id
		}
	}
}
示例#4
0
文件: commands.go 项目: pzsz/sp0rkle
// remind
func set(line *base.Line) {
	// s == <target> <reminder> in|at|on <time>
	s := strings.Fields(line.Args[1])
	if len(s) < 4 {
		bot.ReplyN(line, "Invalid remind syntax. Sucka.")
		return
	}
	i := len(s) - 1
	for i > 0 {
		lc := strings.ToLower(s[i])
		if lc == "in" || lc == "at" || lc == "on" {
			break
		}
		i--
	}
	if i < 1 {
		bot.ReplyN(line, "Invalid remind syntax. Sucka.")
		return
	}
	reminder := strings.Join(s[1:i], " ")
	timestr := strings.ToLower(strings.Join(s[i+1:], " "))
	// TODO(fluffle): surface better errors from datetime.Parse
	at, ok := datetime.Parse(timestr)
	if !ok {
		bot.ReplyN(line, "Couldn't parse time string '%s'", timestr)
		return
	}
	now := time.Now()
	start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
	if at.Before(now) && at.After(start) {
		// Perform some basic hacky corrections before giving up
		if strings.Contains(timestr, "am") || strings.Contains(timestr, "pm") {
			at = at.Add(24 * time.Hour)
		} else {
			at = at.Add(12 * time.Hour)
		}
	}
	if at.Before(now) {
		bot.ReplyN(line, "Time '%s' is in the past.", timestr)
		return
	}
	n, c := line.Storable()
	// TODO(fluffle): Use state tracking! And do this better.
	t := base.Nick(s[0])
	if t.Lower() == strings.ToLower(line.Nick) ||
		t.Lower() == "me" {
		t = n
	}
	r := reminders.NewReminder(reminder, at, t, n, c)
	if err := rc.Insert(r); err != nil {
		bot.ReplyN(line, "Error saving reminder: %v", err)
		return
	}
	// Any previously-generated list of reminders is now obsolete.
	delete(listed, line.Nick)
	bot.ReplyN(line, "%s", r.Acknowledge())
	Remind(r)
}
示例#5
0
文件: commands.go 项目: pzsz/sp0rkle
func add(line *base.Line) {
	n, c := line.Storable()
	quote := quotes.NewQuote(line.Args[1], n, c)
	quote.QID = qc.NewQID()
	if err := qc.Insert(quote); err == nil {
		bot.ReplyN(line, "Quote added succesfully, id #%d.", quote.QID)
	} else {
		bot.ReplyN(line, "Error adding quote: %s.", err)
	}
}
示例#6
0
// Look up or create a "seen" entry for the line.
// Explicitly don't handle updating line.Text or line.OtherNick
func seenNickFromLine(line *base.Line) *seen.Nick {
	sn := sc.LastSeenDoing(line.Nick, line.Cmd)
	n, c := line.Storable()
	if sn == nil {
		sn = seen.SawNick(n, c, line.Cmd, "")
	} else {
		sn.Nick, sn.Chan = n, c
		sn.Timestamp = time.Now()
	}
	return sn
}
示例#7
0
文件: commands.go 项目: pzsz/sp0rkle
// Factoid chance: 'chance of that is' => sets chance of lastSeen[chan]
func chance(line *base.Line) {
	str := line.Args[1]
	var chance float64

	if strings.HasSuffix(str, "%") {
		// Handle 'chance of that is \d+%'
		if i, err := strconv.Atoi(str[:len(str)-1]); err != nil {
			bot.ReplyN(line, "'%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 {
			bot.ReplyN(line, "'%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 {
		bot.ReplyN(line, "'%s' was outside possible chance ranges.", str)
		return
	}

	// Retrieve last seen ObjectId, replace with ""
	ls := LastSeen(line.Args[0], "")
	// 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(line.Storable())
		// And store the new factoid data
		if err := fc.Update(bson.M{"_id": ls}, fact); err == nil {
			bot.ReplyN(line, "'%s' was at %.0f%% chance, now is at %.0f%%.",
				fact.Key, old*100, chance*100)
		} else {
			bot.ReplyN(line, "I failed to replace '%s': %s", fact.Key, err)
		}
	} else {
		bot.ReplyN(line, "Whatever that was, I've already forgotten it.")
	}
}
示例#8
0
文件: handlers.go 项目: pzsz/sp0rkle
func lookup(line *base.Line) {
	// Only perform extra prefix removal if we weren't addressed directly
	key := ToKey(line.Args[1], !line.Addressed)
	var fact *factoids.Factoid

	if fact = fc.GetPseudoRand(key); fact == nil && line.Cmd == "ACTION" {
		// Support sp0rkle's habit of stripping off it's own nick
		// but only for actions, not privmsgs.
		if strings.HasSuffix(key, bot.Nick()) {
			key = strings.TrimSpace(key[:len(key)-len(bot.Nick())])
			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(line.Args[0], fact.Id)
		// Update the Accessed field
		// TODO(fluffle): fd should take care of updating Accessed internally
		fact.Access(line.Storable())
		// And store the new factoid data
		if err := fc.Update(bson.M{"_id": fact.Id}, fact); err != nil {
			bot.ReplyN(line, "I failed to update '%s' (%s): %s ",
				fact.Key, fact.Id, err)
		}
		keys := map[string]bool{key: true}
		val := recurse(fact.Value, keys)
		switch fact.Type {
		case factoids.F_ACTION:
			bot.Do(line, "%s", val)
		default:
			bot.Reply(line, "%s", val)
		}
	}
}
示例#9
0
文件: handlers.go 项目: pzsz/sp0rkle
func recordLines(line *base.Line) {
	sn := sc.LinesFor(line.Nick, line.Args[0])
	if sn == nil {
		n, c := line.Storable()
		sn = seen.SawNick(n, c, "LINES", "")
	}
	sn.Lines++
	for _, n := range milestones {
		if sn.Lines == n {
			bot.Reply(line, "%s has said %d lines in this channel and "+
				"should now shut the f**k up and do something useful",
				line.Nick, sn.Lines)
		}
	}
	if _, err := sc.Upsert(sn.Id(), sn); err != nil {
		bot.Reply(line, "Failed to store seen data: %v", err)
	}
}
示例#10
0
文件: handlers.go 项目: pzsz/sp0rkle
func smoke(line *base.Line) {
	if !smokeRx.MatchString(line.Args[1]) {
		return
	}
	sn := sc.LastSeenDoing(line.Nick, "SMOKE")
	n, c := line.Storable()
	if sn != nil {
		bot.ReplyN(line, "You last went for a smoke %s ago...",
			util.TimeSince(sn.Timestamp))
		sn.Nick, sn.Chan = n, c
		sn.Timestamp = time.Now()
	} else {
		sn = seen.SawNick(n, c, "SMOKE", "")
	}
	if _, err := sc.Upsert(sn.Id(), sn); err != nil {
		bot.Reply(line, "Failed to store smoke data: %v", err)
	}
}
示例#11
0
文件: handlers.go 项目: pzsz/sp0rkle
func recordKarma(line *base.Line) {
	// Karma can look like some.text.string++ or (text with spaces)--
	// and there could be multiple occurrences of it in a string.
	nick, _ := line.Storable()
	for _, kt := range karmaThings(line.Args[1]) {
		k := kc.KarmaFor(kt.thing)
		if k == nil {
			k = karma.New(kt.thing)
		}
		if kt.plus {
			k.Plus(nick)
		} else {
			k.Minus(nick)
		}
		if _, err := kc.Upsert(k.Id(), k); err != nil {
			bot.Reply(line, "Failed to insert Karma: %s", err)
		}
	}
}
示例#12
0
文件: commands.go 项目: pzsz/sp0rkle
// Factoid replace: 'replace that with' => updates lastSeen[chan]
func replace(line *base.Line) {
	ls := LastSeen(line.Args[0], "")
	if fact := fc.GetById(ls); fact != nil {
		// Store the old factoid value
		old := fact.Value
		// Replace the value with the new one
		fact.Value = line.Args[1]
		// Update the Modified field
		fact.Modify(line.Storable())
		// And store the new factoid data
		if err := fc.Update(bson.M{"_id": ls}, fact); err == nil {
			bot.ReplyN(line, "'%s' was '%s', now is '%s'.",
				fact.Key, old, fact.Value)
		} else {
			bot.ReplyN(line, "I failed to replace '%s': %s", fact.Key, err)
		}
	} else {
		bot.ReplyN(line, "Whatever that was, I've already forgotten it.")
	}
}
示例#13
0
文件: handlers.go 项目: pzsz/sp0rkle
func recordMarkov(line *base.Line) {
	nick, _ := line.Storable()
	sentence := line.Args[1]

	if !isStorable(line) {
		return
	}

	words := strings.Split(sentence, " ")
	output := make([]string, 0, len(words))
	for _, word := range words {
		clean_word := processWord(word)
		if len(clean_word) > 0 {
			output = append(output, clean_word)
		}
	}

	err := mc.AddSentence(output, "user:"******"%v", err)
}
示例#14
0
文件: commands.go 项目: pzsz/sp0rkle
func cache(line *base.Line) {
	var u *urls.Url
	if line.Args[1] == "" {
		// assume we have been given "cache that"
		if u = uc.GetById(lastseen[line.Args[0]]); u == nil {
			bot.ReplyN(line, "I seem to have forgotten what to cache")
			return
		}
		if u.CachedAs != "" {
			bot.ReplyN(line, "That was already cached as %s%s%s at %s",
				bot.HttpHost(), cachePath, u.CachedAs,
				u.CacheTime.Format(time.RFC1123))
			return
		}
	} else {
		url := strings.TrimSpace(line.Args[1])
		if idx := strings.Index(url, " "); idx != -1 {
			url = url[:idx]
		}
		if !util.LooksURLish(url) {
			bot.ReplyN(line, "'%s' doesn't look URLish", url)
			return
		}
		if u = uc.GetByUrl(url); u == nil {
			n, c := line.Storable()
			u = urls.NewUrl(url, n, c)
		} else if u.CachedAs != "" {
			bot.ReplyN(line, "That was already cached as %s%s%s at %s",
				bot.HttpHost(), cachePath, u.CachedAs,
				u.CacheTime.Format(time.RFC1123))
			return
		}
	}
	if err := Cache(u); err != nil {
		bot.ReplyN(line, "Failed to store cached url: %s", err)
		return
	}
	bot.ReplyN(line, "%s cached as %s%s%s",
		u.Url, bot.HttpHost(), cachePath, u.CachedAs)
}
示例#15
0
文件: commands.go 项目: pzsz/sp0rkle
func shorten(line *base.Line) {
	var u *urls.Url
	if line.Args[1] == "" {
		// assume we have been given "shorten that"
		if u = uc.GetById(lastseen[line.Args[0]]); u == nil {
			bot.ReplyN(line, "I seem to have forgotten what to shorten")
			return
		}
		if u.Shortened != "" {
			bot.ReplyN(line, "That was already shortened as %s%s%s",
				bot.HttpHost(), shortenPath, u.Shortened)
			return
		}
	} else {
		url := strings.TrimSpace(line.Args[1])
		if idx := strings.Index(url, " "); idx != -1 {
			url = url[:idx]
		}
		if !util.LooksURLish(url) {
			bot.ReplyN(line, "'%s' doesn't look URLish", url)
			return
		}
		if u = uc.GetByUrl(url); u == nil {
			n, c := line.Storable()
			u = urls.NewUrl(url, n, c)
		} else if u.Shortened != "" {
			bot.ReplyN(line, "That was already shortened as %s%s%s",
				bot.HttpHost(), shortenPath, u.Shortened)
			return
		}
	}
	if err := Shorten(u); err != nil {
		bot.ReplyN(line, "Failed to store shortened url: %s", err)
		return
	}
	bot.ReplyN(line, "%s shortened to %s%s%s",
		u.Url, bot.HttpHost(), shortenPath, u.Shortened)
}
示例#16
0
文件: commands.go 项目: pzsz/sp0rkle
// tell
func tell(line *base.Line) {
	// s == <target> <stuff>
	idx := strings.Index(line.Args[1], " ")
	if idx == -1 {
		bot.ReplyN(line, "Tell who what?")
		return
	}
	tell := line.Args[1][idx+1:]
	n, c := line.Storable()
	t := base.Nick(line.Args[1][:idx])
	if t.Lower() == strings.ToLower(line.Nick) ||
		t.Lower() == "me" {
		bot.ReplyN(line, "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 {
		bot.ReplyN(line, "Error saving tell: %v", err)
		return
	}
	// Any previously-generated list of reminders is now obsolete.
	delete(listed, line.Nick)
	bot.ReplyN(line, "%s", r.Acknowledge())
}