Просмотр исходного кода

Server invite handling

Simplified how Servers track away states

git-svn-id: http://svn.dmdirc.com/trunk@2400 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.5
Chris Smith 16 лет назад
Родитель
Сommit
8cc322d30e

+ 105
- 0
src/com/dmdirc/Invite.java Просмотреть файл

@@ -0,0 +1,105 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc;
24
+
25
+import java.util.Date;
26
+
27
+/**
28
+ * Model for a channel invitation.
29
+ *
30
+ * @author Chris
31
+ */
32
+public class Invite {
33
+    
34
+    /** The server this invite was on. */
35
+    private final Server server;
36
+    
37
+    /** The channel this invite is for. */
38
+    private final String channel;
39
+    
40
+    /** The time this invite was created. */
41
+    private final long timestamp;
42
+    
43
+    /** The source of this invite. */
44
+    private final String source;
45
+    
46
+    /**
47
+     * Creates a new instance of Invite.
48
+     * 
49
+     * @param server The server that this invite was received for
50
+     * @param channel The channel that this invite is for
51
+     * @param source The source of this invite
52
+     */
53
+    public Invite(final Server server, final String channel, final String source) {
54
+        this.server = server;
55
+        this.channel = channel;
56
+        this.source = source;
57
+        this.timestamp = new Date().getTime();
58
+    }
59
+
60
+    /**
61
+     * Retrieves the server that this invite is associated with.
62
+     * 
63
+     * @return This invite's server
64
+     */
65
+    public Server getServer() {
66
+        return server;
67
+    }
68
+
69
+    /**
70
+     * Retrieves the name of the channel that this invite is for.
71
+     * 
72
+     * @return This invite's channel
73
+     */
74
+    public String getChannel() {
75
+        return channel;
76
+    }
77
+
78
+    /**
79
+     * Retrieves the timestamp that this invite was received at.
80
+     * 
81
+     * @return This invite's timestamp
82
+     */
83
+    public long getTimestamp() {
84
+        return timestamp;
85
+    }
86
+
87
+    /**
88
+     * Retrieves the source of this invite.
89
+     * 
90
+     * @return This invite's source
91
+     */
92
+    public String getSource() {
93
+        return source;
94
+    }
95
+    
96
+    /**
97
+     * Join the channel that belongs to this invite.
98
+     */
99
+    public void accept() {
100
+        server.getParser().joinChannel(channel);
101
+        
102
+        server.removeInvite(this);
103
+    }
104
+    
105
+}

+ 86
- 31
src/com/dmdirc/Server.java Просмотреть файл

@@ -29,6 +29,7 @@ import com.dmdirc.commandparser.CommandManager;
29 29
 import com.dmdirc.config.ConfigManager;
30 30
 import com.dmdirc.config.Identity;
31 31
 import com.dmdirc.config.IdentityManager;
32
+import com.dmdirc.interfaces.InviteListener;
32 33
 import com.dmdirc.logger.ErrorLevel;
33 34
 import com.dmdirc.logger.Logger;
34 35
 import com.dmdirc.parser.ChannelInfo;
@@ -51,6 +52,8 @@ import java.util.Map;
51 52
 import java.util.Timer;
52 53
 import java.util.TimerTask;
53 54
 
55
+import javax.swing.event.EventListenerList;
56
+
54 57
 /**
55 58
  * The Server class represents the client's view of a server. It maintains
56 59
  * a list of all channels, queries, etc, and handles parser callbacks pertaining
@@ -110,14 +113,18 @@ public final class Server extends WritableFrameContainer implements Serializable
110 113
     /** The config manager for this server. */
111 114
     private ConfigManager configManager;
112 115
     
113
-    /** Whether we're marked as away or not. */
114
-    private boolean away;
115 116
     /** Our reason for being away, if any. */
116
-    private String awayMessage;
117
+    private String awayMessage = "";
117 118
     
118 119
     /** Our event handler. */
119 120
     private final ServerEventHandler eventHandler = new ServerEventHandler(this);
120 121
     
