Example #1
0
// 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)
	}
}
Example #2
0
func karmaCmd(line *base.Line) {
	if k := kc.KarmaFor(line.Args[1]); k != nil {
		bot.ReplyN(line, "%s", k)
	} else {
		bot.ReplyN(line, "No karma found for '%s'", line.Args[1])
	}
}
Example #3
0
func calculate(line *base.Line) {
	maths := line.Args[1]
	if num, err := calc.Calc(maths); err == nil {
		bot.ReplyN(line, "%s = %g", maths, num)
	} else {
		bot.ReplyN(line, "%s error while parsing %s", err, maths)
	}
}
Example #4
0
func ord(line *base.Line) {
	ord := line.Args[1]
	r, _ := utf8.DecodeRuneInString(ord)
	if r == utf8.RuneError {
		bot.ReplyN(line, "Couldn't parse a utf8 rune from %s", ord)
		return
	}
	bot.ReplyN(line, "ord(%c) is %d, %U, '%s'", r, r, r, utf8repr(r))
}
Example #5
0
func chr(line *base.Line) {
	chr := line.Args[1]
	// handles decimal, hex, and octal \o/
	i, err := strconv.ParseInt(chr, 0, 0)
	if err != nil {
		bot.ReplyN(line, "Couldn't parse %s as an integer: %s", chr, err)
		return
	}
	bot.ReplyN(line, "chr(%s) is %c, %U, '%s'", chr, i, i, utf8repr(rune(i)))
}
Example #6
0
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)
	}
}
Example #7
0
func netmask(line *base.Line) {
	s := strings.Split(line.Args[1], " ")
	if strings.Index(s[1], "/") != -1 {
		// Assume we have netmask ip/cidr
		bot.ReplyN(line, "%s", parseCIDR(s[0]))
	} else if len(s) == 2 {
		// Assume we have netmask ip nm
		bot.ReplyN(line, "%s", parseMask(s[0], s[1]))
	} else {
		bot.ReplyN(line, "bad netmask args: %s", line.Args[1])
	}
}
Example #8
0
func randomCmd(line *base.Line) {
	source := mc.CreateSourceForTag("user:"******"%s would say: %s", line.Args[1], out)
	} else {
		bot.ReplyN(line, "error: %v", err)
	}
}
Example #9
0
// Factoid delete: 'forget|delete that' => deletes lastSeen[chan]
func forget(line *base.Line) {
	// Get fresh state on the last seen factoid.
	ls := LastSeen(line.Args[0], "")
	if fact := fc.GetById(ls); fact != nil {
		if err := fc.Remove(bson.M{"_id": ls}); err == nil {
			bot.ReplyN(line, "I forgot that '%s' was '%s'.",
				fact.Key, fact.Value)
		} else {
			bot.ReplyN(line, "I failed to forget '%s': %s", fact.Key, err)
		}
	} else {
		bot.ReplyN(line, "Whatever that was, I've already forgotten it.")
	}
}
Example #10
0
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
		}
	}
}
Example #11
0
func fetch(line *base.Line) {
	if RateLimit(line.Nick) {
		return
	}
	qid, err := strconv.Atoi(line.Args[1])
	if err != nil {
		bot.ReplyN(line, "'%s' doesn't look like a quote id.", line.Args[1])
		return
	}
	quote := qc.GetByQID(qid)
	if quote != nil {
		bot.Reply(line, "#%d: %s", quote.QID, quote.Quote)
	} else {
		bot.ReplyN(line, "No quote found for id %d", qid)
	}
}
Example #12
0
// remind del
func del(line *base.Line) {
	list, ok := listed[line.Nick]
	if !ok {
		bot.ReplyN(line, "Please use 'remind list' first, "+
			"to be sure of what you're deleting.")
		return
	}
	idx, err := strconv.Atoi(line.Args[1])
	if err != nil || idx > len(list) || idx <= 0 {
		bot.ReplyN(line, "Invalid reminder index '%s'", line.Args[1])
		return
	}
	idx--
	Forget(list[idx], true)
	delete(listed, line.Nick)
	bot.ReplyN(line, "I'll forget that one, then...")
}
Example #13
0
func lookup(line *base.Line) {
	if RateLimit(line.Nick) {
		return
	}
	quote := qc.GetPseudoRand(line.Args[1])
	if quote == nil {
		bot.ReplyN(line, "No quotes matching '%s' found.", line.Args[1])
		return
	}

	// TODO(fluffle): qd should take care of updating Accessed internally
	quote.Accessed++
	if err := qc.Update(bson.M{"_id": quote.Id}, quote); err != nil {
		bot.ReplyN(line, "I failed to update quote #%d: %s", quote.QID, err)
	}
	bot.Reply(line, "#%d: %s", quote.QID, quote.Quote)
}
Example #14
0
// Factoid info: 'fact info key' => some information about key
func info(line *base.Line) {
	key := ToKey(line.Args[1], false)
	count := fc.GetCount(key)
	if count == 0 {
		bot.ReplyN(line, "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,",
			c.Timestamp.Format(time.ANSIC), c.Nick))
	}
	if modified := fc.GetLast("modified", key); modified != nil {
		m := modified.Modified
		msgs = append(msgs, fmt.Sprintf("modified on %s by %s,",
			m.Timestamp.Format(time.ANSIC), m.Nick))
	}
	if accessed := fc.GetLast("accessed", key); accessed != nil {
		a := accessed.Accessed
		msgs = append(msgs, fmt.Sprintf("and accessed on %s by %s.",
			a.Timestamp.Format(time.ANSIC), 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))
	}
	bot.ReplyN(line, "%s", strings.Join(msgs, " "))
}
Example #15
0
// 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.")
	}
}
Example #16
0
func lines(line *base.Line) {
	n := line.Nick
	if len(line.Args[1]) > 0 {
		n = line.Args[1]
	}
	sn := sc.LinesFor(n, line.Args[0])
	if sn != nil {
		bot.ReplyN(line, "%s has said %d lines in this channel",
			sn.Nick, sn.Lines)
	}
}
Example #17
0
func del(line *base.Line) {
	// Strip optional # before qid
	if line.Args[1][0] == '#' {
		line.Args[1] = line.Args[1][1:]
	}
	qid, err := strconv.Atoi(line.Args[1])
	if err != nil {
		bot.ReplyN(line, "'%s' doesn't look like a quote id.", line.Args[1])
		return
	}
	if quote := qc.GetByQID(qid); quote != nil {
		if err := qc.RemoveId(quote.Id); err == nil {
			bot.ReplyN(line, "I forgot quote #%d: %s", qid, quote.Quote)
		} else {
			bot.ReplyN(line, "I failed to forget quote #%d: %s", qid, err)
		}
	} else {
		bot.ReplyN(line, "No quote found for id %d", qid)
	}
}
Example #18
0
// remind list
func list(line *base.Line) {
	r := rc.RemindersFor(line.Nick)
	c := len(r)
	if c == 0 {
		bot.ReplyN(line, "You have no reminders set.")
		return
	}
	if c > 5 && line.Args[0][0] == '#' {
		bot.ReplyN(line, "You've got lots of reminders, ask me privately.")
		return
	}
	// Save an ordered list of ObjectIds for easy reminder deletion
	bot.ReplyN(line, "You have %d reminders set:", c)
	list := make([]bson.ObjectId, c)
	for i := range r {
		bot.Reply(line, "%d: %s", i+1, r[i].List(line.Nick))
		list[i] = r[i].Id
	}
	listed[line.Nick] = list
}
Example #19
0
// Factoid search: 'fact search regexp' => list of possible key matches
func search(line *base.Line) {
	keys := fc.GetKeysMatching(line.Args[1])
	if keys == nil || len(keys) == 0 {
		bot.ReplyN(line, "I couldn't think of anything matching '%s'.",
			line.Args[1])
		return
	}
	// RESULTS.
	count := len(keys)
	if count > 10 {
		res := strings.Join(keys[:10], "', '")
		bot.ReplyN(line,
			"I found %d keys matching '%s', here's the first 10: '%s'.",
			count, line.Args[1], res)
	} else {
		res := strings.Join(keys, "', '")
		bot.ReplyN(line,
			"%s: I found %d keys matching '%s', here they are: '%s'.",
			count, line.Args[1], res)
	}
}
Example #20
0
// 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)
}
Example #21
0
func convertBase(line *base.Line) {
	s := strings.Split(line.Args[1], " ")
	fromto := strings.Split(s[0], "to")
	if len(fromto) != 2 {
		bot.ReplyN(line, "Specify base as: <from base>to<to base>")
		return
	}
	from, errf := strconv.Atoi(fromto[0])
	to, errt := strconv.Atoi(fromto[1])
	if errf != nil || errt != nil ||
		from < 2 || from > 36 || to < 2 || to > 36 {
		bot.ReplyN(line, "Either %s or %s is a bad base, must be in range 2-36",
			fromto[0], fromto[1])
		return
	}
	i, err := strconv.ParseInt(s[1], from, 64)
	if err != nil {
		bot.ReplyN(line, "Couldn't parse %s as a base %d integer", s[1], from)
		return
	}
	bot.ReplyN(line, "%s in base %d is %s in base %d",
		s[1], from, strconv.FormatInt(i, to), to)
}
Example #22
0
// Factoid literal: 'literal key' => info about factoid
func literal(line *base.Line) {
	key := ToKey(line.Args[1], false)
	if count := fc.GetCount(key); count == 0 {
		bot.ReplyN(line, "I don't know anything about '%s'.", key)
		return
	} else if count > 10 && strings.HasPrefix(line.Args[0], "#") {
		bot.ReplyN(line, "I know too much about '%s', ask me privately.", key)
		return
	}

	// Passing an anonymous function to For makes it a little hard to abstract
	// away in lib/factoids. Fortunately this is something of a one-off.
	var fact *factoids.Factoid
	f := func() error {
		if fact != nil {
			bot.ReplyN(line, "[%3.0f%%] %s", fact.Chance*100, fact.Value)
		}
		return nil
	}
	// TODO(fluffle): For() is deprecated nao. FixitFixitFixit.
	if err := fc.Find(bson.M{"key": key}).For(&fact, f); err != nil {
		bot.ReplyN(line, "Something literally went wrong: %s", err)
	}
}
Example #23
0
// 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())
}
Example #24
0
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)
		}
	}
}
Example #25
0
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)
	}
}
Example #26
0
func tellCheck(line *base.Line) {
	nick := line.Nick
	if line.Cmd == "NICK" {
		// We want the destination nick, not the source.
		nick = line.Args[0]
	}
	r := rc.TellsFor(nick)
	for i := range r {
		if line.Cmd == "NICK" {
			bot.Privmsg(string(r[i].Chan), nick+": "+r[i].Reply())
			bot.Reply(line, "%s", r[i].Reply())
		} else {
			bot.Privmsg(line.Nick, r[i].Reply())
			bot.ReplyN(line, "%s", r[i].Reply())
		}
		rc.RemoveId(r[i].Id)
	}
	if len(r) > 0 {
		delete(listed, line.Nick)
	}
}
Example #27
0
// 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.")
	}
}
Example #28
0
func seenCmd(line *base.Line) {
	s := strings.Fields(line.Args[1])
	if len(s) == 2 {
		// Assume we have "seen <nick> <action>"
		if n := sc.LastSeenDoing(s[0], strings.ToUpper(s[1])); n != nil {
			bot.ReplyN(line, "%s", n)
			return
		}
	}
	// Not specifically asking for that action, or no matching action.
	if n := sc.LastSeen(s[0]); n != nil {
		bot.ReplyN(line, "%s", n)
		return
	}
	// No exact matches for nick found, look for possible partial matches.
	if m := sc.SeenAnyMatching(s[1]); len(m) > 0 {
		if len(m) == 1 {
			if n := sc.LastSeen(m[0]); n != nil {
				bot.ReplyN(line, "1 possible match: %s", n)
			}
		} else if len(m) > 10 {
			bot.ReplyN(line, "%d possible matches, first 10 are: %s.",
				len(m), strings.Join(m[:9], ", "))
		} else {
			bot.ReplyN(line, "%d possible matches: %s.",
				len(m), strings.Join(m, ", "))
		}
		return
	}
	// No partial matches found. Check for people playing silly buggers.
	for _, w := range wittyComebacks {
		logging.Debug("Matching %#v...", w)
		if w.rx.MatchString(line.Args[1]) {
			bot.ReplyN(line, "%s", w.resp)
			return
		}
	}
	// Ok, probably a genuine query.
	bot.ReplyN(line, "Haven't seen %s before, sorry.", line.Args[1])
}
Example #29
0
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)
}
Example #30
0
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)
}