2024-08-15 10:12:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2024-09-05 16:07:20 +02:00
|
|
|
"git.defalsify.org/vise.git/asm"
|
|
|
|
"git.defalsify.org/vise.git/db"
|
2024-09-04 18:57:08 +02:00
|
|
|
fsdb "git.defalsify.org/vise.git/db/fs"
|
2024-09-05 16:07:20 +02:00
|
|
|
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
|
2024-08-15 12:49:36 +02:00
|
|
|
"git.defalsify.org/vise.git/engine"
|
2024-09-07 21:05:09 +02:00
|
|
|
"git.defalsify.org/vise.git/logging"
|
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"
|
2024-08-26 14:53:07 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
2024-08-15 10:12:58 +02:00
|
|
|
)
|
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
var (
|
2024-09-07 21:05:09 +02:00
|
|
|
logg = logging.NewVanilla()
|
2024-08-15 12:49:36 +02:00
|
|
|
scriptDir = path.Join("services", "registration")
|
|
|
|
)
|
|
|
|
|
2024-09-06 10:03:01 +02:00
|
|
|
func getParser(fp string, debug bool) (*asm.FlagParser, error) {
|
2024-09-05 16:07:20 +02:00
|
|
|
flagParser := asm.NewFlagParser().WithDebug()
|
|
|
|
_, err := flagParser.Load(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return flagParser, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
|
|
|
|
|
2024-09-06 01:40:57 +02:00
|
|
|
ussdHandlers, err := ussd.NewHandlers(appFlags, userdataStore)
|
2024-09-05 16:07:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-06 01:40:57 +02:00
|
|
|
ussdHandlers = ussdHandlers.WithPersister(pe)
|
2024-09-05 16:07:20 +02:00
|
|
|
rs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
|
|
|
|
rs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
|
|
|
|
rs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
|
|
|
|
rs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin)
|
|
|
|
rs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier)
|
|
|
|
rs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus)
|
|
|
|
rs.AddLocalFunc("authorize_account", ussdHandlers.Authorize)
|
|
|
|
rs.AddLocalFunc("quit", ussdHandlers.Quit)
|
|
|
|
rs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance)
|
|
|
|
rs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient)
|
|
|
|
rs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset)
|
|
|
|
rs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount)
|
|
|
|
rs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount)
|
|
|
|
rs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount)
|
|
|
|
rs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient)
|
|
|
|
rs.AddLocalFunc("get_sender", ussdHandlers.GetSender)
|
|
|
|
rs.AddLocalFunc("get_amount", ussdHandlers.GetAmount)
|
|
|
|
rs.AddLocalFunc("reset_incorrect", ussdHandlers.ResetIncorrectPin)
|
|
|
|
rs.AddLocalFunc("save_firstname", ussdHandlers.SaveFirstname)
|
|
|
|
rs.AddLocalFunc("save_familyname", ussdHandlers.SaveFamilyname)
|
|
|
|
rs.AddLocalFunc("save_gender", ussdHandlers.SaveGender)
|
|
|
|
rs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
|
|
|
rs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
|
|
|
rs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
|
|
|
rs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
|
|
|
|
rs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
|
|
|
rs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
|
|
|
rs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
|
|
|
rs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob)
|
|
|
|
rs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob)
|
|
|
|
rs.AddLocalFunc("set_reset_single_edit", ussdHandlers.SetResetSingleEdit)
|
|
|
|
rs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction)
|
2024-09-14 19:52:17 +02:00
|
|
|
rs.AddLocalFunc("quit_with_help",ussdHandlers.QuitWithHelp)
|
2024-09-05 16:07:20 +02:00
|
|
|
|
|
|
|
return ussdHandlers, nil
|
|
|
|
}
|
|
|
|
|
2024-09-10 21:44:10 +02:00
|
|
|
func ensureDbDir(dbDir string) error {
|
2024-09-05 16:07:20 +02:00
|
|
|
err := os.MkdirAll(dbDir, 0700)
|
|
|
|
if err != nil {
|
2024-09-10 21:44:10 +02:00
|
|
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPersister(dbDir string, ctx context.Context) (*persist.Persister, error) {
|
|
|
|
err := ensureDbDir(dbDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-09-05 16:07:20 +02:00
|
|
|
}
|
|
|
|
store := gdbmdb.NewGdbmDb()
|
2024-09-05 17:45:35 +02:00
|
|
|
storeFile := path.Join(dbDir, "state.gdbm")
|
2024-09-05 16:07:20 +02:00
|
|
|
store.Connect(ctx, storeFile)
|
|
|
|
pr := persist.NewPersister(store)
|
|
|
|
return pr, nil
|
2024-09-04 18:57:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 16:07:20 +02:00
|
|
|
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
|
|
|
store := gdbmdb.NewGdbmDb()
|
|
|
|
storeFile := path.Join(dbDir, "userdata.gdbm")
|
|
|
|
store.Connect(ctx, storeFile)
|
2024-09-07 21:05:09 +02:00
|
|
|
|
2024-09-05 16:07:20 +02:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func getResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
|
|
|
store := fsdb.NewFsDb()
|
|
|
|
err := store.Connect(ctx, resourceDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-09-04 18:57:08 +02:00
|
|
|
}
|
2024-09-05 16:07:20 +02:00
|
|
|
rfs := resource.NewDbResource(store)
|
|
|
|
return rfs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) *engine.DefaultEngine {
|
|
|
|
en := engine.NewEngine(cfg, rs)
|
|
|
|
en = en.WithPersister(pr)
|
|
|
|
return en
|
2024-09-04 18:57:08 +02:00
|
|
|
}
|
|
|
|
|
2024-08-15 10:12:58 +02:00
|
|
|
func main() {
|
2024-09-05 16:07:20 +02:00
|
|
|
var dbDir string
|
2024-08-15 10:12:58 +02:00
|
|
|
var size uint
|
|
|
|
var sessionId string
|
2024-09-06 00:55:51 +02:00
|
|
|
var debug bool
|
2024-09-05 16:07:20 +02:00
|
|
|
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
|
|
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
2024-09-06 00:55:51 +02:00
|
|
|
flag.BoolVar(&debug, "d", false, "use engine debug output")
|
2024-09-05 16:07:20 +02:00
|
|
|
flag.UintVar(&size, "s", 160, "max size of output")
|
2024-09-05 17:45:35 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-09-10 11:32:19 +02:00
|
|
|
logg.Infof("start command", "dbdir", dbDir, "outputsize", size)
|
2024-08-15 10:12:58 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-09-07 21:05:09 +02:00
|
|
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
2024-09-02 20:06:42 +02:00
|
|
|
pfp := path.Join(scriptDir, "pp.csv")
|
2024-09-06 10:03:01 +02:00
|
|
|
flagParser, err := getParser(pfp, true)
|
2024-09-05 16:07:20 +02:00
|
|
|
|
2024-09-02 20:06:42 +02:00
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-19 09:59:20 +02:00
|
|
|
|
2024-08-15 12:49:36 +02:00
|
|
|
cfg := engine.Config{
|
2024-09-05 16:07:20 +02:00
|
|
|
Root: "root",
|
|
|
|
SessionId: sessionId,
|
|
|
|
OutputSize: uint32(size),
|
|
|
|
FlagCount: uint32(16),
|
2024-08-15 12:49:36 +02:00
|
|
|
}
|
2024-09-05 16:07:20 +02:00
|
|
|
|
2024-09-10 11:32:19 +02:00
|
|
|
rs, err := getResource(scriptDir, ctx)
|
2024-08-15 10:12:58 +02:00
|
|
|
if err != nil {
|
2024-09-05 16:07:20 +02:00
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
2024-08-15 10:12:58 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-08-21 20:37:29 +02:00
|
|
|
|
2024-09-06 01:40:57 +02:00
|
|
|
pe, err := getPersister(dbDir, ctx)
|
2024-08-15 12:49:36 +02:00
|
|
|
if err != nil {
|
2024-09-05 16:07:20 +02:00
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
2024-09-04 18:57:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 16:07:20 +02:00
|
|
|
store := getUserdataDb(dbDir, ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-09-04 18:57:08 +02:00
|
|
|
|
2024-09-05 17:45:35 +02:00
|
|
|
dbResource, ok := rs.(*resource.DbResource)
|
2024-09-04 18:57:08 +02:00
|
|
|
if !ok {
|
2024-09-06 01:40:57 +02:00
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
2024-09-04 18:57:08 +02:00
|
|
|
os.Exit(1)
|
2024-08-15 12:49:36 +02:00
|
|
|
}
|
2024-08-31 09:21:20 +02:00
|
|
|
|
2024-09-06 01:40:57 +02:00
|
|
|
hl, err := getHandler(flagParser, dbResource, pe, store)
|
2024-09-02 15:05:13 +02:00
|
|
|
if err != nil {
|
2024-09-05 16:07:20 +02:00
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
2024-08-31 09:21:20 +02:00
|
|
|
}
|
2024-08-26 14:53:07 +02:00
|
|
|
|
2024-09-06 01:40:57 +02:00
|
|
|
en := getEngine(cfg, rs, pe)
|
2024-09-06 00:55:51 +02:00
|
|
|
en = en.WithFirst(hl.Init)
|
|
|
|
if debug {
|
|
|
|
en = en.WithDebug(nil)
|
|
|
|
}
|
2024-08-16 12:38:58 +02:00
|
|
|
|
2024-09-04 18:57:08 +02:00
|
|
|
_, err = en.Init(ctx)
|
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)
|
|
|
|
}
|
2024-09-04 18:57:08 +02:00
|
|
|
|
2024-08-15 10:12:58 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|