diff --git a/README.md b/README.md index 158028a..bcc1d16 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Download [Latest Release Version](https://github.com/virtomize/mail2most/release ## build it yourself You can compile the project yourself using this repo and [mage](https://magefile.org). -Just clone the repo and run `mage build`, you can find the binary under `bin/mail2most` +Just clone the repo and run `mage service:build`, you can find the binary under `bin/mail2most` # Usage diff --git a/lib/config.go b/lib/config.go index 149f7aa..e1fda5c 100644 --- a/lib/config.go +++ b/lib/config.go @@ -50,7 +50,7 @@ type filter struct { } type mattermost struct { - URL, Team, Username, Password, AccessToken string + URL, Team, Username, Password, AccessToken, UserId string Channels []string Users []string Broadcast []string diff --git a/lib/mail2most.go b/lib/mail2most.go index d122ff3..84946e8 100644 --- a/lib/mail2most.go +++ b/lib/mail2most.go @@ -82,17 +82,19 @@ func (m Mail2Most) Run() error { if send { err := m.PostMattermost(p, mail) if err != nil { - m.Error("Mattermost Error", map[string]interface{}{ + m.Error("Right after PostMattermost, Mattermost Error. Email not set as synced in mattermost.", map[string]interface{}{ "Error": err, }) } else { alreadySend[p] = append(alreadySend[p], mail.ID) - } - err = writeToFile(alreadySend, m.Config.General.File) - if err != nil { - m.Error("File Error", map[string]interface{}{ - "Error": err, - }) + m.Debug("In mail2most Run, Before writeToFile on " + m.Config.General.File,nil) + err = writeToFile(alreadySend, m.Config.General.File) + + if err != nil { + m.Error("File Error", map[string]interface{}{ + "Error": err, + }) + } } } } diff --git a/lib/mattermost.go b/lib/mattermost.go index 88b6c4c..9b3c587 100644 --- a/lib/mattermost.go +++ b/lib/mattermost.go @@ -14,12 +14,14 @@ import ( func (m Mail2Most) mlogin(profile int) (*model.Client4, error) { c := model.NewAPIv4Client(m.Config.Profiles[profile].Mattermost.URL) - + m.Info("in mlogin", nil) if m.Config.Profiles[profile].Mattermost.Username != "" && m.Config.Profiles[profile].Mattermost.Password != "" { - _, resp := c.Login(m.Config.Profiles[profile].Mattermost.Username, m.Config.Profiles[profile].Mattermost.Password) + me, resp := c.Login(m.Config.Profiles[profile].Mattermost.Username, m.Config.Profiles[profile].Mattermost.Password) if resp.Error != nil { return nil, resp.Error } + m.Config.Profiles[profile].Mattermost.UserId = me.Id + } else if m.Config.Profiles[profile].Mattermost.AccessToken != "" { c.AuthToken = m.Config.Profiles[profile].Mattermost.AccessToken c.AuthType = "BEARER" @@ -28,7 +30,9 @@ func (m Mail2Most) mlogin(profile int) (*model.Client4, error) { return nil, err } u := model.UserFromJson(r.Body) + m.Config.Profiles[profile].Mattermost.Username = u.Email + m.Config.Profiles[profile].Mattermost.UserId = u.Id } else { return nil, fmt.Errorf("no username, password or token is set") } @@ -59,6 +63,7 @@ func (m Mail2Most) PostMattermost(profile int, mail Mail) error { if err != nil { return err } + defer c.Logout() // check if body is base64 encoded @@ -112,13 +117,13 @@ func (m Mail2Most) PostMattermost(profile int, mail Mail) error { email := fmt.Sprintf("%s@%s", mail.From[0].MailboxName, mail.From[0].HostName) user, resp := c.GetUserByEmail(email, "") if resp.Error != nil { - m.Debug("user not found in system", map[string]interface{}{"error": resp.Error}) + m.Debug("user not found in system for email " + email, map[string]interface{}{"error": resp.Error}) msg += m.getFromLine(profile, mail.From[0].PersonalName, email) } else { msg += m.getFromLine(profile, "@"+user.Username, email) } } - + //m.Debug("Before SubjectOnly/BodyOnly " + err.Error,nil) if m.Config.Profiles[profile].Mattermost.SubjectOnly && m.Config.Profiles[profile].Mattermost.BodyOnly { err := fmt.Errorf("config defines SubjectOnly and BodyOnly to be true which exclude each other") m.Error("Configuration inconsistency found", map[string]interface{}{"Config.Profile.Mattermost.SubjectOnly": true, "Config.Profile.Mattermost.BodyOnly": true, "error": err}) @@ -190,33 +195,24 @@ func (m Mail2Most) PostMattermost(profile int, mail Mail) error { return err } } - + m.Debug("In PostMattermost, Before m.Config.Profiles[profile].Mattermost.Users treatment",nil) if len(m.Config.Profiles[profile].Mattermost.Users) > 0 { var ( - me *model.User resp *model.Response ) - // who am i - // user is defined by its email address - if strings.Contains(m.Config.Profiles[profile].Mattermost.Username, "@") { - me, resp = c.GetUserByEmail(m.Config.Profiles[profile].Mattermost.Username, "") - if resp.Error != nil { - return resp.Error - } - } else { - me, resp = c.GetUserByEmail(m.Config.Profiles[profile].Mattermost.Username, "") - if resp.Error != nil { - return resp.Error - } - } - myid := me.Id + myid := m.Config.Profiles[profile].Mattermost.UserId for _, user := range m.Config.Profiles[profile].Mattermost.Users { var ( u *model.User ) + m.Debug("In PostMattermost, In m.Config.Profiles[profile].Mattermost.Users treatment ", map[string]interface{}{"length of user name": len(user)}) + + if len(user) == 0 { + return errors.New("User to send message to in Mattermost seems to be empty") + } // user is defined by its email address if strings.Contains(user, "@") { u, resp = c.GetUserByEmail(user, "")