Exemple #1
0
        /// <summary>
        /// Interpret a received search result
        /// (active and passive results can be handled with this method)
        /// this will automatically add sources to already existing download
        /// queue entries, these will not be shown in the search results
        /// TODO make this an option
        /// </summary>
        /// <param name="result">the result to be interpreted</param>
        private void InterpretReceivedSearchResult(SearchResults.SearchResult result)
        {
            //Console.WriteLine("Adding Result to SearchResults");
            result.Hub = ResolveHub(result.HubAddress);
            if (result.Hub != null) //only add results for hubs still connected
            {
                if (result.HasTTH)
                {//only if a result has a tth it is considered a source for some queue entry
                    Queue.QueueEntry entry = download_queue.FindQueueEntryByTTH(result.TTH);
                    if (entry != null)
                    {//this searchresult is also a source for a queue entry
                        //,instead using of giving it back as result we add it to the source pool of the queue entry
                        entry.AddSource(new Queue.QueueEntry.Source(result.UserName, result.Filename, result.Hub));
                    } //no queue entry found for this one just hand it over to SearchResults
                    else
                    {
                        search_results.AddResult(result);
                    }
                }
                else
                {
                    search_results.AddResult(result);
                }
            }

            /*
             *      //Console.WriteLine("Adding Result to SearchResults");
             *      result.Hub = ResolveHub(result.HubAddress);
             *      search_results.AddResult(result);
             */
        }
Exemple #2
0
 /// <summary>
 /// Start downloading a search result
 /// ,also adds a queue entry
 /// </summary>
 /// <param name="result">the search result you want to download</param>
 public void StartDownload(SearchResults.SearchResult result)
 {
     if (result.IsHubResolved)
     {                                                //TODO put this into queue class
         download_queue.AddSearchResult(result);
         result.Hub.SendConnectToMe(result.UserName); //signal download to hub to start it
     }
     else
     {
         Console.WriteLine("Hub was not resolved from result hub address: " + result.HubAddress);
     }
 }
