44e1130aee128dcece6d64f7aec7659453e9738c
[jelmer/openchange.git] / torture / mapi_sendmail_html.c
1 /*
2    OpenChange MAPI torture suite implementation.
3
4    Send attachments to an Exchange server
5
6    Copyright (C) Julien Kerihuel 2007.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <libmapi/libmapi.h>
23 #include <gen_ndr/ndr_exchange.h>
24 #include <param.h>
25 #include <credentials.h>
26 #include <torture/mapi_torture.h>
27 #include <torture.h>
28 #include <torture/torture_proto.h>
29 #include <samba/popt.h>
30
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 bool torture_rpc_mapi_sendmail_html(struct torture_context *torture)
35 {
36         enum MAPISTATUS         retval;
37         TALLOC_CTX              *mem_ctx;
38         bool                    ret = true;
39         const char              *subject = lp_parm_string(torture->lp_ctx, NULL, "mapi", "subject");
40         const char              *body = lp_parm_string(torture->lp_ctx, NULL, "mapi", "body");
41         const char              *filename = lp_parm_string(torture->lp_ctx, NULL, "mapi", "html");
42         const char              **usernames;
43         const char              **usernames_to;
44         const char              **usernames_cc;
45         const char              **usernames_bcc;
46         struct mapi_session     *session;
47         mapi_object_t           obj_store;
48         mapi_object_t           obj_outbox;
49         mapi_object_t           obj_message;
50         uint64_t                id_outbox;
51         struct SRowSet          *SRowSet = NULL;
52         struct SPropTagArray    *flaglist = NULL;
53         uint32_t                index = 0;
54         struct SPropTagArray    *SPropTagArray;
55         struct SBinary_short    html;
56         struct SPropValue       SPropValue;
57         struct SPropValue       props[3];
58         uint32_t                msgflag;
59
60         /* init torture */
61         mem_ctx = talloc_init("torture_rpc_mapi_sendmail");
62
63         /* get the attachment filename */
64         if (!filename) {
65                 DEBUG(0, ("No filename specified with mapi:html\n"));
66                 return false;
67         }
68
69         /* init mapi */
70         if ((session = torture_init_mapi(mem_ctx, torture->lp_ctx)) == NULL) return false;
71
72         /* init objects */
73         mapi_object_init(&obj_store);
74         mapi_object_init(&obj_outbox);
75         mapi_object_init(&obj_message);
76
77         /* default if null */
78         if (subject == 0) subject = "";
79         if (body == 0) body = "";
80
81         /* session::OpenMsgStore() */
82         retval = OpenMsgStore(session, &obj_store);
83         mapi_errstr("OpenMsgStore", GetLastError());
84         if (retval != MAPI_E_SUCCESS) return false;
85
86         /* id_outbox = store->GeOutboxFolder() */
87         retval = GetOutboxFolder(&obj_store, &id_outbox);
88         mapi_errstr("GetOutboxFodler", GetLastError());
89         if (retval != MAPI_E_SUCCESS) return false;
90
91         /* outbox = store->OpenFolder(id_outbox) */
92         retval = OpenFolder(&obj_store, id_outbox, &obj_outbox);
93         if (retval != MAPI_E_SUCCESS) return false;
94
95         /* message = outbox->CreateMessage() */
96         retval = CreateMessage(&obj_outbox, &obj_message);
97         mapi_errstr("CreateMessage", GetLastError());
98         if (retval != MAPI_E_SUCCESS) return false;
99
100         SPropTagArray = set_SPropTagArray(mem_ctx, 0x6,
101                                           PR_OBJECT_TYPE,
102                                           PR_DISPLAY_TYPE,
103                                           PR_7BIT_DISPLAY_NAME_UNICODE,
104                                           PR_DISPLAY_NAME_UNICODE,
105                                           PR_SMTP_ADDRESS_UNICODE,
106                                           PR_GIVEN_NAME_UNICODE);
107
108         usernames_to = get_cmdline_recipients(mem_ctx, "to");
109         usernames_cc = get_cmdline_recipients(mem_ctx, "cc");
110         usernames_bcc = get_cmdline_recipients(mem_ctx, "bcc");
111         usernames = collapse_recipients(mem_ctx, usernames_to, usernames_cc, usernames_bcc);
112
113         retval = ResolveNames(mapi_object_get_session(&obj_store), (const char **)usernames, 
114                               SPropTagArray, &SRowSet, &flaglist, MAPI_UNICODE);
115         mapi_errstr("ResolveNames", GetLastError());
116         if (retval != MAPI_E_SUCCESS) return false;
117
118         if (!SRowSet) {
119                 SRowSet = talloc_zero(mem_ctx, struct SRowSet);
120         }
121
122         set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames_to, flaglist, MAPI_TO);
123         set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames_cc, flaglist, MAPI_CC);
124         set_usernames_RecipientType(mem_ctx, &index, SRowSet, usernames_bcc, flaglist, MAPI_BCC);
125
126
127         SPropValue.ulPropTag = PR_SEND_INTERNET_ENCODING;
128         SPropValue.value.l = 0;
129         SRowSet_propcpy(mem_ctx, &SRowSet[0], SPropValue);
130
131         /* message->ModifyRecipients() */
132         retval = ModifyRecipients(&obj_message, SRowSet);
133         mapi_errstr("ModifyRecipients", GetLastError());
134         if (retval != MAPI_E_SUCCESS) return false;
135         
136         retval = MAPIFreeBuffer(SRowSet);
137         mapi_errstr("MAPIFreeBuffer: SRowSet", GetLastError());
138
139         retval = MAPIFreeBuffer(flaglist);
140         mapi_errstr("MAPIFreeBuffer: flaglist", GetLastError());
141
142         /* message->SetProps()
143          */
144
145         {
146                 struct stat     sb;
147                 int             fd;
148
149                 if (stat(filename, &sb) != 0) return false;
150                 if ((fd = open(filename, O_RDONLY)) == -1) {
151                         DEBUG(0, ("Error while opening %s\n", filename));
152                         return false;
153                 }
154                 html.lpb = talloc_size(mem_ctx, sb.st_size);
155                 html.cb = read(fd, html.lpb, sb.st_size);
156                 close(fd);
157         }
158
159         msgflag = MSGFLAG_UNSENT;
160         set_SPropValue_proptag(&props[0], PR_SUBJECT, (const void *)subject);
161         set_SPropValue_proptag(&props[1], PR_HTML, (const void *)&html);
162         set_SPropValue_proptag(&props[2], PR_MESSAGE_FLAGS, (const void *)&msgflag);
163         retval = SetProps(&obj_message, props, 3);
164         mapi_errstr("SetProps", GetLastError());
165
166         /* message->SubmitMessage() */
167         retval = SubmitMessage(&obj_message);
168         mapi_errstr("SubmitMessage", GetLastError());
169         if (retval != MAPI_E_SUCCESS) return false;
170
171         /* objects->Release()
172          */
173         mapi_object_release(&obj_message);
174         mapi_object_release(&obj_outbox);
175         mapi_object_release(&obj_store);
176         
177         /* session::Uninitialize()
178          */
179         MAPIUninitialize();
180
181         talloc_free(mem_ctx);
182         return ret;
183 }