예제 #1
0
        public Result DoCommand(string strCommand,LocalUser user)
        {
            Result retval = new Result();
            CommandParser parser = new CommandParser(strCommand);

            if (parser.Parse())
            {
                int iListChoice = 0;

                if (int.TryParse(parser.ApplicationName, out iListChoice) && iListChoice > 0)
                {
                    // user entered a number, let's deal with the lastlists
                    UserLastList ll = VBotDB.Instance.UserLastLists.FirstOrDefault(l => l.LocalUserID == user.LocalUserID);

                    if (ll != null)
                    {
                        switch (ll.Name)
                        {
                            case @"forum":
                                retval = GotoForumIndex(iListChoice, user);
                                break;

                            case @"thread":
                                retval = GotoThreadIndex(iListChoice, user);
                                break;

                            case @"post":
                                retval = GotoPostIndex(iListChoice, user, parser);
                                break;

                            default:
                                log.ErrorFormat("Unknown lastlist {0}", ll.Name);
                            break;
                        }
                    }
                    else
                    {
                        retval = new Result(ResultCode.Error, @"Use `lf`,`lt` or `lp` to browse forums, threads and posts.");
                    }
                }
                else
                { // assume a command was entered
                    switch (parser.ApplicationName.ToLower())
                    {
                        #region long switch statement to be refactored
                        case @"\":
                        case "/":
                            retval = GotoForumIndex(-1, user, true);
                            break;

                        case @".":
                            retval = GotoParentForum(user);
                            break;

                        case @"cp":
                            retval = GotoPostIndex(user.PostIndex,user,parser);
                            break;

                        case @"gt":
                            retval = GotoThread(user, parser.Parameters);
                            break;

                        case @"im":
                            retval = TurnOnOffAutoIMS(user, parser.Parameters);
                            break;

                        case "lf":
                            retval = ListForum(user);
                            break;

                        case @"lp":
                            retval = ListPosts(user, parser.Parameters);
                            break;

                        case "lt":
                            retval = ListThreads(user, parser.Parameters);
                            break;

                        case "mfr":
                            retval = MarkRead(user, @"forum");
                            break;

                        case "mtr":
                            retval = MarkRead(user, @"thread");
                            break;

                        case @"n":
                            retval = GotoNextPost(user, true);
                            break;

                        case @"nt":
                            retval = NewThread(user);
                            break;

                        case @"p":
                            retval = GotoNextPost(user, false);
                            break;

                        case @"r":
                            retval = ThreadReply(user,false);
                            break;

                        case @"rq":
                            retval = ThreadReply(user, true);
                            break;

                        case "sub":
                            retval = SubscribeThread(user, parser.Parameters);
                            break;

                        case "unsub":
                            retval = UnsubscribeThread(user, parser.Parameters);
                            break;

                        case @"whereami":
                            retval = WhereAmI(user);
                            break;

                        case "whoami":
                            // TODO: the string in UserConnectionName should come from somewhere else?
                            retval = WhoAmI(ResponseChannel.ToName, ResponseChannel.Connection.Alias);
                        break;

                        default:
                            retval = new Result(ResultCode.Error, @"Unknown command. Please see http://code.google.com/p/vbulletinbot/ for help.");
                        break;

                        #endregion
                    }
                }
            }

            return retval;
        }
예제 #2
0
        public void ExecuteCommand(string strCommand, bool bUseMethodName)
        {
            CommandParser parser = new CommandParser(strCommand, this);
            parser.Parse();

            MethodInfo mi = null;

            if (bUseMethodName)
            {
                mi = this.GetType().GetMethod(parser.ApplicationName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
            }

            if (mi == null)
            {
                foreach (MethodInfo mit in this.GetType().GetMethods())
                {
                    foreach (object obj in mit.GetCustomAttributes(false))
                    {
                        MethodAliasAttribute maa = obj as MethodAliasAttribute;

                        if (maa != null)
                        {
                            if (maa.Aliases != null && maa.Aliases.Contains(parser.ApplicationName))
                            {
                                mi = mit;
                                break;
                            }
                        }
                    }

                    if (mi != null)
                    {
                        break;
                    }
                }
            }

            if (mi != null)
            {
                try
                {
                    if (mi.GetParameters().Length == 0)
                    {
                        mi.Invoke(this, null);
                    }
                    else
                    {
                        object[] oparams = new object[mi.GetParameters().Length];

                        foreach (ParameterInfo m in mi.GetParameters())
                        {
                            if (m.ParameterType == typeof(string))
                            {
                                oparams[oparams.Length - 1] = parser.WorkingString;
                            }
                            else if (m.ParameterType == typeof(CommandParser))
                                oparams[oparams.Length - 1] = parser;
                        }

                        if (oparams != null)
                            mi.Invoke(this, oparams);

                    }
                }
                catch (Exception e)
                {
                    log.Error("Commands.ExecuteCommand exception: " + e.Message);
                }
            }
            else
            {
                log.Info(@"Unknown command");
            }
        }