~ubuntu-branches/ubuntu/natty/kdenetwork/natty-proposed

« back to all changes in this revision

Viewing changes to kget/transfer-plugins/bittorrent/libbtcore/peer/peerid.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-01-21 16:28:50 UTC
  • mfrom: (1.1.55 upstream)
  • Revision ID: james.westby@ubuntu.com-20110121162850-5okl235t3l91cwx0
Tags: 4:4.6.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2005 by Joris Guisson                                   *
3
 
 *   joris.guisson@gmail.com                                               *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
19
 
 ***************************************************************************/
20
 
#include "peerid.h"
21
 
#include <time.h>
22
 
#include <stdlib.h>
23
 
#include <qmap.h>
24
 
#include <klocale.h>
25
 
#include "btversion.h"
26
 
 
27
 
namespace bt
28
 
{
29
 
        char RandomLetterOrNumber()
30
 
        {
31
 
                int i = rand() % 62;
32
 
                if (i < 26)
33
 
                        return 'a' + i;
34
 
                else if (i < 52)
35
 
                        return 'A' + (i - 26);
36
 
                else
37
 
                        return '0' + (i - 52);
38
 
        }
39
 
 
40
 
        
41
 
        PeerID::PeerID()
42
 
        {
43
 
                srand(time(0));
44
 
                memcpy(id,bt::PeerIDPrefix().toAscii(),8);
45
 
                for (int i = 8;i < 20;i++)
46
 
                        id[i] = RandomLetterOrNumber(); 
47
 
                client_name = identifyClient();
48
 
        }
49
 
 
50
 
        PeerID::PeerID(const char* pid)
51
 
        {
52
 
                if (pid)
53
 
                        memcpy(id,pid,20);
54
 
                else
55
 
                        memset(id,0,20);
56
 
                client_name = identifyClient();
57
 
        }
58
 
 
59
 
        PeerID::PeerID(const PeerID & pid)
60
 
        {
61
 
                memcpy(id,pid.id,20);
62
 
                client_name = pid.client_name;
63
 
        }
64
 
 
65
 
        PeerID::~PeerID()
66
 
        {}
67
 
 
68
 
 
69
 
 
70
 
        PeerID & PeerID::operator = (const PeerID & pid)
71
 
        {
72
 
                memcpy(id,pid.id,20);
73
 
                client_name = pid.client_name;
74
 
                return *this;
75
 
        }
76
 
 
77
 
        bool operator == (const PeerID & a,const PeerID & b)
78
 
        {
79
 
                for (int i = 0;i < 20;i++)
80
 
                        if (a.id[i] != b.id[i])
81
 
                                return false;
82
 
 
83
 
                return true;
84
 
        }
85
 
 
86
 
        bool operator != (const PeerID & a,const PeerID & b)
87
 
        {
88
 
                return ! operator == (a,b);
89
 
        }
90
 
 
91
 
        bool operator < (const PeerID & a,const PeerID & b)
92
 
        {
93
 
                for (int i = 0;i < 20;i++)
94
 
                        if (a.id[i] < b.id[i])
95
 
                                return true;
96
 
 
97
 
                return false;
98
 
        }
99
 
 
100
 
        QString PeerID::toString() const
101
 
        {
102
 
                QString r;
103
 
                for (int i = 0;i < 20;i++)
104
 
                        r += id[i] == 0 ? ' ' : id[i];
105
 
                return r;
106
 
        }
107
 
 
108
 
        QString PeerID::identifyClient() const
109
 
        {
110
 
                if (!client_name.isNull())
111
 
                        return client_name;
112
 
                
113
 
                QString peer_id = toString();
114
 
                // we only need to create this map once
115
 
                // so make it static
116
 
                static QMap<QString, QString> Map;
117
 
                static bool first = true; 
118
 
 
119
 
                if (first)
120
 
                {
121
 
                        // Keep things a bit alphabetic to make it easier add new ones
122
 
                        //AZUREUS STYLE
123
 
                        Map["AG"] = "Ares";
124
 
                        Map["A~"] = "Ares";
125
 
                        Map["AV"] = "Avicora";
126
 
                        Map["AX"] = "BitPump";
127
 
                        Map["AR"] = "Arctic";
128
 
                        Map["AZ"] = "Azureus";
129
 
                        Map["BB"] = "BitBuddy";
130
 
                        Map["BC"] = "BitComet";
131
 
                        Map["BF"] = "Bitflu";
132
 
                        Map["BG"] = "BTGetit";
133
 
                        Map["BM"] = "BitMagnet";
134
 
                        Map["BO"] = "BitsOnWheels";
135
 
                        Map["BR"] = "BitRocket";
136
 
                        Map["BS"] = "BTSlave"; 
137
 
                        Map["BX"] = "BitTorrent X";
138
 
                        Map["CD"] = "Enhanced CTorrent";
139
 
                        Map["CT"] = "CTorrent";
140
 
                        Map["DE"] = "DelugeTorrent";
141
 
                        Map["DP"] = "Propagate Data Client";
142
 
                        Map["EB"] = "EBit";
143
 
                        Map["ES"] = "electric sheep";
144
 
                        Map["FT"] = "FoxTorrent";
145
 
                        Map["GS"] = "GSTorrent";
146
 
                        Map["G3"] = "G3 Torrent";
147
 
                        Map["HL"] = "Halite";
148
 
                        Map["HN"] = "Hydranode";
149
 
                        Map["KG"] = "KGet";
150
 
                        Map["KT"] = "KTorrent"; // lets not forget our own client
151
 
                        Map["LH"] = "LH-ABC";
152
 
                        Map["lt"] = "libTorrent";
153
 
                        Map["LT"] = "libtorrent";
154
 
                        Map["LP"] = "Lphant";
155
 
                        Map["LW"] = "LimeWire";
156
 
                        Map["ML"] = "MLDonkey";
157
 
                        Map["MO"] = "MonoTorrent";
158
 
                        Map["MP"] = "MooPolice";
159
 
                        Map["MT"] = "MoonLight";
160
 
                        Map["PD"] = "Pando";
161
 
                        Map["qB"] = "qBittorrent";
162
 
                        Map["QD"] = "QQDownload";
163
 
                        Map["QT"] = "Qt 4 Torrent example";
164
 
                        Map["RS"] = "Rufus";
165
 
                        Map["RT"] = "Retriever";
166
 
                        Map["S~"] = "Shareaza alpha/beta";
167
 
                        Map["SB"] = "Swiftbit";
168
 
                        Map["SS"] = "SwarmScope";
169
 
                        Map["ST"] = "SymTorrent";
170
 
                        Map["st"] = "sharktorrent";
171
 
                        Map["SZ"] = "Shareaza";
172
 
                        Map["TN"] = "Torrent .NET";
173
 
                        Map["TR"] = "Transmission";
174
 
                        Map["TS"] = "Torrent Storm";
175
 
                        Map["TT"] = "TuoTu";
176
 
                        Map["UL"] = "uLeecher!";
177
 
                        Map["UT"] = QString("%1Torrent").arg(QChar(0x00B5)); // µTorrent, 0x00B5 is unicode for µ
178
 
                        Map["WT"] = "BitLet";
179
 
                        Map["WY"] = "FireTorrent";
180
 
                        Map["XL"] = "Xunlei";
181
 
                        Map["XT"] = "Xan Torrent";
182
 
                        Map["XX"] = "Xtorrent";
183
 
                        Map["ZT"] = "Zip Torrent";
184
 
                        
185
 
                        //SHADOWS STYLE
186
 
                        Map["A"] = "ABC";
187
 
                        Map["O"] = "Osprey Permaseed";
188
 
                        Map["Q"] = "BTQueue";
189
 
                        Map["R"] = "Tribler";
190
 
                        Map["S"] = "Shadow's";
191
 
                        Map["T"] = "BitTornado";
192
 
                        Map["U"] = "UPnP NAT BitTorrent";
193
 
                        //OTHER
194
 
                        Map["Plus"] = "Plus! II";
195
 
                        Map["OP"] = "Opera";
196
 
                        Map["BOW"] = "Bits on Wheels";
197
 
                        Map["M"] = "BitTorrent";
198
 
                        Map["exbc"] = "BitComet";
199
 
                        Map["Mbrst"] = "Burst!";
200
 
                        first = false;
201
 
                }
202
 
 
203
 
                QString name = i18n("Unknown client");
204
 
                if (peer_id.at(0) == '-' &&
205
 
                        peer_id.at(1).isLetter() &&
206
 
                        peer_id.at(2).isLetter() ) //AZ style
207
 
                {
208
 
                        QString ID(peer_id.mid(1,2));
209
 
                        if (Map.contains(ID))
210
 
                                name = Map[ID] + ' ' + peer_id.at(3) + '.' + peer_id.at(4) + '.'
211
 
                                        + peer_id.at(5) + '.' + peer_id.at(6);
212
 
                }
213
 
                else if (peer_id.at(0).isLetter() &&
214
 
                                peer_id.at(1).isDigit() &&
215
 
                                peer_id.at(2).isDigit() )  //Shadow's style
216
 
                {
217
 
                        QString ID = QString(peer_id.at(0));
218
 
                        if (Map.contains(ID))
219
 
                                name = Map[ID] + ' ' + peer_id.at(1) + '.' +
220
 
                                                peer_id.at(2) + '.' + peer_id.at(3);
221
 
                }
222
 
                else if (peer_id.at(0) == 'M' && peer_id.at(2) == '-' && (peer_id.at(4) == '-' || peer_id.at(5) == '-'))
223
 
                {
224
 
                        name = Map["M"] + ' ' + peer_id.at(1) + '.' + peer_id.at(3);
225
 
                        if(peer_id.at(4) == '-')
226
 
                                name += QString(".%1").arg(peer_id.at(5));
227
 
                        else
228
 
                                name += QString("%1.%2").arg(peer_id.at(4)).arg(peer_id.at(6));
229
 
                }
230
 
                else if (peer_id.startsWith("OP"))
231
 
                {
232
 
                        name = Map["OP"];
233
 
                }
234
 
                else if ( peer_id.startsWith("exbc") )
235
 
                {
236
 
                        name = Map["exbc"];
237
 
                }
238
 
                else if ( peer_id.mid(1,3) == "BOW")
239
 
                {
240
 
                        name = Map["BOW"];
241
 
                }
242
 
                else if ( peer_id.startsWith("Plus"))
243
 
                {
244
 
                        name = Map["Plus"];
245
 
                }
246
 
                else if ( peer_id.startsWith("Mbrst"))
247
 
                {
248
 
                        name = Map["Mbrst"] + ' ' + peer_id.at(5) + '.' + peer_id.at(7);
249
 
                }
250
 
                        
251
 
                return name;
252
 
        }
253
 
}