mldonkey-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Mldonkey-commits] Changes to mldonkey/src/networks/bittorrent/bTTorrent


From: mldonkey-commits
Subject: [Mldonkey-commits] Changes to mldonkey/src/networks/bittorrent/bTTorrent.ml
Date: Thu, 04 Aug 2005 18:00:29 -0400

Index: mldonkey/src/networks/bittorrent/bTTorrent.ml
diff -u mldonkey/src/networks/bittorrent/bTTorrent.ml:1.6 
mldonkey/src/networks/bittorrent/bTTorrent.ml:1.7
--- mldonkey/src/networks/bittorrent/bTTorrent.ml:1.6   Wed Jul 27 20:06:16 2005
+++ mldonkey/src/networks/bittorrent/bTTorrent.ml       Thu Aug  4 22:00:23 2005
@@ -57,19 +57,32 @@
 
 
 let chunk_size = Int64.of_int (256 * 1024)
-  
+
 let decode_torrent s =
-  if !verbose_msg_servers then lprintf_nl "[BT]: .torrent file loaded\n";
+  if !verbose_msg_servers then lprintf_nl "[BT]: .torrent file loaded";
 (*            lprintf "Loaded: %s\n" (String.escaped s); *)
   let v = Bencode.decode s in
 (*            lprintf "Decoded file: %s\n" (Bencode.print v);  *)
 
 
   let announce = ref "" in
+  let announce_list = ref [] in
   let file_info = ref (List []) in
   let file_name = ref "" in
+  let file_torrent_filename = ref "" in
+  let file_name_utf8 = ref "" in
   let file_piece_size = ref zero in
   let file_pieces = ref "" in
+  let file_comment = ref "" in
+  let file_created_by = ref "" in
+  let file_creation_date = ref zero in
+  let file_modified_by = ref "" in
+  let file_encoding = ref "" in
+  let file_ed2k_hash = ref "" in
+  let file_nodes = ref "" in
+  let file_is_private = ref zero in
+  let file_aps = ref (List []) in
+  let file_dht_backup_enable = ref zero in
   let length = ref zero in
   let file_files = ref [] in
 
@@ -121,9 +134,40 @@
         List.iter (fun (key, value) ->
             match key, value with
               String "announce", String tracker_url ->
+               if !verbose_msg_servers then
+                 lprintf_nl "[BT]: New tracker added :%s" tracker_url;
                 announce := tracker_url
+            | String "announce-list", List list ->
+                List.iter (fun url_list ->
+                    let next_urls = ref [] in
+                    match url_list with
+                    | List next_url_list ->
+                        List.iter (fun myvalue ->
+                            match myvalue with
+                            | String next_url ->
+                                next_urls := next_url :: !next_urls;
+                                if !verbose_msg_servers then
+                                  lprintf_nl "[BT]: New tracker received :%s" 
next_url
+                            | _ ->
+                                if !verbose_msg_servers then
+                                  lprintf_nl "[BT]: error while decoding 
announce list"
+                        ) next_url_list;
+                        if List.length !next_urls > 1 then begin
+                            next_urls := List2.shuffle !next_urls;
+                            announce_list := !next_urls @ !announce_list
+                          end
+                        else
+                          announce_list := List.hd !next_urls :: !announce_list
+                    | _ ->
+                        lprintf_nl "[BT]: unknown field in announce list"
+                    ) list;
+                    announce_list := List.rev !announce_list;
+                    if !verbose_msg_servers then
+                      List.iter (fun url ->
+                        lprintf_nl "[BT]: New tracker added :%s" url
+                        ) !announce_list
             | String "info", ((Dictionary list) as info) ->
-                
+
                 file_info := info;
                 List.iter (fun (key, value) ->
                     match key, value with
@@ -137,15 +181,83 @@
                         file_piece_size := n
                     | String "pieces", String pieces ->
                         file_pieces := pieces
-                    | String key, _ -> 
-                        if !verbose_msg_servers then lprintf "other field [%s] 
in info\n" key
-                    | _ -> 
-                        lprintf "other field in info\n"
+                    | String "ed2k", String string_ed2k ->
+                        if !!enable_donkey then
+                          file_ed2k_hash := string_ed2k;
+                      (* TODO: Add new ed2k download if ed2k hash is available,
+                               then merge it with current download *)
+                    | String "sha1", String string_sha1 -> ()
+                      (* TODO: Parse sha1 hash *)
+
+                    | String "publisher", String created_by ->
+                      file_created_by := created_by
+                    | String "publisher-url", String publisher_url ->
+                      file_created_by := !file_created_by ^ " @ " ^ 
publisher_url
+
+                    | String "name.utf-8", String name_utf8 ->
+                        file_name_utf8 := name_utf8
+
+                    | String "publisher.utf-8", String publisher_utf8 -> ()
+                    | String "publisher-url.utf-8", String publisher_url_utf8 
-> ()
+
+                    | String "private", Int n ->
+                        (* TODO: if set to 1, only accept peers from tracker *)
+                        file_is_private := n;
+                        if !verbose_msg_servers &&
+                          Int64.to_int !file_is_private = 1 then
+                            lprintf_nl "[BT]: torrent is private"
+                    | String key, _ ->
+                        if !verbose_msg_servers then
+                          lprintf_nl "[BT]: found other field [%s] with value 
[%s] in info" key (Bencode.print value)
+                    | _ ->
+                        lprintf_nl "[BT]: other field in info"
+                ) list
+
+            | String "comment", String comment
+            | String "comment.utf-8", String comment ->
+              file_comment := comment
+            (* Next 2 strings are after info sometimes  *)
+            | String "publisher", String created_by ->
+              file_created_by := created_by
+            | String "publisher-url", String publisher_url ->
+              file_created_by := !file_created_by ^ " @ " ^ publisher_url
+
+            | String "created by", String created_by ->
+              file_created_by := created_by
+            | String "creation date", Int creation_date ->
+              file_creation_date := creation_date
+            | String "modified-by", String modified_by ->
+              file_modified_by := modified_by
+            | String "encoding", String encoding ->
+              file_encoding := encoding
+            | String "torrent filename", String torrent_filename ->
+              file_torrent_filename := torrent_filename
+            | String "nodes", nodes -> ()
+              (* TODO : nodes is a list of DHT Network nodes ,parse and use 
them *)
+
+(*
+              file_nodes := nodes
+*)
+
+            | String "azureus_properties", ((Dictionary list) as 
azureus_properties) ->
+                file_aps := azureus_properties;
+                List.iter (fun (key, value) ->
+                    match key, value with
+                    | String "dht_backup_enable", Int n ->
+                        file_dht_backup_enable := n;
+                        if !verbose_msg_servers &&
+                          Int64.to_int !file_dht_backup_enable = 1 then
+                            lprintf_nl "[BT]: azureus properties : Torrent has 
dht backup"
+                    | String key, _ ->
+                        if !verbose_msg_servers then
+                          lprintf_nl "[BT]: found other field [%s] with value 
[%s] in azureus properties" key (Bencode.print value)
+                    | _ ->
+                        lprintf_nl "[BT]: other field in azureus properties"
                 ) list
             | String key, _ ->
-                if !verbose_msg_servers then lprintf "other field [%s] after 
info\n" key
+                if !verbose_msg_servers then lprintf_nl "[BT]: found other 
field [%s] with value [%s] after info" key (Bencode.print value)
             | _ ->
-                lprintf "other field after info\n"
+                lprintf_nl "[BT]: other field after info"
         ) list
     | _ -> assert false
   end;
