Example #1
0
// Sanitize the html, leaving only tags we consider safe (see the sanitize package for details and tests)
func Sanitize(s string) got.HTML {
	s, err := sanitize.HTMLAllowing(s)
	if err != nil {
		fmt.Printf("#error sanitizing html:%s", err)
		return got.HTML("")
	}
	return got.HTML(s)
}
Example #2
0
func sanitizeMessage(message string) string {

	isHTML := strings.Contains(message, "<html>")

	if isHTML {

		text, err := sanitize.HTMLAllowing(message, allowedTags, allowedAttributes)

		if err != nil {
			log.Println("failed to convert to text " + err.Error())
		}
		return text
	}

	return message

}
Example #3
0
// EditMessageText edit the text of message previously sent by the bot
func (c *Context) EditMessageText(om *OutgoingMessage, text string) error {
	if om == nil {
		return errors.New("Empty message provided")
	}
	textHash := fmt.Sprintf("%x", md5.Sum([]byte(text)))
	bot := c.Bot()
	if om.TextHash == textHash {
		return errors.New("EditMessageText: text not mofified")
	}

	if om.ParseMode == "HTML" {
		textCleared, err := sanitize.HTMLAllowing(text, []string{"a", "b", "strong", "i", "em", "a", "code", "pre"}, []string{"href"})

		if err == nil && textCleared != "" {
			text = textCleared
		}
	}

	_, err := bot.API.Send(tg.EditMessageTextConfig{
		BaseEdit: tg.BaseEdit{
			ChatID:      om.ChatID,
			MessageID:   om.MsgID,
			ReplyMarkup: &tg.InlineKeyboardMarkup{InlineKeyboard: om.InlineKeyboardMarkup.tg()},
		},
		ParseMode:             om.ParseMode,
		DisableWebPagePreview: !om.WebPreview,
		Text: text,
	})
	if err != nil {
		if err.(tg.Error).IsCantAccessChat() || err.(tg.Error).ChatMigratedToChatID() != 0 {
			if c.Callback != nil && c.Callback.AnsweredAt == nil {
				c.AnswerCallbackQuery("Sorry, message can be outdated. Bot can't edit messages created before converting to the Super Group", false)
			}
		} else if err.(tg.Error).IsAntiFlood() {
			c.Log().WithError(err).Warn("TG Anti flood activated")
		}
	} else {
		err = c.db.C("messages").UpdateId(om.ID, bson.M{"$set": bson.M{"texthash": textHash}})
	}
	return err
}
Example #4
0
func Format(d *goquery.Document) (ret *Article, err error) {
	article := d.Find("article, .b-topic_news").First()
	article.Find(".info, .b-label").
		Each(func(i int, e *goquery.Selection) {
			var cont string
			cont, err = e.Html()
			e.ReplaceWithHtml("<figcaption>" + cont + "</figcaption>")
		})
	if err != nil {
		return
	}

	article.Find(".b-topic__socials," +
		".b-banner-superfooter," +
		"script," +
		".b-topic-addition," +
		".b-inline-topics-box," +
		".b-inline-topics-box," +
		".b-inline-topics-box_wide," +
		".b-banner").Remove()

	articleBody, err := article.Html()
	if err != nil {
		return
	}
	articleS, err := sanitize.HTMLAllowing(articleBody,
		TagWhiteList,
		AttrWhiteList)

	if err != nil {
		return
	}

	retGq, err := goquery.NewDocumentFromReader(strings.NewReader(articleS))
	if err != nil {
		return nil, err
	}

	return (*Article)(retGq), nil
}
Example #5
0
// EditMessageTextAndInlineKeyboard edit the outgoing message's text and inline keyboard
func (c *Context) EditMessageTextAndInlineKeyboard(om *OutgoingMessage, fromState string, text string, kb InlineKeyboard) error {
	bot := c.Bot()
	if om.MsgID != 0 {
		log.WithField("msgID", om.MsgID).Debug("EditMessageTextAndInlineKeyboard")
	} else {
		om.ChatID = 0
		log.WithField("inlineMsgID", om.InlineMsgID).Debug("EditMessageTextAndInlineKeyboard")
	}

	var msg OutgoingMessage
	var ci *mgo.ChangeInfo
	var err error
	if fromState != "" {
		ci, err = c.db.C("messages").Find(bson.M{"_id": om.ID, "$or": []bson.M{{"inlinekeyboardmarkup.state": fromState}, {"inlinekeyboardmarkup": bson.M{"$exists": false}}}}).Apply(mgo.Change{Update: bson.M{"$set": bson.M{"inlinekeyboardmarkup": kb, "text": text}}}, &msg)
	} else {
		ci, err = c.db.C("messages").Find(bson.M{"_id": om.ID}).Apply(mgo.Change{Update: bson.M{"$set": bson.M{"inlinekeyboardmarkup": kb, "text": text}}}, &msg)
	}

	if err != nil {
		c.Log().WithError(err).Error("EditMessageTextAndInlineKeyboard messages update error")
	}

	if msg.BotID == 0 {
		c.Log().Warn(fmt.Sprintf("EditMessageTextAndInlineKeyboard – message (_id=%s botid=%v id=%v state %s) not found", om.ID, bot.ID, om.MsgID, fromState))
		return nil

	}
	if ci.Updated == 0 {
		c.Log().Warn(fmt.Sprintf("EditMessageTextAndInlineKeyboard – message (_id=%s botid=%v id=%v state %s) not updated ", om.ID, bot.ID, om.MsgID, fromState))

		return nil
	}

	if om.ParseMode == "HTML" {
		textCleared, err := sanitize.HTMLAllowing(text, []string{"a", "b", "strong", "i", "em", "a", "code", "pre"}, []string{"href"})

		if err == nil && textCleared != "" {
			text = textCleared
		}
	}

	_, err = bot.API.Send(tg.EditMessageTextConfig{
		BaseEdit: tg.BaseEdit{
			ChatID:          om.ChatID,
			InlineMessageID: om.InlineMsgID,
			MessageID:       om.MsgID,
			ReplyMarkup:     &tg.InlineKeyboardMarkup{InlineKeyboard: kb.tg()},
		},
		ParseMode: om.ParseMode,
		Text:      text,
		DisableWebPagePreview: !om.WebPreview,
	})

	if err != nil {
		if err.(tg.Error).IsCantAccessChat() || err.(tg.Error).ChatMigratedToChatID() != 0 {
			if c.Callback != nil {
				c.AnswerCallbackQuery("Message can be outdated. Bot can't edit messages created before converting to the Super Group", false)
			}
		} else if err.(tg.Error).IsAntiFlood() {
			c.Log().WithError(err).Warn("TG Anti flood activated")
		}
		// Oops. error is occurred – revert the original keyboard
		c.db.C("messages").Update(bson.M{"_id": msg.ID}, bson.M{"$set": bson.M{"inlinekeyboardmarkup": msg.InlineKeyboardMarkup}})
		return err
	}

	return nil
}