Ejemplo n.º 1
0
 private String editUserPermission(Operation op, Permission permission, IUser user) {
   DiscordUser u = cacheService.findUserById(user.getID()).orElseGet(() -> new DiscordUser(user));
   changePermission(op, permission, u);
   u = cacheService.saveUser(u);
   log.info("Saving new user permission settings: {}", u);
   permissionService.evict();
   return String.format(
       "Modified user %s: %s permission %s",
       u.getName(), op.name().toLowerCase(), permission.getName());
 }
Ejemplo n.º 2
0
  protected void update(String[] params, HttpServletRequest request, HttpServletResponse response) {
    ISocialmore sm;
    IUser user;
    IPost post;
    HttpSession session = request.getSession();

    if (session.getAttribute("user") == null) {
      forwardTo(request, response, "Sessions/login");
      return;
    }

    sm = Socialmore.connect(this);
    user = User.getSession(request);

    int post_id = -1;
    try {
      post_id = Integer.parseInt(params[1]);
    } catch (Exception e) {
    }

    String content = request.getParameter("content");

    try {
      post = sm.getPost(post_id);

      if (post != null && !content.isEmpty()) {
        System.out.println("Edit: " + user.getID() + " with " + content);
        post.edit(user.getID(), content);
        System.out.println("Edited!!! :D");
      }
    } catch (RemoteException e) {
      System.out.println("Fail!");
    }

    forwardTo(request, response, "Users/index");
  }
Ejemplo n.º 3
0
 private String editPermission(
     Operation op, IMessage message, Permission permission, List<String> args) {
   String first = args.size() >= 1 ? args.get(0) : "";
   String second = args.size() >= 2 ? args.get(1) : "";
   String third = args.size() >= 3 ? args.get(2) : "";
   String fourth = args.size() >= 4 ? args.get(3) : "";
   if (first.equalsIgnoreCase("this")) {
     // this <channel|guild>
     if (second.equalsIgnoreCase("channel")) {
       // this channel
       return editChannelPermission(op, permission, message.getChannel());
     } else if (second.equalsIgnoreCase("server")) {
       // this guild
       if (message.getChannel().isPrivate()) {
         return "Not a valid call from private channels";
       }
       IGuild guild = message.getChannel().getGuild();
       return editGuildPermission(op, permission, guild);
     } else {
       return "Use `this channel` or `this server`";
     }
   } else if (first.equalsIgnoreCase("user")) {
     // user <name or id>
     List<IUser> matching =
         discordService
             .getClient()
             .getGuilds()
             .stream()
             .flatMap(g -> g.getUsers().stream())
             .filter(u -> u.getName().equalsIgnoreCase(second) || u.getID().equals(second))
             .distinct()
             .collect(Collectors.toList());
     if (matching.size() > 1) {
       StringBuilder builder =
           new StringBuilder("Multiple users matched, please narrow search or use ID\n");
       for (IUser user : matching) {
         builder.append(user.getName()).append(" has id `").append(user.getID()).append("`\n");
       }
       return builder.toString();
     } else if (matching.isEmpty()) {
       return "User " + second + " not found in cache";
     } else {
       return editUserPermission(op, permission, matching.get(0));
     }
   } else if (first.equalsIgnoreCase("channel")) {
     // channel <name or id>
     List<IChannel> matching =
         discordService
             .getClient()
             .getGuilds()
             .stream()
             .flatMap(g -> g.getChannels().stream())
             .filter(c -> c.getName().equalsIgnoreCase(second) || c.getID().equals(second))
             .distinct()
             .collect(Collectors.toList());
     if (matching.size() > 1) {
       StringBuilder builder =
           new StringBuilder("Multiple channels matched, please narrow search or use ID\n");
       for (IChannel channel : matching) {
         builder
             .append(channel.getName())
             .append(" has id `")
             .append(channel.getID())
             .append("`\n");
       }
       return builder.toString();
     } else if (matching.isEmpty()) {
       return "Channel " + second + " not found in cache";
     } else {
       return editChannelPermission(op, permission, matching.get(0));
     }
   } else if (first.equalsIgnoreCase("server")) {
     // guild <name or id>
     List<IGuild> matching =
         discordService
             .getClient()
             .getGuilds()
             .stream()
             .filter(g -> g.getName().equalsIgnoreCase(second) || g.getID().equals(second))
             .distinct()
             .collect(Collectors.toList());
     if (matching.size() > 1) {
       StringBuilder builder =
           new StringBuilder("Multiple servers matched, please narrow search or use ID\n");
       for (IGuild guild : matching) {
         builder.append(guild.getName()).append(" has id `").append(guild.getID()).append("`\n");
       }
       return builder.toString();
     } else if (matching.isEmpty()) {
       return "Server " + second + " not found in cache";
     } else {
       return editGuildPermission(op, permission, matching.get(0));
     }
   } else if (first.equalsIgnoreCase("role")) {
     // role <name or id> [in <guild name or id>]
     // when a guild name/id is not specified:
     //      if triggered from a private channel, all case insensitive matches are picked up
     //      if triggered from a public channel, only the channel's guild is searched
     boolean isPrivate = message.getChannel().isPrivate();
     boolean specific = third.equalsIgnoreCase("in") && !fourth.isEmpty();
     List<IRole> matching =
         discordService
             .getClient()
             .getGuilds()
             .stream()
             .filter(
                 g -> {
                   if (isPrivate) {
                     return !specific
                         || g.getName().equalsIgnoreCase(fourth)
                         || g.getID().equals(fourth);
                   } else {
                     if (!specific) {
                       return g.equals(message.getChannel().getGuild());
                     } else {
                       return g.getName().equalsIgnoreCase(fourth) || g.getID().equals(fourth);
                     }
                   }
                 })
             .flatMap(g -> g.getRoles().stream())
             .filter(r -> r.getName().equalsIgnoreCase(second) || r.getID().equals(second))
             .distinct()
             .collect(Collectors.toList());
     if (matching.size() > 1) {
       StringBuilder builder =
           new StringBuilder("Multiple role matched, please narrow search or use ID\n");
       for (IRole role : matching) {
         builder.append(role.getName()).append(" has id `").append(role.getID()).append("`\n");
       }
       return builder.toString();
     } else if (matching.isEmpty()) {
       return "Role " + second + " not found in cache";
     } else {
       return editRolePermission(op, permission, matching.get(0));
     }
   } else {
     return "Invalid argument format! Check details with `.perm -?`";
   }
 }
