Skip to content

Commit

Permalink
prototype for O365 and MFA. #2724
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Dec 6, 2024
1 parent 1967481 commit f76f11a
Show file tree
Hide file tree
Showing 16 changed files with 444 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,8 @@ public class MailSettingsTestAction extends ProgressableAction {

private boolean isMsExchange=false;
private String authToken=null;
private String clientId=null;
private String clientSecret=null;

public MailSettingsTestAction(ProgressIndicator i, JDialog owner, JButton cmdTestMail, String address, String outServer, String outPort, String outUser, String outPwd, boolean outSsl, boolean outStartTls, String inServer, String inUser, String inPwd, boolean inSsl, String inType, boolean isMsExchange, String clientId, String clientSecret, String authToken) {
public MailSettingsTestAction(ProgressIndicator i, JDialog owner, JButton cmdTestMail, String address, String outServer, String outPort, String outUser, String outPwd, boolean outSsl, boolean outStartTls, String inServer, String inUser, String inPwd, boolean inSsl, String inType, boolean isMsExchange, String authToken) {
super(i, false);
this.owner = owner;
this.cmdTestMail = cmdTestMail;
Expand All @@ -721,8 +719,6 @@ public MailSettingsTestAction(ProgressIndicator i, JDialog owner, JButton cmdTes

this.isMsExchange=isMsExchange;
this.authToken=authToken;
this.clientId=clientId;
this.clientSecret=clientSecret;
}

@Override
Expand Down Expand Up @@ -758,12 +754,12 @@ public boolean execute() throws Exception {
port = Integer.parseInt(this.outPort);
}
this.progress("Test läuft (Versand)...");
sysMan.testSendMail(this.outServer, port, this.outUser, this.outPwd, this.outSsl, this.outStartTls, this.address);
sysMan.testSendMail(this.outServer, port, this.outUser, this.outPwd, this.outSsl, this.outStartTls, this.address, this.isMsExchange, this.authToken);
this.progress("VERSAND: Testnachricht erfolgreich verschickt - bitte Posteingang prüfen");
Thread.sleep(5000);
process="Empfang / Posteingang";
this.progress("Test läuft (Empfang)...");
sysMan.testReceiveMail(this.address, this.inServer, this.inType, this.inSsl, this.inUser, this.inPwd, this.isMsExchange, this.clientId, this.clientSecret, this.authToken);
sysMan.testReceiveMail(this.address, this.inServer, this.inType, this.inSsl, this.inUser, this.inPwd, this.isMsExchange, this.authToken);
this.progress("EMPFANG: Posteingang erfolgreich geprüft");
Thread.sleep(3000);
SwingUtilities.invokeLater(() -> {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
<Component id="cmdUrl" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="txtCode" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="cmdCopy" alignment="3" min="-2" pref="28" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="txtCode" max="32767" attributes="0"/>
<Component id="cmdCopy" max="32767" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="progress" min="-2" max="-2" attributes="0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addComponent(txtUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdUrl))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cmdCopy, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtCode)
.addComponent(cmdCopy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,9 @@
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.Folder;
import javax.swing.Icon;
Expand All @@ -691,7 +693,10 @@ public class EmailFolderTreeCellRenderer extends DefaultTreeCellRenderer {
private static final String FOLDER_INBOX = "inbox";
private static final String FOLDER_DRAFTS = "drafts";

// for special folders like inbox, trash, ....
private static final Map<String, Icon> folderIcons = new HashMap<>();
// for ordinary folders
private static final List<String> defaultFolderIcons = new ArrayList<>();

private final ImageIcon draftsIcon = new javax.swing.ImageIcon(EmailFolderTreeCellRenderer.class.getResource("/icons/drafts.png"));
private final ImageIcon trashIcon = new javax.swing.ImageIcon(EmailFolderTreeCellRenderer.class.getResource("/icons/trashcan_full.png"));
Expand Down Expand Up @@ -773,54 +778,63 @@ public Component getTreeCellRendererComponent(JTree jTree, Object object, boolea

private Icon getIconByFolder(Folder f) {

if (!folderIcons.containsKey(f.getName())) {
String folderName=f.getName();

if(defaultFolderIcons.contains(folderName))
return null;

if (!folderIcons.containsKey(folderName)) {

if (FOLDER_TRASH.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), trashIcon);
} else if (FOLDER_SENT.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), sentIcon);
} else if (FOLDER_DRAFTS.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), draftsIcon);
} else if (FOLDER_INBOX.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), inboxIcon);
} else if ("in Akte importiert".equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), importedIcon);
if (FOLDER_TRASH.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, trashIcon);
} else if (FOLDER_SENT.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, sentIcon);
} else if (FOLDER_DRAFTS.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, draftsIcon);
} else if (FOLDER_INBOX.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, inboxIcon);
} else if ("in Akte importiert".equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, importedIcon);
} else {
boolean iconIsSet = false;
for (String a : EmailUtils.getFolderAliases(CommonMailUtils.TRASH)) {
if (a.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), trashIcon);
if (a.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, trashIcon);
iconIsSet = true;
break;
}
}
for (String a : EmailUtils.getFolderAliases(CommonMailUtils.DRAFTS)) {
if (a.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), draftsIcon);
if (a.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, draftsIcon);
iconIsSet = true;
break;
}
}
if (!iconIsSet) {
for (String a : EmailUtils.getFolderAliases(CommonMailUtils.SENT)) {
if (a.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), sentIcon);
if (a.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, sentIcon);
iconIsSet = true;
break;
}
}
}
if (!iconIsSet) {
for (String a : EmailUtils.getFolderAliases(CommonMailUtils.INBOX)) {
if (a.equalsIgnoreCase(f.getName())) {
folderIcons.put(f.getName(), inboxIcon);
if (a.equalsIgnoreCase(folderName)) {
folderIcons.put(folderName, inboxIcon);
break;
}
}
}
}
}
return folderIcons.get(f.getName());

