//postCommentApp checks posted data and replaces >> links to html links,
//and  saves it as record.
func (m *mchCGI) postCommentApp() {
	if m.Req.Method != http.MethodPost {
		m.WR.Header().Set("Content-Type", "text/plain")
		m.WR.WriteHeader(404)
		fmt.Fprintf(m.WR, "404 Not Found")
		return
	}
	info := m.getCommentData()
	info["host"] = m.Req.Host
	key := m.checkInfo(info)
	if key == "" {
		return
	}

	referer := m.getCP932("Referer")
	reg := regexp.MustCompile("/2ch_([^/]+)/")
	var tag string
	if ma := reg.FindStringSubmatch(referer); ma != nil && m.HasAuth() {
		tag = util.FileDecode("dummy_" + ma[1])
	}
	table := mch.NewResTable(thread.NewCache(key))
	reg = regexp.MustCompile(">>([1-9][0-9]*)")
	body := reg.ReplaceAllStringFunc(info["body"], func(str string) string {
		noStr := reg.FindStringSubmatch(str)[1]
		no, err := strconv.Atoi(noStr)
		if err != nil {
			log.Fatal(err)
		}
		return ">>" + table.Num2id[no]
	})

	name := info["name"]
	var passwd string
	if strings.ContainsRune(name, '#') {
		ary := strings.Split(name, "#")
		name = ary[0]
		passwd = ary[1]
	}
	if passwd != "" && !m.IsAdmin() {
		m.errorResp("自ノード以外で署名機能は使えません", info)
	}
	err := m.postComment(key, name, info["mail"], body, passwd, tag)
	if err == errSpamM {
		m.errorResp("スパムとみなされました", info)
	}
	m.WR.Header().Set("Content-Type", "text/html; charset=Shift_JIS")
	fmt.Fprintln(m.WR,
		util.ToSJIS(`<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html"><title>書きこみました。</title></head><body>書きこみが終わりました。<br><br></body></html>`))
}
//serveContent serves str as content with name=name(only used suffix to determine
//data type),time=t after converted cp932. ServeContent is used to make clients possible
//to use range request.
func (m *mchCGI) serveContent(name string, t time.Time, str string) {
	br := bytes.NewReader([]byte(util.ToSJIS(str)))
	http.ServeContent(m.WR, m.Req, name, t, br)
}