Exemple #3
0
        /*public Object QueueLock
         * {
         *  get
         *  {
         *      return (queue_lock);
         *  }
         *  set
         *  {
         *      queue_lock = value;
         *  }
         * }*/

        //deprecated , because automatic adding of sources should make this a never to be called again function
        public QueueEntry FindExistingEntryForSearchResult(SearchResults.SearchResult result)
        {
            QueueEntry ret = null;

            lock (queue_lock)
            {
                foreach (QueueEntry entry in items)
                {
                    //if ((entry.OutputFilename == download_directory + "\\" + System.IO.Path.GetFileName(result.Filename) && entry.Filesize == result.Filesize)
                    //    || (result.HasTTH && entry.HasTTH && entry.TTH == result.TTH))
                    if (result.HasTTH && entry.HasTTH && entry.TTH == result.TTH)
                    {
                        ret = (entry);
                        break;
                    }
                }
            }
            return(ret);
        }
        /// <summary>
        /// Interpret a single command
        /// in this case only $SR search results received via udp
        /// </summary>
        /// <param name="received_command">the command to interpret</param>
        private void InterpretCommand(string received_command)
        {
            int command_end = received_command.IndexOf(" ");

            if (command_end != -1)
            {
                string   command    = received_command.Substring(1, command_end - 1);
                string   parameter  = received_command.Substring(command_end + 1);
                string[] parameters = parameter.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                //Console.WriteLine("Command: '" + command + "' ,Parameter("+parameters.Length+"): '" + parameter + "'");

                switch (command)
                {
                case "SR":
                    //Console.WriteLine("Search result received: " + parameter);
                    SearchResults.SearchResult result = new SearchResults.SearchResult();
                    result.ResultLine = parameter;
                    try
                    {
                        if (SearchResultReceived != null)
                        {
                            SearchResultReceived(result);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception in event handler: " + ex.Message);
                    }

                    break;


                default:
                    Console.WriteLine("Unknown Command received: " + command + ", Parameter: " + parameter);
                    break;
                }
            }
            else
            {
                Console.WriteLine("Error interpreting command: " + received_command);
            }
        }
Exemple #5
0
        public void AddSearchResult(SearchResults.SearchResult result)
        {
            if (result.IsFile)
            {
                QueueEntry existing = FindExistingEntryForSearchResult(result);
                if (existing != null)
                {//This should be a deprecated case.. never ever be called again :-)
                 //lock (queue_lock)
                 //{
                    existing.AddSource(new QueueEntry.Source(result.UserName, result.Filename, result.Hub));
                    //TODO source add event
                    //}
                    return;
                }
                QueueEntry entry = new QueueEntry();
                entry.Type           = QueueEntry.EntryType.File;
                entry.Filesize       = result.Filesize;
                entry.OutputFilename = download_directory + "\\" + System.IO.Path.GetFileName(result.Filename); //TODO add directory support somehow
                if (File.Exists(entry.OutputFilename))
                {                                                                                               //already some file existing with this name ... try a (i) at the end increment until file is not there
                    int    i             = 1;
                    string new_extension = Path.GetExtension(entry.OutputFilename);

                    string new_filename = Path.GetDirectoryName(entry.OutputFilename) + "\\" + Path.GetFileNameWithoutExtension(entry.OutputFilename) + "(" + i + ")" + new_extension;
                    while (File.Exists(new_filename))
                    {
                        i++;
                        new_filename = Path.GetDirectoryName(entry.OutputFilename) + "\\" + Path.GetFileNameWithoutExtension(entry.OutputFilename) + "(" + i + ")" + new_extension;
                    }
                    entry.OutputFilename = new_filename;
                }
                if (result.HasTTH)
                {
                    entry.TTH = result.TTH;
                }
                //if(result.IsHubResolved)
                entry.AddSource(new Queue.QueueEntry.Source(result.UserName, result.Filename, result.Hub));
                lock (queue_lock)
                {
                    items.Add(entry);
                }
                GrabEntry(entry);
                //Console.WriteLine("Queue Entry added: '"+entry.OutputFilename+"'");
                try
                {
                    if (EntryAdded != null)
                    {
                        EntryAdded(entry);
                    }
                    if (EntriesChanged != null)
                    {
                        EntriesChanged();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured in added event callback: " + ex.Message);
                }
            }
            //download_directory + "\\" +
        }
        /// <summary>
        /// Interpret a single command 
        /// </summary>
        /// <param name="received_command">the command to interpret</param>
        private void InterpretCommand(string received_command)
        {
            int command_end = received_command.IndexOf(" ");
            if (command_end == -1) command_end = received_command.Length;

            if (command_end != -1)
            {
                string parameter = "";
                string[] parameters ={ };
                string command = received_command.Substring(1);
                if (command_end != received_command.Length)
                {
                    command = received_command.Substring(1, command_end - 1);
                    parameter = received_command.Substring(command_end + 1);
                    parameters = parameter.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    //Console.WriteLine("Command: '" + command + "' ,Parameter(" + parameters.Length + "): '" + parameter + "'");
                }
                switch (command)
                {
                    case "HubName" :
                        //Console.WriteLine("Hubname Command received: " + parameter);
                        name = parameter;
                        //fire nameChange event
                        break;

                    case "Hello" :
                        //Console.WriteLine("Hello Command received: " + parameters[0]);
                        if (!is_logged_in)
                        {
                            is_logged_in = true;
                            SendCommand("Version", my_version);
                            SendCommand("GetNickList");
                            SendMyInfo();
                            //Console.WriteLine("Logged in Hub: "+name);
                            try
                            {
                                if (LoggedIn != null)
                                    LoggedIn(this);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Exception in LoggedIn: " + ex.Message);
                            }
                        }
                        else
                        {//new user announced by server
                            //Console.WriteLine("User "+parameters[0]+" has joined Hub: "+name);
                            UserListAdd(parameters[0]);
                        }
                        break;

                    case "Quit":
                        //Console.WriteLine("User "+parameters[0]+" has left Hub: "+name);
                        UserListRemove(parameters[0]);
                        break;

                    case "NickList":
                        Console.WriteLine("NickList Message received.");
                        UserListClear();
                        string[] temp_users = parameters[0].Split("$$".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        foreach (string temp_user in temp_users)
                        {
                            UserListAdd(temp_user);
                        }
                        break;

                    case "OpList":
                        op_list.Clear();
                        string[] temp_ops = parameters[0].Split("$$".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        foreach (string temp_op in temp_ops)
                        {
                            op_list.Add(temp_op);
                        }
                        break;

                    case "ConnectToMe":
                            try
                            {
                                string peer_address = "";
                                if (parameters.Length == 2)
                                {
                                    peer_address = parameters[1];
                                }
                                else if (parameters.Length == 3)
                                {
                                    peer_address = parameters[2];
                                }
                                else break;
                                Peer peer = new Peer(peer_address); //add username also, to counter possible network attacks
                                if (ConnectToMeReceived != null)
                                    ConnectToMeReceived(this, peer);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Exception in ConnectToMe EventHandler: " + ex.Message);
                            }
                        break;

                    case "RevConnectToMe":
                        SendConnectToMe(parameters[0]);
                        break;

                    case "HubTopic":
                        //topic = parameters[0];
                        topic = parameter;
                        break;

                    case "UserCommand":
                        //Console.WriteLine("User Command received: "+parameter);
                        //TODO support user context menu entries
                        break;

                    case "Search":
                        //Console.WriteLine("Search Command received: "+parameter);
                        SearchParameters search = new SearchParameters();
                        if(parameters[0].StartsWith("Hub:"))
                        {
                            search.mode = ConnectionMode.Passive;
                            int username_start = parameters[0].IndexOf(":");
                            if (username_start == -1 || username_start + 1 > parameters[0].Length) break;
                            search.username = parameters[0].Substring(username_start + 1);
                        }
                        else
                        {
                            search.mode = ConnectionMode.Active;
                            int port_start = parameters[0].IndexOf(":");
                            if (port_start == -1 || port_start + 1 > parameters[0].Length) break;
                            search.ip = parameters[0].Substring(0,port_start);
                            try
                            {
                                search.port = int.Parse(parameters[0].Substring(port_start + 1));
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("error parsing port in search: " + ex.Message);
                                break;
                            }
                        }

                        char[] seps ={ '?' };
                        string[] search_parameters = parameters[1].Split(seps,StringSplitOptions.RemoveEmptyEntries);
                        if (search_parameters.Length < 4) break;
                        if (search_parameters[0] == "F")
                            search.size_restricted = false;
                        else search.size_restricted = true;
                        if (search_parameters[1] == "F")
                            search.is_max_size = false;
                        else search.is_max_size = true;
                        try
                        {
                            search.size = long.Parse(search_parameters[2]);
                            search.file_type = (SearchFileType)int.Parse(search_parameters[3]);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("error parsing ints in search: " + ex.Message);
                            break;
                        }
                        if (search_parameters[4].StartsWith("TTH:") && search.file_type == SearchFileType.tth && search_parameters[4].Length > 4)
                            search.tth = search_parameters[4].Substring(4);
                        else search.search_string = search_parameters[4];
                        //TODO above throws exceptions
                        if (SearchReceived != null)
                            SearchReceived(this,search);
                        break;

                    case "Supports":
                        supports = (string[])parameters.Clone();
                        break;

                    case "UserIP":
                        Console.WriteLine("UserIP Message received: " + parameter);
                        break;

                    case "MCTo:":
                        string mcto_username = parameters[1].Substring(1); //to skip the leading $
                        int mcto_message_start = parameters[0].Length + parameters[1].Length + 2;
                        if (mcto_message_start < parameter.Length)
                        {
                            string mcto_message = parameter.Substring(mcto_message_start);
                            AddChatToHistory(mcto_username, mcto_message);
                        }
                        break;

                    case "To:":
                        //Console.WriteLine("Private Message received: " + parameter);
                        string to_username = parameters[2];
                        int to_message_start = parameters[0].Length + parameters[1].Length + parameters[2].Length + parameters[3].Length + 4;
                        if (to_message_start < parameter.Length )
                        {
                            string to_message = parameter.Substring(to_message_start);
                            ChatLine to_message_line = new ChatLine(to_username, to_message);
                            if (PrivateChatLineReceived != null)
                                PrivateChatLineReceived(this, to_message_line);
                        }
                        break;

                    case "SR":
                        //Console.WriteLine("Search result received: " + parameter);
                        SearchResults.SearchResult result = new SearchResults.SearchResult();
                        result.ResultLine = parameter;
                        try
                        {
                            if (SearchResultReceived != null)
                                SearchResultReceived(this, result);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception in event handler: " + ex.Message);
                        }

                        break;

                    case "LogedIn":
                        Console.WriteLine("LogedIn Message received: " + parameter);
                        //what the hell is this and who forgot to take some english lessons ?
                        break;
                    case "MyINFO":
                        //Console.WriteLine("MyINFO Message received: " + parameter);
                        UserListAdd(parameters[1]);
                        break;
                    case "GetPass":
                        Console.WriteLine("GetPass Message received: " + parameter);
                        if (PasswordRequested != null)
                        {
                            string password = PasswordRequested(this);
                            SendPassword(password);
                        }
                        break;

                    case "ForceMove":
                        //Console.WriteLine("FORCE MOVE NOT IMPLEMENTED");
                        if (MoveForced != null)
                        {
                            Hub dst_hub = this.Copy();
                            MoveForced(this, dst_hub);
                        }
                        break;

                    case "ValidateDenide":
                        Console.WriteLine("Nick: "+parameters[0]+" on Hub: " + name + " is already in use.");
                        break;

                    case "HubIsFull":
                        Console.WriteLine("Hub: " + name + " is full.");
                        Disconnect();
                        break;

                    case "Lock" :
                        //Console.WriteLine("Lock Command received: "+parameter);
                        //int key_end = parameter.IndexOf(" ");
                        //if (key_end != -1)
                        //{
                            //string key = parameter.Substring(0, key_end);
                        if (parameters.Length > 1)
                        {
                            string key = parameters[0];
                            //Console.WriteLine("Key: " + key);
                            if (key.StartsWith("EXTENDEDPROTOCOL"))
                            {
                                is_extended_protocol = true;
                                //Console.WriteLine("Hub is using the dc++ protocol enhancements.");
                                //SendCommand("Supports", "UserCommand NoGetINFO NoHello UserIP2 TTHSearch ZPipe0 GetZBlock ");
                                SendCommand("Supports", "UserCommand TTHSearch NoGetINFO NoHello ");
                            }

                            //string decoded_key = MyLockToKey(key);
                            string decoded_key = L2K(key);
                            //Console.WriteLine("Decoded key: " + decoded_key);
                            SendCommand("Key" , decoded_key);
                            SendCommand("ValidateNick", nick);

                        }
                        break;
                    default:
                        Console.WriteLine("Unknown Command received: " + command + ", Parameter: " + parameter);
                        break;
                }
            }
            else Console.WriteLine("Error interpreting command: " + received_command);
        }
        /// <summary>
        /// Interpret a single command 
        /// in this case only $SR search results received via udp
        /// </summary>
        /// <param name="received_command">the command to interpret</param>
        private void InterpretCommand(string received_command)
        {
            int command_end = received_command.IndexOf(" ");
            if (command_end != -1)
            {
                string command = received_command.Substring(1, command_end - 1);
                string parameter = received_command.Substring(command_end + 1);
                string[] parameters = parameter.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                //Console.WriteLine("Command: '" + command + "' ,Parameter("+parameters.Length+"): '" + parameter + "'");

                switch (command)
                {
                    case "SR":
                        //Console.WriteLine("Search result received: " + parameter);
                        SearchResults.SearchResult result = new SearchResults.SearchResult();
                        result.ResultLine = parameter;
                        try
                        {
                            if (SearchResultReceived != null)
                                SearchResultReceived( result);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception in event handler: " + ex.Message);
                        }

                        break;

                    default:
                        Console.WriteLine("Unknown Command received: " + command + ", Parameter: " + parameter);
                        break;
                }
            }
            else Console.WriteLine("Error interpreting command: " + received_command);
        }