コード例 #1
0
ファイル: IsGd.cs プロジェクト: Manishearth/helpmebot
        /// <summary>
        /// Shortens the specified long URL.
        /// </summary>
        /// <param name="longUrl">The long URL.</param>
        /// <returns></returns>
        public static Uri shorten(Uri longUrl)
        {
            DAL.Select q = new DAL.Select("suc_shorturl");
            q.setFrom("shorturlcache");
            q.addWhere(new DAL.WhereConds("suc_fullurl", longUrl.ToString()));
            string cachelookup = DAL.singleton().executeScalarSelect(q);

            if (cachelookup == "")
            {
                try
                {
                    string shorturl = getShortUrl(longUrl);
                    DAL.singleton().insert("shorturlcache", "", longUrl.ToString(), shorturl);
                    return(new Uri(shorturl));
                }
                catch (WebException ex)
                {
                    GlobalFunctions.errorLog(ex);
                    return(longUrl);
                }
            }

            return(new Uri(cachelookup));
        }
コード例 #2
0
        private void ialDataRecievedEvent(string data)
        {
            Logger.instance().addToLog(data, Logger.LogTypes.IRC);

            char[] colonSeparator = { ':' };

            string command, parameters;
            string messagesource = command = parameters = "";

            basicParser(data, ref messagesource, ref command, ref parameters);

            User source = new User();

            if (messagesource != null)
            {
                source = User.newFromString(messagesource, _networkId);
            }

            switch (command)
            {
            case "ERROR":
                if (parameters.ToLower().Contains(":closing link"))
                {
                    _tcpClient.Close();
                    _ircReaderThread.Abort();
                    _ircWriterThread.Abort();
                }
                break;

            case "PING":
                this.pingEvent(parameters);
                break;

            case "NICK":
                this.nicknameChangeEvent(source.nickname, parameters.Substring(1));
                break;

            case "MODE":
                try
                {
                    string subject     = parameters.Split(' ')[0];
                    string flagchanges = parameters.Split(' ')[1];
                    string param       = parameters.Split(' ').Length > 2 ? parameters.Split(' ')[2] : "";

                    this.modeChangeEvent(source, subject, flagchanges, param);
                }
                catch (NullReferenceException ex)
                {
                    GlobalFunctions.errorLog(ex);
                }
                break;

            case "QUIT":
                this.quitEvent(source, parameters);
                break;

            case "JOIN":
                this.joinEvent(source, parameters);
                break;

            case "PART":
                this.partEvent(source, parameters.Split(' ')[0],
                               parameters.Contains(new String(colonSeparator))
                                  ? parameters.Split(colonSeparator, 2)[1]
                                  : string.Empty);
                break;

            case "TOPIC":
                this.topicEvent(source, parameters.Split(' ')[0], parameters.Split(colonSeparator, 2)[1]);
                break;

            case "INVITE":
                this.inviteEvent(source, parameters.Split(' ')[0], parameters.Split(' ')[1].Substring(1));
                break;

            case "KICK":
                this.kickEvent(source, parameters.Split(' ')[0], parameters.Split(' ')[1],
                               parameters.Split(colonSeparator, 2)[1]);
                break;

            case "PRIVMSG":
                string        message = parameters.Split(colonSeparator, 2)[1];
                ASCIIEncoding asc     = new ASCIIEncoding();
                byte[]        ctcp    = { Convert.ToByte(1) };

                string destination = parameters.Split(colonSeparator, 2)[0].Trim();
                if (destination == this.ircNickname)
                {
                    destination = source.nickname;
                }

                if (message.StartsWith(asc.GetString(ctcp)))
                {
                    this.ctcpEvent(
                        source,
                        destination,
                        message.Trim(Convert.ToChar(Convert.ToByte(1)))
                        );
                }
                else
                {
                    this.privmsgEvent(source, destination, message.Trim());
                }
                break;

            case "NOTICE":
                string noticedestination = parameters.Split(colonSeparator, 2)[0].Trim();
                if (noticedestination == this.ircNickname)
                {
                    noticedestination = source.nickname;
                }
                this.noticeEvent(source, noticedestination, parameters.Split(colonSeparator, 2)[1]);
                break;

            case "001":
                this.connectionRegistrationSucceededEvent();
                break;

            case "002":
                this.RPL_YourHostEvent(parameters);
                break;

            case "003":
                this.RPL_CreatedEvent(parameters);
                break;

            case "004":
                this.RPL_MyInfoEvent(parameters);
                break;

            case "433":
                this.errNicknameInUseEvent();
                break;

            case "437":
                this.errUnavailResource();
                break;

            default:
                this.unrecognisedDataRecievedEvent(data);
                break;
            }
        }
