コード例 #1
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func qd_privmsg(bot *bot.Sp0rkle, line *base.Line) {
	qd := bot.GetDriver(driverName).(*quoteDriver)

	if !line.Addressed {
		return
	}

	nl := line.Copy()
	switch {
	// Quote add: qadd | quote add | add quote
	case util.StripAnyPrefix(&nl.Args[1], []string{"quote add ", "qadd ", "add quote "}):
		qd_add(bot, qd, nl)
	// Quote delete: qdel | quote del | del quote  #?QID
	case util.StripAnyPrefix(&nl.Args[1], []string{"quote del ", "qdel ", "del quote "}):
		// Strip optional # before qid
		if nl.Args[1][0] == '#' {
			nl.Args[1] = nl.Args[1][1:]
		}
		qd_delete(bot, qd, nl)
	// Quote lookup: quote #QID
	case util.StripAnyPrefix(&nl.Args[1], []string{"quote #"}):
		qd_fetch(bot, qd, nl)
	// Quote lookup: quote | quote regex
	case strings.ToLower(nl.Args[1]) == "quote":
		nl.Args[1] = ""
		fallthrough
	// This needs to come after the other cases as it will strip just "quote "
	case util.StripAnyPrefix(&nl.Args[1], []string{"quote "}):
		qd_lookup(bot, qd, nl)
	}
}
コード例 #2
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func ud_scan(bot *bot.Sp0rkle, ud *urlDriver, line *base.Line) {
	words := strings.Split(line.Args[1], " ")
	n, c := line.Storable()
	for _, w := range words {
		if util.LooksURLish(w) {
			if u := ud.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 = ud.Encode(w)
			}
			if err := ud.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.Prefix, shortenPath, u.Shortened)
			}
			ud.lastseen[line.Args[0]] = u.Id
		}
	}
}
コード例 #3
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func sd_record_kick(bot *bot.Sp0rkle, line *base.Line) {
	sd := bot.GetDriver(driverName).(*seenDriver)
	n, c := line.Storable()
	kn := db.StorableNick{Nick: line.Args[1]}
	// SeenNickFromLine doesn't work with the hacks for KICKING and KICKED
	// First, handle KICKING
	kr := sd.LastSeenDoing(line.Nick, "KICKING")
	if kr == nil {
		kr = seen.SawNick(n, c, "KICKING", line.Args[2])
	} else {
		kr.StorableNick, kr.StorableChan = n, c
		kr.Timestamp, kr.Text = time.Now(), line.Args[2]
	}
	kr.OtherNick = kn
	_, err := sd.Upsert(kr.Index(), kr)
	if err != nil {
		bot.Reply(line, "Failed to store seen data: %v", err)
	}
	// Now, handle KICKED
	ke := sd.LastSeenDoing(line.Args[1], "KICKED")
	if ke == nil {
		ke = seen.SawNick(kn, c, "KICKED", line.Args[2])
	} else {
		ke.StorableNick, ke.StorableChan = kn, c
		ke.Timestamp, ke.Text = time.Now(), line.Args[2]
	}
	ke.OtherNick = n
	_, err = sd.Upsert(ke.Index(), ke)
	if err != nil {
		bot.Reply(line, "Failed to store seen data: %v", err)
	}
}
コード例 #4
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func ud_privmsg(bot *bot.Sp0rkle, line *base.Line) {
	ud := bot.GetDriver(driverName).(*urlDriver)

	// If we're not being addressed directly, short-circuit to scan.
	if !line.Addressed {
		ud_scan(bot, ud, line)
		return
	}

	nl := line.Copy()
	switch {
	case util.StripAnyPrefix(&nl.Args[1],
		[]string{"url find ", "urlfind ", "url search ", "urlsearch "}):
		ud_find(bot, ud, nl)
	case util.HasAnyPrefix(nl.Args[1], []string{"random url", "randurl"}):
		nl.Args[1] = ""
		ud_find(bot, ud, nl)
	case util.StripAnyPrefix(&nl.Args[1],
		[]string{"shorten that", "shorten"}):
		ud_shorten(bot, ud, nl)
	case util.StripAnyPrefix(&nl.Args[1],
		[]string{"cache that", "save that", "cache ", "save "}):
		ud_cache(bot, ud, nl)
	default:
		ud_scan(bot, ud, line)
	}
}
コード例 #5
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func qd_add(bot *bot.Sp0rkle, qd *quoteDriver, line *base.Line) {
	n, c := line.Storable()
	quote := quotes.NewQuote(line.Args[1], n, c)
	quote.QID = qd.NewQID()
	if err := qd.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
ファイル: seendriver.go プロジェクト: b33f/sp0rkle
// Look up or create a "seen" entry for the line.
// Explicitly don't handle updating line.Text or line.OtherNick
func (sd *seenDriver) SeenNickFromLine(line *base.Line) *seen.Nick {
	sn := sd.LastSeenDoing(line.Nick, line.Cmd)
	n, c := line.Storable()
	if sn == nil {
		sn = seen.SawNick(n, c, line.Cmd, "")
	} else {
		sn.StorableNick, sn.StorableChan = n, c
		sn.Timestamp = time.Now()
	}
	return sn
}
コード例 #7
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func fd_chance(bot *bot.Sp0rkle, fd *factoidDriver, line *base.Line) {
	str := strings.TrimSpace(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 := fd.Lastseen(line.Args[0], "")
	// ok, we're good to update the chance.
	if fact := fd.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 := fd.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 プロジェクト: b33f/sp0rkle
func fd_lookup(bot *bot.Sp0rkle, fd *factoidDriver, 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 = fd.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.Conn.Me.Nick) {
			key = strings.TrimSpace(key[:len(key)-len(bot.Conn.Me.Nick)])
			fact = fd.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
		fd.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 := fd.Update(bson.M{"_id": fact.Id}, fact); err != nil {
			bot.ReplyN(line, "I failed to update '%s' (%s): %s ",
				fact.Key, fact.Id, err)
		}

		// Apply the list of factoid plugins to the factoid value.
		val := fd.ApplyPlugins(fact.Value, line)
		switch fact.Type {
		case factoids.F_ACTION:
			bot.Conn.Action(line.Args[0], val)
		default:
			bot.Conn.Privmsg(line.Args[0], val)
		}
	}
}
コード例 #9
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func fd_privmsg(bot *bot.Sp0rkle, line *base.Line) {
	fd := bot.GetDriver(driverName).(*factoidDriver)

	// If we're not being addressed directly, short-circuit to lookup.
	if !line.Addressed {
		fd_lookup(bot, fd, line)
		return
	}

	nl := line.Copy()
	// Test for various possible courses of action.
	switch {
	// Factoid add: 'key := value' or 'key :is value'
	case util.ContainsAny(nl.Args[1], []string{":=", ":is"}):
		fd_add(bot, fd, nl)

	// Factoid delete: 'forget|delete that' => deletes fd.lastseen[chan]
	case util.HasAnyPrefix(nl.Args[1], []string{"forget that", "delete that"}):
		fd_delete(bot, fd, nl)

	// Factoid replace: 'replace that with' => updates fd.lastseen[chan]
	case util.StripAnyPrefix(&nl.Args[1], []string{"replace that with "}):
		fd_replace(bot, fd, nl)

	// Factoid chance: 'chance of that is' => sets chance of fd.lastseen[chan]
	case util.StripAnyPrefix(&nl.Args[1], []string{"chance of that is "}):
		fd_chance(bot, fd, nl)

	// Factoid literal: 'literal key' => info about factoid
	case util.StripAnyPrefix(&nl.Args[1], []string{"literal "}):
		fd_literal(bot, fd, nl)

	// Factoid search: 'fact search regexp' => list of possible key matches
	case util.StripAnyPrefix(&nl.Args[1], []string{"fact search "}):
		fd_search(bot, fd, nl)

	// Factoid info: 'fact info key' => some information about key
	case util.StripAnyPrefix(&nl.Args[1], []string{"fact info "}):
		fd_info(bot, fd, nl)

	// If we get to here, none of the other FD command possibilities
	// have matched, so try a lookup...
	default:
		fd_lookup(bot, fd, nl)
	}
}
コード例 #10
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func sd_smoke(bot *bot.Sp0rkle, line *base.Line) {
	if !smokeRx.MatchString(line.Args[1]) {
		return
	}
	sd := bot.GetDriver(driverName).(*seenDriver)
	sn := sd.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.StorableNick, sn.StorableChan = n, c
		sn.Timestamp = time.Now()
	} else {
		sn = seen.SawNick(n, c, "SMOKE", "")
	}
	if _, err := sd.Upsert(sn.Index(), sn); err != nil {
		bot.Reply(line, "Failed to store smoke data: %v", err)
	}
}
コード例 #11
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func sd_record_lines(bot *bot.Sp0rkle, line *base.Line) {
	sd := bot.GetDriver(driverName).(*seenDriver)
	sn := sd.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)
		}
	}
	_, err := sd.Upsert(sn.Index(), sn)
	if err != nil {
		bot.Reply(line, "Failed to store seen data: %v", err)
	}
}
コード例 #12
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func fd_replace(bot *bot.Sp0rkle, fd *factoidDriver, line *base.Line) {
	ls := fd.Lastseen(line.Args[0], "")
	if fact := fd.GetById(ls); fact != nil {
		// Store the old factoid value
		old := fact.Value
		// Replace the value with the new one
		fact.Value = strings.TrimSpace(line.Args[1])
		// Update the Modified field
		fact.Modify(line.Storable())
		// And store the new factoid data
		if err := fd.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 プロジェクト: b33f/sp0rkle
func ud_cache(bot *bot.Sp0rkle, ud *urlDriver, line *base.Line) {
	var u *urls.Url
	if line.Args[1] == "" {
		// assume we have been given "cache that"
		if u = ud.GetById(ud.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.Prefix, 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 = ud.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.Prefix, cachePath, u.CachedAs,
				u.CacheTime.Format(time.RFC1123))
			return
		}
	}
	if err := ud.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.Prefix, cachePath, u.CachedAs)
}
コード例 #14
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func fd_add(bot *bot.Sp0rkle, fd *factoidDriver, line *base.Line) {
	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)
	if err := fd.Insert(fact); err == nil {
		count := fd.GetCount(key)
		bot.ReplyN(line, "Woo, I now know %d things about '%s'.", count, key)
	} else {
		bot.ReplyN(line, "Error storing factoid: %s.", err)
	}
}
コード例 #15
0
ファイル: handlers.go プロジェクト: b33f/sp0rkle
func ud_shorten(bot *bot.Sp0rkle, ud *urlDriver, line *base.Line) {
	var u *urls.Url
	if line.Args[1] == "" {
		// assume we have been given "shorten that"
		if u = ud.GetById(ud.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.Prefix, 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 = ud.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.Prefix, shortenPath, u.Shortened)
			return
		}
	}
	if err := ud.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.Prefix, shortenPath, u.Shortened)
}