Skip to content

Commit

Permalink
move debug to config file. cleanup imesg callbacks. improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Sep 22, 2019
1 parent 71c77fc commit c80db46
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
16 changes: 8 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/motifini.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
state_file = "/usr/local/var/lib/motifini-subscribers.json"
port = 8765
temp_dir = "/private/tmp/"
debug = false
allowed_to = [
"user1@service.com",
"user2@email.com",
"+19259114969"
]

[imessage]
sql_path = "~/Library/Messages/chat.db"
sql_path = "~/Library/Messages/chat.db"
clear_messages = false
queue_size = 20

Expand Down
48 changes: 37 additions & 11 deletions pkg/messenger/imessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,66 @@ func (m *Messenger) recviMessageHandler(msg imessage.Incoming) {
resp := m.chat.HandleCommand(h)
// Send the reply as files and/or text.
if resp.Reply != "" {
m.Info.Printf("[%s] iMessage Reply to %s, size: %d", h.ID, msg.From, len(resp.Reply))
m.SendiMessage(imessage.Outgoing{To: msg.From, ID: h.ID, Text: resp.Reply})
}
for _, path := range resp.Files {
m.SendiMessage(imessage.Outgoing{To: msg.From, ID: h.ID, Text: path, File: true, Call: m.FileCallback})
m.SendiMessage(imessage.Outgoing{To: msg.From, ID: h.ID, Text: path, File: true})
}
}

// SendiMessage is how we send out a message or file via iMessage.
// Use this wrapper so the internal counters are updated, and callbacks used.
func (m *Messenger) SendiMessage(msg imessage.Outgoing) {
if msg.File {
m.Info.Printf("[%s] iMessage sending file to %s, file: %s", msg.ID, msg.To, msg.Text)
export.Map.Files.Add(1)
if msg.Call == nil {
msg.Call = m.fileCallback
}
} else {
m.Info.Printf("[%s] iMessage sending to %s, size: %d", msg.ID, msg.To, len(msg.Text))
export.Map.Sent.Add(1)
if msg.Call == nil {
msg.Call = m.msgCallback
}
}
m.imsg.Send(msg)
}