コード例 #3
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destination">The destination.</param>
        /// <param name="command">The command.</param>
        /// <param name="args">The args.</param>
        public void handleCommand(User source, string destination, string command, string[] args)
        {
            Logger.instance().addToLog("Handling recieved message...", Logger.LogTypes.General);

            // if on ignore list, ignore!
            if (source.accessLevel == User.UserRights.Ignored)
            {
                return;
            }

            // flip destination over if required
            if (destination == Helpmebot6.irc.ircNickname)
            {
                destination = source.nickname;
            }


            /*
             * check category codes
             */
            if (WatcherController.instance().isValidKeyword(command))
            {
                int argsLength = GlobalFunctions.realArrayLength(args);

                string[] newArgs     = new string[argsLength + 1];
                int      newArrayPos = 1;
                for (int i = 0; i < args.Length; i++)
                {
                    if (!String.IsNullOrEmpty(args[i]))
                    {
                        newArgs[newArrayPos] = args[i];
                    }
                    newArrayPos++;
                }
                newArgs[0] = command;
                string directedTo          = findRedirection(destination, ref newArgs);
                CommandResponseHandler crh = new CategoryWatcher().run(source, destination, newArgs);
                this.handleCommandResponseHandler(source, destination, directedTo, crh);
                return;
            }

            /*
             * Check for a valid command
             * search for a class that can handle this command.
             */

            // Create a new object which holds the type of the command handler, if it exists.
            // if the command handler doesn't exist, then this won't be set to a value
            Type commandHandler =
                Type.GetType("helpmebot6.Commands." + command.Substring(0, 1).ToUpper() + command.Substring(1).ToLower());

            // check the type exists
            if (commandHandler != null)
            {
                string directedTo = findRedirection(destination, ref args);

                // create a new instance of the commandhandler.
                // cast to genericcommand (which holds all the required methods to run the command)
                // run the command.
                CommandResponseHandler response = ((GenericCommand)Activator.CreateInstance(commandHandler)).run(
                    source, destination, args);
                this.handleCommandResponseHandler(source, destination, directedTo, response);
                return;
            }

            /*
             * Check for a learned word
             */
            {
                WordLearner.RemeberedWord rW  = WordLearner.remember(command);
                CommandResponseHandler    crh = new CommandResponseHandler();
                string wordResponse           = rW.phrase;
                string directedTo             = "";
                if (wordResponse != String.Empty)
                {
                    if (source.accessLevel < User.UserRights.Normal)
                    {
                        crh.respond(new Message().get("accessDenied"),
                                    CommandResponseDestination.PrivateMessage);
                        string[] aDArgs = { source.ToString(), MethodBase.GetCurrentMethod().Name };
                        crh.respond(new Message().get("accessDeniedDebug", aDArgs),
                                    CommandResponseDestination.ChannelDebug);
                    }
                    else
                    {
                        wordResponse = String.Format(wordResponse, args);
                        if (rW.action)
                        {
                            crh.respond(IAL.wrapCTCP("ACTION", wordResponse));
                        }
                        else
                        {
                            directedTo = findRedirection(destination, ref args);
                            crh.respond(wordResponse);
                        }
                        this.handleCommandResponseHandler(source, destination, directedTo, crh);
                    }
                    return;
                }
            }
        }