1
1
package bitbot ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .List ;
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
5
6
+ import org .jivesoftware .smack .Chat ;
7
+ import org .jivesoftware .smack .ChatManagerListener ;
6
8
import org .jivesoftware .smack .Connection ;
9
+ import org .jivesoftware .smack .MessageListener ;
7
10
import org .jivesoftware .smack .PacketListener ;
8
11
import org .jivesoftware .smack .SmackConfiguration ;
9
12
import org .jivesoftware .smack .XMPPConnection ;
10
13
import org .jivesoftware .smack .XMPPException ;
11
14
import org .jivesoftware .smack .packet .Message ;
12
15
import org .jivesoftware .smack .packet .Packet ;
16
+ import org .jivesoftware .smack .util .StringUtils ;
13
17
import org .jivesoftware .smackx .muc .DiscussionHistory ;
14
18
import org .jivesoftware .smackx .muc .MultiUserChat ;
15
19
import org .jivesoftware .smackx .muc .Occupant ;
16
20
17
21
18
22
public class ChatServer {
19
23
20
- private final List < Room > rooms = new ArrayList < Room >();
24
+ private final Map < String , Room > rooms = new HashMap < String , Room >();
21
25
private final ChatServerConfig config ;
22
26
private final MessageHandler messageHandler ;
27
+ private final PrivateChats privateChats ;
23
28
24
29
private Connection connection ;
25
30
26
31
public ChatServer (ChatServerConfig config , MessageHandler messageHandler ) {
27
32
this .config = config ;
28
33
this .messageHandler = messageHandler ;
34
+ privateChats = new PrivateChats ();
29
35
}
30
36
31
37
public void connect () {
@@ -35,6 +41,7 @@ public void connect() {
35
41
System .out .println ("Connected to " + connection .getServiceName ());
36
42
connection .login (config .getUsername (), config .getPassword (), config .getResource ());
37
43
System .out .println ("Logged in as " + connection .getUser ());
44
+ connection .getChatManager ().addChatListener (new ChatHandler ());
38
45
} catch (XMPPException e ) {
39
46
System .out .println ("Failed to connect to " + config .getServer () + ": " + e );
40
47
}
@@ -47,13 +54,32 @@ public void disconnect() {
47
54
48
55
public void joinRoom (RoomConfig roomConfig ) {
49
56
try {
50
- rooms .add ( new Room (roomConfig ));
57
+ rooms .put ( roomConfig . getRoom (), new Room (roomConfig ));
51
58
} catch (XMPPException e ) {
52
59
System .out .println ("Failed to join " + roomConfig .getRoom () + ": " + e );
53
60
}
54
61
}
55
62
56
- private class Room implements PacketListener {
63
+ private final class ChatHandler implements ChatManagerListener {
64
+
65
+ @ Override
66
+ public void chatCreated (Chat chat , boolean createdLocally ) {
67
+ if (!createdLocally ) {
68
+ Room room = rooms .get (StringUtils .parseBareAddress (chat .getParticipant ()));
69
+ if (room != null ) {
70
+ System .out .println ("New room private chat by " + chat .getParticipant ());
71
+ chat .addMessageListener (room );
72
+ } else {
73
+ System .out .println ("New private chat by " + chat .getParticipant ());
74
+ chat .addMessageListener (privateChats );
75
+ }
76
+ } else {
77
+ System .out .println ("Ignore chat created locally: " + chat .getParticipant ());
78
+ }
79
+ }
80
+ }
81
+
82
+ private class Room implements PacketListener , MessageListener {
57
83
58
84
private MultiUserChat muc ;
59
85
@@ -77,19 +103,29 @@ public void processPacket(Packet packet) {
77
103
} else if (occupant .getNick ().equals (muc .getNickname ())) {
78
104
System .out .println ("Ignoring own message" );
79
105
}
80
- messageHandler .processMessage (msg .getBody (), new User (occupant ), false , new RoomReplier (muc , msg .getFrom ()));
106
+ messageHandler .processMessage (msg .getBody (), User . fromOccupant (occupant ), false , new RoomReplier (this , muc , msg .getFrom ()));
81
107
} else {
82
108
System .out .println ("Unhandled packet type from room: " + packet .getClass ());
83
109
}
84
110
}
111
+
112
+ @ Override
113
+ public void processMessage (Chat chat , Message msg ) {
114
+ System .out .println ("Room PM: " + chat .getParticipant () + ": " + msg .getBody ());
115
+ messageHandler .processMessage (msg .getBody (), User .fromRoomJID (chat .getParticipant ()), true , new PrivateReplier (chat ));
116
+ }
85
117
}
86
118
87
- private static class RoomReplier implements Replier {
119
+ private class RoomReplier implements Replier {
88
120
121
+ private final Room room ;
89
122
private final MultiUserChat muc ;
90
123
private final String user ;
91
124
92
- public RoomReplier (MultiUserChat muc , String user ) {
125
+ private Chat chat ;
126
+
127
+ public RoomReplier (Room room , MultiUserChat muc , String user ) {
128
+ this .room = room ;
93
129
this .muc = muc ;
94
130
this .user = user ;
95
131
}
@@ -103,6 +139,57 @@ public void reply(String text) {
103
139
}
104
140
}
105
141
142
+ @ Override
143
+ public void replyPrivately (String text ) {
144
+ try {
145
+ if (chat == null ) {
146
+ System .out .println ("Starting private chat with " + user );
147
+ chat = muc .createPrivateChat (user , room );
148
+ }
149
+ System .out .println ("Sending to " + chat .getParticipant () + ": " + text );
150
+ chat .sendMessage (text );
151
+ } catch (XMPPException e ) {
152
+ System .out .println ("Failed to send message to occupant " + chat .getParticipant () + ": " + e );
153
+ }
154
+ }
155
+
156
+ }
157
+
158
+ private class PrivateChats implements MessageListener {
159
+
160
+ public PrivateChats () {
161
+ }
162
+
163
+ @ Override
164
+ public void processMessage (Chat chat , Message msg ) {
165
+ System .out .println ("PM: " + chat .getParticipant () + ": " + msg .getBody ());
166
+ messageHandler .processMessage (msg .getBody (), User .fromRealJID (chat .getParticipant ()), true , new PrivateReplier (chat ));
167
+ }
168
+ }
169
+
170
+ private class PrivateReplier implements Replier {
171
+
172
+ private final Chat chat ;
173
+
174
+ public PrivateReplier (Chat chat ) {
175
+ this .chat = chat ;
176
+ }
177
+
178
+ @ Override
179
+ public void reply (String text ) {
180
+ try {
181
+ System .out .println ("PM to " + chat .getParticipant () + ": " + text );
182
+ chat .sendMessage (text );
183
+ } catch (XMPPException e ) {
184
+ System .out .println ("Failed to set PM to " + chat .getParticipant () + ": " + e );
185
+ }
186
+ }
187
+
188
+ @ Override
189
+ public void replyPrivately (String text ) {
190
+ reply (text );
191
+ }
192
+
106
193
}
107
194
108
195
}
0 commit comments