4 * Command-line utility to transmit a server command.
6 * Copyright (c) 1987-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ctdl_module.h"
27 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
48 #include <libcitadel.h>
50 #include "citadel_ipc.h"
54 #define LOCKFILE "/tmp/LCK.sendcommand"
56 static CtdlIPC *ipc = NULL;
59 * make sure only one copy of sendcommand runs at a time, using lock files
61 int set_lockfile(void)
67 if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
68 rv = fscanf(lfp, "%d", &onppid);
70 if (!kill(onppid, 0) || errno == EPERM)
73 lfp = fopen(LOCKFILE, "w");
74 fprintf(lfp, "%ld\n", (long) getpid());
79 void remove_lockfile(void)
85 * Why both cleanup() and nq_cleanup() ? Notice the alarm() call in
86 * cleanup() . If for some reason sendcommand hangs waiting for the server
87 * to clean up, the alarm clock goes off and the program exits anyway.
88 * The cleanup() routine makes a check to ensure it's not reentering, in
89 * case the ipc module looped it somehow.
91 void nq_cleanup(int e)
98 * send binary to server
100 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
102 unsigned int bytes_written = 0;
105 #if defined(HAVE_OPENSSL)
107 serv_write_ssl(ipc, buf, nbytes);
112 while (bytes_written < nbytes) {
113 retval = write(ipc->sock, &buf[bytes_written],
114 nbytes - bytes_written);
116 connection_died(ipc, 0);
119 bytes_written += retval;
126 static int nested = 0;
129 signal(SIGALRM, nq_cleanup);
130 serv_write(ipc, "\n", 1);
137 * This is implemented as a function rather than as a macro because the
138 * client-side IPC modules expect logoff() to be defined. They call logoff()
139 * when a problem connecting or staying connected to the server occurs.
146 static char *args[] =
150 * Connect sendcommand to the Citadel server running on this computer.
152 void np_attach_to_server(char *host, char *port)
155 char hostbuf[256] = "";
156 char portbuf[256] = "";
159 fprintf(stderr, "Attaching to server...\n");
160 strncpy(hostbuf, host, 256);
161 strncpy(portbuf, port, 256);
162 ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
164 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
167 CtdlIPC_chat_recv(ipc, buf);
168 fprintf(stderr, "%s\n", &buf[4]);
169 snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
170 r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
171 fprintf(stderr, "%s\n", buf);
178 void sendcommand_die(void) {
184 * saves filelen bytes from file at pathname
186 int save_buffer(void *file, size_t filelen, const char *pathname)
189 size_t bytes_written = 0;
192 fp = fopen(pathname, "w");
194 fprintf(stderr, "Cannot open '%s': %s\n", pathname, strerror(errno));
198 block = fwrite((char *)file + bytes_written, 1,
199 filelen - bytes_written, fp);
200 bytes_written += block;
201 } while (errno == EINTR && bytes_written < filelen);
204 if (bytes_written < filelen) {
205 fprintf(stderr,"Trouble saving '%s': %s\n", pathname,
216 int main(int argc, char **argv)
221 int MessageToRetrieve;
222 int MessageFound = 0;
226 char relhome[PATH_MAX]="";
227 char ctdldir[PATH_MAX]=CTDLDIR;
231 int server_shutting_down = 0;
232 struct ctdlipcroom *Room;
233 struct ctdlipcmessage *mret;
235 unsigned long *msgarr;
238 strcpy(ctdl_home_directory, DEFAULT_PORT);
241 * Change directories if specified
243 for (a = 1; a < argc && n < 5; ++a) {
244 if (!strncmp(argv[a], "-h", 2)) {
245 relh=argv[a][2]!='/';
246 if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
247 sizeof ctdl_home_directory);
249 safestrncpy(relhome, &argv[a][2],
254 strcpy(cmd[n++], argv[a]);
258 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
261 signal(SIGINT, cleanup);
262 signal(SIGQUIT, cleanup);
263 signal(SIGHUP, cleanup);
264 signal(SIGTERM, cleanup);
266 fprintf(stderr, "getmail: started (pid=%d) "
269 ctdl_home_directory);
273 // signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
275 np_attach_to_server(UDS, ctdl_run_dir);
277 setIPCDeathHook(sendcommand_die);
279 fprintf(stderr, "GOTO %s\n", cmd[0]);
280 CtdlIPCGotoRoom(ipc, cmd[0], "", &Room, cret);
281 fprintf(stderr, "%s\n", cret);
283 MessageToRetrieve = atol(cmd[1]);
285 r = CtdlIPCGetMessages(ipc, 0, 0, NULL, &msgarr, buf);
286 printf("Messages: ");
287 for (i = 0; msgarr[i] > 0 ; i ++)
289 // printf(" %ld ", msgarr[i]);
290 if (msgarr[i] == MessageToRetrieve)
294 printf("Message %d not found in the above list.", MessageToRetrieve);
297 CtdlIPCGetSingleMessage(ipc, MessageToRetrieve,0,4, &mret, cret);
298 fprintf(stderr, "%s\n", cret);
299 fprintf(stderr, "%s: %s\n", "path", mret->path);
300 fprintf(stderr, "%s: %s\n", "author", mret->author);
301 fprintf(stderr, "%s: %s\n", "subject", mret->subject);
302 fprintf(stderr, "%s: %s\n", "email", mret->email);
303 fprintf(stderr, "%s: %s\n", "text", mret->text);
305 att = mret->attachments;
312 fprintf(stderr, "Attachment: [%s] %s\n", att->number, att->filename);
313 r = CtdlIPCAttachmentDownload(ipc, MessageToRetrieve, att->number, &attachment, NULL, buf);
314 printf("----\%s\n----\n", buf);
320 len = (size_t)extract_long(buf, 0);
321 CtdlMakeTempFileName(tmp, sizeof tmp);
322 strcat(tmp, att->filename);
323 printf("Saving Attachment to %s", tmp);
324 save_buffer(attachment, len, tmp);
342 CtdlIPC_chat_send(ipc, cmd[4]);
343 CtdlIPC_chat_recv(ipc, buf);
344 fprintf(stderr, "%s\n", buf);
349 if (!strncasecmp(&buf[1], "31", 2)) {
350 server_shutting_down = 1;
354 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
356 alarm(5); /* Kick the watchdog timer */
358 } else if (buf[0] == '4') {
360 if (fgets(buf, sizeof buf, stdin) == NULL)
362 if (!IsEmptyStr(buf))
363 if (buf[strlen(buf) - 1] == '\n')
364 buf[strlen(buf) - 1] = 0;
365 if (!IsEmptyStr(buf))
366 if (buf[strlen(buf) - 1] == '\r')
367 buf[strlen(buf) - 1] = 0;
368 if (strcmp(buf, "000"))
369 CtdlIPC_chat_send(ipc, buf);
372 FD_SET(ipc->sock, &read_fd);
373 ret = select(ipc->sock+1, &read_fd, NULL, NULL, &tv);
376 printf("select failed: %d", err);
379 if (!(errno == EINTR || errno == EAGAIN))
380 printf("select failed: %d", err);
389 n = read(ipc->sock, rbuf, SIZ);
392 fprintf(stderr, "%s", rbuf);
396 alarm(5); /* Kick the watchdog timer */
397 } while (strcmp(buf, "000"));
398 CtdlIPC_chat_send(ipc, "\n");
399 CtdlIPC_chat_send(ipc, "000");
401 alarm(0); /* Shutdown the watchdog timer */
402 fprintf(stderr, "sendcommand: processing ended.\n");
404 /* Clean up and log off ... unless the server indicated that the command
405 * we sent is shutting it down, in which case we want to just cut the
406 * connection and exit.
408 if (server_shutting_down) {
421 void stty_ctdl(int cmd) {