b35583dd168f1bc99f3ce4fb21428c945ccf8fa5
[jelmer/openchange.git] / torture / mapi_sendappointment.c
1 /*
2    OpenChange MAPI torture suite implementation.
3
4    Send appointments 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 <time.h>
32
33 #define DATE_FORMAT "%Y-%m-%d %H:%M:%S"
34
35 #define CN_PROPS 14
36
37 bool torture_rpc_mapi_sendappointment(struct torture_context *torture)
38 {
39         NTSTATUS                nt_status;
40         enum MAPISTATUS         retval;
41         struct dcerpc_pipe      *p;
42         TALLOC_CTX              *mem_ctx;
43         bool                    ret = true;
44         const char              *appointment = lp_parm_string(torture->lp_ctx, NULL, "mapi", "appointment");
45         const char              *body = lp_parm_string(torture->lp_ctx, NULL, "mapi", "body");
46         const char              *location = lp_parm_string(torture->lp_ctx, NULL, "mapi", "location");
47         const char              *start = lp_parm_string(torture->lp_ctx, NULL, "mapi", "start");
48         const char              *end = lp_parm_string(torture->lp_ctx, NULL, "mapi", "end");
49         uint32_t                busy_status = lp_parm_int(torture->lp_ctx, NULL, "mapi", "busystatus", 0);
50         uint32_t                label = lp_parm_int(torture->lp_ctx, NULL, "mapi", "label", 0);
51         struct mapi_session     *session;
52         uint64_t                id_calendar;
53         mapi_object_t           obj_store;
54         mapi_object_t           obj_calendar;
55         mapi_object_t           obj_message;
56         struct SPropValue       props[CN_PROPS];
57         struct mapi_nameid      *nameid;
58         struct SPropTagArray    *SPropTagArray;
59         NTTIME                  nt;
60         struct tm               tm;
61         struct FILETIME         *start_date;
62         struct FILETIME         *end_date;
63         uint32_t                flag;
64         uint8_t                 flag2;
65
66         if (!appointment) return false;
67         if (busy_status > 3) return false;
68         if (!start || !end) return false;
69
70         /* init torture */
71         mem_ctx = talloc_init("torture_rpc_mapi_sendappointment");
72         nt_status = torture_rpc_connection(torture, &p, &ndr_table_exchange_emsmdb);
73         if (!NT_STATUS_IS_OK(nt_status)) {
74                 talloc_free(mem_ctx);
75                 return false;
76         }
77
78         /* init mapi */
79         if ((session = torture_init_mapi(mem_ctx, torture->lp_ctx)) == NULL) return false;
80
81         /* init objects */
82         mapi_object_init(&obj_store);
83         mapi_object_init(&obj_calendar);
84
85         /* session::OpenMsgStore */
86         retval = OpenMsgStore(session, &obj_store);
87         mapi_errstr("OpenMsgStore", GetLastError());
88         if (retval != MAPI_E_SUCCESS) return false;
89
90         retval = GetDefaultFolder(&obj_store, &id_calendar, olFolderCalendar);
91         mapi_errstr("GetDefaultFolder", GetLastError());
92         if (retval != MAPI_E_SUCCESS) return false;
93
94         /* We now open the calendar folder */
95         retval = OpenFolder(&obj_store, id_calendar, &obj_calendar);
96         mapi_errstr("OpenFolder", GetLastError());
97         if (retval != MAPI_E_SUCCESS) return false;
98
99         /* Operations on the calendar folder */
100         retval = CreateMessage(&obj_calendar, &obj_message);
101         mapi_errstr("CreateMessage", GetLastError());
102         if (retval != MAPI_E_SUCCESS) return false;
103
104         /* Build the list of named properties we want to set */
105         nameid = mapi_nameid_new(mem_ctx);
106         mapi_nameid_OOM_add(nameid, "Location", PSETID_Appointment);
107         mapi_nameid_OOM_add(nameid, "BusyStatus", PSETID_Appointment);
108         mapi_nameid_OOM_add(nameid, "ApptStateFlags", PSETID_Appointment);
109         mapi_nameid_OOM_add(nameid, "CommonStart", PSETID_Common);
110         mapi_nameid_OOM_add(nameid, "CommonEnd", PSETID_Common);
111         mapi_nameid_OOM_add(nameid, "Label", PSETID_Appointment);
112         mapi_nameid_OOM_add(nameid, "ReminderDelta", PSETID_Common);
113
114         /* GetIDsFromNames and map property types */
115         SPropTagArray = talloc_zero(mem_ctx, struct SPropTagArray);
116         retval = GetIDsFromNames(&obj_calendar, nameid->count,
117                                  nameid->nameid, 0, &SPropTagArray);
118         if (retval != MAPI_E_SUCCESS) return false;
119         mapi_nameid_SPropTagArray(nameid, SPropTagArray);
120         MAPIFreeBuffer(nameid);
121
122         if (!strptime(start, DATE_FORMAT, &tm)) {
123                 printf("Invalid date format: yyyy-mm-dd hh:mm:ss (e.g.: 2007-09-17 10:00:00)\n");
124                 return false;
125         }
126         unix_to_nt_time(&nt, mktime(&tm));
127         start_date = talloc(mem_ctx, struct FILETIME);
128         start_date->dwLowDateTime = (nt << 32) >> 32;
129         start_date->dwHighDateTime = (nt >> 32);
130
131         if (!strptime(end, DATE_FORMAT, &tm)) {
132                 printf("Invalid date format: yyyy-mm-dd hh:mm:ss (e.g.:2007-09-17 18:30:00)\n");
133                 return false;
134         }
135         unix_to_nt_time(&nt, mktime(&tm));
136         end_date = talloc(mem_ctx, struct FILETIME);
137         end_date->dwLowDateTime = (nt << 32) >> 32;
138         end_date->dwHighDateTime = (nt >> 32);
139
140         set_SPropValue_proptag(&props[0], PR_CONVERSATION_TOPIC, 
141                                                    (const void *) appointment);
142         set_SPropValue_proptag(&props[1], PR_NORMALIZED_SUBJECT, 
143                                                    (const void *) appointment);
144         set_SPropValue_proptag(&props[2], PR_START_DATE, (const void *) start_date);
145         set_SPropValue_proptag(&props[3], PR_END_DATE, (const void *) end_date);
146         set_SPropValue_proptag(&props[4], PR_MESSAGE_CLASS, (const void *)"IPM.Appointment");
147         flag = 1;
148         set_SPropValue_proptag(&props[5], PR_MESSAGE_FLAGS, (const void *) &flag);
149         set_SPropValue_proptag(&props[6], SPropTagArray->aulPropTag[0], (const void *)(location?location:""));
150         set_SPropValue_proptag(&props[7], SPropTagArray->aulPropTag[1], (const void *) &busy_status);
151         flag= MEETING_STATUS_NONMEETING;
152         set_SPropValue_proptag(&props[8], SPropTagArray->aulPropTag[2], (const void *) &flag);
153         flag2 = true;
154         set_SPropValue_proptag(&props[9], SPropTagArray->aulPropTag[3], (const void *) start_date);
155         set_SPropValue_proptag(&props[10], SPropTagArray->aulPropTag[4], (const void *) end_date);
156         set_SPropValue_proptag(&props[11], SPropTagArray->aulPropTag[5], (const void *)&label);
157         flag = 30;
158         set_SPropValue_proptag(&props[12], SPropTagArray->aulPropTag[6], (const void *)&flag);
159         set_SPropValue_proptag(&props[13], PR_BODY, (const void *)(body?body:""));
160         retval = SetProps(&obj_message, props, CN_PROPS);
161         mapi_errstr("SetProps", GetLastError());
162         MAPIFreeBuffer(SPropTagArray);
163         if (retval != MAPI_E_SUCCESS) return false;
164
165
166         retval = SaveChangesMessage(&obj_calendar, &obj_message, KeepOpenReadOnly);
167         mapi_errstr("SaveChangesMessage", GetLastError());
168         if (retval != MAPI_E_SUCCESS) return false;
169
170
171         mapi_object_release(&obj_calendar);
172         mapi_object_release(&obj_store);
173
174         /* uninitialize mapi
175          */
176         MAPIUninitialize();
177         talloc_free(mem_ctx);
178         
179         return (ret);
180 }