if(folderIcons.get(folderName)==null && !defaultFolderIcons.contains(folderName))
defaultFolderIcons.add(folderName);

return folderIcons.get(folderName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,18 @@ public boolean isCellEditable(int row, int column) {
Folder folder = folderC.getFolder();
try {
if (!folder.isOpen()) {
folder.open(Folder.READ_WRITE);
try {
folder.open(Folder.READ_WRITE);
} catch (Exception ex) {
MailboxSetup ms=this.getMailboxSetup(selNode);
log.warn("Attempting to reconnect to " + ms.getEmailAddress());
String pw=ms.getEmailInPwd();
if(ms.isMsExchange())
pw=ms.getAuthToken();
folder.getStore().connect(ms.getEmailInServer(), ms.getEmailInUser(), pw);
folder.close(false);
folder.open(Folder.READ_WRITE);
}
}

DefaultTableModel tm = new DefaultTableModel(new String[]{"Betreff", "Absender", "Empfänger", "Gesendet"}, 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,14 +732,14 @@ public int getUnreadMessageCount() {

if (this.cachedUnreadUpdated == -1 || ((System.currentTimeMillis() - cachedUnreadUpdated) > this.getRetentionTime())) {
if (this.folder != null) {
this.cachedUnreadUpdated = System.currentTimeMillis();
try {
this.cachedUnread = this.folder.getUnreadMessageCount();
} catch (StoreClosedException stex) {
log.warn("Unable to determine number of unread messages - folder is closed");
} catch (MessagingException ex) {
log.error("Unable to determine number of unread messages", ex);
}
this.cachedUnreadUpdated = System.currentTimeMillis();
}
}
return cachedUnread;
Expand All @@ -766,7 +766,8 @@ public String toString() {
}
}

int msgCount = this.folder.getMessageCount();
//int msgCount = this.folder.getMessageCount();
int msgCount = this.getUnreadMessageCount();
cachedToStringUpdated = System.currentTimeMillis();
if (msgCount < 0) {
cachedToString = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@
*/
package com.jdimension.jlawyer.client.mail;

import com.jdimension.jlawyer.client.editors.EditorsRegistry;
import com.jdimension.jlawyer.client.processing.ProgressIndicator;
import com.jdimension.jlawyer.client.processing.ProgressableAction;
import com.jdimension.jlawyer.client.settings.ClientSettings;
Expand All @@ -678,6 +677,7 @@
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.search.AndTerm;
import javax.mail.search.BodyTerm;
import javax.mail.search.FlagTerm;
import javax.mail.search.FromStringTerm;
Expand All @@ -703,15 +703,15 @@ public class LoadFolderAction extends ProgressableAction {
private JTable table = null;
private int sortCol = -1;
private int scrollToRow = -1;
private String searchTerm=null;
private String searchTerm = null;

public LoadFolderAction(ProgressIndicator i, Folder f, JTable table, int sortCol, int scrollToRow, String searchTerm) {
super(i, false);
this.f = f;
this.table = table;
this.sortCol = sortCol;
this.scrollToRow = scrollToRow;
this.searchTerm=searchTerm;
this.searchTerm = searchTerm;
}

@Override
Expand All @@ -732,18 +732,17 @@ public int getMin() {
@Override
public boolean execute() throws Exception {
try {
EditorsRegistry.getInstance().updateStatus("Öffne Ordner " + f.getName(), true);

if (!(f.isOpen())) {
f.open(Folder.READ_WRITE);
}
try {
if (EmailUtils.isIMAP(f)) {
f.expunge();
}
} catch (Throwable t) {
log.error("Could not expunge folder", t);
}
// try {
// if (EmailUtils.isIMAP(f)) {
// f.expunge();
// }
// } catch (Throwable t) {
// log.error("Could not expunge folder", t);
// }
// System.out.println("load 20 = " + (System.currentTimeMillis() - start));

ClientSettings cs = ClientSettings.getInstance();
String restriction = cs.getConfiguration(ClientSettings.CONF_MAIL_DOWNLOADRESTRICTION, "" + LoadFolderRestriction.RESTRICTION_50);
Expand All @@ -757,38 +756,57 @@ public boolean execute() throws Exception {

int fromIndex = 1;
int toIndex = f.getMessageCount();
int maxQuantity=Integer.MAX_VALUE;
int maxQuantity = Integer.MAX_VALUE;
switch (currentRestriction.getRestriction()) {
case LoadFolderRestriction.RESTRICTION_20:
fromIndex = Math.max(1, toIndex - 20);
maxQuantity=20;
maxQuantity = 20;
break;
case LoadFolderRestriction.RESTRICTION_50:
fromIndex = Math.max(1, toIndex - 50);
maxQuantity=50;
maxQuantity = 50;
break;
case LoadFolderRestriction.RESTRICTION_100:
fromIndex = Math.max(1, toIndex - 100);
maxQuantity=100;
maxQuantity = 100;
break;
case LoadFolderRestriction.RESTRICTION_500:
fromIndex = Math.max(1, toIndex - 500);
maxQuantity=500;
maxQuantity = 500;
break;
default:
break;
}

Message[] messages =null;
if(StringUtils.isEmpty(this.searchTerm)) {
messages = f.getMessages(fromIndex, toIndex);
Message[] messages = null;
FlagTerm notDeleted = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
if (StringUtils.isEmpty(this.searchTerm)) {
if (currentRestriction.getRestriction() == LoadFolderRestriction.RESTRICTION_UNREAD) {
messages=f.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false), messages);
// Combine unread filter with notDeleted filter
FlagTerm unread = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
AndTerm filter = new AndTerm(unread, notDeleted);
messages = f.search(filter, messages);
} else {
messages = f.getMessages(fromIndex, toIndex);

// Apply the filter to the subset of messages
messages = f.search(notDeleted, messages);
}
} else {
maxQuantity=Integer.MAX_VALUE;
OrTerm orTerm=new OrTerm(new SearchTerm[] {new SubjectTerm(this.searchTerm), new FromStringTerm(this.searchTerm), new RecipientStringTerm(Message.RecipientType.TO, this.searchTerm), new RecipientStringTerm(Message.RecipientType.CC, this.searchTerm), new RecipientStringTerm(Message.RecipientType.BCC, this.searchTerm), new BodyTerm(this.searchTerm)});
messages=f.search(orTerm);
maxQuantity = Integer.MAX_VALUE;
// Build the OR search term for subject, sender, recipients, and body
OrTerm orTerm = new OrTerm(new SearchTerm[]{
new SubjectTerm(this.searchTerm),
new FromStringTerm(this.searchTerm),
new RecipientStringTerm(Message.RecipientType.TO, this.searchTerm),
new RecipientStringTerm(Message.RecipientType.CC, this.searchTerm),
new RecipientStringTerm(Message.RecipientType.BCC, this.searchTerm),
new BodyTerm(this.searchTerm)
});

// Combine the OR term with notDeleted filter
AndTerm filter = new AndTerm(orTerm, notDeleted);
messages = f.search(filter);
}

FetchProfile fp = new FetchProfile();
Expand All @@ -812,7 +830,6 @@ public boolean execute() throws Exception {

if ((i % 100) == 0 || i == (messages.length - 1)) {
this.progress("Lade Nachrichten... " + (i + 1));
EditorsRegistry.getInstance().updateStatus("Lade Nachricht " + (i + 1), true);
}

final Message msg = messages[i];
Expand Down Expand Up @@ -863,9 +880,9 @@ public boolean execute() throws Exception {
}
}

String sentString="";
if(msg.getSentDate()!=null) {
sentString=df.format(msg.getSentDate());
String sentString = "";
if (msg.getSentDate() != null) {
sentString = df.format(msg.getSentDate());
}
Object[] newRow = new Object[]{new MessageContainer(msg, msg.getSubject(), msg.isSet(Flags.Flag.SEEN)), from, toString, sentString};
tableRows.add(newRow);
Expand All @@ -881,7 +898,7 @@ public boolean execute() throws Exception {
try {
for (Object[] rowObject : tableRowsClone) {
((DefaultTableModel) table.getModel()).addRow(rowObject);

}
if (scrollToRow > 0) {
if (currentIndex == (indexMax)) {
Expand Down Expand Up @@ -913,8 +930,6 @@ public boolean execute() throws Exception {
ComponentUtils.autoSizeColumns(table, 0);
});

EditorsRegistry.getInstance().clearStatus(true);

new Thread(() -> {
try {
Thread.sleep(5000);
Expand All @@ -925,7 +940,6 @@ public boolean execute() throws Exception {
log.error(t);
}
}).start();

} catch (Throwable ex) {
log.error(ex);
return false;
Expand Down
Loading

0 comments on commit f76f11a

Please sign in to comment.