Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 Allow to receive ItemId and support calendar refresh #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ const (
ResponseClassError ResponseClass = "Error"
)

type ResponseItems struct {
Message []ItemId `xml:"Items>Message>ItemId"`
CalendarItem []ItemId `xml:"Items>CalendarItem>ItemId"`
}

type Response struct {
ResponseClass ResponseClass `xml:"ResponseClass,attr"`
MessageText string `xml:"MessageText"`
ResponseCode string `xml:"ResponseCode"`
MessageXml MessageXml `xml:"MessageXml"`

ResponseItems
}

type EmailAddress struct {
Expand Down
68 changes: 37 additions & 31 deletions create_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ type Message struct {
}

type CalendarItem struct {
Subject string `xml:"t:Subject"`
Body Body `xml:"t:Body"`
ReminderIsSet bool `xml:"t:ReminderIsSet"`
ReminderMinutesBeforeStart int `xml:"t:ReminderMinutesBeforeStart"`
Start time.Time `xml:"t:Start"`
End time.Time `xml:"t:End"`
IsAllDayEvent bool `xml:"t:IsAllDayEvent"`
LegacyFreeBusyStatus string `xml:"t:LegacyFreeBusyStatus"`
Location string `xml:"t:Location"`
RequiredAttendees []Attendees `xml:"t:RequiredAttendees"`
OptionalAttendees []Attendees `xml:"t:OptionalAttendees"`
Resources []Attendees `xml:"t:Resources"`
Subject string `xml:"t:Subject,omitempty"`
Body Body `xml:"t:Body,omitempty"`
ReminderIsSet bool `xml:"t:ReminderIsSet,omitempty"`
ReminderMinutesBeforeStart int `xml:"t:ReminderMinutesBeforeStart,omitempty"`
Start time.Time `xml:"t:Start,omitempty"`
End time.Time `xml:"t:End,omitempty"`
IsAllDayEvent bool `xml:"t:IsAllDayEvent,omitempty"`
LegacyFreeBusyStatus string `xml:"t:LegacyFreeBusyStatus,omitempty"`
Location string `xml:"t:Location,omitempty"`
RequiredAttendees []Attendees `xml:"t:RequiredAttendees,omitempty"`
OptionalAttendees []Attendees `xml:"t:OptionalAttendees,omitempty"`
Resources []Attendees `xml:"t:Resources,omitempty"`
}

type Body struct {
Expand Down Expand Up @@ -75,21 +75,24 @@ type createItemResponseBodyEnvelop struct {
XMLName struct{} `xml:"Envelope"`
Body createItemResponseBody `xml:"Body"`
}

type createItemResponseBody struct {
CreateItemResponse CreateItemResponse `xml:"CreateItemResponse"`
CreateItemResponse ItemOperationResponse `xml:"CreateItemResponse"`
}

type CreateItemResponse struct {
type ItemOperationResponse struct {
ResponseMessages ResponseMessages `xml:"ResponseMessages"`
}

type ResponseMessages struct {
CreateItemResponseMessage Response `xml:"CreateItemResponseMessage"`
UpdateItemResponseMessage Response `xml:"UpdateItemResponseMessage"`
DeleteItemResponseMessage Response `xml:"DeleteItemResponseMessage"`
}

// CreateMessageItem
// https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-email-message
func CreateMessageItem(c Client, m ...Message) error {
func CreateMessageItem(c Client, m ...Message) ([]ItemId, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move ItemId from get_room_lists.go to common.go?


item := &CreateItem{
MessageDisposition: "SendAndSaveCopy",
Expand All @@ -99,24 +102,25 @@ func CreateMessageItem(c Client, m ...Message) error {

xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
return nil, err
}

bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
return nil, err
}

if err := checkCreateItemResponseForErrors(bb); err != nil {
return err
items, err := checkCreateItemResponseForErrors(bb)
if err != nil {
return nil, err
}

return nil
return items.Message, nil
}

// CreateCalendarItem
// https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem-operation-calendar-item
func CreateCalendarItem(c Client, ci ...CalendarItem) error {
func CreateCalendarItem(c Client, ci ...CalendarItem) ([]ItemId, error) {

item := &CreateItem{
SendMeetingInvitations: "SendToAllAndSaveCopy",
Expand All @@ -126,30 +130,32 @@ func CreateCalendarItem(c Client, ci ...CalendarItem) error {

xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
return nil, err
}

bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
return nil, err
}

if err := checkCreateItemResponseForErrors(bb); err != nil {
return err
items, err := checkCreateItemResponseForErrors(bb)
if err != nil {
return nil, err
}

return nil
return items.CalendarItem, nil
}

func checkCreateItemResponseForErrors(bb []byte) error {
func checkCreateItemResponseForErrors(bb []byte) (items ResponseItems, err error) {
var soapResp createItemResponseBodyEnvelop
if err := xml.Unmarshal(bb, &soapResp); err != nil {
return err
if err = xml.Unmarshal(bb, &soapResp); err != nil {
return
}

resp := soapResp.Body.CreateItemResponse.ResponseMessages.CreateItemResponseMessage
if resp.ResponseClass == ResponseClassError {
return errors.New(resp.MessageText)
err = errors.New(resp.MessageText)
return
}
return nil
return resp.ResponseItems, nil
}
74 changes: 74 additions & 0 deletions delete_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ews

import (
"encoding/xml"
"errors"
)

type DeleteStrategy struct {
DeleteType string `xml:"DeleteType,attr,omitempty"`
SendMeetingCancellations string `xml:"SendMeetingCancellations,attr,omitempty"`
}

type DeleteItem struct {
XMLName struct{} `xml:"m:DeleteItem"`
ItemIds ItemIds `xml:"m:ItemIds"`

DeleteStrategy
}

type ItemIds struct {
XMLName struct{} `xml:"m:ItemIds"`

ItemId []ItemId `xml:"t:ItemId"`
}

type deleteItemResponseBodyEnvelop struct {
XMLName struct{} `xml:"Envelope"`
Body deleteItemResponseBody `xml:"Body"`
}

type deleteItemResponseBody struct {
DeleteItemResponse ItemOperationResponse `xml:"DeleteItemResponse"`
}

// DeleteItems
// https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-delete-appointments-and-cancel-meetings-by-using-ews-in-exchange
func DeleteItems(c Client, id []ItemId, strategy ...DeleteStrategy) error {

strategy = append(strategy, DeleteStrategy{
DeleteType: "MoveToDeletedItems",
SendMeetingCancellations: "SendToAllAndSaveCopy",
})

item := DeleteItem{
ItemIds: ItemIds{
ItemId: id,
},
DeleteStrategy: strategy[0],
}

xmlBytes, err := xml.MarshalIndent(item, "", " ")
if err != nil {
return err
}

bb, err := c.SendAndReceive(xmlBytes)
if err != nil {
return err
}
return checkDeleteItemResponseForErrors(bb)
}

func checkDeleteItemResponseForErrors(bb []byte) (err error) {
var soapResp deleteItemResponseBodyEnvelop
if err = xml.Unmarshal(bb, &soapResp); err != nil {
return
}

resp := soapResp.Body.DeleteItemResponse.ResponseMessages.DeleteItemResponseMessage
if resp.ResponseClass == ResponseClassError {
err = errors.New(resp.MessageText)
}
return
}
7 changes: 6 additions & 1 deletion ews.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ func (c *client) SendAndReceive(body []byte) ([]byte, error) {

func applyConfig(config *Config, client *http.Client) {
if config.NTLM {
client.Transport = ntlmssp.Negotiator{}
client.Transport = ntlmssp.Negotiator{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate more on this?

RoundTripper: &http.Transport{
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.SkipTLS},
},
}
}
if config.SkipTLS {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
Expand Down
29 changes: 15 additions & 14 deletions ewsutil/create_event.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package ewsutil

import (
"github.com/mhewedy/ews"
"time"

"github.com/mhewedy/ews"
)

func CreateHTMLEvent(
c ews.Client, to, optional []string, subject, body, location string, from time.Time, duration time.Duration,
) error {
) ([]ews.ItemId, error) {
return createEvent(c, to, optional, subject, body, location, "HTML", from, duration)
}

// CreateEvent helper method to send Message
func CreateEvent(
c ews.Client, to, optional []string, subject, body, location string, from time.Time, duration time.Duration,
) error {
) ([]ews.ItemId, error) {
return createEvent(c, to, optional, subject, body, location, "Text", from, duration)
}

func createEvent(
c ews.Client, to, optional []string, subject, body, location, bodyType string, from time.Time, duration time.Duration,
) error {
) ([]ews.ItemId, error) {

requiredAttendees := make([]ews.Attendee, len(to))
for i, tt := range to {
Expand All @@ -41,16 +42,16 @@ func createEvent(
BodyType: bodyType,
Body: []byte(body),
},
ReminderIsSet: true,
ReminderMinutesBeforeStart: 15,
Start: from,
End: from.Add(duration),
IsAllDayEvent: false,
LegacyFreeBusyStatus: ews.BusyTypeBusy,
Location: location,
RequiredAttendees: []ews.Attendees{{Attendee: requiredAttendees}},
OptionalAttendees: []ews.Attendees{{Attendee: optionalAttendees}},
Resources: []ews.Attendees{{Attendee: room}},
// ReminderIsSet: duration != 0,
Copy link
Owner

@mhewedy mhewedy Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you commenting these? at least you might want to pass as options instead.

// ReminderMinutesBeforeStart: 15,
Start: from,
End: from.Add(duration),
IsAllDayEvent: duration == 0,
LegacyFreeBusyStatus: ews.BusyTypeBusy,
Location: location,
RequiredAttendees: []ews.Attendees{{Attendee: requiredAttendees}},
OptionalAttendees: []ews.Attendees{{Attendee: optionalAttendees}},
Resources: []ews.Attendees{{Attendee: room}},
}

return ews.CreateCalendarItem(c, m)
Expand Down
11 changes: 11 additions & 0 deletions ewsutil/delete_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ewsutil

import (
"github.com/mhewedy/ews"
)

func DeleteEvent(
c ews.Client, id ...ews.ItemId,
) error {
return ews.DeleteItems(c, id)
}
5 changes: 3 additions & 2 deletions ewsutil/send_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ewsutil
import "github.com/mhewedy/ews"

// SendEmail helper method to send Message
func SendEmail(c ews.Client, to []string, subject, body string) error {
func SendEmail(c ews.Client, to []string, subject, body string) (err error) {

m := ews.Message{
ItemClass: "IPM.Note",
Expand All @@ -24,5 +24,6 @@ func SendEmail(c ews.Client, to []string, subject, body string) error {
}
m.ToRecipients.Mailbox = append(m.ToRecipients.Mailbox, mb...)

return ews.CreateMessageItem(c, m)
_, err = ews.CreateMessageItem(c, m)
return
}
58 changes: 58 additions & 0 deletions ewsutil/update_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ewsutil

import (
"time"

"github.com/mhewedy/ews"
)

func UpdateHTMLEvent(
c ews.Client, id ews.ItemId, to, optional []string, subject, body, location string, from time.Time, duration time.Duration,
) ([]ews.ItemId, error) {
return updateEvent(c, id, to, optional, subject, body, location, "HTML", from, duration)
}

// UpdateEvent helper method to update Message
func UpdateEvent(
c ews.Client, id ews.ItemId, to, optional []string, subject, body, location string, from time.Time, duration time.Duration,
) ([]ews.ItemId, error) {
return updateEvent(c, id, to, optional, subject, body, location, "Text", from, duration)
}

func updateEvent(
c ews.Client, id ews.ItemId, to, optional []string, subject, body, location, bodyType string, from time.Time, duration time.Duration,
) ([]ews.ItemId, error) {

requiredAttendees := make([]ews.Attendee, len(to))
for i, tt := range to {
requiredAttendees[i] = ews.Attendee{Mailbox: ews.Mailbox{EmailAddress: tt}}
}

optionalAttendees := make([]ews.Attendee, len(optional))
for i, tt := range optional {
optionalAttendees[i] = ews.Attendee{Mailbox: ews.Mailbox{EmailAddress: tt}}
}

room := make([]ews.Attendee, 1)
room[0] = ews.Attendee{Mailbox: ews.Mailbox{EmailAddress: location}}

m := ews.CalendarItem{
Subject: subject,
Body: ews.Body{
BodyType: bodyType,
Body: []byte(body),
},
// ReminderIsSet: duration != 0,
// ReminderMinutesBeforeStart: 15,
Start: from,
End: from.Add(duration),
IsAllDayEvent: duration == 0,
LegacyFreeBusyStatus: ews.BusyTypeBusy,
Location: location,
RequiredAttendees: []ews.Attendees{{Attendee: requiredAttendees}},
OptionalAttendees: []ews.Attendees{{Attendee: optionalAttendees}},
Resources: []ews.Attendees{{Attendee: room}},
}

return ews.UpdateCalendarItem(c, id, m)
}
Loading