Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package nl.rvgate.arrv.Commands;
  2.  
  3.  
  4. import nl.rvgate.arrv.Channel;
  5. import nl.rvgate.arrv.Client;
  6. import nl.rvgate.arrv.Datastore;
  7. import nl.rvgate.arrv.Modes;
  8. import nl.rvgate.arrv.Reply;
  9.  
  10.  
  11. public class Join {
  12.     public static final int params = 2;
  13.    
  14.     public Join(Client cl, String channels){
  15.         new Join(cl, channels, "");
  16.     }
  17.     public Join(Client cl, String channels, String keys){
  18.         String[] keysarray = keys.split(",");
  19.         String[] channelsarray = channels.split(",");
  20.        
  21.         int index = 0;
  22.         for(String channelstring : channelsarray){
  23.             Channel channel = Datastore.getChannelByName(channelstring);
  24.            
  25.             String key = "";
  26.             try {
  27.                 key = keysarray[index];
  28.             } catch (Exception E){
  29.                 key = "";
  30.             }
  31.            
  32.             if(channel == null){
  33.                 Reply reply = new Reply(Reply.ERR_NOSUCHCHANNEL, cl);
  34.                 reply.set(channelstring+" :No such channel");
  35.                 cl.send(reply);
  36.                 continue;
  37.             }
  38.            
  39.             if(channel.hasMode(Modes.CHANNEL_INVITEONLY)){
  40.                 if(!channel.isInvited(cl)){
  41.                     Reply reply = new Reply(Reply.ERR_INVITEONLYCHAN, cl);
  42.                     reply.set(channelstring+" :Cannot join channel (+i)");
  43.                     cl.send(reply);
  44.                     continue;
  45.                 }
  46.             }
  47.            
  48.             if(channel.isBanned(cl)){
  49.                 Reply reply = new Reply(Reply.ERR_BANNEDFROMCHAN, cl);
  50.                 reply.set(channelstring+" :Cannot join channel (+b)");
  51.                 cl.send(reply);
  52.                 continue;
  53.             }
  54.            
  55.             if(channel.hasMode(Modes.CHANNEL_PASSWORD)){
  56.                 if(!key.equals(channel.getKey())){
  57.                     Reply reply = new Reply(Reply.ERR_BADCHANNELKEY, cl);
  58.                     reply.set(channelstring+" :Cannot join channel (+k)");
  59.                     cl.send(reply);
  60.                     continue;
  61.                 }
  62.             }
  63.            
  64.             channel.registerClient(cl);
  65.             // Send notice
  66.            
  67.             // Notify user he has joined
  68.             Reply joinreply = new Reply(cl);
  69.             joinreply.set("JOIN "+channelstring);
  70.             cl.send(joinreply);
  71.            
  72.             // Send topic
  73.             Reply reply = null;
  74.             if(channel.getTopic() == null){
  75.                 reply = new Reply(Reply.RPL_NOTOPIC, cl);
  76.                 reply.set(channelstring+" :No topic is set");
  77.                 cl.send(reply);
  78.             } else {
  79.                 reply = new Reply(Reply.RPL_TOPIC, cl);
  80.                 reply.set(channelstring+" :"+channel.getTopic());
  81.                 cl.send(reply);
  82.             }
  83.  
  84.             // Send userlist
  85.             String str = "";
  86.             for(Client client : channel.getClients()){
  87.                 str = str + " " + client.getNickname();
  88.             }
  89.             reply = new Reply(Reply.RPL_NAMREPLY, cl);
  90.             reply.set(channelstring + " :" + str);
  91.             cl.send(reply);
  92.            
  93.             reply = new Reply(Reply.RPL_ENDOFNAMES, cl);
  94.             reply.set(channelstring + " :End of /NAMES list");
  95.             cl.send(reply);
  96.            
  97.             // Notify all other users in channel that he joined
  98.             for(Client client : channel.getClients()){
  99.                 if(client != cl) client.send(joinreply);
  100.             }
  101.            
  102.             index++;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement