blob: f9dc05e992c09c899e2d75e31948af5603e6c013 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// simul_efun.c -- simul efun's
4//
5// $Id: simul_efun.c 7408 2010-02-06 00:27:25Z Zesstra $
Zesstra4dbb9882019-11-26 21:26:36 +01006#pragma strong_types,save_types,rtt_checks
MG Mud User88f12472016-06-24 23:31:02 +02007#pragma no_clone,no_shadow,no_inherit
Zesstra4dbb9882019-11-26 21:26:36 +01008#pragma range_check,warn_deprecated
MG Mud User88f12472016-06-24 23:31:02 +02009#pragma warn_empty_casts,warn_missing_return,warn_function_inconsistent
10
11// Absolute Pfade erforderlich - zum Zeitpunkt, wo der Master geladen
12// wird, sind noch keine Include-Pfade da ...
13
14#define SNOOPLOGFILE "SNOOP"
15#define ASNOOPLOGFILE "ARCH/SNOOP"
16
Zesstraf1673762018-09-05 16:41:34 +020017public int mkdirp(string dir);
18
MG Mud User88f12472016-06-24 23:31:02 +020019#include "/secure/config.h"
20#include "/secure/wizlevels.h"
21#include "/sys/snooping.h"
22#include "/sys/language.h"
23#include "/sys/thing/properties.h"
24#include "/sys/wizlist.h"
25#include "/sys/erq.h"
26#include "/sys/lpctypes.h"
27#include "/sys/daemon.h"
28#include "/sys/player/base.h"
29#include "/sys/thing/description.h"
30#include "/sys/container.h"
31#include "/sys/defines.h"
32#include "/sys/telnet.h"
33#include "/sys/objectinfo.h"
34#include "/sys/files.h"
35#include "/sys/strings.h"
36#include "/sys/time.h"
37#include "/sys/lpctypes.h"
38#include "/sys/notify_fail.h"
39#include "/sys/tls.h"
40#include "/sys/input_to.h"
41#include "/sys/object_info.h"
42
43/* function prototypes
44 */
45string dtime(int wann);
46varargs int log_file(string file, string txt, int size_to_break);
47int query_wiz_level(mixed player);
Zesstra8d245512020-04-28 20:35:34 +020048nomask varargs int snoop(object snooper, object snoopee);
MG Mud User88f12472016-06-24 23:31:02 +020049varargs string country(mixed ip, string num);
50int query_wiz_grp(mixed wiz);
51public varargs object deep_present(mixed what, object ob);
52nomask int secure_level();
53nomask string secure_euid();
54public nomask int process_call();
55nomask mixed __create_player_dummy(string name);
Zesstra953b7ea2019-11-28 19:29:37 +010056varargs string replace_personal(string str, <string|object>* obs, int caps);
MG Mud User88f12472016-06-24 23:31:02 +020057
58//replacements for dropped efuns in LD
59#if !__EFUN_DEFINED__(extract)
60varargs string extract(string str, int from, int to);
61#endif
62#if !__EFUN_DEFINED__(slice_array)
63varargs mixed slice_array(mixed array, int from, int to);
64#endif
65#if !__EFUN_DEFINED__(member_array)
66int member_array(mixed item, mixed arraystring);
67#endif
68
69// Include the different sub 'modules' of the simul_efun
70#include __DIR__"debug_info.c"
71#include __DIR__"enable_commands.c"
72#include __DIR__"hash.c"
73#include __DIR__"object_info.c"
74#include __DIR__"query_editing.c"
75#include __DIR__"query_idle.c"
76#include __DIR__"query_input_pending.c"
77#include __DIR__"query_ip_name.c"
78#include __DIR__"query_limits.c"
79#include __DIR__"query_load_average.c"
80#include __DIR__"query_mud_port.c"
81#include __DIR__"query_once_interactive.c"
82#include __DIR__"query_snoop.c"
83#include __DIR__"set_heart_beat.c"
84#if __BOOT_TIME__ < 1456261859
85#include __DIR__"set_prompt.c"
86#endif
87#include __DIR__"shadow.c"
88#include __DIR__"livings.c"
89#include __DIR__"comm.c"
Zesstra984bde92017-01-29 22:19:52 +010090#include __DIR__"files.c"
Zesstra2ea5d3e2017-06-20 22:03:32 +020091#include __DIR__"seteuid.c"
MG Mud User88f12472016-06-24 23:31:02 +020092
93#define TO efun::this_object()
94#define TI efun::this_interactive()
95#define TP efun::this_player()
96#define PO efun::previous_object(0)
97#define LEVEL(x) query_wiz_level(x)
98#define NAME(x) capitalize(getuid(x))
99
100#define DEBUG(x) if (find_player("zesstra")) \
101 tell_object(find_player("zesstra"),x)
102
103mixed dtime_cache = ({-1,""});
104
105#ifdef IOSTATS
106struct iostat_s {
107 string oname;
108 int time;
109 int size;
110};
111mixed saveo_stat = ({ 0,allocate(200, 0) });
112mixed restoreo_stat = ({ 0,allocate(200,0) });
113//mixed writefile_stat = ({ 0,allocate(100,(<iostat_s>)) });
114//mixed readfile_stat = ({ 0,allocate(100,(<iostat_s>)) });
115//mixed log_stat = ({ 0,allocate(100,(<iostat_s>)) });
116
117mixed ___iostats(int type) {
118 switch(type) {
119 case 1:
120 return saveo_stat;
121 case 2:
122 return restoreo_stat;
123/* case 3:
124 return writefile_stat;
125 case 4:
126 return readfile_stat;
127 case 5:
128 return log_stat;
129 */
130 }
131 return 0;
132}
133#endif
134
135// Nicht jeder Magier muss die simul_efun entsorgen koennen.
136string NotifyDestruct(object caller) {
137 if( (caller!=this_object() && !ARCH_SECURITY) || process_call() ) {
138 return "Du darfst das simul_efun Objekt nicht zerstoeren!\n";
139 }
140 return 0;
141}
142
143public nomask void remove_interactive( object ob )
144{
145 if ( objectp(ob) && previous_object()
146 && object_name(previous_object())[0..7] != "/secure/"
147 && ((previous_object() != ob
148 && (ob != this_player() || ob != this_interactive()))
149 || (previous_object() == ob
150 && (this_player() && this_player() != ob
151 || this_interactive() && this_interactive() != ob)) ) )
152
153 log_file( "PLAYERDEST",
154 sprintf( "%s: %O ausgeloggt von PO %O, TI %O, TP %O\n",
155 dtime(time()), ob, previous_object(),
156 this_interactive(), this_player() ) );
157
158 efun::remove_interactive(ob);
159}
160
161
162void ___updmaster()
163{
164 object ob;
165
166 //sollte nicht jeder duerfen.
167 if (process_call() || !ARCH_SECURITY)
168 raise_error("Illegal use of ___updmaster()!");
169
170 write("Removing old master ... ");
171 foreach(string file:
172 get_dir("/secure/master/*.c",GETDIR_NAMES|GETDIR_UNSORTED|GETDIR_PATH)) {
173 if (ob = find_object(file))
174 efun::destruct(ob);
175 }
176 efun::destruct(efun::find_object("/secure/master"));
177 write("done.\nLoading again ... ");
178 load_object("/secure/master");
179
180 write("done.\n");
181}
182
183varargs string country(mixed ip, string num) {
184 mixed ret;
185
Zesstra4dbb9882019-11-26 21:26:36 +0100186 if(ret = "/p/daemon/iplookup"->country(num || ip)) {
MG Mud User88f12472016-06-24 23:31:02 +0200187 return ret;
188 } else return "???";
189}
190
191
192// * Snoopen und was dazugehoert
MG Mud User88f12472016-06-24 23:31:02 +0200193
MG Mud User88f12472016-06-24 23:31:02 +0200194private string Lcut(string str) {
195 return str[5..11]+str[18..];
196}
197
Zesstra8d245512020-04-28 20:35:34 +0200198nomask varargs int snoop( object snooper, object snoopee )
MG Mud User88f12472016-06-24 23:31:02 +0200199{
200 int ret;
MG Mud User88f12472016-06-24 23:31:02 +0200201
Zesstra8d245512020-04-28 20:35:34 +0200202 if( !objectp(snooper) || snooper == snoopee || !PO )
MG Mud User88f12472016-06-24 23:31:02 +0200203 return 0;
204
Zesstra2e499d62020-04-28 22:13:53 +0200205 // Evtl. gibt es bereits einen snoopee, der von snopper gesnoopt wird?
Zesstra69174b42020-04-28 22:48:45 +0200206 object existing_snoopee = efun::interactive_info(snooper, II_SNOOP_PREV);
MG Mud User88f12472016-06-24 23:31:02 +0200207
Zesstra69174b42020-04-28 22:48:45 +0200208 // soll jemand neues gesnoopt werden?
Zesstra8d245512020-04-28 20:35:34 +0200209 if(snoopee)
210 {
Zesstra69174b42020-04-28 22:48:45 +0200211 // Jemand mit niedrigerem Level kann keinen hoeherleveligen snoopen
212 // lassen.
Zesstra8d245512020-04-28 20:35:34 +0200213 if ( PO != snooper
214 && query_wiz_grp(snooper) >= query_wiz_grp(geteuid(PO)) )
MG Mud User88f12472016-06-24 23:31:02 +0200215 return 0;
216
Zesstra69174b42020-04-28 22:48:45 +0200217 // Niedriglevelige User koennen nur mit Einverstaendnis hoeherlevelige
218 // snoopen.
219 if ( query_wiz_grp(snooper) <= query_wiz_grp(snoopee)
220 && !(snoopee->QueryAllowSnoop(snooper)) )
Zesstra6c333cc2020-04-28 20:18:45 +0200221 {
Zesstra69174b42020-04-28 22:48:45 +0200222 // es sei denn der snooper ist Sheriff und der snoopee ist kein
223 // EM+
Zesstra8d245512020-04-28 20:35:34 +0200224 if ( !IS_DEPUTY(snooper) || IS_ARCH(snoopee) )
MG Mud User88f12472016-06-24 23:31:02 +0200225 return 0;
Zesstra6c333cc2020-04-28 20:18:45 +0200226 }
Zesstra69174b42020-04-28 22:48:45 +0200227 // Wird der snoopee bereits gesnoopt? Dann darf sich der neue snooper
228 // nur unter Umstaenden in die Snoop-Kette einreihen...
229 object existing_snooper;
230 if ( (existing_snooper = efun::interactive_info(snoopee, II_SNOOP_NEXT))
231 && query_wiz_grp(existing_snooper) >= query_wiz_grp(snooper) )
Zesstra6c333cc2020-04-28 20:18:45 +0200232 {
Zesstra69174b42020-04-28 22:48:45 +0200233 // ... naemlich nur dann, wenn der bestehende Snooper kein
234 // SF_LOCKED gesetzt hat.
235 if ( existing_snooper->QueryProp(P_SNOOPFLAGS) & SF_LOCKED )
MG Mud User88f12472016-06-24 23:31:02 +0200236 return 0;
237
Zesstra69174b42020-04-28 22:48:45 +0200238 tell_object( existing_snooper, sprintf( "%s snooped jetzt %s.\n",
Zesstra8d245512020-04-28 20:35:34 +0200239 snooper->name(WER), snoopee->name(WER) ) );
MG Mud User88f12472016-06-24 23:31:02 +0200240
Zesstra69174b42020-04-28 22:48:45 +0200241 // Evtl. wird der neue snooper selber gesnoopt. Dafuer wird jetzt
242 // ggf. die Kette von *ihren* snoopern verfolgt.
243 object snooper_of_new_snooper = snooper;
244 object snooper_rover;
245 while ( snooper_rover = interactive_info(snooper_of_new_snooper, II_SNOOP_NEXT) )
Zesstra6c333cc2020-04-28 20:18:45 +0200246 {
Zesstra69174b42020-04-28 22:48:45 +0200247 tell_object( existing_snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200248 sprintf( "%s wird seinerseits von %s gesnooped.\n"
Zesstra69174b42020-04-28 22:48:45 +0200249 ,snooper_of_new_snooper->name(WER),
250 snooper_rover->name(WEM) ) );
251 snooper_of_new_snooper = snooper_rover;
MG Mud User88f12472016-06-24 23:31:02 +0200252 }
253
Zesstra69174b42020-04-28 22:48:45 +0200254 // Der letzt snooper des hier anzumeldenden snoopers wird nun vom
255 // bestehenden snooper gesnoopt, falls moeglich.
256 efun::snoop( existing_snooper, snooper_of_new_snooper );
MG Mud User88f12472016-06-24 23:31:02 +0200257
Zesstra69174b42020-04-28 22:48:45 +0200258 if ( efun::interactive_info(snooper_of_new_snooper, II_SNOOP_NEXT)
259 != existing_snooper )
260 tell_object( existing_snooper, sprintf( "Du kannst %s nicht snoopen.\n",
261 snooper_of_new_snooper->name(WEN) ) );
Zesstra6c333cc2020-04-28 20:18:45 +0200262 else
263 {
Zesstra69174b42020-04-28 22:48:45 +0200264 tell_object( existing_snooper, sprintf( "Du snoopst jetzt %s.\n",
265 snooper_of_new_snooper->name(WEN) ) );
266 if ( !IS_DEPUTY(existing_snooper) )
Zesstra6c333cc2020-04-28 20:18:45 +0200267 {
MG Mud User88f12472016-06-24 23:31:02 +0200268 log_file( SNOOPLOGFILE, sprintf("%s: %O %O %O\n",
269 dtime(time()),
Zesstra69174b42020-04-28 22:48:45 +0200270 existing_snooper,
271 snooper_of_new_snooper,
272 environment(snooper_of_new_snooper) ),
MG Mud User88f12472016-06-24 23:31:02 +0200273 100000 );
Zesstra69174b42020-04-28 22:48:45 +0200274 if (existing_snoopee)
275 CHMASTER->send( "Snoop", existing_snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200276 sprintf( "%s *OFF* %s (%O)",
Zesstra69174b42020-04-28 22:48:45 +0200277 capitalize(getuid(existing_snooper)),
278 capitalize(getuid(existing_snoopee)),
279 environment(existing_snoopee) ) );
MG Mud User88f12472016-06-24 23:31:02 +0200280
Zesstra69174b42020-04-28 22:48:45 +0200281 CHMASTER->send( "Snoop", existing_snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200282 sprintf("%s -> %s (%O)",
Zesstra69174b42020-04-28 22:48:45 +0200283 capitalize(getuid(existing_snooper)),
284 capitalize(getuid(snooper_of_new_snooper)),
285 environment(snooper_of_new_snooper)));
MG Mud User88f12472016-06-24 23:31:02 +0200286 }
Zesstra6c333cc2020-04-28 20:18:45 +0200287 else
288 {
MG Mud User88f12472016-06-24 23:31:02 +0200289 log_file( ASNOOPLOGFILE, sprintf( "%s: %O %O %O\n",
290 dtime(time()),
Zesstra69174b42020-04-28 22:48:45 +0200291 existing_snooper,
292 snooper_of_new_snooper,
293 environment(snooper_of_new_snooper) )
MG Mud User88f12472016-06-24 23:31:02 +0200294 ,100000 );
295 }
296 }
297 }
298 else
Zesstra6c333cc2020-04-28 20:18:45 +0200299 {
Zesstra69174b42020-04-28 22:48:45 +0200300 if (existing_snooper)
Zesstra6c333cc2020-04-28 20:18:45 +0200301 {
Zesstra8d245512020-04-28 20:35:34 +0200302 if ( !snooper->QueryProp(P_SNOOPFLAGS) & SF_LOCKED )
Zesstra6c333cc2020-04-28 20:18:45 +0200303 {
MG Mud User88f12472016-06-24 23:31:02 +0200304 printf( "%s wird bereits von %s gesnooped. Benutze das "
305 "\"f\"-Flag, wenn du dennoch snoopen willst.\n",
Zesstra69174b42020-04-28 22:48:45 +0200306 snoopee->name(WER), existing_snooper->name(WEM) );
MG Mud User88f12472016-06-24 23:31:02 +0200307 return 0;
308 }
Zesstra6c333cc2020-04-28 20:18:45 +0200309 }
310 }
Zesstra8d245512020-04-28 20:35:34 +0200311 ret = efun::snoop( snooper, snoopee );
MG Mud User88f12472016-06-24 23:31:02 +0200312
Zesstra8d245512020-04-28 20:35:34 +0200313 if ( !IS_DEPUTY(snooper)
314 && efun::interactive_info(snoopee, II_SNOOP_NEXT) == snooper)
Zesstra6c333cc2020-04-28 20:18:45 +0200315 {
MG Mud User88f12472016-06-24 23:31:02 +0200316 log_file( SNOOPLOGFILE, sprintf( "%s: %O %O %O\n",
317 Lcut(dtime(time())),
Zesstra8d245512020-04-28 20:35:34 +0200318 snooper, snoopee, environment(snoopee) ),
MG Mud User88f12472016-06-24 23:31:02 +0200319 100000 );
320
Zesstra69174b42020-04-28 22:48:45 +0200321 if (existing_snoopee)
Zesstra6c333cc2020-04-28 20:18:45 +0200322 {
Zesstra8d245512020-04-28 20:35:34 +0200323 CHMASTER->send( "Snoop", snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200324 sprintf( "%s *OFF* %s (%O).",
Zesstra8d245512020-04-28 20:35:34 +0200325 capitalize(getuid(snooper)),
Zesstra69174b42020-04-28 22:48:45 +0200326 capitalize(getuid(existing_snoopee)),
327 environment(existing_snoopee) ) );
Zesstra6c333cc2020-04-28 20:18:45 +0200328 }
MG Mud User88f12472016-06-24 23:31:02 +0200329
Zesstra8d245512020-04-28 20:35:34 +0200330 CHMASTER->send( "Snoop", snooper, sprintf( "%s -> %s (%O).",
331 capitalize(getuid(snooper)),
332 capitalize(getuid(snoopee)),
333 environment(snoopee) ) );
MG Mud User88f12472016-06-24 23:31:02 +0200334 }
Zesstra6c333cc2020-04-28 20:18:45 +0200335 else
336 {
Zesstra8d245512020-04-28 20:35:34 +0200337 if ( efun::interactive_info(snoopee, II_SNOOP_NEXT) == snooper )
Zesstra6c333cc2020-04-28 20:18:45 +0200338 {
MG Mud User88f12472016-06-24 23:31:02 +0200339 log_file( ASNOOPLOGFILE, sprintf( "%s: %O %O %O\n",
340 Lcut(dtime(time())),
Zesstra8d245512020-04-28 20:35:34 +0200341 snooper, snoopee, environment(snoopee) ),
MG Mud User88f12472016-06-24 23:31:02 +0200342 100000 );
343 }
344 }
345
Zesstra8d245512020-04-28 20:35:34 +0200346 if ( ret && query_wiz_grp(snooper) <= query_wiz_grp(snoopee) &&
347 !IS_DEPUTY(snooper) )
348 tell_object( snoopee, "*** " + NAME(snooper) + " snoopt Dich!\n" );
MG Mud User88f12472016-06-24 23:31:02 +0200349
350 return ret;
351 }
Zesstra69174b42020-04-28 22:48:45 +0200352 // Ansonsten soll ein bestehender snoop beendet werden.
Zesstra6c333cc2020-04-28 20:18:45 +0200353 else
354 {
Zesstra69174b42020-04-28 22:48:45 +0200355 // Das beenden duerfen aber nur Aufrufer selber oder hoeherlevelige
356 // ausloesen oder gleichen levels, wenn sie selber gerade vom snooper
357 // gesnoopt werden.
Zesstra8d245512020-04-28 20:35:34 +0200358 if ( (snooper == PO ||
359 query_wiz_grp(geteuid(PO)) > query_wiz_grp(snooper) ||
360 (query_wiz_grp(geteuid(PO)) == query_wiz_grp(snooper) &&
Zesstra69174b42020-04-28 22:48:45 +0200361 efun::interactive_info(PO, II_SNOOP_NEXT) == snooper) )
362 && existing_snoopee )
Zesstra6c333cc2020-04-28 20:18:45 +0200363 {
Zesstra8d245512020-04-28 20:35:34 +0200364 if ( !IS_DEPUTY(snooper) )
Zesstra6c333cc2020-04-28 20:18:45 +0200365 {
MG Mud User88f12472016-06-24 23:31:02 +0200366 log_file( SNOOPLOGFILE, sprintf( "%s: %O %O %O *OFF*\n",
Zesstra8d245512020-04-28 20:35:34 +0200367 Lcut(dtime(time())), snooper,
Zesstra69174b42020-04-28 22:48:45 +0200368 existing_snoopee,
369 environment(existing_snoopee) ),
MG Mud User88f12472016-06-24 23:31:02 +0200370 100000 );
371
Zesstra8d245512020-04-28 20:35:34 +0200372 CHMASTER->send( "Snoop", snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200373 sprintf( "%s *OFF* %s (%O).",
Zesstra8d245512020-04-28 20:35:34 +0200374 capitalize(getuid(snooper)),
Zesstra69174b42020-04-28 22:48:45 +0200375 capitalize(getuid(existing_snoopee)),
376 environment(existing_snoopee) ) );
MG Mud User88f12472016-06-24 23:31:02 +0200377 }
Zesstra6c333cc2020-04-28 20:18:45 +0200378 else
379 {
MG Mud User88f12472016-06-24 23:31:02 +0200380 log_file( ASNOOPLOGFILE, sprintf( "%s: %O %O %O *OFF*\n",
Zesstra8d245512020-04-28 20:35:34 +0200381 Lcut(dtime(time())), snooper,
Zesstra69174b42020-04-28 22:48:45 +0200382 existing_snoopee,
383 environment(existing_snoopee) ),
MG Mud User88f12472016-06-24 23:31:02 +0200384 100000 );
385 }
Zesstra8d245512020-04-28 20:35:34 +0200386 return efun::snoop(snooper);
MG Mud User88f12472016-06-24 23:31:02 +0200387 }
388 }
389 return 0;
390}
391
392
393
394// * Emulation des 'alten' explode durch das neue
395string *old_explode(string str, string del) {
396 int s, t;
397 string *strs;
398
399 if (!stringp(str)) {
400 set_this_object(previous_object());
401 raise_error(sprintf(
402 "Invalid argument 1 to old_explode()! Expected <string>, got: "
403 "%.30O\n",str));
404 }
405 if (!stringp(del)) {
406 set_this_object(previous_object());
407 raise_error(sprintf(
408 "Invalid argument 2 to old_explode()! Expected <string>, got: "
409 "%.30O\n",del));
410 }
411 if(del == "")
412 return ({str});
413 strs=efun::explode(str, del);
414 t=sizeof(strs)-1;
415 while(s<=t && strs[s++] == "");s--;
416 while(t>=0 && strs[t--] == "");t++;
417 if(s<=t)
418 return strs[s..t];
419 return ({});
420}
421
422int file_time(string path) {
423 mixed *v;
424
425 set_this_object(previous_object());
426 if (sizeof(v=get_dir(path,GETDIR_DATES))) return v[0];
427 return(0); //sonst
428}
429
MG Mud User88f12472016-06-24 23:31:02 +0200430// * Magier-Level abfragen
431int query_wiz_level(mixed player) {
Zesstra4dbb9882019-11-26 21:26:36 +0100432 return "/secure/master"->query_wiz_level(player);
MG Mud User88f12472016-06-24 23:31:02 +0200433}
434
435#ifdef __ALISTS__
436// * Element aus Alist loeschen (by key)
437mixed *remove_alist(mixed key,mixed *alist)
438{
439 int i,j;
440
441 if (!pointerp(alist) || !sizeof(alist))
442 return alist;
443 if (!pointerp(alist[0]))
444 {
445 if ((i=assoc(key,alist))<0)
446 return alist;
447 return alist[0..i-1]+alist[i+1..];
448 }
449 i = assoc(key,alist[0]);
450 if ((i=assoc(key,alist[0]))<0)
451 return alist;
452 alist=alist[0..];
453 for (j=sizeof(alist)-1;j>=0;j--)
454 alist[j]=alist[j][0..i-1]+alist[j][i+1..];
455 return alist;
456}
457
458// * Element aus Alist loeschen (by pos)
459mixed *exclude_alist(int i,mixed *alist)
460{
461 int j;
462 if (!pointerp(alist) || !sizeof(alist) || i<0)
463 return alist;
464 if (!pointerp(alist[0]))
465 return alist[0..i-1]+alist[i+1..];
466 alist=alist[0..]; /* Create PHYSICAL copy of alist */
467 for (j=sizeof(alist)-1;j>=0;j--)
468 alist[j]=alist[j][0..i-1]+alist[j][i+1..];
469 return alist; /* order_alist is NOT necessary - see /doc/LPC/alist */
470}
471#endif // __ALISTS__
472
473// * German version of ctime()
474#define TAGE ({"Son","Mon","Die","Mit","Don","Fre","Sam"})
475#define MONATE ({"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug",\
476 "Sep","Okt","Nov","Dez"})
477string dtime(int wann) {
478
479 if (wann == dtime_cache[0])
480 return(dtime_cache[1]);
481
482 int *lt = localtime(wann);
483 return sprintf("%s, %2d. %s %d, %02d:%02d:%02d",
484 TAGE[lt[TM_WDAY]], lt[TM_MDAY], MONATE[lt[TM_MON]],
485 lt[TM_YEAR],lt[TM_HOUR], lt[TM_MIN], lt[TM_SEC]);
486}
487
488// wenn strftime im Driver nicht existiert, ist dies hier ein Alias auf dtime(),
489// zwar stimmt das Format dann nicht, aber die Mudlib buggt nicht und schreibt
490// ein ordentliches Datum/Uhrzeit.
491#if !__EFUN_DEFINED__(strftime)
492varargs string strftime(mixed fmt, int clock, int localized) {
493 if (intp(clock) && clock >= 0)
494 return dtime(clock);
495 else if (intp(fmt) && fmt >= 0)
496 return dtime(fmt);
497
498 return dtime(time());
499}
500#endif //!__EFUN_DEFINED__(strftime)
501
502// * Shutdown mit zusaetzlichem logging
503nomask int shutdown(string reason)
504{
505 string name;
506 string obname;
507 string output;
508
509 if (!reason)
510 return 0;
511 if ( !ARCH_SECURITY && getuid(previous_object())!=ROOTID &&
512 object_name(previous_object())!="/obj/shut" )
513 {
514 write("You have no permission to shut down the gamedriver!\n");
515 return 0;
516 }
517 if ((this_interactive())&&(name=getuid(this_interactive())))
518 {
519 name=capitalize(name);
520 filter(users(),#'tell_object,//'
521 capitalize(name)+" faehrt das Spiel herunter!\n");
522 }
523 else
524 name="ANONYMOUS";
525 if (previous_object()) obname=capitalize(getuid(previous_object()));
526 output=name;
527 if (obname && name!=obname) output=output+" ("+obname+")";
528 if (previous_object()&&object_name(previous_object())=="/obj/shut"){
529 output+=" faehrt das Spiel via Armageddon herunter.\n";
530 output=dtime(time())+": "+output;
531 log_file("GAME_LOG",output+"\n",-1);
532 efun::shutdown();
533 return 1;
534 }
535 output=ctime(time())+": "+output+" faehrt das Spiel herunter.\n";
536 output+=" Grund: "+reason;
537 log_file("GAME_LOG",output+"\n",-1);
538 efun::shutdown();
539 return 1;
540}
541
542// * lowerchar
543
544int lowerchar(int char) {
545 if (char<'A' || char>'Z') return char;
546 return char+32;
547}
548
549// * upperstring
550
551string upperstring(string s)
552{
553#if __EFUN_DEFINED__(upper_case)
554 return(upper_case(s));
555#else
556 int i;
557 if (!stringp(s)) return 0;
558 for (i=sizeof(s)-1;i>=0;i--) s[i]=((s[i]<'a'||s[i]>'z')?s[i]:s[i]-32);
559 return s;
560#endif
561}
562
563// * lowerstring
564
565string lowerstring(string s)
566{
567 if (!stringp(s)) return 0;
568 return lower_case(s);
569}
570
571
572// * GD version
573string version()
574{
575 return __VERSION__;
576}
577
578// * break_string
579// stretch() -- stretch a line to fill a given width
580private string stretch(string s, int width) {
581 int len=sizeof(s);
582 if (len==width) return s;
583
584 // reine Leerzeilen, direkt zurueckgeben
585 string trimmed=trim(s,TRIM_LEFT," ");
586 if (trimmed=="") return s;
587 int start_spaces = len - sizeof(trimmed);
588
589 string* words = explode(trimmed, " ");
590 // der letzte kriegt keine Spaces
591 int word_count=sizeof(words) - 1;
592 // wenn Zeile nur aus einem Wort, wird das Wort zurueckgegeben
593 if (!word_count)
594 return " "*start_spaces + words[0];
595
596 int space_count = width - len;
597
598 int space_per_word=(word_count+space_count) / word_count;
599 // Anz.Woerter mit Zusatz-Space
600 int rest=(word_count+space_count) % word_count;
601 // Rest-Spaces Verteilen
602 foreach (int pos : rest) words[pos]+=" ";
603 return (" "*start_spaces) + implode( words, " "*space_per_word );
604}
605
606// aus Geschwindigkeitsgruenden hat der Blocksatz fuer break_string eine
607// eigene Funktion bekommen:
608private varargs string block_string(string s, int width, int flags) {
609 // wenn BS_LEAVE_MY_LFS, aber kein BS_NO_PARINDENT, dann werden Zeilen mit
610 // einem Leerzeichen begonnen.
611 // BTW: Wenn !BS_LEAVE_MY_LFS, hat der Aufrufer bereits alle \n durch " "
612 // ersetzt.
613 if ( (flags & BS_LEAVE_MY_LFS)
614 && !(flags & BS_NO_PARINDENT))
615 {
616 s = " "+regreplace(s,"\n","\n ",1);
617 }
618
619 // sprintf fuellt die letzte Zeile auf die Feldbreite (hier also
620 // Zeilenbreite) mit Fuellzeichen auf, wenn sie NICHT mit \n umgebrochen
621 // ist. Es wird an die letzte Zeile aber kein Zeilenumbruch angehaengt.
622 // Eigentlich ist das Auffuellen doof, aber vermutlich ist es unnoetig, es
623 // wieder rueckgaengig zu machen.
624 s = sprintf( "%-*=s", width, s);
625
626 string *tmp=explode(s, "\n");
627 // Nur wenn s mehrzeilig ist, Blocksatz draus machen. Die letzte Zeile wird
628 // natuerlich nicht gedehnt. Sie ist dafuer schon von sprintf() aufgefuellt
629 // worden. BTW: Die letzte Zeile endet u.U. noch nicht mit einem \n (bzw.
630 // nur dann, wenn BS_LEAVE_MY_LFS und der uebergebene Text schon nen \n am
631 // Ende der letzten Zeile hat), das macht der Aufrufer...
632 if (sizeof(tmp) > 1)
633 return implode( map( tmp[0..<2], #'stretch/*'*/, width ), "\n" )
634 + "\n" + tmp[<1];
635
636 return s;
637}
638
639public varargs string break_string(string s, int w, mixed indent, int flags)
640{
641 if ( !s || s == "" ) return "";
642
643 if ( !w ) w=78;
644
645 if( intp(indent) )
646 indent = indent ? " "*indent : "";
647
648 int indentlen=stringp(indent) ? sizeof(indent) : 0;
649
650 if (indentlen>w) {
651 set_this_object(previous_object());
652 raise_error(sprintf("break_string: indent longer %d than width %d\n",
653 indentlen,w));
654 // w=((indentlen/w)+1)*w;
655 }
656
657 if (!(flags & BS_LEAVE_MY_LFS))
658 s=regreplace( s, "\n", " ", 1 );
659
660 if ( flags & BS_SINGLE_SPACE )
661 s = regreplace( s, "(^|\n| ) *", "\\1", 1 );
662
663 string prefix="";
664 if (indentlen && flags & BS_PREPEND_INDENT) {
665 if (indentlen+sizeof(s) > w ||
666 (flags & BS_LEAVE_MY_LFS) && strstr(s,"\n")>-1) {
667 prefix=indent+"\n";
668 indent=(flags & BS_NO_PARINDENT) ? "" : " ";
669 indentlen=sizeof(indent);
670 }
671 }
672
673 if ( flags & BS_BLOCK ) {
674 /*
675 s = implode( map( explode( s, "\n" ),
676 #'block_string, w, indentlen, flags),
677 "" );
678 */
679 s = block_string( s , w - indentlen, flags );
680 }
681 else {
682 s = sprintf("%-1.*=s",w-indentlen,s);
683 }
684 if ( s[<1] != '\n' ) s += "\n";
685
686 if ( !indentlen ) return prefix + s;
687
688 string indent2 = ( flags & BS_INDENT_ONCE ) ? (" "*indentlen) :indent;
689
690 return prefix + indent +
691 regreplace( s[0..<2], "\n", "\n"+indent2, 1 ) + "\n";
692 /*
693 string *buf;
694
695 buf = explode( s, "\n" );
696 return prefix + indent + implode( buf[0..<2], "\n"+indent2 ) + buf[<1] + "\n";
697 */
698}
699
700// * Elemente aus mapping loeschen - mapping vorher kopieren
701
702mapping m_copy_delete(mapping m, mixed key) {
703 return m_delete(copy(m), key);
704}
705
706// * times
707int last_reboot_time()
708{
709 return __BOOT_TIME__;
710}
711
712int first_boot_time()
713{
714 return 701517600;
715}
716
717int exist_days()
718{
719 return (((time()-first_boot_time())/8640)+5)/10;
720}
721
722// * uptime :)
723string uptime()
724{
725 int t;
726 int tmp;
727 string s;
728
729 t=time()-__BOOT_TIME__;
730 s="";
731 if (t>=86400)
732 s+=sprintf("%d Tag%s, ",tmp=t/86400,(tmp==1?"":"e"));
733 if (t>=3600)
734 s+=sprintf("%d Stunde%s, ",tmp=(t=t%86400)/3600,(tmp==1?"":"n"));
735 if (t>60)
736 s+=sprintf("%d Minute%s und ",tmp=(t=t%3600)/60,(tmp==1?"":"n"));
737 return s+sprintf("%d Sekunde%s",t=t%60,(t==1?"":"n"));
738}
739
740// * Was tun bei 'dangling' lfun-closures ?
741void dangling_lfun_closure() {
742 raise_error("dangling lfun closure\n");
743}
744
745// * Sperren ausser fuer master/simul_efun
746
747#if __EFUN_DEFINED__(set_environment)
748nomask void set_environment(object o1, object o2) {
749 raise_error("Available only for root\n");
750}
751#endif
752
753nomask void set_this_player(object pl) {
754 raise_error("Available only for root\n");
755}
756
757#if __EFUN_DEFINED__(export_uid)
758nomask void export_uid(object ob) {
759 raise_error("Available only for root\n");
760}
761#endif
762
763// * Jetzt auch closures
764int process_flag;
765
766public nomask int process_call()
767{
768 if (process_flag>0)
769 return process_flag;
770 else return(0);
771}
772
773private nomask string _process_string(string str,object po) {
774 set_this_object(po);
775 return(efun::process_string(str));
776}
777
778nomask string process_string( mixed str )
779{
780 string tmp, err;
781 int flag;
782
783 if ( closurep(str) ) {
784 set_this_object( previous_object() );
785 return funcall(str);
786 }
787 else if (str==0)
Zesstra4dbb9882019-11-26 21:26:36 +0100788 return(str);
MG Mud User88f12472016-06-24 23:31:02 +0200789 else if ( !stringp(str) ) {
790 return to_string(str);
791 }
792
793 if ( !(flag = process_flag > time() - 60))
794 process_flag=time();
795
796 err = catch(tmp = funcall(#'_process_string,str,previous_object()); publish);
797
798 if ( !flag )
799 process_flag=0;
800
801 if (err) {
802 // Verarbeitung abbrechen
803 set_this_object(previous_object());
804 raise_error(err);
805 }
806 return tmp;
807}
808
809// 'mkdir -p' - erzeugt eine komplette Hierarchie von Verzeichnissen.
810// wenn das Verzeichnis angelegt wurde oder schon existiert, wird 1
811// zurueckgeliefert, sonst 0.
812// Wirft einen Fehler, wenn das angebene Verzeichnis nicht absolut ist!
813public int mkdirp(string dir) {
814 // wenn es nur keinen fuehrenden / gibt, ist das ein Fehler.
815 if (strstr(dir, "/") != 0)
816 raise_error("mkdirp(): Pfad ist nicht absolute.\n");
817 // cut off trailing /...
818 if (dir[<1]=='/')
819 dir = dir[0..<2];
820
821 int fstat = file_size(dir);
822 // wenn es schon existiert, tun wir einfach so, als haetten wir es angelegt.
823 if (fstat == FSIZE_DIR)
824 return 1;
825 // wenn schon ne Datei existiert, geht es nicht.
826 if (fstat != FSIZE_NOFILE)
827 return 0;
828 // wenn es nur einen / gibt (den fuehrenden), dann ist es ein
829 // toplevel-verzeichnis, was direkt angelegt wird.
830 if (strrstr(dir,"/")==0) {
831 return funcall(bind_lambda(#'efun::mkdir, previous_object()), dir);
832 }
833
834 // mkdir() nicht direkt rufen, sondern vorher als closure ans aufrufende
835 // Objekt binden. Sonst laeuft die Rechtepruefung in valid_write() im Master
836 // unter der Annahme, dass die simul_efun.c mit ihrer root id was will.
837
838 // jetzt rekursiv die Verzeichnishierarchie anlegen. Wenn das erfolgreich
839 // ist, dann koennen wir jetzt mit mkdir das tiefste Verzeichnis anlegen
840 if (mkdirp(dir[0..strrstr(dir,"/")-1]) == 1)
841 return funcall(bind_lambda(#'efun::mkdir, previous_object()), dir);
Zesstraa25c9c12018-11-17 00:09:25 +0100842 return 0;
MG Mud User88f12472016-06-24 23:31:02 +0200843}
844
845
846// * Properties ggfs. mitspeichern
847mixed save_object(mixed name)
848{
849 mapping properties;
850 mapping save;
851 mixed index, res;
852 int i;
Zesstraa012d622017-01-29 20:38:48 +0100853 string oldpath;
MG Mud User88f12472016-06-24 23:31:02 +0200854
855 // nur Strings und 0 zulassen
856 if ((!stringp(name) || !sizeof(name)) &&
857 (!intp(name) || name!=0)) {
858 set_this_object(previous_object());
859 raise_error(sprintf(
860 "Only non-empty strings and 0 may be used as filename in "
861 "sefun::save_object()! Argument was %O\n",name));
862 }
863
Zesstraa012d622017-01-29 20:38:48 +0100864 if (stringp(name)) {
865 // abs. Pfad erzeugen. *seufz*
866 if (name[0]!='/')
867 name = "/" + name;
Zesstraa012d622017-01-29 20:38:48 +0100868 // automatisch in LIBDATADIR speichern
869 if (strstr(name,"/"LIBDATADIR"/") != 0) {
Zesstra60aed3e2017-01-31 16:03:51 +0100870 oldpath = name;
Zesstraa012d622017-01-29 20:38:48 +0100871 name = "/"LIBDATADIR + name;
872 // wenn das Verzeichnis nicht existiert, ggf. anlegen
873 string dir = name[0..strrstr(name,"/")-1];
874 if (file_size(dir) != FSIZE_DIR) {
875 if (mkdirp(dir) != 1)
876 raise_error("save_object(): kann Verzeichnis " + dir
877 + " nicht anlegen!");
878 }
879 }
880 }
881
MG Mud User88f12472016-06-24 23:31:02 +0200882 save = m_allocate(0, 2);
Zesstra4dbb9882019-11-26 21:26:36 +0100883 properties = previous_object()->QueryProperties();
MG Mud User88f12472016-06-24 23:31:02 +0200884
885 if(mappingp(properties))
886 {
887 // delete all entries in mapping properties without SAVE flag!
888 index = m_indices(properties);
889 for(i = sizeof(index)-1; i>=0;i--)
890 {
891 if(properties[index[i], F_MODE] & SAVE)
892 {
893 save[index[i]] = properties[index[i]];
894 save[index[i], F_MODE] =
895 properties[index[i], F_MODE] &
896 (~(SETMNOTFOUND|QUERYMNOTFOUND|QUERYCACHED|SETCACHED));
897 }
898 }
899 }
900 else save = ([]);
901
902 // save object!
903 previous_object()->_set_save_data(save);
904 // format: wie definiert in config.h
Zesstraa012d622017-01-29 20:38:48 +0100905 if (stringp(name)) {
MG Mud User88f12472016-06-24 23:31:02 +0200906 res = funcall(bind_lambda(#'efun::save_object, previous_object()), name,
907 __LIB__SAVE_FORMAT_VERSION__);
Zesstraa012d622017-01-29 20:38:48 +0100908 // wenn erfolgreich und noch nen Savefile existiert, was nicht unter
909 // /data/ liegt, wird das geloescht.
910 if (!res && oldpath
911 && file_size(oldpath+".o") >= 0)
912 rm(oldpath+".o");
913 }
MG Mud User88f12472016-06-24 23:31:02 +0200914 else
915 res = funcall(bind_lambda(#'efun::save_object, previous_object()),
916 __LIB__SAVE_FORMAT_VERSION__);
Zesstraa012d622017-01-29 20:38:48 +0100917
MG Mud User88f12472016-06-24 23:31:02 +0200918 previous_object()->_set_save_data(0);
919
920#ifdef IOSTATS
921 // Stats...
922 struct iostat_s stat = (<iostat_s>);
923 stat->oname = object_name(previous_object());
924 stat->time = time();
925 //stat->size = (int)object_info(previous_object(),OINFO_MEMORY,
926 // OIM_TOTAL_DATA_SIZE);
927 if (stringp(name))
928 stat->size = file_size(name + ".o");
929 else
930 stat->sizeof(res);
931 //debug_message("saveo: "+saveo_stat[0]+"\n");
932 saveo_stat[1][saveo_stat[0]] = stat;
933 saveo_stat[0] = (saveo_stat[0] + 1) % sizeof(saveo_stat[1]);
934 //debug_message("saveo 2: "+saveo_stat[0]+"\n");
935#endif
936
937 return res;
938}
939
940// * Auch Properties laden
941int restore_object(string name)
942{
943 int result;
944 mixed index;
945 mixed save;
946 mapping properties;
947 int i;
948 closure cl;
949
Zesstra94381a42017-01-29 22:37:52 +0100950 if (sizeof(name) < 1)
951 {
952 set_this_object(previous_object());
953 raise_error("Bad arg 1 to restore_object(): expected non-empty "
954 "'string'.\n");
955 }
956
Zesstraa012d622017-01-29 20:38:48 +0100957 // Wenn name vermutlich ein Pfad (also nicht mit #x:y anfaengt)
958 if (name[0] != '#')
959 {
960 // abs. Pfad erzeugen *seufz*
Zesstra984bde92017-01-29 22:19:52 +0100961 if (name[0]!='/')
Zesstraa012d622017-01-29 20:38:48 +0100962 name = "/" + name;
963
Zesstraf2a0d492017-04-29 11:05:46 +0200964 // .c am Ende loeschen, sonst wird das File ggf. nicht gefunden.
965 if(name[<2..]==".c")
966 name=name[..<3];
967
Zesstraa012d622017-01-29 20:38:48 +0100968 // wenn kein /data/ vorn steht, erstmal gucken, ob das Savefile unter
969 // /data/ existiert. Wenn ja, wird das geladen.
Zesstra984bde92017-01-29 22:19:52 +0100970 if (strstr(name,"/"LIBDATADIR"/") != 0)
Zesstraa012d622017-01-29 20:38:48 +0100971 {
972 string path = "/"LIBDATADIR + name;
973 if (file_size(path + ".o") >= 0)
974 name = path;
975 }
976 }
977
MG Mud User88f12472016-06-24 23:31:02 +0200978 // get actual property settings (by create())
Zesstra4dbb9882019-11-26 21:26:36 +0100979 properties = previous_object()->QueryProperties();
MG Mud User88f12472016-06-24 23:31:02 +0200980
981// DEBUG(sprintf("RESTORE %O\n",name));
982 // restore object
983 result=funcall(bind_lambda(#'efun::restore_object, previous_object()), name);
984 //'))
985 //_get_save_data liefert tatsaechlich mixed zurueck, wenn das auch immer ein
986 //mapping sein sollte.
Zesstra4dbb9882019-11-26 21:26:36 +0100987 save = previous_object()->_get_save_data();
988 if((save))
MG Mud User88f12472016-06-24 23:31:02 +0200989 {
990 index = m_indices(save);
991 for(i = sizeof(index)-1; i>=0; i--)
992 {
993 properties[index[i]] = save[index[i]];
994 properties[index[i], F_MODE] = save[index[i], F_MODE]
995 &~(SETCACHED|QUERYCACHED);
996 }
997 }
998 else properties = ([]);
999
1000 // restore properties
1001 funcall(
1002 bind_lambda(
1003 unbound_lambda(({'arg}), //'})
1004 ({#'call_other,({#'this_object}),
1005 "SetProperties",'arg})),//')
1006 previous_object()),properties);
1007 previous_object()->_set_save_data(0);
1008
1009#ifdef IOSTATS
1010 // Stats...
1011 //debug_message("restoreo: "+restoreo_stat[0]+"\n");
1012 struct iostat_s stat = (<iostat_s>);
1013 stat->oname = object_name(previous_object());
1014 stat->time = time();
1015 //stat->size = (int)object_info(previous_object(),OINFO_MEMORY,
1016 // OIM_TOTAL_DATA_SIZE);
1017 stat->size = file_size(name + ".o");
1018 restoreo_stat[1][restoreo_stat[0]] = stat;
1019
1020 restoreo_stat[0] = (restoreo_stat[0] + 1) % sizeof(restoreo_stat[1]);
1021#endif
1022
1023 return result;
1024}
1025
1026// * HB eines Objektes ein/ausschalten
1027int set_object_heart_beat(object ob, int flag)
1028{
1029 if (objectp(ob))
1030 return funcall(bind_lambda(#'efun::configure_object,ob), ob, OC_HEART_BEAT, flag);
1031}
1032
1033// * Magierlevelgruppen ermitteln
1034int query_wiz_grp(mixed wiz)
1035{
1036 int lev;
1037
1038 lev=query_wiz_level(wiz);
1039 if (lev<SEER_LVL) return 0;
1040 if (lev>=GOD_LVL) return lev;
1041 if (lev>=ARCH_LVL) return ARCH_GRP;
1042 if (lev>=ELDER_LVL) return ELDER_GRP;
1043 if (lev>=LORD_LVL) return LORD_GRP;
1044 if (lev>=SPECIAL_LVL) return SPECIAL_GRP;
1045 if (lev>=DOMAINMEMBER_LVL) return DOMAINMEMBER_GRP;
1046 if (lev>=WIZARD_LVL) return WIZARD_GRP;
1047 if (lev>=LEARNER_LVL) return LEARNER_GRP;
1048 return SEER_GRP;
1049}
1050
1051mixed *wizlist_info()
1052{
1053 if (ARCH_SECURITY || !extern_call())
1054 return efun::wizlist_info();
1055 return 0;
1056}
1057
1058// * wizlist ausgeben
1059varargs void wizlist(string name, int sortkey ) {
1060
1061 if (!name)
1062 {
1063 if (this_player())
1064 name = getuid(this_player());
1065 if (!name)
1066 return;
1067 }
1068
1069 // Schluessel darf nur in einem gueltigen Bereich sein
1070 if (sortkey<WL_NAME || sortkey>=WL_SIZE) sortkey=WL_COST;
1071
1072 mixed** wl = efun::wizlist_info();
1073 // nach <sortkey> sortieren
1074 wl = sort_array(wl, function int (mixed a, mixed b)
1075 {return a[sortkey] < b[sortkey]; } );
1076
1077 // Summe ueber alle Kommandos ermitteln.
1078 int total_cmd, i;
1079 int pos=-1;
1080 foreach(mixed entry : wl)
1081 {
1082 total_cmd += entry[WL_COMMANDS];
1083 if (entry[WL_NAME] == name)
1084 pos = i;
1085 ++i;
1086 }
1087
1088 if (pos < 0 && name != "ALL" && name != "TOP100")
1089 return;
1090
1091 if (name == "TOP100")
1092 {
1093 if (sizeof(wl) > 100)
1094 wl = wl[0..100];
1095 else
1096 wl = wl;
1097 }
1098 // um name herum schneiden
1099 else if (name != "ALL")
1100 {
1101 if (sizeof(wl) <= 21)
1102 wl = wl;
1103 else if (pos + 10 < sizeof(wl) && pos - 10 > 0)
1104 wl = wl[pos-10..pos+10];
1105 else if (pos <=21)
1106 wl = wl[0..20];
1107 else if (pos >= sizeof(wl) - 21)
1108 wl = wl[<21..];
1109 else
1110 wl = wl;
1111 }
1112
1113 write("\nWizard top score list\n\n");
1114 if (total_cmd == 0)
1115 total_cmd = 1;
1116 printf("%-20s %-6s %-3s %-17s %-6s %-6s %-6s\n",
1117 "EUID", "cmds", "%", "Costs", "HB", "Arrays","Mapp.");
1118 foreach(mixed e: wl)
1119 {
1120 printf("%-:20s %:6d %:2d%% [%:6dk,%:6dG] %:6d %:6d %:6d\n",
1121 e[WL_NAME], e[WL_COMMANDS], e[WL_COMMANDS] * 100 / total_cmd,
1122 e[WL_COST] / 1000, e[WL_TOTAL_GIGACOST],
1123 e[WL_HEART_BEATS], e[WL_ARRAY_TOTAL], e[WL_MAPPING_TOTAL]
1124 );
1125 }
1126 printf("\nTotal %7d (%d)\n\n", total_cmd, sizeof(wl));
1127}
1128
1129
1130// Ab hier folgen Hilfsfunktionen fuer call_out() bzw. fuer deren Begrenzung
1131
1132// ermittelt das Objekt des Callouts.
1133private object _call_out_obj( mixed call_out ) {
1134 return pointerp(call_out) ? call_out[0] : 0;
1135}
1136
1137private void _same_object( object ob, mapping m ) {
1138 // ist nicht so bloed, wie es aussieht, s. nachfolgede Verwendung von m
1139 if ( m[ob] )
1140 m[ob] += ({ ob });
1141 else
1142 m[ob] = ({ ob });
1143}
1144
1145// alle Objekte im Mapping m zusammenfassen, die den gleichen Loadname (Name der
1146// Blueprint) haben, also alle Clones der BP, die Callouts laufen haben.
1147// Objekte werden auch mehrfach erfasst, je nach Anzahl ihrer Callouts.
1148private void _same_path( object key, object *obs, mapping m ) {
1149 string path;
1150 if (!objectp(key) || !pointerp(obs)) return;
1151
1152 path = load_name(key);
1153
1154 if ( m[path] )
1155 m[path] += obs;
1156 else
1157 m[path] = obs;
1158}
1159
1160// key kann object oder string sein.
1161private int _too_many( mixed key, mapping m, int i ) {
1162 return sizeof(m[key]) >= i;
1163}
1164
1165// alle Objekte in obs zerstoeren und Fehlermeldung ausgeben. ACHTUNG: Die
1166// Objekte werden idR zu einer BP gehoeren, muessen aber nicht! In dem Fall
1167// wird auf der Ebene aber nur ein Objekt in der Fehlermeldung erwaehnt.
1168private void _destroy( mixed key, object *obs, string text, int uid ) {
1169 if (!pointerp(obs)) return;
1170 // Array mit unique Eintraege erzeugen.
1171 obs = m_indices( mkmapping(obs) );
1172 // Fehlermeldung auf der Ebene ausgeben, im catch() mit publish, damit es
1173 // auf der Ebene direkt scrollt, der backtrace verfuegbar ist (im
1174 // gegensatz zur Loesung mittels Callout), die Ausfuehrung aber weiter
1175 // laeuft.
1176 catch( efun::raise_error(
1177 sprintf( text,
Zesstra4dbb9882019-11-26 21:26:36 +01001178 uid ? master()->creator_file(key) : key,
MG Mud User88f12472016-06-24 23:31:02 +02001179 sizeof(obs), object_name(obs[<1]) ) );publish);
1180 // Und weg mit dem Kram...
1181 filter( obs, #'efun::destruct/*'*/ );
1182}
1183
1184// Alle Objekt einer UID im Mapping m mit UID als KEys zusammenfassen. Objekt
1185// sind dabei nicht unique.
1186private void _same_uid( string key, object *obs, mapping m, closure cf ) {
1187 string uid;
1188
1189 if ( !pointerp(obs) || !sizeof(obs) )
1190 return;
1191
1192 uid = funcall( cf, key );
1193
1194 if ( m[uid] )
1195 m[uid] += obs; // obs ist nen Array
1196 else
1197 m[uid] = obs;
1198}
1199
1200private int _log_call_out(mixed co)
1201{
1202 log_file("TOO_MANY_CALLOUTS",
1203 sprintf("%s::%O (%d)\n",object_name(co[0]),co[1],co[2]),
1204 200000);
1205 return 0;
1206}
1207
1208private int last_callout_log=0;
1209
1210nomask varargs void call_out( varargs mixed *args )
1211{
1212 mixed tmp, *call_outs;
1213
1214 // Bei >600 Callouts alle Objekte killen, die mehr als 30 Callouts laufen
1215 // haben.
1216 if ( efun::driver_info(DI_NUM_CALLOUTS) > 600
1217 && geteuid(previous_object()) != ROOTID )
1218 {
1219 // Log erzeugen...
1220 if (last_callout_log <
1221 efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES) - 10
1222 && get_eval_cost() > 200000)
1223 {
1224 last_callout_log = efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES);
1225 log_file("TOO_MANY_CALLOUTS",
1226 sprintf("\n%s: ############ Too many callouts: %d ##############\n",
1227 strftime("%y%m%d-%H%M%S"),
1228 efun::driver_info(DI_NUM_CALLOUTS)));
1229 filter(efun::call_out_info(), #'_log_call_out);
1230 }
1231 // Objekte aller Callouts ermitteln
1232 call_outs = map( efun::call_out_info(), #'_call_out_obj );
1233 mapping objectmap = ([]);
1234 filter( call_outs, #'_same_object, &objectmap );
1235 // Master nicht grillen...
1236 efun::m_delete( objectmap, master(1) );
1237 // alle Objekte raussuchen, die zuviele haben...
1238 mapping res = filter_indices( objectmap, #'_too_many, objectmap, 29 );
1239 // und ueber alle Keys gehen, an _destroy() werden Key und Array mit
1240 // Objekten uebergeben (in diesem Fall sind Keys und Array mit
1241 // Objekten jeweils das gleiche Objekt).
1242 if ( sizeof(res) )
1243 walk_mapping(res, #'_destroy, "CALL_OUT overflow by single "
1244 "object [%O]. Destructed %d objects. [%s]\n", 0 );
1245
1246 // Bei (auch nach dem ersten Aufraeumen noch) >800 Callouts alle
1247 // Objekte killen, die mehr als 50 Callouts laufen haben - und
1248 // diesmal zaehlen Clones nicht eigenstaendig! D.h. es werden alle
1249 // Clones einer BP gekillt, die Callouts laufen haben, falls alle
1250 // diese Objekte _zusammen_ mehr als 50 Callouts haben!
1251 if ( efun::driver_info(DI_NUM_CALLOUTS) > 800 ) {
1252 // zerstoerte Objekte von der letzten Aktion sind in objectmap nicht
1253 // mehr drin, da sie dort als Keys verwendet wurden.
1254 mapping pathmap=([]);
1255 // alle Objekt einer BP in res sortieren, BP-Name als Key, Arrays
1256 // von Objekten als Werte.
1257 walk_mapping( objectmap, #'_same_path, &pathmap);
1258 // alle BPs (und ihre Objekte) raussuchen, die zuviele haben...
1259 res = filter_indices( pathmap, #'_too_many/*'*/, pathmap, 50 );
1260 // und ueber alle Keys gehen, an _destroy() werden die Clones
1261 // uebergeben, die Callouts haben.
1262 if ( sizeof(res) )
1263 walk_mapping( res, #'_destroy/*'*/, "CALL_OUT overflow by file "
1264 "'%s'. Destructed %d objects. [%s]\n", 0 );
1265
1266 // Wenn beide Aufraeumarbeiten nichts gebracht haben und immer
1267 // noch >1000 Callouts laufen, werden diesmal alle Callouts
1268 // einer UID zusammengezaehlt.
1269 // Alle Objekte einer UID, die es in Summe aller ihrer Objekt mit
1270 // Callouts auf mehr als 100 Callouts bringt, werden geroestet.
1271 if (efun::driver_info(DI_NUM_CALLOUTS) > 1000)
1272 {
1273 // das nach BP-Namen vorgefilterte Mapping jetzt nach UIDs
1274 // zusammensortieren. Zerstoerte Clones filter _same_uid()
1275 // raus.
1276 mapping uidmap=([]);
1277 walk_mapping( pathmap, #'_same_uid, &uidmap,
1278 symbol_function( "creator_file",
1279 "/secure/master" ) );
1280 // In res nun UIDs als Keys und Arrays von Objekten als Werte.
1281 // Die rausfiltern, die mehr als 100 Objekte (non-unique, d.h.
1282 // 100 Callouts!) haben.
1283 res = filter_indices( uidmap, #'_too_many, uidmap, 100 );
1284 // und erneut ueber die Keys laufen und jeweils die Arrays mit
1285 // den Objekten zur Zerstoerung an _destroy()...
1286 if ( sizeof(res) )
1287 walk_mapping( res, #'_destroy, "CALL_OUT overflow by "
1288 "UID '%s'. Destructed %d objects. [%s]\n",
1289 1 );
1290 }
1291 }
1292 }
1293
1294 // Falls das aufrufende Objekt zerstoert wurde beim Aufraeumen
1295 if ( !previous_object() )
1296 return;
1297
1298 set_this_object( previous_object() );
1299 apply( #'efun::call_out, args );
1300 return;
1301}
1302
1303mixed call_out_info() {
1304
1305 object po = previous_object();
1306 mixed coi = efun::call_out_info();
1307
1308 // ungefilterten Output nur fuer bestimmte Objekte, Objekte in /std oder
1309 // /obj haben die BackboneID.
1310 if (query_wiz_level(getuid(po)) >= ARCH_LVL
Zesstra4dbb9882019-11-26 21:26:36 +01001311 || master()->creator_file(load_name(po)) == BACKBONEID ) {
MG Mud User88f12472016-06-24 23:31:02 +02001312 return coi;
1313 }
1314 else {
1315 return filter(coi, function mixed (mixed arr) {
1316 if (pointerp(arr) && arr[0]==po)
1317 return 1;
1318 else return 0; });
1319 }
1320}
1321
1322// * Zu einer closure das Objekt, an das sie gebunden ist, suchen
1323// NICHT das Objekt, was ggf. die lfun definiert!
1324mixed query_closure_object(closure c) {
1325 return
1326 CLOSURE_IS_UNBOUND_LAMBDA(get_type_info(c, 1)) ?
1327 0 :
1328 (to_object(c) || -1);
1329}
1330
1331// * Wir wollen nur EIN Argument ... ausserdem checks fuer den Netztotenraum
1332varargs void move_object(mixed what, mixed where)
1333{
1334 object po,tmp;
1335
1336 po=previous_object();
1337 if (!where)
1338 {
1339 where=what;
1340 what=po;
1341 }
1342 if (((stringp(where) && where==NETDEAD_ROOM ) ||
1343 (objectp(where) && where==find_object(NETDEAD_ROOM))) &&
1344 objectp(what) && object_name(what)!="/obj/sperrer")
1345 {
1346 if (!query_once_interactive(what))
1347 {
1348 what->remove();
1349 if (what) destruct(what);
1350 return;
1351 }
1352 if (living(what) || interactive(what))
1353 {
1354 log_file("NDEAD2",sprintf("TRYED TO MOVE TO NETDEAD: %O\n",what));
1355 return;
1356 }
1357 set_object_heart_beat(what,0);
1358 }
1359 tmp=what;
1360 while (tmp=environment(tmp))
1361 // Ja. Man ruft die _set_xxx()-Funktionen eigentlich nicht direkt auf.
1362 // Aber das Lichtsystem ist schon *so* rechenintensiv und gerade der
1363 // P_LAST_CONTENT_CHANGE-Cache wird *so* oft benoetigt, dass es mir
1364 // da um jedes bisschen Rechenzeit geht.
1365 // Der Zweck heiligt ja bekanntlich die Mittel. ;-)
1366 //
1367 // Tiamak
1368 tmp->_set_last_content_change();
1369 funcall(bind_lambda(#'efun::move_object,po),what,where);
1370 if (tmp=what)
1371 while (tmp=environment(tmp))
1372 tmp->_set_last_content_change();
1373}
1374
1375
1376void start_simul_efun() {
1377 mixed *info;
1378
1379 // Falls noch nicht getan, extra_wizinfo initialisieren
1380 if ( !pointerp(info = get_extra_wizinfo(0)) )
1381 set_extra_wizinfo(0, info = allocate(BACKBONE_WIZINFO_SIZE));
1382
1383 InitLivingData(info);
1384
1385 set_next_reset(10); // direkt mal aufraeumen
1386}
1387
1388protected void reset() {
1389 set_next_reset(7200);
1390 CleanLivingData();
1391}
1392
1393#if !__EFUN_DEFINED__(absolute_hb_count)
1394int absolute_hb_count() {
1395 return efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES);
1396}
1397#endif
1398
1399void __set_environment(object ob, mixed target)
1400{
1401 string path;
1402 object obj;
1403
1404 if (!objectp(ob))
1405 return;
1406 if (!IS_ARCH(geteuid(previous_object())) || !ARCH_SECURITY )
1407 return;
1408 if (objectp(target))
1409 {
1410 efun::set_environment(ob,target);
1411 return;
1412 }
Zesstra4dbb9882019-11-26 21:26:36 +01001413 path=master()->make_path_absolute(target);
MG Mud User88f12472016-06-24 23:31:02 +02001414 if (stringp(path) && file_size(path+".c")>=0 &&
1415 !catch(load_object(path);publish) )
1416 {
1417 obj=find_object(path);
1418 efun::set_environment(ob,obj);
1419 return;
1420 }
1421}
1422
1423void _dump_wizlist(string file, int sortby) {
1424 int i;
1425 mixed *a;
1426
1427 if (!LORD_SECURITY)
1428 return;
1429 if (!master()->valid_write(file,geteuid(previous_object()),"write_file"))
1430 {
1431 write("NO WRITE PERMISSION\n");
1432 return;
1433 }
1434 a = wizlist_info();
1435 a = sort_array(a, lambda( ({'a,'b}),
1436 ({#'<,
1437 ({#'[,'a,sortby}),
1438 ({#'[,'b,sortby})
1439 })));
1440 rm(file);
1441 for (i=sizeof(a)-1;i>=0;i--)
1442 write_file(file,sprintf("%-11s: eval=%-8d cmds=%-6d HBs=%-5d array=%-5d mapping=%-7d\n",
1443 a[i][WL_NAME],a[i][WL_TOTAL_COST],a[i][WL_COMMANDS],a[i][WL_HEART_BEATS],
1444 a[i][WL_ARRAY_TOTAL],a[i][WL_CALL_OUT]));
1445}
1446
1447public varargs object deep_present(mixed what, object ob) {
1448
1449 if(!objectp(ob))
1450 ob=previous_object();
1451 // Wenn ein Objekt gesucht wird: Alle Envs dieses Objekts ermitteln und
1452 // schauen, ob in diesen ob vorkommt. Dann ist what in ob enthalten.
1453 if(objectp(what)) {
1454 object *envs=all_environment(what);
1455 // wenn ob kein Environment hat, ist es offensichtlich nicht in what
1456 // enthalten.
1457 if (!pointerp(envs)) return 0;
1458 if (member(envs, ob) != -1) return what;
1459 }
1460 // sonst wirds teurer, ueber alle Objekte im (deep) Inv laufen und per id()
1461 // testen. Dabei muss aber die gewuenschte Nr. ("flasche 3") abgeschnitten
1462 // werden und selber gezaehlt werden, welche das entsprechende Objekt ist.
1463 else if (stringp(what)) {
1464 int cnt;
1465 string newwhat;
1466 if(sscanf(what,"%s %d",newwhat,cnt)!=2)
1467 cnt=1;
1468 else
1469 what=newwhat;
1470 foreach(object invob: deep_inventory(ob)) {
1471 if (invob->id(what) && !--cnt)
1472 return invob;
1473 }
1474 }
1475 else {
1476 set_this_object(previous_object());
1477 raise_error(sprintf("Wrong argument 1 to deep_present(). "
1478 "Expected \"object\" or \"string\", got %.50O.\n",
1479 what));
1480 }
1481 return 0;
1482}
1483
1484mapping dump_ip_mapping()
1485{
1486 return 0;
1487}
1488
1489nomask void swap(object obj)
1490{
1491 write("Your are not allowed to swap objects by hand!\n");
1492 return;
1493}
1494
1495nomask varargs void garbage_collection(string str)
1496{
1497 if(previous_object()==0 || !IS_ARCH(geteuid(previous_object()))
1498 || !ARCH_SECURITY)
1499 {
1500 write("Call GC now and the mud will crash in 5-6 hours. DONT DO IT!\n");
1501 return;
1502 }
1503 else if (stringp(str))
1504 {
1505 return efun::garbage_collection(str);
1506 }
1507 else
1508 return efun::garbage_collection();
1509}
1510
1511varargs void notify_fail(mixed nf, int prio) {
1512 object po,oldo;
1513 int oldprio;
1514
1515 if (!PL || !objectp(po=previous_object())) return;
1516 if (!stringp(nf) && !closurep(nf)) {
1517 set_this_object(po);
1518 raise_error(sprintf(
1519 "Only strings and closures allowed for notify_fail! "
1520 "Argument was: %.50O...\n",nf));
1521 }
1522
1523 // falls ein Objekt bereits nen notify_fail() setzte, Prioritaeten abschaetzen
1524 // und vergleichen.
1525 if (objectp(oldo=query_notify_fail(1)) && po!=oldo) {
1526 if (!prio) {
1527 //Prioritaet dieses notify_fail() 'abschaetzen'
1528 if (po==PL) // Spieler-interne (soul-cmds)
1529 prio=NF_NL_OWN;
1530 else if (living(po))
1531 prio=NF_NL_LIVING;
Zesstra4dbb9882019-11-26 21:26:36 +01001532 else if (po->IsRoom())
MG Mud User88f12472016-06-24 23:31:02 +02001533 prio=NF_NL_ROOM;
1534 else
1535 prio=NF_NL_THING;
1536 }
1537 //Prioritaet des alten Setzers abschaetzen
1538 if (oldo==PL)
1539 oldprio=NF_NL_OWN;
1540 else if (living(oldo))
1541 oldprio=NF_NL_LIVING;
Zesstra4dbb9882019-11-26 21:26:36 +01001542 else if (oldo->IsRoom())
MG Mud User88f12472016-06-24 23:31:02 +02001543 oldprio=NF_NL_ROOM;
1544 else
1545 oldprio=NF_NL_THING;
1546 }
1547 else // wenn es noch kein Notify_fail gibt:
1548 oldprio=NF_NL_NONE;
1549
1550 //vergleichen und ggf. setzen
1551 if (prio >= oldprio) {
1552 set_this_object(po);
1553 efun::notify_fail(nf);
1554 }
1555
1556 return;
1557}
1558
1559void _notify_fail(string str)
1560{
1561 //query_notify_fail() benutzen, um das Objekt
1562 //des letzten notify_fail() zu ermitteln
1563 object o;
1564 if ((o=query_notify_fail(1)) && o!=previous_object())
1565 return;
1566 //noch kein notify_fail() fuer dieses Kommando gesetzt, auf gehts.
1567 set_this_object(previous_object());
1568 efun::notify_fail(str);
1569 return;
1570}
1571
1572string time2string( string format, int zeit )
1573{
1574 int i,ch,maxunit,dummy;
1575 string *parts, fmt;
1576
1577 int secs = zeit;
1578 int mins = (zeit/60);
1579 int hours = (zeit/3600);
1580 int days = (zeit/86400);
1581 int weeks = (zeit/604800);
1582 int months = (zeit/2419200);
1583 int abbr = 0;
1584
1585 parts = regexplode( format, "\(%\(-|\)[0-9]*[nwdhmsxNWDHMSX]\)|\(%%\)" );
1586
1587 for( i=1; i<sizeof(parts); i+=2 )
1588 {
1589 ch = parts[i][<1];
1590 switch( parts[i][<1] )
1591 {
1592 case 'x': case 'X':
1593 abbr = sscanf( parts[i], "%%%d", dummy ) && dummy==0;
1594 // NO break !
1595 case 'n': case 'N':
1596 maxunit |= 31;
1597 break;
1598 case 'w': case 'W':
1599 maxunit |= 15;
1600 break;
1601 case 'd': case 'D':
1602 maxunit |= 7;
1603 break;
1604 case 'h': case 'H':
1605 maxunit |= 3;
1606 break;
1607 case 'm': case 'M':
1608 maxunit |= 1;
1609 break;
1610 }
1611 }
1612 if( maxunit & 16 ) weeks %= 4;
1613 if( maxunit & 8 ) days %= 7;
1614 if( maxunit & 4 ) hours %= 24;
1615 if( maxunit & 2 ) mins %= 60;
1616 if( maxunit ) secs %= 60;
1617
1618 for( i=1; i<sizeof(parts); i+=2 )
1619 {
1620 fmt = parts[i][0..<2];
1621 ch = parts[i][<1];
1622 if( ch=='x' )
1623 {
1624 if (months > 0) ch='n';
1625 else if( weeks>0 ) ch='w';
1626 else if( days>0 ) ch='d';
1627 else if( hours>0 ) ch='h';
1628 else if(mins > 0) ch = 'm';
1629 else ch = 's';
1630 }
1631 else if( ch=='X' )
1632 {
1633 if (months > 0) ch='N';
1634 else if( weeks>0 ) ch='W';
1635 else if( days>0 ) ch='D';
1636 else if( hours>0 ) ch='H';
1637 else if(mins > 0) ch = 'M';
1638 else ch = 'S';
1639 }
1640
1641 switch( ch )
1642 {
1643 case 'n': parts[i] = sprintf( fmt+"d", months ); break;
1644 case 'w': parts[i] = sprintf( fmt+"d", weeks ); break;
1645 case 'd': parts[i] = sprintf( fmt+"d", days ); break;
1646 case 'h': parts[i] = sprintf( fmt+"d", hours ); break;
1647 case 'm': parts[i] = sprintf( fmt+"d", mins ); break;
1648 case 's': parts[i] = sprintf( fmt+"d", secs ); break;
1649 case 'N':
1650 if(abbr) parts[i] = "M";
1651 else parts[i] = sprintf( fmt+"s", (months==1) ? "Monat" : "Monate" );
1652 break;
1653 case 'W':
1654 if(abbr) parts[i] = "w"; else
1655 parts[i] = sprintf( fmt+"s", (weeks==1) ? "Woche" : "Wochen" );
1656 break;
1657 case 'D':
1658 if(abbr) parts[i] = "d"; else
1659 parts[i] = sprintf( fmt+"s", (days==1) ? "Tag" : "Tage" );
1660 break;
1661 case 'H':
1662 if(abbr) parts[i] = "h"; else
1663 parts[i] = sprintf( fmt+"s", (hours==1) ? "Stunde" : "Stunden" );
1664 break;
1665 case 'M':
1666 if(abbr) parts[i] = "m"; else
1667 parts[i] = sprintf( fmt+"s", (mins==1) ? "Minute" : "Minuten" );
1668 break;
1669 case 'S':
1670 if(abbr) parts[i] = "s"; else
1671 parts[i] = sprintf( fmt+"s", (secs==1) ? "Sekunde" : "Sekunden" );
1672 break;
1673 case '%':
1674 parts[i] = "%";
1675 break;
1676 }
1677 }
1678 return implode( parts, "" );
1679}
1680
1681nomask mixed __create_player_dummy(string name)
1682{
1683 string err;
1684 object ob;
1685 mixed m;
1686 //hat nen Scherzkeks die Blueprint bewegt?
1687 if ((ob=find_object("/secure/login")) && environment(ob))
1688 catch(destruct(ob);publish);
1689 err = catch(ob = clone_object("secure/login");publish);
1690 if (err)
1691 {
1692 write("Fehler beim Laden von /secure/login.c\n"+err+"\n");
1693 return 0;
1694 }
Zesstra4dbb9882019-11-26 21:26:36 +01001695 if (objectp(m=ob->new_logon(name))) netdead[name]=m;
MG Mud User88f12472016-06-24 23:31:02 +02001696 return m;
1697}
1698
1699nomask int secure_level()
1700{
1701 int *level;
1702 //kette der Caller durchlaufen, den niedrigsten Level in der Kette
1703 //zurueckgeben. Zerstoerte Objekte (Selbstzerstoerer) fuehren zur Rueckgabe
1704 //von 0.
1705 //caller_stack(1) fuegt dem Rueckgabearray this_interactive() hinzu bzw. 0,
1706 //wenn es keinen Interactive gibt. Die 0 fuehrt dann wie bei zerstoerten
1707 //Objekten zur Rueckgabe von 0, was gewuenscht ist, da es hier einen
1708 //INteractive geben muss.
1709 level=map(caller_stack(1),function int (object caller)
1710 {if (objectp(caller))
1711 return(query_wiz_level(geteuid(caller)));
1712 return(0); // kein Objekt da, 0.
1713 } );
1714 return(min(level)); //den kleinsten Wert im Array zurueckgeben (ggf. 0)
1715}
1716
1717nomask string secure_euid()
1718{
1719 string euid;
1720
1721 if (!this_interactive()) // Es muss einen interactive geben
1722 return 0;
1723 euid=geteuid(this_interactive());
1724 // ueber alle Caller iterieren. Wenn eines davon eine andere euid hat als
1725 // der Interactive und diese nicht die ROOTID ist, wird 0 zurueckgeben.
1726 // Ebenso, falls ein Selbstzerstoerer irgendwo in der Kette ist.
1727 foreach(object caller: caller_stack()) {
1728 if (!objectp(caller) ||
1729 (geteuid(caller)!=euid && geteuid(caller)!=ROOTID))
1730 return 0;
1731 }
1732 return euid; // 'sichere' euid zurueckgeben
1733}
1734
1735// INPUT_PROMPT und nen Leerprompt hinzufuegen, wenn keins uebergeben wird.
1736// Das soll dazu dienen, dass alle ggf. ein EOR am Promptende kriegen...
1737//#if __BOOT_TIME__ < 1360017213
1738varargs void input_to( mixed fun, int flags, varargs mixed *args )
1739{
1740 mixed *arr;
1741 int i;
1742
1743 if ( !this_player() || !previous_object() )
1744 return;
1745
1746 // TODO: input_to(...,INPUT_PROMPT, "", ...), wenn kein INPUT_PROMPT
1747 // vorkommt...
1748 if ( flags&INPUT_PROMPT ) {
1749 arr = ({ fun, flags }) + args;
1750 }
1751 else {
1752 // ggf. ein INPUT_PROMPT hinzufuegen und nen Leerstring als Prompt.
1753 flags |= INPUT_PROMPT;
1754 arr = ({ fun, flags, "" }) + args;
1755 }
1756
1757 // Arrays gegen flatten quoten.
1758 for ( i = sizeof(arr) - 1; i > 1; i-- )
1759 if ( pointerp(arr[i]) )
1760 arr[i] = quote(arr[i]);
1761
1762 apply( bind_lambda( unbound_lambda( ({}),
1763 ({ #'efun::input_to/*'*/ }) + arr ),
1764 previous_object() ) );
1765}
1766//#endif
1767
1768nomask int set_light(int i)
1769// erhoeht das Lichtlevel eines Objekts um i
1770// result: das Lichtlevel innerhalb des Objekts
1771{
1772 object ob, *inv;
1773 int lall, light, dark, tmp;
1774
1775 if (!(ob=previous_object())) return 0; // ohne das gehts nicht.
1776
1777 // aus kompatibilitaetsgruenden kann man auch den Lichtlevel noch setzen
1778 if (i!=0) ob->SetProp(P_LIGHT, ob->QueryProp(P_LIGHT)+i);
1779
1780 // Lichtberechnung findet eigentlich in der Mudlib statt.
Zesstra4dbb9882019-11-26 21:26:36 +01001781 return ob->QueryProp(P_INT_LIGHT);
MG Mud User88f12472016-06-24 23:31:02 +02001782}
1783
MG Mud User88f12472016-06-24 23:31:02 +02001784public varargs string CountUp( string *s, string sep, string lastsep )
1785{
1786 string ret;
1787
1788 if ( !pointerp(s) )
1789 return "";
1790
1791 if (!sep) sep = ", ";
1792 if (!lastsep) lastsep = " und ";
1793
1794 switch (sizeof(s)) {
1795 case 0: ret=""; break;
1796 case 1: ret=s[0]; break;
1797 default:
1798 ret = implode(s[0..<2], sep);
1799 ret += lastsep + s[<1];
1800 }
1801 return ret;
1802}
1803
1804nomask varargs int query_next_reset(object ob) {
1805
1806 // Typpruefung: etwas anderes als Objekte oder 0 sollen Fehler sein.
1807 if (ob && !objectp(ob))
1808 raise_error(sprintf("Bad arg 1 to query_next_reset(): got %.20O, "
1809 "expected object.\n",ob));
1810
1811 // Defaultobjekt PO, wenn 0 uebergeben.
1812 if ( !objectp(ob) )
1813 ob = previous_object();
1814
1815 return efun::object_info(ob, OI_NEXT_RESET_TIME);
1816}
1817
1818
MG Mud User88f12472016-06-24 23:31:02 +02001819
1820// ### Ersatzaufloesung in Strings ###
Arathorn066820b2019-11-27 19:47:19 +01001821varargs string replace_personal(string str, <string|object>* obs, int caps) {
1822 string* parts = regexplode(str, "@WE[A-SU]*[0-9]");
1823 int i = sizeof(parts);
MG Mud User88f12472016-06-24 23:31:02 +02001824
Zesstra19102132016-09-01 22:50:36 +02001825 if (i>1)
1826 {
MG Mud User88f12472016-06-24 23:31:02 +02001827 int j, t;
MG Mud User88f12472016-06-24 23:31:02 +02001828 t = j = sizeof(obs);
1829
Arathorn066820b2019-11-27 19:47:19 +01001830 <string|closure>* name_cls = allocate(j);
1831 while (j--) {
MG Mud User88f12472016-06-24 23:31:02 +02001832 if (objectp(obs[j]))
1833 name_cls[j] = symbol_function("name", obs[j]);
1834 else if (stringp(obs[j]))
1835 name_cls[j] = obs[j];
Arathorn066820b2019-11-27 19:47:19 +01001836 }
MG Mud User88f12472016-06-24 23:31:02 +02001837
Zesstra19102132016-09-01 22:50:36 +02001838 while ((i-= 2)>0)
1839 {
MG Mud User88f12472016-06-24 23:31:02 +02001840 // zu ersetzendes Token in Fall und Objektindex aufspalten
Arathorn066820b2019-11-27 19:47:19 +01001841 int ob_nr = parts[i][<1]-'1';
MG Mud User88f12472016-06-24 23:31:02 +02001842 if (ob_nr<0 || ob_nr>=t) {
1843 set_this_object(previous_object());
1844 raise_error(sprintf("replace_personal: using wrong object index %d\n",
1845 ob_nr));
1846 return implode(parts, "");
1847 }
1848
1849 // casus kann man schon hier entscheiden
1850 int casus;
1851 string part = parts[i];
1852 switch (part[3]) {
1853 case 'R': casus = WER; break;
1854 case 'S': casus = WESSEN; break;
1855 case 'M': casus = WEM; break;
1856 case 'N': casus = WEN; break;
1857 default: continue; // passt schon jetzt nicht in das Hauptmuster
1858 }
1859
1860 // und jetzt die einzelnen Keywords ohne fuehrendes "@WE", beendende Id
1861 mixed tmp;
1862 switch (part[3..<2]) {
1863 case "R": case "SSEN": case "M": case "N": // Name
1864 parts[i] = funcall(name_cls[ob_nr], casus, 1); break;
1865 case "RU": case "SSENU": case "MU": case "NU": // unbestimmt
1866 parts[i] = funcall(name_cls[ob_nr], casus); break;
1867 case "RQP": case "SSENQP": case "MQP": case "NQP": // Pronoun
1868 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001869 parts[i] = tmp->QueryPronoun(casus);
MG Mud User88f12472016-06-24 23:31:02 +02001870 break;
1871 case "RQA": case "SSENQA": case "MQA": case "NQA": // Article
1872 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001873 tmp = tmp->QueryArticle(casus, 1, 1);
MG Mud User88f12472016-06-24 23:31:02 +02001874 if (stringp(tmp) && !(tmp[<1]^' '))
1875 tmp = tmp[0..<2]; // Extra-Space wieder loeschen
1876 break;
1877 case "RQPPMS": case "SSENQPPMS": case "MQPPMS": case "NQPPMS":
1878 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001879 parts[i] = tmp->QueryPossPronoun(MALE, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001880 break;
1881 case "RQPPFS": case "SSENQPPFS": case "MQPPFS": case "NQPPFS":
1882 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001883 parts[i] = tmp->QueryPossPronoun(FEMALE, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001884 break;
1885 case "RQPPNS": case "SSENQPPNS": case "MQPPNS": case "NQPPNS":
1886 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001887 parts[i] = tmp->QueryPossPronoun(NEUTER, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001888 break;
1889 case "RQPPMP": case "SSENQPPMP": case "MQPPMP": case "NQPPMP":
1890 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001891 parts[i] = tmp->QueryPossPronoun(MALE, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001892 break;
1893 case "RQPPFP": case "SSENQPPFP": case "MQPPFP": case "NQPPFP":
1894 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001895 parts[i] = tmp->QueryPossPronoun(FEMALE, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001896 break;
1897 case "RQPPNP": case "SSENQPPNP": case "MQPPNP": case "NQPPNP":
1898 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001899 parts[i] = tmp->QueryPossPronoun(NEUTER, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001900 break;
1901 default:
1902 continue;
1903 }
Zesstra19102132016-09-01 22:50:36 +02001904
MG Mud User88f12472016-06-24 23:31:02 +02001905 // wenn tmp ein String war, weisen wir es hier pauschal zu
1906 if (stringp(tmp))
1907 parts[i] = tmp;
1908
1909 // auf Wunsch wird nach Satzenden gross geschrieben
1910 if (caps)
Zesstra19102132016-09-01 22:50:36 +02001911 {
1912 // Wenn das vorhergehende parts[i] == "" ist, sind wir am Anfang vom
1913 // String und dies wird wie ein Satzende vorher behandelt.
1914 if (parts[i-1] == "")
MG Mud User88f12472016-06-24 23:31:02 +02001915 parts[i] = capitalize(parts[i]);
Zesstra19102132016-09-01 22:50:36 +02001916 else
1917 {
1918 switch (parts[i-1][<2..])
1919 {
1920 case ". ": case "! ": case "? ":
1921 case ".": case "!": case "?":
1922 case ".\n": case "!\n": case "?\n":
1923 case "\" ": case "\"\n":
1924 parts[i] = capitalize(parts[i]);
1925 break;
1926 }
MG Mud User88f12472016-06-24 23:31:02 +02001927 }
Zesstra19102132016-09-01 22:50:36 +02001928 }
MG Mud User88f12472016-06-24 23:31:02 +02001929 }
1930 return implode(parts, "");
1931 }
1932 return str;
1933}
1934
MG Mud User88f12472016-06-24 23:31:02 +02001935//replacements for dropped efuns in LD
1936#if !__EFUN_DEFINED__(extract)
1937deprecated varargs string extract(string str, int from, int to) {
1938
1939 if(!stringp(str)) {
1940 set_this_object(previous_object());
1941 raise_error(sprintf("Bad argument 1 to extract(): %O",str));
1942 }
1943 if (intp(from) && intp(to)) {
1944 if (from>=0 && to>=0)
1945 return(str[from .. to]);
1946 else if (from>=0 && to<0)
1947 return(str[from .. <abs(to)]);
1948 else if (from<0 && to>=0)
1949 return(str[<abs(from) .. to]);
1950 else
1951 return(str[<abs(from) .. <abs(to)]);
1952 }
1953 else if (intp(from)) {
1954 if (from>=0)
1955 return(str[from .. ]);
1956 else
1957 return(str[<abs(from) .. ]);
1958 }
1959 else {
1960 return(str);
1961 }
1962}
1963#endif // !__EFUN_DEFINED__(extract)
1964
1965#if !__EFUN_DEFINED__(slice_array)
1966deprecated varargs mixed slice_array(mixed array, int from, int to) {
1967
1968 if(!pointerp(array)) {
1969 set_this_object(previous_object());
1970 raise_error(sprintf("Bad argument 1 to slice_array(): %O",array));
1971 }
1972 if (intp(from) && intp(to)) {
1973 if (from>=0 && to>=0)
1974 return(array[from .. to]);
1975 else if (from>=0 && to<0)
1976 return(array[from .. <abs(to)]);
1977 else if (from<0 && to>=0)
1978 return(array[<abs(from) .. to]);
1979 else
1980 return(array[<abs(from) .. <abs(to)]);
1981 }
1982 else if (intp(from)) {
1983 if (from>=0)
1984 return(array[from .. ]);
1985 else
1986 return(array[<abs(from) .. ]);
1987 }
1988 else {
1989 return(array);
1990 }
1991}
1992#endif // !__EFUN_DEFINED__(slice_array)
1993
1994#if !__EFUN_DEFINED__(member_array)
1995deprecated int member_array(mixed item, mixed arraystring) {
1996
1997 if (pointerp(arraystring)) {
1998 return(efun::member(arraystring,item));
1999 }
2000 else if (stringp(arraystring)) {
2001 return(efun::member(arraystring,to_int(item)));
2002 }
2003 else {
2004 set_this_object(previous_object());
2005 raise_error(sprintf("Bad argument 1 to member_array(): %O",arraystring));
2006 }
2007}
2008#endif // !__EFUN_DEFINED__(member_array)
2009
2010// The digit at the i'th position is the number of bits set in 'i'.
2011string count_table =
2012 "0112122312232334122323342334344512232334233434452334344534454556";
2013int broken_count_bits( string s ) {
2014 int i, res;
2015 if( !stringp(s) || !(i=sizeof(s)) ) return 0;
2016 for( ; i-->0; ) {
2017 // We are counting 6 bits at a time using a precompiled table.
2018 res += count_table[(s[i]-' ')&63]-'0';
2019 }
2020 return res;
2021}
2022
2023#if !__EFUN_DEFINED__(count_bits)
2024int count_bits( string s ) {
2025 return(broken_count_bits(s));
2026}
2027#endif
2028
2029
2030// * Teile aus einem Array entfernen *** OBSOLETE
2031deprecated mixed *exclude_array(mixed *arr,int from,int to)
2032{
2033 if (to<from)
2034 to = from;
2035 return arr[0..from-1]+arr[to+1..];
2036}
2037