2 * Command-line utility to transmit a server command.
4 * Copyright (c) 1987-2009 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ctdl_module.h"
25 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
46 #include <libcitadel.h>
48 #include "citadel_ipc.h"
52 #define LOCKFILE "/tmp/LCK.sendcommand"
54 static CtdlIPC *ipc = NULL;
57 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
58 * cleanup() . If for some reason sendcommand hangs waiting for the server
59 * to clean up, the alarm clock goes off and the program exits anyway.
60 * The cleanup() routine makes a check to ensure it's not reentering, in
61 * case the ipc module looped it somehow.
63 void nq_cleanup(int e)
69 * send binary to server
71 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
73 unsigned int bytes_written = 0;
76 #if defined(HAVE_OPENSSL)
78 serv_write_ssl(ipc, buf, nbytes);
83 while (bytes_written < nbytes) {
84 retval = write(ipc->sock, &buf[bytes_written],
85 nbytes - bytes_written);
87 connection_died(ipc, 0);
90 bytes_written += retval;
97 static int nested = 0;
100 signal(SIGALRM, nq_cleanup);
101 serv_write(ipc, "\n", 1);
108 * This is implemented as a function rather than as a macro because the
109 * client-side IPC modules expect logoff() to be defined. They call logoff()
110 * when a problem connecting or staying connected to the server occurs.
117 static char *args[] =
121 * Connect sendcommand to the Citadel server running on this computer.
123 void np_attach_to_server(char *host, char *port)
126 char hostbuf[256] = "";
127 char portbuf[256] = "";
130 fprintf(stderr, "Attaching to server...\n");
131 strncpy(hostbuf, host, 256);
132 strncpy(portbuf, port, 256);
133 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
135 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
138 CtdlIPC_chat_recv(ipc, buf);
139 fprintf(stderr, "%s\n", &buf[4]);
140 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
141 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
142 fprintf(stderr, "%s\n", buf);
149 void sendcommand_die(void) {
155 * saves filelen bytes from file at pathname
157 int save_buffer(void *file, size_t filelen, const char *pathname)
160 size_t bytes_written = 0;
163 fp = fopen(pathname, "w");
165 fprintf(stderr, "Cannot open '%s': %s\n", pathname, strerror(errno));
169 block = fwrite((char *)file + bytes_written, 1,
170 filelen - bytes_written, fp);
171 bytes_written += block;
172 } while (errno == EINTR && bytes_written < filelen);
175 if (bytes_written < filelen) {
176 fprintf(stderr,"Trouble saving '%s': %s\n", pathname,
187 int main(int argc, char **argv)
192 int MessageToRetrieve;
193 int MessageFound = 0;
197 char relhome[PATH_MAX]="";
198 char ctdldir[PATH_MAX]=CTDLDIR;
202 int server_shutting_down = 0;
203 struct ctdlipcroom *Room;
204 struct ctdlipcmessage *mret;
206 unsigned long *msgarr;
209 strcpy(ctdl_home_directory, DEFAULT_PORT);
212 * Change directories if specified
214 for (a = 1; a < argc && n < 5; ++a) {
215 if (!strncmp(argv[a], "-h", 2)) {
216 relh=argv[a][2]!='/';
217 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
218 sizeof ctdl_home_directory);
220 safestrncpy(relhome, &argv[a][2],
225 strcpy(cmd[n++], argv[a]);
229 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
232 signal(SIGINT, cleanup);
233 signal(SIGQUIT, cleanup);
234 signal(SIGHUP, cleanup);
235 signal(SIGTERM, cleanup);
237 fprintf(stderr, "getmail: started (pid=%d) "
240 ctdl_home_directory);
244 // signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
246 np_attach_to_server(UDS, ctdl_run_dir);
248 setIPCDeathHook(sendcommand_die);
250 fprintf(stderr, "GOTO %s\n", cmd[0]);
251 CtdlIPCGotoRoom(ipc, cmd[0], "", &Room, cret);
252 fprintf(stderr, "%s\n", cret);
254 MessageToRetrieve = atol(cmd[1]);
256 r = CtdlIPCGetMessages(ipc, 0, 0, NULL, &msgarr, buf);
257 printf("Messages: ");
258 for (i = 0; msgarr[i] > 0 ; i ++)
260 // printf(" %ld ", msgarr[i]);
261 if (msgarr[i] == MessageToRetrieve)
265 printf("Message %d not found in the above list.", MessageToRetrieve);
268 CtdlIPCGetSingleMessage(ipc, MessageToRetrieve,0,4, &mret, cret);
269 fprintf(stderr, "%s\n", cret);
270 fprintf(stderr, "%s: %s\n", "path", mret->path);
271 fprintf(stderr, "%s: %s\n", "author", mret->author);
272 fprintf(stderr, "%s: %s\n", "subject", mret->subject);
273 fprintf(stderr, "%s: %s\n", "email", mret->email);
274 fprintf(stderr, "%s: %s\n", "text", mret->text);
276 att = mret->attachments;
283 fprintf(stderr, "Attachment: [%s] %s\n", att->number, att->filename);
284 r = CtdlIPCAttachmentDownload(ipc, MessageToRetrieve, att->number, &attachment, NULL, buf);
285 printf("----\%s\n----\n", buf);
291 len = (size_t)extract_long(buf, 0);
292 CtdlMakeTempFileName(tmp, sizeof tmp);
293 strcat(tmp, att->filename);
294 printf("Saving Attachment to %s", tmp);
295 save_buffer(attachment, len, tmp);
313 CtdlIPC_chat_send(ipc, cmd[4]);
314 CtdlIPC_chat_recv(ipc, buf);
315 fprintf(stderr, "%s\n", buf);
320 if (!strncasecmp(&buf[1], "31", 2)) {
321 server_shutting_down = 1;
325 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
327 alarm(5); /* Kick the watchdog timer */
329 } else if (buf[0] == '4') {
331 if (fgets(buf, sizeof buf, stdin) == NULL)
333 if (!IsEmptyStr(buf))
334 if (buf[strlen(buf) - 1] == '\n')
335 buf[strlen(buf) - 1] = 0;
336 if (!IsEmptyStr(buf))
337 if (buf[strlen(buf) - 1] == '\r')
338 buf[strlen(buf) - 1] = 0;
339 if (strcmp(buf, "000"))
340 CtdlIPC_chat_send(ipc, buf);
343 FD_SET(ipc->sock, &read_fd);
344 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
347 printf("select failed: %d", err);
350 if (!(errno == EINTR || errno == EAGAIN))
351 printf("select failed: %d", err);
360 n = read(ipc->sock, rbuf, SIZ);
363 fprintf(stderr, "%s", rbuf);
367 alarm(5); /* Kick the watchdog timer */
368 } while (strcmp(buf, "000"));
369 CtdlIPC_chat_send(ipc, "\n");
370 CtdlIPC_chat_send(ipc, "000");
372 alarm(0); /* Shutdown the watchdog timer */
373 fprintf(stderr, "sendcommand: processing ended.\n");
375 /* Clean up and log off ... unless the server indicated that the command
376 * we sent is shutting it down, in which case we want to just cut the
377 * connection and exit.
379 if (server_shutting_down) {
392 void stty_ctdl(int cmd) {