// FileCallback runs in a go routine after a video or picture iMessage is processed.
func (m *Messenger) FileCallback(msg *imessage.Response) {
var size int64
if fi, err := os.Stat(msg.Text); err == nil {
size = fi.Size()
// msgCallback is used as a generic callback function for messages. It just writes logs.
func (m *Messenger) msgCallback(msg *imessage.Response) {
if msg.Errs != nil {
export.Map.Errors.Add(1)
m.Error.Printf("[%v] m.Msgs.Send '%v': sent: %v, %d errs: %v", msg.ID, msg.To, msg.Sent, len(msg.Errs), msg.Errs)
}
if !msg.Sent {
return
}
m.Info.Printf("[%v] iMessage Reply SENT to %s, size: %d", msg.ID, msg.To, len(msg.Text))
}

// fileCallback runs in a go routine after a video or picture iMessage is processed.
func (m *Messenger) fileCallback(msg *imessage.Response) {
if msg.Errs != nil {
export.Map.Errors.Add(1)
m.Error.Printf("[%v] m.Msgs.Send '%v': %v", msg.ID, msg.To, msg.Errs)
} else {
m.Info.Printf("[%v] iMessage File '%v' (%.2fMb) sent to: %v", msg.ID, msg.Text, float32(size)/1024/1024, msg.To)
m.Error.Printf("[%v] m.Msgs.Send '%v': sent: %v, %d errs: %v", msg.ID, msg.To, msg.Sent, len(msg.Errs), msg.Errs)
}
if msg.Sent {
var size int64
if fi, err := os.Stat(msg.Text); err == nil {
size = fi.Size()
}
m.Info.Printf("[%v] iMessage File '%v' (%.2fMb) SENT to: %v", msg.ID, msg.Text, float32(size)/1024/1024, msg.To)
}
if !strings.HasPrefix(msg.Text, m.TempDir) {
// Only delete files in tempdir.
return
}
// Might take a while to upload.
time.Sleep(20 * time.Second)
if err := os.Remove(msg.Text); err != nil && !os.IsNotExist(err) {
m.Error.Printf("[%v] Remove(path): %v", msg.ID, err)
return
}
m.Debug.Printf("[%v] Deleted: %v", msg.ID, msg.Text)
m.Info.Printf("[%v] Deleted: %v", msg.ID, msg.Text)
}
5 changes: 4 additions & 1 deletion pkg/messenger/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
APIiMessage = "imessage"
)

// Messenger is all the data needed to initialize this library.
type Messenger struct {
chat *chat.Chat
imsg *imessage.Messages
Expand All @@ -31,6 +32,7 @@ type Messenger struct {
TempDir string
}

// New provides a messenger handler.
func New(m *Messenger) error {
if m.Conf == nil {
return fmt.Errorf("imessage config is nil")
Expand Down Expand Up @@ -59,12 +61,13 @@ func New(m *Messenger) error {
}

// SendFileOrMsg will send a notification to any subscriber provided using any supported messenger.
// This method is used by event handlers to notify subscribers.
func (m *Messenger) SendFileOrMsg(id, msg, path string, subs []*subscribe.Subscriber) {
for _, sub := range subs {
switch sub.API {
case APIiMessage:
if path != "" {
m.SendiMessage(imessage.Outgoing{ID: id, To: sub.Contact, Text: path, File: true, Call: m.FileCallback})
m.SendiMessage(imessage.Outgoing{ID: id, To: sub.Contact, Text: path, File: true})
}
if msg != "" {
m.SendiMessage(imessage.Outgoing{ID: id, To: sub.Contact, Text: msg})
Expand Down
11 changes: 5 additions & 6 deletions pkg/motifini/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type Motifini struct {

// Flags defines our application's CLI arguments.
type Flags struct {
Debug bool
VersionReq bool
ConfigFile string
*pflag.FlagSet
Expand All @@ -61,6 +60,7 @@ type Config struct {
TempDir string `toml:"temp_dir"`
StateFile string `toml:"state_file"`
AllowedTo []string `toml:"allowed_to"`
Debug bool
} `toml:"motifini"`
Imessage *imessage.Config `toml:"imessage"`
SecuritySpy *securityspy.Config `toml:"security_spy"`
Expand All @@ -74,28 +74,27 @@ func (flag *Flags) ParseArgs(args []string) {
flag.PrintDefaults()
}
flag.StringVarP(&flag.ConfigFile, "config", "c", "/usr/local/etc/"+Binary+".conf", "Config File")
flag.BoolVarP(&flag.Debug, "debug", "D", false, "Turn on the Spam.")
flag.BoolVarP(&flag.VersionReq, "version", "v", false, "Print the version and exit")
_ = flag.Parse(args) // flag.ExitOnError means this will never return != nil
}

// Start the daemon.
func Start() error {
rand.Seed(time.Now().UnixNano())
m := &Motifini{Flag: &Flags{}}
m := &Motifini{Flag: &Flags{}, Info: log.New(os.Stdout, "[INFO] ", log.LstdFlags)}
if m.Flag.ParseArgs(os.Args[1:]); m.Flag.VersionReq {
fmt.Printf("%s v%s\n", Binary, Version)
return nil // don't run anything else w/ version request.
}

m.setLogging()
export.Init(Binary) // Initialize the main expvar map.
export.Map.Version.Set(Version)
export.Map.ConfigFile.Set(m.Flag.ConfigFile)
if err := m.ParseConfigFile(); err != nil {
m.Flag.Usage()
return err
}
m.setLogging()
export.Map.ListenPort.Set(int64(m.Conf.Global.Port))
m.Info.Printf("Motifini %v Starting! (PID: %v)", Version, os.Getpid())
defer m.Info.Printf("Exiting!")
Expand All @@ -107,7 +106,7 @@ func Start() error {
func (m *Motifini) setLogging() {
debugOut := ioutil.Discard
flags := log.LstdFlags
if m.Flag.Debug {
if m.Conf.Global.Debug {
debugOut = os.Stdout
flags = log.LstdFlags | log.Lshortfile
}
Expand All @@ -121,7 +120,7 @@ func (m *Motifini) setLogging() {
func (m *Motifini) ParseConfigFile() error {
// Preload our defaults.
m.Conf = &Config{}
m.Info.Printf("Loading Configuration File: %s", m.Flag.ConfigFile)
m.Info.Println("Loading Configuration File:", m.Flag.ConfigFile)
switch buf, err := ioutil.ReadFile(m.Flag.ConfigFile); {
case err != nil:
return err
Expand Down
15 changes: 3 additions & 12 deletions pkg/webserver/webhandler_send_imessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/davidnewhall/motifini/pkg/export"
"github.com/davidnewhall/motifini/pkg/messenger"
"github.com/gorilla/mux"
"golift.io/imessage"
Expand Down Expand Up @@ -61,7 +60,7 @@ func (c *Config) processVideoRequest(id string, cam *securityspy.Camera, to stri
}
// Input data OK, video grabbed, send an attachment to each recipient.
for _, t := range strings.Split(to, ",") {
c.Msgs.SendiMessage(imessage.Outgoing{ID: id, To: t, Text: path, File: true, Call: c.Msgs.FileCallback})
c.Msgs.SendiMessage(imessage.Outgoing{ID: id, To: t, Text: path, File: true})
}
}

Expand Down Expand Up @@ -94,7 +93,7 @@ func (c *Config) sendPictureHandler(w http.ResponseWriter, r *http.Request) {
} else {
// Input data OK, send a message to each recipient.
for _, t := range to {
c.Msgs.SendiMessage(imessage.Outgoing{ID: id, To: t, Text: path, File: true, Call: c.Msgs.FileCallback})
c.Msgs.SendiMessage(imessage.Outgoing{ID: id, To: t, Text: path, File: true})
}
reply = "REQ ID: " + id + ", msg: " + reply + "\n"
}
Expand All @@ -115,21 +114,13 @@ func (c *Config) sendMessageHandler(w http.ResponseWriter, r *http.Request) {
break
}
}
callback := func(msg *imessage.Response) {
if msg.Errs != nil {
export.Map.Errors.Add(1)
c.Error.Printf("[%v] msgs.Msgs.Send '%v': %v", msg.ID, msg.To, msg.Errs)
return
}
c.Info.Printf("[%v] iMessage (%d chars) sent to: %v", msg.ID, len(msg.Text), msg.To)
}
if code == 500 || msg == "" {
c.Debug.Printf("[%v] Invalid 'to' provided or 'msg' empty: %v", id, msg)
code, reply = 500, "ERROR: Missing 'to' or 'msg'"
} else {
// Input data OK, send a message to each recipient.
for _, t := range to {
c.Msgs.SendiMessage(imessage.Outgoing{ID: id, To: t, Text: msg, File: false, Call: callback})
c.Msgs.SendiMessage(imessage.Outgoing{ID: id, To: t, Text: msg, File: false})
}
}
reply = "REQ ID: " + id + ", msg: " + reply + "\n"
Expand Down

0 comments on commit c80db46

Please sign in to comment.