1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Copyright (C) 2002-2004 Novell, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 /* exchange-oof: Out of Office code */
26 #include "exchange-oof.h"
27 #include "exchange-account.h"
28 #include "e2k-propnames.h"
29 #include "e2k-utils.h"
34 /* Taken from gal/util/e-util.c */
36 find_str_case (const char *haystack, const char *needle)
38 /* find the needle in the haystack neglecting case */
42 g_return_val_if_fail (haystack != NULL, NULL);
43 g_return_val_if_fail (needle != NULL, NULL);
46 if (len > strlen(haystack))
50 return (char *) haystack;
52 for (ptr = haystack; *(ptr + len - 1) != '\0'; ptr++)
53 if (!g_ascii_strncasecmp (ptr, needle, len))
61 * @account: an #ExchangeAccount
62 * @oof: pointer to variable to pass back OOF state in
63 * @message: pointer to variable to pass back OOF message in
65 * Checks if Out-of-Office is enabled for @account and returns the
66 * state in *@oof and the message in *@message (which the caller
69 * Return value: %TRUE if the OOF state was read, %FALSE if an error
73 exchange_oof_get (ExchangeAccount *account, gboolean *oof, char **message)
77 char *url, *body = NULL, *p = NULL, *checked, *ta_start, *ta_end;
80 ctx = exchange_account_get_context (account);
85 /* Do this the easy way */
86 const char *prop = E2K_PR_EXCHANGE_OOF_STATE;
90 url = e2k_uri_concat (account->home_uri, "NON_IPM_SUBTREE/");
91 status = e2k_context_propfind (ctx, NULL, url, &prop, 1,
94 if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (status) || nresults == 0)
97 prop = e2k_properties_get_prop (results[0].props, E2K_PR_EXCHANGE_OOF_STATE);
98 *oof = prop && atoi (prop);
100 e2k_results_free (results, nresults);
104 url = e2k_uri_concat (account->home_uri, "?Cmd=options");
105 status = e2k_context_get_owa (ctx, NULL, url, FALSE, &body, &len);
107 if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (status))
110 p = find_str_case (body, "<!--End OOF Assist-->");
114 body[len - 1] = '\0';
116 p = find_str_case (body, "name=\"OofState\"");
118 p = find_str_case (body, "value=\"1\"");
120 g_warning ("Could not find OofState in options page");
125 checked = find_str_case (p, "checked");
126 *oof = (checked && checked < strchr (p, '>'));
129 ta_end = find_str_case (p, "</textarea>");
131 g_warning ("Could not find OOF text in options page");
133 *message = g_strdup ("");
136 for (ta_start = ta_end - 1; ta_start > p; ta_start--) {
137 if (*ta_start == '>')
140 if (*ta_start++ != '>') {
141 g_warning ("Could not find OOF text in options page");
143 *message = g_strdup ("");
147 *message = g_strndup (ta_start, ta_end - ta_start);
148 /* FIXME: HTML decode */
157 * @account: an #ExchangeAccount
158 * @oof: new OOF state
159 * @message: new OOF message, or %NULL
161 * Sets the OOF state for @account to @oof.
163 * Return value: %TRUE if the OOF state was updated, %FALSE if an
167 exchange_oof_set (ExchangeAccount *account, gboolean oof, const char *message)
170 E2kHTTPStatus status;
172 ctx = exchange_account_get_context (account);
177 char *body, *message_enc;
179 message_enc = e2k_uri_encode (message, FALSE, NULL);
180 body = g_strdup_printf ("Cmd=options&OofState=%d&"
182 oof ? 1 : 0, message_enc);
183 status = e2k_context_post (ctx, NULL, account->home_uri,
184 "application/x-www-form-urlencoded",
185 body, strlen (body), NULL, NULL);
186 g_free (message_enc);
189 E2kProperties *props;
192 props = e2k_properties_new ();
193 e2k_properties_set_bool (props, E2K_PR_EXCHANGE_OOF_STATE, oof);
194 url = e2k_uri_concat (account->home_uri, "NON_IPM_SUBTREE/");
195 /* Need to pass TRUE for "create" here or it won't work */
196 status = e2k_context_proppatch (ctx, NULL, url, props,
199 e2k_properties_free (props);
202 return E2K_HTTP_STATUS_IS_SUCCESSFUL (status) ||
203 E2K_HTTP_STATUS_IS_REDIRECTION (status);