// ensureOwnerExists makes sure that at least one owner exists in the database. func (bot *Bot) ensureOwnerExists() { result, err := bot.Db.Query(`SELECT EXISTS(SELECT 1 FROM users WHERE owner=1 LIMIT 1);`) if err != nil { bot.Log.Fatalf("Can't check if owner exists: %s", err) } defer result.Close() if result.Next() { var ownerExists bool if err = result.Scan(&ownerExists); err != nil { bot.Log.Fatalf("Can't check if owner exists: %s", err) } if !ownerExists { bot.Log.Warningf("No owner found in the database. Must create one.") stty, _ := exec.LookPath("stty") sttyArgs := syscall.ProcAttr{ "", []string{}, []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()}, nil, } reader := bufio.NewReader(os.Stdin) fmt.Print("Enter owner's nick: ") nick, _ := reader.ReadString('\n') // Disable echo. if stty != "" { syscall.ForkExec(stty, []string{"stty", "-echo"}, &sttyArgs) } // Get password. fmt.Print("Enter owner's password: "******"\nConfirm owner's password: "******"Passwords don't match.") } fmt.Print("\n") // Enable echo. if stty != "" { syscall.ForkExec(stty, []string{"stty", "echo"}, &sttyArgs) } result.Close() if bot.addUser(utils.CleanString(nick, false), utils.CleanString(pass1, false), true, true); err != nil { bot.Log.Fatalf("%s", err) } } } }
// searchWiki will query Wikipedia database for information. func (ext *ExtensionWiki) searchWiki(bot *papaBot.Bot, lang, search string) (string, string) { // Fetch data. body, err := bot.GetPageBodyByURL( fmt.Sprintf( "http://%s.wikipedia.org/w/api.php?action=query&prop=revisions&format=json&rvprop=content&rvlimit=1"+ "&rvsection=0&generator=search&redirects=&gsrwhat=text&gsrlimit=1&gsrsearch=%s", lang, url.QueryEscape(search), )) if err != nil { bot.Log.Warningf("Error getting wiki data: %s", err) return "", "" } // Convert from JSON var raw_data interface{} if err := json.Unmarshal(body, &raw_data); err != nil { bot.Log.Warningf("Error parsing wiki data: %s", err) return "", "" } // Hacky digging. data := raw_data.(map[string]interface{}) if data["query"] == nil { return "", "" } data = data["query"].(map[string]interface{}) if data["pages"] == nil { return "", "" } data = data["pages"].(map[string]interface{}) for _, page := range data { data := page.(map[string]interface{}) if data["revisions"] == nil { return "", "" } title := data["title"].(string) revisions := data["revisions"].([]interface{}) if len(revisions) == 0 { return "", "" } if revisions[0].(map[string]interface{})["*"] == nil { return "", "" } content := revisions[0].(map[string]interface{})["*"].(string) // Cleanup.M content = utils.StripTags(ext.cleanWikiBody(utils.CleanString(content, true))) title = utils.CleanString(title, true) return title, content } return "", "" }
// getTitle find the title and description. func (ext *ExtensionMeta) getTitle(body string) (string, string, error) { // Iterate over meta tags to get the description description := "" metas := ext.metaRe.FindAllStringSubmatch(string(body), -1) for i := range metas { if len(metas[i]) > 1 { isDesc := ext.descRe.FindString(metas[i][0]) if isDesc != "" && (len(metas[i][1]) > len(description)) { description = utils.CleanString(metas[i][1], true) } } } // Get the title match := ext.titleRe.FindStringSubmatch(string(body)) if len(match) > 1 { title := utils.CleanString(match[1], true) return title, description, nil } return "", "", nil }