|
| 1 | +package com.trixiether.dashchan.chan.refugedobrochan; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | +import android.util.Pair; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashSet; |
| 9 | + |
| 10 | +import chan.http.RequestEntity; |
| 11 | +import chan.text.ParseException; |
| 12 | +import chan.text.TemplateParser; |
| 13 | +import chan.util.StringUtils; |
| 14 | + |
| 15 | +public class RefugeDobrochanAntispamFieldsParser { |
| 16 | + |
| 17 | + private final HashSet<String> ignoreFields = new HashSet<>(); |
| 18 | + private final HashSet<String> hiddenInputsNames = new HashSet<>(); |
| 19 | + private final ArrayList<Pair<String, String>> fields = new ArrayList<>(); |
| 20 | + |
| 21 | + private boolean formParsing; |
| 22 | + private String fieldName; |
| 23 | + |
| 24 | + private RefugeDobrochanAntispamFieldsParser(String source, RequestEntity entity, String... ignoreFields) throws ParseException { |
| 25 | + Collections.addAll(this.ignoreFields, ignoreFields); |
| 26 | + Collections.addAll(this.hiddenInputsNames, "user", "username", "login", "search", |
| 27 | + "q", "url", "firstname", "lastname", "text", "message", "hash"); |
| 28 | + PARSER.parse(source, this); |
| 29 | + for (Pair<String, String> field : fields) { |
| 30 | + entity.add(field.first, field.second); |
| 31 | + Log.d("!!!", field.first + " - " + field.second); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static void parseAndApply(String source, RequestEntity entity, String... ignoreFields) |
| 36 | + throws ParseException { |
| 37 | + new RefugeDobrochanAntispamFieldsParser(source, entity, ignoreFields); |
| 38 | + } |
| 39 | + |
| 40 | + private static final TemplateParser<RefugeDobrochanAntispamFieldsParser> PARSER = TemplateParser.<RefugeDobrochanAntispamFieldsParser>builder() |
| 41 | + .equals("form", "name", "post").open((instance, holder, tagName, attributes) -> { |
| 42 | + holder.formParsing = true; |
| 43 | + return false; |
| 44 | + }) |
| 45 | + .name("input").open((instance, holder, tagName, attributes) -> { |
| 46 | + if (holder.formParsing) { |
| 47 | + String name = StringUtils.unescapeHtml(attributes.get("name")); |
| 48 | + boolean findTag = true; |
| 49 | + for (String s : holder.ignoreFields) { |
| 50 | + if (s.equals(name)) { |
| 51 | + findTag = false; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + if (findTag) { |
| 56 | + if (!name.contains("delete")) { |
| 57 | + String value = StringUtils.unescapeHtml(attributes.get("value")); |
| 58 | + holder.fields.add(new Pair<>(StringUtils.unescapeHtml(name), value)); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | + }) |
| 64 | + .name("textarea").open((instance, holder, tagName, attributes) -> { |
| 65 | + holder.fieldName = StringUtils.unescapeHtml(attributes.get("name")); |
| 66 | + return true; |
| 67 | + }) |
| 68 | + .content((instance, holder, source) -> { |
| 69 | + if (holder.formParsing) { |
| 70 | + if (!source.isEmpty()) { |
| 71 | + for (String s : holder.hiddenInputsNames) |
| 72 | + if (s.equals(holder.fieldName)) |
| 73 | + holder.fields.add(new Pair<>(holder.fieldName, StringUtils.unescapeHtml(source))); |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | + .prepare(); |
| 78 | +} |
0 commit comments