Skip to content

Instantly share code, notes, and snippets.

View chibenwa's full-sized avatar

Benoit TELLIER chibenwa

View GitHub Profile
@chibenwa
chibenwa / Auth.java
Last active September 13, 2019 04:01
Demonstrate use of commons-net with delegation on top of James, for writing Java administration scripts
public void authenticate(String targetUser, String authenticatorId, String password) throws IOException {
Preconditions.checkState(imapClient.getState() == IMAP.IMAPState.NOT_AUTH_STATE);
String loginString = targetUser + "\0" + authenticatorId + "\0" + password;
String b64 = Base64.getEncoder().encodeToString(loginString.getBytes(StandardCharsets.UTF_8));
boolean success = imapClient.doCommand(IMAPCommand.AUTHENTICATE, "\"PLAIN\" " + b64 + "\r\n");
if (!success) {
throw new IOException("Authentication failed");
}
new jmap.Client(new jmap.RequestTransport())
.withAPIUrl('http://192.168.1.23:44333/jmap')
.withAuthenticationToken('d13f3593-3c43-4540-9f83-46342b626696')
.listThenGetMessages()
.then((response) => {
var messages = response[1][1].list;
messages.forEach(element => {
console.log('email has subject ' + element.subject)
});
listThenGetMessages() {
const getMessageListCommandName = '#0';
const body = new BodyBuilder()
.appendGetMessageList({}, getMessageListCommandName)
.appendGetMessages({
"#ids": {
"resultOf": getMessageListCommandName,
"name": "getMessageList",
"path": "/messageIds"
export default class BodyBuilder {
constructor() {
this.result = [];
}
appendGetMessageList(options, name = '#0') {
this.append('getMessageList', options, name);
return this;
}
_jmapRequestWithMultipleCommands(commands) {
return this.transport
.post(this.apiUrl, this._defaultHeaders(), commands);
}
private List<MessageId> resolveMessagesIds(GetMessagesRequest getMessagesRequest, ExecutionContext executionContext) {
if (getMessagesRequest.getIdsBackReferencesPath().isPresent()) {
BackReferencesPath path = getMessagesRequest.getIdsBackReferencesPath().get();
return executionContext.retreiveBackReferences(path, MessageId.class);
}
return getMessagesRequest.getIds();
}
@JsonIgnore
@Override
public <T> List<T> resolve(ResultReferencesPath path, Class<T> clazz) {
Preconditions.checkArgument(path.getPath().equals("/messageIds"), "only messageIds is supported");
ResultReferenceTypeMismatchException.assertValidTypes(MessageId.class, clazz);
return (List<T>) getMessageIds();
}
@JsonProperty("#ids")
public Builder idsBackReference(BackReferencesPath backReferencesPath) {
this.idsBackReferencesPath = Optional.of(backReferencesPath);
return this;
}
@chibenwa
chibenwa / exemple.json
Created July 2, 2019 04:21
Exemple.json
[[ "Email/query", {
"accountId": "A1",
"filter": { "inMailbox": "id_of_inbox" },
"sort": [{ "property": "receivedAt", "isAscending": false }],
"collapseThreads": true,
"position": 0,
"limit": 10,
"calculateTotal": true
}, "t0" ],
[ "Email/get", {
@chibenwa
chibenwa / ExecutionContext.java
Last active July 2, 2019 04:20
JMAP Result reference
public class ExecutionContext {
private final HashMap<ClientId, Method.Response> previousResponses;
public void addResponse(ClientId clientId, Method.Response response) {
previousResponses.put(clientId, response);
}
public <T> List<T> retrieveResultReferences(ResultReferencesPath path, Class<T> clazz) {
ClientId callId = path.getMethodCallId();
Method.Response response = Optional.ofNullable(previousResponses.get(callId)).get();