summaryrefslogtreecommitdiffstats
path: root/innbbsd/ctlinnbbsd.c
blob: 95e7d315162555349a0233508e62e57ef5caafd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdlib.h>
#include "innbbsconf.h"
#include "bbslib.h"
#include "externs.h"

extern char    *optarg;
extern int      opterr, optind;

void
usage(name)
    char           *name;
{
    fprintf(stderr, "Usage: %s [-p path] commands\n", name);
    fprintf(stderr, " where available commands:\n");
    fprintf(stderr, "  ctlinnbbsd reload   : reload datafiles for innbbsd\n");
    fprintf(stderr, "  ctlinnbbsd shutdown : shutdown innbbsd gracefully\n");
    fprintf(stderr, "  ctlinnbbsd mode     : examine mode of innbbsd\n");
    fprintf(stderr, "  ctlinnbbsd addhist <mid> path: add history\n");
    fprintf(stderr, "  ctlinnbbsd grephist <mid>: query history\n");
    fprintf(stderr, "  ctlinnbbsd verboselog on|off : verboselog on/off\n");
    fprintf(stderr, "  ctlinnbbsd hismaint : maintain history\n");
    fprintf(stderr, "  ctlinnbbsd listnodelist  : list nodelist.bbs\n");
    fprintf(stderr, "  ctlinnbbsd listnewsfeeds : list newsfeeds.bbs\n");
#ifdef GETRUSAGE
    fprintf(stderr, "  ctlinnbbsd getrusage: get resource usage\n");
#endif
#ifdef MALLOCMAP
    fprintf(stderr, "  ctlinnbbsd mallocmap: get malloc map\n");
#endif
}


char           *DefaultPath = LOCALDAEMON;
char            INNBBSbuffer[4096];

FILE           *innbbsin, *innbbsout;
int             innbbsfd;

void
ctlinnbbsd(argc, argv)
    int             argc;
    char          **argv;
{
    fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
    printf("%s", INNBBSbuffer);
    if (strcasecmp(argv[0], "shutdown") == 0 ||
    strcasecmp(argv[0], "reload") == 0 ||
    strcasecmp(argv[0], "hismaint") == 0 ||
#ifdef GETRUSAGE
    strcasecmp(argv[0], "getrusage") == 0 ||
#endif
#ifdef MALLOCMAP
    strcasecmp(argv[0], "mallocmap") == 0 ||
#endif
    strcasecmp(argv[0], "mode") == 0 ||
    strcasecmp(argv[0], "listnodelist") == 0 ||
    strcasecmp(argv[0], "listnewsfeeds") == 0
    ) {
    fprintf(innbbsout, "%s\r\n", argv[0]);
    fflush(innbbsout);
    fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
    printf("%s", INNBBSbuffer);
    if (strcasecmp(argv[0], "mode") == 0
#ifdef GETRUSAGE
        ||
        strcasecmp(argv[0], "getrusage") == 0
        ||
        strcasecmp(argv[0], "mallocmap") == 0
#endif
        ||
        strcasecmp(argv[0], "listnodelist") == 0
        ||
        strcasecmp(argv[0], "listnewsfeeds") == 0
        ) {
        while (fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin) != NULL) {
        if (strcmp(INNBBSbuffer, ".\r\n") == 0) {
            break;
        }
        printf("%s", INNBBSbuffer);
        }
    }
    } else if (strcasecmp(argv[0], "grephist") == 0 ||
           strcasecmp(argv[0], "verboselog") == 0) {
    if (argc < 2) {
        usage("ctlinnbbsd");
    } else {
        fprintf(innbbsout, "%s %s\r\n", argv[0], argv[1]);
        fflush(innbbsout);
        fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
        printf("%s\n", INNBBSbuffer);
    }
    } else if (strcasecmp(argv[0], "addhist") == 0) {
    if (argc < 3) {
        usage("ctlinnbbsd");
    } else {
        fprintf(innbbsout, "%s %s %s\r\n", argv[0], argv[1], argv[2]);
        fflush(innbbsout);
        fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
        printf("%s", INNBBSbuffer);
    }
    } else {
    fprintf(stderr, "invalid command %s\n", argv[0]);
    }
    if (strcasecmp(argv[0], "shutdown") != 0) {
    fprintf(innbbsout, "QUIT\r\n");
    fflush(innbbsout);
    fgets(INNBBSbuffer, sizeof INNBBSbuffer, innbbsin);
    }
}

void
initsocket()
{
    innbbsfd = unixclient(DefaultPath, "tcp");
    if (innbbsfd < 0) {
    fprintf(stderr, "Connect to %s error. You may not run innbbsd\n", DefaultPath);
    exit(2);
    }
    if ((innbbsin = fdopen(innbbsfd, "r")) == NULL ||
    (innbbsout = fdopen(innbbsfd, "w")) == NULL) {
    fprintf(stderr, "fdopen error\n");
    exit(3);
    }
}

void
closesocket()
{
    if (innbbsin != NULL)
    fclose(innbbsin);
    if (innbbsout != NULL)
    fclose(innbbsout);
    if (innbbsfd >= 0)
    close(innbbsfd);
}

int
main(argc, argv)
    int             argc;
    char          **argv;
{
    int             c, errflag = 0;

    while ((c = getopt(argc, argv, "p:h?")) != -1)
    switch (c) {
    case 'p':
        DefaultPath = optarg;
        break;
    case 'h':
    case '?':
    default:
        errflag++;
        break;
    }
    if (errflag > 0) {
    usage(argv[0]);
    return (1);
    }
    if (argc - optind < 1) {
    usage(argv[0]);
    exit(1);
    }
    initial_bbs(NULL);
    initsocket();
    ctlinnbbsd(argc - optind, argv + optind);
    closesocket();
}