]> git.pond.sub.org Git - empserver/blob - src/lib/commands/xdump.c
License upgrade to GPL version 3 or later
[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-2008
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 "version.h"
40 #include "xdump.h"
41
42 /*
43  * Is object P of type TYPE visible to the player?
44  * TODO: Fold this into interators.
45  */
46 static int
47 xdvisible(int type, void *p)
48 {
49     struct empobj *gp = p;
50     struct trtstr *tp = p;
51     struct lonstr *lp = p;
52     struct natstr *natp;
53     int tlev;
54
55     switch (type) {
56     case EF_SECTOR:
57     case EF_REALM:
58         return gp->own == player->cnum || player->god;
59     case EF_SHIP:
60     case EF_PLANE:
61     case EF_LAND:
62     case EF_NUKE:
63     case EF_LOST:
64         return gp->own != 0 && (gp->own == player->cnum || player->god);
65     case EF_NATION:
66         return ((struct natstr *)p)->nat_stat != STAT_UNUSED;
67     case EF_COUNTRY:
68         return gp->own == player->cnum;
69     case EF_NEWS:
70         return ((struct nwsstr *)p)->nws_vrb != 0
71             && (!opt_HIDDEN || player->god); /* FIXME */
72     case EF_TREATY:
73         return tp->trt_status != TS_FREE
74             && (tp->trt_cna == player->cnum || tp->trt_cnb == player->cnum
75                 || player->god);
76     case EF_LOAN:
77         if (lp->l_status == LS_FREE)
78             return 0;
79         if (lp->l_status == LS_SIGNED)
80             return 1;
81         return lp->l_loner == player->cnum || lp->l_lonee == player->cnum
82             || player->god;
83     case EF_TRADE:
84     case EF_COMM:
85         return gp->own != 0;
86     case EF_SHIP_CHR:
87         tlev = ((struct mchrstr *)p)->m_tech;
88         goto tech;
89     case EF_PLANE_CHR:
90         tlev = ((struct plchrstr *)p)->pl_tech;
91         goto tech;
92     case EF_LAND_CHR:
93         tlev = ((struct lchrstr *)p)->l_tech;
94     tech:
95         natp = getnatp(player->cnum);
96         return player->god || tlev <= (int)(1.25 * natp->nat_level[NAT_TLEV]);
97     case EF_NUKE_CHR:
98         tlev = ((struct nchrstr *)p)->n_tech;
99         if (drnuke_const > MIN_DRNUKE_CONST) {
100             natp = getnatp(player->cnum);
101             if (tlev > (int)((int)(1.25 * natp->nat_level[NAT_RLEV])
102                              / drnuke_const))
103                 return player->god;
104         }
105         goto tech;
106     case EF_NEWS_CHR:
107         return ((struct rptstr *)p)->r_newspage != 0;
108     case EF_TABLE:
109         return ((struct empfile *)p)->cadef != NULL;
110     default:
111         return 1;
112     }
113 }
114
115 /*
116  * Dump items of type TYPE selected by ARG to XD.
117  * Return RET_OK on success, RET_SYN on error.
118  */
119 static int
120 xditem(struct xdstr *xd, int type, char *arg)
121 {
122     struct castr *ca;
123     struct nstr_item ni;
124     int n;
125     char buf[2048];             /* FIXME buffer size? */
126
127     ca = ef_cadef(type);
128     if (!ca)
129         return RET_SYN;
130
131     if (!snxtitem(&ni, type, arg, NULL))
132         return RET_SYN;
133
134     xdhdr(xd, ef_nameof(type), 0);
135
136     n = 0;
137     while (nxtitem(&ni, buf)) {
138         if (!xdvisible(type, buf))
139             continue;
140         ++n;
141         xdflds(xd, ca, buf);
142         xd->pr("\n");
143     }
144
145     xdftr(xd, n);
146
147     return RET_OK;
148 }
149
150 /* Extended dump command */
151 int
152 xdump(void)
153 {
154     char *p;
155     char buf[1024];
156     struct xdstr xd;
157     struct natstr *natp;
158     int type;
159     int meta = 0;
160
161     p = getstarg(player->argp[1], "Table name, or meta? ", buf);
162     if (p && strcmp(p, "meta") == 0) {
163         meta = 1;
164         p = getstarg(player->argp[2], "Table name? ", buf);
165     }
166     if (!p || !*p)
167         return RET_SYN;
168
169     xdinit(&xd, player->cnum, 0, pr);
170     natp = getnatp(player->cnum);
171     type = isdigit(p[0]) ? atoi(p) : ef_byname(p);
172     if (type < 0 || type >= EF_MAX)
173         return RET_SYN;
174     if (meta)
175         return xdmeta(&xd, type);
176     if ((EF_IS_GAME_STATE(type) || EF_IS_VIEW(type))
177         && !(natp->nat_stat == STAT_ACTIVE || player->god)) {
178         pr("Access to table %s denied\n", ef_nameof(type));
179         return RET_FAIL;
180     }
181     if (type == EF_VERSION && !player->argp[2])
182         return xditem(&xd, type, "*"); /* backward compatibility */
183     return xditem(&xd, type, player->argp[2]);
184 }