1
0
Fork 0
mrtiboute/lib/utils/mailer.dart

130 lines
4.8 KiB
Dart
Raw Normal View History

2022-04-14 21:57:32 +02:00
import 'dart:async';
2022-04-14 22:48:15 +02:00
import 'dart:math';
2022-04-14 21:57:32 +02:00
import 'package:desktopapp/classes/stream_element.dart';
2022-04-12 22:28:30 +02:00
import 'package:desktopapp/utils/logger.dart';
2022-04-14 22:48:15 +02:00
import 'package:desktopapp/utils/requests.dart';
2022-04-26 21:28:49 +02:00
import 'package:desktopapp/widgets/popups/mailbox_select.dart';
2022-04-12 22:28:30 +02:00
import 'package:enough_mail/enough_mail.dart';
2022-04-26 21:28:49 +02:00
import 'package:flutter/cupertino.dart';
2022-04-12 22:28:30 +02:00
class Mailer {
2022-04-14 22:48:15 +02:00
static final List<String> customMessages = [
'Un gueux vient de commander de la HOLY',
'Un nouveau gueux vient de commander de la HOLY'
];
static final List<String> whiteListEmail = ['louis@holyenergy.fr'];
2022-04-14 21:57:32 +02:00
static MailClient? _mailClient;
2022-04-26 20:29:09 +02:00
static Timer? _timer;
2022-04-14 21:57:32 +02:00
2022-04-12 22:28:30 +02:00
/// High level mail API example
2022-04-14 21:57:32 +02:00
static Future<bool> connect({
2022-04-26 21:28:49 +02:00
required BuildContext context,
2022-04-14 21:57:32 +02:00
required String email,
required String password,
required StreamController<StreamElement> streamController,
}) async {
2022-04-12 22:28:30 +02:00
Logger.log(LoggerType.info, Mailer, 'Discovering settings for $email');
final config = await Discover.discover(email);
if (config == null) {
Logger.log(LoggerType.info, Mailer,
'Unable to autodiscover settings for $email');
2022-04-14 21:57:32 +02:00
return false;
2022-04-12 22:28:30 +02:00
}
Logger.log(LoggerType.info, Mailer, 'Settings for $email : $config');
final account = MailAccount.fromDiscoveredSettings(
'my account', email, password, config);
2022-04-14 21:57:32 +02:00
_mailClient = MailClient(account, isLogEnabled: true);
2022-04-12 22:28:30 +02:00
try {
2022-04-14 21:57:32 +02:00
await _mailClient!.connect();
2022-04-12 22:28:30 +02:00
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
2022-04-14 21:57:32 +02:00
streamController
.add(StreamElement(StreamElementType.log, 'Connected to $email'));
2022-04-26 21:28:49 +02:00
var mailboxes = await _mailClient!.listMailboxes();
var mailbox = await MailBoxSelect.show(context, mailboxes: mailboxes);
await _mailClient!.selectMailbox(mailbox!);
streamController.add(StreamElement(
StreamElementType.log, '${mailbox.name} selected for $email'));
2022-04-14 22:48:15 +02:00
List<String> whitelist = [email, ...whiteListEmail];
streamController.add(StreamElement(
StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}'));
2022-04-14 21:57:32 +02:00
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
2022-04-14 22:48:15 +02:00
await _onMessage(event, streamController, email, whitelist);
2022-04-12 22:28:30 +02:00
});
2022-04-26 20:29:09 +02:00
await _mailClient!.startPolling(const Duration(seconds: 1));
_startDebugProfiler(streamController);
2022-04-14 21:57:32 +02:00
return true;
2022-04-26 21:28:49 +02:00
} catch (e) {
streamController
.add(StreamElement(StreamElementType.error, e.toString()));
2022-04-14 21:57:32 +02:00
return false;
2022-04-12 22:28:30 +02:00
}
}
2022-04-14 21:57:32 +02:00
2022-04-14 22:48:15 +02:00
static Future<void> disconnect({
required StreamController<StreamElement> streamController,
}) async {
2022-04-14 21:57:32 +02:00
if (_mailClient == null) return;
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
2022-04-26 20:29:09 +02:00
_stopDebugProfiler();
2022-04-14 21:57:32 +02:00
await _mailClient!.stopPolling();
await _mailClient!.disconnect();
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
_mailClient = null;
2022-04-14 22:48:15 +02:00
streamController
.add(StreamElement(StreamElementType.log, 'Disconnected from mail'));
}
static Future<void> _onMessage(
ImapEvent event,
StreamController streamController,
String email,
List<String> whitelist) async {
if (event.eventType == ImapEventType.exists) {
final evt = event as ImapMessagesExistEvent;
if (evt.newMessagesExists <= evt.oldMessagesExists) return;
final sequence = MessageSequence();
if (evt.newMessagesExists - evt.oldMessagesExists > 1) {
sequence.addRange(evt.oldMessagesExists, evt.newMessagesExists);
} else {
sequence.add(evt.newMessagesExists);
}
final messages = await _mailClient!.fetchMessageSequence(sequence,
fetchPreference: FetchPreference.envelope);
for (final message in messages) {
MailAddress? mail = message.from?.cast<MailAddress?>().firstWhere(
(mEmail) => whitelist.contains(mEmail!.email),
orElse: () => null);
if (mail == null) continue;
var mailEvt = MailLoadEvent(message, _mailClient!);
_onMailEvent(mailEvt, streamController);
}
}
}
static Future<void> _onMailEvent(
MailLoadEvent mailLoadEvent, StreamController streamController) async {
final message =
customMessages[Random.secure().nextInt(customMessages.length)];
2022-04-17 22:32:27 +02:00
await Requests.sendAlert(message, type: AlertType.host);
2022-04-14 22:48:15 +02:00
streamController.add(StreamElement(StreamElementType.alert, message));
2022-04-14 21:57:32 +02:00
}
2022-04-26 20:29:09 +02:00
static void _startDebugProfiler(StreamController streamController) {
_timer = Timer.periodic(const Duration(seconds: 1), (timer) async {
if (!_mailClient!.isPolling()) {
streamController.add(StreamElement(StreamElementType.warning,
'Mail client is not polling | Restart it'));
await _mailClient!.startPolling();
}
});
}
static void _stopDebugProfiler() {
_timer?.cancel();
_timer = null;
}
2022-04-12 22:28:30 +02:00
}