#!/bin/sh

. "`dirname \"$0\"`/common.sh"

USER="mailto:vincent.vole@example.com"
SENDER="mailto:paul.boddie@example.com"

mkdir -p "$PREFS/$USER"
echo 'Europe/Oslo' > "$PREFS/$USER/TZID"
echo 'share' > "$PREFS/$USER/freebusy_sharing"

# Present a free/busy request for all periods to the recipient.

person_script "$TEMPLATES/fb-request-person-all.txt" "out0.tmp"

# Expect a reply with no periods.

   is_present 'METHOD:REPLY' "out0.tmp" \
&& is_absent '^FREEBUSY' "out0.tmp" \
&& success || failed "out0.tmp"

# Present a free/busy request for periods of interest to the recipient.

person_script "$TEMPLATES/fb-request-person.txt" "out1.tmp"

# Expect a reply with no periods.

   is_present 'METHOD:REPLY' "out1.tmp" \
&& is_absent '^FREEBUSY' "out1.tmp" \
&& success || failed "out1.tmp"

# Make a new event.

cat > "event_cmd.tmp" <<EOF
new
period 0 edit 20141126 16 00 00 Europe/Oslo 20141126 17 00 00 Europe/Oslo
attendance accept
attendee mailto:vincent.vole@example.com
summary Meeting at 4pm
finish
update event.tmp
publish event_self.tmp
UID event_uid.tmp
EOF

text_client "$SENDER" "event_cmd.tmp"

# Obtain the event UID.

read UID1 < "event_uid.tmp"

# Register the event at the sender.

outgoing_script "event_self.tmp"

# Present the event to the recipient.

person_script "event.tmp" "out2.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out2.tmp" && success || failed "out2.tmp"

# Expect no free/busy periods for the recipient.

list_script "out2f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out2f.tmp"

# Expect free/busy periods for the sender at the recipient.

list_script "out2fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy '20141126T150000Z' '20141126T160000Z' "out2fo.tmp"

# Accept the invitation.

cat > "event_accept_cmd.tmp" <<EOF
attendance accept
finish
update event_accept.tmp
EOF

text_client_specific "$USER" "event_accept_cmd.tmp" "$UID1"

# Register the acceptance at the recipient.

outgoing_script "event_accept.tmp"

# Expect free/busy periods at the recipient.

list_script "out3f.tmp" "$USER" "freebusy"
test_freebusy '20141126T150000Z' '20141126T160000Z' "out3f.tmp"

# Make another event at the same time.

sed \
's/summary Meeting at 4pm/summary A conflicting meeting at 4pm/;'\
's/update event.tmp/update event_conflicting.tmp/;'\
's/UID event_uid.tmp/UID event_conflicting_uid.tmp/' "event_cmd.tmp" \
> "event_conflicting_cmd.tmp"

text_client "$SENDER" "event_conflicting_cmd.tmp"

# Obtain the event UID.

read UID2 < "event_conflicting_uid.tmp"

# Present the event to the recipient.

person_script "event_conflicting.tmp" "out4.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out4.tmp" && success || failed "out4.tmp"

# Expect no free/busy periods for the recipient for this event.

list_script "out4f.tmp" "$USER" "freebusy"
is_absent "$UID2" "out4f.tmp" && success || failed "out4f.tmp"

# Expect free/busy periods for the sender at the recipient for this event.

list_script "out4fo.tmp" "$USER" "freebusy_other" "$SENDER"
is_present "$UID2" "out4fo.tmp" && success || failed "out4fo.tmp"

# Decline the invitation.

cat > "event_decline_cmd.tmp" <<EOF
attendance decline
finish
update event_decline.tmp
EOF

text_client_specific "$USER" "event_decline_cmd.tmp" "$UID2"

# Register the rejection at the recipient.

outgoing_script "event_decline.tmp"

# Expect free/busy periods only for the original event, not the last one.

   list_script "out5f.tmp" "$USER" "freebusy"
   is_present "$UID1" "out5f.tmp" \
&& is_absent "$UID2" "out5f.tmp" \
&& success || failed "out5f.tmp"

# Cancel the event for a recipient.

cat > "event_cancel_cmd.tmp" <<EOF
attendee mailto:vincent.vole@example.com remove
finish
cancel event_cancel.tmp
EOF

text_client_specific "$SENDER" "event_cancel_cmd.tmp" "$UID1" 

# Present the cancellation to the recipient.

person_script "event_cancel.tmp" "out6.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out6.tmp" && success || failed "out6.tmp"

# Expect no free/busy periods for the recipient.

list_script "out6f.tmp" "$USER" "freebusy"
is_absent "$UID1" "out6f.tmp" && success || failed "out6f.tmp"

# Expect free/busy periods for the sender at the recipient for this event since
# only the recipient was uninvited.

list_script "out6fo.tmp" "$USER" "freebusy_other" "$SENDER"
is_present "$UID1" "out6fo.tmp" && success || failed "out6fo.tmp"

# Cancel the event for all recipients.

cat > "event_cancel_all_cmd.tmp" <<EOF
finish
cancel event_cancel_all.tmp
EOF

text_client_specific "$SENDER" "event_cancel_all_cmd.tmp" "$UID1"

# Present the cancellation to the recipient.

person_script "event_cancel_all.tmp" "out7.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out7.tmp" && success || failed "out7.tmp"

# Expect no free/busy periods for the recipient.

list_script "out7f.tmp" "$USER" "freebusy"
is_absent "$UID1" "out7f.tmp" && success || failed "out7f.tmp"

# Expect no free/busy periods for the sender at the recipient for this event.

list_script "out7fo.tmp" "$USER" "freebusy_other" "$SENDER"
is_absent "$UID1" "out7fo.tmp" && success || failed "out7fo.tmp"

# Create a message from another sender that is largely a copy of the original
# event trying to organise the event using someone else's identity.

sed \
's/summary Meeting at 4pm/summary A spoofed meeting at 4pm/;'\
's/update event.tmp/update event_spoofed.tmp/;'\
's/UID event_uid.tmp/UID event_spoofed_uid.tmp/' "event_cmd.tmp" \
> "event_spoofed_cmd.tmp"

text_client "$SENDER" "event_spoofed_cmd.tmp"

# Obtain the event UID.

read UID3 < "event_spoofed_uid.tmp"

sed 's/From: paul.boddie/From: harvey.horse/' "event_spoofed.tmp" \
> "event_spoofed_ready.tmp"

# Present the event to the recipient.

person_script "event_spoofed_ready.tmp" "out8.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out8.tmp" && success || failed "out8.tmp"

# Expect no free/busy periods for the recipient.

list_script "out8f.tmp" "$USER" "freebusy"
is_absent "$UID3" "out8f.tmp" && success || failed "out8f.tmp"

# Expect no free/busy periods for the sender at the recipient.

list_script "out8fo.tmp" "$USER" "freebusy_other" "$SENDER"
is_absent "$UID3" "out8fo.tmp" && success || failed "out8fo.tmp"
