Skip to content

Commit 571a11e

Browse files
committed
Add Avatar class to smack-repl
1 parent d51e6dd commit 571a11e

File tree

1 file changed

+126
-0
lines changed
  • smack-repl/src/main/java/org/igniterealtime/smack/smackrepl

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
*
3+
* Copyright 2020 Paul Schaub
4+
*
5+
* This file is part of smack-repl.
6+
*
7+
* smack-repl is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
package org.igniterealtime.smack.smackrepl;
22+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.File;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.io.OutputStream;
29+
import java.util.Scanner;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
32+
33+
import org.jivesoftware.smack.SmackException;
34+
import org.jivesoftware.smack.XMPPException;
35+
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
36+
import org.jivesoftware.smack.util.Async;
37+
import org.jivesoftware.smack.util.StringUtils;
38+
import org.jivesoftware.smackx.avatar.MemoryAvatarMetadataStore;
39+
import org.jivesoftware.smackx.avatar.MetadataInfo;
40+
import org.jivesoftware.smackx.avatar.UserAvatarManager;
41+
import org.jivesoftware.smackx.avatar.element.MetadataExtension;
42+
import org.jivesoftware.smackx.avatar.listener.AvatarListener;
43+
44+
import org.apache.commons.io.FileUtils;
45+
import org.bouncycastle.util.io.Streams;
46+
import org.jxmpp.jid.EntityBareJid;
47+
48+
/**
49+
* Connect to an XMPP account and download the avatars of all contacts.
50+
* Shutdown with "/quit".
51+
*/
52+
public class Avatar {
53+
54+
private static final Logger LOGGER = Logger.getLogger("Avatar");
55+
56+
public static void main(String[] args) throws IOException, InterruptedException, XMPPException, SmackException {
57+
if (args.length != 2) {
58+
throw new IllegalArgumentException("Usage: java Avatar <jid> <password>");
59+
}
60+
61+
XMPPTCPConnection connection = new XMPPTCPConnection(args[0], args[1]);
62+
63+
UserAvatarManager avatarManager = UserAvatarManager.getInstanceFor(connection);
64+
avatarManager.setAvatarMetadataStore(new MemoryAvatarMetadataStore());
65+
66+
File avatarDownloadDirectory = new File(FileUtils.getTempDirectory(), "avatarTest" + StringUtils.randomString(6));
67+
createDownloadDirectory(avatarDownloadDirectory);
68+
69+
avatarManager.addAvatarListener(new AvatarListener() {
70+
@Override
71+
public void onAvatarUpdateReceived(EntityBareJid user, MetadataExtension metadata) {
72+
Async.go(() -> {
73+
File userDirectory = new File(avatarDownloadDirectory, user.asUrlEncodedString());
74+
userDirectory.mkdirs();
75+
MetadataInfo avatarInfo = metadata.getInfoElements().get(0);
76+
File avatarFile = new File(userDirectory, avatarInfo.getId());
77+
78+
try {
79+
if (avatarInfo.getUrl() == null) {
80+
LOGGER.log(Level.INFO, "Fetch avatar from pubsub for " + user.toString());
81+
byte[] bytes = avatarManager.fetchAvatarFromPubSub(user, avatarInfo);
82+
writeAvatar(avatarFile, new ByteArrayInputStream(bytes));
83+
} else {
84+
LOGGER.log(Level.INFO, "Fetch avatar from " + avatarInfo.getUrl().toString() + " for " + user.toString());
85+
writeAvatar(avatarFile, avatarInfo.getUrl().openStream());
86+
}
87+
} catch (Exception e) {
88+
LOGGER.log(Level.SEVERE, "Error downloading avatar", e);
89+
}
90+
});
91+
}
92+
});
93+
94+
avatarManager.enable();
95+
96+
connection.connect().login();
97+
98+
Scanner input = new Scanner(System.in);
99+
while (true) {
100+
String line = input.nextLine();
101+
102+
if (line.equals("/quit")) {
103+
connection.disconnect();
104+
System.exit(0);
105+
break;
106+
}
107+
}
108+
}
109+
110+
private static void createDownloadDirectory(File avatarDownloadDirectory) throws IOException {
111+
if (!avatarDownloadDirectory.mkdirs()) {
112+
throw new IOException("Cannot create temp directory '" + avatarDownloadDirectory.getAbsolutePath() +"'");
113+
} else {
114+
LOGGER.info("Created temporary avatar download directory '" + avatarDownloadDirectory.getAbsolutePath() + "'");
115+
}
116+
}
117+
118+
private static void writeAvatar(File file, InputStream inputStream) throws IOException {
119+
file.createNewFile();
120+
OutputStream outputStream = new FileOutputStream(file);
121+
Streams.pipeAll(inputStream, outputStream);
122+
123+
inputStream.close();
124+
outputStream.close();
125+
}
126+
}

0 commit comments

Comments
 (0)