2018-01-13 15:57:13 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
const EnigmaAssert = require('./enigma_assert.js');
|
2022-06-05 20:04:25 +00:00
|
|
|
const Address = require('./ftn_address.js');
|
2023-01-12 05:37:09 +00:00
|
|
|
const { AddressFlavor } = require('./message.js');
|
2022-06-05 20:04:25 +00:00
|
|
|
const Message = require('./message.js');
|
2018-01-13 15:57:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.getAddressedToInfo = getAddressedToInfo;
|
2023-01-12 05:37:09 +00:00
|
|
|
exports.setExternalAddressedToInfo = setExternalAddressedToInfo;
|
|
|
|
exports.copyExternalAddressedToInfo = copyExternalAddressedToInfo;
|
2018-01-13 15:57:13 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const EMAIL_REGEX =
|
2023-01-22 01:51:54 +00:00
|
|
|
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}]?)|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
2018-01-13 15:57:13 +00:00
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Input Output
|
|
|
|
----------------------------------------------------------------------------------------------------
|
|
|
|
User { name : 'User', flavor : 'local' }
|
|
|
|
Some User { name : 'Some User', flavor : 'local' }
|
|
|
|
JoeUser @ 1:103/75 { name : 'JoeUser', flavor : 'ftn', remote : '1:103/75' }
|
|
|
|
Bob@1:103/705@fidonet.org { name : 'Bob', flavor : 'ftn', remote : '1:103/705@fidonet.org' }
|
|
|
|
1:103/705@fidonet.org { flavor : 'ftn', remote : '1:103/705@fidonet.org' }
|
|
|
|
Jane <23:4/100> { name : 'Jane', flavor : 'ftn', remote : '23:4/100' }
|
|
|
|
43:20/100.2 { flavor : 'ftn', remote : '43:20/100.2' }
|
|
|
|
foo@host.com { name : 'foo', flavor : 'email', remote : 'foo@host.com' }
|
|
|
|
Bar <baz@foobar.net> { name : 'Bar', flavor : 'email', remote : 'baz@foobar.com' }
|
2023-01-11 16:47:07 +00:00
|
|
|
@JoeUser@some.host.com { name : 'Joe User', flavor : 'activitypub', remote 'JoeUser@some.host.com' }
|
2018-01-13 15:57:13 +00:00
|
|
|
*/
|
|
|
|
function getAddressedToInfo(input) {
|
2018-06-22 05:15:04 +00:00
|
|
|
input = input.trim();
|
|
|
|
|
|
|
|
const firstAtPos = input.indexOf('@');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (firstAtPos < 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let addr = Address.fromString(input);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Address.isValidAddress(addr)) {
|
2023-01-12 05:37:09 +00:00
|
|
|
return { flavor: AddressFlavor.FTN, remote: input };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const lessThanPos = input.indexOf('<');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (lessThanPos < 0) {
|
2023-01-12 05:37:09 +00:00
|
|
|
return { name: input, flavor: AddressFlavor.Local };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const greaterThanPos = input.indexOf('>');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (greaterThanPos < lessThanPos) {
|
2023-01-12 05:37:09 +00:00
|
|
|
return { name: input, flavor: AddressFlavor.Local };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = Address.fromString(input.slice(lessThanPos + 1, greaterThanPos));
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Address.isValidAddress(addr)) {
|
|
|
|
return {
|
|
|
|
name: input.slice(0, lessThanPos).trim(),
|
2023-01-12 05:37:09 +00:00
|
|
|
flavor: AddressFlavor.FTN,
|
2022-06-05 20:04:25 +00:00
|
|
|
remote: addr.toString(),
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
return { name: input, flavor: AddressFlavor.Local };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 16:47:07 +00:00
|
|
|
if (firstAtPos === 0) {
|
|
|
|
const secondAtPos = input.indexOf('@', 1);
|
|
|
|
if (secondAtPos > 0) {
|
|
|
|
const m = input.slice(1).match(EMAIL_REGEX);
|
|
|
|
if (m) {
|
|
|
|
return {
|
|
|
|
name: input.slice(1, secondAtPos),
|
2023-01-12 05:37:09 +00:00
|
|
|
flavor: AddressFlavor.ActivityPub,
|
2023-01-11 16:47:07 +00:00
|
|
|
remote: input.slice(firstAtPos),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const lessThanPos = input.indexOf('<');
|
|
|
|
const greaterThanPos = input.indexOf('>');
|
|
|
|
if (lessThanPos > 0 && greaterThanPos > lessThanPos) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const addr = input.slice(lessThanPos + 1, greaterThanPos);
|
|
|
|
const m = addr.match(EMAIL_REGEX);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (m) {
|
|
|
|
return {
|
|
|
|
name: input.slice(0, lessThanPos).trim(),
|
2023-01-12 05:37:09 +00:00
|
|
|
flavor: AddressFlavor.Email,
|
2022-06-05 20:04:25 +00:00
|
|
|
remote: addr,
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
return { name: input, flavor: AddressFlavor.Local };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let m = input.match(EMAIL_REGEX);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (m) {
|
|
|
|
return {
|
|
|
|
name: input.slice(0, firstAtPos),
|
2023-01-12 05:37:09 +00:00
|
|
|
flavor: AddressFlavor.Email,
|
2022-06-05 20:04:25 +00:00
|
|
|
remote: input,
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
let addr = Address.fromString(input); // 5D?
|
|
|
|
if (Address.isValidAddress(addr)) {
|
2023-01-12 05:37:09 +00:00
|
|
|
return { flavor: AddressFlavor.FTN, remote: addr.toString() };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = Address.fromString(input.slice(firstAtPos + 1).trim());
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Address.isValidAddress(addr)) {
|
|
|
|
return {
|
|
|
|
name: input.slice(0, firstAtPos).trim(),
|
2023-01-12 05:37:09 +00:00
|
|
|
flavor: AddressFlavor.FTN,
|
2022-06-05 20:04:25 +00:00
|
|
|
remote: addr.toString(),
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
return { name: input, flavor: AddressFlavor.Local };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns true if it's an external address
|
|
|
|
function setExternalAddressedToInfo(addressInfo, message) {
|
|
|
|
const isValidAddressInfo = () => {
|
|
|
|
return addressInfo.name.length > 1 && addressInfo.remote.length > 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (addressInfo.flavor) {
|
|
|
|
case AddressFlavor.FTN:
|
|
|
|
case AddressFlavor.Email:
|
|
|
|
case AddressFlavor.QWK:
|
|
|
|
case AddressFlavor.NNTP:
|
|
|
|
case AddressFlavor.ActivityPub:
|
|
|
|
EnigmaAssert(isValidAddressInfo());
|
|
|
|
|
|
|
|
message.setRemoteToUser(addressInfo.remote);
|
|
|
|
message.setExternalFlavor(addressInfo.flavor);
|
|
|
|
message.toUserName = addressInfo.name;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case AddressFlavor.Local:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyExternalAddressedToInfo(fromMessage, toMessage) {
|
|
|
|
const sm = Message.SystemMetaNames;
|
|
|
|
toMessage.setRemoteToUser(fromMessage.meta.System[sm.RemoteFromUser]);
|
|
|
|
toMessage.setExternalFlavor(fromMessage.meta.System[sm.ExternalFlavor]);
|
2018-01-13 15:57:13 +00:00
|
|
|
}
|