Updated init script.
[dtbartle/bnbt.git] / tracker_torrent.cpp
1 /***\r
2 *\r
3 * BNBT Beta 8.0 - A C++ BitTorrent Tracker\r
4 * Copyright (C) 2003-2004 Trevor Hogan\r
5 *\r
6 * CBTT variations (C) 2003-2005 Harold Feit\r
7 *\r
8 * This library is free software; you can redistribute it and/or\r
9 * modify it under the terms of the GNU Lesser General Public\r
10 * License as published by the Free Software Foundation; either\r
11 * version 2.1 of the License, or (at your option) any later version.\r
12 *\r
13 * This library is distributed in the hope that it will be useful,\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
16 * Lesser General Public License for more details.\r
17 *\r
18 * You should have received a copy of the GNU Lesser General Public\r
19 * License along with this library; if not, write to the Free Software\r
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
21 *\r
22 ***/\r
23 \r
24 #include "bnbt.h"\r
25 #include "atom.h"\r
26 #include "bencode.h"\r
27 #include "tracker.h"\r
28 #include "util.h"\r
29 \r
30 void CTracker :: serverResponseTorrent( struct request_t *pRequest, struct response_t *pResponse, user_t user )\r
31 {\r
32         if( !m_pAllowed || !m_bAllowTorrentDownloads || !( user.iAccess & ACCESS_DL ) )\r
33         {\r
34                 pResponse->strCode = "403 Forbidden";\r
35 \r
36                 return;\r
37         }\r
38 \r
39         CAtom *pTorrent = m_pAllowed->getItem( UTIL_StringToHash( pRequest->mapParams["info_hash"] ) );\r
40 \r
41         if( pTorrent && pTorrent->isList( ) )\r
42         {\r
43                 vector<CAtom *> vecTorrent = ( (CAtomList *)pTorrent )->getValue( );\r
44 \r
45                 if( vecTorrent.size( ) == 6 )\r
46                 {\r
47                         CAtom *pFileName = vecTorrent[0];\r
48 \r
49                         if( pFileName )\r
50                         {\r
51                                 string strPath = m_strAllowedDir + pFileName->toString( );\r
52 \r
53                                 if( UTIL_CheckFile( strPath.c_str( ) ) )\r
54                                 {\r
55                                         pResponse->strCode = "200 OK";\r
56 \r
57                                         //addition by labarks\r
58                                         string strFile = pRequest->strURL.substr(10);   \r
59                                         if ( strFile == pFileName->toString( ) )\r
60                                         {\r
61                                                 pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "application/x-bittorrent; name=\"" ) + UTIL_EscapedToString(strFile) + "\"" ) );\r
62                                                 pResponse->mapHeaders.insert( pair<string, string>( "Content-Disposition", string( "attachment; filename=\"" ) + UTIL_EscapedToString(strFile) + "\"" ) );\r
63                                         }\r
64                                         else\r
65                                         {\r
66                                                 pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "application/x-bittorrent; name=\"" ) + UTIL_EscapedToString(pFileName->toString( )) + "\"" ) );\r
67                                                 pResponse->mapHeaders.insert( pair<string, string>( "Content-Disposition", string( "attachment; filename=\"" ) + UTIL_EscapedToString(pFileName->toString( )) + "\"" ) );\r
68                                         }\r
69                                         /* Original Source Code:\r
70                                         pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "application/x-bittorrent; name=\"" ) + pFileName->toString( ) + "\"" ) );\r
71                                         pResponse->mapHeaders.insert( pair<string, string>( "Content-Disposition", string( "attachment; filename=\"" ) + pFileName->toString( ) + "\"" ) );\r
72                                         */\r
73                                         //end addition\r
74 \r
75                                         // cache for awhile\r
76 \r
77                                         time_t tNow = time( NULL ) + m_iFileExpires * 60;\r
78                                         char *szTime = asctime( gmtime( &tNow ) );\r
79                                         szTime[strlen( szTime ) - 1] = '\0';\r
80 \r
81                                         pResponse->mapHeaders.insert( pair<string, string>( "Expires", string( szTime ) + " GMT" ) );\r
82 \r
83                                         if( m_iDontCompressTorrent == 1 )\r
84                                                 pResponse->bCompressOK = false;\r
85 \r
86 \r
87                                         string strData = UTIL_ReadFile( strPath.c_str( ) );\r
88 \r
89                                         if( !m_strForceAnnounceURL.empty( ) && m_bForceAnnounceOnDL )\r
90                                         {\r
91                                                 CAtom *pData = Decode( strData );\r
92 \r
93                                                 if( pData && pData->isDicti( ) )\r
94                                                 {\r
95                                                         ( (CAtomDicti *)pData )->setItem( "announce", new CAtomString( m_strForceAnnounceURL ) );\r
96 \r
97                                                         pResponse->strContent = Encode( pData );\r
98                                                 }\r
99 \r
100                                                 delete pData;\r
101                                         }\r
102                                         else\r
103                                                 pResponse->strContent = strData;\r
104                                 }\r
105                                 else\r
106                                         pResponse->strCode = "404 Not Found";\r
107                         }\r
108                         else\r
109                                 pResponse->strCode = "404 Not Found";\r
110                 }\r
111                 else\r
112                         pResponse->strCode = "404 Not Found";\r
113         }\r
114         else\r
115                 pResponse->strCode = "404 Not Found";\r
116 }\r