Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / groupwise / camel-groupwise-transport.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-groupwise-transport.c : class for an groupwise transport */
3
4 /* 
5  * Authors: Sivaiah Nallagatla <snallagatla@novell.com>
6  *          Parthasarathi Susarla <sparthasarathi@novell.com>
7  *
8  * Copyright (C) 2004 Novell, Inc.
9  *
10  * This program is free software; you can redistribute it and/or 
11  * modify it under the terms of version 2 of the GNU Lesser General Public 
12  * License as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22  * USA
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <string.h>
30
31 #include <glib.h>
32 #include <glib/gi18n-lib.h>
33
34 #include <libsoup/soup-misc.h>
35
36 #include "camel-data-wrapper.h"
37 #include "camel-medium.h"
38 #include "camel-mime-message.h"
39 #include "camel-mime-utils.h"
40 #include "camel-multipart.h"
41 #include "camel-session.h" 
42 #include "camel-stream-mem.h"
43 #include "camel-stream.h"
44
45 #include "camel-groupwise-store.h"
46 #include "camel-groupwise-transport.h"
47 #include "camel-groupwise-utils.h"
48
49 static gboolean groupwise_send_to (CamelTransport *transport,
50                                   CamelMimeMessage *message,
51                                   CamelAddress *from,
52                                   CamelAddress *recipients,
53                                   CamelException *ex);
54
55 static gboolean groupwise_transport_connect (CamelService *service, CamelException *ex);
56 static char *groupwise_transport_get_name (CamelService *service, gboolean brief);
57 static void groupwise_transport_construct (CamelService *service, CamelSession *session,
58                                            CamelProvider *provider, CamelURL *url, CamelException *ex);
59
60
61 static CamelTransportClass *parent_class = NULL;
62
63
64
65 static void
66 camel_groupwise_transport_class_init (CamelGroupwiseTransportClass *camel_groupwise_transport_class)
67 {
68         CamelTransportClass *camel_transport_class =
69                 CAMEL_TRANSPORT_CLASS (camel_groupwise_transport_class);
70
71         CamelServiceClass *camel_service_class =
72                 CAMEL_SERVICE_CLASS (camel_groupwise_transport_class);
73         
74         parent_class = CAMEL_TRANSPORT_CLASS (camel_type_get_global_classfuncs (camel_transport_get_type ()));
75         
76         camel_service_class->connect = groupwise_transport_connect;
77         camel_service_class->get_name = groupwise_transport_get_name;
78         camel_service_class->construct = groupwise_transport_construct;
79         
80         /* virtual method overload */
81         camel_transport_class->send_to = groupwise_send_to;
82 }
83
84 static void
85 camel_groupwise_transport_init (CamelTransport *transport)
86 {
87         return;
88 }
89
90 static void
91 groupwise_transport_construct (CamelService *service, CamelSession *session,
92                 CamelProvider *provider, CamelURL *url,
93                 CamelException *ex)
94 {
95         CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
96         if (camel_exception_is_set (ex))
97                 return;
98 }
99
100 CamelType
101 camel_groupwise_transport_get_type (void)
102 {
103         static CamelType camel_groupwise_transport_type = CAMEL_INVALID_TYPE;
104
105         if (camel_groupwise_transport_type == CAMEL_INVALID_TYPE) {
106                 camel_groupwise_transport_type =
107                         camel_type_register (CAMEL_TRANSPORT_TYPE,
108                                              "CamelGroupwiseTransport",
109                                              sizeof (CamelGroupwiseTransport),
110                                              sizeof (CamelGroupwiseTransportClass),
111                                              (CamelObjectClassInitFunc) camel_groupwise_transport_class_init,
112                                              NULL,
113                                              (CamelObjectInitFunc) camel_groupwise_transport_init,
114                                              NULL);
115         }
116
117         return camel_groupwise_transport_type;
118 }
119
120 static char *groupwise_transport_get_name (CamelService *service, gboolean brief)
121 {
122         if (brief)
123                 return g_strdup_printf (_("GroupWise server %s"), service->url->host);
124         else {
125                 return g_strdup_printf (_("GroupWise mail delivery via %s"),
126                                 service->url->host);
127         }
128 }
129
130
131 static gboolean
132 groupwise_transport_connect (CamelService *service, CamelException *ex)
133 {
134         return TRUE;
135
136 }
137
138
139 static gboolean
140 groupwise_send_to (CamelTransport *transport, 
141                    CamelMimeMessage *message,
142                    CamelAddress *from, 
143                    CamelAddress *recipients,
144                    CamelException *ex)
145 {
146         CamelService *service;
147         CamelStore *store =  NULL;
148         CamelGroupwiseStore *groupwise_store = NULL;
149         CamelGroupwiseStorePrivate *priv = NULL;
150         EGwItem *item ,*temp_item=NULL;
151         EGwConnection *cnc = NULL;
152         EGwConnectionStatus status = 0;
153         GSList *sent_item_list = NULL;
154         char *url = NULL;
155         const char *reply_request = NULL;
156         EGwItemLinkInfo *info = NULL;
157
158         if (!transport) {
159                 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE, _("Authentication failed"));
160                 return FALSE;
161         }
162
163         service = CAMEL_SERVICE(transport);
164         url = camel_url_to_string (service->url,
165                                    (CAMEL_URL_HIDE_PASSWORD |
166                                     CAMEL_URL_HIDE_PARAMS   |
167                                     CAMEL_URL_HIDE_AUTH) );
168
169         camel_operation_start (NULL, _("Sending Message") );
170
171         /*camel groupwise store and cnc*/
172         store = camel_session_get_store (service->session, url, ex );
173         g_free (url);
174         if (!store) {
175                 g_warning ("ERROR: Could not get a pointer to the store");
176                 camel_exception_set (ex, CAMEL_EXCEPTION_STORE_INVALID, _("Cannot get folder: Invalid operation on this store"));
177                 return FALSE;
178         }
179         groupwise_store = CAMEL_GROUPWISE_STORE (store);
180         priv = groupwise_store->priv;
181
182         cnc = cnc_lookup (priv);
183         if (!cnc) {
184                 g_warning ("||| Eh!!! Failure |||\n");
185                 camel_operation_end (NULL);
186                 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE, _("Authentication failed"));
187                 return FALSE;
188         }
189         
190         item = camel_groupwise_util_item_from_message (cnc, message, from);
191         
192         reply_request = (char *)camel_medium_get_header (CAMEL_MEDIUM (message), "In-Reply-To");
193         if (reply_request) {
194                 char *id;
195                 int len = strlen (reply_request);
196
197                 id = (char *)g_malloc0 (len-1);
198                 id = memcpy(id, reply_request+2, len-3);
199                 status = e_gw_connection_reply_item (cnc, id, NULL, &temp_item);
200                 if (status != E_GW_CONNECTION_STATUS_OK) 
201                         g_warning ("Could not send a replyRequest...continuing without!!\n");
202                 else {
203                         info = e_gw_item_get_link_info (temp_item);
204                         e_gw_item_set_link_info (item, info);
205                 }
206
207                 g_free (id);
208         }
209         
210         
211         /*Send item*/
212         status = e_gw_connection_send_item (cnc, item, &sent_item_list);
213         if (status != E_GW_CONNECTION_STATUS_OK) {
214                 g_warning (" Error Sending mail");
215                 camel_operation_end (NULL);
216                 e_gw_item_set_link_info (item, NULL);
217                 g_object_unref (item);
218                 if (temp_item)
219                         g_object_unref (temp_item);
220
221                 /* FIXME: 58652 should be changed with an enum.*/
222                 if (status == 58652)
223                         camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE, _("You have exceeded this account's storage limit. Your messages are queued in your Outbox. Resend by pressing Send/Receive after deleting/archiving some of your mail.\n"));
224                 else
225                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,_("Could not send message: %s"),_("Unknown error"));
226                 status = 0;
227                 return FALSE;
228         }
229         e_gw_item_set_link_info (item, NULL);
230
231         e_gw_item_set_recipient_list (item, NULL);
232
233         if (temp_item)
234                 g_object_unref (temp_item);
235         g_object_unref (item);
236
237         camel_operation_end (NULL);
238
239         return TRUE;
240 }
241