@ -29,6 +29,9 @@ import org.floens.chan.core.model.SavedReply;
import org.floens.chan.ui.activity.ImagePickActivity ;
import org.floens.chan.ui.activity.ImagePickActivity ;
import org.floens.chan.utils.Logger ;
import org.floens.chan.utils.Logger ;
import org.floens.chan.utils.Utils ;
import org.floens.chan.utils.Utils ;
import org.jsoup.Jsoup ;
import org.jsoup.nodes.Document ;
import org.jsoup.select.Elements ;
import java.io.File ;
import java.io.File ;
import java.io.IOException ;
import java.io.IOException ;
@ -333,6 +336,37 @@ public class ReplyManager {
public String responseData = "" ;
public String responseData = "" ;
}
}
private void getCaptchaHash ( final CaptchaHashListener listener , String challenge , String response ) {
HttpPost httpPost = new HttpPost ( ChanUrls . getCaptchaFallback ( ) ) ;
MultipartEntityBuilder entity = MultipartEntityBuilder . create ( ) ;
entity . addTextBody ( "c" , challenge , TEXT_UTF_8 ) ;
entity . addTextBody ( "response" , response , TEXT_UTF_8 ) ;
httpPost . setEntity ( entity . build ( ) ) ;
sendHttpPost ( httpPost , new HttpPostSendListener ( ) {
@Override
public void onResponse ( String responseString , HttpClient client , HttpResponse response ) {
if ( responseString ! = null ) {
Document document = Jsoup . parseBodyFragment ( responseString ) ;
Elements verificationToken = document . select ( "div.fbc-verification-token textarea" ) ;
String hash = verificationToken . text ( ) ;
if ( hash . length ( ) > 0 ) {
listener . onHash ( hash ) ;
return ;
}
}
listener . onHash ( null ) ;
}
} ) ;
}
private interface CaptchaHashListener {
public void onHash ( String hash ) ;
}
/ * *
/ * *
* Send an reply off to the server .
* Send an reply off to the server .
*
*
@ -343,96 +377,111 @@ public class ReplyManager {
public void sendReply ( final Reply reply , final ReplyListener listener ) {
public void sendReply ( final Reply reply , final ReplyListener listener ) {
Logger . i ( TAG , "Sending reply request: " + reply . board + ", " + reply . resto ) ;
Logger . i ( TAG , "Sending reply request: " + reply . board + ", " + reply . resto ) ;
HttpPost httpPost = new HttpPost ( ChanUrls . getReplyUrl ( reply . board ) ) ;
CaptchaHashListener captchaHashListener = new CaptchaHashListener ( ) {
@Override
MultipartEntityBuilder entity = MultipartEntityBuilder . create ( ) ;
public void onHash ( String captchaHash ) {
if ( captchaHash = = null ) {
// Could not find a hash in the response html
ReplyResponse e = new ReplyResponse ( ) ;
e . isUserError = true ;
e . isCaptchaError = true ;
listener . onResponse ( e ) ;
return ;
}
reply . password = Long . toHexString ( random . nextLong ( ) ) ;
HttpPost httpPost = new HttpPost ( ChanUrls . getReplyUrl ( reply . board ) ) ;
entity . addTextBody ( "name" , reply . name , TEXT_UTF_8 ) ;
MultipartEntityBuilder entity = MultipartEntityBuilder . create ( ) ;
entity . addTextBody ( "email" , reply . email , TEXT_UTF_8 ) ;
entity . addTextBody ( "sub" , reply . subject , TEXT_UTF_8 ) ;
reply . password = Long . toHexString ( random . nextLong ( ) ) ;
entity . addTextBody ( "com" , reply . comment , TEXT_UTF_8 ) ;
if ( reply . resto > = 0 ) {
entity . addTextBody ( "name" , reply . name , TEXT_UTF_8 ) ;
entity . addTextBody ( "resto" , Integer . toString ( reply . resto ) ) ;
entity . addTextBody ( "email" , reply . email , TEXT_UTF_8 ) ;
}
if ( reply . spoilerImage ) {
entity . addTextBody ( "sub" , reply . subject , TEXT_UTF_8 ) ;
entity . addTextBody ( "spoiler" , "on" ) ;
entity . addTextBody ( "com" , reply . comment , TEXT_UTF_8 ) ;
}
entity . addTextBody ( "recaptcha_challenge_field" , reply . captchaChallenge ) ;
if ( reply . resto > = 0 ) {
entity . addTextBody ( "g-recaptcha-response" , reply . captchaResponse , TEXT_UTF_8 ) ;
entity . addTextBody ( "resto" , Integer . toString ( reply . resto ) ) ;
}
entity . addTextBody ( "mode" , "regist" ) ;
if ( reply . spoilerImage ) {
entity . addTextBody ( "pwd" , reply . password ) ;
entity . addTextBody ( "spoiler" , "on" ) ;
}
if ( reply . usePass ) {
entity . addTextBody ( "g-recaptcha-response" , captchaHash , TEXT_UTF_8 ) ;
httpPost . addHeader ( "Cookie" , "pass_id=" + reply . passId ) ;
}
if ( reply . file ! = null ) {
entity . addTextBody ( "mode" , "regist" ) ;
entity . addBinaryBody ( "upfile" , reply . file , ContentType . APPLICATION_OCTET_STREAM , reply . fileName ) ;
entity . addTextBody ( "pwd" , reply . password ) ;
}
httpPost . setEntity ( entity . build ( ) ) ;
if ( reply . usePass ) {
httpPost . addHeader ( "Cookie" , "pass_id=" + reply . passId ) ;
}
sendHttpPost ( httpPost , new HttpPostSendListener ( ) {
if ( reply . file ! = null ) {
@Override
entity . addBinaryBody ( "upfile" , reply . file , ContentType . APPLICATION_OCTET_STREAM , reply . fileName ) ;
public void onResponse ( String responseString , HttpClient client , HttpResponse response ) {
}
ReplyResponse e = new ReplyResponse ( ) ;
if ( responseString = = null ) {
httpPost . setEntity ( entity . build ( ) ) ;
e . isNetworkError = true ;
} else {
e . responseData = responseString ;
if ( responseString . contains ( "No file selected" ) ) {
sendHttpPost ( httpPost , new HttpPostSendListener ( ) {
e . isUserError = true ;
@Override
e . isFileError = true ;
public void onResponse ( String responseString , HttpClient client , HttpResponse response ) {
} else if ( responseString . contains ( "You forgot to solve the CAPTCHA" )
ReplyResponse e = new ReplyResponse ( ) ;
| | responseString . contains ( "You seem to have mistyped the CAPTCHA" ) ) {
e . isUserError = true ;
e . isCaptchaError = true ;
} else if ( responseString . toLowerCase ( Locale . ENGLISH ) . contains ( "post successful" ) ) {
e . isSuccessful = true ;
}
}
if ( e . isSuccessful ) {
if ( responseString = = null ) {
Matcher matcher = responsePattern . matcher ( e . responseData ) ;
e . isNetworkError = true ;
} else {
int threadNo = - 1 ;
e . responseData = responseString ;
int no = - 1 ;
if ( matcher . find ( ) ) {
if ( responseString . contains ( "No file selected" ) ) {
try {
e . isUserError = true ;
threadNo = Integer . parseInt ( matcher . group ( 1 ) ) ;
e . isFileError = true ;
no = Integer . parseInt ( matcher . group ( 2 ) ) ;
} else if ( responseString . contains ( "You forgot to solve the CAPTCHA" )
} catch ( NumberFormatException err ) {
| | responseString . contains ( "You seem to have mistyped the CAPTCHA" ) ) {
err . printStackTrace ( ) ;
e . isUserError = true ;
e . isCaptchaError = true ;
} else if ( responseString . toLowerCase ( Locale . ENGLISH ) . contains ( "post successful" ) ) {
e . isSuccessful = true ;
}
}
}
}
if ( threadNo > = 0 & & no > = 0 ) {
if ( e . isSuccessful ) {
SavedReply savedReply = new SavedReply ( ) ;
Matcher matcher = responsePattern . matcher ( e . responseData ) ;
savedReply . board = reply . board ;
savedReply . no = no ;
int threadNo = - 1 ;
savedReply . password = reply . password ;
int no = - 1 ;
if ( matcher . find ( ) ) {
try {
threadNo = Integer . parseInt ( matcher . group ( 1 ) ) ;
no = Integer . parseInt ( matcher . group ( 2 ) ) ;
} catch ( NumberFormatException err ) {
err . printStackTrace ( ) ;
}
}
ChanApplication . getDatabaseManager ( ) . saveReply ( savedReply ) ;
if ( threadNo > = 0 & & no > = 0 ) {
SavedReply savedReply = new SavedReply ( ) ;
savedReply . board = reply . board ;
savedReply . no = no ;
savedReply . password = reply . password ;
e . threadNo = threadNo ;
ChanApplication . getDatabaseManager ( ) . saveReply ( savedReply ) ;
e . no = no ;
} else {
Logger . w ( TAG , "No thread & no in the response" ) ;
}
}
listener . onResponse ( e ) ;
e . threadNo = threadNo ;
e . no = no ;
} else {
Logger . w ( TAG , "No thread & no in the response" ) ;
}
}
listener . onResponse ( e ) ;
}
} ) ;
}
}
} ) ;
} ;
getCaptchaHash ( captchaHashListener , reply . captchaChallenge , reply . captchaResponse ) ;
}
}
public static interface ReplyListener {
public static interface ReplyListener {