예제 #1
0
        private void PluginUrlShort(bool isPrivate, string channel, string nik, string[] cmd, string cmds)
        {
            if (cmd.Length < 2)
            {
                this.Conf.Irc.SendNotice(nik, (string)Properties.Resources.ResourceManager.GetString("fmtUrlShortHelp", this._ci));
                return;
            }
            if (
                (!string.IsNullOrWhiteSpace(cmd[1])) &&
                (Regex.IsMatch(cmd[1], PluginUrlShort_regExp, RegexOptions.IgnoreCase | RegexOptions.Multiline))
                )
            {
                stWebClient   wcl        = null;
                List <string> UrlList    = new List <string>();
                List <string> ShortList  = new List <string>();
                string []     ApiService = null;

                ApiService = ((cmd.Length > 2) ?
                              IrcCommand.PluginUrlShort_ShortApiService.Find(o =>
                {
                    return(o[0].Equals(cmd[2]));
                }
                                                                             ) :
                              IrcCommand.PluginUrlShort_ShortApiService[0]
                              );
                ApiService = ((ApiService == null) ? IrcCommand.PluginUrlShort_ShortApiService[0] : ApiService);

                foreach (var match in new Regex(PluginUrlShort_regExp).Matches(cmd[1]))
                {
                    UrlList.Add(match.ToString());
                }
                try
                {
                    wcl            = new stWebClient();
                    wcl.wUserAgent = HttpUtil.GetHttpUA(Properties.Settings.Default.setCoCHttpClient);

                    foreach (string URL in UrlList)
                    {
                        ShortList.Add(wcl.DownloadString(ApiService[1] + URL));
                    }
                }
                catch (Exception e)
                {
                    this._Send(
                        true, channel, nik,
                        (
                            string.Format(
                                (string)Properties.Resources.ResourceManager.GetString("cmdUrlShortError", this._ci),
                                cmd[1],
                                e.Message
                                )
                        ).ColorText(IrcFormatText.Color.White, IrcFormatText.Color.Red)
                        );
                    return;
                }
                finally
                {
                    if (wcl != null)
                    {
                        wcl.Dispose();
                    }
                }
                this._Send(
                    isPrivate, channel, nik,
                    string.Format(
                        (string)Properties.Resources.ResourceManager.GetString("cmdUrlShortOK", this._ci),
                        nik,
                        cmd[1],
                        string.Join(", ", ShortList)
                        )
                    );
            }
            else
            {
                this._Send(
                    true, channel, nik,
                    (
                        string.Format(
                            (string)Properties.Resources.ResourceManager.GetString("cmdUrlShortError", this._ci),
                            cmd[1],
                            "not valid URL"
                            )
                    ).ColorText(IrcFormatText.Color.White, IrcFormatText.Color.Red)
                    );
            }
        }
        private void PluginContextUrlTitle(bool isPrivate, string channel, string nik, string[] cmd, string cmds)
        {
            if (
                (string.IsNullOrWhiteSpace(cmds)) ||
                (!Regex.IsMatch(cmds, PluginUrlShort_regExp, RegexOptions.IgnoreCase | RegexOptions.Multiline))
                )
            {
                return;
            }

            stWebClient   wcl     = null;
            List <string> UrlList = new List <string>();

            foreach (var match in new Regex(PluginUrlShort_regExp).Matches(cmds))
            {
                UrlList.Add(match.ToString());
            }
            try
            {
                wcl            = new stWebClient();
                wcl.wUserAgent = HttpUtil.GetHttpUA(Properties.Settings.Default.setCoCHttpClient);

                foreach (string URL in UrlList)
                {
                    string src = wcl.DownloadString(URL);
                    if (string.IsNullOrWhiteSpace(src))
                    {
                        continue;
                    }
                    Match titleMatch = Regex.Match(src, PluginContextUrlTitle_regExp, RegexOptions.IgnoreCase);
                    if (string.IsNullOrWhiteSpace(titleMatch.Groups["Title"].Value))
                    {
                        continue;
                    }
                    this._Send(
                        isPrivate, channel, nik,
                        string.Format(
                            (string)Properties.Resources.ResourceManager.GetString("fmtFetchTitle", this._ci),
                            nik,
                            URL,
                            titleMatch.Groups["Title"].Value
                            )
                        );
                }
            }
            catch (Exception e)
            {
                this.Conf.ILog.LogError(
                    string.Format(
                        (string)Properties.Resources.ResourceManager.GetString("cmdFetchTitleError", this._ci),
                        IrcCommand.thisClass,
                        e.Message
                        )
                    );
                return;
            }
            finally
            {
                if (wcl != null)
                {
                    wcl.Dispose();
                }
            }
        }