commit 2a175e4bfc10ded5f79a73cf2b2cd8b6675c1dd2 Author: Hugo Thunnissen Date: Fri Jan 3 20:43:22 2020 +0100 Initial commit: working echo command + config boilerplate diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e9bbe6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/config.yml +/whapp-deltachat diff --git a/botcommands/echo.go b/botcommands/echo.go new file mode 100644 index 0000000..16f0bdf --- /dev/null +++ b/botcommands/echo.go @@ -0,0 +1,23 @@ +package botcommands + +import ( + "github.com/hugot/go-deltachat/deltachat" +) + +type Echo struct{} + +const echoPrefix = "!echo" + +func (e *Echo) Accepts(c *deltachat.Chat, m *deltachat.Message) bool { + messageText := m.GetText() + + return len(messageText) > len(echoPrefix) && messageText[0:len(echoPrefix)] == echoPrefix +} + +func (e *Echo) Execute(c *deltachat.Context, chat *deltachat.Chat, message *deltachat.Message) { + chatID := chat.GetID() + + messageText := message.GetText() + + c.SendTextMessage(chatID, messageText[len(echoPrefix)+1:]) +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..1cb8aa9 --- /dev/null +++ b/config.go @@ -0,0 +1,29 @@ +package main + +import ( + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +type AppConfig struct { + DataFolder string `yaml:"data_folder"` + UserAddress string `yaml:"user"` +} + +type Config struct { + Deltachat map[string]string `yaml:"deltachat"` + App AppConfig `yaml:"app"` +} + +func ConfigFromFile(filePath string) (*Config, error) { + fileContents, err := ioutil.ReadFile(filePath) + + if err != nil { + return nil, err + } + + config := &Config{} + + return config, yaml.Unmarshal(fileContents, config) +} diff --git a/deltachat.go b/deltachat.go new file mode 100644 index 0000000..85a66b6 --- /dev/null +++ b/deltachat.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "log" + + "github.com/hugot/go-deltachat/deltabot" + "github.com/hugot/go-deltachat/deltachat" + "github.com/hugot/whapp-deltachat/botcommands" +) + +func DcClientFromConfig(databasePath string, config map[string]string) *deltachat.Client { + client := &deltachat.Client{} + + // Handler for info logs from libdeltachat + client.On(deltachat.DC_EVENT_INFO, func(c *deltachat.Context, e *deltachat.Event) { + info, _ := e.Data2.String() + + log.Println(*info) + }) + + client.Open(databasePath) + + for key, value := range config { + client.SetConfig(key, value) + } + + // TODO: Make this configurable for users + client.SetConfig( + "server_flags", + fmt.Sprintf( + "%d", + deltachat.DC_LP_AUTH_NORMAL| + deltachat.DC_LP_IMAP_SOCKET_SSL| + deltachat.DC_LP_SMTP_SOCKET_STARTTLS, + ), + ) + + client.Configure() + + return client +} + +func BootstrapDcClientFromConfig(config Config) *deltachat.Client { + dcClient := DcClientFromConfig(config.App.DataFolder+"/deltachat.db", config.Deltachat) + + context := dcClient.Context() + userName := "user" + + userID := context.CreateContact(&userName, &config.App.UserAddress) + + context.SendTextMessage(context.CreateChatByContactID(userID), "Whapp-Deltachat initialized") + + bot := &deltabot.Bot{} + + bot.AddCommand(&botcommands.Echo{}) + + dcClient.On(deltachat.DC_EVENT_INCOMING_MSG, bot.HandleMessage) + + return dcClient +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b3b0991 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/hugot/whapp-deltachat + +go 1.13 + +require ( + github.com/hugot/go-deltachat v0.0.0-20200103100028-e93f1f3d8b97 + gopkg.in/yaml.v2 v2.2.2 +) + +replace github.com/hugot/go-deltachat v0.0.0-20200103100028-e93f1f3d8b97 => ../go-deltachat diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..542d234 --- /dev/null +++ b/go.sum @@ -0,0 +1,43 @@ +github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cheggaaa/pb v1.0.25/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/kvannotten/pcd v0.6.1/go.mod h1:7T/uPMn1rNucUEPEWtuwJIOBjqB+GBYg+tS5aEURXXs= +github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/afero v1.1.1/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.2.0/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/jwalterweatherman v0.0.0-20180814060501-14d3d4c51834/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.1.0/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..ba1c91d --- /dev/null +++ b/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "os" + "os/signal" +) + +func main() { + argLen := len(os.Args) + + if argLen != 2 { + fmt.Fprintln(os.Stderr, "Usage: whapp-deltachat CONFIG_FILE") + return + } + + configPath := os.Args[1] + + config, err := ConfigFromFile(configPath) + + if err != nil { + log.Fatal(err) + } + + err = ensureDirectory(config.App.DataFolder) + + if err != nil { + log.Fatal(err) + } + + dcClient := BootstrapDcClientFromConfig(*config) + + wait := make(chan os.Signal, 1) + signal.Notify(wait, os.Interrupt) + + for { + select { + case sig := <-wait: + log.Println(sig) + + // Give dc an opportunity to perform some close-down logic + // and close it's db etc. + dcClient.Close() + return + } + } + +} + +func ensureDirectory(dir string) error { + if _, err := os.Stat(dir); os.IsNotExist(err) { + err = os.MkdirAll(dir, 0700) + if err != nil { + return err + } + + } + + err := os.Chmod(dir, 0700) + if err != nil { + return err + } + + return nil +}