old_flsystem/应用/MessageSupervises/Class1.cs

386 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Api.Framework;
using Api.Framework.Enums;
using Api.Framework.Model;
using Api.Framework.SDK;
using Api.Framework.Tools;
using Chat.Framework.WXSdk;
using Chat.Framework.WXSdk.Implement;
using MessageSupervises.Entitys;
using MessageSupervises.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MessageSupervises
{
public class Class1 : Plugin
{
public Class1()
{
this.Logo = Resources.;
this.Name = Resources.PluginName;
this.Note = Resources.PluginNote;
}
#region
public static Config Config = null;
private MainForm mainForm = null;
#endregion
public override void Start()
{
try
{
var session = ApiClient.GetSession();
#region
if (!session.TableExist<fl_plugin_messagesupervises_messinfo>())
{
session.CreateTable<fl_plugin_messagesupervises_messinfo>();
session.AddIndex<fl_plugin_messagesupervises_messinfo>("member_id");
}
#endregion
//创建配置文件
Config = this.ReadConfig<Config>();
SDK.ReciveIMEvent += SDK_ReciveIMEvent;
SDK.WXApplyGroupEvent += SDK_WXApplyGroupEvent;
SDK.WXApplyFriendEvent += SDK_WXApplyFriendEvent;
SDK.WXNewFriendEvent += SDK_WXNewFriendEvent;
ApiClient.BlackMemberFunc = () =>
{
return Config.FrequencyCloudBlack;
};
}
catch (Exception ex)
{
this.OnLog(ex.Message);
}
}
private void Ding(string message)
{
if (Config.auth_dingding_swich)
ApiClient.SendNoticeMessage(message, Config.notice_robotname);
}
private bool _check_insert_black(fl_member_info info, WeixinBase client, string remark, string message = "")
{
if (info.status == MemberType.) return false;
if (Config.auth_black_swich && client != null)
{
var session = ApiClient.GetSession();
//15天内自动跳过
if (session.TemporaryBypassedBlack(info))
return false;
bool flag = false;
var time = DateTime.Now - info.crt_time;
if (time.TotalDays >= Config.auto_black_day && info.finish_order <= Config.auto_black_order)
{
var alimama_order = session.Queryable<fl_order_alimama>().Where(f => f.db_userid == info.id).Count();
var pinduoduo_order = session.Queryable<fl_order_pinduoduo>().Where(f => f.db_userid == info.id).Count();
var jingdong_order = session.Queryable<fl_order_jingdong>().Where(f => f.db_userid == info.id).Count();
var count = alimama_order + pinduoduo_order + jingdong_order;
if (Config.auto_black_order >= count)
{
// this.OnLog("订单数量:" + count);
flag = true;
}
}
if (!flag && !string.IsNullOrEmpty(Config.auto_black_message) && Regex.Match(message, Config.auto_black_message).Success)
flag = true;
if (flag)
{
info.status = MemberType.;
info.blackout_time = DateTime.Now;
session.SaveOrUpdate(info);
session.FindBlacklistMemberInfos(true);//刷新黑名单缓存
Ding($"增加黑名单提醒\r\n————\r\n事件类型{""}\r\n微信账号{client.WeixinHao}\r\n微信昵称{client.User.Nick}\r\n客户账号{info.username}\r\n客户昵称{info.usernick}\r\n添加时间{info.crt_time.ToString("yyyy-MM-dd HH:mm:ss")}");
return true;
}
}
return false;
}
/// <summary>
/// 禁止黑名单操作
/// </summary>
private bool _ForbidBlacklist(string username, string usernick, string remark, WeixinBase client)
{
if (client == null) return false;
try
{
var session = ApiClient.GetSession();
var info = session.Queryable<fl_member_info>().First(f => f.username == username && f.robot_type == (client.WeixinType == WeixinType.QYHook微信 ? ChatType. : ChatType.)/*f.robot_type == ChatType.微信*/);
if (info != null)
{
if (info.status == MemberType.) return false;
if (info.status == MemberType.)
{
Ding($"增加黑名单提醒\r\n————\r\n事件类型{""}\r\n微信账号{client.WeixinHao}\r\n微信昵称{client.User.Nick}\r\n客户账号{username}\r\n客户昵称{usernick}");
return true;
}
if (Config.FrequencyCloudBlack)
{
CloudBlack cloud = new CloudBlack();
var black = cloud.QueryBlack(info);
if (black != null)
{
Ding($"云黑名单提醒\r\n————\r\n事件类型{""}\r\n微信账号{client.WeixinHao}\r\n微信昵称{client.User.Nick}\r\n客户账号{username}\r\n客户昵称{usernick}\r\n拉黑原因{black.remark}");
if (black.iscloud)
{
info.status = MemberType.;
ApiClient.GetSession().SaveOrUpdate(info);
}
return true;
}
else
return false;
}
return _check_insert_black(info, client, remark);
}
else
{
CloudBlack cloud = new CloudBlack();
return cloud.QueryBlack(username, (client.WeixinType == WeixinType.QYHook微信 ? ChatType. : ChatType.)) != null;
}
}
catch (Exception)
{ }
return false;
}
private void SDK_WXNewFriendEvent(object sender, Chat.Framework.WXSdk.Events.WXNewFriend e)
{
e.Cancel = _ForbidBlacklist(e.Friend.UserName, e.Friend.NickName, $"新增好友事件", e.Client);
}
private void SDK_WXApplyFriendEvent(object sender, Chat.Framework.WXSdk.Events.WXApplyFriend e)
{
var isCancel = _ForbidBlacklist(e.NewFriendWxid, e.NewFriendNick, $"申请添加好友", e.Client);
if (isCancel == false)
{
//查看邀请用户是否黑名单
if (!string.IsNullOrWhiteSpace(e.OldFriendWxid))
{
isCancel = _ForbidBlacklist(e.OldFriendWxid, e.OldFriendNick, $"邀请好友,申请添加", e.Client);
}
}
e.Cancel = isCancel;
}
private void SDK_WXApplyGroupEvent(object sender, Chat.Framework.WXSdk.Events.WXApplyGroup e)
{
e.Cancel = _ForbidBlacklist(e.Wxid, e.Usernick, $"申请入群", e.Client);
}
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
{
ApiClient.BlackMemberFunc = null;
if (mainForm != null) //关闭窗体
{
mainForm.CloseForm();
mainForm = null;
}
}
catch (Exception ex)
{
this.OnLog(ex.Message);
}
}
/// <summary>
/// 收到通用消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SDK_ReciveIMEvent(object sender, ReciveIMEvent e)
{
try
{
var client = e.Sender as WeixinBase;
//屏蔽机器人之间的消息
if (Chat.Framework.ChatClient.WXClient.Values.ToList().FirstOrDefault(f => f.User.Username == e.Username) != null)
{
e.Cancel = true;
return;
}
var member = e.GetMemberinfo();
if (Config.ProhibitSL && string.IsNullOrEmpty(e.Groupid))
{
e.Cancel = true; return;
}
if (Config.ProhibitQL && !string.IsNullOrEmpty(e.Groupid))
{
e.Cancel = true; return;
}
if (member.status == MemberType.)
return;
else if (member.status == MemberType.)
{
//白名单关键词
if (!string.IsNullOrWhiteSpace(Config.WhiteListWords))
{
var strs = Config.WhiteListWords.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).ToList();
if (strs.FirstOrDefault(f => e.Message.Contains(f)) != null)
return;
}
if (string.IsNullOrEmpty(e.Groupid) && Class1.Config.auth_delete_black)
{
var wechat = e.Sender as WXClientImpl_IPAD;
if (wechat != null)
{
this.OnLog($"自动删除黑名单好友:{e.NickName}({e.Username})");
wechat.DeleteFriend(e.Username);
}
}
if (client != null) Ding($"黑名单警告\r\n————\r\n事件类型{""}\r\n微信账号{client.WeixinHao}\r\n微信昵称{client.User.Nick}\r\n客户账号{e.Username}\r\n客户昵称{e.NickName}");
e.Cancel = true;
return;
}
else if (Config.FrequencyCloudBlack)
{
CloudBlack cloud = new CloudBlack();
var black = cloud.QueryBlack(member);
if (black != null)
{
if (client != null)
Ding($"云黑名单警告\r\n————\r\n事件类型{""}\r\n微信账号{client.WeixinHao}\r\n微信昵称{client.User.Nick}\r\n客户账号{e.Username}\r\n客户昵称{e.NickName}\r\n拉黑原因{black.remark}");
e.Cancel = true;
if (black.iscloud)
{
member.status = MemberType.;
ApiClient.GetSession().SaveOrUpdate(member);
}
return;
}
}
//if (!string.IsNullOrEmpty(e.Groupid) && !Config.WhiteListSwitch && !Config.GroupWhiteList.Contains(e.Groupid))
//{ e.Cancel = true; return; }
if (!string.IsNullOrEmpty(e.Groupid))
{
if (!Config.WhiteListSwitch)
{
if ((Config.GroupResponseType && !Config.GroupWhiteList.Contains(e.Groupid)) || (!Config.GroupResponseType && Config.GroupWhiteList.Contains(e.Groupid)))
{ e.Cancel = true; return; }
}
if (Config.ProhibitWxid && Config.ProhibitWxidList.Length != 0)
{
if (Config.ProhibitWxidList.ToList().Contains(e.Username))
{
e.Cancel = true;
return;
}
}
if (Config.ProhibitNick && Config.ProhibitNickList.Length != 0)
{
var result = Config.ProhibitNickList.FirstOrDefault(f => e.NickName.Contains(f));
if (!string.IsNullOrEmpty(result))
{
e.Cancel = true;
return;
}
}
}
else if (string.IsNullOrEmpty(e.Groupid))
{
if (Config.ProhibitWxid && Config.ProhibitWxidList.Length != 0)
{
if (Config.ProhibitWxidList.ToList().Contains(e.Username))
{
e.Cancel = true;
return;
}
}
if (Config.ProhibitNick && Config.ProhibitNickList.Length != 0)
{
var result = Config.ProhibitNickList.FirstOrDefault(f => e.NickName.Contains(f));
if (!string.IsNullOrEmpty(result))
{
e.Cancel = true;
return;
}
}
var session = ApiClient.GetSession();
int mess_count = 1;
var _flag = true;
if (mess_count < Config.FrequencyIntervalSendNum) //用户发送消息
{
mess_count = int.Parse(session.FindRow("select count(*) as num from fl_plugin_messagesupervises_messinfo where member_id = @id and send_time >= @time ", new { id = member.id, time = DateTime.Now.AddSeconds(-Config.FrequencyIntervalSecs) })["num"].ToString()) + 1;
if (mess_count < Config.FrequencyIntervalSendNum) //为超出了频率限制,插入数据库
{
session.ExcuteSQL("insert into fl_plugin_messagesupervises_messinfo (member_id,send_time) values (@member_id,@send_time)", new { member_id = member.id, send_time = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) });
_flag = false;
}
}
if (_flag)
{
e.SendMessage(Class1.Config.FrequencyDetentionTip);
member.status = MemberType.; //拉入黑名单
session.SaveOrUpdate(member);
session.FindBlacklistMemberInfos(true);//刷新黑名单缓存
e.Cancel = true;
return;
}
}
e.Cancel = _check_insert_black(e.GetMemberinfo(), client, "收到聊天消息", e.Message);
if (e.Cancel)
{
if (string.IsNullOrEmpty(e.Groupid) && Class1.Config.auth_delete_black)
{
var wechat = e.Sender as Chat.Framework.WXSdk.Implement.WXClientImpl_IPAD;
if (wechat != null)
{
wechat.DeleteFriend(e.Username);
this.OnLog($"自动删除黑名单好友:{e.NickName}({e.Username})");
}
}
}
}
catch (Exception ex)
{
this.OnLog(ex.Message + " - " + ex.StackTrace);
}
}
}
}