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 * make sure only one copy of sendcommand runs at a time, using lock files
59 int set_lockfile(void)
65 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
66 rv = fscanf(lfp, "%d", &onppid);
68 if (!kill(onppid, 0) || errno == EPERM)
71 lfp = fopen(LOCKFILE, "w");
72 fprintf(lfp, "%ld\n", (long) getpid());
77 void remove_lockfile(void)
83 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
84 * cleanup() . If for some reason sendcommand hangs waiting for the server
85 * to clean up, the alarm clock goes off and the program exits anyway.
86 * The cleanup() routine makes a check to ensure it's not reentering, in
87 * case the ipc module looped it somehow.
89 void nq_cleanup(int e)
96 * send binary to server
98 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
100 unsigned int bytes_written = 0;
103 #if defined(HAVE_OPENSSL)
105 serv_write_ssl(ipc, buf, nbytes);
110 while (bytes_written < nbytes) {
111 retval = write(ipc->sock, &buf[bytes_written],
112 nbytes - bytes_written);
114 connection_died(ipc, 0);
117 bytes_written += retval;
124 static int nested = 0;
127 signal(SIGALRM, nq_cleanup);
128 serv_write(ipc, "\n", 1);
135 * This is implemented as a function rather than as a macro because the
136 * client-side IPC modules expect logoff() to be defined. They call logoff()
137 * when a problem connecting or staying connected to the server occurs.
144 static char *args[] =
148 * Connect sendcommand to the Citadel server running on this computer.
150 void np_attach_to_server(char *host, char *port)
153 char hostbuf[256] = "";
154 char portbuf[256] = "";
157 fprintf(stderr, "Attaching to server...\n");
158 strncpy(hostbuf, host, 256);
159 strncpy(portbuf, port, 256);
160 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
162 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
165 CtdlIPC_chat_recv(ipc, buf);
166 fprintf(stderr, "%s\n", &buf[4]);
167 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
168 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
169 fprintf(stderr, "%s\n", buf);
176 void sendcommand_die(void) {
182 * saves filelen bytes from file at pathname
184 int save_buffer(void *file, size_t filelen, const char *pathname)
187 size_t bytes_written = 0;
190 fp = fopen(pathname, "w");
192 fprintf(stderr, "Cannot open '%s': %s\n", pathname, strerror(errno));
196 block = fwrite((char *)file + bytes_written, 1,
197 filelen - bytes_written, fp);
198 bytes_written += block;
199 } while (errno == EINTR && bytes_written < filelen);
202 if (bytes_written < filelen) {
203 fprintf(stderr,"Trouble saving '%s': %s\n", pathname,
214 int main(int argc, char **argv)
219 int MessageToRetrieve;
220 int MessageFound = 0;
224 char relhome[PATH_MAX]="";
225 char ctdldir[PATH_MAX]=CTDLDIR;
229 int server_shutting_down = 0;
230 struct ctdlipcroom *Room;
231 struct ctdlipcmessage *mret;
233 unsigned long *msgarr;
236 strcpy(ctdl_home_directory, DEFAULT_PORT);
239 * Change directories if specified
241 for (a = 1; a < argc && n < 5; ++a) {
242 if (!strncmp(argv[a], "-h", 2)) {
243 relh=argv[a][2]!='/';
244 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
245 sizeof ctdl_home_directory);
247 safestrncpy(relhome, &argv[a][2],
252 strcpy(cmd[n++], argv[a]);
256 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
259 signal(SIGINT, cleanup);
260 signal(SIGQUIT, cleanup);
261 signal(SIGHUP, cleanup);
262 signal(SIGTERM, cleanup);
264 fprintf(stderr, "getmail: started (pid=%d) "
267 ctdl_home_directory);
271 // signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
273 np_attach_to_server(UDS, ctdl_run_dir);
275 setIPCDeathHook(sendcommand_die);
277 fprintf(stderr, "GOTO %s\n", cmd[0]);
278 CtdlIPCGotoRoom(ipc, cmd[0], "", &Room, cret);
279 fprintf(stderr, "%s\n", cret);
281 MessageToRetrieve = atol(cmd[1]);
283 r = CtdlIPCGetMessages(ipc, 0, 0, NULL, &msgarr, buf);
284 printf("Messages: ");
285 for (i = 0; msgarr[i] > 0 ; i ++)
287 // printf(" %ld ", msgarr[i]);
288 if (msgarr[i] == MessageToRetrieve)
292 printf("Message %d not found in the above list.", MessageToRetrieve);
295 CtdlIPCGetSingleMessage(ipc, MessageToRetrieve,0,4, &mret, cret);
296 fprintf(stderr, "%s\n", cret);
297 fprintf(stderr, "%s: %s\n", "path", mret->path);
298 fprintf(stderr, "%s: %s\n", "author", mret->author);
299 fprintf(stderr, "%s: %s\n", "subject", mret->subject);
300 fprintf(stderr, "%s: %s\n", "email", mret->email);
301 fprintf(stderr, "%s: %s\n", "text", mret->text);
303 att = mret->attachments;
310 fprintf(stderr, "Attachment: [%s] %s\n", att->number, att->filename);
311 r = CtdlIPCAttachmentDownload(ipc, MessageToRetrieve, att->number, &attachment, NULL, buf);
312 printf("----\%s\n----\n", buf);
318 len = (size_t)extract_long(buf, 0);
319 CtdlMakeTempFileName(tmp, sizeof tmp);
320 strcat(tmp, att->filename);
321 printf("Saving Attachment to %s", tmp);
322 save_buffer(attachment, len, tmp);
340 CtdlIPC_chat_send(ipc, cmd[4]);
341 CtdlIPC_chat_recv(ipc, buf);
342 fprintf(stderr, "%s\n", buf);
347 if (!strncasecmp(&buf[1], "31", 2)) {
348 server_shutting_down = 1;
352 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
354 alarm(5); /* Kick the watchdog timer */
356 } else if (buf[0] == '4') {
358 if (fgets(buf, sizeof buf, stdin) == NULL)
360 if (!IsEmptyStr(buf))
361 if (buf[strlen(buf) - 1] == '\n')
362 buf[strlen(buf) - 1] = 0;
363 if (!IsEmptyStr(buf))
364 if (buf[strlen(buf) - 1] == '\r')
365 buf[strlen(buf) - 1] = 0;
366 if (strcmp(buf, "000"))
367 CtdlIPC_chat_send(ipc, buf);
370 FD_SET(ipc->sock, &read_fd);
371 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
374 printf("select failed: %d", err);
377 if (!(errno == EINTR || errno == EAGAIN))
378 printf("select failed: %d", err);
387 n = read(ipc->sock, rbuf, SIZ);
390 fprintf(stderr, "%s", rbuf);
394 alarm(5); /* Kick the watchdog timer */
395 } while (strcmp(buf, "000"));
396 CtdlIPC_chat_send(ipc, "\n");
397 CtdlIPC_chat_send(ipc, "000");
399 alarm(0); /* Shutdown the watchdog timer */
400 fprintf(stderr, "sendcommand: processing ended.\n");
402 /* Clean up and log off ... unless the server indicated that the command
403 * we sent is shutting it down, in which case we want to just cut the
404 * connection and exit.
406 if (server_shutting_down) {
419 void stty_ctdl(int cmd) {