~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/src/magnet_uri.cpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2007, Arvid Norberg
4
 
All rights reserved.
5
 
 
6
 
Redistribution and use in source and binary forms, with or without
7
 
modification, are permitted provided that the following conditions
8
 
are met:
9
 
 
10
 
    * Redistributions of source code must retain the above copyright
11
 
      notice, this list of conditions and the following disclaimer.
12
 
    * Redistributions in binary form must reproduce the above copyright
13
 
      notice, this list of conditions and the following disclaimer in
14
 
      the documentation and/or other materials provided with the distribution.
15
 
    * Neither the name of the author nor the names of its
16
 
      contributors may be used to endorse or promote products derived
17
 
      from this software without specific prior written permission.
18
 
 
19
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 
POSSIBILITY OF SUCH DAMAGE.
30
 
 
31
 
*/
32
 
 
33
 
#include "libtorrent/magnet_uri.hpp"
34
 
#include "libtorrent/session.hpp"
35
 
#include "libtorrent/torrent_handle.hpp"
36
 
#include "libtorrent/escape_string.hpp"
37
 
 
38
 
#include <string>
39
 
#include <sstream>
40
 
#include <boost/lexical_cast.hpp>
41
 
 
42
 
namespace libtorrent
43
 
{
44
 
        std::string make_magnet_uri(torrent_handle const& handle)
45
 
        {
46
 
                std::stringstream ret;
47
 
                if (!handle.is_valid()) return ret.str();
48
 
 
49
 
                std::string name = handle.name();
50
 
 
51
 
                ret << "magnet:?xt=urn:btih:" << base32encode(
52
 
                        std::string((char*)handle.info_hash().begin(), 20));
53
 
                if (!name.empty())
54
 
                        ret << "&dn=" << escape_string(name.c_str(), name.length());
55
 
                torrent_status st = handle.status();
56
 
                if (!st.current_tracker.empty())
57
 
                {
58
 
                        ret << "&tr=" << escape_string(st.current_tracker.c_str()
59
 
                                , st.current_tracker.length());
60
 
                }
61
 
                else
62
 
                {
63
 
                        std::vector<announce_entry> const& tr = handle.trackers();
64
 
                        if (!tr.empty())
65
 
                        {
66
 
                                ret << "&tr=" << escape_string(tr[0].url.c_str()
67
 
                                        , tr[0].url.length());
68
 
                        }
69
 
                }
70
 
                return ret.str();
71
 
        }
72
 
 
73
 
        std::string make_magnet_uri(torrent_info const& info)
74
 
        {
75
 
                std::stringstream ret;
76
 
                if (!info.is_valid()) return ret.str();
77
 
 
78
 
                std::string name = info.name();
79
 
 
80
 
                ret << "magnet:?xt=urn:btih:" << base32encode(
81
 
                        std::string((char*)info.info_hash().begin(), 20));
82
 
                if (!name.empty())
83
 
                        ret << "&dn=" << escape_string(name.c_str(), name.length());
84
 
                std::vector<announce_entry> const& tr = info.trackers();
85
 
                if (!tr.empty())
86
 
                {
87
 
                        ret << "&tr=" << escape_string(tr[0].url.c_str()
88
 
                                , tr[0].url.length());
89
 
                }
90
 
                return ret.str();
91
 
        }
92
 
 
93
 
#ifndef TORRENT_NO_DEPRECATE
94
 
        torrent_handle add_magnet_uri(session& ses, std::string const& uri
95
 
                , fs::path const& save_path
96
 
                , storage_mode_t storage_mode
97
 
                , bool paused
98
 
                , storage_constructor_type sc
99
 
                , void* userdata)
100
 
        {
101
 
                std::string name;
102
 
                std::string tracker;
103
 
 
104
 
                boost::optional<std::string> display_name = url_has_argument(uri, "dn");
105
 
                if (display_name) name = unescape_string(display_name->c_str());
106
 
                boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
107
 
                if (tracker_string) tracker = unescape_string(tracker_string->c_str());
108
 
        
109
 
                boost::optional<std::string> btih = url_has_argument(uri, "xt");
110
 
                if (!btih) return torrent_handle();
111
 
 
112
 
                if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
113
 
 
114
 
                sha1_hash info_hash;
115
 
                if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
116
 
                else info_hash.assign(base32decode(btih->substr(9)));
117
 
 
118
 
                return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
119
 
                        , name.empty() ? 0 : name.c_str(), save_path, entry()
120
 
                        , storage_mode, paused, sc, userdata);
121
 
        }
122
 
#endif
123
 
 
124
 
        torrent_handle add_magnet_uri(session& ses, std::string const& uri
125
 
                , add_torrent_params p)
126
 
        {
127
 
                std::string name;
128
 
                std::string tracker;
129
 
 
130
 
                boost::optional<std::string> display_name = url_has_argument(uri, "dn");
131
 
                if (display_name) name = unescape_string(display_name->c_str());
132
 
                boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
133
 
                if (tracker_string) tracker = unescape_string(tracker_string->c_str());
134
 
        
135
 
                boost::optional<std::string> btih = url_has_argument(uri, "xt");
136
 
                if (!btih) return torrent_handle();
137
 
 
138
 
                if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();
139
 
 
140
 
                sha1_hash info_hash;
141
 
                if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
142
 
                else info_hash.assign(base32decode(btih->substr(9)));
143
 
 
144
 
                if (!tracker.empty()) p.tracker_url = tracker.c_str();
145
 
                p.info_hash = info_hash;
146
 
                if (!name.empty()) p.name = name.c_str();
147
 
                return ses.add_torrent(p);
148
 
        }
149
 
}
150
 
 
151