Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor modifications for admins, implementation of banning system #72

Closed
wants to merge 4 commits into from
Closed

Minor modifications for admins, implementation of banning system #72

wants to merge 4 commits into from

Conversation

Vogel612
Copy link
Collaborator

Follow up to #65 with continuing changes to privilege system

mvarendorff and others added 4 commits March 27, 2017 12:39
* Automatically read roomowners and grant them privileges

* Allows banning in the same way as adding admins

* Banning possible, banned users' command invokations get ignored

* Changed to guard clause (once again)

* Rooms are now saved locally using their ID

Reading the information needed is about 1000 times faster than reading
them from the corresponding websites DOM.
Adding different domains soon!

* Added support for potential other domains.

Domain is now included in filename to save the roomowners

* Added check for adding privileges for line in properties.
}

Path file = Paths.get("bot.properties");
removePrivFromFile(file, (long) userID, "BANNED");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

being banned is not really a privilege, is it?
Also I really dislike stringly typing code. It would be cleaner to either properly separate banning from administrator privileges or to collapse them both into a "modification" which would be encapsulated into an enum:

enum UserModifier {
    ADMIN,
    BANNED
}

* file to add the ID to
* @param newAdmin
* ID of new admin
*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javadoc is pretty outdated at this point ...

Files.deleteIfExists(tempFile);

BufferedReader br = new BufferedReader(new FileReader(propertyFile.toFile()));
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile.toFile()));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you manually editing the properties-file instead of using the "native" Properties wrapper?

Files.move(tempFile, propertyFile);

} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Problem adding " + role + " " + newEntry + " to property file!", e);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indentation

while((nextLine = br.readLine()) != null){
String line = nextLine.toUpperCase().startsWith(role) ? nextLine.replace(Long.toString(newEntry), "").
//Regex to clean up the line
replace(", ,", ",").replaceAll(", $", "").replace("=, ", "=") : nextLine;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that seems like a really bad idea

Path file = Paths.get("bot.properties");
addAdminToFile(file, (long) newUserId);
return "Added userID " + newUserId + " successful!";
return Permissions.addAdmin(newUserId, bc);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit of a bad idea, because it intermingles application logic (adding the user to admins) with display logic (notifying the user)

Copy link
Contributor

@mvarendorff mvarendorff Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean save the return value of the method first and then returning it? Or throwing an exception and catch it with a new response?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. adding an admin shouldn't return a String for the user, just a Result or something similar. The result can be examined to create a String that's intended for the user:

switch (Permissions.addAdmin(newUserId, bc)) {
     case ALREADY_EXISTS:
          return "User already has elevated privileges";
     case SUCCESS:
          return "Added user " + newUserId + " successfully!";
     case FAILURE:
          return "Failed to add admin. Please contact the bot owner";
   }

removeAdminFromFile(file, (long) remID);

return "Removed " + remID + " sucessful";
return Permissions.removeAdmin(remID, bc);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@@ -182,6 +186,9 @@ private void createNumberCommand() {
private void createShutdownCommand(ChatInterface chatInterface) {
CommandHandle shutdown =
new CommandHandle.Builder("shutdown", message -> {
if (!Permissions.isAdmin((long) message.getUserId(), BOT_CONFIG)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static import could make this a bit nicer-looking, but that seems like personal preference ... @Unihedro what's our opinion on static imports?

* @param htmlCode Code to look for matches
* @return List of all Matches in the String
*/
private List<String> getAllMatches(String htmlCode) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html and regex ... really?
http://stackoverflow.com/a/1732454/1803692

Copy link
Contributor

@mvarendorff mvarendorff Mar 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that answer, but in other answers, it says that regex is fine to parse a limited and known set of html. In addition to that at least the fetching of the room owners has nothing to do directly with html, but looking for a pattern that shows the id's of the roomowner. This pattern completely ignores the tags. And I know that the way of getting the room name is not the clean (lets be honest, it is horrible...). But I could not find a way of finding stuff in the DOM of a website.
At first I thought of passing message.getRoomName() to the createRoom method, but this would then ignore the rooms that the bot joins during startup.
Is there something to find a certain string pattern in HTML except for RegExes? Or should I just leave the name out of the files?

private List<String> getAllMatches(String htmlCode) {
List<String> matches = new ArrayList<>();

Matcher m = Pattern.compile(searchRegex).matcher(htmlCode);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not recompile this for every search

br = new BufferedReader(new InputStreamReader(is));

while ( (line = br.readLine()) != null) {
htmlCode += line;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringBuffer

import com.gmail.inverseconduit.SESite;
import com.gmail.inverseconduit.utils.SourceCodeParser;

public class Room {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half of this file is useless? Unnecessary encapsulation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beeing unsure what you mean I will ask for safety: You mean that I can leave out the getters and setters because the attributes are not supposed to be changed once created? Or did I misunderstood something there?

* @param newAdmin
* ID of new admin
*/
private static void addPrivToFile(Path propertyFile, Long newEntry, String role) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rewriting the file every time is too expensive for our use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So better something like: Get current state, modify in program and write it back to the file all at once?

@Vogel612
Copy link
Collaborator Author

This has been stale for a year now...

@Vogel612 Vogel612 closed this Feb 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants