blob: 680f4d5ba1241ba579b8f3d1190a495968382339 [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);
Zesstra39d284a2020-08-30 23:55:47 +020047int query_wiz_level(object|string 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);
Zesstra39d284a2020-08-30 23:55:47 +020050int query_wiz_grp(object|string wiz);
MG Mud User88f12472016-06-24 23:31:02 +020051public 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
Zesstra39d284a2020-08-30 23:55:47 +0200431int query_wiz_level(object|string 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
MG Mud User88f12472016-06-24 23:31:02 +0200435// * German version of ctime()
436#define TAGE ({"Son","Mon","Die","Mit","Don","Fre","Sam"})
437#define MONATE ({"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug",\
438 "Sep","Okt","Nov","Dez"})
439string dtime(int wann) {
440
441 if (wann == dtime_cache[0])
442 return(dtime_cache[1]);
443
444 int *lt = localtime(wann);
445 return sprintf("%s, %2d. %s %d, %02d:%02d:%02d",
446 TAGE[lt[TM_WDAY]], lt[TM_MDAY], MONATE[lt[TM_MON]],
447 lt[TM_YEAR],lt[TM_HOUR], lt[TM_MIN], lt[TM_SEC]);
448}
449
450// wenn strftime im Driver nicht existiert, ist dies hier ein Alias auf dtime(),
451// zwar stimmt das Format dann nicht, aber die Mudlib buggt nicht und schreibt
452// ein ordentliches Datum/Uhrzeit.
453#if !__EFUN_DEFINED__(strftime)
454varargs string strftime(mixed fmt, int clock, int localized) {
455 if (intp(clock) && clock >= 0)
456 return dtime(clock);
457 else if (intp(fmt) && fmt >= 0)
458 return dtime(fmt);
459
460 return dtime(time());
461}
462#endif //!__EFUN_DEFINED__(strftime)
463
464// * Shutdown mit zusaetzlichem logging
465nomask int shutdown(string reason)
466{
467 string name;
468 string obname;
469 string output;
470
471 if (!reason)
472 return 0;
473 if ( !ARCH_SECURITY && getuid(previous_object())!=ROOTID &&
474 object_name(previous_object())!="/obj/shut" )
475 {
476 write("You have no permission to shut down the gamedriver!\n");
477 return 0;
478 }
479 if ((this_interactive())&&(name=getuid(this_interactive())))
480 {
481 name=capitalize(name);
482 filter(users(),#'tell_object,//'
483 capitalize(name)+" faehrt das Spiel herunter!\n");
484 }
485 else
486 name="ANONYMOUS";
487 if (previous_object()) obname=capitalize(getuid(previous_object()));
488 output=name;
489 if (obname && name!=obname) output=output+" ("+obname+")";
490 if (previous_object()&&object_name(previous_object())=="/obj/shut"){
491 output+=" faehrt das Spiel via Armageddon herunter.\n";
492 output=dtime(time())+": "+output;
493 log_file("GAME_LOG",output+"\n",-1);
494 efun::shutdown();
495 return 1;
496 }
497 output=ctime(time())+": "+output+" faehrt das Spiel herunter.\n";
498 output+=" Grund: "+reason;
499 log_file("GAME_LOG",output+"\n",-1);
500 efun::shutdown();
501 return 1;
502}
503
504// * lowerchar
505
506int lowerchar(int char) {
507 if (char<'A' || char>'Z') return char;
508 return char+32;
509}
510
511// * upperstring
512
513string upperstring(string s)
514{
515#if __EFUN_DEFINED__(upper_case)
516 return(upper_case(s));
517#else
518 int i;
519 if (!stringp(s)) return 0;
520 for (i=sizeof(s)-1;i>=0;i--) s[i]=((s[i]<'a'||s[i]>'z')?s[i]:s[i]-32);
521 return s;
522#endif
523}
524
525// * lowerstring
526
527string lowerstring(string s)
528{
529 if (!stringp(s)) return 0;
530 return lower_case(s);
531}
532
533
534// * GD version
535string version()
536{
537 return __VERSION__;
538}
539
540// * break_string
541// stretch() -- stretch a line to fill a given width
542private string stretch(string s, int width) {
543 int len=sizeof(s);
544 if (len==width) return s;
545
546 // reine Leerzeilen, direkt zurueckgeben
547 string trimmed=trim(s,TRIM_LEFT," ");
548 if (trimmed=="") return s;
549 int start_spaces = len - sizeof(trimmed);
550
551 string* words = explode(trimmed, " ");
552 // der letzte kriegt keine Spaces
553 int word_count=sizeof(words) - 1;
554 // wenn Zeile nur aus einem Wort, wird das Wort zurueckgegeben
555 if (!word_count)
556 return " "*start_spaces + words[0];
557
558 int space_count = width - len;
559
560 int space_per_word=(word_count+space_count) / word_count;
561 // Anz.Woerter mit Zusatz-Space
562 int rest=(word_count+space_count) % word_count;
563 // Rest-Spaces Verteilen
564 foreach (int pos : rest) words[pos]+=" ";
565 return (" "*start_spaces) + implode( words, " "*space_per_word );
566}
567
568// aus Geschwindigkeitsgruenden hat der Blocksatz fuer break_string eine
569// eigene Funktion bekommen:
570private varargs string block_string(string s, int width, int flags) {
571 // wenn BS_LEAVE_MY_LFS, aber kein BS_NO_PARINDENT, dann werden Zeilen mit
572 // einem Leerzeichen begonnen.
573 // BTW: Wenn !BS_LEAVE_MY_LFS, hat der Aufrufer bereits alle \n durch " "
574 // ersetzt.
575 if ( (flags & BS_LEAVE_MY_LFS)
576 && !(flags & BS_NO_PARINDENT))
577 {
578 s = " "+regreplace(s,"\n","\n ",1);
579 }
580
581 // sprintf fuellt die letzte Zeile auf die Feldbreite (hier also
582 // Zeilenbreite) mit Fuellzeichen auf, wenn sie NICHT mit \n umgebrochen
583 // ist. Es wird an die letzte Zeile aber kein Zeilenumbruch angehaengt.
584 // Eigentlich ist das Auffuellen doof, aber vermutlich ist es unnoetig, es
585 // wieder rueckgaengig zu machen.
586 s = sprintf( "%-*=s", width, s);
587
588 string *tmp=explode(s, "\n");
589 // Nur wenn s mehrzeilig ist, Blocksatz draus machen. Die letzte Zeile wird
590 // natuerlich nicht gedehnt. Sie ist dafuer schon von sprintf() aufgefuellt
591 // worden. BTW: Die letzte Zeile endet u.U. noch nicht mit einem \n (bzw.
592 // nur dann, wenn BS_LEAVE_MY_LFS und der uebergebene Text schon nen \n am
593 // Ende der letzten Zeile hat), das macht der Aufrufer...
594 if (sizeof(tmp) > 1)
595 return implode( map( tmp[0..<2], #'stretch/*'*/, width ), "\n" )
596 + "\n" + tmp[<1];
597
598 return s;
599}
600
601public varargs string break_string(string s, int w, mixed indent, int flags)
602{
603 if ( !s || s == "" ) return "";
604
605 if ( !w ) w=78;
606
607 if( intp(indent) )
608 indent = indent ? " "*indent : "";
609
610 int indentlen=stringp(indent) ? sizeof(indent) : 0;
611
612 if (indentlen>w) {
613 set_this_object(previous_object());
614 raise_error(sprintf("break_string: indent longer %d than width %d\n",
615 indentlen,w));
616 // w=((indentlen/w)+1)*w;
617 }
618
619 if (!(flags & BS_LEAVE_MY_LFS))
620 s=regreplace( s, "\n", " ", 1 );
621
622 if ( flags & BS_SINGLE_SPACE )
623 s = regreplace( s, "(^|\n| ) *", "\\1", 1 );
624
625 string prefix="";
626 if (indentlen && flags & BS_PREPEND_INDENT) {
627 if (indentlen+sizeof(s) > w ||
628 (flags & BS_LEAVE_MY_LFS) && strstr(s,"\n")>-1) {
629 prefix=indent+"\n";
630 indent=(flags & BS_NO_PARINDENT) ? "" : " ";
631 indentlen=sizeof(indent);
632 }
633 }
634
635 if ( flags & BS_BLOCK ) {
636 /*
637 s = implode( map( explode( s, "\n" ),
638 #'block_string, w, indentlen, flags),
639 "" );
640 */
641 s = block_string( s , w - indentlen, flags );
642 }
643 else {
644 s = sprintf("%-1.*=s",w-indentlen,s);
645 }
646 if ( s[<1] != '\n' ) s += "\n";
647
648 if ( !indentlen ) return prefix + s;
649
650 string indent2 = ( flags & BS_INDENT_ONCE ) ? (" "*indentlen) :indent;
651
652 return prefix + indent +
653 regreplace( s[0..<2], "\n", "\n"+indent2, 1 ) + "\n";
654 /*
655 string *buf;
656
657 buf = explode( s, "\n" );
658 return prefix + indent + implode( buf[0..<2], "\n"+indent2 ) + buf[<1] + "\n";
659 */
660}
661
662// * Elemente aus mapping loeschen - mapping vorher kopieren
663
664mapping m_copy_delete(mapping m, mixed key) {
665 return m_delete(copy(m), key);
666}
667
668// * times
669int last_reboot_time()
670{
671 return __BOOT_TIME__;
672}
673
674int first_boot_time()
675{
676 return 701517600;
677}
678
679int exist_days()
680{
681 return (((time()-first_boot_time())/8640)+5)/10;
682}
683
684// * uptime :)
685string uptime()
686{
687 int t;
688 int tmp;
689 string s;
690
691 t=time()-__BOOT_TIME__;
692 s="";
693 if (t>=86400)
694 s+=sprintf("%d Tag%s, ",tmp=t/86400,(tmp==1?"":"e"));
695 if (t>=3600)
696 s+=sprintf("%d Stunde%s, ",tmp=(t=t%86400)/3600,(tmp==1?"":"n"));
697 if (t>60)
698 s+=sprintf("%d Minute%s und ",tmp=(t=t%3600)/60,(tmp==1?"":"n"));
699 return s+sprintf("%d Sekunde%s",t=t%60,(t==1?"":"n"));
700}
701
702// * Was tun bei 'dangling' lfun-closures ?
703void dangling_lfun_closure() {
704 raise_error("dangling lfun closure\n");
705}
706
707// * Sperren ausser fuer master/simul_efun
708
709#if __EFUN_DEFINED__(set_environment)
710nomask void set_environment(object o1, object o2) {
711 raise_error("Available only for root\n");
712}
713#endif
714
715nomask void set_this_player(object pl) {
716 raise_error("Available only for root\n");
717}
718
MG Mud User88f12472016-06-24 23:31:02 +0200719// * Jetzt auch closures
720int process_flag;
721
722public nomask int process_call()
723{
724 if (process_flag>0)
725 return process_flag;
726 else return(0);
727}
728
729private nomask string _process_string(string str,object po) {
730 set_this_object(po);
731 return(efun::process_string(str));
732}
733
Zesstra16158af2020-09-01 00:07:59 +0200734private nomask int _illegal_ps_call(string s)
735{
736 int i_arg = strstr(s,"|"); // erst arg-trenner suchen
737 // dann ggf. von dort rueckwaerts den obj-trenner suchen
738 int i_ob = i_arg!=-1 ? strrstr(s, ":", i_arg)
739 : strstr(s,":");
740 // Wenn kein ":" vorkommt, ists max. ein objektinterner Call und erlaubt
741 if (i_ob == -1)
742 return 0;
743 string obname = (i_arg != -1) ? s[i_ob+1..i_arg-1]
744 : s[i_ob+1..];
745 // Wenn es das nicht gibt, ist es auch OK - process_string laedt keine
746 // nicht-geladenen Objekte. Das hier ist auch der Fall, wenn in
747 // dem Substring nen : vorkommt, es aber gar kein process_call ist...
748 object ob = find_object(obname);
749 if (!ob)
750 return 0;
751 // Es gibt ein Objekt. Jetzt wird es spannend. Erlaubt sind calls zwischen
Zesstra2ad183a2020-09-03 20:05:18 +0200752 // Objekten, welche dieselbe UID haben oder vom gleichen Magier stammen.
753 if (getuid(ob) == getuid(previous_object()) ||
754 REAL_UID(ob) == REAL_UID(previous_object()))
Zesstra16158af2020-09-01 00:07:59 +0200755 return 0;
756 // Alles andere ist nicht erlaubt
757 return 1;
758}
759
Zesstra739c58a2020-08-30 23:45:26 +0200760nomask string process_string( string|closure str )
MG Mud User88f12472016-06-24 23:31:02 +0200761{
762 string tmp, err;
Zesstrac5af6a72020-09-03 20:03:06 +0200763 int flag;
764
765 // Hmpf, es wird tatsaechlich reihenweise mit 0 gerufen.
766 if (!str)
767 return 0;
MG Mud User88f12472016-06-24 23:31:02 +0200768
Zesstra739c58a2020-08-30 23:45:26 +0200769 // process_string() wird nur noch ausgewertet, wenn der Aufrufer einen
Zesstra16158af2020-09-01 00:07:59 +0200770 // Level von maximal 30 hat. Das schliesst alle Objekten in /d/, /p/ und den
Zesstra739c58a2020-08-30 23:45:26 +0200771 // Gilden ein, aber verhindert es fuer alle hochstufigen Magier und ihre
772 // Objekte. Ausserdem erlauben wir keine Auswertung mehr fuer
773 // Spielerobjekte, wenn sie mehr als Seher sind.
Zesstra16158af2020-09-01 00:07:59 +0200774 // TODO: aus Spielershells ausbauen
Zesstra739c58a2020-08-30 23:45:26 +0200775 // TODO 2: ganz ausbauen.
776 if ( (query_once_interactive(previous_object())
777 && query_wiz_level(previous_object()) > SEER_LVL
778 )
Zesstrac8030682020-08-31 20:50:08 +0200779 || query_wiz_level(getuid(previous_object())) > SPECIAL_LVL)
Zesstra739c58a2020-08-30 23:45:26 +0200780 {
Zesstra86e26da2020-08-31 21:13:17 +0200781 // Ein Fehler wird aber nur ausgeloest, falls der String ein @@ enthaelt,
Zesstra284a35f2020-08-31 21:11:04 +0200782 // ansonsten koennen wir den ohne Fehler returnieren.
783 if (stringp(str) && strstr(str, "@@") == -1)
784 return str;
785 else
786 {
787 set_this_object(previous_object());
788 raise_error("Illegale Benutzung von process_string(). Aufrufer "
789 "ist Magiershell oder Objekt mit Level > 30.\n");
790 }
Zesstra739c58a2020-08-30 23:45:26 +0200791 }
Zesstra16158af2020-09-01 00:07:59 +0200792 // Kein Aufruf von Funktionen in Objekten anderer Magier erlaubt.
Zesstra98c5d582020-09-03 21:36:19 +0200793 if (stringp(str))
Zesstra16158af2020-09-01 00:07:59 +0200794 {
Zesstra98c5d582020-09-03 21:36:19 +0200795 foreach(string s: explode(str, "@@"))
Zesstra16158af2020-09-01 00:07:59 +0200796 {
Zesstra98c5d582020-09-03 21:36:19 +0200797 if (sizeof(s) && _illegal_ps_call(s))
798 {
799 set_this_object(previous_object());
800 raise_error("Illegale Benutzung von process_string(). Aufruf in "
801 "in fremder UID nicht erlaubt.\n");
802 }
Zesstra16158af2020-09-01 00:07:59 +0200803 }
804 }
Zesstra98c5d582020-09-03 21:36:19 +0200805 else if ( closurep(str) ) {
MG Mud User88f12472016-06-24 23:31:02 +0200806 set_this_object( previous_object() );
807 return funcall(str);
808 }
MG Mud User88f12472016-06-24 23:31:02 +0200809
810 if ( !(flag = process_flag > time() - 60))
811 process_flag=time();
812
813 err = catch(tmp = funcall(#'_process_string,str,previous_object()); publish);
814
815 if ( !flag )
816 process_flag=0;
817
818 if (err) {
819 // Verarbeitung abbrechen
820 set_this_object(previous_object());
821 raise_error(err);
822 }
823 return tmp;
824}
825
826// 'mkdir -p' - erzeugt eine komplette Hierarchie von Verzeichnissen.
827// wenn das Verzeichnis angelegt wurde oder schon existiert, wird 1
828// zurueckgeliefert, sonst 0.
829// Wirft einen Fehler, wenn das angebene Verzeichnis nicht absolut ist!
830public int mkdirp(string dir) {
831 // wenn es nur keinen fuehrenden / gibt, ist das ein Fehler.
832 if (strstr(dir, "/") != 0)
833 raise_error("mkdirp(): Pfad ist nicht absolute.\n");
834 // cut off trailing /...
835 if (dir[<1]=='/')
836 dir = dir[0..<2];
837
838 int fstat = file_size(dir);
839 // wenn es schon existiert, tun wir einfach so, als haetten wir es angelegt.
840 if (fstat == FSIZE_DIR)
841 return 1;
842 // wenn schon ne Datei existiert, geht es nicht.
843 if (fstat != FSIZE_NOFILE)
844 return 0;
845 // wenn es nur einen / gibt (den fuehrenden), dann ist es ein
846 // toplevel-verzeichnis, was direkt angelegt wird.
847 if (strrstr(dir,"/")==0) {
848 return funcall(bind_lambda(#'efun::mkdir, previous_object()), dir);
849 }
850
851 // mkdir() nicht direkt rufen, sondern vorher als closure ans aufrufende
852 // Objekt binden. Sonst laeuft die Rechtepruefung in valid_write() im Master
853 // unter der Annahme, dass die simul_efun.c mit ihrer root id was will.
854
855 // jetzt rekursiv die Verzeichnishierarchie anlegen. Wenn das erfolgreich
856 // ist, dann koennen wir jetzt mit mkdir das tiefste Verzeichnis anlegen
857 if (mkdirp(dir[0..strrstr(dir,"/")-1]) == 1)
858 return funcall(bind_lambda(#'efun::mkdir, previous_object()), dir);
Zesstraa25c9c12018-11-17 00:09:25 +0100859 return 0;
MG Mud User88f12472016-06-24 23:31:02 +0200860}
861
862
863// * Properties ggfs. mitspeichern
864mixed save_object(mixed name)
865{
866 mapping properties;
867 mapping save;
868 mixed index, res;
869 int i;
Zesstraa012d622017-01-29 20:38:48 +0100870 string oldpath;
MG Mud User88f12472016-06-24 23:31:02 +0200871
872 // nur Strings und 0 zulassen
873 if ((!stringp(name) || !sizeof(name)) &&
874 (!intp(name) || name!=0)) {
875 set_this_object(previous_object());
876 raise_error(sprintf(
877 "Only non-empty strings and 0 may be used as filename in "
878 "sefun::save_object()! Argument was %O\n",name));
879 }
880
Zesstraa012d622017-01-29 20:38:48 +0100881 if (stringp(name)) {
882 // abs. Pfad erzeugen. *seufz*
883 if (name[0]!='/')
884 name = "/" + name;
Zesstraa012d622017-01-29 20:38:48 +0100885 // automatisch in LIBDATADIR speichern
886 if (strstr(name,"/"LIBDATADIR"/") != 0) {
Zesstra60aed3e2017-01-31 16:03:51 +0100887 oldpath = name;
Zesstraa012d622017-01-29 20:38:48 +0100888 name = "/"LIBDATADIR + name;
889 // wenn das Verzeichnis nicht existiert, ggf. anlegen
890 string dir = name[0..strrstr(name,"/")-1];
891 if (file_size(dir) != FSIZE_DIR) {
892 if (mkdirp(dir) != 1)
893 raise_error("save_object(): kann Verzeichnis " + dir
894 + " nicht anlegen!");
895 }
896 }
897 }
898
MG Mud User88f12472016-06-24 23:31:02 +0200899 save = m_allocate(0, 2);
Zesstra4dbb9882019-11-26 21:26:36 +0100900 properties = previous_object()->QueryProperties();
MG Mud User88f12472016-06-24 23:31:02 +0200901
902 if(mappingp(properties))
903 {
904 // delete all entries in mapping properties without SAVE flag!
905 index = m_indices(properties);
906 for(i = sizeof(index)-1; i>=0;i--)
907 {
908 if(properties[index[i], F_MODE] & SAVE)
909 {
910 save[index[i]] = properties[index[i]];
911 save[index[i], F_MODE] =
912 properties[index[i], F_MODE] &
913 (~(SETMNOTFOUND|QUERYMNOTFOUND|QUERYCACHED|SETCACHED));
914 }
915 }
916 }
917 else save = ([]);
918
919 // save object!
920 previous_object()->_set_save_data(save);
921 // format: wie definiert in config.h
Zesstraa012d622017-01-29 20:38:48 +0100922 if (stringp(name)) {
MG Mud User88f12472016-06-24 23:31:02 +0200923 res = funcall(bind_lambda(#'efun::save_object, previous_object()), name,
924 __LIB__SAVE_FORMAT_VERSION__);
Zesstraa012d622017-01-29 20:38:48 +0100925 // wenn erfolgreich und noch nen Savefile existiert, was nicht unter
926 // /data/ liegt, wird das geloescht.
927 if (!res && oldpath
928 && file_size(oldpath+".o") >= 0)
929 rm(oldpath+".o");
930 }
MG Mud User88f12472016-06-24 23:31:02 +0200931 else
932 res = funcall(bind_lambda(#'efun::save_object, previous_object()),
933 __LIB__SAVE_FORMAT_VERSION__);
Zesstraa012d622017-01-29 20:38:48 +0100934
MG Mud User88f12472016-06-24 23:31:02 +0200935 previous_object()->_set_save_data(0);
936
937#ifdef IOSTATS
938 // Stats...
939 struct iostat_s stat = (<iostat_s>);
940 stat->oname = object_name(previous_object());
941 stat->time = time();
942 //stat->size = (int)object_info(previous_object(),OINFO_MEMORY,
943 // OIM_TOTAL_DATA_SIZE);
944 if (stringp(name))
945 stat->size = file_size(name + ".o");
946 else
947 stat->sizeof(res);
948 //debug_message("saveo: "+saveo_stat[0]+"\n");
949 saveo_stat[1][saveo_stat[0]] = stat;
950 saveo_stat[0] = (saveo_stat[0] + 1) % sizeof(saveo_stat[1]);
951 //debug_message("saveo 2: "+saveo_stat[0]+"\n");
952#endif
953
954 return res;
955}
956
957// * Auch Properties laden
958int restore_object(string name)
959{
960 int result;
961 mixed index;
962 mixed save;
963 mapping properties;
964 int i;
MG Mud User88f12472016-06-24 23:31:02 +0200965
Zesstra94381a42017-01-29 22:37:52 +0100966 if (sizeof(name) < 1)
967 {
968 set_this_object(previous_object());
969 raise_error("Bad arg 1 to restore_object(): expected non-empty "
970 "'string'.\n");
971 }
972
Zesstraa012d622017-01-29 20:38:48 +0100973 // Wenn name vermutlich ein Pfad (also nicht mit #x:y anfaengt)
974 if (name[0] != '#')
975 {
976 // abs. Pfad erzeugen *seufz*
Zesstra984bde92017-01-29 22:19:52 +0100977 if (name[0]!='/')
Zesstraa012d622017-01-29 20:38:48 +0100978 name = "/" + name;
979
Zesstraf2a0d492017-04-29 11:05:46 +0200980 // .c am Ende loeschen, sonst wird das File ggf. nicht gefunden.
981 if(name[<2..]==".c")
982 name=name[..<3];
983
Zesstraa012d622017-01-29 20:38:48 +0100984 // wenn kein /data/ vorn steht, erstmal gucken, ob das Savefile unter
985 // /data/ existiert. Wenn ja, wird das geladen.
Zesstra984bde92017-01-29 22:19:52 +0100986 if (strstr(name,"/"LIBDATADIR"/") != 0)
Zesstraa012d622017-01-29 20:38:48 +0100987 {
988 string path = "/"LIBDATADIR + name;
989 if (file_size(path + ".o") >= 0)
990 name = path;
991 }
992 }
993
MG Mud User88f12472016-06-24 23:31:02 +0200994 // get actual property settings (by create())
Zesstra4dbb9882019-11-26 21:26:36 +0100995 properties = previous_object()->QueryProperties();
MG Mud User88f12472016-06-24 23:31:02 +0200996
997// DEBUG(sprintf("RESTORE %O\n",name));
998 // restore object
999 result=funcall(bind_lambda(#'efun::restore_object, previous_object()), name);
1000 //'))
1001 //_get_save_data liefert tatsaechlich mixed zurueck, wenn das auch immer ein
1002 //mapping sein sollte.
Zesstra4dbb9882019-11-26 21:26:36 +01001003 save = previous_object()->_get_save_data();
1004 if((save))
MG Mud User88f12472016-06-24 23:31:02 +02001005 {
1006 index = m_indices(save);
1007 for(i = sizeof(index)-1; i>=0; i--)
1008 {
1009 properties[index[i]] = save[index[i]];
1010 properties[index[i], F_MODE] = save[index[i], F_MODE]
1011 &~(SETCACHED|QUERYCACHED);
1012 }
1013 }
1014 else properties = ([]);
1015
1016 // restore properties
1017 funcall(
1018 bind_lambda(
1019 unbound_lambda(({'arg}), //'})
1020 ({#'call_other,({#'this_object}),
1021 "SetProperties",'arg})),//')
1022 previous_object()),properties);
1023 previous_object()->_set_save_data(0);
1024
1025#ifdef IOSTATS
1026 // Stats...
1027 //debug_message("restoreo: "+restoreo_stat[0]+"\n");
1028 struct iostat_s stat = (<iostat_s>);
1029 stat->oname = object_name(previous_object());
1030 stat->time = time();
1031 //stat->size = (int)object_info(previous_object(),OINFO_MEMORY,
1032 // OIM_TOTAL_DATA_SIZE);
1033 stat->size = file_size(name + ".o");
1034 restoreo_stat[1][restoreo_stat[0]] = stat;
1035
1036 restoreo_stat[0] = (restoreo_stat[0] + 1) % sizeof(restoreo_stat[1]);
1037#endif
1038
1039 return result;
1040}
1041
1042// * HB eines Objektes ein/ausschalten
1043int set_object_heart_beat(object ob, int flag)
1044{
1045 if (objectp(ob))
1046 return funcall(bind_lambda(#'efun::configure_object,ob), ob, OC_HEART_BEAT, flag);
Zesstra738a1ef2021-05-14 17:01:19 +02001047 return 0;
MG Mud User88f12472016-06-24 23:31:02 +02001048}
1049
1050// * Magierlevelgruppen ermitteln
Zesstra39d284a2020-08-30 23:55:47 +02001051int query_wiz_grp(object|string wiz)
MG Mud User88f12472016-06-24 23:31:02 +02001052{
1053 int lev;
1054
1055 lev=query_wiz_level(wiz);
1056 if (lev<SEER_LVL) return 0;
1057 if (lev>=GOD_LVL) return lev;
1058 if (lev>=ARCH_LVL) return ARCH_GRP;
1059 if (lev>=ELDER_LVL) return ELDER_GRP;
1060 if (lev>=LORD_LVL) return LORD_GRP;
1061 if (lev>=SPECIAL_LVL) return SPECIAL_GRP;
1062 if (lev>=DOMAINMEMBER_LVL) return DOMAINMEMBER_GRP;
1063 if (lev>=WIZARD_LVL) return WIZARD_GRP;
1064 if (lev>=LEARNER_LVL) return LEARNER_GRP;
1065 return SEER_GRP;
1066}
1067
1068mixed *wizlist_info()
1069{
1070 if (ARCH_SECURITY || !extern_call())
1071 return efun::wizlist_info();
1072 return 0;
1073}
1074
1075// * wizlist ausgeben
1076varargs void wizlist(string name, int sortkey ) {
1077
1078 if (!name)
1079 {
1080 if (this_player())
1081 name = getuid(this_player());
1082 if (!name)
1083 return;
1084 }
1085
1086 // Schluessel darf nur in einem gueltigen Bereich sein
1087 if (sortkey<WL_NAME || sortkey>=WL_SIZE) sortkey=WL_COST;
1088
1089 mixed** wl = efun::wizlist_info();
1090 // nach <sortkey> sortieren
1091 wl = sort_array(wl, function int (mixed a, mixed b)
1092 {return a[sortkey] < b[sortkey]; } );
1093
1094 // Summe ueber alle Kommandos ermitteln.
1095 int total_cmd, i;
1096 int pos=-1;
1097 foreach(mixed entry : wl)
1098 {
1099 total_cmd += entry[WL_COMMANDS];
1100 if (entry[WL_NAME] == name)
1101 pos = i;
1102 ++i;
1103 }
1104
1105 if (pos < 0 && name != "ALL" && name != "TOP100")
1106 return;
1107
1108 if (name == "TOP100")
1109 {
1110 if (sizeof(wl) > 100)
1111 wl = wl[0..100];
1112 else
1113 wl = wl;
1114 }
1115 // um name herum schneiden
1116 else if (name != "ALL")
1117 {
1118 if (sizeof(wl) <= 21)
1119 wl = wl;
1120 else if (pos + 10 < sizeof(wl) && pos - 10 > 0)
1121 wl = wl[pos-10..pos+10];
1122 else if (pos <=21)
1123 wl = wl[0..20];
1124 else if (pos >= sizeof(wl) - 21)
1125 wl = wl[<21..];
1126 else
1127 wl = wl;
1128 }
1129
1130 write("\nWizard top score list\n\n");
1131 if (total_cmd == 0)
1132 total_cmd = 1;
1133 printf("%-20s %-6s %-3s %-17s %-6s %-6s %-6s\n",
1134 "EUID", "cmds", "%", "Costs", "HB", "Arrays","Mapp.");
1135 foreach(mixed e: wl)
1136 {
1137 printf("%-:20s %:6d %:2d%% [%:6dk,%:6dG] %:6d %:6d %:6d\n",
1138 e[WL_NAME], e[WL_COMMANDS], e[WL_COMMANDS] * 100 / total_cmd,
1139 e[WL_COST] / 1000, e[WL_TOTAL_GIGACOST],
1140 e[WL_HEART_BEATS], e[WL_ARRAY_TOTAL], e[WL_MAPPING_TOTAL]
1141 );
1142 }
1143 printf("\nTotal %7d (%d)\n\n", total_cmd, sizeof(wl));
1144}
1145
1146
1147// Ab hier folgen Hilfsfunktionen fuer call_out() bzw. fuer deren Begrenzung
1148
1149// ermittelt das Objekt des Callouts.
1150private object _call_out_obj( mixed call_out ) {
1151 return pointerp(call_out) ? call_out[0] : 0;
1152}
1153
1154private void _same_object( object ob, mapping m ) {
1155 // ist nicht so bloed, wie es aussieht, s. nachfolgede Verwendung von m
1156 if ( m[ob] )
1157 m[ob] += ({ ob });
1158 else
1159 m[ob] = ({ ob });
1160}
1161
1162// alle Objekte im Mapping m zusammenfassen, die den gleichen Loadname (Name der
1163// Blueprint) haben, also alle Clones der BP, die Callouts laufen haben.
1164// Objekte werden auch mehrfach erfasst, je nach Anzahl ihrer Callouts.
1165private void _same_path( object key, object *obs, mapping m ) {
1166 string path;
1167 if (!objectp(key) || !pointerp(obs)) return;
1168
1169 path = load_name(key);
1170
1171 if ( m[path] )
1172 m[path] += obs;
1173 else
1174 m[path] = obs;
1175}
1176
1177// key kann object oder string sein.
1178private int _too_many( mixed key, mapping m, int i ) {
1179 return sizeof(m[key]) >= i;
1180}
1181
1182// alle Objekte in obs zerstoeren und Fehlermeldung ausgeben. ACHTUNG: Die
1183// Objekte werden idR zu einer BP gehoeren, muessen aber nicht! In dem Fall
1184// wird auf der Ebene aber nur ein Objekt in der Fehlermeldung erwaehnt.
1185private void _destroy( mixed key, object *obs, string text, int uid ) {
1186 if (!pointerp(obs)) return;
1187 // Array mit unique Eintraege erzeugen.
1188 obs = m_indices( mkmapping(obs) );
1189 // Fehlermeldung auf der Ebene ausgeben, im catch() mit publish, damit es
1190 // auf der Ebene direkt scrollt, der backtrace verfuegbar ist (im
1191 // gegensatz zur Loesung mittels Callout), die Ausfuehrung aber weiter
1192 // laeuft.
1193 catch( efun::raise_error(
1194 sprintf( text,
Zesstra4dbb9882019-11-26 21:26:36 +01001195 uid ? master()->creator_file(key) : key,
MG Mud User88f12472016-06-24 23:31:02 +02001196 sizeof(obs), object_name(obs[<1]) ) );publish);
1197 // Und weg mit dem Kram...
1198 filter( obs, #'efun::destruct/*'*/ );
1199}
1200
1201// Alle Objekt einer UID im Mapping m mit UID als KEys zusammenfassen. Objekt
1202// sind dabei nicht unique.
1203private void _same_uid( string key, object *obs, mapping m, closure cf ) {
1204 string uid;
1205
1206 if ( !pointerp(obs) || !sizeof(obs) )
1207 return;
1208
1209 uid = funcall( cf, key );
1210
1211 if ( m[uid] )
1212 m[uid] += obs; // obs ist nen Array
1213 else
1214 m[uid] = obs;
1215}
1216
1217private int _log_call_out(mixed co)
1218{
1219 log_file("TOO_MANY_CALLOUTS",
1220 sprintf("%s::%O (%d)\n",object_name(co[0]),co[1],co[2]),
1221 200000);
1222 return 0;
1223}
1224
1225private int last_callout_log=0;
1226
1227nomask varargs void call_out( varargs mixed *args )
1228{
Zesstra738a1ef2021-05-14 17:01:19 +02001229 mixed *call_outs;
MG Mud User88f12472016-06-24 23:31:02 +02001230
1231 // Bei >600 Callouts alle Objekte killen, die mehr als 30 Callouts laufen
1232 // haben.
1233 if ( efun::driver_info(DI_NUM_CALLOUTS) > 600
1234 && geteuid(previous_object()) != ROOTID )
1235 {
1236 // Log erzeugen...
1237 if (last_callout_log <
1238 efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES) - 10
1239 && get_eval_cost() > 200000)
1240 {
1241 last_callout_log = efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES);
1242 log_file("TOO_MANY_CALLOUTS",
1243 sprintf("\n%s: ############ Too many callouts: %d ##############\n",
1244 strftime("%y%m%d-%H%M%S"),
1245 efun::driver_info(DI_NUM_CALLOUTS)));
1246 filter(efun::call_out_info(), #'_log_call_out);
1247 }
1248 // Objekte aller Callouts ermitteln
1249 call_outs = map( efun::call_out_info(), #'_call_out_obj );
1250 mapping objectmap = ([]);
1251 filter( call_outs, #'_same_object, &objectmap );
1252 // Master nicht grillen...
1253 efun::m_delete( objectmap, master(1) );
1254 // alle Objekte raussuchen, die zuviele haben...
1255 mapping res = filter_indices( objectmap, #'_too_many, objectmap, 29 );
1256 // und ueber alle Keys gehen, an _destroy() werden Key und Array mit
1257 // Objekten uebergeben (in diesem Fall sind Keys und Array mit
1258 // Objekten jeweils das gleiche Objekt).
1259 if ( sizeof(res) )
1260 walk_mapping(res, #'_destroy, "CALL_OUT overflow by single "
1261 "object [%O]. Destructed %d objects. [%s]\n", 0 );
1262
1263 // Bei (auch nach dem ersten Aufraeumen noch) >800 Callouts alle
1264 // Objekte killen, die mehr als 50 Callouts laufen haben - und
1265 // diesmal zaehlen Clones nicht eigenstaendig! D.h. es werden alle
1266 // Clones einer BP gekillt, die Callouts laufen haben, falls alle
1267 // diese Objekte _zusammen_ mehr als 50 Callouts haben!
1268 if ( efun::driver_info(DI_NUM_CALLOUTS) > 800 ) {
1269 // zerstoerte Objekte von der letzten Aktion sind in objectmap nicht
1270 // mehr drin, da sie dort als Keys verwendet wurden.
1271 mapping pathmap=([]);
1272 // alle Objekt einer BP in res sortieren, BP-Name als Key, Arrays
1273 // von Objekten als Werte.
1274 walk_mapping( objectmap, #'_same_path, &pathmap);
1275 // alle BPs (und ihre Objekte) raussuchen, die zuviele haben...
1276 res = filter_indices( pathmap, #'_too_many/*'*/, pathmap, 50 );
1277 // und ueber alle Keys gehen, an _destroy() werden die Clones
1278 // uebergeben, die Callouts haben.
1279 if ( sizeof(res) )
1280 walk_mapping( res, #'_destroy/*'*/, "CALL_OUT overflow by file "
1281 "'%s'. Destructed %d objects. [%s]\n", 0 );
1282
1283 // Wenn beide Aufraeumarbeiten nichts gebracht haben und immer
1284 // noch >1000 Callouts laufen, werden diesmal alle Callouts
1285 // einer UID zusammengezaehlt.
1286 // Alle Objekte einer UID, die es in Summe aller ihrer Objekt mit
1287 // Callouts auf mehr als 100 Callouts bringt, werden geroestet.
1288 if (efun::driver_info(DI_NUM_CALLOUTS) > 1000)
1289 {
1290 // das nach BP-Namen vorgefilterte Mapping jetzt nach UIDs
1291 // zusammensortieren. Zerstoerte Clones filter _same_uid()
1292 // raus.
1293 mapping uidmap=([]);
1294 walk_mapping( pathmap, #'_same_uid, &uidmap,
1295 symbol_function( "creator_file",
1296 "/secure/master" ) );
1297 // In res nun UIDs als Keys und Arrays von Objekten als Werte.
1298 // Die rausfiltern, die mehr als 100 Objekte (non-unique, d.h.
1299 // 100 Callouts!) haben.
1300 res = filter_indices( uidmap, #'_too_many, uidmap, 100 );
1301 // und erneut ueber die Keys laufen und jeweils die Arrays mit
1302 // den Objekten zur Zerstoerung an _destroy()...
1303 if ( sizeof(res) )
1304 walk_mapping( res, #'_destroy, "CALL_OUT overflow by "
1305 "UID '%s'. Destructed %d objects. [%s]\n",
1306 1 );
1307 }
1308 }
1309 }
1310
1311 // Falls das aufrufende Objekt zerstoert wurde beim Aufraeumen
1312 if ( !previous_object() )
1313 return;
1314
1315 set_this_object( previous_object() );
1316 apply( #'efun::call_out, args );
1317 return;
1318}
1319
1320mixed call_out_info() {
1321
1322 object po = previous_object();
1323 mixed coi = efun::call_out_info();
1324
1325 // ungefilterten Output nur fuer bestimmte Objekte, Objekte in /std oder
1326 // /obj haben die BackboneID.
1327 if (query_wiz_level(getuid(po)) >= ARCH_LVL
Zesstra4dbb9882019-11-26 21:26:36 +01001328 || master()->creator_file(load_name(po)) == BACKBONEID ) {
MG Mud User88f12472016-06-24 23:31:02 +02001329 return coi;
1330 }
1331 else {
1332 return filter(coi, function mixed (mixed arr) {
1333 if (pointerp(arr) && arr[0]==po)
1334 return 1;
1335 else return 0; });
1336 }
1337}
1338
1339// * Zu einer closure das Objekt, an das sie gebunden ist, suchen
1340// NICHT das Objekt, was ggf. die lfun definiert!
1341mixed query_closure_object(closure c) {
1342 return
1343 CLOSURE_IS_UNBOUND_LAMBDA(get_type_info(c, 1)) ?
1344 0 :
1345 (to_object(c) || -1);
1346}
1347
1348// * Wir wollen nur EIN Argument ... ausserdem checks fuer den Netztotenraum
1349varargs void move_object(mixed what, mixed where)
1350{
1351 object po,tmp;
1352
1353 po=previous_object();
1354 if (!where)
1355 {
1356 where=what;
1357 what=po;
1358 }
1359 if (((stringp(where) && where==NETDEAD_ROOM ) ||
1360 (objectp(where) && where==find_object(NETDEAD_ROOM))) &&
1361 objectp(what) && object_name(what)!="/obj/sperrer")
1362 {
1363 if (!query_once_interactive(what))
1364 {
1365 what->remove();
1366 if (what) destruct(what);
1367 return;
1368 }
1369 if (living(what) || interactive(what))
1370 {
1371 log_file("NDEAD2",sprintf("TRYED TO MOVE TO NETDEAD: %O\n",what));
1372 return;
1373 }
1374 set_object_heart_beat(what,0);
1375 }
1376 tmp=what;
1377 while (tmp=environment(tmp))
1378 // Ja. Man ruft die _set_xxx()-Funktionen eigentlich nicht direkt auf.
1379 // Aber das Lichtsystem ist schon *so* rechenintensiv und gerade der
1380 // P_LAST_CONTENT_CHANGE-Cache wird *so* oft benoetigt, dass es mir
1381 // da um jedes bisschen Rechenzeit geht.
1382 // Der Zweck heiligt ja bekanntlich die Mittel. ;-)
1383 //
1384 // Tiamak
1385 tmp->_set_last_content_change();
1386 funcall(bind_lambda(#'efun::move_object,po),what,where);
1387 if (tmp=what)
1388 while (tmp=environment(tmp))
1389 tmp->_set_last_content_change();
1390}
1391
1392
1393void start_simul_efun() {
1394 mixed *info;
1395
1396 // Falls noch nicht getan, extra_wizinfo initialisieren
1397 if ( !pointerp(info = get_extra_wizinfo(0)) )
1398 set_extra_wizinfo(0, info = allocate(BACKBONE_WIZINFO_SIZE));
1399
1400 InitLivingData(info);
1401
1402 set_next_reset(10); // direkt mal aufraeumen
1403}
1404
1405protected void reset() {
1406 set_next_reset(7200);
1407 CleanLivingData();
1408}
1409
1410#if !__EFUN_DEFINED__(absolute_hb_count)
1411int absolute_hb_count() {
1412 return efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES);
1413}
1414#endif
1415
1416void __set_environment(object ob, mixed target)
1417{
1418 string path;
1419 object obj;
1420
1421 if (!objectp(ob))
1422 return;
1423 if (!IS_ARCH(geteuid(previous_object())) || !ARCH_SECURITY )
1424 return;
1425 if (objectp(target))
1426 {
1427 efun::set_environment(ob,target);
1428 return;
1429 }
Zesstra4dbb9882019-11-26 21:26:36 +01001430 path=master()->make_path_absolute(target);
MG Mud User88f12472016-06-24 23:31:02 +02001431 if (stringp(path) && file_size(path+".c")>=0 &&
1432 !catch(load_object(path);publish) )
1433 {
1434 obj=find_object(path);
1435 efun::set_environment(ob,obj);
1436 return;
1437 }
1438}
1439
1440void _dump_wizlist(string file, int sortby) {
1441 int i;
1442 mixed *a;
1443
1444 if (!LORD_SECURITY)
1445 return;
1446 if (!master()->valid_write(file,geteuid(previous_object()),"write_file"))
1447 {
1448 write("NO WRITE PERMISSION\n");
1449 return;
1450 }
1451 a = wizlist_info();
1452 a = sort_array(a, lambda( ({'a,'b}),
1453 ({#'<,
1454 ({#'[,'a,sortby}),
1455 ({#'[,'b,sortby})
1456 })));
1457 rm(file);
1458 for (i=sizeof(a)-1;i>=0;i--)
1459 write_file(file,sprintf("%-11s: eval=%-8d cmds=%-6d HBs=%-5d array=%-5d mapping=%-7d\n",
1460 a[i][WL_NAME],a[i][WL_TOTAL_COST],a[i][WL_COMMANDS],a[i][WL_HEART_BEATS],
1461 a[i][WL_ARRAY_TOTAL],a[i][WL_CALL_OUT]));
1462}
1463
1464public varargs object deep_present(mixed what, object ob) {
1465
1466 if(!objectp(ob))
1467 ob=previous_object();
1468 // Wenn ein Objekt gesucht wird: Alle Envs dieses Objekts ermitteln und
1469 // schauen, ob in diesen ob vorkommt. Dann ist what in ob enthalten.
1470 if(objectp(what)) {
1471 object *envs=all_environment(what);
1472 // wenn ob kein Environment hat, ist es offensichtlich nicht in what
1473 // enthalten.
1474 if (!pointerp(envs)) return 0;
1475 if (member(envs, ob) != -1) return what;
1476 }
1477 // sonst wirds teurer, ueber alle Objekte im (deep) Inv laufen und per id()
1478 // testen. Dabei muss aber die gewuenschte Nr. ("flasche 3") abgeschnitten
1479 // werden und selber gezaehlt werden, welche das entsprechende Objekt ist.
1480 else if (stringp(what)) {
1481 int cnt;
1482 string newwhat;
1483 if(sscanf(what,"%s %d",newwhat,cnt)!=2)
1484 cnt=1;
1485 else
1486 what=newwhat;
1487 foreach(object invob: deep_inventory(ob)) {
1488 if (invob->id(what) && !--cnt)
1489 return invob;
1490 }
1491 }
1492 else {
1493 set_this_object(previous_object());
1494 raise_error(sprintf("Wrong argument 1 to deep_present(). "
1495 "Expected \"object\" or \"string\", got %.50O.\n",
1496 what));
1497 }
1498 return 0;
1499}
1500
1501mapping dump_ip_mapping()
1502{
1503 return 0;
1504}
1505
1506nomask void swap(object obj)
1507{
1508 write("Your are not allowed to swap objects by hand!\n");
1509 return;
1510}
1511
1512nomask varargs void garbage_collection(string str)
1513{
1514 if(previous_object()==0 || !IS_ARCH(geteuid(previous_object()))
1515 || !ARCH_SECURITY)
1516 {
1517 write("Call GC now and the mud will crash in 5-6 hours. DONT DO IT!\n");
1518 return;
1519 }
1520 else if (stringp(str))
1521 {
1522 return efun::garbage_collection(str);
1523 }
1524 else
1525 return efun::garbage_collection();
1526}
1527
Zesstrae490a532020-09-22 22:07:54 +02001528varargs void notify_fail(string|closure nf, int prio)
1529{
MG Mud User88f12472016-06-24 23:31:02 +02001530 int oldprio;
Zesstrae490a532020-09-22 22:07:54 +02001531
Zesstrad7795012020-09-22 22:13:31 +02001532 if (!PL || !previous_object())
Zesstrae490a532020-09-22 22:07:54 +02001533 return;
MG Mud User88f12472016-06-24 23:31:02 +02001534
1535 // falls ein Objekt bereits nen notify_fail() setzte, Prioritaeten abschaetzen
1536 // und vergleichen.
Zesstrad7795012020-09-22 22:13:31 +02001537 object oldo = query_notify_fail(1);
1538 if (oldo && previous_object()!=oldo)
1539 {
1540 if (!prio)
1541 {
MG Mud User88f12472016-06-24 23:31:02 +02001542 //Prioritaet dieses notify_fail() 'abschaetzen'
Zesstrad7795012020-09-22 22:13:31 +02001543 if (previous_object()==PL) // Spieler-interne (soul-cmds)
MG Mud User88f12472016-06-24 23:31:02 +02001544 prio=NF_NL_OWN;
Zesstrad7795012020-09-22 22:13:31 +02001545 else if (living(previous_object()))
MG Mud User88f12472016-06-24 23:31:02 +02001546 prio=NF_NL_LIVING;
Zesstrad7795012020-09-22 22:13:31 +02001547 else if (previous_object()->IsRoom())
MG Mud User88f12472016-06-24 23:31:02 +02001548 prio=NF_NL_ROOM;
1549 else
1550 prio=NF_NL_THING;
1551 }
1552 //Prioritaet des alten Setzers abschaetzen
1553 if (oldo==PL)
1554 oldprio=NF_NL_OWN;
1555 else if (living(oldo))
1556 oldprio=NF_NL_LIVING;
Zesstra4dbb9882019-11-26 21:26:36 +01001557 else if (oldo->IsRoom())
MG Mud User88f12472016-06-24 23:31:02 +02001558 oldprio=NF_NL_ROOM;
1559 else
1560 oldprio=NF_NL_THING;
1561 }
1562 else // wenn es noch kein Notify_fail gibt:
1563 oldprio=NF_NL_NONE;
1564
Zesstrad7795012020-09-22 22:13:31 +02001565 // vergleichen und ggf. setzen
1566 if (prio >= oldprio)
1567 {
1568 set_this_object(previous_object());
MG Mud User88f12472016-06-24 23:31:02 +02001569 efun::notify_fail(nf);
1570 }
1571
1572 return;
1573}
1574
Zesstrae490a532020-09-22 22:07:54 +02001575void _notify_fail(string|closure str)
MG Mud User88f12472016-06-24 23:31:02 +02001576{
Zesstra351ca4d2020-09-22 22:17:24 +02001577 notify_fail(str, NF_NL_NONE);
MG Mud User88f12472016-06-24 23:31:02 +02001578}
1579
1580string time2string( string format, int zeit )
1581{
1582 int i,ch,maxunit,dummy;
1583 string *parts, fmt;
1584
1585 int secs = zeit;
1586 int mins = (zeit/60);
1587 int hours = (zeit/3600);
1588 int days = (zeit/86400);
1589 int weeks = (zeit/604800);
1590 int months = (zeit/2419200);
1591 int abbr = 0;
1592
1593 parts = regexplode( format, "\(%\(-|\)[0-9]*[nwdhmsxNWDHMSX]\)|\(%%\)" );
1594
1595 for( i=1; i<sizeof(parts); i+=2 )
1596 {
1597 ch = parts[i][<1];
1598 switch( parts[i][<1] )
1599 {
1600 case 'x': case 'X':
1601 abbr = sscanf( parts[i], "%%%d", dummy ) && dummy==0;
1602 // NO break !
1603 case 'n': case 'N':
1604 maxunit |= 31;
1605 break;
1606 case 'w': case 'W':
1607 maxunit |= 15;
1608 break;
1609 case 'd': case 'D':
1610 maxunit |= 7;
1611 break;
1612 case 'h': case 'H':
1613 maxunit |= 3;
1614 break;
1615 case 'm': case 'M':
1616 maxunit |= 1;
1617 break;
1618 }
1619 }
1620 if( maxunit & 16 ) weeks %= 4;
1621 if( maxunit & 8 ) days %= 7;
1622 if( maxunit & 4 ) hours %= 24;
1623 if( maxunit & 2 ) mins %= 60;
1624 if( maxunit ) secs %= 60;
1625
1626 for( i=1; i<sizeof(parts); i+=2 )
1627 {
1628 fmt = parts[i][0..<2];
1629 ch = parts[i][<1];
1630 if( ch=='x' )
1631 {
1632 if (months > 0) ch='n';
1633 else if( weeks>0 ) ch='w';
1634 else if( days>0 ) ch='d';
1635 else if( hours>0 ) ch='h';
1636 else if(mins > 0) ch = 'm';
1637 else ch = 's';
1638 }
1639 else if( ch=='X' )
1640 {
1641 if (months > 0) ch='N';
1642 else if( weeks>0 ) ch='W';
1643 else if( days>0 ) ch='D';
1644 else if( hours>0 ) ch='H';
1645 else if(mins > 0) ch = 'M';
1646 else ch = 'S';
1647 }
1648
1649 switch( ch )
1650 {
1651 case 'n': parts[i] = sprintf( fmt+"d", months ); break;
1652 case 'w': parts[i] = sprintf( fmt+"d", weeks ); break;
1653 case 'd': parts[i] = sprintf( fmt+"d", days ); break;
1654 case 'h': parts[i] = sprintf( fmt+"d", hours ); break;
1655 case 'm': parts[i] = sprintf( fmt+"d", mins ); break;
1656 case 's': parts[i] = sprintf( fmt+"d", secs ); break;
1657 case 'N':
1658 if(abbr) parts[i] = "M";
1659 else parts[i] = sprintf( fmt+"s", (months==1) ? "Monat" : "Monate" );
1660 break;
1661 case 'W':
1662 if(abbr) parts[i] = "w"; else
1663 parts[i] = sprintf( fmt+"s", (weeks==1) ? "Woche" : "Wochen" );
1664 break;
1665 case 'D':
1666 if(abbr) parts[i] = "d"; else
1667 parts[i] = sprintf( fmt+"s", (days==1) ? "Tag" : "Tage" );
1668 break;
1669 case 'H':
1670 if(abbr) parts[i] = "h"; else
1671 parts[i] = sprintf( fmt+"s", (hours==1) ? "Stunde" : "Stunden" );
1672 break;
1673 case 'M':
1674 if(abbr) parts[i] = "m"; else
1675 parts[i] = sprintf( fmt+"s", (mins==1) ? "Minute" : "Minuten" );
1676 break;
1677 case 'S':
1678 if(abbr) parts[i] = "s"; else
1679 parts[i] = sprintf( fmt+"s", (secs==1) ? "Sekunde" : "Sekunden" );
1680 break;
1681 case '%':
1682 parts[i] = "%";
1683 break;
1684 }
1685 }
1686 return implode( parts, "" );
1687}
1688
1689nomask mixed __create_player_dummy(string name)
1690{
1691 string err;
1692 object ob;
1693 mixed m;
1694 //hat nen Scherzkeks die Blueprint bewegt?
1695 if ((ob=find_object("/secure/login")) && environment(ob))
1696 catch(destruct(ob);publish);
1697 err = catch(ob = clone_object("secure/login");publish);
1698 if (err)
1699 {
1700 write("Fehler beim Laden von /secure/login.c\n"+err+"\n");
1701 return 0;
1702 }
Zesstra4dbb9882019-11-26 21:26:36 +01001703 if (objectp(m=ob->new_logon(name))) netdead[name]=m;
MG Mud User88f12472016-06-24 23:31:02 +02001704 return m;
1705}
1706
1707nomask int secure_level()
1708{
1709 int *level;
1710 //kette der Caller durchlaufen, den niedrigsten Level in der Kette
1711 //zurueckgeben. Zerstoerte Objekte (Selbstzerstoerer) fuehren zur Rueckgabe
1712 //von 0.
1713 //caller_stack(1) fuegt dem Rueckgabearray this_interactive() hinzu bzw. 0,
1714 //wenn es keinen Interactive gibt. Die 0 fuehrt dann wie bei zerstoerten
1715 //Objekten zur Rueckgabe von 0, was gewuenscht ist, da es hier einen
1716 //INteractive geben muss.
1717 level=map(caller_stack(1),function int (object caller)
1718 {if (objectp(caller))
1719 return(query_wiz_level(geteuid(caller)));
1720 return(0); // kein Objekt da, 0.
1721 } );
1722 return(min(level)); //den kleinsten Wert im Array zurueckgeben (ggf. 0)
1723}
1724
1725nomask string secure_euid()
1726{
1727 string euid;
1728
1729 if (!this_interactive()) // Es muss einen interactive geben
1730 return 0;
1731 euid=geteuid(this_interactive());
1732 // ueber alle Caller iterieren. Wenn eines davon eine andere euid hat als
1733 // der Interactive und diese nicht die ROOTID ist, wird 0 zurueckgeben.
1734 // Ebenso, falls ein Selbstzerstoerer irgendwo in der Kette ist.
1735 foreach(object caller: caller_stack()) {
1736 if (!objectp(caller) ||
1737 (geteuid(caller)!=euid && geteuid(caller)!=ROOTID))
1738 return 0;
1739 }
1740 return euid; // 'sichere' euid zurueckgeben
1741}
1742
Zesstra42324bf2021-05-14 15:25:20 +02001743deprecated nomask int set_light(int i)
MG Mud User88f12472016-06-24 23:31:02 +02001744// erhoeht das Lichtlevel eines Objekts um i
1745// result: das Lichtlevel innerhalb des Objekts
1746{
Zesstra738a1ef2021-05-14 17:01:19 +02001747 object ob;
MG Mud User88f12472016-06-24 23:31:02 +02001748
1749 if (!(ob=previous_object())) return 0; // ohne das gehts nicht.
1750
1751 // aus kompatibilitaetsgruenden kann man auch den Lichtlevel noch setzen
1752 if (i!=0) ob->SetProp(P_LIGHT, ob->QueryProp(P_LIGHT)+i);
1753
1754 // Lichtberechnung findet eigentlich in der Mudlib statt.
Zesstra4dbb9882019-11-26 21:26:36 +01001755 return ob->QueryProp(P_INT_LIGHT);
MG Mud User88f12472016-06-24 23:31:02 +02001756}
1757
MG Mud User88f12472016-06-24 23:31:02 +02001758public varargs string CountUp( string *s, string sep, string lastsep )
1759{
1760 string ret;
1761
1762 if ( !pointerp(s) )
1763 return "";
1764
1765 if (!sep) sep = ", ";
1766 if (!lastsep) lastsep = " und ";
1767
1768 switch (sizeof(s)) {
1769 case 0: ret=""; break;
1770 case 1: ret=s[0]; break;
1771 default:
1772 ret = implode(s[0..<2], sep);
1773 ret += lastsep + s[<1];
1774 }
1775 return ret;
1776}
1777
1778nomask varargs int query_next_reset(object ob) {
1779
1780 // Typpruefung: etwas anderes als Objekte oder 0 sollen Fehler sein.
1781 if (ob && !objectp(ob))
1782 raise_error(sprintf("Bad arg 1 to query_next_reset(): got %.20O, "
1783 "expected object.\n",ob));
1784
1785 // Defaultobjekt PO, wenn 0 uebergeben.
1786 if ( !objectp(ob) )
1787 ob = previous_object();
1788
1789 return efun::object_info(ob, OI_NEXT_RESET_TIME);
1790}
1791
1792
MG Mud User88f12472016-06-24 23:31:02 +02001793
1794// ### Ersatzaufloesung in Strings ###
Arathorn066820b2019-11-27 19:47:19 +01001795varargs string replace_personal(string str, <string|object>* obs, int caps) {
1796 string* parts = regexplode(str, "@WE[A-SU]*[0-9]");
1797 int i = sizeof(parts);
MG Mud User88f12472016-06-24 23:31:02 +02001798
Zesstra19102132016-09-01 22:50:36 +02001799 if (i>1)
1800 {
MG Mud User88f12472016-06-24 23:31:02 +02001801 int j, t;
MG Mud User88f12472016-06-24 23:31:02 +02001802 t = j = sizeof(obs);
1803
Arathorn066820b2019-11-27 19:47:19 +01001804 <string|closure>* name_cls = allocate(j);
1805 while (j--) {
MG Mud User88f12472016-06-24 23:31:02 +02001806 if (objectp(obs[j]))
1807 name_cls[j] = symbol_function("name", obs[j]);
1808 else if (stringp(obs[j]))
1809 name_cls[j] = obs[j];
Arathorn066820b2019-11-27 19:47:19 +01001810 }
MG Mud User88f12472016-06-24 23:31:02 +02001811
Zesstra19102132016-09-01 22:50:36 +02001812 while ((i-= 2)>0)
1813 {
MG Mud User88f12472016-06-24 23:31:02 +02001814 // zu ersetzendes Token in Fall und Objektindex aufspalten
Arathorn066820b2019-11-27 19:47:19 +01001815 int ob_nr = parts[i][<1]-'1';
MG Mud User88f12472016-06-24 23:31:02 +02001816 if (ob_nr<0 || ob_nr>=t) {
1817 set_this_object(previous_object());
Zesstra738a1ef2021-05-14 17:01:19 +02001818 raise_error(sprintf(
1819 "replace_personal: using wrong object index %d\n", ob_nr));
MG Mud User88f12472016-06-24 23:31:02 +02001820 }
1821
1822 // casus kann man schon hier entscheiden
1823 int casus;
1824 string part = parts[i];
1825 switch (part[3]) {
1826 case 'R': casus = WER; break;
1827 case 'S': casus = WESSEN; break;
1828 case 'M': casus = WEM; break;
1829 case 'N': casus = WEN; break;
1830 default: continue; // passt schon jetzt nicht in das Hauptmuster
1831 }
1832
1833 // und jetzt die einzelnen Keywords ohne fuehrendes "@WE", beendende Id
1834 mixed tmp;
1835 switch (part[3..<2]) {
1836 case "R": case "SSEN": case "M": case "N": // Name
1837 parts[i] = funcall(name_cls[ob_nr], casus, 1); break;
1838 case "RU": case "SSENU": case "MU": case "NU": // unbestimmt
1839 parts[i] = funcall(name_cls[ob_nr], casus); break;
1840 case "RQP": case "SSENQP": case "MQP": case "NQP": // Pronoun
1841 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001842 parts[i] = tmp->QueryPronoun(casus);
MG Mud User88f12472016-06-24 23:31:02 +02001843 break;
1844 case "RQA": case "SSENQA": case "MQA": case "NQA": // Article
1845 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001846 tmp = tmp->QueryArticle(casus, 1, 1);
MG Mud User88f12472016-06-24 23:31:02 +02001847 if (stringp(tmp) && !(tmp[<1]^' '))
1848 tmp = tmp[0..<2]; // Extra-Space wieder loeschen
1849 break;
1850 case "RQPPMS": case "SSENQPPMS": case "MQPPMS": case "NQPPMS":
1851 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001852 parts[i] = tmp->QueryPossPronoun(MALE, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001853 break;
1854 case "RQPPFS": case "SSENQPPFS": case "MQPPFS": case "NQPPFS":
1855 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001856 parts[i] = tmp->QueryPossPronoun(FEMALE, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001857 break;
1858 case "RQPPNS": case "SSENQPPNS": case "MQPPNS": case "NQPPNS":
1859 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001860 parts[i] = tmp->QueryPossPronoun(NEUTER, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001861 break;
1862 case "RQPPMP": case "SSENQPPMP": case "MQPPMP": case "NQPPMP":
1863 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001864 parts[i] = tmp->QueryPossPronoun(MALE, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001865 break;
1866 case "RQPPFP": case "SSENQPPFP": case "MQPPFP": case "NQPPFP":
1867 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001868 parts[i] = tmp->QueryPossPronoun(FEMALE, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001869 break;
1870 case "RQPPNP": case "SSENQPPNP": case "MQPPNP": case "NQPPNP":
1871 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001872 parts[i] = tmp->QueryPossPronoun(NEUTER, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001873 break;
1874 default:
1875 continue;
1876 }
Zesstra19102132016-09-01 22:50:36 +02001877
MG Mud User88f12472016-06-24 23:31:02 +02001878 // wenn tmp ein String war, weisen wir es hier pauschal zu
1879 if (stringp(tmp))
1880 parts[i] = tmp;
1881
1882 // auf Wunsch wird nach Satzenden gross geschrieben
1883 if (caps)
Zesstra19102132016-09-01 22:50:36 +02001884 {
1885 // Wenn das vorhergehende parts[i] == "" ist, sind wir am Anfang vom
1886 // String und dies wird wie ein Satzende vorher behandelt.
1887 if (parts[i-1] == "")
MG Mud User88f12472016-06-24 23:31:02 +02001888 parts[i] = capitalize(parts[i]);
Zesstra19102132016-09-01 22:50:36 +02001889 else
1890 {
1891 switch (parts[i-1][<2..])
1892 {
1893 case ". ": case "! ": case "? ":
1894 case ".": case "!": case "?":
1895 case ".\n": case "!\n": case "?\n":
1896 case "\" ": case "\"\n":
1897 parts[i] = capitalize(parts[i]);
1898 break;
1899 }
MG Mud User88f12472016-06-24 23:31:02 +02001900 }
Zesstra19102132016-09-01 22:50:36 +02001901 }
MG Mud User88f12472016-06-24 23:31:02 +02001902 }
1903 return implode(parts, "");
1904 }
1905 return str;
1906}
1907
MG Mud User88f12472016-06-24 23:31:02 +02001908//replacements for dropped efuns in LD
1909#if !__EFUN_DEFINED__(extract)
1910deprecated varargs string extract(string str, int from, int to) {
1911
1912 if(!stringp(str)) {
1913 set_this_object(previous_object());
1914 raise_error(sprintf("Bad argument 1 to extract(): %O",str));
1915 }
1916 if (intp(from) && intp(to)) {
1917 if (from>=0 && to>=0)
1918 return(str[from .. to]);
1919 else if (from>=0 && to<0)
1920 return(str[from .. <abs(to)]);
1921 else if (from<0 && to>=0)
1922 return(str[<abs(from) .. to]);
1923 else
1924 return(str[<abs(from) .. <abs(to)]);
1925 }
1926 else if (intp(from)) {
1927 if (from>=0)
1928 return(str[from .. ]);
1929 else
1930 return(str[<abs(from) .. ]);
1931 }
1932 else {
1933 return(str);
1934 }
1935}
1936#endif // !__EFUN_DEFINED__(extract)
1937
1938#if !__EFUN_DEFINED__(slice_array)
1939deprecated varargs mixed slice_array(mixed array, int from, int to) {
1940
1941 if(!pointerp(array)) {
1942 set_this_object(previous_object());
1943 raise_error(sprintf("Bad argument 1 to slice_array(): %O",array));
1944 }
1945 if (intp(from) && intp(to)) {
1946 if (from>=0 && to>=0)
1947 return(array[from .. to]);
1948 else if (from>=0 && to<0)
1949 return(array[from .. <abs(to)]);
1950 else if (from<0 && to>=0)
1951 return(array[<abs(from) .. to]);
1952 else
1953 return(array[<abs(from) .. <abs(to)]);
1954 }
1955 else if (intp(from)) {
1956 if (from>=0)
1957 return(array[from .. ]);
1958 else
1959 return(array[<abs(from) .. ]);
1960 }
1961 else {
1962 return(array);
1963 }
1964}
1965#endif // !__EFUN_DEFINED__(slice_array)
1966
1967#if !__EFUN_DEFINED__(member_array)
1968deprecated int member_array(mixed item, mixed arraystring) {
1969
1970 if (pointerp(arraystring)) {
1971 return(efun::member(arraystring,item));
1972 }
1973 else if (stringp(arraystring)) {
1974 return(efun::member(arraystring,to_int(item)));
1975 }
1976 else {
1977 set_this_object(previous_object());
1978 raise_error(sprintf("Bad argument 1 to member_array(): %O",arraystring));
1979 }
1980}
1981#endif // !__EFUN_DEFINED__(member_array)
1982
1983// The digit at the i'th position is the number of bits set in 'i'.
1984string count_table =
1985 "0112122312232334122323342334344512232334233434452334344534454556";
1986int broken_count_bits( string s ) {
1987 int i, res;
1988 if( !stringp(s) || !(i=sizeof(s)) ) return 0;
1989 for( ; i-->0; ) {
1990 // We are counting 6 bits at a time using a precompiled table.
1991 res += count_table[(s[i]-' ')&63]-'0';
1992 }
1993 return res;
1994}
1995
1996#if !__EFUN_DEFINED__(count_bits)
1997int count_bits( string s ) {
1998 return(broken_count_bits(s));
1999}
2000#endif
2001
2002
2003// * Teile aus einem Array entfernen *** OBSOLETE
2004deprecated mixed *exclude_array(mixed *arr,int from,int to)
2005{
2006 if (to<from)
2007 to = from;
2008 return arr[0..from-1]+arr[to+1..];
2009}
2010