@@ -174,11 +286,24 @@
 
   file_id, {
     torrent_name = !file_name;
+    torrent_filename = !file_torrent_filename;
+    torrent_name_utf8 = !file_name_utf8;
     torrent_length = !length;
     torrent_announce = !announce;
+    torrent_announce_list = !announce_list;
     torrent_piece_size = !file_piece_size;
     torrent_files = !file_files;
     torrent_pieces = pieces;
+    torrent_comment = !file_comment;
+    torrent_created_by = !file_created_by;
+    torrent_creation_date = !file_creation_date;
+    torrent_modified_by = !file_modified_by;
+    torrent_encoding = !file_encoding;
+    torrent_private = !file_is_private;
+
+(*
+    torrent_nodes = !file_nodes;
+*)
   }
 
 let encode_torrent torrent =
@@ -211,20 +336,31 @@
     Dictionary [
       files;
       String "name", String torrent.torrent_name;
+      String "name.utf-8", String torrent.torrent_name_utf8;
       String "piece length", Int torrent.torrent_piece_size;
       String "pieces", String pieces;
+      String "private", Int torrent.torrent_private;
     ]
   in
 
   let info_encoded = Bencode.encode info in
   let file_id = Sha1.string info_encoded in
-  file_id, 
+  file_id,
   Dictionary [
     String "announce", String torrent.torrent_announce;
     String "info", info;
+    String "comment", String torrent.torrent_comment;
+    String "created by", String torrent.torrent_created_by;
+    String "creation date", Int torrent.torrent_creation_date;
+    String "encoding", String torrent.torrent_encoding;
+    String "modified-by", String torrent.torrent_modified_by;
+(*
+    String "nodes", String torrent.torrent_nodes;
+*)
   ]
 
-let make_torrent announce filename =
+let make_torrent announce filename comment is_private =
+  let announce_list = [ announce ] in
   let basename = Filename.basename filename in
   let files, t =
     if Unix2.is_directory filename then
@@ -271,15 +407,32 @@
 
   {
     torrent_name = basename;
+    torrent_filename = "";
+    torrent_name_utf8 = Charset.to_utf8 basename;
     torrent_length = length;
     torrent_announce = announce;
+    torrent_announce_list = announce_list;
     torrent_piece_size = chunk_size;
     torrent_files = files;
     torrent_pieces = pieces;
+    torrent_comment =
+      if String.length comment > 1 then
+        comment
+      else
+        Printf.sprintf "Created by MLdonkey/%s" Autoconf.current_version;
+
+    torrent_created_by = Printf.sprintf "MLdonkey/%s" Autoconf.current_version;
+    torrent_creation_date = Int64.of_float (Unix.gettimeofday ());
+    torrent_modified_by = "";
+    torrent_encoding = "";
+    torrent_private = is_private;
+(*
+    torrent_nodes = "";
+*)
   }
 
-let generate_torrent announce torrent_filename filename =
-  let torrent = make_torrent announce filename in
+let generate_torrent announce torrent_filename torrent_comment torrent_private 
filename =
+  let torrent = make_torrent announce filename torrent_comment torrent_private 
in
   let file_id, encoded = encode_torrent torrent in
   let encoded = Bencode.encode encoded in
-  File.from_string torrent_filename encoded 
+  File.from_string torrent_filename encoded




reply via email to

[Prev in Thread] Current Thread [Next in Thread]