Beispiel #1
0
func (d *DiscourseSite) RefreshCSRF() (err error) {
	if d.csrfToken != "" {
		panic("unneeded refreshing of csrf")
	}
	req, err := d.makeRequest("/session/csrf.json")
	if err != nil {
		return
	}
	resp, err := d.do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode > 200 {
		return ErrorStatusCode(resp.StatusCode)
	}

	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var csrf ResponseCSRF
	err = json.Unmarshal(buf, &csrf)
	if err != nil {
		log.Error("Error getting csrf", err, buf)
		return err
	}

	d.csrfToken = csrf.Csrf
	return nil
}
Beispiel #2
0
func (bot *DiscourseSite) Reply(topicId int, postNumber int, raw string) (S_Post, error) {
	<-bot.postRateLimit

	var postReturn S_Post
	data := url.Values{}
	data.Set("raw", fmt.Sprintf(
		`%s

<!-- Generated by DisGoBot v%s -->`, raw, VERSION))
	data.Set("topic_id", strconv.Itoa(topicId))
	data.Set("reply_to_post_number", strconv.Itoa(postNumber))
	data.Set("archetype", "regular")

	err := bot.DPostJsonTyped("/posts", data, &postReturn)
	if err != nil {
		log.Error("While replying:", err)
		return S_Post{}, err
	}
	return postReturn, nil
}