Beispiel #1
0
        protected override async Task <ActionEvent> NotifyActionEventAsync(ActionEvent actionEvent)
        {
            var type     = actionEvent.Type;
            var typeName = type.GetDescription();
            var target   = actionEvent.Target;

            switch (type)
            {
            case ActionEventType.EvtError:
            {
                var ex = (QQException)target;
                Logger.LogError($"[Action={ActionName}, Result={typeName}, {ex}");
                await _context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.Error, ex));

                break;
            }

            case ActionEventType.EvtRetry:
            {
                var ex = (QQException)target;
                Logger.LogWarning($"[Action={ActionName}, Result={typeName}, RetryTimes={RetryTimes}][{ex.ToSimpleString()}]");
                break;
            }

            case ActionEventType.EvtCanceled:
                Logger.LogWarning($"[Action={ActionName}, Result={typeName}, Target={target}]");
                break;

            default:
                Logger.LogDebug($"[Action={ActionName}, Result={typeName}]");
                break;
            }
            return(await base.NotifyActionEventAsync(actionEvent));
        }
Beispiel #2
0
        private void HandleMessage(JToken resultValue, List <QQNotifyEvent> events)
        {
            /*
             *  {
             *      "result": [
             *          {
             *              "poll_type": "message",
             *              "value": {
             *                  "content": [
             *                      [
             *                          "font",
             *                          {
             *                              "color": "000000",
             *                              "name": "微软雅黑",
             *                              "size": 10,
             *                              "style": [
             *                                  0,
             *                                  0,
             *                                  0
             *                              ]
             *                          }
             *                      ],
             *                      [
             *                          "face",
             *                          107
             *                      ],
             *                      "xxx",
             *                      [
             *                          "face",
             *                          107
             *                      ]
             *                  ],
             *                  "from_uin": 3219658576,
             *                  "msg_id": 1019,
             *                  "msg_type": 0,
             *                  "time": 1479178662,
             *                  "to_uin": 89009143
             *              }
             *          }
             *      ],
             *      "retcode": 0
             *  }
             */
            var msg = resultValue.ToObject <FriendMessage>();

            msg.Friend = Store.GetOrAddFriendByUin(msg.FromUin, u =>
            {
                events.Add(QQNotifyEvent.CreateEvent(QQNotifyEventType.NeedUpdateFriends));
                return(new QQFriend()
                {
                    Uin = u
                });
            });
            msg.Contents = ContentFatory.ParseContents(resultValue["content"].ToJArray());

            events.Add(QQNotifyEvent.CreateEvent(QQNotifyEventType.ChatMsg, msg));
        }