122
+    /** A list of outstanding invites. */
123
+    private final List<Invite> invites = new ArrayList<Invite>();
124
+    
125
+    /** A list of listeners for this server's events. */
126
+    private final EventListenerList listeners = new EventListenerList();
127
+    
121 128
     /**
122 129
      * Creates a new instance of Server.
123 130
      *
@@ -245,8 +252,8 @@ public final class Server extends WritableFrameContainer implements Serializable
245 252
         
246 253
         doCallbacks();
247 254
         
248
-        away = false;
249 255
         awayMessage = "";
256
+        invites.clear();
250 257
         window.setAwayIndicator(false);
251 258
         
252 259
         try {
@@ -497,7 +504,7 @@ public final class Server extends WritableFrameContainer implements Serializable
497 504
      * @return True if the client is marked as away, false otherwise
498 505
      */
499 506
     public boolean isAway() {
500
-        return away;
507
+        return !awayMessage.isEmpty();
501 508
     }
502 509
     
503 510
     /**
@@ -682,7 +689,7 @@ public final class Server extends WritableFrameContainer implements Serializable
682 689
             final Channel newChan = new Channel(this, chan);
683 690
             
684 691
             tabCompleter.addEntry(chan.getName());
685
-            channels.put(parser.toLowerCase(chan.getName()), newChan);            
692
+            channels.put(parser.toLowerCase(chan.getName()), newChan);
686 693
             newChan.show();
687 694
         }
688 695
     }
@@ -832,7 +839,7 @@ public final class Server extends WritableFrameContainer implements Serializable
832 839
     
833 840
     /**
834 841
      * Called when a private CTCP is received.
835
-     * 
842
+     *
836 843
      * @param type The type of the CTCP
837 844
      * @param message The body (if any) of the CTCP
838 845
      * @param host The host of the user that sent the CTCP
@@ -868,7 +875,7 @@ public final class Server extends WritableFrameContainer implements Serializable
868 875
     
869 876
     /**
870 877
      * Called when a private CTCP reply is received.
871
-     * 
878
+     *
872 879
      * @param type The type of CTCPR
873 880
      * @param message The body of the CTCPR
874 881
      * @param host The host of the user who is sending the CTCPR
@@ -884,7 +891,7 @@ public final class Server extends WritableFrameContainer implements Serializable
884 891
     
885 892
     /**
886 893
      * Called when a private notice is received.
887
-     * 
894
+     *
888 895
      * @param message The message content of the notice
889 896
      * @param host The host of the user who sent the notice
890 897
      */
@@ -900,7 +907,7 @@ public final class Server extends WritableFrameContainer implements Serializable
900 907
     /**
901 908
      * Called when the server says that the nickname we're trying to use is
902 909
      * already in use.
903
-     * 
910
+     *
904 911
      * @param nickname The nickname that we were trying to use
905 912
      */
906 913
     public void onNickInUse(final String nickname) {
@@ -937,7 +944,7 @@ public final class Server extends WritableFrameContainer implements Serializable
937 944
     /**
938 945
      * Called when the parser has determined the network name and IRCd type
939 946
      * of our server.
940
-     * 
947
+     *
941 948
      * @param networkName The name of the network we're connected to
942 949
      * @param ircdType The type of IRCd that we're connected to
943 950
      */
@@ -947,9 +954,9 @@ public final class Server extends WritableFrameContainer implements Serializable
947 954
         updateIgnoreList();
948 955
     }
949 956
     
950
-    /** 
957
+    /**
951 958
      * Called when we start receiving the server's MOTD.
952
-     * 
959
+     *
953 960
      * @param data The data sent by the server
954 961
      */
955 962
     public void onMOTDStart(final String data) {
@@ -960,9 +967,9 @@ public final class Server extends WritableFrameContainer implements Serializable
960 967
         addLine(buffer, data);
961 968
     }
962 969
     
963
-    /** 
970
+    /**
964 971
      * Called when we receive a single line of the server's MOTD.
965
-     * 
972
+     *
966 973
      * @param data The data sent by the server
967 974
      */
968 975
     public void onMOTDLine(final String data) {
@@ -973,7 +980,7 @@ public final class Server extends WritableFrameContainer implements Serializable
973 980
         addLine(buffer, data);
974 981
     }
975 982
     
976
-    /** 
983
+    /**
977 984
      * Called when the server has finished sending the MOTD.
978 985
      */
