using Api.Framework; using Api.Framework.Model; using Api.Framework.SDK; using Api.Framework.Tools; using Chat.Framework; using Chat.Framework.WXSdk.Implement; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using 监听违规.Entitys; using 监听违规.Properties; namespace 监听违规 { public class Class1 : Plugin { public Class1() { this.Name = "维护工具"; this.Note = "维护工具"; this.Logo = Resources._3; } #region 自定义变量 public static Config Config; private MainForm mainForm = null; #endregion public override void Start() { try { var session = ApiClient.GetSession(); #region 判断表是否存在,不存在创建表 if (!session.TableExist()) { session.CreateTable(); session.AddIndex("wxid");//增加索引.以后数据多了.查询速度会比较快 session.AddUnique("wxid");//增加唯一约束 } #endregion //创建配置文件 Config = this.ReadConfig(); SDK.ReciveIMEvent += SDK_ReciveIMEvent; } catch (Exception ex) { this.OnLog(ex.Message); } } private List GetSplit(string str) { if (string.IsNullOrWhiteSpace(str)) return null; try { return str.Replace(",", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).ToList(); } catch (Exception ex) { this.OnLog(ex.Message); } return null; } private string Reg_CheckIsUrl = @"(?<链接>((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?)"; private void SDK_ReciveIMEvent(object sender, ReciveIMEvent e) { if (!string.IsNullOrWhiteSpace(e.Groupid)) { if (Regex.IsMatch(e.Message.Trim(), @"(\s*)?") || (e.Message.Trim().StartsWith(@"(.*?)"))) return; var session = ApiClient.GetSession(); //var white = session.FindSingle("wxid = @wxid and user_type", new { wxid = e.Username, user_type = UserType.白名单 }); if (Common.IsWhite(e.Username)) return; Task.Run(() => { var db = ApiClient.GetSession(); try { var client = e.Sender as WeixinBase; if (client == null) return; var black = db.FindSingle("wxid = @wxid and user_type", new { wxid = e.Username, user_type = UserType.黑名单 }); if (black != null) { Action(e, client, false); this.OnLog($"{e.NickName}({e.Username}) 发送消息的用户为黑名单,踢出"); var member = e.GetMemberinfo(); if (member != null) { member.status = Api.Framework.Enums.MemberType.黑名单; //e.SendMessage(new VariateReplace().CommonReplace(Config.expelmess, e.GetMemberinfo(), e.RobotInfo)); db.SaveOrUpdate(member); var user = db.FindSingle("wxid = @wxid", new { wxid = e.Username }); if (user == null) db.Insertable(new UserLists_db() { user_type = UserType.黑名单, wxid = e.Username, nick_name = e.NickName }).ExecuteCommand(); } return; } #region 关键词昵称踢人 if (Config.is_nickkey) { var list = GetSplit(Config.nickkey); if (list != null && list.FirstOrDefault(f => e.NickName.Contains(f)) != null) { Action(e, client, false); //e.SendMessage(new VariateReplace().CommonReplace(Config.expelmess, e.GetMemberinfo(), e.RobotInfo)); return; } } #endregion #region 关键词消息踢人 if (Config.is_messkey && !e.Message.StartsWith(@" e.Message.Contains(f)) != null) { Action(e, client); this.OnLog($"{e.NickName}({e.Username}) 发送的消息包含关键词,踢出"); return; } } #endregion if (!e.Message.Trim().StartsWith(@"", StringComparison.OrdinalIgnoreCase) && !e.Message.Trim().EndsWith("", StringComparison.OrdinalIgnoreCase)) { #region 消息大于多少行踢人 var rows = e.Message.Split('\n').Length; if (Config.is_messrows && rows >= Config.messrows) { Action(e, client); this.OnLog($"{e.NickName}({e.Username}) 发送的消息行数大于{Config.messrows},踢出"); return; } #endregion #region 消息大于多少字踢人 if (Config.is_messwords && e.Message.Trim().Length >= Config.messwords) { Action(e, client); this.OnLog($"{e.NickName}({e.Username}) 发送的消息字数大于{Config.messwords},踢出"); return; } #endregion } #region 发链接、小程序踢人 if (Config.is_link && ((e.Message.Contains(@"((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?)"))) { Action(e, client); this.OnLog($"{e.NickName}({e.Username}) 发送连接或小程序,踢出"); return; } #endregion #region 发名片踢人 if (Config.is_card) { if (Regex.IsMatch(e.Message, @"^<\?xml version=.+antispamticket=(.+) />$")) { Action(e, client); this.OnLog($"{e.NickName}({e.Username}) 发送名片,踢出"); return; } } #endregion #region 图片二维码踢人 if (Config.is_qrcord) { //var regs = Regex.Matches(e.Message, Reg_CheckIsUrl); //if (regs.Count != 0) //{ // foreach (Match reg in regs) // { // var link = reg.Groups["链接"].Value; // Bitmap img = null; // Image mainpic = null; // try // { // string imagePath = string.Empty; // try // { // mainpic = null; // imagePath = Util.MapFile(DateTime.Now.Ticks.ToString() + ".jpg", "Cache\\image"); // if (!link.StartsWith("http")) link = "http:" + link; // mainpic = FileTools.DownloadImage(link, imagePath); // img = new Bitmap(mainpic); // var QRCodeData = Util.DecodeQRCode(img);//解析机器人二维码,获取到二维码的链接 // if (!string.IsNullOrWhiteSpace(QRCodeData)) // { // client.DeleteGroupMember(e.Username, e.Groupid); // return; // } // } // catch (Exception ex) // { // throw new Exception($"读取文件异常:图片路径 = {imagePath} .{ex.Message} - {ex.StackTrace}"); // } // if (mainpic == null) throw new Exception($"下载失败:url = {link}"); // } // catch (Exception) // { // if (img != null) img.Dispose(); // if (mainpic != null) mainpic.Dispose(); // } // } //} var reg = Regex.Match(e.Message, @"\[图片=(.+?)\]"); if (reg.Success) { var file = reg.Groups[1].Value; if (!File.Exists(file)) return; Bitmap img = null; //Image mainpic = null; try { string imagePath = string.Empty; try { //mainpic = null; //imagePath = Util.MapFile(DateTime.Now.Ticks.ToString() + ".jpg", "Cache\\image"); //if (!link.StartsWith("http")) link = "http:" + link; //mainpic = FileTools.DownloadImage(link, imagePath); //img = new Bitmap(mainpic); img = Util.ReadImageFile(file); var QRCodeData = Util.DecodeQRCode(img);//解析机器人二维码,获取到二维码的链接 if (!string.IsNullOrWhiteSpace(QRCodeData)) { //client.DeleteGroupMember(e.Username, e.Groupid); //e.SendMessage(new VariateReplace().CommonReplace(Config.expelmess, e.GetMemberinfo(), e.RobotInfo)); Action(e, client); this.OnLog($"{e.NickName}({e.Username}) 发送的图片中带有二维码,踢出"); return; } } catch (Exception ex) { throw new Exception($"读取文件异常:图片路径 = {imagePath} .{ex.Message} - {ex.StackTrace}"); } //if (mainpic == null) throw new Exception($"下载失败:url = {link}"); } catch (Exception) { if (img != null) img.Dispose(); //if (mainpic != null) mainpic.Dispose(); } //} } } #endregion } catch (Exception ex) { this.OnLog(ex.Message); } }); } } public void Action(ReciveIMEvent e, WeixinBase client, bool isSend = true) { try { string key = "fl_jianting_key_" + e.Username; var falg = ApiClient.Cache.Get(key); if (!string.IsNullOrWhiteSpace(falg)) return; client.DeleteGroupMember(e.Username, e.Groupid); if (isSend) e.SendMessage(new VariateReplace().CommonReplace(Config.expelmess, e.GetMemberinfo(), e.RobotInfo)); ApiClient.Cache.Set(key, "1", 2); } catch (Exception ex) { this.OnLog("缓存异常:" + ex.Message); } } public override void ShowForm() { try { if (mainForm == null || mainForm.IsDisposed) { mainForm = new MainForm(); mainForm.Show(); } mainForm.TopMost = true; mainForm.TopMost = false; } catch (Exception ex) { this.OnLog(ex.Message); } } public override void Stop() { try { if (mainForm != null) { mainForm.CloseForm(); mainForm = null; } } catch (Exception ex) { this.OnLog(ex.Message); } } } }