]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
81fcb9149eebaed67b55224c73aa7550e364aa1b
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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.
10  *
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.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  xdump.c: Extended dump
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2016
31  */
32
33 #include <config.h>
34
35 #include <ctype.h>
36 #include "commands.h"
37 #include "empobj.h"
38 #include "optlist.h"
39 #include "xdump.h"
40
41 /*
42  * Is object @p of type @type visible to the player?
43  * TODO: Fold this into interators.
44  */
45 static int
46 xdvisible(int type, void *p)
47 {
48     struct empobj *gp = p;
49     struct lonstr *lp = p;
50     struct natstr *natp;
51     int tlev;
52
53     if (!empobj_in_use(type, p))
54         return 0;
55
56     switch (type) {
57     case EF_SECTOR:
58     case EF_SHIP:
59     case EF_PLANE:
60     case EF_LAND:
61     case EF_NUKE:
62     case EF_LOST:
63     case EF_REALM:
64         return gp->own == player->cnum || player->god;
65     case EF_COUNTRY:
66         return gp->own == player->cnum;
67     case EF_NEWS:
68         return !opt_HIDDEN || player->god; /* FIXME */
69     case EF_LOAN:
70         if (lp->l_status == LS_SIGNED)
71             return 1;
72         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
73             || player->god;
74     case EF_SHIP_CHR:
75         tlev = ((struct mchrstr *)p)->m_tech;
76         goto tech;
77     case EF_PLANE_CHR:
78         tlev = ((struct plchrstr *)p)->pl_tech;
79         goto tech;
80     case EF_LAND_CHR:
81         tlev = ((struct lchrstr *)p)->l_tech;
82     tech:
83         natp = getnatp(player->cnum);
84         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
85     case EF_NUKE_CHR:
86         tlev = ((struct nchrstr *)p)->n_tech;
87         if (drnuke_const > MIN_DRNUKE_CONST) {
88             natp = getnatp(player->cnum);
89             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
90                              / drnuke_const))
91                 return player->god;
92         }
93         goto tech;
94     case EF_TABLE:
95         return ((struct empfile *)p)->cadef != NULL;
96     default:
97         return 1;
98     }
99 }
100
101 /*
102  * Dump meta-data for items of type @type to @xd.
103  * Return RET_SYN when @type doesn't have meta-data, else RET_OK.
104  */
105 static int
106 xdmeta(struct xdstr *xd, int type)
107 {
108     struct castr *ca = ef_cadef(type);
109     int i;
110     int n = 0;
111
112     if (!ca)
113         return RET_SYN;
114
115     xdhdr(xd, ef_nameof(type), 1);
116     xdcolhdr(xd, ca);
117
118     for (i = 0; ca[i].ca_name; i++) {
119         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
120             continue;
121         if (ca[i].ca_dump == CA_DUMP_NONE)
122             continue;
123         xdflds(xd, mdchr_ca, &ca[i]);
124         xd->pr("\n");
125         n++;
126     }
127
128     xdftr(xd, n);
129
130     return RET_OK;
131 }
132
133 /*
134  * Dump items of type @type selected by @arg to @xd.
135  * Return RET_OK on success, RET_SYN on error.
136  */
137 static int
138 xditem(struct xdstr *xd, int type, char *arg)
139 {
140     struct castr *ca;
141     struct nstr_item ni;
142     int n;
143     unsigned char buf[EF_WITH_CADEF_MAX_ENTRY_SIZE];
144
145     ca = ef_cadef(type);
146     if (!ca)
147         return RET_SYN;
148
149     if (!snxtitem(&ni, type, arg, NULL))
150         return RET_SYN;
151
152     xdhdr(xd, ef_nameof(type), 0);
153
154     n = 0;
155     while (nxtitem(&ni, buf)) {
156         if (!xdvisible(type, buf))
157             continue;
158         ++n;
159         xdflds(xd, ca, buf);
160         xd->pr("\n");
161     }
162
163     xdftr(xd, n);
164
165     return RET_OK;
166 }
167
168 /* Extended dump command */
169 int
170 xdump(void)
171 {
172     char *p;
173     char buf[1024];
174     struct xdstr xd;
175     struct natstr *natp;
176     int type;
177     int meta = 0;
178
179     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
180     if (p && strcmp(p, "meta") == 0) {
181         meta = 1;
182         p = getstarg(player->argp[2], "Table name? ", buf);
183     }
184     if (!p || !*p)
185         return RET_SYN;
186
187     xdinit(&xd, player->cnum, 0, 0, pr);
188     natp = getnatp(player->cnum);
189     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
190     if (type < 0 || type >= EF_MAX)
191         return RET_SYN;
192     if (meta)
193         return xdmeta(&xd, type);
194     if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
195         && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
196         pr("Access to table %s denied\n", ef_nameof(type));
197         return RET_FAIL;
198     }
199     return xditem(&xd, type, player->argp[2]);
200 }