package de.nerdclubtfg.signalbot.components;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;

import de.thoffbauer.signal4j.listener.ConversationListener;
import de.thoffbauer.signal4j.store.User;

public class SignalConsole extends Signal {
	
	private ArrayList<ConversationListener> listeners = new ArrayList<>();
	private Scanner scanner = new Scanner(System.in);

	@Override
	public void sendMessage(String address, SignalServiceDataMessage message) throws IOException {
		ArrayList<String> addresses = new ArrayList<>();
		addresses.add(address);
		sendMessage(addresses, message);
	}

	@Override
	public void sendMessage(List<String> addresses, SignalServiceDataMessage message) throws IOException {
		String out = String.format(
				"Send message to %s:\n"
				+ "%s",
				String.join(", ", addresses),
				message.getBody().or("no body"));
		System.out.println(out);
	}

	@Override
	public void addConversationListener(ConversationListener listener) {
		listeners.add(listener);
	}

	@Override
	public void pull(int timeoutMillis) throws IOException {
		System.out.print("Enter sender: ");
		String sender = scanner.nextLine();
		System.out.println("Body:");
		String body = "";
		String line;
		while(!(line = scanner.nextLine()).isEmpty()) {
			body += line + "\n";
		}
		
		SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
				.withTimestamp(System.currentTimeMillis())
				.withBody(body)
				.build();
		for(ConversationListener listener : listeners) {
			listener.onMessage(new User(sender), message, null);
		}
	}

}

@@ -83,73 +83,275 @@
 ava.
-util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Scanner;%0A
+nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.StandardCopyOption;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Scanner;%0A%0Aimport org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
 %0Aimp
@@ -1181,16 +1181,919 @@
 ody%22));%0A
+%09%09if(message.getAttachments().isPresent() && !message.getAttachments().get().isEmpty()) %7B%0A%09%09%09Path attachments = Paths.get(%22attachments%22);%0A%09%09%09if(!Files.exists(attachments)) %7B%0A%09%09%09%09Files.createDirectory(attachments);%0A%09%09%09%7D%0A%09%09%09message.getAttachments().get().stream()%0A%09%09%09%09%09.map(SignalServiceAttachment::asStream)%0A%09%09%09%09%09.map(stream -%3E %7B%0A%09%09%09%09%09%09Path path = attachments.resolve(%22bot%22 + System.currentTimeMillis() + %22.%22 %0A%09%09%09%09%09%09%09%09%09%09%09%09%09+ stream.getContentType().split(%22/%22)%5B1%5D);%0A%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09Files.copy(stream.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);%0A%09%09%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09%09%09System.err.println(%22Could not save an attachment! %22 + e.getMessage() + %22 (%22 + e.getClass().getSimpleName() + %22)%22);%0A%09%09%09%09%09%09%09return null;%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09return path;%0A%09%09%09%09%09%7D)%0A%09%09%09%09%09.filter(p -%3E p != null)%0A%09%09%09%09%09.forEach(p -%3E %7B%0A%09%09%09%09%09%09System.out.println(%22Saved an attachment to %22 + p.toString());%0A%09%09%09%09%09%7D);%0A%09%09%7D%0A
 %09%09System
@@ -2516,16 +2516,84 @@
 n%22;%0A%09%09%7D%0A
+%09%09body = body.substring(0, body.length() - 1); // strip off last %5Cn%0A
 %09%09%0A%09%09Sig

Add saving of attachments in offline mode (#18)