home *** CD-ROM | disk | FTP | other *** search
- /***** BEGIN LICENSE BLOCK *****
- - Version: MPL 1.1/GPL 2.0/LGPL 2.1
- -
- - The contents of this file are subject to the Mozilla Public License Version
- - 1.1 (the "License"); you may not use this file except in compliance with
- - the License. You may obtain a copy of the License at
- - http://www.mozilla.org/MPL/
- -
- - Software distributed under the License is distributed on an "AS IS" basis,
- - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- - for the specific language governing rights and limitations under the
- - License.
- -
- - The Original Code is "FlashGot".
- -
- - The Initial Developer of the Original Code is Giorgio Maone.
- - Portions created by the Initial Developer are Copyright (C) 2004
- - the Initial Developer. All Rights Reserved.
- -
- - Contributor(s): Giorgio Maone <g.maone @ informaction.com>
- -
- - Alternatively, the contents of this file may be used under the terms of
- - either the GNU General Public License Version 2 or later (the "GPL"), or
- - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- - in which case the provisions of the GPL or the LGPL are applicable instead
- - of those above. If you wish to allow use of your version of this file only
- - under the terms of either the GPL or the LGPL, and not to allow others to
- - use your version of this file under the terms of the MPL, indicate your
- - decision by deleting the provisions above and replace them with the notice
- - and other provisions required by the LGPL or the GPL. If you do not delete
- - the provisions above, a recipient may use your version of this file under
- - the terms of any one of the MPL, the GPL or the LGPL.
- -
- - ***** END LICENSE BLOCK *****/
-
- #include "stdafx.h"
- #include <sys/stat.h>
- #include <comdef.h>
- #include <atlbase.h>
- #include <atlcom.h>
- #include <objbase.h>
-
- #define BUF_SIZE 1024
-
- char g_buf[BUF_SIZE];
- wchar_t g_wbuf[BUF_SIZE];
-
-
- // [START DOWNLOAD MANAGER SUPPORT CLASSES]
-
- using namespace std;
- class DMSupport
- {
-
- protected:
-
- static BOOL createProcess(char *commandLine, PROCESS_INFORMATION *pi) {
- STARTUPINFO si;
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
-
- PROCESS_INFORMATION *mypi=NULL;
- if(!pi) {
- pi=mypi=new PROCESS_INFORMATION;
- }
- ZeroMemory(pi, sizeof(pi) );
-
-
- BOOL ret=CreateProcess( NULL,
- commandLine, // Command line.
- NULL, // Process handle not inheritable.
- NULL, // Thread handle not inheritable.
- FALSE, // Set handle inheritance to FALSE.
- 0, // No creation flags.
- NULL, // Use parent's environment block.
- NULL, // Use parent's starting directory.
- &si, // Pointer to STARTUPINFO structure.
- pi ) // Pointer to PROCESS_INFORMATION structure.
- ;
-
- if(mypi) {
- closeProcess(mypi);
- delete mypi;
- }
-
- return ret;
- }
-
- static void closeProcess(PROCESS_INFORMATION *pi) {
- CloseHandle( pi->hProcess );
- CloseHandle( pi->hThread );
- }
-
-
- public:
- enum OpType { OP_ONE=0, OP_SEL=1, OP_ALL=2, OP_MIN=OP_ONE, OP_MAX=OP_ALL };
-
- virtual void check(void) = 0;
- virtual void dispatch(const int linksCount, const int linesCount, const bstr_t* parm, const OpType opType ) = 0;
- virtual const char *getName(void) = 0;
-
-
- };
-
-
-
- class DMSupportCOM :
- public DMSupport
- {
-
- #define COMCALL(Call) if(hr=FAILED(Call)) throw _com_error(hr);
-
- private:
- HRESULT hr;
- CComDispatchDriver comObj;
- CComPtr<IDispatch> lpTDispatch;
-
- protected:
-
-
- void prepareCOMObj() {
- if(!comObj) {
- COMCALL(lpTDispatch.CoCreateInstance(_bstr_t(getProgId())));
- comObj=lpTDispatch;
- }
- }
-
- static SAFEARRAY *createSafeArray(int elemCount)
- {
- SAFEARRAYBOUND bounds [1];
- bounds[0].cElements=elemCount;
- bounds[0].lLbound=0;
- return SafeArrayCreate(VT_VARIANT, 1, bounds);
- }
-
- virtual const char * getProgId() = 0;
-
- void getMemberID(char *memberName, DISPID *dispid) {
- USES_CONVERSION;
- OLECHAR FAR* oleMember=A2OLE(memberName);
- prepareCOMObj();
- COMCALL(comObj->GetIDsOfNames(IID_NULL, &oleMember, 1, LOCALE_SYSTEM_DEFAULT, dispid));
- }
-
- void invoke(DISPID *dispid, VARIANT *parms, const unsigned int parmsCount) {
- prepareCOMObj();
- COMCALL(comObj.InvokeN(*dispid,parms,parmsCount,NULL));
- }
-
- void invoke(char *memberName, VARIANT *parms, const unsigned int parmsCount) {
- DISPID dispid;
- getMemberID(memberName,&dispid);
- invoke(&dispid,parms,parmsCount);
- }
-
- void putProperty(char *propertyName, VARIANT val) {
- USES_CONVERSION;
- LPCOLESTR oleMember=A2COLE(propertyName);
- prepareCOMObj();
- COMCALL(comObj.PutPropertyByName(oleMember, &val));
- }
-
- public:
-
- void check()
- {
- CLSID clsid;
- COMCALL(CLSIDFromProgID(_bstr_t(getProgId()),&clsid));
- }
-
-
- };
-
-
-
-
- class DMSFlashGet :
- public DMSupportCOM
- {
-
-
- protected:
-
- const char * getProgId() { return "JetCar.Netscape"; }
-
- public:
-
- const char * getName() { return "FlashGet"; }
-
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- int elemCount=linksCount * 2 + 1; // linksCount * 2 (url,info)
- SAFEARRAY *psa=createSafeArray(elemCount);
-
- VARIANT v;
- v.vt = VT_BSTR;
- long ix[]={0};
- long j=0, fetchCount=3;
- for (; ix[0] < elemCount ; ix[0]++) {
- v.bstrVal=parms[j];
- SafeArrayPutElement(psa, ix, &v);
- // skip cookie and postData
- if(--fetchCount) {
- ++j;
- } else {
- j+=3;
- fetchCount=2;
- }
- }
-
-
- v.vt = VT_ARRAY | VT_VARIANT | VT_BYREF; // VBScript Array
- v.pparray=&psa;
-
- invoke("AddUrlList",&v,1);
- SafeArrayDestroy(psa);
- }
- };
-
-
- class DMSGetRight :
- public DMSupport
- {
-
- private:
- static char *findGetRight(HKEY baseKey, char *leafPath) {
- long res;
- HKEY hk;
- char *path=NULL;
-
- if( (res=RegOpenKeyEx(baseKey,leafPath,0,KEY_QUERY_VALUE,&hk))==ERROR_SUCCESS)
- {
-
-
- char *installKey="InstallDir";
- long pathLen=0;
- if((res=RegQueryValueEx(hk,installKey,0,NULL,NULL,(LPDWORD)&pathLen))==ERROR_SUCCESS)
- {
- char *exeLeaf="\\getright.exe";
- path=new char[pathLen+strlen(exeLeaf)];
- BOOL fileExists=FALSE;
- if((res=RegQueryValueEx(hk,installKey,0,NULL,(LPBYTE)path,(LPDWORD)&pathLen))==ERROR_SUCCESS)
- {
- strcat(path,exeLeaf);
- struct stat statbuf;
- fileExists=!stat(path,&statbuf);
- }
- if(!fileExists) {
- delete path;
- path=NULL;
- }
- }
- RegCloseKey(hk);
- }
-
- return path;
-
- }
-
- static char *findGetRight()
- {
- char *path;
- if( ( path=findGetRight(HKEY_CURRENT_USER,"Software\\Headlight\\GetRight\\Config") )
- || ( path=findGetRight(HKEY_LOCAL_MACHINE,"Software\\Headlight\\GetRight") ) )
- {
- return path;
- }
- throw "Can't find GetRight executable";
- }
-
- static char *createCmdLine(char *path, char *arg) {
- char *cmdLine=new char[strlen(path)+strlen(arg)+5];
- sprintf(cmdLine,"\"%s\" %s",path,arg);
- return cmdLine;
- }
-
- public:
-
- const char * getName() { return "GetRight"; }
-
- void check()
- {
- delete findGetRight();
- }
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- char *cmdLine=NULL;
- char *path=NULL;
- BOOL ret=createProcess(cmdLine=createCmdLine(path=findGetRight(),(char *)parms[linksCount?1:0]),NULL);
- if(cmdLine) {
- delete cmdLine;
- }
- if(path) delete path;
- if(!ret) throw "Can't launch GetRight";
- }
- };
-
-
- class DMSLeechGet :
- public DMSupportCOM
- {
- protected:
- void invoke(char *memberName,_bstr_t parm) {
- VARIANT v;
- v.vt=VT_BSTR;
- v.bstrVal=parm;
- DMSupportCOM::invoke(memberName,&v,1);
- }
-
- public:
-
-
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- if(linksCount>0)
- {
- this->invoke("Wizard",parms[1]);
- }
- else
- {
- this->invoke("Parse",parms[0]);
- }
- }
- };
-
- class DMSLeechGet2002 :
- public DMSLeechGet
- {
-
- protected:
- const char * getProgId()
- {
- return "LeechGetIE.AddURL";
- }
- public:
- const char * getName() { return "LeechGet 2002"; }
- };
-
- class DMSLeechGet2004 :
- public DMSLeechGet
- {
- protected:
- const char * getProgId()
- {
- return "LeechGetIE.LeechIE";
- }
- public:
- const char * getName() { return "LeechGet 2004"; }
- };
-
- class DMSNet_Transport :
- public DMSupportCOM
- {
-
- protected:
-
- const char * getProgId() { return "NTIEHelper.NTIEAddUrl"; }
-
- public:
- const char * getName() { return "Net Transport"; }
-
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- SAFEARRAY *psaa[2]=
- {
- createSafeArray(linksCount),
- createSafeArray(linksCount)
- };
-
-
- VARIANT vstr;
- vstr.vt = VT_BSTR;
- long ix[]={0};
- for(int j=1; ix[0]<linksCount; ix[0]++)
- {
- for(int a=0; a<2; a++)
- {
- vstr.bstrVal=parms[j++];
- SafeArrayPutElement(psaa[a], ix, &vstr);
- }
- j+=2; // skipping cookie & postData;
- }
-
- //AddList(referrer,urls, remarks);
- VARIANT v[3];
- v[2].vt=VT_BSTR;
- v[2].bstrVal=parms[0];
- v[1].vt=v[0].vt=VT_VARIANT | VT_BYREF ;
- SafeArrayAccessData(psaa[0], (void **)&(v[1].pvarVal));
- SafeArrayAccessData(psaa[1], (void **)&(v[0].pparray));
- invoke("AddList",v,3);
-
- SafeArrayUnlock(psaa[1]);
- SafeArrayUnlock(psaa[0]);
- SafeArrayDestroy(psaa[0]);
- SafeArrayDestroy(psaa[1]);
- }
- };
-
- class DMSWestByte :
- public DMSupportCOM
- {
-
- public:
-
-
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- if(linksCount>0)
- {
-
- VARIANT v[2];
- v[0].vt = VT_BSTR ;
- v[0].bstrVal=parms[0]; // referer
-
- if(linksCount==1)
- {
- v[1].vt = VT_BSTR; // VBScript Array
- v[1].bstrVal=parms[1];
- invoke("AddURL",v,2);
- }
- else
- {
-
- int elemCount=linksCount * 2;
-
-
- SAFEARRAY *psa=createSafeArray(elemCount);
-
- VARIANT vstr;
- vstr.vt = VT_BSTR;
- long ix[]={0};
- long j=1,fetchCount=2;
-
- for (; ix[0] < elemCount; ix[0]++) {
- vstr.bstrVal=parms[j];
- SafeArrayPutElement(psa, ix, &vstr);
- // skip cookie and postData
- if(--fetchCount) {
- ++j;
- } else {
- j+=3;
- fetchCount=2;
- }
- }
-
-
- v[1].vt = VT_ARRAY | VT_VARIANT | VT_BYREF; // VBScript Array
- v[1].pparray=&psa;
-
-
-
- invoke("AddURLs",v,2);
-
- SafeArrayDestroy(psa);
- }
- }
- }
-
- };
-
- class DMSDownloadMaster :
- public DMSWestByte
- {
- protected:
- const char * getProgId() { return "DMIE.MoveURL"; }
- public:
- const char * getName() { return "Download Master"; }
- };
-
- class DMSInternetDownloadAccelerator :
- public DMSWestByte
- {
- protected:
- const char * getProgId() { return "IDAIE.MoveURLIDA"; }
- public:
- const char * getName() { return "Internet Download Accelerator"; }
- };
-
-
-
- #import "progid:WG.WGUrlListReceiver.1"
- using namespace DataStretcherLib;
- class DMSFreeDownloadManager :
- public DMSupportCOM
- {
-
- protected:
-
- const char * getProgId() { return "WG.WGUrlListReceiver"; }
-
- public:
-
- const char * getName() { return "Free Download Manager"; }
-
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- if(linksCount>0)
- {
- if(linksCount<2)
- {
- IWGUrlReceiverPtr fdm(__uuidof(WGUrlReceiver));
- fdm->PutReferer(parms[0]);
- fdm->PutUrl(parms[1]);
- fdm->PutComment(parms[2]);
- fdm->AddDownload();
- }
- else
- {
- IWGUrlListReceiverPtr fdm(__uuidof(WGUrlListReceiver));
- fdm->PutReferer(parms[0]);
- int elemCount=linksCount*4;
- for(int j=1; j<=elemCount; j+=3) { // why j+=3? because we're skipping cookie & postData
- fdm->PutUrl(parms[j++]);
- fdm->PutComment(parms[j]);
- fdm->AddUrlToList();
- }
- fdm->ShowAddUrlListDialog();
- }
- }
- }
-
- };
-
- class DMSReGet :
- public DMSupportCOM
- {
-
-
- public:
-
-
- void dispatch(const int linksCount, const int linesCount, const bstr_t* parms, const OpType opType )
- {
- BOOL multiple=linksCount>1;
- DISPID di_AddDownload;
- DMSupportCOM::getMemberID(multiple?"AddDownloadToList":"AddDownload",&di_AddDownload);
- int elemCount=linksCount*4;
- VARIANT v[5];
- v[0].vt=v[1].vt=v[2].vt=v[3].vt=v[4].vt=VT_BSTR;
-
- v[3].bstrVal=parms[0]; // referer
- for(int j=1; j<=elemCount;)
- {
- v[4].bstrVal=parms[j++]; // URL
- v[1].bstrVal=parms[j++]; // info
- v[2].bstrVal=parms[j++]; // cookie
- v[0].bstrVal=parms[j++]; // postData
- if(!SysStringLen(v[0].bstrVal)) v[0].bstrVal=NULL;
- for(int attempts=120; attempts-->0;)
- { // we give ReGet 2min to wake up
- try
- {
- if(!multiple) {
- putProperty("Url",v[4]);
- putProperty("Referer",v[3]);
- putProperty("Cookie",v[2]);
- putProperty("Info",v[1]);
- putProperty("PostData",v[0]);
- DMSupportCOM::invoke(&di_AddDownload,NULL,0);
- } else {
- DMSupportCOM::invoke(&di_AddDownload,v,5);
- }
- break;
- }
- catch(_com_error ce)
- {
- Sleep(1000);
- }
- }
- }
-
- if(multiple) try
- {
- DMSupportCOM::invoke("FlushDownloadList",v,0);
- } catch(...) {}
- }
- };
-
- class DMSReGetJr :
- public DMSReGet
- {
-
- protected:
- const char * getProgId()
- {
- return "ReGetJr.RegetDownloadApi";
- }
- public:
- const char * getName() { return "ReGet Junior"; }
- };
-
- class DMSReGetDx :
- public DMSReGet
- {
-
- protected:
- const char * getProgId()
- {
- return "ReGetDx.RegetDownloadApi";
- }
- public:
- const char * getName() { return "ReGet Deluxe"; }
- };
-
- class DMSReGetPro :
- public DMSReGet
- {
-
- protected:
- const char * getProgId()
- {
- return "ReGetPro.RegetDownloadApi";
- }
- public:
- const char * getName() { return "ReGet Pro"; }
- };
-
-
- // [END DOWNLOAD MANAGER SUPPORT CLASSES]
-
-
- typedef struct _DMSNode {
- unsigned int id;
- DMSupport *dms;
- _DMSNode *prev;
- } DMSNode;
-
-
-
- class DMSFactory
- {
- private:
- static DMSFactory *instance;
- DMSNode *last;
- DMSupport *add(DMSupport *dms) {
- if(!last) {
- (last=new DMSNode())->prev=NULL;
- last->id=1;
- } else {
- DMSNode *newNode=new DMSNode();
- newNode->prev=last;
- newNode->id=last->id << 1;
- last=newNode;
- }
- return last->dms=dms;
- }
-
- DMSFactory() : last(NULL) {
- add(new DMSDownloadMaster());
- add(new DMSFlashGet());
- add(new DMSFreeDownloadManager());
- add(new DMSGetRight());
- add(new DMSInternetDownloadAccelerator());
- add(new DMSLeechGet2002());
- add(new DMSLeechGet2004());
- add(new DMSNet_Transport());
- add(new DMSReGetDx());
- add(new DMSReGetJr());
- add(new DMSReGetPro());
- }
-
- public:
-
- static DMSFactory *getInstance() {
- return instance?instance:instance=new DMSFactory();
- }
-
-
- DMSupport *getDMS(char *name) {
- DMSNode *cursor=last;
- for(; cursor && strcmp(cursor->dms->getName(),name); cursor=cursor->prev);
- return cursor
- ?cursor->dms
- :NULL;
- }
-
- unsigned int checkAll() {
- unsigned int retVal=0;
- CoInitialize(NULL);
- for(DMSNode *cursor=last; cursor; cursor=cursor->prev) {
- try {
- fprintf(stdout,"%s|",cursor->dms->getName());
- cursor->dms->check();
- fprintf(stdout,"OK\n");
- continue;
- } catch(_com_error ce) {
- fprintf(stdout,"BAD\nCOM error: %s\n", ce.ErrorMessage());
- } catch(...) {
- fprintf(stdout,"BAD\nunexpected unknown error!\n");
- }
- retVal |= cursor->id;
- }
- CoUninitialize();
- return retVal;
- }
-
-
-
- ~DMSFactory() {
- DMSNode *cursor=last;
- while( cursor ) {
- delete cursor->dms;
- last=cursor;
- cursor=last->prev;
- delete last;
- }
- }
- };
- DMSFactory * DMSFactory::instance=NULL;
-
-
-
-
- char* readLine(FILE *stream, char *buf)
- {
-
- char *res=fgets(buf,BUF_SIZE,stream);
- if(res)
- {
- size_t lastPos=strlen(res)-1;
- while(lastPos>=0 &&
- (res[lastPos]==0x0a || res[lastPos]==0x0d)
- ) res[lastPos--]='\0';
- }
- return res;
- }
-
-
- void fail(char *msg, int code)
- {
- MessageBox(NULL, msg,
- "FlashGot error",
- MB_OK | MB_ICONERROR);
- exit(code);
- }
-
-
- DMSupport* createDMS(char *name) {
- DMSupport *res=DMSFactory::getInstance()->getDMS(name);
- if(res) return res;
- sprintf(g_buf,"Unsupported Download Manager %s", name);
- fail(g_buf,-8000);
- return NULL;
- }
-
- wchar_t *UTF8toUnicode(char *buf) {
- return buf && (
- MultiByteToWideChar(CP_UTF8,0,buf,-1,g_wbuf,BUF_SIZE)
- || MultiByteToWideChar(CP_ACP,0,buf,-1,g_wbuf,BUF_SIZE))
- ? g_wbuf : L""
- ;
- }
-
- int main(int argc, char* argv[])
- {
- char *fname;
- FILE *f;
-
-
- if(argc<2 || strcmp(argv[1],"-o") == 0 )
- {
- if(argc>=2)
- {
- freopen(argv[2],"w", stdout);
- freopen(argv[2],"a", stderr);
- }
- exit(DMSFactory::getInstance()->checkAll());
-
- }
- else if(!(f=fopen(fname=argv[1],"rb")))
- {
- sprintf(g_buf,"Can't open file %s",fname);
- fail(g_buf,0x01000);
- }
- else
- {
- if(!feof(f))
- {
-
- CoInitialize(NULL);
- DMSupport *dms = NULL;
- const char *errMsg="Download manager not properly installed.\n%s";
- BOOL done=FALSE;
- try
- {
- int linksCount=atoi(readLine(f,g_buf));
- int linesCount=linksCount*4+1; // referer + (url + info + cookie + postdata) * 4
-
-
- char *header[3];
- char header_buf[BUF_SIZE];
- strcpy(header_buf,g_buf);
- char *cur=header_buf;
- for(int j=0; j<3 ; j++ ) {
- if(cur && (cur=strchr(cur,';')) ) {
- cur[0]='\0';
- header[j]=++cur;
- } else {
- header[j]=NULL;
- }
- }
-
- char *dmName=header[0]?header[0]:"FlashGet";
- int typeId=header[1]?atoi(header[1]) : DMSupport::OP_ALL;
-
- if(typeId<DMSupport::OP_MIN||typeId>DMSupport::OP_MAX) {
- typeId=DMSupport::OP_ALL;
- }
-
- bstr_t *parms =new bstr_t[linesCount];
-
- for(int j=0; j<linesCount; j++) {
- parms[j]=bstr_t(UTF8toUnicode(readLine(f,g_buf)));
- }
-
- (dms=createDMS(dmName))->dispatch(linksCount,linesCount,parms,(DMSupport::OpType)typeId);
- done=TRUE;
- }
- catch(_com_error ce)
- {
- sprintf(g_buf,errMsg,ce.ErrorMessage());
- }
- catch(char *ex) {
- sprintf(g_buf,errMsg,ex);
- }
- catch(...) {
- sprintf(g_buf,errMsg,"");
- }
- if(dms) delete dms;
- CoUninitialize();
- if(!done) fail(g_buf,0x02000);
- }
-
- else
- {
- sprintf(g_buf,"Temporary file %s is empty",fname);
- fail(g_buf,0x03000);
- }
- fclose(f);
- //remove(fname);
- }
- return 0;
- }
-
- int WINAPI
- WinMain(HINSTANCE inst, HINSTANCE previnst, LPSTR cmdline, int cmdshow)
- {
- return main(__argc, __argv);
- }
-