wind/lib/nntp_client.dart

206 lines
6.0 KiB
Dart
Raw Normal View History

2022-04-14 00:53:18 +00:00
import 'dart:async';
import 'dart:collection';
2022-04-15 02:46:18 +00:00
import 'package:enough_mail/mime_message.dart';
2022-04-14 00:53:18 +00:00
import 'package:web_socket_channel/web_socket_channel.dart';
2022-04-15 02:46:18 +00:00
import 'package:tuple/tuple.dart';
2022-04-14 00:53:18 +00:00
class NNTPClient {
late WebSocketChannel _channel;
2022-04-18 22:23:30 +00:00
final Queue<_NNTPCommand> _commandQueue = new Queue();
final List<String> _tempBuffer = [];
2022-04-15 02:46:18 +00:00
String? currentGroup;
2022-04-14 00:53:18 +00:00
NNTPClient(String addr) {
_channel = WebSocketChannel.connect(
Uri.parse("ws://$addr"),
);
_channel.stream.listen((data) {
2022-04-15 02:46:18 +00:00
if ((data as String).startsWith("201")) {
2022-04-14 00:53:18 +00:00
// skip welcome message
return;
}
var resp = data.toString();
2022-04-14 00:53:18 +00:00
var respLines = resp.split("\r\n");
2022-04-15 02:46:18 +00:00
if (respLines.last == "") respLines.removeLast(); // trailing empty line
2022-04-18 22:23:30 +00:00
if ((respLines.length > 1 || _tempBuffer.isNotEmpty) &&
2022-04-15 02:46:18 +00:00
respLines.last.codeUnits.last != ".".codeUnits.first) {
// if it's multiline response and it doesn't contain dot in the end
// then looks like we need to wait for next message to concatenate with current msg
2022-04-18 22:23:30 +00:00
_tempBuffer.add(resp);
2022-04-15 02:46:18 +00:00
return;
}
2022-04-18 22:23:30 +00:00
if (_tempBuffer.isNotEmpty) {
_tempBuffer.add(resp);
resp = _tempBuffer.join();
2022-04-15 02:46:18 +00:00
respLines = resp.split("\r\n");
respLines.removeLast(); // trailing empty line
2022-04-18 22:23:30 +00:00
_tempBuffer.clear();
2022-04-15 02:46:18 +00:00
}
2022-04-18 22:23:30 +00:00
var command = _commandQueue.removeFirst();
2022-04-14 00:53:18 +00:00
var respCode = int.parse(respLines[0].split(" ")[0]);
command.responseCompleter.complete(_CommandResponse(respCode, respLines));
});
}
Future<_CommandResponse> _sendCommand(
String command, List<String> args) async {
var cmd = _NNTPCommand(_CommandRequest(command, args));
2022-04-18 22:23:30 +00:00
_commandQueue.add(cmd);
2022-04-14 00:53:18 +00:00
if (args.length > 0) {
_channel.sink.add("$command ${args.join(" ")}\r\n");
} else {
_channel.sink.add("$command\r\n");
}
var result = await cmd.response;
return result;
}
Future<List<GroupInfo>> getNewsGroupList() async {
List<GroupInfo> l = [];
var groupMap = {};
await _sendCommand("LIST", ["NEWSGROUPS"]).then((value) {
value.lines.removeAt(0);
value.lines.removeLast();
value.lines.forEach((element) {
var firstSpace = element.indexOf(" ");
var name = element.substring(0, firstSpace);
groupMap.addAll({
name: {"desc": element.substring(firstSpace + 1)}
});
});
});
await _sendCommand("LIST", ["ACTIVE"]).then((value) {
value.lines.removeAt(0);
value.lines.removeLast();
value.lines.forEach((element) {
var splitted = element.split(" ");
var name = splitted[0];
var high = splitted[1];
var low = splitted[2];
groupMap[name]["high"] = high;
groupMap[name]["low"] = low;
});
});
groupMap.forEach((key, value) {
l.add(GroupInfo(key, value["desc"], int.parse(value["low"]),
int.parse(value["high"])));
});
return l;
}
2022-04-15 02:46:18 +00:00
Future<void> selectGroup(String name) async {
await _sendCommand("GROUP", [name]).then((value) => {currentGroup = name});
}
Future<List<Tuple2<int, MimeMessage>>> getNewThreads(
int perPage, int pageNum) async {
if (currentGroup == null) throw new ArgumentError("current group is null");
List<Tuple2<int, MimeMessage>> threads = [];
var newThreadList = await _sendCommand(
"NEWTHREADS", [perPage.toString(), pageNum.toString()]);
newThreadList.lines.removeAt(0);
newThreadList.lines.removeLast(); // remove dot
await Future.forEach<String>(newThreadList.lines, (element) async {
await _sendCommand("ARTICLE", [element]).then((response) {
response.lines.removeAt(0);
2022-04-15 04:03:42 +00:00
response.lines.removeLast();
var rawMsg = response.lines.join("\r\n");
threads
.add(Tuple2(int.parse(element), MimeMessage.parseFromText(rawMsg)));
});
});
return threads;
}
Future<MimeMessage> getPost(int postNumber) async {
var resp = await _sendCommand("ARTICLE", [postNumber.toString()]);
resp.lines.removeLast();
return MimeMessage.parseFromText(resp.lines.join("\r\n"));
}
Future<List<Tuple2<int, MimeMessage>>> getThread(int threadNumber) async {
2022-04-15 04:03:42 +00:00
if (currentGroup == null) throw new ArgumentError("current group is null");
List<Tuple2<int, MimeMessage>> threads = [];
var newThreadList = await _sendCommand("THREAD", [threadNumber.toString()]);
2022-04-15 04:03:42 +00:00
newThreadList.lines.removeAt(0);
newThreadList.lines.removeLast(); // remove dot
await Future.forEach<String>(newThreadList.lines, (element) async {
await _sendCommand("ARTICLE", [element]).then((response) {
response.lines.removeAt(0);
2022-04-15 02:46:18 +00:00
response.lines.removeLast();
var rawMsg = response.lines.join("\r\n");
threads
.add(Tuple2(int.parse(element), MimeMessage.parseFromText(rawMsg)));
});
});
return threads;
}
2022-04-18 22:23:30 +00:00
Future<int> postArticle(MimeMessage message) async {
var resp = await _sendCommand("POST", []);
if (resp.responseCode != 340) return resp.responseCode;
var rawMessage = message.renderMessage() + "\r\n.\r\n";
_channel.sink.add(rawMessage);
var cmd = _NNTPCommand(_CommandRequest("POST", []));
_commandQueue.add(cmd);
resp = await cmd.response;
return resp.responseCode;
}
2022-04-14 00:53:18 +00:00
}
class _NNTPCommand {
late _CommandRequest request;
late Future<_CommandResponse> response;
late Completer<_CommandResponse> responseCompleter;
_NNTPCommand(_CommandRequest request) {
this.request = request;
this.responseCompleter = Completer();
this.response = responseCompleter.future;
}
}
class _CommandRequest {
final String command;
final List<String> args;
_CommandRequest(this.command, this.args);
}
class _CommandResponse {
final int responseCode;
final List<String> lines;
_CommandResponse(this.responseCode, this.lines);
}
class GroupInfo {
final String name;
final String description;
final int lowWater;
final int highWater;
GroupInfo(this.name, this.description, this.lowWater, this.highWater);
}