Updated init script.
[dtbartle/bnbt.git] / tracker_info.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 "link.h"\r
27 #include "bencode.h"\r
28 #include "tracker.h"\r
29 #include "util.h"\r
30 \r
31 void CTracker :: serverResponseInfo( struct request_t *pRequest, struct response_t *pResponse, user_t user )\r
32 {\r
33         struct bnbttv btv = UTIL_CurrentTime( );\r
34 \r
35         pResponse->strCode = "200 OK";\r
36 \r
37         pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "text/html; charset=" ) + gstrCharSet ) );\r
38 \r
39         pResponse->strContent += "<html>\n";\r
40         pResponse->strContent += "<head>\n";\r
41 \r
42         if ( !gstrTrackerTitle.empty( ) )\r
43                 pResponse->strContent += "<title>" + gstrTrackerTitle + " - Tracker Info</title>\n";\r
44         else\r
45                 pResponse->strContent += "<title>BNBT Tracker Info</title>\n";\r
46 \r
47         if( !gstrStyle.empty( ) )\r
48                 pResponse->strContent += "<link rel=stylesheet type=\"text/css\" href=\"" + gstrStyle + "\">\n";\r
49 \r
50         pResponse->strContent += "</head>\n";\r
51         pResponse->strContent += "<body>\n";\r
52 \r
53         if( !m_bDisableLogon )\r
54         {\r
55                 if( user.strLogin.empty( ) )\r
56                         pResponse->strContent += "<p class=\"login1_index\">You are not logged in. Click <a href=\"/login.html\">here</a> to login.</p>\n";\r
57                 else\r
58                         pResponse->strContent += "<p class=\"login2_index\">You are logged in as <span class=\"username\">" + UTIL_RemoveHTML( user.strLogin ) + "</span>. Click <a href=\"/login.html?logout=1\">here</a> to logout.</p>\n";\r
59         }\r
60 \r
61         if ( !gstrTrackerTitle.empty( ) )\r
62                 pResponse->strContent += "<title>" + gstrTrackerTitle + " - Tracker Info</title>\n";\r
63         else\r
64                 pResponse->strContent += "<title>BNBT Tracker Info</title>\n";\r
65 \r
66         if( user.iAccess & ACCESS_VIEW )\r
67         {\r
68                 time_t tNow = time( NULL );\r
69                 char *szTime = asctime( localtime( &tNow ) );\r
70                 szTime[strlen( szTime ) - 1] = '\0';\r
71 \r
72                 pResponse->strContent += "<ul>\n";\r
73                 pResponse->strContent += "<li><strong>Tracker Version:</strong> " + string( BNBT_VER ) + "</li>\n";\r
74                 pResponse->strContent += "<li><strong>Server Time:</strong> " + string( szTime ) + "</li>\n";\r
75                 pResponse->strContent += "<li><strong>Uptime:</strong> " + UTIL_SecondsToString( GetTime( ) ) + "</li>\n";\r
76 \r
77                 if( m_pDFile )\r
78                 {\r
79                         pResponse->strContent += "<li><strong>Tracking " + CAtomLong( m_pDFile->getValuePtr( )->size( ) ).toString( ) + " Files, ";\r
80 \r
81                         unsigned long iPeers = 0;\r
82 \r
83                         map<string, CAtom *> *pmapDicti = m_pDFile->getValuePtr( );\r
84 \r
85                         for( map<string, CAtom *> :: iterator i = pmapDicti->begin( ); i != pmapDicti->end( ); i++ )\r
86                         {\r
87                                 if( (*i).second->isDicti( ) )\r
88                                         iPeers += ( (CAtomDicti *)(*i).second )->getValuePtr( )->size( );\r
89                         }\r
90 \r
91                         pResponse->strContent += CAtomLong( iPeers ).toString( ) + " Peers";\r
92 \r
93                         if( m_bCountUniquePeers )\r
94                                 pResponse->strContent += ", " + CAtomLong( m_pIPs->getValuePtr( )->size( ) ).toString( ) + " Unique";\r
95 \r
96                         pResponse->strContent += "</strong></li>\n";\r
97                 }\r
98 \r
99                 if( m_pUsers )\r
100                         pResponse->strContent += "<li><strong>" + CAtomLong( m_pUsers->getValuePtr( )->size( ) ).toString( ) + " Users</strong></li>\n";\r
101 \r
102                 pResponse->strContent += "</ul>\n";\r
103                 // The Trinity Edition - Addition Begins\r
104                 // The following adds an RTT link to the Tracker Info page\r
105 \r
106                 pResponse->strContent += "<p><a href=\"/index.html\">Return to Tracker</a></p>\n";\r
107 \r
108                 // ------------------------------------------------- End of Addition\r
109         }\r
110         else\r
111                 pResponse->strContent += "<p class=\"denied\">You are not authorized to view this page.</p>\n";\r
112 \r
113         if( m_bGen )\r
114                 pResponse->strContent += "<p class=\"gen\">Generated in " + UTIL_ElapsedTimeStr( btv, UTIL_CurrentTime( ) ) + " seconds.</p>\n";\r
115 \r
116         pResponse->strContent += "</body>\n";\r
117         pResponse->strContent += "</html>\n";\r
118 }\r
119 \r
120 void CTracker :: serverResponseBencodeInfo( struct request_t *pRequest, struct response_t *pResponse, user_t user )\r
121 {\r
122     pResponse->strCode = "200 OK";\r
123         pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "text/plain" ) ) );\r
124 \r
125         CAtomDicti *pData = new CAtomDicti( );\r
126         pData->setItem("version", new CAtomString( string( BNBT_VER ) ) );\r
127         CAtomDicti *pFeatures = new CAtomDicti( );\r
128         pFeatures->setItem("linking", new CAtomString( "ka" ) );\r
129         pFeatures->setItem("banning", new CAtomString( "cih" ) );\r
130         CAtomList *pStatistics = new CAtomList();\r
131         if( m_strDumpXMLFile != string() )\r
132                 pStatistics->addItem( new CAtomString( "XML Dump" ));\r
133     pData->setItem("features", pFeatures);\r
134 \r
135 #ifdef BNBT_MYSQL\r
136         pStatistics->addItem( new CAtomString( "MySQL" ));\r
137         if ( m_bMySQLOverrideDState == true )\r
138                 pData->setItem("database", new CAtomString( "MySQL Overriding DState" ) );\r
139         else\r
140         {\r
141                 pData->setItem("database", new CAtomString( "MySQL Statistics Dump with Flatfile dstate" ) );\r
142                 pStatistics->addItem( new CAtomString( "Dstate Dfile" ));\r
143         }\r
144 #else\r
145         pData->setItem("database", new CAtomString( "Flatfile Dstate" ) );\r
146         pStatistics->addItem( new CAtomString( "Dstate Dfile" ));\r
147 #endif\r
148         if( m_iSaveScrapeInterval > 0 )\r
149                 pStatistics->addItem( new CAtomString( "Timed scrape save" ) );\r
150 \r
151         pFeatures->setItem("statistics", pStatistics);\r
152 \r
153         if( gpLinkServer )\r
154         {\r
155                 long m_lTrackers;\r
156                 long m_lInactiveTrackers;\r
157                 m_lTrackers = 0;\r
158                 m_lInactiveTrackers = 0;\r
159                 gpLinkServer->m_mtxLinks.Claim( );\r
160                 for( vector<CLinkClient *> :: iterator i = gpLinkServer->m_vecLinks.begin( ); i != gpLinkServer->m_vecLinks.end( ); i++ )\r
161                 {\r
162                         if( (*i)->m_bActive )\r
163                                 m_lTrackers++;\r
164                         else\r
165                                 m_lInactiveTrackers++;\r
166                 }\r
167                 gpLinkServer->m_mtxLinks.Release( );\r
168                 CAtomDicti *pTrackers = new CAtomDicti( );\r
169                 if( m_lTrackers > 0 )\r
170                         pTrackers->setItem("active", new CAtomLong(m_lTrackers) );\r
171                 if( m_lInactiveTrackers > 0 )\r
172             pTrackers->setItem("inactive", new CAtomLong(m_lInactiveTrackers) );\r
173                 if( m_lTrackers + m_lInactiveTrackers > 0 )\r
174             pTrackers->setItem("total", new CAtomLong(m_lTrackers + m_lInactiveTrackers) );\r
175                 pData->setItem("links", pTrackers);\r
176         }\r
177         if( gpLink )\r
178         {\r
179                 CAtomDicti *pTrackers = new CAtomDicti( );\r
180                 pTrackers->setItem("active", new CAtomLong( 1 ) );\r
181                 pTrackers->setItem("total", new CAtomLong( 1 ) );\r
182                 pData->setItem("links", pTrackers);\r
183         }\r
184 \r
185         if( m_pDFile )\r
186                 {\r
187                 pData->setItem("files", new CAtomLong( m_pDFile->getValuePtr( )->size( ) ) );\r
188                 unsigned long iPeers = 0;\r
189 \r
190                 map<string, CAtom *> *pmapDicti = m_pDFile->getValuePtr( );\r
191 \r
192                 for( map<string, CAtom *> :: iterator i = pmapDicti->begin( ); i != pmapDicti->end( ); i++ )\r
193                 {\r
194                         if( (*i).second->isDicti( ) )\r
195                                 iPeers += ( (CAtomDicti *)(*i).second )->getValuePtr( )->size( );\r
196                 }\r
197 \r
198                 pData->setItem("peers", new CAtomLong( iPeers ) );\r
199 \r
200                 if( m_bCountUniquePeers )\r
201                         pData->setItem("unique", new CAtomLong( m_pIPs->getValuePtr( )->size( ) ) );\r
202         }\r
203 \r
204         pResponse->strContent = Encode( pData );\r
205         pResponse->bCompressOK = false;\r
206 \r
207         delete pData;\r
208 }