16 #include <sys/types.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
29 #include "dynloader.h"
36 #include "internet_addressing.h"
37 #include "serv_imap.h"
38 #include "imap_tools.h"
39 #include "imap_fetch.h"
40 #include "imap_misc.h"
49 * imap_copy() calls imap_do_copy() to do its actual work, once it's
50 * validated and boiled down the request a bit. (returns 0 on success)
52 int imap_do_copy(char *destination_folder) {
54 char roomname[ROOMNAMELEN];
56 i = imap_grabroom(roomname, destination_folder);
57 if (i != 0) return(i);
59 if (IMAP->num_msgs > 0) {
60 for (i = 0; i < IMAP->num_msgs; ++i) {
61 if (IMAP->flags[i] && IMAP_SELECTED) {
63 IMAP->msgids[i], roomname);
73 * This function is called by the main command loop.
75 void imap_copy(int num_parms, char *parms[]) {
79 cprintf("%s BAD invalid parameters\r\n", parms[0]);
83 if (imap_is_message_set(parms[2])) {
84 imap_pick_range(parms[2], 0);
87 cprintf("%s BAD invalid parameters\r\n", parms[0]);
91 ret = imap_do_copy(parms[3]);
93 cprintf("%s OK COPY completed\r\n", parms[0]);
96 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
101 * This function is called by the main command loop.
103 void imap_uidcopy(int num_parms, char *parms[]) {
105 if (num_parms != 5) {
106 cprintf("%s BAD invalid parameters\r\n", parms[0]);
110 if (imap_is_message_set(parms[3])) {
111 imap_pick_range(parms[3], 1);
114 cprintf("%s BAD invalid parameters\r\n", parms[0]);
118 if (imap_do_copy(parms[4]) == 0) {
119 cprintf("%s OK UID COPY completed\r\n", parms[0]);
122 cprintf("%s NO UID COPY failed\r\n", parms[0]);
128 * Poll for express messages (yeah, we can do this in IMAP ... I think)
130 void imap_print_express_messages(void) {
131 struct ExpressMessage *ptr, *holdptr;
132 char *dumpomatic = NULL;
135 if (CC->FirstExpressMessage == NULL) {
138 begin_critical_section(S_SESSION_TABLE);
139 ptr = CC->FirstExpressMessage;
140 CC->FirstExpressMessage = NULL;
141 end_critical_section(S_SESSION_TABLE);
143 while (ptr != NULL) {
144 dumpomatic = mallok(strlen(ptr->text) + SIZ);
145 strcpy(dumpomatic, "");
146 if (ptr->flags && EM_BROADCAST)
147 strcat(dumpomatic, "Broadcast message ");
148 else if (ptr->flags && EM_CHAT)
149 strcat(dumpomatic, "Chat request ");
150 else if (ptr->flags && EM_GO_AWAY)
151 strcat(dumpomatic, "Please logoff now, as requested ");
153 strcat(dumpomatic, "Message ");
154 sprintf(&dumpomatic[strlen(dumpomatic)],
155 "from %s:\n", ptr->sender);
156 if (ptr->text != NULL)
157 strcat(dumpomatic, ptr->text);
160 if (ptr->text != NULL) phree(ptr->text);
164 for (i=0; i<strlen(dumpomatic); ++i) {
165 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
166 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
167 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
170 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
179 * This function is called by the main command loop.
181 void imap_append(int num_parms, char *parms[]) {
182 size_t literal_length;
183 struct CtdlMessage *msg;
185 char roomname[ROOMNAMELEN];
187 char savedroom[ROOMNAMELEN];
192 cprintf("%s BAD usage error\r\n", parms[0]);
196 if ( (parms[num_parms-1][0] != '{')
197 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
198 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
202 literal_length = (size_t) atol(&parms[num_parms-1][1]);
203 if (literal_length < 1) {
204 cprintf("%s BAD Message length must be at least 1.\r\n",
209 imap_free_transmitted_message(); /* just in case. */
210 IMAP->transmitted_message = mallok(literal_length + 1);
211 if (IMAP->transmitted_message == NULL) {
212 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
215 IMAP->transmitted_length = literal_length;
217 cprintf("+ Transmit message now.\r\n");
218 ret = client_read(IMAP->transmitted_message, literal_length);
219 IMAP->transmitted_message[literal_length] = 0;
221 cprintf("%s NO Read failed.\r\n", parms[0]);
225 lprintf(9, "Converting message...\n");
226 msg = convert_internet_message(IMAP->transmitted_message);
227 IMAP->transmitted_message = NULL;
228 IMAP->transmitted_length = 0;
230 /* If the user is locally authenticated, FORCE the From: header to
231 * show up as the real sender. FIXME do we really want to do this?
232 * Probably should make it site-definable or even room-definable.
235 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
236 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
237 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
238 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
239 msg->cm_fields['N'] = strdoop(config.c_nodename);
240 msg->cm_fields['H'] = strdoop(config.c_humannode);
243 ret = imap_grabroom(roomname, parms[2]);
245 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
251 * usergoto() formally takes us to the desired room. (If another
252 * folder is selected, save its name so we can return there!!!!!)
254 if (IMAP->selected) {
255 strcpy(savedroom, CC->quickroom.QRname);
257 usergoto(roomname, 0, &msgs, &new);
262 ret = CtdlDoIHavePermissionToPostInThisRoom(buf);
265 /* Nope ... print an error message */
266 cprintf("%s NO %s\r\n", parms[0], buf);
270 /* Yes ... go ahead and post! */
272 CtdlSaveMsg(msg, "", "", 0);
274 cprintf("%s OK APPEND completed\r\n", parms[0]);
278 * IMAP protocol response to client has already been sent by now.
280 * If another folder is selected, go back to that room so we can resume
281 * our happy day without violent explosions.
283 if (IMAP->selected) {
284 usergoto(savedroom, 0, &msgs, &new);
287 /* We don't need this buffer anymore */
288 CtdlFreeMessage(msg);