#!/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" $ARGS < "$TEMPLATES/fb-request-person-all.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out0.tmp

# Expect a reply with no periods.

   grep -q 'METHOD:REPLY' out0.tmp \
&& ! grep -q '^FREEBUSY' out0.tmp \
&& echo "Success" \
|| echo "Failed"

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-request-person.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out1.tmp

# Expect a reply with no periods.

   grep -q 'METHOD:REPLY' out1.tmp \
&& ! grep -q '^FREEBUSY' out1.tmp \
&& echo "Success" \
|| echo "Failed"

# 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" $ARGS -u "$SENDER" --echo \
< event_cmd.tmp \
> event_log.tmp

# Obtain the event UID.

  read UID1 < event_uid.tmp

# Register the event at the sender.

  "$OUTGOING_SCRIPT" < event.tmp $ARGS 2>> $ERROR

# Present the event to the recipient.

  "$PERSON_SCRIPT" $ARGS < event.tmp 2>> $ERROR \
| "$SHOWMAIL" \
> out2.tmp

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

   ! grep -q 'METHOD:REPLY' out2.tmp \
&& echo "Success" \
|| echo "Failed"

# Expect no free/busy periods for the recipient.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out2f.tmp

   ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" out2f.tmp \
&& echo "Success" \
|| echo "Failed"

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out2fo.tmp

   grep -q "^20141126T150000Z${TAB}20141126T160000Z" out2fo.tmp \
&& echo "Success" \
|| echo "Failed"

# Accept the invitation.

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

  "$TEXT_CLIENT" $ARGS -u "$USER" -f event.tmp --echo \
< event_accept_cmd.tmp \
> event_accept_log.tmp

# Register the acceptance at the recipient.

  "$OUTGOING_SCRIPT" < event_accept.tmp $ARGS 2>> $ERROR

# Expect free/busy periods at the recipient.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out3f.tmp

   grep -q "^20141126T150000Z${TAB}20141126T160000Z" out3f.tmp \
&& echo "Success" \
|| echo "Failed"

# 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" $ARGS -u "$SENDER" --echo \
< event_conflicting_cmd.tmp \
> event_conflicting_log.tmp

# Obtain the event UID.

  read UID2 < event_conflicting_uid.tmp

# Present the event to the recipient.

  "$PERSON_SCRIPT" $ARGS < event_conflicting.tmp 2>> $ERROR \
| "$SHOWMAIL" \
> out4.tmp

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

   ! grep -q 'METHOD:REPLY' out4.tmp \
&& echo "Success" \
|| echo "Failed"

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out4f.tmp

   ! grep -q "$UID2" out4f.tmp \
&& echo "Success" \
|| echo "Failed"

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out4fo.tmp

   grep -q "$UID2" out4fo.tmp \
&& echo "Success" \
|| echo "Failed"

# Decline the invitation.

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

  "$TEXT_CLIENT" $ARGS -u "$USER" -f event_conflicting.tmp --echo \
< event_decline_cmd.tmp \
> event_decline_log.tmp

# Register the rejection at the recipient.

  "$OUTGOING_SCRIPT" < event_decline.tmp $ARGS 2>> $ERROR

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out5f.tmp

   grep -q "$UID1" out5f.tmp \
&& ! grep -q "$UID2" out5f.tmp \
&& echo "Success" \
|| echo "Failed"

# Cancel the event for a recipient.

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

  "$TEXT_CLIENT" $ARGS -u "$SENDER" --echo -f event.tmp \
< event_cancel_cmd.tmp \
> event_cancel_log.tmp

# Present the cancellation to the recipient.

  "$PERSON_SCRIPT" $ARGS < event_cancel.tmp 2>> $ERROR \
| "$SHOWMAIL" \
> out6.tmp

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

   ! grep -q 'METHOD:REPLY' out6.tmp \
&& echo "Success" \
|| echo "Failed"

# Expect no free/busy periods for the recipient.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out6f.tmp

   ! grep -q "$UID1" out6f.tmp \
&& echo "Success" \
|| echo "Failed"

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out6fo.tmp

   grep -q "$UID1" out6fo.tmp \
&& echo "Success" \
|| echo "Failed"

# Cancel the event for all recipients.

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

  "$TEXT_CLIENT" $ARGS -u "$SENDER" --echo -f event.tmp \
< event_cancel_all_cmd.tmp \
> event_cancel_all_log.tmp

# Present the cancellation to the recipient.

  "$PERSON_SCRIPT" $ARGS < "event_cancel_all.tmp" 2>> $ERROR \
| "$SHOWMAIL" \
> out7.tmp

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

   ! grep -q 'METHOD:REPLY' out7.tmp \
&& echo "Success" \
|| echo "Failed"

# Expect no free/busy periods for the recipient.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out7f.tmp

   ! grep -q "$UID1" "out7f.tmp" \
&& echo "Success" \
|| echo "Failed"

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out7fo.tmp

   ! grep -q "$UID1" "out7fo.tmp" \
&& echo "Success" \
|| echo "Failed"

# 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" $ARGS -u "$SENDER" --echo \
< event_spoofed_cmd.tmp \
> event_spoofed_log.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" $ARGS < "event_spoofed_ready.tmp" 2>> $ERROR \
| "$SHOWMAIL" \
> out8.tmp

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

   ! grep -q 'METHOD:REPLY' out8.tmp \
&& echo "Success" \
|| echo "Failed"

# Expect no free/busy periods for the recipient.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out8f.tmp

   ! grep -q "$UID3" "out8f.tmp" \
&& echo "Success" \
|| echo "Failed"

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out8fo.tmp

   ! grep -q "$UID3" "out8fo.tmp" \
&& echo "Success" \
|| echo "Failed"
