]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
dfa7f84090403a6a2e617fe8b1d44e3598bdcdf3
[empserver] / src / lib / commands / xdump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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-2011
31  */
32
33 #include <config.h>
34
35 #include <ctype.h>
36 #include "commands.h"
37 #include "empobj.h"
38 #include "news.h"
39 #include "optlist.h"
40 #include "product.h"
41 #include "version.h"
42 #include "xdump.h"
43
44 /*
45  * Is object P of type TYPE visible to the player?
46  * TODO: Fold this into interators.
47  */
48 static int
49 xdvisible(int type, void *p)
50 {
51     struct empobj *gp = p;
52     struct trtstr *tp = p;
53     struct lonstr *lp = p;
54     struct natstr *natp;
55     int tlev;
56     char *name;
57
58     switch (type) {
59     case EF_SECTOR:
60         return gp->own == player->cnum || player->god;
61     case EF_SHIP:
62     case EF_PLANE:
63     case EF_LAND:
64     case EF_NUKE:
65     case EF_LOST:
66         return gp->own != 0 && (gp->own == player->cnum || player->god);
67     case EF_NATION:
68         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
69     case EF_COUNTRY:
70         return gp->own == player->cnum;
71     case EF_NEWS:
72         return ((struct nwsstr *)p)->nws_vrb != 0
73             && (!opt_HIDDEN || player->god); /* FIXME */
74     case EF_TREATY:
75         return tp->trt_status != TS_FREE
76             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
77                 || player->god);
78     case EF_LOAN:
79         if (lp->l_status == LS_FREE)
80             return 0;
81         if (lp->l_status == LS_SIGNED)
82             return 1;
83         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
84             || player->god;
85     case EF_TRADE:
86     case EF_COMM:
87         return gp->own != 0;
88     case EF_REALM:
89         natp = getnatp(((struct realmstr *)p)->r_cnum);
90         return (gp->own == player->cnum || player->god)
91             && (natp->nat_stat != STAT_UNUSED);
92     case EF_PRODUCT:
93         return ((struct pchrstr *)p)->p_sname[0] != 0;
94     case EF_SHIP_CHR:
95         tlev = ((struct mchrstr *)p)->m_tech;
96         name = ((struct mchrstr *)p)->m_name;
97         goto tech;
98     case EF_PLANE_CHR:
99         tlev = ((struct plchrstr *)p)->pl_tech;
100         name = ((struct plchrstr *)p)->pl_name;
101         goto tech;
102     case EF_LAND_CHR:
103         tlev = ((struct lchrstr *)p)->l_tech;
104         name = ((struct lchrstr *)p)->l_name;
105     tech:
106         natp = getnatp(player->cnum);
107         if (!name[0])
108             return 0;
109         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
110     case EF_NUKE_CHR:
111         tlev = ((struct nchrstr *)p)->n_tech;
112         name = ((struct nchrstr *)p)->n_name;
113         if (drnuke_const > MIN_DRNUKE_CONST) {
114             natp = getnatp(player->cnum);
115             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
116                              / drnuke_const))
117                 return player->god;
118         }
119         goto tech;
120     case EF_NEWS_CHR:
121         return ((struct rptstr *)p)->r_newspage != 0;
122     case EF_TABLE:
123         return ((struct empfile *)p)->cadef != NULL;
124     default:
125         return 1;
126     }
127 }
128
129 /*
130  * Dump meta-data for items of type TYPE to XD.
131  * Return RET_SYN when TYPE doesn't have meta-data, else RET_OK.
132  */
133 static int
134 xdmeta(struct xdstr *xd, int type)
135 {
136     struct castr *ca = ef_cadef(type);
137     int i;
138     int n = 0;
139
140     if (!ca)
141         return RET_SYN;
142
143     xdhdr(xd, ef_nameof(type), 1);
144     xdcolhdr(xd, ca);
145
146     for (i = 0; ca[i].ca_name; i++) {
147         if (ca[i].ca_flags & NSC_DEITY && !xd->divine)
148             continue;
149         if (ca[i].ca_flags & NSC_EXTRA)
150             continue;
151         xdflds(xd, mdchr_ca, &ca[i]);
152         xd->pr("\n");
153         n++;
154     }
155
156     xdftr(xd, n);
157
158     return RET_OK;
159 }
160
161 /*
162  * Dump items of type TYPE selected by ARG to XD.
163  * Return RET_OK on success, RET_SYN on error.
164  */
165 static int
166 xditem(struct xdstr *xd, int type, char *arg)
167 {
168     struct castr *ca;
169     struct nstr_item ni;
170     int n;
171     char buf[2048];             /* FIXME buffer size? */
172
173     ca = ef_cadef(type);
174     if (!ca)
175         return RET_SYN;
176
177     if (!snxtitem(&ni, type, arg, NULL))
178         return RET_SYN;
179
180     xdhdr(xd, ef_nameof(type), 0);
181
182     n = 0;
183     while (nxtitem(&ni, buf)) {
184         if (!xdvisible(type, buf))
185             continue;
186         ++n;
187         xdflds(xd, ca, buf);
188         xd->pr("\n");
189     }
190
191     xdftr(xd, n);
192
193     return RET_OK;
194 }
195
196 /* Extended dump command */
197 int
198 xdump(void)
199 {
200     char *p;
201     char buf[1024];
202     struct xdstr xd;
203     struct natstr *natp;
204     int type;
205     int meta = 0;
206
207     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
208     if (p && strcmp(p, "meta") == 0) {
209         meta = 1;
210         p = getstarg(player->argp[2], "Table name? ", buf);
211     }
212     if (!p || !*p)
213         return RET_SYN;
214
215     xdinit(&xd, player->cnum, 0, pr);
216     natp = getnatp(player->cnum);
217     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
218     if (type < 0 || type >= EF_MAX)
219         return RET_SYN;
220     if (meta)
221         return xdmeta(&xd, type);
222     if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
223         && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
224         pr("Access to table %s denied\n", ef_nameof(type));
225         return RET_FAIL;
226     }
227     return xditem(&xd, type, player->argp[2]);
228 }