/// <summary> /// 取群成员信息。 /// <para> /// 多线程同步等待,采用阻塞线程的方式等待客户端返回群成员信息,响应时间较慢,建议使用缓存。 /// </para> /// <para> /// 缓存时长1天,超过1天的成员,在下次访问时会通过酷Q重新获取最新信息。 /// </para> /// </summary> /// <param name="groupNumber">群号码。</param> /// <param name="qqNumber">被操作的QQ号码。</param> /// <param name="cache">是否使用缓存(使用缓存后,当后第一次访问会通过客户端读取,之后每次都通过缓存获得)。</param> public static CQGroupMemberInfo GetGroupMemberInfo(long groupNumber, long qqNumber, bool cache = true) { lock (_syncRoot) { Dictionary <long, CQGroupMemberInfo> dicMemebers = new Dictionary <long, CQGroupMemberInfo>(); if (_dicCache.ContainsKey(groupNumber)) { dicMemebers = _dicCache[groupNumber]; } else { _dicCache.Add(groupNumber, dicMemebers); } CQGroupMemberInfo member = new CQGroupMemberInfo(); if (dicMemebers.ContainsKey(qqNumber)) { member = dicMemebers[qqNumber]; } else { dicMemebers.Add(qqNumber, member); } if (!cache || member.RefreshDate.Date.AddDays(1) < DateTime.Now) { CQLogger.GetInstance().AddLog(String.Format("[↓][成员] 群:{0} QQ:{1}", groupNumber, qqNumber)); string content = String.Empty; if (CQ.ProxyType == CQProxyType.UDP) { content = String.Format("GroupMemberRequest|{0}|{1}", groupNumber, qqNumber); member = CQUDPProxy.GetInstance().GetGroupMemberInfo(content); } if (CQ.ProxyType == CQProxyType.NativeClr) { content = CQAPI.GetGroupMemberInfo(CQAPI.GetAuthCode(), groupNumber, qqNumber, cache ? 1 : 0); member = CQMessageAnalysis.AnalyzeGroupMember(content); } if (cache) { dicMemebers[qqNumber] = member; } } return(member); } return(new CQGroupMemberInfo()); }
/// <summary> /// 获取群成员列表,阻塞线程,等待客户端响应(有风险,待测试)。 /// </summary> internal CQGroupMemberInfo GetGroupMemberInfo(string message) { byte[] data = Encoding.Default.GetBytes(message); this.mySocket.SendTo(data, data.Length, SocketFlags.None, RemotePoint); myResetEvent.WaitOne(2000); try { CQGroupMemberInfo info = CQMessageAnalysis.AnalyzeGroupMember(this._strMessage); this._strMessage = String.Empty; return(info); } catch { return(new CQGroupMemberInfo()); } }
/// <summary> /// 解析消息。 /// </summary> /// <param name="data"></param> private void AnalyzeMessage(object data) { string[] args = data.ToString().Split(new char[] { '|' }); if (args.Length == 12) { string eventType = args[0]; //1事件类型 int subType = Convert.ToInt32(args[1]); //2子类型 int sendTime = String.IsNullOrEmpty(args[2]) ? 0 : Convert.ToInt32(args[2]); //3发送时间(时间戳) long fromGroup = String.IsNullOrEmpty(args[3]) ? 0 : Convert.ToInt64(args[3]); //4来源群号 long fromDiscuss = String.IsNullOrEmpty(args[4]) ? 0 : Convert.ToInt64(args[4]); //5来源讨论组 long fromQQ = String.IsNullOrEmpty(args[5]) ? 0 : Convert.ToInt64(args[5]); //6来源QQ string fromAnonymous = args[6]; //7来源匿名者 long beingOperateQQ = String.IsNullOrEmpty(args[7]) ? 0 : Convert.ToInt64(args[7]); //8被操作QQ string msg = args[8].Replace("$内容分割$", "|"); //9消息内容 int font = String.IsNullOrEmpty(args[9]) ? 0 : Convert.ToInt32(args[9]); //10字体 string responseFlag = args[10]; //11反馈标识(处理请求用) string file = args[11]; //12上传文件信息 switch (eventType) { case "PrivateMessage": //私聊消息 CQLogger.GetInstance().AddLog(String.Format("[↓][私聊] QQ:{0} {1}", fromQQ, msg)); break; case "GroupMessage": //群消息 CQLogger.GetInstance().AddLog(String.Format("[↓][群聊] 群:{0} QQ:{1} {2}", fromGroup, fromQQ, msg)); break; case "DiscussMessage": //讨论组消息 CQLogger.GetInstance().AddLog(String.Format("[↓][讨论] 组:{0} QQ:{1} {2}", fromDiscuss, fromQQ, msg)); break; case "GroupUpload": //群文件上传事件 CQLogger.GetInstance().AddLog(String.Format("[↓][上传] 群:{0} QQ:{1} {2}", fromGroup, fromQQ, file)); break; case "GroupAdmin": //群事件-管理员变动 CQLogger.GetInstance().AddLog(String.Format("[↓][管理] 群:{0} QQ:{1}", fromGroup, beingOperateQQ)); break; case "GroupMemberDecrease": //群事件-群成员减少 CQLogger.GetInstance().AddLog(String.Format("[↓][减员] 群:{0} QQ:{1} OperateQQ:{2}", fromGroup, fromQQ, beingOperateQQ)); break; case "GroupMemberIncrease": //群事件-群成员增加 CQLogger.GetInstance().AddLog(String.Format("[↓][增员] 群:{0} QQ:{1} OperateQQ:{2}", fromGroup, fromQQ, beingOperateQQ)); break; case "FriendAdded": //好友事件-好友已添加 CQLogger.GetInstance().AddLog(String.Format("[↓][加友] QQ:{0}", fromQQ)); break; case "RequestAddFriend": //请求-好友添加 CQLogger.GetInstance().AddLog(String.Format("[↓][请友] QQ:{0} {1}", fromQQ, msg)); break; case "RequestAddGroup": //请求-群添加 CQLogger.GetInstance().AddLog(String.Format("[↓][请群] 群:{0} QQ:{1} {2}", fromGroup, fromQQ, msg)); break; } } CQMessageAnalysis.Analyze(data.ToString()); }