-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
352afeb
commit 00dbf32
Showing
6 changed files
with
49 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package team.elrant.bubbles.xmpp; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jivesoftware.smack.chat2.Chat; | ||
import org.jivesoftware.smack.chat2.IncomingChatMessageListener; | ||
import org.jivesoftware.smack.packet.Message; | ||
import org.jxmpp.jid.BareJid; | ||
import org.jxmpp.jid.EntityBareJid; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public class ChatListener implements IncomingChatMessageListener { | ||
private static final Logger logger = LogManager.getLogger(ChatListener.class); | ||
public BareJid contactJid; | ||
Consumer<String> updateChatDisplay; | ||
|
||
public ChatListener(BareJid contactJid, Consumer<String> updateChatDisplay){ | ||
this.contactJid = contactJid; | ||
this.updateChatDisplay = updateChatDisplay; | ||
} | ||
|
||
/** | ||
* @param from The JID of the sender | ||
* @param message The message contents | ||
* @param chat The chat channel | ||
*/ | ||
@Override | ||
public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) { | ||
try { | ||
if (from != null && from.equals(contactJid) && message.getBody() != null) { | ||
updateChatDisplay.accept(message.getBody()); | ||
logger.info("Received message from {}: {}", from, message.getBody()); | ||
} | ||
} catch (Exception e) { | ||
logger.error("Error updating chat display: {}", e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ public class ConnectedUser extends User { | |
* @param password The password of the user. | ||
* @param serviceName The service name of the XMPP server. | ||
*/ | ||
public ConnectedUser(@NotNull String username, @NotNull String password, @NotNull String serviceName) throws IOException, ClassNotFoundException { | ||
public ConnectedUser(@NotNull String username, @NotNull String password, @NotNull String serviceName) { | ||
super(username, serviceName); | ||
this.password = password; | ||
} | ||
|
@@ -78,7 +78,7 @@ public void initializeConnection() throws SmackException, InterruptedException, | |
* @param contactJid The JID of the contact to add ([email protected]). | ||
* @param nickname The user-defined nickname of the contact, defaults to the contact's username. | ||
*/ | ||
private void addContact(@NotNull BareJid contactJid, @Nullable String nickname) { | ||
public void addContact(@NotNull BareJid contactJid, @Nullable String nickname) { | ||
try { | ||
if (roster != null && !roster.contains(contactJid)) { | ||
roster.createItemAndRequestSubscription(contactJid, nickname, null); | ||
|
@@ -159,7 +159,7 @@ public void saveUserToFile(@NotNull String filename, boolean savePassword) { | |
} | ||
|
||
logger.info("User information (excluding password) saved to {}", filename); | ||
} catch (IOException | ClassNotFoundException e) { | ||
} catch (IOException e) { | ||
logger.error("Error saving user information to file: {}", e.getMessage()); | ||
} | ||
} | ||
|
@@ -203,24 +203,10 @@ public void disconnect() { | |
/** | ||
* Adds an incoming message listener to the chat manager. | ||
*/ | ||
public void addIncomingMessageListener() { | ||
if (chatManager != null) { | ||
chatManager.addIncomingListener((from, message, chat) -> | ||
logger.info("Received message from {}: {}", from, message.getBody())); | ||
} | ||
} | ||
|
||
public void addIncomingMessageListener(BareJid contactJid, Consumer<String> updateChatDisplay) { | ||
if (chatManager != null) { | ||
chatManager.addIncomingListener((from, message, chat) -> { | ||
try { | ||
if (from != null && from.equals(contactJid) && message.getBody() != null) { | ||
updateChatDisplay.accept(message.getBody()); | ||
} | ||
} catch (Exception e) { | ||
logger.error("Error updating chat display: {}", e.getMessage()); | ||
} | ||
}); | ||
ChatListener chatListener = new ChatListener(contactJid, updateChatDisplay); | ||
chatManager.addIncomingListener(chatListener); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters