Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
wikiuser.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "wikiuser.hpp"
12 using namespace Huggle;
13 
14 //QRegExp WikiUser::IPv4Regex("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$");
15 QRegExp WikiUser::IPv4Regex("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b");
16 QRegExp WikiUser::IPv6Regex("^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?:"\
17  ":(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)"\
18  "?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\\.(?4)){3}))$");
19 QList<WikiUser*> WikiUser::ProblematicUsers;
20 QMutex WikiUser::ProblematicUserListLock(QMutex::Recursive);
21 
23 {
24  WikiUser::ProblematicUserListLock.lock();
25  int User = 0;
26  while (User < WikiUser::ProblematicUsers.count())
27  {
28  if (user->Username == WikiUser::ProblematicUsers.at(User)->Username)
29  {
31  WikiUser::ProblematicUserListLock.unlock();
32  return u;
33  }
34  User++;
35  }
36  WikiUser::ProblematicUserListLock.unlock();
37  return NULL;
38 }
39 
41 {
42  WikiUser::ProblematicUserListLock.lock();
43  int i = 0;
44  while (i < WikiUser::ProblematicUsers.count())
45  {
47  if (user->getBadnessScore() == 0 && user->WarningLevel == 0)
48  {
49  // there is no point to hold information for them
50  WikiUser::ProblematicUsers.removeAt(i);
51  delete user;
52  continue;
53  }
54  i++;
55  }
56  WikiUser::ProblematicUserListLock.unlock();
57 }
58 
60 {
61  WikiUser::ProblematicUserListLock.lock();
62  if (!us->IP && us->getBadnessScore() <= Configuration::LocalConfig_WhitelistScore)
63  {
64  if (!Configuration::WhiteList.contains(us->Username))
65  {
67  }
68  }
69  int c=0;
70  while (c<ProblematicUsers.count())
71  {
72  if (ProblematicUsers.at(c)->Username == us->Username)
73  {
74  ProblematicUsers.at(c)->BadnessScore = us->BadnessScore;
75  ProblematicUsers.at(c)->WarningLevel = us->WarningLevel;
76  if (us->IsReported)
77  {
78  ProblematicUsers.at(c)->IsReported = true;
79  }
80  ProblematicUsers.at(c)->ContentsOfTalkPage = us->ContentsOfTalkPage;
81  WikiUser::ProblematicUserListLock.unlock();
82  return;
83  }
84  c++;
85  }
86  ProblematicUsers.append(new WikiUser(us));
87  WikiUser::ProblematicUserListLock.unlock();
88 }
89 
90 WikiUser::WikiUser()
91 {
92  this->UserLock = new QMutex(QMutex::Recursive);
93  this->Username = "";
94  this->IP = true;
95  this->BadnessScore = 0;
96  this->WarningLevel = 0;
97  this->ContentsOfTalkPage = "";
98  this->IsReported = false;
99  this->WhitelistInfo = 0;
100  this->Bot = false;
101 }
102 
103 WikiUser::WikiUser(WikiUser *u)
104 {
105  this->UserLock = new QMutex(QMutex::Recursive);
106  this->IP = u->IP;
107  this->Username = u->Username;
108  this->WarningLevel = u->WarningLevel;
109  this->BadnessScore = u->BadnessScore;
111  this->IsReported = u->IsReported;
112  this->WhitelistInfo = u->WhitelistInfo;
113  this->Bot = u->Bot;
114 }
115 
116 WikiUser::WikiUser(const WikiUser &u)
117 {
118  this->UserLock = new QMutex(QMutex::Recursive);
119  this->WarningLevel = u.WarningLevel;
120  this->IsReported = u.IsReported;
121  this->IP = u.IP;
122  this->Username = u.Username;
123  this->BadnessScore = u.BadnessScore;
125  this->WhitelistInfo = u.WhitelistInfo;
126  this->Bot = u.Bot;
127 }
128 
129 WikiUser::WikiUser(QString user)
130 {
131  this->UserLock = new QMutex(QMutex::Recursive);
132  this->IP = false;
133  if (user != "")
134  {
135  this->IP = WikiUser::IPv6Regex.exactMatch(user);
136  if (!this->IP)
137  {
138  if (WikiUser::IPv4Regex.exactMatch(user))
139  {
140  this->IP = true;
141  }
142  }
143  }
144  this->Username = user;
145  int c=0;
146  this->ContentsOfTalkPage = "";
147  while (c<ProblematicUsers.count())
148  {
149  if (ProblematicUsers.at(c)->Username == this->Username)
150  {
151  this->BadnessScore = ProblematicUsers.at(c)->BadnessScore;
152  this->WarningLevel = ProblematicUsers.at(c)->WarningLevel;
153  this->IsReported = ProblematicUsers.at(c)->IsReported;
154  return;
155  }
156  c++;
157  }
158  this->BadnessScore = 0;
159  this->WarningLevel = 0;
160  this->Bot = false;
161  this->IsReported = false;
162 }
163 
164 WikiUser::~WikiUser()
165 {
166  delete UserLock;
167  while (this->Contributions.count() > 0)
168  {
169  delete this->Contributions.at(0);
170  this->Contributions.removeAt(0);
171  }
172 }
173 
175 {
176  WikiUser::ProblematicUserListLock.lock();
177  WikiUser *user = WikiUser::RetrieveUser(this);
178  if (user != NULL)
179  {
180  this->BadnessScore = user->BadnessScore;
182  if (user->WarningLevel > this->WarningLevel)
183  {
184  this->WarningLevel = user->WarningLevel;
185  }
186  }
187  // we can finally unlock it
188  WikiUser::ProblematicUserListLock.unlock();
189 }
190 
192 {
193  // first we need to lock this object because it might be accessed from another thread in same moment
194  this->UserLock->lock();
195  // check if there isn't some global talk page
196  WikiUser *user = WikiUser::RetrieveUser(this);
197  // we need to copy the value to local variable so that if someone change it from different
198  // thread we are still working with same data
199  QString contents = "";
200  if (user != NULL)
201  {
202  // we return a value of user from global db instead of local
203  contents = user->ContentsOfTalkPage;
204  this->UserLock->unlock();
205  return contents;
206  }
207  contents = this->ContentsOfTalkPage;
208  this->UserLock->unlock();
209  return contents;
210 }
211 
213 {
214  this->UserLock->lock();
215  this->ContentsOfTalkPage = text;
216  this->Update();
217  this->UserLock->unlock();
218 }
219 
220 void WikiUser::Update(bool MatchingOnly)
221 {
222  WikiUser::ProblematicUserListLock.lock();
223  if (MatchingOnly)
224  {
225  if (WikiUser::RetrieveUser(this) == NULL)
226  {
227  WikiUser::ProblematicUserListLock.unlock();
228  return;
229  }
230  }
231  WikiUser::UpdateUser(this);
232  WikiUser::ProblematicUserListLock.unlock();
233 }
234 
236 {
237  this->IP = true;
238 }
239 
241 {
242  return IP;
243 }
244 
246 {
247  return Configuration::LocalConfig_NSUserTalk + this->Username;
248 }
249 
251 {
252  if (this->WhitelistInfo == 1)
253  {
254  return true;
255  }
256  if (this->WhitelistInfo == 2)
257  {
258  return false;
259  }
260  if (Configuration::WhiteList.contains(this->Username))
261  {
262  this->WhitelistInfo = 1;
263  return true;
264  } else
265  {
266  this->WhitelistInfo = 2;
267  return false;
268  }
269 }
270 
271 long WikiUser::getBadnessScore(bool _resync)
272 {
273  if (_resync)
274  {
275  this->Resync();
276  }
277  return BadnessScore;
278 }
279 
280 void WikiUser::setBadnessScore(long value)
281 {
282  BadnessScore = value;
283  this->Update(true);
284 }
285 
287 {
288  QString pflags = "";
289  QString nflags = "";
290  if (this->GetContentsOfTalkPage() == "")
291  {
292  nflags += "T";
293  } else
294  {
295  pflags += "T";
296  }
297  if (this->WarningLevel > 0)
298  {
299  pflags += "w";
300  }
301  if (this->IsWhitelisted())
302  {
303  pflags += "E";
304  }
305  if (this->IsIP())
306  {
307  nflags += "R";
308  }
309  if (this->IsReported)
310  {
311  pflags += "r";
312  }
313  if (this->Bot)
314  {
315  pflags += "b";
316  }
317  QString flags = "";
318  if (nflags != "")
319  {
320  flags += "-" + nflags;
321  }
322  if (pflags != "")
323  {
324  flags += "+" + pflags;
325  }
326  return flags;
327 }
328 bool WikiUser::GetBot() const
329 {
330  return Bot;
331 }
332 
333 void WikiUser::SetBot(bool value)
334 {
335  Bot = value;
336 }
337 
338 
QString GetContentsOfTalkPage()
GetContentsOfTalkPage returns a precached content of this users talk page If there is a global instan...
Definition: wikiuser.cpp:191
long BadnessScore
Badness score of current user.
Definition: wikiuser.hpp:120
void Update(bool MatchingOnly=false)
Call UpdateUser on current user.
Definition: wikiuser.cpp:220
bool IsIP()
Returns true in case the current user is IP user.
Definition: wikiuser.cpp:240
QString Username
Username.
Definition: wikiuser.hpp:52
long getBadnessScore(bool _resync=true)
Retrieve a badness score for current user, see WikiUser::BadnessScore for more.
Definition: wikiuser.cpp:271
static QRegExp IPv4Regex
Matches only IPv4.
Definition: wikiuser.hpp:122
static WikiUser * RetrieveUser(WikiUser *user)
Function that return static version of this user.
Definition: wikiuser.cpp:22
int WarningLevel
Current warning level of user.
Definition: wikiuser.hpp:54
void Resync()
Update the information of this user based on global user list.
Definition: wikiuser.cpp:174
QString Flags()
Flags.
Definition: wikiuser.cpp:286
static QStringList WhiteList
Data of wl.
bool IsWhitelisted()
Returns true if this user is wl.
Definition: wikiuser.cpp:250
static QList< WikiUser * > ProblematicUsers
List of users that are scored in this instance of huggle.
Definition: wikiuser.hpp:35
QString ContentsOfTalkPage
In case that we retrieved the talk page during parse of warning level, this string contains it...
Definition: wikiuser.hpp:127
static QRegExp IPv6Regex
Matches all IP.
Definition: wikiuser.hpp:124
QList< WikiEdit * > Contributions
Cache of contributions made by this user.
Definition: wikiuser.hpp:78
void ForceIP()
Change the IP property to true forcefully even if user isn&#39;t IP.
Definition: wikiuser.cpp:235
QString GetTalk()
Return a link to talk page of this user (like User talk:Jimbo)
Definition: wikiuser.cpp:245
static void TrimProblematicUsersList()
Delete all users that have badness score 0 these users aren&#39;t necessary to be stored in a list...
Definition: wikiuser.cpp:40
void SetContentsOfTalkPage(QString text)
SetContentsOfTalkPage Change a cache for talk page in local and global cache.
Definition: wikiuser.cpp:212
static void UpdateUser(WikiUser *us)
Update a list of problematic users.
Definition: wikiuser.cpp:59