Skip to content

Commit

Permalink
Merge pull request TrixiEther#83 from Sweetsound/2ch-emoji-captcha
Browse files Browse the repository at this point in the history
Support emoji_captcha for 2ch.hk
  • Loading branch information
TrixiEther authored Sep 9, 2024
2 parents 5190073 + 0292c37 commit bbceaed
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 13 deletions.
1 change: 1 addition & 0 deletions extensions/dvach/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

<resources>
<string name="preference_captcha_full_keyboard">Использовать полную клавиатуру для капчи</string>
<string name="emoji_captcha_input">Выберите все символы на картинке (в любом порядке)</string>
</resources>
1 change: 1 addition & 0 deletions extensions/dvach/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

<resources>
<string name="preference_captcha_full_keyboard">Use full keyboard for captcha</string>
<string name="emoji_captcha_input">Select all icons from the picture (any order)</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

public class DvachChanConfiguration extends ChanConfiguration {
public static final String CAPTCHA_TYPE_2CH_CAPTCHA = "2ch_captcha";
public static final String CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA = "emoji_captcha";

public static final Map<String, String> CAPTCHA_TYPES;

static {
Map<String, String> captchaTypes = new LinkedHashMap<>();
captchaTypes.put(CAPTCHA_TYPE_2CH_CAPTCHA, "2chcaptcha");
captchaTypes.put(CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA, "emoji");
captchaTypes.put(CAPTCHA_TYPE_RECAPTCHA_2, "recaptcha");
captchaTypes.put(CAPTCHA_TYPE_RECAPTCHA_2_INVISIBLE, "invisible_recaptcha");
CAPTCHA_TYPES = Collections.unmodifiableMap(captchaTypes);
Expand Down Expand Up @@ -69,15 +71,23 @@ public Board obtainBoardConfiguration(String boardName) {

@Override
public Captcha obtainCustomCaptchaConfiguration(String captchaType) {
if (CAPTCHA_TYPE_2CH_CAPTCHA.equals(captchaType)) {
Captcha captcha = new Captcha();
captcha.title = "2ch Captcha";
captcha.input = Captcha.Input.ALL;
captcha.validity = Captcha.Validity.IN_THREAD;
captcha.ttl = CAPTCHA_TTL;
return captcha;
Captcha captcha = new Captcha();
switch (captchaType) {
case CAPTCHA_TYPE_2CH_CAPTCHA:
captcha.title = "2ch Captcha";
captcha.input = Captcha.Input.ALL;
captcha.validity = Captcha.Validity.IN_THREAD;
captcha.ttl = CAPTCHA_TTL;
return captcha;
case CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA:
captcha.title = "Emoji Captcha";
captcha.input = Captcha.Input.ALL;
captcha.validity = Captcha.Validity.IN_THREAD;
captcha.ttl = CAPTCHA_TTL;
return captcha;
default:
return null;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,25 @@ private ReadCaptchaResult onReadCaptcha(ReadCaptchaData data, String captchaPass
}
}

} else if (DvachChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2.equals(data.captchaType) ||
} else if (DvachChanConfiguration.CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA.equals(data.captchaType)) {
if (data.mayShowLoadButton) {
return new ReadCaptchaResult(CaptchaState.NEED_LOAD, null);
}
DvachEmojiCaptchaProvider.DvachEmojiCaptchaAnswerRetriever retriever =
(Bitmap task, Bitmap[] keyboardImages) -> {
try {
return requireUserImageSingleChoice(-1,
keyboardImages,
configuration.getResources().getString(
R.string.emoji_captcha_input),
task);
} catch (HttpException e) {
return -1;
}
};
return new DvachEmojiCaptchaProvider(data, locator, id, retriever)
.loadEmojiCaptcha();
} else if (DvachChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2.equals(data.captchaType) ||
DvachChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2_INVISIBLE.equals(data.captchaType)) {
result = new ReadCaptchaResult(CaptchaState.CAPTCHA, captchaData);
captchaData.put(CaptchaData.API_KEY, id);
Expand Down Expand Up @@ -1091,14 +1109,19 @@ public SendPostResult onSendPost(SendPostData data) throws HttpException, ApiExc
String challenge = data.captchaData.get(CaptchaData.CHALLENGE);
String input = StringUtils.emptyIfNull(data.captchaData.get(CaptchaData.INPUT));

String remoteCaptchaType = DvachChanConfiguration.CAPTCHA_TYPES.get(data.captchaType);
if (remoteCaptchaType != null) {
entity.add("captcha_type", remoteCaptchaType);
if (!DvachChanConfiguration.CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA.equals(data.captchaType)) {
String remoteCaptchaType = DvachChanConfiguration.CAPTCHA_TYPES.get(data.captchaType);
if (remoteCaptchaType != null) {
entity.add("captcha_type", remoteCaptchaType);
}
}
if (DvachChanConfiguration.CAPTCHA_TYPE_2CH_CAPTCHA.equals(data.captchaType)) {
entity.add("2chcaptcha_id", challenge);
entity.add("2chcaptcha_value", input);
} else if (DvachChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2.equals(data.captchaType) ||
} else if (DvachChanConfiguration.CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA.equals(data.captchaType)) {
entity.add("captcha_type", DvachChanConfiguration.CAPTCHA_TYPE_2CH_EMOJI_CAPTCHA);
entity.add("emoji_captcha_id", challenge);
} else if (DvachChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2.equals(data.captchaType) ||
DvachChanConfiguration.CAPTCHA_TYPE_RECAPTCHA_2_INVISIBLE.equals(data.captchaType)) {
entity.add("g-recaptcha-response", input);
}
Expand Down
Loading

0 comments on commit bbceaed

Please sign in to comment.