Ejemplo n.º 1
0
        /// <summary>
        /// 构造方法,初始化模块和服务
        /// </summary>
        /// <param name="username">账号</param>
        /// <param name="password">密码</param>
        /// <param name="notifyListener">监听器</param>
        /// <param name="actorDispatcher">线程执行器</param>
        public WebQQClient(string username, string password,
                           QQNotifyHandler notifyListener, IQQActorDispatcher actorDispatcher)
        {
            _modules  = new Dictionary <QQModuleType, IQQModule>();
            _services = new Dictionary <QQServiceType, IQQService>();

            _modules.Add(QQModuleType.LOGIN, new LoginModule());
            _modules.Add(QQModuleType.PROC, new ProcModule());
            _modules.Add(QQModuleType.USER, new UserModule());
            _modules.Add(QQModuleType.BUDDY, new BuddyModule());
            _modules.Add(QQModuleType.CATEGORY, new CategoryModule());
            _modules.Add(QQModuleType.GROUP, new GroupModule());
            _modules.Add(QQModuleType.CHAT, new ChatModule());
            _modules.Add(QQModuleType.DISCUZ, new DiscuzModule());
            _modules.Add(QQModuleType.EMAIL, new EmailModule());
            _services.Add(QQServiceType.HTTP, new HttpService());

            Account = new QQAccount
            {
                Username = username,
                Password = password
            };
            Session          = new QQSession();
            Store            = new QQStore();
            NotifyListener   = notifyListener;
            _actorDispatcher = actorDispatcher;

            Init();
        }
