4444import org .jdownloader .gui .translate ._GUI ;
4545import org .jdownloader .plugins .accounts .AccountBuilderInterface ;
4646import org .jdownloader .plugins .components .XFileSharingProBasic ;
47- import org .jdownloader .plugins .components .config .XFSConfigSendCm ;
48- import org .jdownloader .plugins .components .config .XFSConfigSendCm .LoginMode ;
49- import org .jdownloader .plugins .config .PluginJsonConfig ;
50- import org .jdownloader .plugins .controller .LazyPlugin ;
5147import org .jdownloader .plugins .controller .LazyPlugin .FEATURE ;
5248import org .jdownloader .scripting .JavaScriptEngineFactory ;
5349
6258import jd .plugins .Account .AccountType ;
6359import jd .plugins .AccountInfo ;
6460import jd .plugins .AccountInvalidException ;
65- import jd .plugins .DefaultEditAccountPanelAPIKeyLogin ;
6661import jd .plugins .DownloadLink ;
6762import jd .plugins .DownloadLink .AvailableStatus ;
6863import jd .plugins .HostPlugin ;
6964import jd .plugins .LinkStatus ;
7065import jd .plugins .PluginException ;
7166
72- @ HostPlugin (revision = "$Revision: 51314 $" , interfaceVersion = 3 , names = {}, urls = {})
67+ @ HostPlugin (revision = "$Revision: 51323 $" , interfaceVersion = 3 , names = {}, urls = {})
7368public class SendCm extends XFileSharingProBasic {
7469 public SendCm (final PluginWrapper wrapper ) {
7570 super (wrapper );
@@ -79,21 +74,6 @@ public SendCm(final PluginWrapper wrapper) {
7974 private static final String PROPERTY_ACCOUNT_FORCE_API_LOGIN = "force_api_login" ;
8075 private static final String PROPERTY_ACCOUNT_FORCE_WEBSITE_LOGIN = "force_website_login" ;
8176
82- @ Override
83- public LazyPlugin .FEATURE [] getFeatures () {
84- if (DebugMode .TRUE_IN_IDE_ELSE_FALSE ) {
85- final List <LazyPlugin .FEATURE > ret = new ArrayList <LazyPlugin .FEATURE >();
86- if (requiresCookieLogin ()) {
87- ret .add (LazyPlugin .FEATURE .COOKIE_LOGIN_ONLY );
88- } else {
89- ret .add (LazyPlugin .FEATURE .COOKIE_LOGIN_OPTIONAL );
90- }
91- return ret .toArray (new LazyPlugin .FEATURE [0 ]);
92- } else {
93- return super .getFeatures ();
94- }
95- }
96-
9777 /**
9878 * DEV NOTES XfileSharingProBasic Version SEE SUPER-CLASS<br />
9979 * mods: See overridden functions<br />
@@ -381,7 +361,40 @@ protected String regexAPIKey(final Browser br) {
381361 public AccountInfo fetchAccountInfo (final Account account ) throws Exception {
382362 if (account .hasProperty (PROPERTY_ACCOUNT_FORCE_WEBSITE_LOGIN )) {
383363 return this .fetchAccountInfoWebsite (account );
384- } else if (this .enableAccountApiOnlyMode () || account .hasProperty (PROPERTY_ACCOUNT_FORCE_API_LOGIN )) {
364+ } else if (account .hasProperty (PROPERTY_ACCOUNT_FORCE_API_LOGIN )) {
365+ return this .fetchAccountInfoAPI (this .br , account );
366+ } else if (this .looksLikeValidAPIKey (account .getPass ())) {
367+ /* Auto mode e.g. headless login */
368+ try {
369+ logger .info ("Attempting API login" );
370+ /* Important for getAPIKeyFromAccount !! */
371+ account .setProperty (PROPERTY_ACCOUNT_apikey , account .getPass ());
372+ final AccountInfo ai_api = this .fetchAccountInfoAPI (this .br , account );
373+ /* API login successful -> Always login via API with this account in the future */
374+ account .setProperty (PROPERTY_ACCOUNT_FORCE_API_LOGIN , true );
375+ // account.setProperty(PROPERTY_ACCOUNT_apikey, account.getPass());
376+ return ai_api ;
377+ } catch (final InterruptedException ie1 ) {
378+ throw ie1 ;
379+ } catch (final Exception pe1 ) {
380+ logger .info ("API login failed -> Trying website login" );
381+ /* Remove property again as API login failed. */
382+ account .removeProperty (PROPERTY_ACCOUNT_apikey );
383+ try {
384+ final AccountInfo ai_website = this .fetchAccountInfoWebsite (account );
385+ /* Website login successful -> Always login via website with this account in the future */
386+ account .setProperty (PROPERTY_ACCOUNT_FORCE_WEBSITE_LOGIN , true );
387+ return ai_website ;
388+ } catch (final InterruptedException ie2 ) {
389+ throw ie2 ;
390+ } catch (final PluginException pe2 ) {
391+ logger .log (pe2 );
392+ logger .info ("API login and website login failed -> Throwing API login exception" );
393+ throw pe1 ;
394+ }
395+ }
396+ } else if (this .enableAccountApiOnlyMode ()) {
397+ /* This should never happen (for this particular plugin). */
385398 return this .fetchAccountInfoAPI (this .br , account );
386399 } else {
387400 return this .fetchAccountInfoWebsite (account );
@@ -470,14 +483,15 @@ protected AccountInfo fetchAccountInfoAPI(final Browser br, final Account accoun
470483 ai .setUsedSpace (SizeFormatter .getSize (storage_used_bytesO .toString ()));
471484 }
472485 }
473- if (this . enableAccountApiOnlyMode ( ) && !account . hasProperty ( PROPERTY_ACCOUNT_FORCE_WEBSITE_LOGIN ) && !StringUtils .isEmpty ( email )) {
486+ if (! account . hasProperty ( PROPERTY_ACCOUNT_FORCE_WEBSITE_LOGIN ) && !StringUtils . isEmpty ( email ) && !StringUtils .equals ( account . getUser (), email )) {
474487 /*
475488 * Each account is unique. Do not care what the user entered - trust what API returns! </br> This is not really important - more
476489 * visually so that something that makes sense is displayed to the user in his account managers' "Username" column!
477490 */
491+ logger .info ("User has entered API key into username field -> Correcting username to email: " + email );
478492 account .setUser (email );
479493 } else if (StringUtils .equals (account .getUser (), this .getAPIKeyFromAccount (account ))) {
480- logger .info ("User has entered API key as username & password -> Set email as username" );
494+ logger .info ("User has entered API key as username & password -> Set email as username: " + email );
481495 account .setUser (email );
482496 }
483497 if (DebugMode .TRUE_IN_IDE_ELSE_FALSE ) {
@@ -542,17 +556,6 @@ protected boolean supportsAPIMassLinkcheck() {
542556 }
543557 }
544558
545- @ Override
546- protected boolean enableAccountApiOnlyMode () {
547- // TODO: Try to remove this override
548- final XFSConfigSendCm cfg = PluginJsonConfig .get (XFSConfigSendCm .class );
549- if (cfg .getLoginMode () == LoginMode .API ) {
550- return true ;
551- } else {
552- return false ;
553- }
554- }
555-
556559 @ Override
557560 protected boolean supportsAPISingleLinkcheck () {
558561 return looksLikeValidAPIKey (this .getAPIKey ());
@@ -588,7 +591,7 @@ public boolean massLinkcheckerAPI(final DownloadLink[] urls, final String apikey
588591 index ++;
589592 }
590593 }
591- final ArrayList <DownloadLink > apiLinkcheckLinks = new ArrayList <DownloadLink >();
594+ final List <DownloadLink > apiLinkcheckLinks = new ArrayList <DownloadLink >();
592595 sb .delete (0 , sb .capacity ());
593596 for (final DownloadLink link : links ) {
594597 try {
@@ -730,24 +733,19 @@ public boolean massLinkcheckerAPI(final DownloadLink[] urls, final String apikey
730733 }
731734
732735 @ Override
733- public Class <? extends XFSConfigSendCm > getConfigInterface () {
734- return XFSConfigSendCm .class ;
736+ protected boolean looksLikeValidAPIKey (final String str ) {
737+ if (str == null ) {
738+ return false ;
739+ } else if (str .matches ("^[a-z0-9]{20,}$" )) {
740+ return true ;
741+ } else {
742+ return false ;
743+ }
735744 }
736745
737746 @ Override
738747 public AccountBuilderInterface getAccountFactory (final InputChangedCallbackInterface callback ) {
739- if (DebugMode .TRUE_IN_IDE_ELSE_FALSE ) {
740- /* 2025-08-07: Testing */
741- return new SendCmAccountFactory (callback , this );
742- } else {
743- /* Website login */
744- final XFSConfigSendCm cfg = PluginJsonConfig .get (XFSConfigSendCm .class );
745- if (cfg .getLoginMode () == LoginMode .API || cfg .getLoginMode () == LoginMode .AUTO || cfg .getLoginMode () == LoginMode .DEFAULT ) {
746- return new DefaultEditAccountPanelAPIKeyLogin (callback , this );
747- } else {
748- return super .getAccountFactory (callback );
749- }
750- }
748+ return new SendCmAccountFactory (callback , this );
751749 }
752750
753751 public static class SendCmAccountFactory extends MigPanel implements AccountBuilderInterface {
@@ -1014,23 +1012,14 @@ public boolean validateInputs() {
10141012
10151013 @ Override
10161014 public Account getAccount () {
1017- if (plg .looksLikeValidAPIKey (this .getApikey ())) {
1015+ final String apikey = this .getApikey ();
1016+ if (plg .looksLikeValidAPIKey (apikey )) {
10181017 /* Use API key as password */
1019- final String apikey = this .getApikey ();
10201018 final Account account = new Account (getUsername (), apikey );
10211019 account .setProperty (PROPERTY_ACCOUNT_apikey , apikey );
10221020 account .setProperty (PROPERTY_ACCOUNT_FORCE_API_LOGIN , true );
10231021 return account ;
10241022 } else {
1025- /**
1026- * Workaround for users who set this setting to API but login via website/free-account <br>
1027- * The API login mode is still needed for headless installations! <br>
1028- *
1029- * This workaround is required so that PluginForHost.validateLogins will not fail due to API key validation when
1030- * enableAccountApiOnlyMode() returns true.
1031- */
1032- final XFSConfigSendCm cfg = PluginJsonConfig .get (XFSConfigSendCm .class );
1033- cfg .setLoginMode (LoginMode .DEFAULT );
10341023 final Account account = new Account (getUsername (), getPassword ());
10351024 account .setProperty (PROPERTY_ACCOUNT_FORCE_WEBSITE_LOGIN , true );
10361025 return account ;
0 commit comments