forked from huggle/huggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.vb
243 lines (202 loc) · 7.01 KB
/
User.vb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'This is a source code or part of Huggle project
'Copyright (C) 2011 Huggle team
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
Imports System.Text.RegularExpressions
<DebuggerDisplay("{Name}")> <DebuggerStepThrough()> _
Class User
'Represents a user account or anonymous user
'Regex to match IP addresses
Private Shared AnonymousIPv4Regex As New Regex _
("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", RegexOptions.Compiled)
Private Shared AnonymousIPv6Regex As New Regex _
("(?:[A-F0-9]{1,4}:){6}(?:[A-F0-9]{1,4}:[A-F0-9]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))", RegexOptions.Compiled)
Private Shared All As New Dictionary(Of String, User)
Private _AnonymousIPv4 As Boolean
Private _AnonymousIPv6 As Boolean
Private _EditCount As Integer
Private _Ignored As Boolean
Private _Level As UserLevel
Private _Name As String
Private _SessionEditCount As Integer
Private _SharedIP As Boolean
Public FirstEdit As Edit
Public LastEdit As Edit
Public Bot As Boolean
Public WarnTime As Date
Public Warnings As List(Of Warning)
Public WarningsCurrent As Boolean
Public Blocks As List(Of Block)
Public BlocksCurrent As Boolean
Public ContribsOffset As String
Public RecentContribsRetrieved As Boolean
Public Sub New(ByVal Name As String)
_Name = Name
_EditCount = -1
_AnonymousIPv4 = AnonymousIPv4Regex.IsMatch(Name)
_AnonymousIPv6 = AnonymousIPv6Regex.IsMatch(Name)
_Ignored = Whitelist.Contains(Name)
If Not All.ContainsKey(Name) Then All.Add(Name, Me)
End Sub
Public Shared ReadOnly Property [Me]() As User
Get
Return GetUser(Config.Username)
End Get
End Property
Public ReadOnly Property Anonymous() As Boolean
Get
Return _AnonymousIPv4 Or _AnonymousIPv6
End Get
End Property
Public Property EditCount() As Integer
Get
Return _EditCount
End Get
Set(ByVal value As Integer)
_EditCount = value
RefreshInfo()
End Set
End Property
Public ReadOnly Property Edits() As List(Of Edit)
Get
Dim UserEdits As New List(Of Edit)
Dim Edit As Edit = LastEdit
While Edit IsNot Nothing AndAlso Edit IsNot NullEdit
UserEdits.Add(Edit)
Edit = Edit.PrevByUser
End While
Return UserEdits
End Get
End Property
Public ReadOnly Property Range() As String
Get
If Not Anonymous Then Return Nothing
'TODO: currently no range support for IPv6, this will return the full address
Dim NamePart As String = Name.Substring(0, Name.LastIndexOf("."))
NamePart = NamePart.Substring(0, NamePart.LastIndexOf("."))
Return NamePart
End Get
End Property
Public Property Ignored() As Boolean
Get
Return _Ignored
End Get
Set(ByVal value As Boolean)
_Ignored = value
RefreshEdits()
RefreshInfo()
End Set
End Property
Public ReadOnly Property IsMe() As Boolean
Get
Return (Name = Config.Username)
End Get
End Property
Public Property Level() As UserLevel
Get
Return _Level
End Get
Set(ByVal value As UserLevel)
_Level = value
RefreshEdits()
RefreshInfo()
End Set
End Property
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public Property SessionEditCount() As Integer
Get
Return _SessionEditCount
End Get
Set(ByVal value As Integer)
_SessionEditCount = value
RefreshInfo()
End Set
End Property
Public Property SharedIP() As Boolean
Get
Return _SharedIP
End Get
Set(ByVal value As Boolean)
_SharedIP = value
RefreshInfo()
End Set
End Property
Public ReadOnly Property TalkPage() As Page
Get
Return GetPage(Space.UserTalk.Name & ":" & Name)
End Get
End Property
Public ReadOnly Property Userpage() As Page
Get
Return GetPage(Space.User.Name & ":" & Name)
End Get
End Property
Public Overrides Function ToString() As String
Return _Name
End Function
Public Shared Sub ClearAll()
All.Clear()
End Sub
Public Sub RefreshEdits()
'Remove and re-add edits with changed properties that might affect sort order
For Each Item As Edit In Edits
For Each Queue As Queue In Queue.All.Values
Queue.RefreshEdit(Item)
Next Queue
Next Item
End Sub
Public Sub RefreshInfo()
'Refresh any open user info forms
For Each Item As Form In Application.OpenForms
Dim Form As UserInfoForm = TryCast(Item, UserInfoForm)
If Form IsNot Nothing AndAlso Form.User Is Me Then Form.RefreshData()
Next Item
End Sub
Public Shared Function GetUser(ByVal Name As String) As User
Name = SanitizeName(Name)
If Name Is Nothing Then Return Nothing
If All.ContainsKey(Name) Then Return All(Name) Else Return New User(Name)
End Function
Public Shared Function SanitizeName(ByVal Name As String) As String
'Remove illegal characters
If Name.Contains("#") Then Name = Name.Substring(0, Name.IndexOf("#"))
Name = Name.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("|", "") _
.Replace("<", "").Replace(">", "").Replace("#", "").Replace(Tab, "").Replace(LF, "") _
.Replace("_", " ").Trim(" "c)
If Name.EndsWith(":") Then Return Nothing
If (Name.Contains(":")) Then
If (Name.Substring(Name.IndexOf(":") + 1).Contains(":") = False) Then
Name = Name.Substring(Name.IndexOf(":") + 1)
End If
End If
If Name Is Nothing OrElse Name.Length = 0 Then Return Nothing
'Capitalize
If Name.Length > 1 Then Name = Name.Substring(0, 1).ToUpper & Name.Substring(1) Else Name = Name.ToUpper
Return Name
End Function
End Class
Enum UserLevel As Integer
None
Notification
Reverted
ReportedUAA
Message
Warning
Warn1
Warn2
Warn3
Warn4im
WarnFinal
ReportedAIV
Blocked
End Enum