Advertisement
JimDeadlock

PrivateMessages.cs

Jan 18th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using Oxide.Core;
  6. using Oxide.Core.Libraries.Covalence;
  7. using Oxide.Core.Plugins;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11. [Info("PrivateMessages", "MisterPixie", "1.1.11")]
  12. [Description("Allows users to send private messages to each other")]
  13. class PrivateMessages : CovalencePlugin
  14. {
  15. private readonly Dictionary<string, string> pmHistory = new Dictionary<string, string>();
  16. private Dictionary<string, double> cooldown = new Dictionary<string, double>();
  17. private List<LastFivePms> lastFivePms = new List<LastFivePms>();
  18. private const string allowPerm = "privatemessages.allow";
  19.  
  20. class LastFivePms
  21. {
  22. public string target { get; set; }
  23. public string sender { get; set; }
  24. public List<string> messages { get; set; }
  25. }
  26.  
  27. [PluginReference] private Plugin Ignore, UFilter, BetterChatMute;
  28.  
  29. #region lang
  30.  
  31. private string Lang(string key, string id = null, params object[] args) =>
  32. string.Format(lang.GetMessage(key, this, id), args);
  33.  
  34. protected override void LoadDefaultMessages()
  35. {
  36. lang.RegisterMessages(new Dictionary<string, string>
  37. {
  38. {"PMTo", "[#00FFFF]PM to {0}[/#]: {1}"},
  39. {"PMFrom", "[#00FFFF]PM from {0}[/#]: {1}"},
  40. {"PlayerNotOnline", "{0} is not online."},
  41. {"NotOnlineAnymore", "The last person you was talking to is not online anymore."},
  42. {"NotMessaged", "You haven't messaged anyone or they haven't messaged you."},
  43. {"IgnoreYou", "[#FF0000]{0} is ignoring you and cant receive your PMs[/#]"},
  44. {"SelfPM", "You can not send messages to yourself."},
  45. {"SyntaxR", "Incorrect Syntax use: /r <msg>"},
  46. {"HistorySyntax", "Incorrect Syntax use: /pmhistory <name>"},
  47. {"SyntaxPM", "Incorrect Syntax use: /{0} <name> <msg>"},
  48. {"NotAllowedToChat", "You are not allowed to chat here"},
  49. {"History", "Your History:\n{0}"},
  50. {"CooldownMessage", "You will be able to send a private message in {0} seconds"},
  51. {"NoHistory", "There is not any saved pm history with this player."},
  52. {"CannotFindUser", "Cannot find this user"},
  53. {"CommandDisabled", "This command has been disabled"},
  54. {"IsMuted", "You are currently muted & cannot send private messages"},
  55. {"TargetMuted", "This person is muted & cannot receive your private message"},
  56. {"NoPermission", "You don't have the correct permissions to run this command"},
  57. {"HistoryPM", "[#00FFFF]{0}[/#]: {1}"},
  58. {"Logging", "[PM]{0}->{1}:{2}"}
  59. }, this);
  60. }
  61.  
  62. #endregion
  63.  
  64. private void Init()
  65. {
  66. LoadVariables();
  67.  
  68. if(string.IsNullOrEmpty(configData.PmCommand))
  69. AddCovalenceCommand("pm", "cmdPm");
  70. else
  71. AddCovalenceCommand(configData.PmCommand, "cmdPm");
  72.  
  73. AddCovalenceCommand("r", "cmdPmReply");
  74. AddCovalenceCommand("pmhistory", "cmdPmHistory");
  75. permission.RegisterPermission(allowPerm, this);
  76. }
  77.  
  78. private void OnUserDisconnected(IPlayer player)
  79. {
  80. if (pmHistory.ContainsKey(player.Id))
  81. {
  82. pmHistory.Remove(player.Id);
  83. }
  84. }
  85.  
  86. private void cmdPm(IPlayer player, string command, string[] args)
  87. {
  88. if (configData.UsePermission)
  89. {
  90. if (!player.HasPermission(allowPerm))
  91. {
  92. player.Reply(Lang("NoPermission", player.Id));
  93. return;
  94. }
  95. }
  96.  
  97. if (args.Length > 1)
  98. {
  99. var name = args[0];
  100. var p = FindPlayer(name);
  101. if (p != null)
  102. {
  103. if (p.Id == player.Id)
  104. {
  105. player.Reply(Lang("SelfPM", player.Id));
  106. return;
  107. }
  108.  
  109. if (!(bool)(Interface.Oxide.CallHook("CanChat", player) ?? true))
  110. {
  111. player.Reply(Lang("NotAllowedToChat", player.Id));
  112. return;
  113. }
  114.  
  115. if (configData.UseBetterChatMute && BetterChatMute != null && CheckMuteStatus(player, p))
  116. {
  117. return;
  118. }
  119.  
  120. if (IsCooldowned(player))
  121. {
  122. return;
  123. }
  124.  
  125. if (IsIgnored(player, p))
  126. {
  127. return;
  128. }
  129.  
  130. var msg = RemoveRichText(IsUFilter(args));
  131.  
  132. if (Interface.Oxide.CallHook("OnPMProcessed", player, p, msg) != null)
  133. {
  134. return;
  135. }
  136.  
  137. AddPmHistory(player.Id, p.Id);
  138.  
  139. player.Reply(Lang("PMTo", player.Id, p.Name, msg));
  140. p.Reply(Lang("PMFrom", p.Id, player.Name, msg));
  141.  
  142. AddHistoryAndLogging(player, p, msg);
  143. }
  144. else
  145. {
  146. player.Reply(Lang("PlayerNotOnline", player.Id, name));
  147. }
  148. }
  149. else
  150. {
  151. player.Reply(Lang("SyntaxPM", player.Id, configData.PmCommand));
  152. }
  153. }
  154.  
  155. private void cmdPmReply(IPlayer player, string command, string[] args)
  156. {
  157. if (configData.UsePermission)
  158. {
  159. if (!player.HasPermission(allowPerm))
  160. {
  161. player.Reply(Lang("NoPermission", player.Id));
  162. return;
  163. }
  164. }
  165.  
  166. if (args.Length > 0)
  167. {
  168. string steamid;
  169. if (pmHistory.TryGetValue(player.Id, out steamid))
  170. {
  171. var p = FindPlayer(steamid);
  172. if (p != null)
  173. {
  174. if (!(bool)(Interface.Oxide.CallHook("CanChat", player) ?? true))
  175. {
  176. player.Reply(Lang("NotAllowedToChat", player.Id));
  177. return;
  178. }
  179.  
  180. if (configData.UseBetterChatMute && BetterChatMute != null && CheckMuteStatus(player, p))
  181. {
  182. return;
  183. }
  184.  
  185. if (IsCooldowned(player))
  186. {
  187. return;
  188. }
  189.  
  190. if (IsIgnored(player, p))
  191. {
  192. return;
  193. }
  194.  
  195. var msg = RemoveRichText(IsUFilter(args, true));
  196.  
  197. if (Interface.Oxide.CallHook("OnPMProcessed", player, p, msg) != null)
  198. {
  199. return;
  200. }
  201.  
  202. AddPmHistory(player.Id, p.Id);
  203.  
  204. player.Reply(Lang("PMTo", player.Id, p.Name, msg));
  205. p.Reply(Lang("PMFrom", p.Id, player.Name, msg));
  206.  
  207. AddHistoryAndLogging(player, p, msg);
  208. }
  209. else
  210. {
  211. player.Reply(Lang("NotOnlineAnymore", player.Id));
  212. }
  213. }
  214. else
  215. {
  216. player.Reply(Lang("NotMessaged", player.Id));
  217. }
  218. }
  219. else
  220. {
  221. player.Reply(Lang("SyntaxR", player.Id));
  222. }
  223. }
  224.  
  225. private void AddHistoryAndLogging(IPlayer initiator, IPlayer target, string message)
  226. {
  227. if (configData.EnableHistory)
  228. AddToHistory(initiator.Id, target.Id, Lang("HistoryPM", null, initiator.Name, message));
  229.  
  230. if (configData.EnableLogging)
  231. Puts(Lang("Logging", null, initiator.Name, target.Name, message));
  232. }
  233.  
  234. private void AddPmHistory(string initiatorId, string targetId)
  235. {
  236. pmHistory[initiatorId] = targetId;
  237. pmHistory[targetId] = initiatorId;
  238. }
  239.  
  240. private bool CheckMuteStatus(IPlayer player, IPlayer target)
  241. {
  242. if ((bool)BetterChatMute.CallHook("API_IsMuted", player))
  243. {
  244. player.Reply(Lang("IsMuted", player.Id));
  245. return true;
  246. }
  247.  
  248. if ((bool)BetterChatMute.CallHook("API_IsMuted", target))
  249. {
  250. player.Reply(Lang("TargetMuted", player.Id));
  251. return true;
  252. }
  253.  
  254. return false;
  255. }
  256.  
  257. private void cmdPmHistory(IPlayer player, string command, string[] args)
  258. {
  259. if (configData.EnableHistory)
  260. {
  261. if (args.Length == 0)
  262. {
  263. player.Reply(Lang("HistorySyntax", player.Id));
  264. return;
  265. }
  266.  
  267. if (args.Length == 1)
  268. {
  269. var p = covalence.Players.FindPlayer(args[0]);
  270.  
  271. if (p != null)
  272. {
  273. var History = GetLastFivePms(player.Id, p.Id);
  274.  
  275. if (History == null)
  276. {
  277. player.Reply(Lang("NoHistory", player.Id));
  278. return;
  279. }
  280.  
  281. string msg = string.Join(Environment.NewLine, History.messages.ToArray());
  282.  
  283. player.Reply(Lang("History", player.Id, msg));
  284. }
  285. else
  286. {
  287. player.Reply(Lang("CannotFindUser", player.Id));
  288. }
  289. }
  290. }
  291. else
  292. {
  293. player.Reply(Lang("CommandDisabled", player.Id));
  294. }
  295. }
  296.  
  297. private string RemoveRichText(string message)
  298. {
  299. char[] fullArray = new char[message.Length];
  300. int arrayIndex = 0;
  301. bool tags = false;
  302. char c;
  303. for (int i = 0; i < message.Length; i++)
  304. {
  305. c = message[i];
  306. if (c == '<')
  307. {
  308. tags = true;
  309. continue;
  310. }
  311.  
  312. if (c == '>')
  313. {
  314. tags = false;
  315. continue;
  316. }
  317.  
  318. if (!tags)
  319. {
  320. fullArray[arrayIndex] = c;
  321. arrayIndex++;
  322. }
  323. }
  324.  
  325. return new string(fullArray, 0, arrayIndex);
  326. }
  327.  
  328. private IPlayer FindPlayer(string nameOrIdOrIp)
  329. {
  330. foreach (var activePlayer in covalence.Players.Connected)
  331. {
  332. if (activePlayer.Id == nameOrIdOrIp)
  333. return activePlayer;
  334. if (activePlayer.Name.Contains(nameOrIdOrIp))
  335. return activePlayer;
  336. if (activePlayer.Name.ToLower().Contains(nameOrIdOrIp.ToLower()))
  337. return activePlayer;
  338. if (activePlayer.Address == nameOrIdOrIp)
  339. return activePlayer;
  340. }
  341.  
  342. return null;
  343. }
  344.  
  345. private void AddToHistory(string sender, string target, string msg)
  346. {
  347. var value = GetLastFivePms(sender, target);
  348.  
  349. if (value == null)
  350. {
  351. lastFivePms.Add(new LastFivePms
  352. {
  353. sender = sender,
  354. target = target,
  355. messages = new List<string>
  356. {
  357. msg
  358. }
  359. });
  360. }
  361. else
  362. {
  363. value.messages.Add(msg);
  364.  
  365. if (value.messages.Count == 6)
  366. {
  367. value.messages.Remove(value.messages.First());
  368. }
  369. }
  370. }
  371.  
  372. private LastFivePms GetLastFivePms(string sender, string target) => lastFivePms.Find(x =>
  373. x.sender == sender && x.target == target || x.sender == target && x.target == sender);
  374.  
  375. private bool IsIgnored(IPlayer sender, IPlayer target)
  376. {
  377. if (configData.UseIgnore)
  378. {
  379. var hasIgnore = Ignore?.CallHook("HasIgnored", target.Id, sender.Id);
  380.  
  381. if (hasIgnore != null && (bool)hasIgnore)
  382. {
  383. sender.Reply(Lang("IgnoreYou", sender.Id, target.Name));
  384. return true;
  385. }
  386. }
  387.  
  388. return false;
  389. }
  390.  
  391. private string IsUFilter(string[] args, bool isR = false)
  392. {
  393. string message = string.Join(" ", args.Skip(1).ToArray());
  394.  
  395. if (isR)
  396. message = string.Join(" ", args.Skip(0).ToArray());
  397.  
  398. if (configData.UseUFilter)
  399. {
  400. var hasUFilter = (object)UFilter?.Call("ProcessText", message);
  401.  
  402. if (hasUFilter != null)
  403. {
  404. message = hasUFilter.ToString();
  405. }
  406. }
  407.  
  408. return message;
  409. }
  410.  
  411. private bool IsCooldowned(IPlayer player)
  412. {
  413. if (configData.UseCooldown)
  414. {
  415. double time;
  416. if (cooldown.TryGetValue(player.Id, out time))
  417. {
  418. if (time > GetTimeStamp())
  419. {
  420. player.Reply(Lang("CooldownMessage", player.Id, Math.Round(time - GetTimeStamp(), 2)));
  421. return true;
  422. }
  423.  
  424. cooldown.Remove(player.Id);
  425. }
  426.  
  427. cooldown.Add(player.Id, GetTimeStamp() + configData.CooldownTime);
  428. }
  429.  
  430. return false;
  431. }
  432.  
  433. private double GetTimeStamp()
  434. {
  435. return (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
  436. }
  437.  
  438. #region Config
  439.  
  440. private ConfigData configData;
  441.  
  442. private class ConfigData
  443. {
  444. public bool UseUFilter;
  445. public bool UseIgnore;
  446. public bool UseCooldown;
  447. public bool UseBetterChatMute;
  448. public bool EnableLogging;
  449. public bool EnableHistory;
  450. public bool UsePermission;
  451. public int CooldownTime;
  452. public string PmCommand;
  453. }
  454.  
  455. private void LoadVariables()
  456. {
  457. LoadConfigVariables();
  458. SaveConfig();
  459. }
  460.  
  461. protected override void LoadDefaultConfig()
  462. {
  463. var config = new ConfigData
  464. {
  465. UseIgnore = false,
  466. UseCooldown = false,
  467. UseUFilter = false,
  468. UsePermission = false,
  469. UseBetterChatMute = false,
  470. EnableLogging = true,
  471. EnableHistory = false,
  472. CooldownTime = 3,
  473. PmCommand = "pm"
  474. };
  475. SaveConfig(config);
  476. }
  477.  
  478. private void LoadConfigVariables() => configData = Config.ReadObject<ConfigData>();
  479. void SaveConfig(ConfigData config) => Config.WriteObject(config, true);
  480.  
  481. #endregion
  482. }
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement