Example #1
0
        public Search(Hub hub, SearchInfo info)
            : base(hub, null)
        {
            this.info = info;

            #region Id
            string id = null;
            switch (hub.Me.Mode)
            {
            case FlowLib.Enums.ConnectionTypes.Direct:
            case FlowLib.Enums.ConnectionTypes.UPnP:
            case FlowLib.Enums.ConnectionTypes.Forward:
                string port = hub.Share.Port.ToString();
                if (hub.Me.ContainsKey(UserInfo.UDPPORT))
                {
                    port = hub.Me.Get(UserInfo.UDPPORT);
                }

                if (hub.Me.ContainsKey(UserInfo.IP))
                {
                    id = string.Format("{0}:{1}", hub.Me.Get(UserInfo.IP), port);
                }
                else
                {
                    id = string.Format("{0}:{1}", hub.LocalAddress.Address, port);
                }
                break;

            default:
                id = string.Format("Hub:{0}", hub.Me.ID);
                break;
            }
            #endregion
            string search = string.Empty;
            #region Size
            string size = "F?F?0";
            if (info.ContainsKey(SearchInfo.SIZETYPE))
            {
                switch (info.Get(SearchInfo.SIZETYPE))
                {
                case "1":         // Min Size
                    size = "T?F" + info.Get(SearchInfo.SIZE);
                    break;

                case "2":         // Max Size
                    size = "T:T" + info.Get(SearchInfo.SIZE);
                    break;
                }
            }
            #endregion
            #region Extention
            string type = "?1";
            switch (info.Get(SearchInfo.EXTENTION))
            {
            case "mp3":
            case "mp2":
            case "wav":
            case "au":
            case "rm":
            case "mid":
            case "sm":
                type = "?2";
                break;

            case "zip":
            case "arj":
            case "rar":
            case "lzh":
            case "gz":
            case "z":
            case "arc":
            case "pak":
                type = "?3";
                break;

            case "doc":
            case "txt":
            case "wri":
            case "pdf":
            case "ps":
            case "tex":
                type = "?4";
                break;

            case "pm":
            case "exe":
            case "bat":
            case "com":
                type = "?5";
                break;

            case "gif":
            case "jpg":
            case "jpeg":
            case "bmp":
            case "pcx":
            case "png":
            case "wmf":
            case "psd":
                type = "?6";
                break;

            case "mpg":
            case "mpeg":
            case "avi":
            case "asf":
            case "mov":
                type = "?7";
                break;

            case "$0":
                type = "?8";
                break;

            case "$1":
                type = "?9";
                break;
            }
            #endregion

            string schStr = info.Get(SearchInfo.SEARCH);
            schStr = schStr.Replace(" ", "$");

            #region Is TTH Search?
            switch (info.Get(SearchInfo.TYPE))
            {
            case "1":       // Directory
                type = "?8";
                break;

            case "2":       // TTH
                type   = "?9";
                schStr = "TTH:" + schStr;
                break;
            }
            #endregion

            search = string.Format("{0}{1}?{2}", size, type, schStr);
            Raw    = string.Format("$Search {0} {1}|", id, search);
        }
Example #2
0
 public SCH(IConnection con, SearchInfo info, string userId)
     : base(con, null)
 {
     this.info = info;
     // BSCH NRQF TRUDHPNB4BUIQV2LAI4HDWRL3KLJTUSXCTAMJHNII TOauto
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     #region EX
     if (info.ContainsKey(SearchInfo.EXTENTION))
     {
         string[] ext = info.Get(SearchInfo.EXTENTION).Split(' ');
         foreach (string extention in ext)
         {
             sb.Append(" EX" + AdcProtocol.ConvertOutgoing(extention));
         }
     }
     #endregion
     #region TY
     if (info.ContainsKey(SearchInfo.TYPE))
     {
         switch (info.Get(SearchInfo.TYPE))
         {
             case "0":
                 sb.Append(" TY" + AdcProtocol.ConvertOutgoing("1")); break;
             case "1":
                 sb.Append(" TY" + AdcProtocol.ConvertOutgoing("2")); break;
             case "2":
                 sb.Append(" AN" + info.Get(SearchInfo.SEARCH)); break;
         }
     }
     #endregion
     #region AN
     if (info.ContainsKey(SearchInfo.SEARCH) && info.ContainsKey(SearchInfo.TYPE))
         sb.Append(" AN" + AdcProtocol.ConvertOutgoing(info.Get(SearchInfo.SEARCH)));
     #endregion
     #region NO
     if (info.ContainsKey(SearchInfo.NOSEARCH))
         sb.Append(" NO" + AdcProtocol.ConvertOutgoing(info.Get(SearchInfo.NOSEARCH)));
     #endregion
     #region TO
     if (info.ContainsKey(SearchInfo.TOKEN))
         sb.Append(" TO" + AdcProtocol.ConvertOutgoing(info.Get(SearchInfo.TOKEN)));
     #endregion
     #region Size Type
     if (info.ContainsKey(SearchInfo.SIZETYPE))
     {
         string size = info.Get(SearchInfo.SIZE);
         switch (info.Get(SearchInfo.SIZETYPE))
         {
             case "1":
                 sb.Append(" GE" + size);
                 break;
             case "2":
                 sb.Append(" LE" + size);
                 break;
             case "3":
                 sb.Append(" EQ" + size);
                 break;
         }
     }
     #endregion
     Raw = string.Format("BSCH {0}{1}\n", userId, sb.ToString());
 }
