/*
- * $Id: sendcommand.c 5736 2007-11-10 23:12:19Z dothebart $
+ * $Id$
*
* Command-line utility to transmit a server command.
*
+ * Copyright (c) 1987-2009 by the citadel.org team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
+#include <libcitadel.h>
#include "citadel.h"
-#include "tools.h"
#include "citadel_ipc.h"
#include "server.h"
#include "config.h"
{
FILE *lfp;
int onppid;
+ int rv;
if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
- fscanf(lfp, "%d", &onppid);
+ rv = fscanf(lfp, "%d", &onppid);
fclose(lfp);
if (!kill(onppid, 0) || errno == EPERM)
return 1;
cleanup(e);
}
+static char *args[] =
+{"getmail", NULL};
+
/*
* Connect sendcommand to the Citadel server running on this computer.
*/
void np_attach_to_server(char *host, char *port)
{
char buf[SIZ];
- char hostbuf[256], portbuf[256];
- char *args[] =
- {"sendcommand", NULL};
+ char hostbuf[256] = "";
+ char portbuf[256] = "";
int r;
fprintf(stderr, "Attaching to server...\n");
- strcpy(hostbuf, host);
- strcpy(portbuf, port);
+ strncpy(hostbuf, host, 256);
+ strncpy(portbuf, port, 256);
ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
if (!ipc) {
fprintf(stderr, "Can't connect: %s\n", strerror(errno));
}
+/*
+ * saves filelen bytes from file at pathname
+ */
+int save_buffer(void *file, size_t filelen, const char *pathname)
+{
+ size_t block = 0;
+ size_t bytes_written = 0;
+ FILE *fp;
+
+ fp = fopen(pathname, "w");
+ if (!fp) {
+ fprintf(stderr, "Cannot open '%s': %s\n", pathname, strerror(errno));
+ return 0;
+ }
+ do {
+ block = fwrite((char *)file + bytes_written, 1,
+ filelen - bytes_written, fp);
+ bytes_written += block;
+ } while (errno == EINTR && bytes_written < filelen);
+ fclose(fp);
+
+ if (bytes_written < filelen) {
+ fprintf(stderr,"Trouble saving '%s': %s\n", pathname,
+ strerror(errno));
+ return 0;
+ }
+ return 1;
+}
+
+
/*
* main
*/
int main(int argc, char **argv)
{
- int a;
+ int a, r, i;
char cmd[5][SIZ];
char buf[SIZ];
-
+ int MessageToRetrieve;
+ int MessageFound = 0;
int relh=0;
int home=0;
int n=0;
struct ctdlipcroom *Room;
struct ctdlipcmessage *mret;
char cret[SIZ];
-
- CtdlInitBase64Table();
+ unsigned long *msgarr;
+ struct parts *att;
strcpy(ctdl_home_directory, DEFAULT_PORT);
else
safestrncpy(relhome, &argv[a][2],
sizeof relhome);
- home_specified = 1;
home=1;
} else {
signal(SIGHUP, cleanup);
signal(SIGTERM, cleanup);
- fprintf(stderr, "sendcommand: started (pid=%d) "
+ fprintf(stderr, "getmail: started (pid=%d) "
"running in %s\n",
(int) getpid(),
ctdl_home_directory);
fflush(stderr);
- alarm(5);
- signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
+// alarm(5);
+// signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
- np_attach_to_server(UDS, ctdl_home_directory);
+ np_attach_to_server(UDS, ctdl_run_dir);
fflush(stderr);
setIPCDeathHook(sendcommand_die);
fprintf(stderr, "GOTO %s\n", cmd[0]);
CtdlIPCGotoRoom(ipc, cmd[0], "", &Room, cret);
fprintf(stderr, "%s\n", cret);
- CtdlIPCGetSingleMessage(ipc, atol(cmd[1]) ,atol(cmd[2]),4, &mret, cret);
+
+ MessageToRetrieve = atol(cmd[1]);
+
+ r = CtdlIPCGetMessages(ipc, 0, 0, NULL, &msgarr, buf);
+ printf("Messages: ");
+ for (i = 0; msgarr[i] > 0 ; i ++)
+ {
+// printf(" %ld ", msgarr[i]);
+ if (msgarr[i] == MessageToRetrieve)
+ MessageFound = 1;
+ }
+ if (!MessageFound)
+ printf("Message %d not found in the above list.", MessageToRetrieve);
+ printf("\n");
+
+ CtdlIPCGetSingleMessage(ipc, MessageToRetrieve,0,4, &mret, cret);
fprintf(stderr, "%s\n", cret);
fprintf(stderr, "%s: %s\n", "path", mret->path);
+ fprintf(stderr, "%s: %s\n", "author", mret->author);
fprintf(stderr, "%s: %s\n", "subject", mret->subject);
fprintf(stderr, "%s: %s\n", "email", mret->email);
+ fprintf(stderr, "%s: %s\n", "text", mret->text);
+
+ att = mret->attachments;
+
+ while (att != NULL){
+ void *attachment;
+ char tmp[PATH_MAX];
+ char buf[SIZ];
+
+ fprintf(stderr, "Attachment: [%s] %s\n", att->number, att->filename);
+ r = CtdlIPCAttachmentDownload(ipc, MessageToRetrieve, att->number, &attachment, NULL, buf);
+ printf("----\%s\n----\n", buf);
+ if (r / 100 != 2) {
+ printf("%s\n", buf);
+ } else {
+ size_t len;
+
+ len = (size_t)extract_long(buf, 0);
+ CtdlMakeTempFileName(tmp, sizeof tmp);
+ strcat(tmp, att->filename);
+ printf("Saving Attachment to %s", tmp);
+ save_buffer(attachment, len, tmp);
+ free(attachment);
+ }
+ att = att->next;
+
+ }
+
+ ///if (
+
+
CtdlIPCQuit(ipc);
exit (1);
+
+
+
+
+
+
CtdlIPC_chat_send(ipc, cmd[4]);
CtdlIPC_chat_recv(ipc, buf);
fprintf(stderr, "%s\n", buf);
n = read(ipc->sock, rbuf, SIZ);
if (n>0) {
rbuf[n]='\0';
- fprintf (stderr, rbuf);
- fflush (stdout);
+ fprintf(stderr, "%s", rbuf);
+ fflush(stdout);
}
}
alarm(5); /* Kick the watchdog timer */
}
return 0;
}
+
+
+/*
+ * Stub function
+ */
+void stty_ctdl(int cmd) {
+}
+
+