979 986
     public void onMOTDEnd() {
@@ -986,7 +993,7 @@ public final class Server extends WritableFrameContainer implements Serializable
986 993
     
987 994
     /**
988 995
      * Called when the server sends a numeric event.
989
-     * 
996
+     *
990 997
      * @param numeric The numeric code for the event
991 998
      * @param tokens The (tokenised) arguments of the event
992 999
      */
@@ -1013,8 +1020,8 @@ public final class Server extends WritableFrameContainer implements Serializable
1013 1020
     
1014 1021
     /**
1015 1022
      * Called when we receive an auth notice.
1016
-     * 
1017
-     * @param data The content of the notice 
1023
+     *
1024
+     * @param data The content of the notice
1018 1025
      */
1019 1026
     public void onNoticeAuth(final String data) {
1020 1027
         handleNotification("authNotice", data);
@@ -1025,8 +1032,8 @@ public final class Server extends WritableFrameContainer implements Serializable
1025 1032
     
1026 1033
     /**
1027 1034
      * Called when we receive an unknown notice.
1028
-     * 
1029
-     * @param message The content of the notice 
1035
+     *
1036
+     * @param message The content of the notice
1030 1037
      * @param target The destination of this notice (such as $*)
1031 1038
      * @param host The host of the user that sent the notice
1032 1039
      */
@@ -1040,7 +1047,7 @@ public final class Server extends WritableFrameContainer implements Serializable
1040 1047
     
1041 1048
     /**
1042 1049
      * Called when we receive a user mode change.
1043
-     * 
1050
+     *
1044 1051
      * @param client The updated client object for the victim (us)
1045 1052
      * @param setBy The host of the user that performed the change
1046 1053
      * @param modes The modes that were changed
@@ -1063,27 +1070,25 @@ public final class Server extends WritableFrameContainer implements Serializable
1063 1070
     
1064 1071
     /**
1065 1072
      * Called when our away state changes.
1066
-     * 
1073
+     *
1067 1074
      * @param currentState The new aray state
1068 1075
      * @param reason Our away reason, if applicable
1069 1076
      */
1070 1077
     public void onAwayState(final boolean currentState, final String reason) {
1071 1078
         if (currentState) {
1072
-            away = true;
1073 1079
             awayMessage = reason;
1074 1080
             
1075 1081
             ActionManager.processEvent(CoreActionType.SERVER_AWAY, null, this, awayMessage);
1076 1082
         } else {
1077
-            away = false;
1078 1083
             awayMessage = "";
1079 1084
             
1080 1085
             ActionManager.processEvent(CoreActionType.SERVER_BACK, null, this);
1081 1086
         }
1082 1087
         
1083
-        window.setAwayIndicator(away);
1088
+        window.setAwayIndicator(isAway());
1084 1089
     }
1085 1090
     
1086
-    /** 
1091
+    /**
1087 1092
      * Called when the socket has been closed.
1088 1093
      */
1089 1094
     public void onSocketClosed() {
@@ -1111,9 +1116,9 @@ public final class Server extends WritableFrameContainer implements Serializable
1111 1116
         }
1112 1117
     }
1113 1118
     
1114
-    /** 
1119
+    /**
1115 1120
      * Called when an error was encountered while connecting.
1116
-     * 
1121
+     *
1117 1122
      * @param errorInfo The parser's error information
1118 1123
      */
1119 1124
     public void onConnectError(final ParserError errorInfo) {
@@ -1191,7 +1196,7 @@ public final class Server extends WritableFrameContainer implements Serializable
1191 1196
             reconnect();
1192 1197
         }
1193 1198
     }
1194
-        
1199
+    
1195 1200
     /**
1196 1201
      * Called after the parser receives the 005 headers from the server.
1197 1202
      */
@@ -1226,7 +1231,57 @@ public final class Server extends WritableFrameContainer implements Serializable
1226 1231
             }
1227 1232
         }
1228 1233
     }