Example #3
0
        public Search(Hub hub, SearchInfo info)
            : base(hub,null)
        {
            this.info = info;

            #region Id
            string id = null;
            switch (hub.Me.Mode)
            {
                case FlowLib.Enums.ConnectionTypes.Direct:
                case FlowLib.Enums.ConnectionTypes.UPnP:
                case FlowLib.Enums.ConnectionTypes.Forward:
                    string port = hub.Share.Port.ToString();
                    if (hub.Me.ContainsKey(UserInfo.UDPPORT))
                        port = hub.Me.Get(UserInfo.UDPPORT);

                    if (hub.Me.ContainsKey(UserInfo.IP))
                    {
                        id = string.Format("{0}:{1}", hub.Me.Get(UserInfo.IP), port);
                    }
                    else
                    {
                        id = string.Format("{0}:{1}", hub.LocalAddress.Address, port);
                    }
                    break;
                default:
                    id = string.Format("Hub:{0}", hub.Me.ID);
                    break;
            }
            #endregion
            string search = string.Empty;
            #region Size
            string size = "F?F?0";
            if (info.ContainsKey(SearchInfo.SIZETYPE))
            {
                switch (info.Get(SearchInfo.SIZETYPE))
                {
                    case "1":     // Min Size
                        size = "T?F" + info.Get(SearchInfo.SIZE);
                        break;
                    case "2":     // Max Size
                        size = "T:T" + info.Get(SearchInfo.SIZE);
                        break;
                }
            }
            #endregion
            #region Extention
            string type = "?1";
            switch (info.Get(SearchInfo.EXTENTION))
            {
                case "mp3":
                case "mp2":
                case "wav":
                case "au":
                case "rm":
                case "mid":
                case "sm":
                    type = "?2";
                    break;
                case "zip":
                case "arj":
                case "rar":
                case "lzh":
                case "gz":
                case "z":
                case "arc":
                case "pak":
                    type = "?3";
                    break;
                case "doc":
                case "txt":
                case "wri":
                case "pdf":
                case "ps":
                case "tex":
                    type = "?4";
                    break;
                case "pm":
                case "exe":
                case "bat":
                case "com":
                    type = "?5";
                    break;
                case "gif":
                case "jpg":
                case "jpeg":
                case "bmp":
                case "pcx":
                case "png":
                case "wmf":
                case "psd":
                    type = "?6";
                    break;
                case "mpg":
                case "mpeg":
                case "avi":
                case "asf":
                case "mov":
                    type = "?7";
                    break;
                case "$0":
                    type = "?8";
                    break;
                case "$1":
                    type = "?9";
                    break;
            }
            #endregion

            string schStr = info.Get(SearchInfo.SEARCH);
            schStr = schStr.Replace(" ", "$");

            #region Is TTH Search?
            switch (info.Get(SearchInfo.TYPE))
            {
                case "1":   // Directory
                    type = "?8";
                    break;
                case "2":   // TTH
                    type = "?9";
                    schStr = "TTH:" + schStr;
                    break;
            }
            #endregion

            search = string.Format("{0}{1}?{2}", size, type, schStr);
            Raw = string.Format("$Search {0} {1}|", id, search);
        }
