Joined
·
10,692 Posts
Without revealing too much detail
, this is part of the report, ban, and thread cleanup control code for the antispam moderator bot:
Code:
[NOPARSE]// ...
Map<Integer, String> postBBCodes = getPostBBCodes(postIDs);
Iterator<UserPost> postItr = posts.iterator();
while (postItr.hasNext()) {
UserPost post = postItr.next();
String postContent = postBBCodes.get(post.postID);
// Post not found; containing thread may be closed and quote-reply inaccessible
if (postContent == null) {
log.info("Cannot retrieve BB Code for " + post.postID + " - Skipping");
updateCheckedPosts(post.postID);
continue;
}
Classifier classifier = new Classifier(postContent);
if (classifier.classify()) {
log.info("Potential spam found in post " + post.postID + " from user " + post.username + " (" + post.userID + "): ");
log.info("---");
log.info(postContent);
log.info("---");
log.info("Detection reason");
for (Rule rule : classifier.getFlagged()) {
log.info("\t" + rule.getDescription());
}
if (checkUserSufficientCredentials(post)) {
updateCheckedPosts(post.postID);
continue;
}
flaggedPosts.add(post.postID);
volatilePosts.remove(post.postID);
// Check reply credentials, if any
boolean hasCredibleReplies = false;
if (canModerate && post.postNumber == 1 && totalPostCount > 1) {
Set<UserPost> replies = new HashSet<UserPost>(posts);
replies.remove(post); // OP
for (UserPost reply : replies) {
if (checkUserSufficientCredentials(reply)) {
hasCredibleReplies = true;
log.info("Suspicious thread has credible replies");
break;
}
}
}
if (canModerate && post.postNumber == 1 && !hasCredibleReplies) {
try {
StringBuilder reason = new StringBuilder("This thread has been classified and deleted as spam.\n\nDetection reason:\n");
reason.append("[LIST=0]\n");
for (Rule rule : classifier.getFlagged()) {
reason.append("[*]" + rule.getDescription() + "\n");
}
reason.append("[/LIST]");
// Quarantine thread
moveThread(thread, quarantineForum);
// Reply to thread before deleting
ForumUtil forumUtil = new ForumUtil(fc);
forumUtil.postReply(topURL + "showthread.php?t=" + thread, reason.toString(), false);
// Ban only needed once
if (!recentBannedUsers.contains(post.userID)) {
if (checkUserInstantBannable(classifier.getFlagged())) {
if (ban(post.username, "Spammer Instaban Tripwire")) {
recentBannedUsers.add(post.userID);
forumUtil.postReply(topURL + "showthread.php?t=" + thread, "User has been banned for this post.", false);
// Log ban in log
forumUtil.postReply(topURL + "showthread.php?t=" + banLogThread,
"[QUOTE="+post.username+";"+post.postID+"]" + unescapeHTML(postContent) + "[/QUOTE]\n\n" +
"User " + post.username + " has been permanently banned due to Spammer Instaban Tripwire.", false);
log.info("Banned user via spammer instaban tripwire");
}
} else if (checkUserChainSpammer(post.userID)) {
if (ban(post.username, "Spam posts exceeded threshold")) {
recentBannedUsers.add(post.userID);
forumUtil.postReply(topURL + "showthread.php?t=" + thread, "User has been banned for chain spamming.", false);
String lookupURL = topURLPublic + "search.php?do=finduser&u=" + post.userID;
// Log ban in log
forumUtil.postReply(topURL + "showthread.php?t=" + banLogThread,
"[QUOTE="+post.username+";"+post.postID+"]" + unescapeHTML(postContent) + "[/QUOTE]\n\n" +
"User " + post.username + " has been permanently banned for chain spamming. [URL=" + lookupURL + "][Review][/URL]", false);
log.info("Banned user for chain spamming");
}
} else {
log.info("User has not yet reached ban conditions");
}
}
deleteThread(thread, classifier.getFlagged());
// Log deletion in log
forumUtil.postReply(topURL + "showthread.php?t=" + logThread,
"[QUOTE="+post.username+";"+post.postID+"]" + unescapeHTML(postContent) + "[/QUOTE]\n\n" +
reason.toString(), false);
} catch (Exception e) {
// Post may have been deleted before replying
log.log(Level.WARNING, "Unable to reply and delete due to an error", e);
}
// Thread head terminated; all remaining posts are pointless to check
while (postItr.hasNext()) {
postItr.next();
postItr.remove();
}
} else {
try {
if ((System.currentTimeMillis() - lastReportTime) < 62000) {
log.info("Unable to report another post within execution deadline -- Skip until next round");
continue;
}
StringBuilder reason = new StringBuilder("This post is suspicious and has been reported as spam.\n\nDetection reason:\n");
reason.append("[LIST=0]\n");
for (Rule rule : classifier.getFlagged()) {
reason.append("[*]" + rule.getDescription() + "\n");
}
reason.append("[/LIST]");
ForumUtil forumUtil = new ForumUtil(fc);
// Log report in log
forumUtil.postReply(topURL + "showthread.php?t=" + logThread,
"[QUOTE="+post.username+";"+post.postID+"]" + unescapeHTML(postContent) + "[/QUOTE]\n\n" +
reason.toString(), false);
report(post.postID, classifier.getFlagged());
} catch (Exception e) {
// Post may have been deleted before replying
log.log(Level.WARNING, "Unable to reply and report due to an error", e);
}
lastReportTime = System.currentTimeMillis();
}
updateReportedPosts(post.postID, postContent, classifier.getFlagged());
recheck = true; // Spam found
} else {
log.info("Post " + post.postID + " is not spam");
}
updateCheckedPosts(post.postID);
volatilePosts.remove(post.postID);
}
// ...[/NOPARSE]