16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
39 #include "dynloader.h"
46 #include "internet_addressing.h"
47 #include "serv_imap.h"
48 #include "imap_tools.h"
49 #include "imap_fetch.h"
50 #include "imap_misc.h"
59 * imap_copy() calls imap_do_copy() to do its actual work, once it's
60 * validated and boiled down the request a bit. (returns 0 on success)
62 int imap_do_copy(char *destination_folder) {
64 char roomname[ROOMNAMELEN];
66 i = imap_grabroom(roomname, destination_folder);
67 if (i != 0) return(i);
69 if (IMAP->num_msgs > 0) {
70 for (i = 0; i < IMAP->num_msgs; ++i) {
71 if (IMAP->flags[i] && IMAP_SELECTED) {
73 IMAP->msgids[i], roomname);
83 * This function is called by the main command loop.
85 void imap_copy(int num_parms, char *parms[]) {
89 cprintf("%s BAD invalid parameters\r\n", parms[0]);
93 if (imap_is_message_set(parms[2])) {
94 imap_pick_range(parms[2], 0);
97 cprintf("%s BAD invalid parameters\r\n", parms[0]);
101 ret = imap_do_copy(parms[3]);
103 cprintf("%s OK COPY completed\r\n", parms[0]);
106 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
111 * This function is called by the main command loop.
113 void imap_uidcopy(int num_parms, char *parms[]) {
115 if (num_parms != 5) {
116 cprintf("%s BAD invalid parameters\r\n", parms[0]);
120 if (imap_is_message_set(parms[3])) {
121 imap_pick_range(parms[3], 1);
124 cprintf("%s BAD invalid parameters\r\n", parms[0]);
128 if (imap_do_copy(parms[4]) == 0) {
129 cprintf("%s OK UID COPY completed\r\n", parms[0]);
132 cprintf("%s NO UID COPY failed\r\n", parms[0]);
138 * Poll for express messages (yeah, we can do this in IMAP ... I think)
140 void imap_print_express_messages(void) {
141 struct ExpressMessage *ptr, *holdptr;
142 char *dumpomatic = NULL;
145 if (CC->FirstExpressMessage == NULL) {
148 begin_critical_section(S_SESSION_TABLE);
149 ptr = CC->FirstExpressMessage;
150 CC->FirstExpressMessage = NULL;
151 end_critical_section(S_SESSION_TABLE);
153 while (ptr != NULL) {
154 dumpomatic = mallok(strlen(ptr->text) + SIZ);
155 strcpy(dumpomatic, "");
156 if (ptr->flags && EM_BROADCAST)
157 strcat(dumpomatic, "Broadcast message ");
158 else if (ptr->flags && EM_CHAT)
159 strcat(dumpomatic, "Chat request ");
160 else if (ptr->flags && EM_GO_AWAY)
161 strcat(dumpomatic, "Please logoff now, as requested ");
163 strcat(dumpomatic, "Message ");
164 sprintf(&dumpomatic[strlen(dumpomatic)],
165 "from %s:\n", ptr->sender);
166 if (ptr->text != NULL)
167 strcat(dumpomatic, ptr->text);
170 if (ptr->text != NULL) phree(ptr->text);
174 for (i=0; i<strlen(dumpomatic); ++i) {
175 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
176 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
177 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
180 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
189 * This function is called by the main command loop.
191 void imap_append(int num_parms, char *parms[]) {
192 size_t literal_length;
193 struct CtdlMessage *msg;
195 char roomname[ROOMNAMELEN];
197 char savedroom[ROOMNAMELEN];
202 cprintf("%s BAD usage error\r\n", parms[0]);
206 if ( (parms[num_parms-1][0] != '{')
207 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
208 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
212 literal_length = (size_t) atol(&parms[num_parms-1][1]);
213 if (literal_length < 1) {
214 cprintf("%s BAD Message length must be at least 1.\r\n",
219 imap_free_transmitted_message(); /* just in case. */
220 IMAP->transmitted_message = mallok(literal_length + 1);
221 if (IMAP->transmitted_message == NULL) {
222 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
225 IMAP->transmitted_length = literal_length;
227 cprintf("+ Transmit message now.\r\n");
228 ret = client_read(IMAP->transmitted_message, literal_length);
229 IMAP->transmitted_message[literal_length] = 0;
231 cprintf("%s NO Read failed.\r\n", parms[0]);
235 lprintf(9, "Converting message...\n");
236 msg = convert_internet_message(IMAP->transmitted_message);
237 IMAP->transmitted_message = NULL;
238 IMAP->transmitted_length = 0;
240 /* If the user is locally authenticated, FORCE the From: header to
241 * show up as the real sender. FIXME do we really want to do this?
242 * Probably should make it site-definable or even room-definable.
245 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
246 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
247 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
248 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
249 msg->cm_fields['N'] = strdoop(config.c_nodename);
250 msg->cm_fields['H'] = strdoop(config.c_humannode);
253 ret = imap_grabroom(roomname, parms[2]);
255 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
261 * usergoto() formally takes us to the desired room. (If another
262 * folder is selected, save its name so we can return there!!!!!)
264 if (IMAP->selected) {
265 strcpy(savedroom, CC->quickroom.QRname);
267 usergoto(roomname, 0, &msgs, &new);
272 ret = CtdlDoIHavePermissionToPostInThisRoom(buf);
275 /* Nope ... print an error message */
276 cprintf("%s NO %s\r\n", parms[0], buf);
280 /* Yes ... go ahead and post! */
282 CtdlSaveMsg(msg, "", "", 0);
284 cprintf("%s OK APPEND completed\r\n", parms[0]);
288 * IMAP protocol response to client has already been sent by now.
290 * If another folder is selected, go back to that room so we can resume
291 * our happy day without violent explosions.
293 if (IMAP->selected) {
294 usergoto(savedroom, 0, &msgs, &new);
297 /* We don't need this buffer anymore */
298 CtdlFreeMessage(msg);