New ctdlsh command "mailq" to show the outbound SMTP queue
[citadel] / ctdlsh / mailq.c
1 /*
2  * (c) 1987-2017 by Art Cancro and citadel.org
3  * This program is open source software, released under the terms of the GNU General Public License v3.
4  * It runs really well on the Linux operating system.
5  * We love open source software but reject Richard Stallman's linguistic fascism.
6  */
7
8 #include "ctdlsh.h"
9
10
11 void mailq_show_this_queue_entry(StrBuf *MsgText) {
12         const char *Pos = NULL;
13         StrBuf *Line = NewStrBuf();
14         int sip = 0;
15
16         do  {
17                 sip = StrBufSipLine(Line, MsgText, &Pos);
18
19                 if (!strncasecmp(ChrPtr(Line), HKEY("msgid|"))) {
20                         printf("Message %ld:\n", atol(ChrPtr(Line)+6));
21                 }
22                 else if (!strncasecmp(ChrPtr(Line), HKEY("submitted|"))) {
23                         time_t submitted =  atol(ChrPtr(Line)+10);
24                         printf("Originally submitted: %s", asctime(localtime(&submitted)));
25                 }
26                 else if (!strncasecmp(ChrPtr(Line), HKEY("attempted|"))) {
27                         time_t attempted =  atol(ChrPtr(Line)+10);
28                         printf("Last delivery attempt: %s", asctime(localtime(&attempted)));
29                 }
30                 else if (!strncasecmp(ChrPtr(Line), HKEY("bounceto|"))) {
31                         printf("Sender: %s\n", ChrPtr(Line)+10);
32                 }
33                 else if (!strncasecmp(ChrPtr(Line), HKEY("remote|"))) {
34                         printf("Recipient: %s\n", ChrPtr(Line)+7);
35                 }
36         } while(sip);
37
38         FreeStrBuf(&Line);
39         printf("\n");
40 }
41
42
43 int cmd_mailq(int server_socket, char *cmdbuf) {
44         char buf[1024];
45         long *msgs;
46         int num_msgs = 0;
47         int num_alloc = 0;
48         int i;
49         StrBuf *MsgText;
50
51         sock_puts(server_socket, "GOTO __CitadelSMTPspoolout__");
52         sock_getln(server_socket, buf, sizeof buf);
53         if (buf[0] != '2') {
54                 printf("%s\n", &buf[4]);
55                 return(cmdret_error);
56         }
57
58         sock_puts(server_socket, "MSGS ALL");
59         sock_getln(server_socket, buf, sizeof buf);
60         if (buf[0] != '1') {
61                 printf("%s\n", &buf[4]);
62                 return(cmdret_error);
63         }
64
65         MsgText = NewStrBuf();
66         while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
67
68                 if (num_alloc == 0) {
69                         num_alloc = 100;
70                         msgs = malloc(num_alloc * sizeof(long));
71                 }
72                 else if (num_msgs >= num_alloc) {
73                         num_alloc *= 2;
74                         msgs = realloc(msgs, num_alloc * sizeof(long));
75                 }
76
77                 msgs[num_msgs++] = atol(buf);
78         }
79
80         for (i=0; i<num_msgs; ++i) {
81                 sock_printf(server_socket, "MSG2 %ld\n", msgs[i]);
82                 sock_getln(server_socket, buf, sizeof buf);
83                 if (buf[0] == '1') {
84                         FlushStrBuf(MsgText);
85                         while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
86                                 StrBufAppendPrintf(MsgText, "%s\n", buf);
87                         }
88                         if (bmstrcasestr((char *)ChrPtr(MsgText), "\nContent-type: application/x-citadel-delivery-list") != NULL) {
89                                 mailq_show_this_queue_entry(MsgText);
90                         }
91                 }
92         }
93
94         free(msgs);
95         FreeStrBuf(&MsgText);
96         return(cmdret_ok);
97 }