Example #4
0
        public SCH(IConnection con, SearchInfo info, string userId)
            : base(con, null)
        {
            this.info = info;
            // BSCH NRQF TRUDHPNB4BUIQV2LAI4HDWRL3KLJTUSXCTAMJHNII TOauto
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            #region EX
            if (info.ContainsKey(SearchInfo.EXTENTION))
            {
                string[] ext = info.Get(SearchInfo.EXTENTION).Split(' ');
                foreach (string extention in ext)
                {
                    sb.Append(" EX" + AdcProtocol.ConvertOutgoing(extention));
                }
            }
            #endregion
            #region TY
            if (info.ContainsKey(SearchInfo.TYPE))
            {
                switch (info.Get(SearchInfo.TYPE))
                {
                case "0":
                    sb.Append(" TY" + AdcProtocol.ConvertOutgoing("1")); break;

                case "1":
                    sb.Append(" TY" + AdcProtocol.ConvertOutgoing("2")); break;

                case "2":
                    sb.Append(" AN" + info.Get(SearchInfo.SEARCH)); break;
                }
            }
            #endregion
            #region AN
            if (info.ContainsKey(SearchInfo.SEARCH) && info.ContainsKey(SearchInfo.TYPE))
            {
                sb.Append(" AN" + AdcProtocol.ConvertOutgoing(info.Get(SearchInfo.SEARCH)));
            }
            #endregion
            #region NO
            if (info.ContainsKey(SearchInfo.NOSEARCH))
            {
                sb.Append(" NO" + AdcProtocol.ConvertOutgoing(info.Get(SearchInfo.NOSEARCH)));
            }
            #endregion
            #region TO
            if (info.ContainsKey(SearchInfo.TOKEN))
            {
                sb.Append(" TO" + AdcProtocol.ConvertOutgoing(info.Get(SearchInfo.TOKEN)));
            }
            #endregion
            #region Size Type
            if (info.ContainsKey(SearchInfo.SIZETYPE))
            {
                string size = info.Get(SearchInfo.SIZE);
                switch (info.Get(SearchInfo.SIZETYPE))
                {
                case "1":
                    sb.Append(" GE" + size);
                    break;

                case "2":
                    sb.Append(" LE" + size);
                    break;

                case "3":
                    sb.Append(" EQ" + size);
                    break;
                }
            }
            #endregion
            Raw = string.Format("BSCH {0}{1}\n", userId, sb.ToString());
        }
Example #5
0
        public SCH(IConnection con, string raw)
            : base(con, raw)
        {
            // BSCH NRQF TRUDHPNB4BUIQV2LAI4HDWRL3KLJTUSXCTAMJHNII TOauto
            for (int i = 0; i < param.Count; i++)
            {
                if (param[i].Length < 2)
                {
                    continue;
                }
                string key   = param[i].Substring(0, 2);
                string value = param[i].Substring(2);
                switch (key)
                {
                case "AN":
                    //TODO : We should make it possible to have more then 1 AN.
                    if (info.ContainsKey(SearchInfo.SEARCH))
                    {
                        value = value + " " + info.Get(SearchInfo.SEARCH);
                    }
                    info.Set(SearchInfo.SEARCH, value);
                    valid = true;
                    break;

                case "NO":
                    //TODO : We should make it possible to have more then 1 AN.
                    info.Set(SearchInfo.NOSEARCH, value);
                    break;

                case "EX":
                    if (info.ContainsKey(SearchInfo.EXTENTION))
                    {
                        value = value + " " + info.Get(SearchInfo.EXTENTION);
                    }
                    info.Set(SearchInfo.EXTENTION, value);
                    break;

                case "LE":
                    info.Set(SearchInfo.SIZETYPE, "2");
                    try
                    {
                        info.Set(SearchInfo.SIZE, long.Parse(value).ToString());
                    }
                    catch { }
                    break;

                case "GE":
                    info.Set(SearchInfo.SIZETYPE, "1");
                    try
                    {
                        info.Set(SearchInfo.SIZE, long.Parse(value).ToString());
                    }
                    catch { }
                    break;

                case "EQ":
                    info.Set(SearchInfo.SIZETYPE, "3");
                    try
                    {
                        info.Set(SearchInfo.SIZE, long.Parse(value).ToString());
                    }
                    catch { }
                    break;

                case "TO":
                    info.Set(SearchInfo.TOKEN, value);
                    break;

                case "TY":
                    switch (value)
                    {
                    case "1":           // File
                        break;

                    case "2":           // Directory
                        info.Set(SearchInfo.TYPE, "1");
                        break;

                    default:
                        break;
                    }
                    break;

                case "TR":
                    info.Set(SearchInfo.SEARCH, value);
                    info.Set(SearchInfo.TYPE, "2");
                    valid = true;
                    break;
                }
            }
        }
