List<FtnMessage> unpackedEchomail = new ArrayList<>();
List<FtnMessage> packedNetmail = new ArrayList<>();
List<FtnMessage> unpackedNetmail = new ArrayList<>();
FtnAddress to = new FtnAddress(link.getLinkAddress());
String password = link.getPaketPassword();
FtnPkt header = new FtnPkt(selectOurAka(link), to, password, new Date());
for (FtnMessage message : messages) {
if (message.isNetmail()) {
if (packNetmail) {
packedNetmail.add(message);
} else {
unpackedNetmail.add(message);
}
} else {
if (packEchomail) {
packedEchomail.add(message);
} else {
unpackedEchomail.add(message);
}
}
}
if (!packedNetmail.isEmpty()) {
try {
for (FtnMessage net : packedNetmail) {
FtnPkt head = new FtnPkt(header.getFromAddr(), to,
password, new Date());
Message m = new Message(createZipFile(head, link,
Arrays.asList(net)));
m.setMessageName(generateEchoBundle());
ret.add(m);
}
} catch (Exception e) {
logger.l1(
"Error while writing netmail to link #" + link.getId(),
e);
}
}
if (!packedEchomail.isEmpty()) {
try {
Message m = new Message(createZipFile(header, link,
packedEchomail));
m.setMessageName(generateEchoBundle());
ret.add(m);
} catch (Exception e) {
logger.l1(
"Error while writing echomail to link #" + link.getId(),
e);
}
}
if (!unpackedNetmail.isEmpty()) {
try {
for (FtnMessage net : unpackedNetmail) {
FtnPkt head = new FtnPkt(header.getFromAddr(), to,
password, new Date());
File out = createOutboundFile(link);
FileOutputStream fos = new FileOutputStream(out);
fos.write(head.pack());
fos.write(net.pack());
fos.write(head.finalz());
fos.close();
Message m = new Message(out);
m.setMessageName(generate8d() + ".pkt");
ret.add(m);
}