forked from amoeba/TownCrier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageUtil.cs
151 lines (125 loc) · 4.84 KB
/
MessageUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace TownCrier
{
class MessageUtil
{
public static string MaybeEscape(string message, bool escape)
{
try
{
if (escape)
{
return Uri.EscapeUriString(message);
}
else
{
return message;
}
}
catch (Exception ex)
{
Util.LogError(ex);
return message;
}
}
public static string SubstituteVariables(string message)
{
return SubstituteVariables(message, null, false);
}
public static string SubstituteVariables(string message, string eventMessage, bool escape)
{
string modified = message;
try
{
if (message == "" && eventMessage == "")
{
return "Empty webhook.";
}
// Short circuit to support EventTriggers with no format string
if (message == "")
{
return eventMessage;
}
if (modified.Contains("$EVENT") && eventMessage != "")
{
modified = modified.Replace("$EVENT", MaybeEscape(eventMessage, escape));
}
if (modified.Contains("$NAME"))
{
modified = modified.Replace("$NAME", MaybeEscape(Globals.Core.CharacterFilter.Name, escape));
}
if (modified.Contains("$LEVEL"))
{
modified = modified.Replace("$LEVEL", MaybeEscape(Globals.Core.CharacterFilter.Level.ToString(), escape));
}
if (modified.Contains("$UXP"))
{
modified = modified.Replace("$UXP", MaybeEscape(string.Format("{0:#,##0}", Globals.Core.CharacterFilter.UnassignedXP), escape));
}
if (modified.Contains("$TXP"))
{
modified = modified.Replace("$TXP", MaybeEscape(string.Format("{0:#,##0}", Globals.Core.CharacterFilter.TotalXP), escape));
}
if (modified.Contains("$HEALTH"))
{
modified = modified.Replace("$HEALTH", MaybeEscape(Globals.Core.CharacterFilter.Health.ToString(), escape));
}
if (modified.Contains("$STAMINA"))
{
modified = modified.Replace("$STAMINA", MaybeEscape(Globals.Core.CharacterFilter.Stamina.ToString(), escape));
}
if (modified.Contains("$MANA"))
{
modified = modified.Replace("$MANA", MaybeEscape(Globals.Core.CharacterFilter.Mana.ToString(), escape));
}
if (modified.Contains("$VITAE"))
{
modified = modified.Replace("$VITAE", MaybeEscape(Globals.Core.CharacterFilter.Vitae.ToString() + "%", escape));
}
if (modified.Contains("$LOC"))
{
modified = modified.Replace("$LOC", MaybeEscape(new Location(Globals.Host.Actions.Landcell, Globals.Host.Actions.LocationX, Globals.Host.Actions.LocationY).ToString(), escape));
}
if (modified.Contains("$DATETIME"))
{
modified = modified.Replace("$DATETIME", MaybeEscape(DateTime.UtcNow.Date.ToString("yyyy/MM/dd HH:mm:ss"), escape));
}
if (modified.Contains("$DATE"))
{
modified = modified.Replace("$DATE", MaybeEscape(DateTime.UtcNow.Date.ToString("yyyy/MM/dd"), escape));
}
if (modified.Contains("$TIME"))
{
modified = modified.Replace("$TIME", MaybeEscape(DateTime.UtcNow.Date.ToString("HH:mm:ss"), escape));
}
return modified;
}
catch (Exception ex)
{
Util.LogError(ex);
return modified;
}
}
public static string StripTags(string message)
{
try
{
Regex e = new Regex("<.+>(.+)<.+>", RegexOptions.Compiled);
Match m = e.Match(message);
if (!m.Success || m.Captures.Count != 1 || m.Groups.Count != 2)
{
return message;
}
return message.Replace(m.Groups[0].Value, m.Groups[1].Value);
}
catch (Exception ex)
{
Util.LogError(ex);
return message;
}
}
}
}