Ejemplo n.º 4
0
  protected void delete(String[] params, HttpServletRequest request, HttpServletResponse response) {
    IUser user;
    ISocialmore sm;
    IPost post;
    IMessage msg;
    int msg_id, post_id = -1;

    HttpSession session = request.getSession();

    sm = Socialmore.connect(this);
    user = User.getSession(request);

    if (sm == null || user == null) {
      forwardTo(request, response, "Sessions/login");
      return;
    }

    if (params[0].equalsIgnoreCase("cancel-message")) {
      try {
        ArrayList<IMessage> inbox = (ArrayList<IMessage>) user.getInbox();
        request.setAttribute("inbox", inbox);
      } catch (RemoteException e1) {
        forwardTo(request, response, "Users/index");
        return;
      }

      try {
        msg_id = Integer.parseInt(params[1]);
        msg = user.getSentMessage(msg_id);
        if (user.getID() == msg.getSenderID() && msg.getReceiving() == null) {
          if (user.removeMessage(msg_id)) {
            request.setAttribute("flash_type", "alert-success");
            request.setAttribute("flash", "Your scheduled message was cancelled!");
            forwardTo(request, response, "Messages/index");
            return;
          }
        }
      } catch (Exception e) {

      }

      request.setAttribute("flash_type", "alert-error");
      request.setAttribute(
          "flash", "It was <b>not</b> possible to remove the schedule message specified");
      forwardTo(request, response, "Messages/index");
      return;
    }

    try {
      post_id = Integer.parseInt(params[1]);
    } catch (Exception e) {
    }

    try {
      post = sm.getPost(post_id);
      if (post != null) {
        post.delete(user.getID());
        System.out.println("Delete " + post_id);
      }
    } catch (RemoteException e) {

    }

    forwardTo(request, response, "Users/index");
  }