Example #6
0
        protected void SendRES(SearchInfo info, UserInfo usr)
        {
            IShare share = null;
            if (hub != null || hub.Share != null || usr != null)
            {
                share = hub.Share;
            }
            else if (trans != null || trans.Share != null || usr != null)
            {
                share = trans.Share;
            }
            else
            {
                return;
            }

            // If we dont have a share object. break here.
            if (share == null)
                return;

            int maxReturns = 10;
            string token = null;
            if (info.ContainsKey(SearchInfo.TOKEN))
                token = info.Get(SearchInfo.TOKEN);

            System.Collections.Generic.List<ContentInfo> ret = new System.Collections.Generic.List<ContentInfo>(maxReturns);
            // TODO : This lookup can be done nicer
            lock (share)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, Containers.ContentInfo> var in share)
                {
                    if (var.Value == null)
                        continue;
                    bool foundEnough = false;
                    string ext = info.Get(SearchInfo.EXTENTION);
                    string sch = info.Get(SearchInfo.SEARCH);
                    if (ext != null && sch != null)
                    {
                        ContentInfo contentInfo = new ContentInfo();
                        if (info.ContainsKey(SearchInfo.TYPE))
                        {
                            switch (info.Get(SearchInfo.TYPE))
                            {
                                case "2":

                                    contentInfo.Set(ContentInfo.TTH, info.Get(SearchInfo.SEARCH));
                                    if (hub.Share.ContainsContent(ref contentInfo))
                                    {
                                        ret.Add(contentInfo);
                                    }
                                    // We are looking through whole share here.
                                    // If no TTH matching. Ignore.
                                    foundEnough = true;
                                    break;
                                case "1":
                                default:
                                    if (var.Value.ContainsKey(ContentInfo.VIRTUAL) && (System.IO.Path.GetDirectoryName(var.Value.Get(ContentInfo.VIRTUAL)).IndexOf(sch, System.StringComparison.OrdinalIgnoreCase) != -1))
                                        ret.Add(var.Value);
                                    break;
                            }
                        }
                        if (!foundEnough)
                        {
                            string infoExt = System.IO.Path.GetExtension(var.Value.Get(ContentInfo.VIRTUAL)).TrimStart('.');
                            if (
                                    var.Value.ContainsKey(ContentInfo.VIRTUAL)
                                    && (var.Value.Get(ContentInfo.VIRTUAL).IndexOf(sch, System.StringComparison.OrdinalIgnoreCase) != -1)
                                    && (ext.Length == 0 || ext.Contains(infoExt))
                                )
                                ret.Add(var.Value);
                        }
                    }
                    if (foundEnough || ret.Count >= maxReturns)
                        break;
                }
            }

            int x = 0;

            // Test against size restrictions
            for (int i = 0; i < ret.Count; i++)
            {
                bool send = true;
                long size = -1;
                try
                {
                    size = long.Parse(info.Get(SearchInfo.SIZE));
                }
                catch { }
                if (info.ContainsKey(SearchInfo.SIZETYPE) && size != -1)
                {
                    switch (info.Get(SearchInfo.SIZETYPE))
                    {
                        case "1":       // Min Size
                            send = (size <= ret[i].Size);
                            break;
                        case "2":       // Max Size
                            send = (size >= ret[i].Size);
                            break;
                        case "3":       // Equal Size
                            send = (size == ret[i].Size);
                            break;
                    }
                }
                // Should this be sent?
                if (send)
                {
                    RES res = new RES(con, ret[i], token, usr);
                    if (res.Address != null && hub != null)
                    {
                        if (10 > x++)
                        {
                            // Send with UDP
                            try
                            {
                                UdpConnection.Send(res, res.Address);
                            }
                            catch { }
                        }
                    }
                    else
                    {
                        if (5 > x++)
                        {
                            if (hub != null)
                            {
                                // Send through hub
                                hub.Send(res);
                            }
                            else if (trans != null)
                            {
                                trans.Send(res);
                            }
                        }
                    }
                }
            }
        }