Update
This commit is contained in:
parent
f2d808f5d3
commit
41b191bdd3
6 changed files with 80 additions and 30 deletions
|
@ -5,9 +5,11 @@ enum StreamElementType { log, alert }
|
||||||
class StreamElement {
|
class StreamElement {
|
||||||
StreamElement(this.type, this.message) {
|
StreamElement(this.type, this.message) {
|
||||||
uuid = const Uuid().v4();
|
uuid = const Uuid().v4();
|
||||||
|
date = DateTime.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
late final String uuid;
|
late final String uuid;
|
||||||
|
late final DateTime date;
|
||||||
final String message;
|
final String message;
|
||||||
final StreamElementType type;
|
final StreamElementType type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:desktopapp/classes/stream_element.dart';
|
import 'package:desktopapp/classes/stream_element.dart';
|
||||||
import 'package:desktopapp/utils/logger.dart';
|
import 'package:desktopapp/utils/logger.dart';
|
||||||
|
import 'package:desktopapp/utils/requests.dart';
|
||||||
import 'package:enough_mail/enough_mail.dart';
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
|
|
||||||
class Mailer {
|
class Mailer {
|
||||||
|
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'];
|
||||||
static MailClient? _mailClient;
|
static MailClient? _mailClient;
|
||||||
|
|
||||||
/// High level mail API example
|
/// High level mail API example
|
||||||
|
@ -32,29 +39,11 @@ class Mailer {
|
||||||
await _mailClient!.selectInbox();
|
await _mailClient!.selectInbox();
|
||||||
streamController.add(
|
streamController.add(
|
||||||
StreamElement(StreamElementType.log, 'INBOX selected for $email'));
|
StreamElement(StreamElementType.log, 'INBOX selected for $email'));
|
||||||
|
List<String> whitelist = [email, ...whiteListEmail];
|
||||||
|
streamController.add(StreamElement(
|
||||||
|
StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}'));
|
||||||
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
||||||
if (event.eventType == ImapEventType.exists) {
|
await _onMessage(event, streamController, email, whitelist);
|
||||||
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) {
|
|
||||||
var mailEvt = MailLoadEvent(message, _mailClient!);
|
|
||||||
streamController.add(StreamElement(StreamElementType.alert,
|
|
||||||
mailEvt.message.decodeSubject() ?? ''));
|
|
||||||
// var content = mailEvt.message.decodeSubject();
|
|
||||||
// var mess = mailEvt.message.decodeTextPlainPart();
|
|
||||||
|
|
||||||
// Logger.log(LoggerType.info, Mailer, 'New message $content : $mess');
|
|
||||||
// Requests.sendAlert('*$content* $mess');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
await _mailClient!.startPolling();
|
await _mailClient!.startPolling();
|
||||||
return true;
|
return true;
|
||||||
|
@ -65,12 +54,53 @@ class Mailer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> disconnect() async {
|
static Future<void> disconnect({
|
||||||
|
required StreamController<StreamElement> streamController,
|
||||||
|
}) async {
|
||||||
if (_mailClient == null) return;
|
if (_mailClient == null) return;
|
||||||
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
|
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
|
||||||
await _mailClient!.stopPolling();
|
await _mailClient!.stopPolling();
|
||||||
await _mailClient!.disconnect();
|
await _mailClient!.disconnect();
|
||||||
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
|
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
|
||||||
_mailClient = null;
|
_mailClient = null;
|
||||||
|
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)];
|
||||||
|
await Requests.sendAlert(message);
|
||||||
|
streamController.add(StreamElement(StreamElementType.alert, message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import 'package:desktopapp/classes/stream_element.dart';
|
import 'package:desktopapp/classes/stream_element.dart';
|
||||||
|
import 'package:desktopapp/utils/requests.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
class StreamElementCard extends StatelessWidget {
|
class StreamElementCard extends StatelessWidget {
|
||||||
StreamElementCard({Key? key, required this.streamElement}) : super(key: key);
|
const StreamElementCard({Key? key, required this.streamElement})
|
||||||
|
: super(key: key);
|
||||||
|
|
||||||
final StreamElement streamElement;
|
final StreamElement streamElement;
|
||||||
|
|
||||||
|
@ -25,15 +28,29 @@ class StreamElementCard extends StatelessWidget {
|
||||||
children: [
|
children: [
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
|
Text(
|
||||||
|
DateFormat.jms().format(streamElement.date),
|
||||||
|
style: const TextStyle(fontSize: 16),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 8),
|
||||||
Icon(
|
Icon(
|
||||||
icon,
|
icon,
|
||||||
size: 30,
|
size: 30,
|
||||||
),
|
),
|
||||||
SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
Text(
|
Expanded(
|
||||||
streamElement.message,
|
child: Text(
|
||||||
style: TextStyle(fontSize: 16),
|
streamElement.message,
|
||||||
|
style: const TextStyle(fontSize: 16),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
|
if (streamElement.type == StreamElementType.alert)
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.send),
|
||||||
|
onPressed: () async {
|
||||||
|
await Requests.sendAlert(streamElement.message);
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
|
|
|
@ -43,7 +43,7 @@ class _HomePageState extends State<HomePage> {
|
||||||
children: [
|
children: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
await Mailer.disconnect();
|
await Mailer.disconnect(streamController: controller);
|
||||||
setState(() {
|
setState(() {
|
||||||
connected = false;
|
connected = false;
|
||||||
});
|
});
|
||||||
|
|
|
@ -206,7 +206,7 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.0.0"
|
version: "4.0.0"
|
||||||
intl:
|
intl:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: intl
|
name: intl
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
|
|
|
@ -41,6 +41,7 @@ dependencies:
|
||||||
dio: ^4.0.6
|
dio: ^4.0.6
|
||||||
enough_mail: ^1.3.6
|
enough_mail: ^1.3.6
|
||||||
uuid: ^3.0.6
|
uuid: ^3.0.6
|
||||||
|
intl: ^0.17.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in a new issue