00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "fsMirrorURLsMgr.h"
00008 #include "inetutil.h"
00009
00010 #ifdef _DEBUG
00011 #undef THIS_FILE
00012 static char THIS_FILE[]=__FILE__;
00013 #define new DEBUG_NEW
00014 #endif
00015
00016 fsMirrorURLsMgr::fsMirrorURLsMgr()
00017 {
00018 m_dldr.SetEventFunc (_DldrEvents, this);
00019 }
00020
00021 fsMirrorURLsMgr::~fsMirrorURLsMgr()
00022 {
00023
00024 }
00025
00026 void fsMirrorURLsMgr::Initialize(LPCSTR pszFileName, UINT64 uSize, LPCSTR pszBaseServer, fsInternetSession* pSession)
00027 {
00028 LOG ("initializing MirrorURLsMgr..." << nl);
00029 LOG ("file: " << pszFileName << nl);
00030 LOG ("size: " << uSize << nl);
00031 LOG ("base server: " << pszBaseServer << nl);
00032 LOG ("session: " << LONG (pSession) << nl);
00033
00034 m_strFile = pszFileName;
00035 m_uSize = uSize;
00036 m_strBaseServer = pszBaseServer;
00037 m_dldr.Initialize (pSession);
00038
00039 LOG ("ok." << nl);
00040 }
00041
00042 void fsMirrorURLsMgr::Set_SearchURL(LPCSTR pszUrl)
00043 {
00044 m_strSearchURL = pszUrl;
00045 LOG ("search url was set: " << pszUrl << nl);
00046 }
00047
00048 int fsMirrorURLsMgr::Get_MirrorURLCount()
00049 {
00050 return m_vMirrorURLs.size ();
00051 }
00052
00053 LPCSTR fsMirrorURLsMgr::Get_MirrorURL(int iIndex)
00054 {
00055 return m_vMirrorURLs [iIndex];
00056 }
00057
00058 fsInternetResult fsMirrorURLsMgr::SearchForMirrors()
00059 {
00060 fsInternetResult ir;
00061
00062 LOG ("starting searching for mirrors..." << nl);
00063
00064 CString strURL = m_strSearchURL;
00065
00066 strURL.Replace ("%file%", m_strFile);
00067
00068 if (m_uSize != _UI64_MAX)
00069 {
00070 CString strSize;
00071 strSize.Format ("%I64u", m_uSize);
00072 strURL.Replace ("%size%", strSize);
00073 }
00074 else
00075 strURL.Replace ("%size%", "");
00076
00077 LOG ("resulting url for script is: " << strURL << nl);
00078
00079 m_bAbort = FALSE;
00080
00081 LOG ("calling script on server..." << nl);
00082
00083 ir = m_dldr.Download (strURL);
00084 if (ir != IR_SUCCESS)
00085 {
00086 LOG ("error occured while retreiving search results: " << LONG (ir)
00087 << "; GetLastError is: " << GetLastError () << nl);
00088 return ir;
00089 }
00090
00091 LOG ("script called successfully" << nl);
00092
00093 return OnSearchScriptResultsReceived ();
00094 }
00095
00096 void fsMirrorURLsMgr::_DldrEvents(fsInternetURLFileDownloaderEvent ev, LPVOID lp)
00097 {
00098 fsMirrorURLsMgr* pThis = (fsMirrorURLsMgr*) lp;
00099
00100 switch (ev)
00101 {
00102 case UFDE_CONNECTING:
00103 LOG ("connecting to search server..." << nl);
00104 pThis->Event (MUME_CONNECTINGSEARCHSERVER);
00105 break;
00106
00107 case UFDE_DOWNLOADING:
00108 LOG ("downloading search results..." << nl);
00109 pThis->Event (MUME_RETREIVINGSEARCHRESULTS);
00110 break;
00111 }
00112 }
00113
00114 void fsMirrorURLsMgr::Set_EventFunc(fntMirrorURLsMgrEvents pfn, LPVOID lp)
00115 {
00116 m_pfnEvents = pfn;
00117 m_lpEvParam = lp;
00118 }
00119
00120 void fsMirrorURLsMgr::Event(fsMirrorURLsMgrEvent ev)
00121 {
00122 if (m_pfnEvents)
00123 m_pfnEvents (ev, m_lpEvParam);
00124 }
00125
00126 fsInternetResult fsMirrorURLsMgr::OnSearchScriptResultsReceived()
00127 {
00128 fsHTMLParser parser;
00129
00130 LOG ("processing search results..." << nl);
00131
00132 parser.SetKillDupes (TRUE);
00133
00134 LOG ("parsing HTML...");
00135
00136 parser.ParseHTML (LPSTR (m_dldr.Get_FileBuffer ()));
00137
00138 LOG ("ok." << nl);
00139
00140 LOG ("initializing list of mirrors...");
00141
00142 m_vMirrorURLs.clear ();
00143
00144 LOG ("ok." << nl);
00145 LOG ("adding mirrors..." << nl);
00146
00147 for (int i = 0; i < parser.GetUrlCount () && m_bAbort == FALSE; i++)
00148 {
00149 fsURL url;
00150 LPCSTR pszUrl = parser.GetUrl (i);
00151
00152 if (IR_SUCCESS != url.Crack (pszUrl))
00153 continue;
00154
00155 if (fsIsServersEqual (url.GetHostName (), m_strBaseServer, TRUE) ||
00156 fsIsServersEqual (m_strBaseServer, url.GetHostName (), TRUE) )
00157 continue;
00158
00159 char szFileName [10000];
00160 fsFileNameFromUrlPath (url.GetPath (), url.GetInternetScheme () == INTERNET_SCHEME_FTP,
00161 TRUE, szFileName, sizeof (szFileName));
00162
00163 if (stricmp (szFileName, m_strFile) == 0)
00164 {
00165 if (IsMirrorURLGood (pszUrl) == FALSE)
00166 continue;
00167
00168 LOG ("adding mirror: " << pszUrl << nl);
00169
00170 m_vMirrorURLs.add (pszUrl);
00171 }
00172 }
00173
00174 LOG ("done adding mirrors. freing resources...");
00175
00176 m_dldr.Free_FileBuffer ();
00177
00178 LOG ("ok." << nl);
00179
00180 if (m_bAbort)
00181 return IR_S_FALSE;
00182
00183 Event (MUME_DONE);
00184 return IR_SUCCESS;
00185 }
00186
00187 BOOL fsMirrorURLsMgr::IsMirrorURLGood(LPCSTR )
00188 {
00189 return TRUE;
00190 }
00191
00192 void fsMirrorURLsMgr::Abort()
00193 {
00194 m_bAbort = TRUE;
00195 m_dldr.Abort ();
00196 }
00197
00198 fsMirrorURLsMgr_FileMirrorsDotCom::fsMirrorURLsMgr_FileMirrorsDotCom()
00199 {
00200 Set_SearchURL (0);
00201 }
00202
00203 fsMirrorURLsMgr_FileMirrorsDotCom::~fsMirrorURLsMgr_FileMirrorsDotCom()
00204 {
00205
00206 }
00207
00208 void fsMirrorURLsMgr_FileMirrorsDotCom::Set_SearchURL(int nURL)
00209 {
00210 static LPCSTR _ppszURLs [] = {
00211 "http://www.filesearching.com/cgi-bin/s?q=%file%&w=a&t=f&e=on&m=20&o=n&s1=%size%&s2=%size%&d=&p=&p2=&x=28&y=14",
00212 "http://findfiles.com/list.php?string=%file%&db=Mirrors&size=%size%",
00213 };
00214
00215 if (nURL < sizeof (_ppszURLs) / sizeof (LPCSTR))
00216 fsMirrorURLsMgr::Set_SearchURL (_ppszURLs [nURL]);
00217 }