Beispiel #3
0
        private Task <ActionEvent> HandlePollMsg(JToken result)
        {
            var array        = result as JArray;
            var notifyEvents = new List <QQNotifyEvent>();

            if (array != null)
            {
                foreach (var item in array)
                {
                    var type  = item["poll_type"].ToObject <PollType>();
                    var value = item["value"];
                    switch (type)
                    {
                    case PollType.InputNotify:
                        break;

                    case PollType.Message:
                        notifyEvents.Add(QQNotifyEvent.CreateEvent(QQNotifyEventType.ChatMsg, HandleMessage(value)));
                        break;

                    case PollType.GroupMessage:
                        notifyEvents.Add(QQNotifyEvent.CreateEvent(QQNotifyEventType.GroupMsg, HandleGroupMessage(value)));
                        break;

                    case PollType.DiscussionMessage:
                        break;

                    case PollType.SessionMessage:
                        break;

                    case PollType.ShakeMessage:
                        break;

                    case PollType.KickMessage:
                        break;

                    case PollType.BuddiesStatusChange:
                        break;

                    case PollType.SystemMessage:
                        break;

                    case PollType.GroupWebMessage:
                        break;

                    case PollType.SysGroupMsg:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }

            return(NotifyActionEventAsync(ActionEventType.EvtOK, notifyEvents));
        }
Beispiel #4
0
        public Task <ActionEvent> Login(ActionEventListener listener)
        {
            return(new QQActionFuture(Context, listener)
                   .PushAction <GetQRCodeAction>(async(sender, @event) => // 1.»ñÈ¡¶þάÂë
            {
                if (@event.Type == ActionEventType.EvtOK)
                {
                    var verify = (Image)@event.Target;
                    await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.QRCodeReady, verify));
                }
            })
                   .PushAction <CheckQRCodeAction>(async(sender, @event) => // 2.»ñÈ¡¶þάÂëɨÃè״̬
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }

                var args = (CheckQRCodeArgs)@event.Target;
                switch (args.Status)
                {
                case QRCodeStatus.OK:
                    Context.GetModule <SessionModule>().CheckSigUrl = args.Msg;
                    await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.QRCodeSuccess));
                    break;

                case QRCodeStatus.Valid:
                case QRCodeStatus.Auth:
                    Context.GetSerivce <ILogger>().LogDebug($"¶þάÂë״̬£º{args.Status.GetDescription()}");
                    @event.Type = ActionEventType.EvtRepeat;
                    await Task.Delay(3000);
                    break;

                case QRCodeStatus.Invalid:
                    await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.QRCodeInvalid, args.Msg));
                    break;
                }
            })
                   .PushAction <CheckSigAction>()
                   .PushAction <GetVfwebqqAction>()
                   .PushAction <ChannelLoginAction>(async(sender, @event) => // 2.»ñÈ¡¶þάÂëɨÃè״̬
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }
                await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.LoginSuccess));
            })
                   //.PushAction<GetFriendsAction>()
                   //.PushAction<GetGroupNameListAction>()
                   .PushAction <GetDiscuzListAction>()
                   .PushAction <GetSelfInfoAction>()
                   .PushAction <GetOnlineBuddiesAction>()
                   .ExecuteAsync());
        }
Beispiel #5
0
        private void HandleGroupMessage(JToken resultValue, List <QQNotifyEvent> events)
        {
            /*
             *   {
             *      "result": [
             *          {
             *              "poll_type": "group_message",
             *              "value": {
             *                  "content": [
             *                      [
             *                          "font",
             *                          {
             *                              "color": "000000",
             *                              "name": "微软雅黑",
             *                              "size": 10,
             *                              "style": [
             *                                  0,
             *                                  0,
             *                                  0
             *                              ]
             *                          }
             *                      ],
             *                      "。。。"
             *                  ],
             *                  "from_uin": 3258316418,
             *                  "group_code": 3258316418,
             *                  "msg_id": 18537,
             *                  "msg_type": 0,
             *                  "send_uin": 2515522700,
             *                  "time": 1479175440,
             *                  "to_uin": 89009143
             *              }
             *          }
             *      ],
             *      "retcode": 0
             *  }
             */

            var msg = resultValue.ToObject <GroupMessage>();

            msg.Group = Store.GetOrAddGroupByGid(msg.GroupCode, u =>
            {
                events.Add(QQNotifyEvent.CreateEvent(QQNotifyEventType.NeedUpdateGroups));
                return(new QQGroup {
                    Gid = u
                });
            }); // 此处的GroupCode实际上是Group的gid
            msg.Contents = ContentFatory.ParseContents(resultValue["content"].ToJArray());

            events.Add(QQNotifyEvent.CreateEvent(QQNotifyEventType.ChatMsg, msg));
        }
