func StartTicker(seconds int) { if tickerExists(&seconds) { logger.Debug("Not starting another " + string(seconds) + "s ticker.") return } Tickers[seconds] = &Ticker{ duration: &seconds, ticker: time.NewTicker(time.Duration(seconds) * time.Second), quit: make(chan struct{}), } go func() { for { select { case <-Tickers[seconds].ticker.C: for _, event := range Tickers[seconds].events { event.callback() } case <-Tickers[seconds].quit: Tickers[seconds].ticker.Stop() logger.Debug("Stopped " + string(seconds) + "s ticker.") return } } }() }
func StopTicker(ticker int) { if !tickerExists(&ticker) { logger.Debug("Tried to stop non-existant " + string(ticker) + "s ticker.") return } close(Tickers[ticker].quit) logger.Debug("Stopped " + string(ticker) + "s ticker.") }
func GetTitle(rawuri string) string { var index int ext := rawuri[strings.LastIndex(rawuri, "//")+2:] if index = strings.LastIndex(ext, "/"); index > 0 { ext = ext[index+1:] if index = strings.Index(ext, "."); index > -1 { ext = ext[index+1:] allow := []string{"htm", "html", "asp", "aspx", "php", "php3", "php5"} if !lib.HasElementString(allow, ext) { logger.Debug(fmt.Sprintf("[web.GetTitle()] Not an OK file extension: %s -> %s", rawuri, ext)) return "" } } } body, err := Get(&rawuri) if err != "" { return "" } r, regErr := regexp.Compile("<title?[^>]+>([^<]+)<\\/title>") if regErr != nil { logger.Error("[web.GetTitle()] Couldn't compile regex title regex") return "" } if title := r.FindString(string(body)); title != "" { rooturl := rawuri[strings.Index(rawuri, "//")+2:] if index = strings.Index(rooturl, "/"); index > -1 { rooturl = rooturl[:index] } return fmt.Sprintf("%s ~ %s", html.UnescapeString(lib.StripHtml(title)), rooturl) } return "" }
func RandPreposition() string { prep := string(*lib.RandSelectBytes(getFileContents("prepositions"))) if prep == "" { logger.Debug("Had to re-fetch Preposition") prep = RandPreposition() } return prep }
func RandPronoun() string { pro := string(*lib.RandSelectBytes(getFileContents("pronouns"))) if pro == "" { logger.Debug("Had to re-fetch Pronoun") pro = RandPronoun() } return pro }
func RandAdverb() string { adv := string(*lib.RandSelectBytes(getFileContents("adverbs"))) if adv == "" { logger.Debug("Had to re-fetch Adverb") adv = RandAdverb() } return adv }
func RandAdjective() string { adj := string(*lib.RandSelectBytes(getFileContents("adjectives"))) if adj == "" { logger.Debug("Had to re-fetch Adjective") adj = RandAdjective() } return adj }
func RandNoun() string { noun := string(*lib.RandSelectBytes(getFileContents("nouns"))) if noun == "" { logger.Debug("Had to re-fetch Noun") noun = RandNoun() } return noun }
func RandVerbing() string { verb := string(*lib.RandSelectBytes(getFileContents("verbs"))) if verb == "" { logger.Debug("Had to re-fetch Verb[ing]") verb = RandVerbing() } return strings.Split(verb, " ")[3] }
func RandVerb() string { verb := string(*lib.RandSelectBytes(getFileContents("verbs"))) if verb == "" { logger.Debug("Had to re-fetch Verb") verb = RandVerb() } return verb[0:strings.Index(verb, " ")] }