Ejemplo n.º 2
0
        private void AfterInitialize()
        {
            _eventHandler = (sender, Event) =>
            {
                switch (Event.Type)
                {
                    case QQActionEventType.EVT_CANCELED:
                    case QQActionEventType.EVT_ERROR:
                    ShowMessage(Event.ToString());
                    break;
                }
            };

            _notifyHandler = (sender, Event) =>
            {
                var client = sender as IQQClient;
                if (client == null) return;

                try
                {
                    switch (Event.Type)
                    {
                        case QQNotifyEventType.BUDDY_INPUT:
                        case QQNotifyEventType.BUDDY_STATUS_CHANGE:
                        {
                            break;
                        }

                        case QQNotifyEventType.CHAT_MSG:
                        {
                            var msg = Event.Target as QQMsg;
                            switch (msg.Type)
                            {
                                case QQMsgType.SESSION_MSG:
                                case QQMsgType.BUDDY_MSG:
                                {
                                    if (msg.From.QQ == default(long))
                                    {
                                        client.GetUserInfo(msg.From);
                                    }
                                    ShowMessage(string.Format("{0}:【{1}】消息:{2}",
                                        client.Account.QQ, msg.From.Nickname, msg.GetText()));

                                    if (chkAutoReply.Checked)
                                    {
                                        var msgReply = new QQMsg()
                                        {
                                            Type = QQMsgType.BUDDY_MSG,
                                            To = msg.From,
                                            From = client.Account,
                                            Date = DateTime.Now,
                                        };
                                        TextItem text = null;

                                        if (chkUseRobot.Checked)
                                        {
                                            var replyEvent = client.GetRobotReply(msg,
                                                RobotType.Tuling).WaitFinalEvent(10000);
                                            if (replyEvent.Type == QQActionEventType.EVT_OK)
                                            {
                                                text = new TextItem(replyEvent.Target as string);
                                            }
                                            else
                                            {
                                                text = new TextItem("这是自动回复");
                                            }
                                        }
                                        else
                                        {
                                            text = new TextItem("这是自动回复");
                                        }
                                        Thread.Sleep(3000);
                                        client.SendMsg(msgReply);
                                        msgReply.ContentList.Add(text);
                                        ShowMessage(string.Format("{0}:自动回复给【{1}】:{2}",
                                            client.Account.QQ, msg.From.Nickname, text.ToText()));
                                    }
                                    break;
                                }

                                case QQMsgType.DISCUZ_MSG:
                                {
                                    ShowMessage(string.Format("{0}:讨论组【{1}】来自【{2}】消息:{3}",
                                        client.Account.QQ, msg.Discuz, msg.From.Nickname, msg.GetText()));
                                    break;
                                }

                                case QQMsgType.GROUP_MSG:
                                {
                                    ShowMessage(string.Format("{0}:群【{1}】来自【{2}】消息:{3}",
                                        client.Account.QQ, msg.Group, msg.From.Nickname, msg.GetText()));
                                    break;
                                }
                            }
                            break;
                        }

                        case QQNotifyEventType.KICK_OFFLINE:
                        ShowMessage(client.Account.QQ + ":被踢下线-" + (String)Event.Target);
                        break;

                        case QQNotifyEventType.CAPACHA_VERIFY:
                        {
                            var verify = (QQNotifyEventArgs.ImageVerify)Event.Target;
                            this.Invoke(new MethodInvoker(() =>
                            {
                                pbVerifyPic.Image = verify.Image;
                                tbVerifyCode.Enabled = true;
                                tbVerifyCode.Text = "";
                                tbVerifyCode.Focus();
                            }));

                            ShowMessage(verify.Reason);
                            ShowMessage(client.Account.Username + ":请输入验证码:");
                            _verifyCodeInputed.WaitOne();
                            client.SubmitVerify(tbVerifyCode.Text, Event);
                            tbVerifyCode.Invoke(new MethodInvoker(() =>
                            {
                                tbVerifyCode.Enabled = false;
                            }));
                            break;
                        }

                        case QQNotifyEventType.SHAKE_WINDOW:
                        {
                            var buddy = Event.Target as QQBuddy;
                            if (buddy?.QQ == default(long))
                            {
                                client.GetUserQQ(buddy, null);
                            }
                            ShowMessage(string.Format("{0}:【{1}】发来抖动屏幕", client.Account.QQ, buddy.ShowName));
                            if (chkAutoReply.Checked)
                            {
                                client.SendShake(buddy);
                            }
                            break;
                        }

                        case QQNotifyEventType.NET_ERROR:
                        case QQNotifyEventType.UNKNOWN_ERROR:
                        ShowMessage(client.Account.QQ + ":出错-" + Event.Target.ToString());
                        break;

                        default:
                        ShowMessage(client.Account.QQ + ":" + Event.Type + ", " + Event.Target);
                        break;
                    }
                    UpdateQQInfo(client);
                }
                catch (QQException e)
                {
                    ShowMessage(client.Account.QQ + ":" + e.StackTrace);
                }
                catch (Exception e)
                {
                    ShowMessage(e.StackTrace);
                }
            };

            var rightButtonCMS = new ContextMenuStrip();

            var TSMIRemove = new ToolStripMenuItem() { Text = "移除" };
            TSMIRemove.Click += (sender, e) =>
            {
                var TSMI = sender as ToolStripMenuItem;
                var CMS = TSMI?.Owner as ContextMenuStrip;
                if (CMS != null)
                {
                    var LV = CMS.SourceControl as ListView;
                    if (LV == lvQQList)
                    {
                        RemoveSelectedQQFromList();
                    }
                }
            };
            rightButtonCMS.Items.Add(TSMIRemove);
            lvQQList.ContextMenuStrip = rightButtonCMS;
        }
