github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/listener.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"runtime/debug"
     6  
     7  	"github.com/RomiChan/protobuf/proto"
     8  
     9  	eventConverter "github.com/LagrangeDev/LagrangeGo/client/event"
    10  	"github.com/LagrangeDev/LagrangeGo/client/internal/network"
    11  	"github.com/LagrangeDev/LagrangeGo/client/packets/pb/message"
    12  	msgConverter "github.com/LagrangeDev/LagrangeGo/message"
    13  	"github.com/LagrangeDev/LagrangeGo/utils"
    14  	"github.com/LagrangeDev/LagrangeGo/utils/binary"
    15  )
    16  
    17  // decoders https://github.com/Mrs4s/MiraiGo/blob/54bdd873e3fed9fe1c944918924674dacec5ac76/client/client.go#L150
    18  var decoders = map[string]func(*QQClient, *network.Packet) (any, error){
    19  	"trpc.msg.olpush.OlPushService.MsgPush":            decodeOlPushServicePacket,
    20  	"trpc.qq_new_tech.status_svc.StatusService.KickNT": decodeKickNTPacket,
    21  }
    22  
    23  func decodeOlPushServicePacket(c *QQClient, pkt *network.Packet) (any, error) {
    24  	msg := message.PushMsg{}
    25  	err := proto.Unmarshal(pkt.Payload, &msg)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	pkg := msg.Message
    30  	typ := pkg.ContentHead.Type
    31  	defer func() {
    32  		if r := recover(); r != nil {
    33  			c.error("recovered from panic: %v\n%s", r, debug.Stack())
    34  			c.error("protobuf data: %x", pkt.Payload)
    35  		}
    36  	}()
    37  	if pkg.Body == nil {
    38  		return nil, errors.New("message body is empty")
    39  	}
    40  
    41  	switch typ {
    42  	case 166: // private msg
    43  		c.PrivateMessageEvent.dispatch(c, msgConverter.ParsePrivateMessage(&msg))
    44  		return nil, nil
    45  	case 82: // group msg
    46  		c.GroupMessageEvent.dispatch(c, msgConverter.ParseGroupMessage(&msg))
    47  		return nil, nil
    48  	case 141: // temp msg
    49  		c.TempMessageEvent.dispatch(c, msgConverter.ParseTempMessage(&msg))
    50  		return nil, nil
    51  	case 33: // member increase
    52  		pb := message.GroupChange{}
    53  		err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		c.GroupMemberJoinEvent.dispatch(c, eventConverter.ParseMemberIncreaseEvent(&pb))
    58  		return nil, nil
    59  	case 34: // member decrease
    60  		pb := message.GroupChange{}
    61  		err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
    62  		if err != nil {
    63  			return nil, err
    64  		}
    65  		// 3 是bot自身被踢出,Operator字段会是一个protobuf
    66  		if pb.DecreaseType == 3 && pb.Operator != nil {
    67  			Operator := message.OperatorInfo{}
    68  			err = proto.Unmarshal(pb.Operator, &Operator)
    69  			if err != nil {
    70  				return nil, err
    71  			}
    72  			pb.Operator = utils.S2B(Operator.OperatorField1.OperatorUid)
    73  		}
    74  		c.GroupMemberLeaveEvent.dispatch(c, eventConverter.ParseMemberDecreaseEvent(&pb))
    75  		return nil, nil
    76  	case 84: // group request join notice
    77  		pb := message.GroupJoin{}
    78  		err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
    79  		if err != nil {
    80  			return nil, err
    81  		}
    82  		c.GroupMemberJoinRequestEvent.dispatch(c, eventConverter.ParseRequestJoinNotice(&pb))
    83  		return nil, nil
    84  	case 525: // group request invitation notice
    85  		pb := message.GroupInvitation{}
    86  		err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		c.GroupMemberJoinRequestEvent.dispatch(c, eventConverter.ParseRequestInvitationNotice(&pb))
    91  		return nil, nil
    92  	case 87: // group invite notice
    93  		pb := message.GroupInvite{}
    94  		err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  		c.GroupInvitedEvent.dispatch(c, eventConverter.ParseInviteNotice(&pb))
    99  		return nil, nil
   100  	case 0x210: // friend event, 528
   101  		subType := pkg.ContentHead.SubType.Unwrap()
   102  		switch subType {
   103  		case 35: // friend request notice
   104  			pb := message.FriendRequest{}
   105  			err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
   106  			if err != nil {
   107  				return nil, err
   108  			}
   109  			c.FriendRequestEvent.dispatch(c, eventConverter.ParseFriendRequestNotice(&pb, &msg))
   110  			return nil, nil
   111  		case 138: // friend recall
   112  			pb := message.FriendRecall{}
   113  			err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
   114  			if err != nil {
   115  				return nil, err
   116  			}
   117  			c.FriendRecallEvent.dispatch(c, eventConverter.ParseFriendRecallEvent(&pb))
   118  			return nil, nil
   119  		case 39: // friend rename
   120  			c.debugln("friend rename")
   121  			pb := message.FriendRenameMsg{}
   122  			err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
   123  			if err != nil {
   124  				return nil, err
   125  			}
   126  			c.RenameEvent.dispatch(c, eventConverter.ParseFriendRenameEvent(&pb, c.cache.GetUin(pb.Body.Data.Uid)))
   127  			return nil, nil
   128  		case 29:
   129  			c.debugln("self rename")
   130  			pb := message.SelfRenameMsg{}
   131  			err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
   132  			if err != nil {
   133  				return nil, err
   134  			}
   135  			c.RenameEvent.dispatch(c, eventConverter.ParseSelfRenameEvent(&pb, &c.transport.Sig))
   136  			return nil, nil
   137  		default:
   138  			c.warning("unknown subtype %d of type 0x210, proto data: %x", subType, pkg.Body.MsgContent)
   139  		}
   140  	case 0x2DC: // grp event, 732
   141  		subType := pkg.ContentHead.SubType.Unwrap()
   142  		switch subType {
   143  		case 20: // nudget(grp_id only)
   144  			return nil, nil
   145  		case 17: // recall
   146  			reader := binary.NewReader(pkg.Body.MsgContent)
   147  			_ = reader.ReadU32() // group uin
   148  			_ = reader.ReadU8()  // reserve
   149  			pb := message.NotifyMessageBody{}
   150  			err = proto.Unmarshal(reader.ReadBytesWithLength("u16", false), &pb)
   151  			if err != nil {
   152  				return nil, err
   153  			}
   154  			c.GroupRecallEvent.dispatch(c, eventConverter.ParseGroupRecallEvent(&pb))
   155  			return nil, nil
   156  		case 12: // mute
   157  			pb := message.GroupMute{}
   158  			err = proto.Unmarshal(pkg.Body.MsgContent, &pb)
   159  			if err != nil {
   160  				return nil, err
   161  			}
   162  			c.GroupMuteEvent.dispatch(c, eventConverter.ParseGroupMuteEvent(&pb))
   163  			return nil, nil
   164  		default:
   165  			c.warning("Unsupported group event, subType: %v", subType)
   166  		}
   167  	default:
   168  		c.warning("Unsupported message type: %v", typ)
   169  	}
   170  
   171  	return nil, nil
   172  }
   173  
   174  func decodeKickNTPacket(c *QQClient, pkt *network.Packet) (any, error) {
   175  	return nil, nil
   176  }