Beispiel #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
		}
	}
}
Beispiel #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
		}
	}
}
Beispiel #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)

}
Beispiel #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)

}
Beispiel #5
0
func config() *oauth2.Config {
	return &oauth2.Config{
		ClientID:     bot.GetSecret(*pushClientID),
		ClientSecret: bot.GetSecret(*pushClientSecret),
		Endpoint: oauth2.Endpoint{
			AuthURL:  "https://www.pushbullet.com/authorize",
			TokenURL: pushAPI("/oauth2/token"),
		},
		RedirectURL: bot.HttpHost() + "/oauth/auth",
		Scopes:      []string{"everything"},
	}
}
Beispiel #6
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)
}
Beispiel #7
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)
}
Beispiel #8
0
func pushDeviceURL(state string) string {
	return bot.HttpHost() + "/oauth/device?state=" + state
}
Beispiel #9
0
func pushSuccessURL() string {
	return bot.HttpHost() + "/oauth/success"
}
Beispiel #10
0
func pushFailureURL(fail string) string {
	return bot.HttpHost() + "/oauth/failure?fail=" + fail
}