Beispiel #6
0
        protected override Task <ActionEvent> HandleResponse(HttpResponseItem response)
        {
            var json    = response.ResponseString.ToJToken();
            var retcode = json["retcode"].ToInt();

            switch (retcode)
            {
            case 0: return(HandlePollMsg(json["result"]));

            case 102:
                // 接连正常,没有消息到达 {"retcode":102,"errmsg":""}
                // 继续进行下一个消息请求
                return(NotifyOkEventAsync());

            case 110:
            case 109:
            {
                // 客户端主动退出
                var msg = $"**** 客户端主动退出 retcode: {retcode} ****";
                Logger.LogWarning(msg);
                Session.State = SessionState.Offline;
                return(NotifyOkEventAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.NetError, msg)));
            }


            case 121:
            case 120:
            case 100:
            {
                // 客户端主动退出
                var msg = $"**** 服务器需求重新认证 retcode: {retcode} ****";
                Logger.LogWarning(msg);
                Session.State = SessionState.Offline;
                return(NotifyOkEventAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.NetError, msg)));
            }

            case 103:     // 此时需要登录Smart QQ,确认能收到消息后点击设置-退出登录,就会恢复正常了
            {
                // 客户端主动退出
                var msg = $"**** 需要登录Smart QQ retcode: {retcode} ****";
                Logger.LogWarning(msg);
                // Session.State = SessionState.Offline;
                // return NotifyOkEventAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.NetError, msg));
                return(NotifyOkEventAsync());
            }
            }
            throw new QQException(QQErrorCode.ResponseError, response.ResponseString);
        }
Beispiel #7
0
        public Task <ActionEvent> Login(ActionEventListener listener)
        {
            return(new WebQQActionFuture(Context, listener)
                   .PushAction <GetQRCodeAction>(async(sender, @event) => // 1.获取二维码
            {
                if (@event.Type == ActionEventType.EvtOK)
                {
                    var verify = (Image)@event.Target;
                    await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.QRCodeReady, verify));
                }
            })
                   .PushAction <CheckQRCodeAction>(async(sender, @event) => // 2.获取二维码扫描状态
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }

                var args = (CheckQRCodeArgs)@event.Target;
                switch (args.Status)
                {
                case QRCodeStatus.OK:
                    Session.CheckSigUrl = args.Msg;
                    await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.QRCodeSuccess));
                    break;

                case QRCodeStatus.Valid:
                case QRCodeStatus.Auth:
                    Logger.LogDebug($"二维码状态:{args.Status.GetDescription()}");
                    @event.Type = ActionEventType.EvtRepeat;
                    await Task.Delay(3000);
                    break;

                case QRCodeStatus.Invalid:
                    await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.QRCodeInvalid, args.Msg));
                    break;
                }
            })
                   .PushAction <CheckSigAction>()
                   .PushAction <GetVfwebqqAction>()
                   .PushAction <ChannelLoginAction>(async(sender, @event) =>
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }
                await Context.FireNotifyAsync(QQNotifyEvent.CreateEvent(QQNotifyEventType.LoginSuccess));
            })
                   .PushAction <GetFriendsAction>(async(sender, @event) =>
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }
                var obj = Store.FriendDic.FirstOrDefault().Value;
                if (obj == null)
                {
                    return;
                }
                await new GetFriendLongNickAction(Context, obj).ExecuteAsyncAuto();
                await new GetFriendQQNumberAction(Context, obj).ExecuteAsyncAuto();
                await new GetFriendInfoAction(Context, obj).ExecuteAsyncAuto();
            })
                   .PushAction <GetGroupNameListAction>(async(sender, @event) =>
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }
                var group = Store.GroupDic.FirstOrDefault().Value;
                if (group != null)
                {
                    await new GetGroupInfoAction(Context, group).ExecuteAsyncAuto();
                }
            })
                   .PushAction <GetDiscussionListAction>(async(sender, @event) =>
            {
                if (@event.Type != ActionEventType.EvtOK)
                {
                    return;
                }
                var dis = Store.DiscussionDic.FirstOrDefault().Value;
                if (dis != null)
                {
                    await new GetDiscussionInfoAction(Context, dis).ExecuteAsyncAuto();
                }
            })
                   .PushAction <GetSelfInfoAction>()
                   .PushAction <GetOnlineFriendsAction>()
                   .ExecuteAsync());
        }
Beispiel #8
0
 public static Task FireNotifyAsync(this IQQContext context, QQNotifyEventType type, object target = null)
 {
     return(context.FireNotifyAsync(QQNotifyEvent.CreateEvent(type, target)));
 }