Ejemplo n.º 3
0
        private void AfterInitialize()
        {
            _eventHandler = (sender, Event) =>
            {
                switch (Event.Type)
                {
                case QQActionEventType.EVT_CANCELED:
                case QQActionEventType.EVT_ERROR:
                    ShowMessage(Event.ToString());
                    break;
                }
            };

            _notifyHandler = (sender, Event) =>
            {
                var client = sender as IQQClient;
                if (client == null)
                {
                    return;
                }

                try
                {
                    switch (Event.Type)
                    {
                    case QQNotifyEventType.BUDDY_INPUT:
                    case QQNotifyEventType.BUDDY_STATUS_CHANGE:
                    {
                        break;
                    }

                    case QQNotifyEventType.CHAT_MSG:
                    {
                        var msg = Event.Target as QQMsg;
                        switch (msg.Type)
                        {
                        case QQMsgType.SESSION_MSG:
                        case QQMsgType.BUDDY_MSG:
                        {
                            if (msg.From.QQ == default(long))
                            {
                                client.GetUserInfo(msg.From);
                            }
                            ShowMessage(string.Format("{0}:【{1}】消息:{2}",
                                                      client.Account.QQ, msg.From.Nickname, msg.GetText()));

                            if (chkAutoReply.Checked)
                            {
                                var msgReply = new QQMsg()
                                {
                                    Type = QQMsgType.BUDDY_MSG,
                                    To   = msg.From,
                                    From = client.Account,
                                    Date = DateTime.Now,
                                };
                                TextItem text = null;

                                if (chkUseRobot.Checked)
                                {
                                    var replyEvent = client.GetRobotReply(msg,
                                                                          RobotType.Tuling).WaitFinalEvent(10000);
                                    text = replyEvent.Type == QQActionEventType.EVT_OK ? new TextItem(replyEvent.Target as string)
                                            : new TextItem("这是自动回复");
                                }
                                else
                                {
                                    text = new TextItem("这是自动回复");
                                }
                                Thread.Sleep(3000);
                                client.SendMsg(msgReply);
                                msgReply.ContentList.Add(text);
                                ShowMessage(string.Format("{0}:自动回复给【{1}】:{2}",
                                                          client.Account.QQ, msg.From.Nickname, text.ToText()));
                            }
                            break;
                        }

                        case QQMsgType.DISCUZ_MSG:
                        {
                            ShowMessage(string.Format("{0}:讨论组【{1}】来自【{2}】消息:{3}",
                                                      client.Account.QQ, msg.Discuz, msg.From.Nickname, msg.GetText()));
                            break;
                        }

                        case QQMsgType.GROUP_MSG:
                        {
                            ShowMessage(string.Format("{0}:群【{1}】来自【{2}】消息:{3}",
                                                      client.Account.QQ, msg.Group, msg.From.Nickname, msg.GetText()));
                            break;
                        }
                        }
                        break;
                    }

                    case QQNotifyEventType.KICK_OFFLINE:
                        ShowMessage(client.Account.QQ + ":被踢下线-" + (String)Event.Target);
                        break;

                    case QQNotifyEventType.CAPACHA_VERIFY:
                    {
                        var verify = (QQNotifyEventArgs.ImageVerify)Event.Target;
                        this.Invoke(new MethodInvoker(() =>
                            {
                                pbVerifyPic.Image    = verify.Image;
                                tbVerifyCode.Enabled = true;
                                tbVerifyCode.Text    = "";
                                tbVerifyCode.Focus();
                            }));

                        ShowMessage(verify.Reason);
                        ShowMessage(client.Account.Username + ":请输入验证码:");
                        _verifyCodeInputed.WaitOne();
                        client.SubmitVerify(tbVerifyCode.Text, Event);
                        tbVerifyCode.Invoke(new MethodInvoker(() =>
                            {
                                tbVerifyCode.Enabled = false;
                            }));
                        break;
                    }

                    case QQNotifyEventType.SHAKE_WINDOW:
                    {
                        var buddy = Event.Target as QQBuddy;
                        if (buddy?.QQ == default(long))
                        {
                            client.GetUserQQ(buddy, null);
                        }
                        ShowMessage(string.Format("{0}:【{1}】发来抖动屏幕", client.Account.QQ, buddy?.ShowName));
                        if (chkAutoReply.Checked)
                        {
                            client.SendShake(buddy);
                        }
                        break;
                    }

                    case QQNotifyEventType.NET_ERROR:
                    case QQNotifyEventType.UNKNOWN_ERROR:
                        ShowMessage(client.Account.QQ + ":出错-" + Event.Target.ToString());
                        break;

                    default:
                        ShowMessage(client.Account.QQ + ":" + Event.Type + ", " + Event.Target);
                        break;
                    }
                    UpdateQQInfo(client);
                }
                catch (QQException e)
                {
                    ShowMessage(client.Account.QQ + ":" + e.StackTrace);
                }
                catch (Exception e)
                {
                    ShowMessage(e.StackTrace);
                }
            };

            var rightButtonCMS = new ContextMenuStrip();

            var TSMIRemove = new ToolStripMenuItem()
            {
                Text = "移除"
            };

            TSMIRemove.Click += (sender, e) =>
            {
                var TSMI = sender as ToolStripMenuItem;
                var CMS  = TSMI?.Owner as ContextMenuStrip;
                if (CMS != null)
                {
                    var LV = CMS.SourceControl as ListView;
                    if (LV == lvQQList)
                    {
                        RemoveSelectedQQFromList();
                    }
                }
            };
            rightButtonCMS.Items.Add(TSMIRemove);
            lvQQList.ContextMenuStrip = rightButtonCMS;
        }