Esempio n. 1
0
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
		}
	}
}
Esempio n. 2
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
		}
	}
}
Esempio n. 3
0
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)

}
Esempio n. 4
0
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)

}
Esempio n. 5
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)
}
Esempio n. 6
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)
}
Esempio n. 7
0
func ParseValue(v string) (ft FactoidType, fv string) {
	// Assume v is a normal factoid
	ft = F_FACT

	// Check for perlfu prefixes and strip them
	if strings.HasPrefix(v, "<me>") {
		// <me>does something
		ft, fv = F_ACTION, v[4:]
	} else if strings.HasPrefix(v, "<reply>") {
		// <reply> is treated the same as F_FACT now,
		// Factoid.Key is not used except for searching.
		// NOTE: careful with this -- it's used in factimporter too...
		fv = v[7:]
	} else {
		fv = v
	}
	if util.LooksURLish(fv) {
		// Quite a few factoids are just <reply>http://some.url/
		// it's helpful to detect this so we can do useful things
		ft = F_URL
	}
	return
}