github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/client/system_msg.go (about)

     1  package client
     2  
     3  import (
     4  	"github.com/Mrs4s/MiraiGo/client/internal/network"
     5  	"github.com/Mrs4s/MiraiGo/client/pb/structmsg"
     6  	"github.com/Mrs4s/MiraiGo/internal/proto"
     7  )
     8  
     9  type (
    10  	GroupSystemMessages struct {
    11  		InvitedRequests []*GroupInvitedRequest  `json:"invited_requests"`
    12  		JoinRequests    []*UserJoinGroupRequest `json:"join_requests"`
    13  	}
    14  
    15  	GroupInvitedRequest struct {
    16  		RequestId   int64  `json:"request_id"`
    17  		InvitorUin  int64  `json:"invitor_uin"`
    18  		InvitorNick string `json:"invitor_nick"`
    19  		GroupCode   int64  `json:"group_id"`
    20  		GroupName   string `json:"group_name"`
    21  
    22  		Checked bool  `json:"checked"`
    23  		Actor   int64 `json:"actor"`
    24  
    25  		client *QQClient
    26  	}
    27  
    28  	UserJoinGroupRequest struct {
    29  		RequestId     int64  `json:"request_id"`
    30  		Message       string `json:"message"`
    31  		RequesterUin  int64  `json:"requester_uin"`
    32  		RequesterNick string `json:"requester_nick"`
    33  		GroupCode     int64  `json:"group_id"`
    34  		GroupName     string `json:"group_name"`
    35  		ActionUinNick string `json:"action_uin_nick"`
    36  		ActionUin     int64  `json:"action_uin"`
    37  
    38  		Checked    bool  `json:"checked"`
    39  		Actor      int64 `json:"actor"`
    40  		Suspicious bool  `json:"suspicious"`
    41  
    42  		client *QQClient
    43  	}
    44  )
    45  
    46  func (c *QQClient) GetGroupSystemMessages() (*GroupSystemMessages, error) {
    47  	i, err := c.sendAndWait(c.buildSystemMsgNewGroupPacket(false))
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	msg := i.(*GroupSystemMessages)
    52  	i, err = c.sendAndWait(c.buildSystemMsgNewGroupPacket(true))
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	msg.InvitedRequests = append(msg.InvitedRequests, i.(*GroupSystemMessages).InvitedRequests...)
    57  	msg.JoinRequests = append(msg.JoinRequests, i.(*GroupSystemMessages).JoinRequests...)
    58  	return msg, nil
    59  }
    60  
    61  func (c *QQClient) exceptAndDispatchGroupSysMsg() {
    62  	if c.groupSysMsgCache == nil {
    63  		c.error("warning: groupSysMsgCache is nil")
    64  		c.groupSysMsgCache, _ = c.GetGroupSystemMessages()
    65  		return
    66  	}
    67  	joinExists := func(req int64) bool {
    68  		for _, msg := range c.groupSysMsgCache.JoinRequests {
    69  			if req == msg.RequestId {
    70  				return true
    71  			}
    72  		}
    73  		return false
    74  	}
    75  	invExists := func(req int64) bool {
    76  		for _, msg := range c.groupSysMsgCache.InvitedRequests {
    77  			if req == msg.RequestId {
    78  				return true
    79  			}
    80  		}
    81  		return false
    82  	}
    83  	msgs, err := c.GetGroupSystemMessages()
    84  	if err != nil {
    85  		return
    86  	}
    87  	for _, msg := range msgs.JoinRequests {
    88  		if !joinExists(msg.RequestId) {
    89  			c.UserWantJoinGroupEvent.dispatch(c, msg)
    90  		}
    91  	}
    92  	for _, msg := range msgs.InvitedRequests {
    93  		if !invExists(msg.RequestId) {
    94  			c.GroupInvitedEvent.dispatch(c, msg)
    95  		}
    96  	}
    97  	c.groupSysMsgCache = msgs
    98  }
    99  
   100  // ProfileService.Pb.ReqSystemMsgNew.Group
   101  func (c *QQClient) buildSystemMsgNewGroupPacket(suspicious bool) (uint16, []byte) {
   102  	req := &structmsg.ReqSystemMsgNew{
   103  		MsgNum:    100,
   104  		Version:   1000,
   105  		Checktype: 3,
   106  		Flag: &structmsg.FlagInfo{
   107  			GrpMsgKickAdmin:                   1,
   108  			GrpMsgHiddenGrp:                   1,
   109  			GrpMsgWordingDown:                 1,
   110  			GrpMsgGetOfficialAccount:          1,
   111  			GrpMsgGetPayInGroup:               1,
   112  			FrdMsgDiscuss2ManyChat:            1,
   113  			GrpMsgNotAllowJoinGrpInviteNotFrd: 1,
   114  			FrdMsgNeedWaitingMsg:              1,
   115  			FrdMsgUint32NeedAllUnreadMsg:      1,
   116  			GrpMsgNeedAutoAdminWording:        1,
   117  			GrpMsgGetTransferGroupMsgFlag:     1,
   118  			GrpMsgGetQuitPayGroupMsgFlag:      1,
   119  			GrpMsgSupportInviteAutoJoin:       1,
   120  			GrpMsgMaskInviteAutoJoin:          1,
   121  			GrpMsgGetDisbandedByAdmin:         1,
   122  			GrpMsgGetC2CInviteJoinGroup:       1,
   123  		},
   124  		FriendMsgTypeFlag: 1,
   125  		ReqMsgType: func() int32 {
   126  			if suspicious {
   127  				return 2
   128  			}
   129  			return 1
   130  		}(),
   131  	}
   132  	payload, _ := proto.Marshal(req)
   133  	return c.uniPacket("ProfileService.Pb.ReqSystemMsgNew.Group", payload)
   134  }
   135  
   136  // ProfileService.Pb.ReqSystemMsgAction.Group
   137  func (c *QQClient) buildSystemMsgGroupActionPacket(reqID, requester, group int64, msgType int32, isInvite, accept, block bool, reason string) (uint16, []byte) {
   138  	subSrcId := int32(31)
   139  	groupMsgType := int32(1)
   140  	if isInvite {
   141  		subSrcId = 10016
   142  		groupMsgType = 2
   143  	}
   144  	infoType := int32(12)
   145  	if accept {
   146  		infoType = 11
   147  	}
   148  	req := &structmsg.ReqSystemMsgAction{
   149  		MsgType:      msgType,
   150  		MsgSeq:       reqID,
   151  		ReqUin:       requester,
   152  		SubType:      1,
   153  		SrcId:        3,
   154  		SubSrcId:     subSrcId,
   155  		GroupMsgType: groupMsgType,
   156  		ActionInfo: &structmsg.SystemMsgActionInfo{
   157  			Type:      infoType,
   158  			GroupCode: group,
   159  			Blacklist: block,
   160  			Msg:       reason,
   161  			Sig:       EmptyBytes,
   162  		},
   163  		Language: 1000,
   164  	}
   165  	payload, _ := proto.Marshal(req)
   166  	return c.uniPacket("ProfileService.Pb.ReqSystemMsgAction.Group", payload)
   167  }
   168  
   169  // ProfileService.Pb.ReqSystemMsgAction.Friend
   170  func (c *QQClient) buildSystemMsgFriendActionPacket(reqID, requester int64, accept bool) (uint16, []byte) {
   171  	infoType := int32(3)
   172  	if accept {
   173  		infoType = 2
   174  	}
   175  	req := &structmsg.ReqSystemMsgAction{
   176  		MsgType:  1,
   177  		MsgSeq:   reqID,
   178  		ReqUin:   requester,
   179  		SubType:  1,
   180  		SrcId:    6,
   181  		SubSrcId: 7,
   182  		ActionInfo: &structmsg.SystemMsgActionInfo{
   183  			Type:         infoType,
   184  			Blacklist:    false,
   185  			AddFrdSNInfo: &structmsg.AddFrdSNInfo{},
   186  		},
   187  	}
   188  	payload, _ := proto.Marshal(req)
   189  	return c.uniPacket("ProfileService.Pb.ReqSystemMsgAction.Friend", payload)
   190  }
   191  
   192  // ProfileService.Pb.ReqSystemMsgNew.Group
   193  func decodeSystemMsgGroupPacket(c *QQClient, pkt *network.Packet) (any, error) {
   194  	rsp := structmsg.RspSystemMsgNew{}
   195  	if err := proto.Unmarshal(pkt.Payload, &rsp); err != nil {
   196  		return nil, err
   197  	}
   198  	ret := &GroupSystemMessages{}
   199  	for _, st := range rsp.Groupmsgs {
   200  		if st.Msg == nil {
   201  			continue
   202  		}
   203  		switch st.Msg.SubType {
   204  		case 1, 2: // 处理被邀请入群 或 处理成员入群申请
   205  			switch st.Msg.GroupMsgType {
   206  			case 1: // 成员申请
   207  				ret.JoinRequests = append(ret.JoinRequests, &UserJoinGroupRequest{
   208  					RequestId:     st.MsgSeq,
   209  					Message:       st.Msg.MsgAdditional,
   210  					RequesterUin:  st.ReqUin,
   211  					RequesterNick: st.Msg.ReqUinNick,
   212  					GroupCode:     st.Msg.GroupCode,
   213  					GroupName:     st.Msg.GroupName,
   214  					Checked:       st.Msg.SubType == 2,
   215  					Actor:         st.Msg.ActorUin,
   216  					Suspicious:    len(st.Msg.WarningTips) > 0,
   217  					client:        c,
   218  				})
   219  			case 2: // 被邀请
   220  				ret.InvitedRequests = append(ret.InvitedRequests, &GroupInvitedRequest{
   221  					RequestId:   st.MsgSeq,
   222  					InvitorUin:  st.Msg.ActionUin,
   223  					InvitorNick: st.Msg.ActionUinNick,
   224  					GroupCode:   st.Msg.GroupCode,
   225  					GroupName:   st.Msg.GroupName,
   226  					Checked:     st.Msg.SubType == 2,
   227  					Actor:       st.Msg.ActorUin,
   228  					client:      c,
   229  				})
   230  			case 22: // 群员邀请其他人
   231  				ret.JoinRequests = append(ret.JoinRequests, &UserJoinGroupRequest{
   232  					RequestId:     st.MsgSeq,
   233  					Message:       st.Msg.MsgAdditional,
   234  					RequesterUin:  st.ReqUin,
   235  					RequesterNick: st.Msg.ReqUinNick,
   236  					GroupCode:     st.Msg.GroupCode,
   237  					GroupName:     st.Msg.GroupName,
   238  					Checked:       st.Msg.SubType == 2,
   239  					Actor:         st.Msg.ActorUin,
   240  					Suspicious:    len(st.Msg.WarningTips) > 0,
   241  					client:        c,
   242  					ActionUin:     st.Msg.ActionUin,
   243  					ActionUinNick: st.Msg.ActionUinQqNick,
   244  				})
   245  			default:
   246  				c.debug("unknown group system message type: %v", st.Msg.GroupMsgType)
   247  			}
   248  		case 3: // ?
   249  		case 5: // 自身状态变更(管理员/加群退群)
   250  		default:
   251  			c.debug("unknown group system msg: %v", st.Msg.SubType)
   252  		}
   253  	}
   254  	return ret, nil
   255  }