549458d4e6368fe10f67bff7d8fa3d851ae1a7ae
[tridge/openchange.git] / branches / current-samba4 / torture / mapi_deletemail.c
1 /*
2    OpenChange MAPI torture suite implementation.
3
4    Delete mail from an Exchange server
5
6    Copyright (C) Fabien Le Mentec 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 <libmapi/defs_private.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
32 #define CN_ROWS 0x100
33
34
35 bool torture_rpc_mapi_deletemail(struct torture_context *torture)
36 {
37         enum MAPISTATUS         retval;
38         TALLOC_CTX              *mem_ctx;
39         bool                    ret = true;
40         const char              *s_subject = lp_parm_string(torture->lp_ctx, NULL, "mapi", "subject");
41         int                     len_subject;
42         struct mapi_session     *session;
43         mapi_object_t           obj_store;
44         mapi_object_t           obj_inbox;
45         mapi_object_t           obj_table;
46         mapi_id_t               id_inbox;
47         mapi_id_t               *id_messages;
48         unsigned long           cn_messages;
49         struct SRowSet          rowset;
50         unsigned long           i_row;
51         unsigned long           cn_rows;
52         struct SPropTagArray    *SPropTagArray;
53
54
55         /* init torture */
56         mem_ctx = talloc_named(NULL, 0, "torture_rpc_mapi_deletemail");
57
58         /* init mapi */
59         if ((session = torture_init_mapi(mem_ctx, torture->lp_ctx)) == NULL) return false;
60
61         /* init objets */
62         mapi_object_init(&obj_store);
63         mapi_object_init(&obj_inbox);
64         mapi_object_init(&obj_table);
65
66         /* session::OpenMsgStore() */
67         retval = OpenMsgStore(session, &obj_store);
68         mapi_errstr("OpenMsgStore", GetLastError());
69         if (retval != MAPI_E_SUCCESS) return false;
70         mapi_object_debug(&obj_store);
71
72         /* id_inbox = store->GetReceiveFolder */
73         retval = GetReceiveFolder(&obj_store, &id_inbox, NULL);
74         mapi_errstr("GetReceiveFolder", GetLastError());
75         if (retval != MAPI_E_SUCCESS) return false;
76
77         /* inbox = store->OpenFolder()
78          */
79         retval = OpenFolder(&obj_store, id_inbox, &obj_inbox);
80         mapi_errstr("OpenFolder", GetLastError());
81         if (retval != MAPI_E_SUCCESS) return false;
82         mapi_object_debug(&obj_inbox);
83
84         /* table = inbox->GetContentsTable() */
85         retval = GetContentsTable(&obj_inbox, &obj_table, 0, NULL);
86         mapi_errstr("GetContentsTable", GetLastError());
87         if (retval != MAPI_E_SUCCESS) return false;
88         mapi_object_debug(&obj_table);
89
90         /* rowset = table->QueryRows() */
91         SPropTagArray = set_SPropTagArray(mem_ctx, 0x5,
92                                           PR_FID,
93                                           PR_MID,
94                                           PR_INST_ID,
95                                           PR_INSTANCE_NUM,
96                                           PR_SUBJECT);
97         retval = SetColumns(&obj_table, SPropTagArray);
98         mapi_errstr("SetColumns", GetLastError());
99         if (retval != MAPI_E_SUCCESS) return false;
100
101         while ((retval = QueryRows(&obj_table, CN_ROWS, TBL_ADVANCE, &rowset)) == MAPI_E_SUCCESS) {
102                 cn_rows = rowset.cRows;
103                 if (!cn_rows) break;
104                 id_messages = talloc_array(mem_ctx, uint64_t, cn_rows);
105                 cn_messages = 0;
106                 
107                 if (s_subject == 0)
108                         s_subject = "default_subject";
109                 len_subject = strlen(s_subject);
110                 
111                 for (i_row = 0; i_row < cn_rows; ++i_row) {
112                         if (strncmp(rowset.aRow[i_row].lpProps[4].value.lpszA, s_subject, len_subject) == 0) {
113                                 id_messages[cn_messages] = rowset.aRow[i_row].lpProps[1].value.d;
114                                 ++cn_messages;
115                                 DEBUG(0, ("delete(%"PRIx64")\n", id_messages[cn_messages - 1]));
116                         }
117                 }
118
119                 /* IMessage::DeleteMessages() */
120                 if (cn_messages) {
121                         retval = DeleteMessage(&obj_inbox, id_messages, cn_messages);
122                         if (retval != MAPI_E_SUCCESS) {
123                                 mapi_errstr("DeleteMessages", GetLastError());
124                         }
125                 }
126         }
127
128         /* release objects
129          */
130         mapi_object_release(&obj_store);
131         mapi_object_release(&obj_inbox);
132         mapi_object_release(&obj_table);
133
134         /* uninitialize mapi
135          */
136         MAPIUninitialize();
137         talloc_free(mem_ctx);
138         
139         return (ret);
140 }