1229
-       
1234
+    
1235
+    /**
1236
+     * Adds an invite listener to this server.
1237
+     *
1238
+     * @param listener The listener to be added
1239
+     */
1240
+    public void addInviteListener(final InviteListener listener) {
1241
+        listeners.add(InviteListener.class, listener);
1242
+    }
1243
+    
1244
+    /**
1245
+     * Removes an invite listener from this server.
1246
+     *
1247
+     * @param listener The listener to be removed
1248
+     */
1249
+    public void removeInviteListener(final InviteListener listener) {
1250
+        listeners.remove(InviteListener.class, listener);
1251
+    }
1252
+    
1253
+    /**
1254
+     * Adds an invite to this server, and fires the appropriate listeners.
1255
+     * 
1256
+     * @param invite The invite to be added
1257
+     */
1258
+    public void addInvite(final Invite invite) {
1259
+        invites.add(invite);
1260
+        
1261
+        final Object[] listenerList = listeners.getListenerList();
1262
+        for (int i = 0; i < listenerList.length; i += 2) {
1263
+            if (listenerList[i] == InviteListener.class) {
1264
+                ((InviteListener) listenerList[i + 1]).inviteReceived(this, invite);
1265
+            }
1266
+        }
1267
+    }
1268
+
1269
+    /**
1270
+     * Removes an invite from this server, and fires the appropriate listeners.
1271
+     * 
1272
+     * @param invite The invite to be removed
1273
+     */    
1274
+    public void removeInvite(final Invite invite) {
1275
+        invites.remove(invite);
1276
+        
1277
+        final Object[] listenerList = listeners.getListenerList();
1278
+        for (int i = 0; i < listenerList.length; i += 2) {
1279
+            if (listenerList[i] == InviteListener.class) {
1280
+                ((InviteListener) listenerList[i + 1]).inviteReceived(this, invite);
1281
+            }
1282
+        }
1283
+    }
1284
+    
1230 1285
     /**
1231 1286
      * Returns this server's name.
1232 1287
      *

+ 9
- 1
src/com/dmdirc/ServerEventHandler.java Просмотреть файл

@@ -43,7 +43,7 @@ public final class ServerEventHandler implements IChannelSelfJoin, IPrivateMessa
43 43
         ISocketClosed, IPrivateNotice, IMOTDStart, IMOTDLine, IMOTDEnd,
44 44
         INumeric, IGotNetwork, IPingFailed, IPingSuccess, IAwayState,
45 45
         IConnectError, IAwayStateOther, INickInUse, IPost005, INoticeAuth,
46
-        IUnknownNotice, IUserModeChanged {
46
+        IUnknownNotice, IUserModeChanged, IInvite {
47 47
     
48 48
     private static final String CALLBACK_PREFIX = "com.dmdirc.parser.callbacks.interfaces.I";
49 49
     
@@ -254,4 +254,12 @@ public final class ServerEventHandler implements IChannelSelfJoin, IPrivateMessa
254 254
         checkParser(tParser);
255 255
         owner.onUserModeChanged(cClient, sSetBy, sModes);
256 256
     }
257
+
258
+    /** {@inheritDoc} */
259
+    public void onInvite(final IRCParser tParser, final String userHost,
260
+            final String channel) {
261
+        checkParser(tParser);
262
+        
263
+        owner.addInvite(new Invite(owner, channel, userHost));
264
+    }
257 265
 }

+ 52
- 0
src/com/dmdirc/interfaces/InviteListener.java Просмотреть файл

@@ -0,0 +1,52 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.interfaces;
24
+
25
+import com.dmdirc.Invite;
26
+import com.dmdirc.Server;
27
+import java.util.EventListener;
28
+
29
+/**
30
+ * Defines the methods that should be implemented by classes which wish to
31
+ * receive information about invites.
32
+ * 
33
+ * @author Chris
34
+ */
35
+public interface InviteListener extends EventListener {
36
+    
37
+    /**
38
+     * Called when a new invite has been received.
39
+     * 
40
+     * @param server The server the invite was received by
41
+     * @param invite The invite that was received
42
+     */
43
+    void inviteReceived(final Server server, final Invite invite);
44
+    
45
+    /**
46
+     * Called when an invite has expired or has been used.
47
+     * 
48
+     * @param server The server the invite was initially received by
49
+     * @param invite The invite that has expired
50
+     */
51
+    void inviteExpired(final Server server, final Invite invite);
52
+}

Загрузка…
Отмена
Сохранить