2024-08-15 10:12:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-22 18:48:34 +02:00
|
|
|
"bytes"
|
2024-08-15 10:12:58 +02:00
|
|
|
"context"
|
2024-08-16 12:38:58 +02:00
|
|
|
"encoding/json"
|
2024-08-15 10:12:58 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2024-08-24 16:08:38 +02:00
|
|
|
"regexp"
|
2024-08-16 12:38:58 +02:00
|
|
|
"time"
|
2024-08-15 10:12:58 +02:00
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
"git.defalsify.org/vise.git/cache"
|
|
|
|
"git.defalsify.org/vise.git/engine"
|
2024-08-23 00:05:14 +02:00
|
|
|
"git.defalsify.org/vise.git/lang"
|
2024-08-15 12:49:36 +02:00
|
|
|
"git.defalsify.org/vise.git/persist"
|
2024-08-15 10:12:58 +02:00
|
|
|
"git.defalsify.org/vise.git/resource"
|
|
|
|
"git.defalsify.org/vise.git/state"
|
2024-08-26 13:34:38 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/models"
|
2024-08-26 12:40:20 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/server/handlers"
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
2024-08-15 10:12:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type fsData struct {
|
2024-08-15 12:49:36 +02:00
|
|
|
path string
|
2024-08-21 20:37:29 +02:00
|
|
|
st *state.State
|
2024-08-15 10:12:58 +02:00
|
|
|
}
|
|
|
|
|
2024-08-23 00:05:14 +02:00
|
|
|
func codeFromCtx(ctx context.Context) string {
|
|
|
|
var code string
|
|
|
|
engine.Logg.DebugCtxf(ctx, "in msg", "ctx", ctx, "val", code)
|
|
|
|
if ctx.Value("Language") != nil {
|
|
|
|
lang := ctx.Value("Language").(lang.Language)
|
|
|
|
code = lang.Code
|
|
|
|
}
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) save_firstname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(input) > 0 {
|
|
|
|
name := string(input)
|
|
|
|
accountData["FirstName"] = name
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) save_familyname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(input) > 0 {
|
|
|
|
//Save name
|
|
|
|
secondname := string(input)
|
|
|
|
accountData["FamilyName"] = secondname
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) save_yob(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(input) > 0 {
|
|
|
|
yob := string(input)
|
|
|
|
accountData["YOB"] = yob
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) save_location(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(input) > 0 {
|
|
|
|
location := string(input)
|
|
|
|
accountData["Location"] = location
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) save_gender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(input) > 0 {
|
|
|
|
gender := string(input)
|
|
|
|
|
|
|
|
switch gender {
|
2024-08-23 06:06:54 +02:00
|
|
|
case "1":
|
|
|
|
gender = "Male"
|
|
|
|
case "2":
|
|
|
|
gender = "Female"
|
|
|
|
case "3":
|
|
|
|
gender = "Other"
|
2024-08-23 05:35:49 +02:00
|
|
|
}
|
|
|
|
accountData["Gender"] = gender
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) save_offerings(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(input) > 0 {
|
|
|
|
offerings := string(input)
|
|
|
|
accountData["Offerings"] = offerings
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) set_language(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-15 10:12:58 +02:00
|
|
|
inputStr := string(input)
|
|
|
|
res := resource.Result{}
|
|
|
|
switch inputStr {
|
|
|
|
case "0":
|
|
|
|
res.FlagSet = []uint32{state.FLAG_LANG}
|
|
|
|
res.Content = "eng"
|
|
|
|
case "1":
|
|
|
|
res.FlagSet = []uint32{state.FLAG_LANG}
|
|
|
|
res.Content = "swa"
|
|
|
|
default:
|
|
|
|
}
|
2024-08-16 12:38:58 +02:00
|
|
|
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_LANGUAGE_SET)
|
2024-08-16 12:38:58 +02:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
f, err := os.OpenFile(fp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
f.Close()
|
2024-08-26 12:40:20 +02:00
|
|
|
accountResp, err := handlers.CreateAccount()
|
2024-08-21 20:49:27 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to create account:", err)
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-16 12:38:58 +02:00
|
|
|
|
|
|
|
accountData := map[string]string{
|
|
|
|
"TrackingId": accountResp.Result.TrackingId,
|
|
|
|
"PublicKey": accountResp.Result.PublicKey,
|
|
|
|
"CustodialId": accountResp.Result.CustodialId.String(),
|
|
|
|
"Status": "PENDING",
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, jsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_ACCOUNT_CREATED)
|
2024-08-16 12:38:58 +02:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) reset_unlock_for_update(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 05:35:49 +02:00
|
|
|
res := resource.Result{}
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_UNLOCKFORUPDATE)
|
2024-08-23 05:35:49 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) reset_account_unlocked(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 06:06:54 +02:00
|
|
|
res := resource.Result{}
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_UNLOCKED)
|
2024-08-23 06:06:54 +02:00
|
|
|
return res, nil
|
2024-08-23 05:35:49 +02:00
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) check_identifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-16 12:38:58 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Content = accountData["PublicKey"]
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) unlock(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-21 20:37:29 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
pin := string(input)
|
2024-08-23 15:01:13 +02:00
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(input) > 1 {
|
|
|
|
if pin != accountData["AccountPIN"] {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTPIN)
|
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_UNLOCKED)
|
2024-08-21 20:37:29 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-26 13:34:38 +02:00
|
|
|
if fsd.st.MatchFlag(models.USERFLAG_ACCOUNT_UNLOCKED, false) {
|
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTPIN)
|
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_UNLOCKFORUPDATE)
|
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_ACCOUNT_UNLOCKED)
|
2024-08-21 20:37:29 +02:00
|
|
|
} else {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_UNLOCKED)
|
2024-08-21 20:37:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-20 15:55:43 +02:00
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) reset_incorrect_pin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-20 15:55:43 +02:00
|
|
|
res := resource.Result{}
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTPIN)
|
2024-08-21 20:37:29 +02:00
|
|
|
return res, nil
|
2024-08-20 15:55:43 +02:00
|
|
|
}
|
|
|
|
|
2024-08-16 12:38:58 +02:00
|
|
|
func (fsd *fsData) check_account_status(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-26 12:40:20 +02:00
|
|
|
//status, err := checkAccountStatus(accountData["TrackingId"])
|
|
|
|
status, err := handlers.CheckAccountStatus(accountData["TrackingId"])
|
2024-08-16 12:38:58 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error checking account status:", err)
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
accountData["Status"] = status
|
|
|
|
|
2024-08-23 13:34:55 +02:00
|
|
|
if status == "SUCCESS" {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_ACCOUNT_SUCCESS)
|
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_PENDING)
|
2024-08-16 12:38:58 +02:00
|
|
|
} else {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagSet, models.USERFLAG_ACCOUNT_SUCCESS)
|
|
|
|
res.FlagSet = append(res.FlagReset, models.USERFLAG_ACCOUNT_PENDING)
|
2024-08-16 12:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-15 10:12:58 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-21 20:37:29 +02:00
|
|
|
func (fsd *fsData) quit(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 00:05:14 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
switch codeFromCtx(ctx) {
|
|
|
|
case "swa":
|
|
|
|
res.Content = "Asante kwa kutumia huduma ya Sarafu. Kwaheri!"
|
|
|
|
default:
|
|
|
|
res.Content = "Thank you for using Sarafu. Goodbye!"
|
2024-08-20 15:55:43 +02:00
|
|
|
}
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_UNLOCKED)
|
2024-08-20 15:55:43 +02:00
|
|
|
return res, nil
|
2024-08-19 09:15:26 +02:00
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) verify_yob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
date := string(input)
|
|
|
|
|
|
|
|
dateRegex := regexp.MustCompile(`^\d{2}/\d{2}/\d{4}$`)
|
|
|
|
isCorrectFormat := dateRegex.MatchString(date)
|
|
|
|
if !isCorrectFormat {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_INCORRECTDATEFORMAT)
|
2024-08-24 16:08:38 +02:00
|
|
|
} else {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
|
2024-08-24 16:08:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-25 21:18:55 +02:00
|
|
|
func (fsd *fsData) reset_incorrect_yob(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-24 16:08:38 +02:00
|
|
|
res := resource.Result{}
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_INCORRECTDATEFORMAT)
|
2024-08-24 16:08:38 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsd *fsData) check_balance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-19 12:36:17 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-26 12:40:20 +02:00
|
|
|
balance, err := handlers.CheckBalance(accountData["PublicKey"])
|
2024-08-19 12:36:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
res.Content = balance
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-19 09:15:26 +02:00
|
|
|
|
2024-08-20 15:25:57 +02:00
|
|
|
func (fsd *fsData) validate_recipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-20 09:15:33 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
recipient := string(input)
|
|
|
|
|
2024-08-20 14:09:02 +02:00
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-21 19:30:59 +02:00
|
|
|
if recipient != "0" {
|
|
|
|
// mimic invalid number check
|
|
|
|
if recipient == "000" {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_RECIPIENT)
|
2024-08-21 19:30:59 +02:00
|
|
|
res.Content = recipient
|
2024-08-20 14:09:02 +02:00
|
|
|
|
2024-08-21 19:30:59 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-20 14:09:02 +02:00
|
|
|
|
2024-08-21 19:30:59 +02:00
|
|
|
accountData["Recipient"] = recipient
|
2024-08-20 14:09:02 +02:00
|
|
|
|
2024-08-21 19:30:59 +02:00
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-20 14:09:02 +02:00
|
|
|
|
2024-08-21 19:30:59 +02:00
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-20 14:09:02 +02:00
|
|
|
}
|
2024-08-20 09:15:33 +02:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:25:57 +02:00
|
|
|
func (fsd *fsData) transaction_reset(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-20 09:15:33 +02:00
|
|
|
res := resource.Result{}
|
2024-08-20 14:09:02 +02:00
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the recipient
|
|
|
|
accountData["Recipient"] = ""
|
|
|
|
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_INVALID_RECIPIENT, models.USERFLAG_INVALID_RECIPIENT_WITH_INVITE)
|
2024-08-21 19:30:59 +02:00
|
|
|
|
2024-08-20 14:09:02 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
func (fsd *fsData) reset_transaction_amount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the amount
|
|
|
|
accountData["Amount"] = ""
|
|
|
|
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_INVALID_AMOUNT)
|
2024-08-22 14:44:28 +02:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 15:25:57 +02:00
|
|
|
func (fsd *fsData) max_amount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-20 14:09:02 +02:00
|
|
|
res := resource.Result{}
|
|
|
|
|
|
|
|
// mimic a max amount
|
|
|
|
res.Content = "10.00"
|
2024-08-20 15:25:57 +02:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsd *fsData) validate_amount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
amount := string(input)
|
|
|
|
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
if amount != "0" {
|
|
|
|
// mimic invalid amount
|
|
|
|
if amount == "00" {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = append(res.FlagSet, models.USERFLAG_INVALID_AMOUNT)
|
2024-08-22 14:44:28 +02:00
|
|
|
res.Content = amount
|
2024-08-20 15:25:57 +02:00
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-20 15:25:57 +02:00
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
res.Content = amount
|
2024-08-20 15:25:57 +02:00
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
accountData["Amount"] = amount
|
2024-08-20 15:25:57 +02:00
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-20 15:25:57 +02:00
|
|
|
|
2024-08-22 14:44:28 +02:00
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2024-08-20 15:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsd *fsData) get_recipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Content = accountData["Recipient"]
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) get_profile_info(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-23 06:06:54 +02:00
|
|
|
res := resource.Result{}
|
2024-08-23 05:35:49 +02:00
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-26 12:40:20 +02:00
|
|
|
name := accountData["FirstName"] + " " + accountData["FamilyName"]
|
2024-08-23 06:06:54 +02:00
|
|
|
gender := accountData["Gender"]
|
2024-08-26 12:40:20 +02:00
|
|
|
yob := accountData["YOB"]
|
2024-08-23 06:06:54 +02:00
|
|
|
location := accountData["Location"]
|
2024-08-26 12:40:20 +02:00
|
|
|
offerings := accountData["Offerings"]
|
|
|
|
|
|
|
|
layout := "02/01/2006"
|
2024-08-23 05:35:49 +02:00
|
|
|
|
2024-08-26 12:40:20 +02:00
|
|
|
birthdate, err := time.Parse(layout, yob)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentDate := time.Now()
|
|
|
|
formattedDate := currentDate.Format(layout)
|
|
|
|
today, err := time.Parse(layout, formattedDate)
|
|
|
|
if err != nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
age := utils.CalculateAge(birthdate, today)
|
|
|
|
|
|
|
|
formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %d\nLocation: %s\nYou provide: %s\n", name, gender, age, location, offerings)
|
2024-08-23 05:35:49 +02:00
|
|
|
res.Content = formattedData
|
|
|
|
|
2024-08-23 06:06:54 +02:00
|
|
|
return res, nil
|
2024-08-23 05:35:49 +02:00
|
|
|
}
|
|
|
|
|
2024-08-20 15:25:57 +02:00
|
|
|
func (fsd *fsData) get_sender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Content = accountData["PublicKey"]
|
|
|
|
|
2024-08-20 09:15:33 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:08:38 +02:00
|
|
|
func (fsd *fsData) quit_with_balance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
2024-08-21 20:49:27 +02:00
|
|
|
res := resource.Result{}
|
2024-08-21 20:37:29 +02:00
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-08-26 12:40:20 +02:00
|
|
|
balance, err := handlers.CheckBalance(accountData["PublicKey"])
|
2024-08-21 20:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
2024-08-21 20:49:27 +02:00
|
|
|
res.Content = fmt.Sprintf("Your account balance is: %s", balance)
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagReset = append(res.FlagReset, models.USERFLAG_ACCOUNT_UNLOCKED)
|
2024-08-21 20:37:29 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-22 18:48:34 +02:00
|
|
|
func (fsd *fsData) save_pin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
accountPIN := string(input)
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountData["AccountPIN"] = accountPIN
|
|
|
|
|
|
|
|
updatedJsonData, err := json.Marshal(accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(fp, updatedJsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fsd *fsData) verify_pin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
|
|
res := resource.Result{}
|
|
|
|
fp := fsd.path + "_data"
|
|
|
|
|
|
|
|
jsonData, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountData map[string]string
|
|
|
|
err = json.Unmarshal(jsonData, &accountData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(input, []byte(accountData["AccountPIN"])) {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = []uint32{models.USERFLAG_VALIDPIN}
|
|
|
|
res.FlagReset = []uint32{models.USERFLAG_PINMISMATCH}
|
2024-08-22 18:48:34 +02:00
|
|
|
} else {
|
2024-08-26 13:34:38 +02:00
|
|
|
res.FlagSet = []uint32{models.USERFLAG_PINMISMATCH}
|
2024-08-22 18:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
var (
|
|
|
|
scriptDir = path.Join("services", "registration")
|
|
|
|
)
|
|
|
|
|
2024-08-15 10:12:58 +02:00
|
|
|
func main() {
|
|
|
|
var dir string
|
|
|
|
var root string
|
|
|
|
var size uint
|
|
|
|
var sessionId string
|
|
|
|
flag.StringVar(&dir, "d", ".", "resource dir to read from")
|
|
|
|
flag.UintVar(&size, "s", 0, "max size of output")
|
2024-08-16 16:57:36 +02:00
|
|
|
flag.StringVar(&root, "root", "root", "entry point symbol")
|
2024-08-15 10:12:58 +02:00
|
|
|
flag.StringVar(&sessionId, "session-id", "default", "session id")
|
|
|
|
flag.Parse()
|
|
|
|
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-08-22 14:44:28 +02:00
|
|
|
st := state.NewState(15)
|
2024-08-19 09:15:26 +02:00
|
|
|
st.UseDebug()
|
2024-08-26 13:34:38 +02:00
|
|
|
state.FlagDebugger.Register(models.USERFLAG_LANGUAGE_SET, "LANGUAGE_CHANGE")
|
|
|
|
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_CREATED, "ACCOUNT_CREATED")
|
|
|
|
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_SUCCESS, "ACCOUNT_SUCCESS")
|
|
|
|
state.FlagDebugger.Register(models.USERFLAG_ACCOUNT_PENDING, "ACCOUNT_PENDING")
|
|
|
|
state.FlagDebugger.Register(models.USERFLAG_INCORRECTPIN, "INCORRECTPIN")
|
2024-08-19 09:59:20 +02:00
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
rfs := resource.NewFsResource(scriptDir)
|
|
|
|
ca := cache.NewCache()
|
|
|
|
cfg := engine.Config{
|
2024-08-16 16:57:36 +02:00
|
|
|
Root: "root",
|
2024-08-15 12:49:36 +02:00
|
|
|
SessionId: sessionId,
|
|
|
|
}
|
2024-08-15 10:12:58 +02:00
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
dp := path.Join(scriptDir, ".state")
|
|
|
|
err := os.MkdirAll(dp, 0700)
|
2024-08-15 10:12:58 +02:00
|
|
|
if err != nil {
|
2024-08-15 12:49:36 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
|
2024-08-15 10:12:58 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-15 12:49:36 +02:00
|
|
|
pr := persist.NewFsPersister(dp)
|
|
|
|
en, err := engine.NewPersistedEngine(ctx, cfg, pr, rfs)
|
2024-08-21 20:37:29 +02:00
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
if err != nil {
|
|
|
|
pr = pr.WithContent(&st, ca)
|
|
|
|
err = pr.Save(cfg.SessionId)
|
2024-08-17 16:57:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to save state with error: %v\n", err)
|
|
|
|
}
|
2024-08-15 12:49:36 +02:00
|
|
|
en, err = engine.NewPersistedEngine(ctx, cfg, pr, rfs)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "engine create exited with error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fp := path.Join(dp, sessionId)
|
|
|
|
fs := &fsData{
|
|
|
|
path: fp,
|
2024-08-21 20:37:29 +02:00
|
|
|
st: &st,
|
2024-08-15 12:49:36 +02:00
|
|
|
}
|
2024-08-24 16:08:38 +02:00
|
|
|
rfs.AddLocalFunc("select_language", fs.set_language)
|
2024-08-16 12:38:58 +02:00
|
|
|
rfs.AddLocalFunc("create_account", fs.create_account)
|
2024-08-22 18:48:34 +02:00
|
|
|
rfs.AddLocalFunc("save_pin", fs.save_pin)
|
|
|
|
rfs.AddLocalFunc("verify_pin", fs.verify_pin)
|
2024-08-24 16:08:38 +02:00
|
|
|
rfs.AddLocalFunc("check_identifier", fs.check_identifier)
|
2024-08-16 12:38:58 +02:00
|
|
|
rfs.AddLocalFunc("check_account_status", fs.check_account_status)
|
2024-08-24 16:08:38 +02:00
|
|
|
rfs.AddLocalFunc("unlock_account", fs.unlock)
|
2024-08-20 15:25:57 +02:00
|
|
|
rfs.AddLocalFunc("quit", fs.quit)
|
2024-08-24 16:08:38 +02:00
|
|
|
rfs.AddLocalFunc("check_balance", fs.check_balance)
|
2024-08-20 14:09:02 +02:00
|
|
|
rfs.AddLocalFunc("validate_recipient", fs.validate_recipient)
|
2024-08-20 09:15:33 +02:00
|
|
|
rfs.AddLocalFunc("transaction_reset", fs.transaction_reset)
|
2024-08-20 14:09:02 +02:00
|
|
|
rfs.AddLocalFunc("max_amount", fs.max_amount)
|
2024-08-20 15:25:57 +02:00
|
|
|
rfs.AddLocalFunc("validate_amount", fs.validate_amount)
|
2024-08-22 14:44:28 +02:00
|
|
|
rfs.AddLocalFunc("reset_transaction_amount", fs.reset_transaction_amount)
|
2024-08-20 15:25:57 +02:00
|
|
|
rfs.AddLocalFunc("get_recipient", fs.get_recipient)
|
|
|
|
rfs.AddLocalFunc("get_sender", fs.get_sender)
|
2024-08-24 16:08:38 +02:00
|
|
|
rfs.AddLocalFunc("reset_incorrect", fs.reset_incorrect_pin)
|
|
|
|
rfs.AddLocalFunc("save_firstname", fs.save_firstname)
|
|
|
|
rfs.AddLocalFunc("save_familyname", fs.save_familyname)
|
|
|
|
rfs.AddLocalFunc("save_gender", fs.save_gender)
|
|
|
|
rfs.AddLocalFunc("save_location", fs.save_location)
|
|
|
|
rfs.AddLocalFunc("save_yob", fs.save_yob)
|
|
|
|
rfs.AddLocalFunc("save_offerings", fs.save_offerings)
|
|
|
|
rfs.AddLocalFunc("quit_with_balance", fs.quit_with_balance)
|
|
|
|
rfs.AddLocalFunc("reset_unlocked", fs.reset_account_unlocked)
|
|
|
|
rfs.AddLocalFunc("reset_unlock_for_update", fs.reset_unlock_for_update)
|
|
|
|
rfs.AddLocalFunc("get_profile_info", fs.get_profile_info)
|
|
|
|
rfs.AddLocalFunc("verify_yob", fs.verify_yob)
|
2024-08-25 21:18:55 +02:00
|
|
|
rfs.AddLocalFunc("reset_incorrect_date_format", fs.reset_incorrect_yob)
|
2024-08-16 12:38:58 +02:00
|
|
|
|
2024-08-15 10:12:58 +02:00
|
|
|
cont, err := en.Init(ctx)
|
2024-08-21 20:37:29 +02:00
|
|
|
en.SetDebugger(engine.NewSimpleDebug(nil))
|
2024-08-15 10:12:58 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if !cont {
|
|
|
|
_, err = en.WriteResult(ctx, os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "dead init write error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-15 12:49:36 +02:00
|
|
|
err = en.Finish()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "engine finish error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-15 10:12:58 +02:00
|
|
|
os.Stdout.Write([]byte{0x0a})
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
err = engine.Loop(ctx, en, os.Stdin, os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|