added overwrite flag. improved chat display.

This commit is contained in:
Vishal 2022-09-06 03:40:34 +05:30
parent 78a840d851
commit 03ef8e5cf0
7 changed files with 50 additions and 19 deletions

View File

@ -47,6 +47,10 @@ usage: dart run bin/nostr_console.dart [OPTIONS]
-y, --difficulty <number> The difficulty number in bits, only for kind 1 messages. Tne next larger number divisible by 4 is
taken as difficulty. Can't be more than 24 bits, because otherwise it typically takes too much
time. Minimum and default is 0, which means no difficulty.
-v, --overwrite Will over write the file with all the events that were read from file, and all newly received. Is
useful when the file has to be cleared of old unused events. A backup should be made just in case
of original file before invoking.
```
To get ALL the latest messages for last 3 days (on linux which allows backtick execution):

View File

@ -24,6 +24,7 @@ const String disableFileArg = "disable-file";
const String difficultyArg = "difficulty";
const String translateArg = "translate";
const String colorArg = "color";
const String overWriteFlag = "overwrite";
void printUsage() {
print(gUsage);
@ -44,6 +45,7 @@ Future<void> main(List<String> arguments) async {
..addFlag(translateArg, abbr: "t", defaultsTo: false)
..addOption(colorArg, abbr:"c")
..addOption(difficultyArg, abbr:"y")
..addFlag(overWriteFlag, abbr:"v", defaultsTo: false)
..addFlag("debug");
try {
ArgResults argResults = parser.parse(arguments);
@ -56,6 +58,11 @@ Future<void> main(List<String> arguments) async {
gDebug = 1;
}
if( argResults[overWriteFlag]) {
print("Going to overwrite file at the end of program execution.");
gOverWriteFile = true;
}
if( argResults[translateArg]) {
gTranslate = true;

View File

@ -88,31 +88,26 @@ Future<void> sendChatMessage(Store node, String channelId, String messageToSend)
sendRequest( gListRelayUrls, toSendMessage);
}
// is same as above. remove it TODO
// send DM
Future<void> sendDirectMessage(Store node, String otherPubkey, String messageToSend) async {
String otherPubkey02 = "02" + otherPubkey;
String encryptedMessageToSend = myEncrypt(userPrivateKey, otherPubkey02, messageToSend);
//print("encrypted = $encryptedMessageToSend");
int ivIndex = encryptedMessageToSend.indexOf("?iv=");
/* int ivIndex = encryptedMessageToSend.indexOf("?iv=");
var iv = encryptedMessageToSend.substring( ivIndex + 4, encryptedMessageToSend.length);
var enc_str = encryptedMessageToSend.substring(0, ivIndex);
//print("enc_str = $enc_str len = ${enc_str.length} iv = $iv len = ${iv.length}");
String decrypted = myPrivateDecrypt(userPrivateKey, otherPubkey02, enc_str, iv);
//print( "decrypted = |$decrypted|");;
*/
String replyKind = "4";
String strTags = '["p","$otherPubkey"]';
strTags += gWhetherToSendClientTag?',["client","nostr_console"]':'';
int createdAt = DateTime.now().millisecondsSinceEpoch ~/1000;
String id = getShaId(userPublicKey, createdAt, replyKind, strTags, encryptedMessageToSend);
String sig = sign(userPrivateKey, id, "12345612345612345612345612345612");
String eventStrToSend = '["EVENT",{"id":"$id","pubkey":"$userPublicKey","created_at":$createdAt,"kind":$replyKind,"tags":[$strTags],"content":"$encryptedMessageToSend","sig":"$sig"}]';
//print(toSendMessage);
sendRequest( gListRelayUrls, eventStrToSend);
}

View File

@ -310,19 +310,28 @@ class EventData {
String getAsLine({int len = 20}) {
String contentToPrint = evaluatedContent.isEmpty? content: evaluatedContent;
//print("$contentToPrint|");
//print("len = ${contentToPrint.length}");
if( len == 0 || len > contentToPrint.length) {
len = contentToPrint.length;
}
contentToPrint = contentToPrint.padLeft(len);
contentToPrint = contentToPrint.replaceAll("\n", " ");
contentToPrint = contentToPrint.replaceAll("\r", " ");
String strToPrint = '"${contentToPrint.substring(0, len)}..." - ${getAuthorName(pubkey)}';
String strToPrint = '${contentToPrint.substring(0, len)}... - ${getAuthorName(pubkey)}';
if( isNotification) {
strToPrint = "$gNotificationColor$strToPrint$gColorEndMarker";
isNotification = false;
}
return strToPrint;
int strWidth = 40;
String paddedStrToPrint = strToPrint.padLeft(strWidth);
//print("\n$paddedStrToPrint");
paddedStrToPrint = paddedStrToPrint.substring(0, strWidth);
//print("\nstrToPrint = $strToPrint len = ${strToPrint.length} paddedStrToPrint = $paddedStrToPrint len = ${paddedStrToPrint.length}");
return paddedStrToPrint;
}
String getStrForChannel(int depth) {
@ -343,7 +352,7 @@ class EventData {
tempEvaluatedContent = tempContent = content; // content would be changed so show that
}
const int nameWidthDepth = 3; // how wide name will be in depth spaces
const int nameWidthDepth = 2; // how wide name will be in depth spaces
const int timeWidthDepth = 2;
int nameWidth = gSpacesPerDepth * nameWidthDepth;
String nameToPrint = name.padLeft(nameWidth).substring(0, nameWidth);
@ -949,7 +958,7 @@ try {
offset += cipherImpl.doFinal(cipherText, offset, finalPlainText, offset);
assert(offset == cipherText.length);
return finalPlainText.sublist(0, finalPlainText.length);
return finalPlainText.sublist(0, offset);
} catch(e) {
//print("cannot open file $gEventsFilename");
if( gDebug >= 0) print("Decryption error = $e");

View File

@ -7,10 +7,12 @@ String gCheckEventId = ""; //"1763016774ceaa8c135dce01e77923994c5afad4cd3e126704
const int gDefaultNumWaitSeconds = 3000; // is used in main()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// file related settings
const String gDefaultEventsFilename = "all_nostr_events.txt";
String gEventsFilename = ""; // is set in arguments, and if set, then file is read from and written to
bool gDontWriteOldEvents = true;
const int gDontSaveBeforeDays = 100; // dont save events older than this many days if gDontWriteOldEvents flag is true
bool gOverWriteFile = false; // overwrite the file, and don't just append. Will write all events in memory.
const int gDaysToGetEventsFor = 100; // when getting events, this is the since field (unless a fully formed request is given in command line)
@ -156,6 +158,9 @@ usage: $exename [OPTIONS]
-y, --difficulty <number> The difficulty number in bits, only for kind 1 messages. Tne next larger number divisible by 4 is
taken as difficulty. Can't be more than 24 bits, because otherwise it typically takes too much
time. Minimum and default is 0, which means no difficulty.
-v, --overwrite Will over write the file with all the events that were read from file, and all newly received. Is
useful when the file has to be cleared of old unused events. A backup should be made just in case
of original file before invoking.
""";
const String helpAndAbout =

View File

@ -839,13 +839,14 @@ class Store {
int numMessages = value.messageIds.length;
stdout.write("${name} ${getNumSpaces(32-name.length)} $numMessages${getNumSpaces(12- numMessages.toString().length)}");
// print latest event in one lin
// print latest event in one line
List<String> messageIds = value.messageIds;
for( int i = messageIds.length - 1; i >= 0; i++) {
if( allChildEventsMap.containsKey(messageIds[i])) {
Event? e = allChildEventsMap[messageIds[i]]?.event;
if( e!= null) {
stdout.write("${e.eventData.getAsLine()}");
String line = e.eventData.getAsLine();
stdout.write(line);
break; // print only one event, the latest one
}
}
@ -984,6 +985,10 @@ class Store {
try {
final File file = File(filename);
if( gOverWriteFile) {
await file.writeAsString("", mode: FileMode.write).then( (file) => file);
}
//await file.writeAsString("", mode: FileMode.append).then( (file) => file);
int eventCounter = 0;
String nLinesStr = "";
@ -993,8 +998,14 @@ class Store {
int linesWritten = 0;
for( var tree in allChildEventsMap.values) {
if( tree.event.readFromFile || tree.event.eventData.isDeleted) { // ignore those already in file; only the new ones are writen/appended to file, or those deleted
continue;
if( tree.event.eventData.isDeleted) { // dont write those deleted
continue;
}
if( gOverWriteFile == false) {
if( tree.event.readFromFile) { // ignore those already in file; only the new ones are writen/appended to file
continue;
}
}
// only write if its not too old

View File

@ -4,7 +4,7 @@ version: 0.0.7-beta
homepage: https://github.com/vishalxl/nostr_console
# colored new chat msgs
# improved display
environment:
sdk: '>=2.17.3 <3.0.0'