blob: fc16a4b5af7f4426b40977938b7b2eb600e76a5d [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
193static object find_snooped(object who)
194{
195 object *u;
196 int i;
197
198 for (i=sizeof(u=users())-1;i>=0;i--)
199 if (who==efun::interactive_info(u[i], II_SNOOP_NEXT))
200 return u[i];
201 return 0;
202}
203
204private string Lcut(string str) {
205 return str[5..11]+str[18..];
206}
207
Zesstra8d245512020-04-28 20:35:34 +0200208nomask varargs int snoop( object snooper, object snoopee )
MG Mud User88f12472016-06-24 23:31:02 +0200209{
210 int ret;
Zesstra8d245512020-04-28 20:35:34 +0200211 object snooper0, snooper1, snooper2, snooper3;
MG Mud User88f12472016-06-24 23:31:02 +0200212
Zesstra8d245512020-04-28 20:35:34 +0200213 if( !objectp(snooper) || snooper == snoopee || !PO )
MG Mud User88f12472016-06-24 23:31:02 +0200214 return 0;
215
Zesstra8d245512020-04-28 20:35:34 +0200216 snooper0 = find_snooped(snooper);
MG Mud User88f12472016-06-24 23:31:02 +0200217
Zesstra8d245512020-04-28 20:35:34 +0200218 if(snoopee)
219 {
220 if ( PO != snooper
221 && query_wiz_grp(snooper) >= query_wiz_grp(geteuid(PO)) )
MG Mud User88f12472016-06-24 23:31:02 +0200222 return 0;
223
Zesstra8d245512020-04-28 20:35:34 +0200224 if ( query_wiz_grp(snooper) <= query_wiz_grp(snoopee) &&
225 !(snoopee->QueryAllowSnoop(snooper)) )
Zesstra6c333cc2020-04-28 20:18:45 +0200226 {
Zesstra8d245512020-04-28 20:35:34 +0200227 if ( !IS_DEPUTY(snooper) || IS_ARCH(snoopee) )
MG Mud User88f12472016-06-24 23:31:02 +0200228 return 0;
Zesstra6c333cc2020-04-28 20:18:45 +0200229 }
MG Mud User88f12472016-06-24 23:31:02 +0200230
Zesstra8d245512020-04-28 20:35:34 +0200231 if ( (snooper1 = efun::interactive_info(snoopee, II_SNOOP_NEXT)) &&
232 query_wiz_grp(snooper1) >= query_wiz_grp(snooper) )
Zesstra6c333cc2020-04-28 20:18:45 +0200233 {
Zesstra8d245512020-04-28 20:35:34 +0200234 if ( snooper1->QueryProp(P_SNOOPFLAGS) & SF_LOCKED )
MG Mud User88f12472016-06-24 23:31:02 +0200235 return 0;
236
Zesstra8d245512020-04-28 20:35:34 +0200237 tell_object( snooper1, sprintf( "%s snooped jetzt %s.\n",
238 snooper->name(WER), snoopee->name(WER) ) );
MG Mud User88f12472016-06-24 23:31:02 +0200239
Zesstra8d245512020-04-28 20:35:34 +0200240 snooper2 = snooper;
MG Mud User88f12472016-06-24 23:31:02 +0200241
Zesstra6c333cc2020-04-28 20:18:45 +0200242 while ( snooper3 = interactive_info(snooper2, II_SNOOP_NEXT) )
243 {
Zesstra8d245512020-04-28 20:35:34 +0200244 tell_object( snooper1,
MG Mud User88f12472016-06-24 23:31:02 +0200245 sprintf( "%s wird seinerseits von %s gesnooped.\n"
246 ,snooper2->name(WER),
247 snooper3->name(WEM) ) );
248 snooper2 = snooper3;
249 }
250
Zesstra8d245512020-04-28 20:35:34 +0200251 efun::snoop( snooper1, snooper2 );
MG Mud User88f12472016-06-24 23:31:02 +0200252
Zesstra8d245512020-04-28 20:35:34 +0200253 if ( efun::interactive_info(snooper2, II_SNOOP_NEXT) != snooper1 )
254 tell_object( snooper1, sprintf( "Du kannst %s nicht snoopen.\n",
MG Mud User88f12472016-06-24 23:31:02 +0200255 snooper2->name(WEN) ) );
Zesstra6c333cc2020-04-28 20:18:45 +0200256 else
257 {
Zesstra8d245512020-04-28 20:35:34 +0200258 tell_object( snooper1, sprintf( "Du snoopst jetzt %s.\n",
MG Mud User88f12472016-06-24 23:31:02 +0200259 snooper2->name(WEN) ) );
Zesstra8d245512020-04-28 20:35:34 +0200260 if ( !IS_DEPUTY(snooper1) )
Zesstra6c333cc2020-04-28 20:18:45 +0200261 {
MG Mud User88f12472016-06-24 23:31:02 +0200262 log_file( SNOOPLOGFILE, sprintf("%s: %O %O %O\n",
263 dtime(time()),
Zesstra8d245512020-04-28 20:35:34 +0200264 snooper1,
MG Mud User88f12472016-06-24 23:31:02 +0200265 snooper2,
266 environment(snooper2) ),
267 100000 );
268 if (snooper0)
Zesstra8d245512020-04-28 20:35:34 +0200269 CHMASTER->send( "Snoop", snooper1,
MG Mud User88f12472016-06-24 23:31:02 +0200270 sprintf( "%s *OFF* %s (%O)",
Zesstra8d245512020-04-28 20:35:34 +0200271 capitalize(getuid(snooper1)),
MG Mud User88f12472016-06-24 23:31:02 +0200272 capitalize(getuid(snooper0)),
273 environment(snooper0) ) );
274
Zesstra8d245512020-04-28 20:35:34 +0200275 CHMASTER->send( "Snoop", snooper1,
MG Mud User88f12472016-06-24 23:31:02 +0200276 sprintf("%s -> %s (%O)",
Zesstra8d245512020-04-28 20:35:34 +0200277 capitalize(getuid(snooper1)),
MG Mud User88f12472016-06-24 23:31:02 +0200278 capitalize(getuid(snooper2)),
279 environment(snooper2)));
280 }
Zesstra6c333cc2020-04-28 20:18:45 +0200281 else
282 {
MG Mud User88f12472016-06-24 23:31:02 +0200283 log_file( ASNOOPLOGFILE, sprintf( "%s: %O %O %O\n",
284 dtime(time()),
Zesstra8d245512020-04-28 20:35:34 +0200285 snooper1,
MG Mud User88f12472016-06-24 23:31:02 +0200286 snooper2,
287 environment(snooper2) )
288 ,100000 );
289 }
290 }
291 }
292 else
Zesstra6c333cc2020-04-28 20:18:45 +0200293 {
Zesstra8d245512020-04-28 20:35:34 +0200294 if (snooper1)
Zesstra6c333cc2020-04-28 20:18:45 +0200295 {
Zesstra8d245512020-04-28 20:35:34 +0200296 if ( !snooper->QueryProp(P_SNOOPFLAGS) & SF_LOCKED )
Zesstra6c333cc2020-04-28 20:18:45 +0200297 {
MG Mud User88f12472016-06-24 23:31:02 +0200298 printf( "%s wird bereits von %s gesnooped. Benutze das "
299 "\"f\"-Flag, wenn du dennoch snoopen willst.\n",
Zesstra8d245512020-04-28 20:35:34 +0200300 snoopee->name(WER), snooper1->name(WEM) );
MG Mud User88f12472016-06-24 23:31:02 +0200301 return 0;
302 }
Zesstra6c333cc2020-04-28 20:18:45 +0200303 }
304 }
Zesstra8d245512020-04-28 20:35:34 +0200305 ret = efun::snoop( snooper, snoopee );
MG Mud User88f12472016-06-24 23:31:02 +0200306
Zesstra8d245512020-04-28 20:35:34 +0200307 if ( !IS_DEPUTY(snooper)
308 && efun::interactive_info(snoopee, II_SNOOP_NEXT) == snooper)
Zesstra6c333cc2020-04-28 20:18:45 +0200309 {
MG Mud User88f12472016-06-24 23:31:02 +0200310 log_file( SNOOPLOGFILE, sprintf( "%s: %O %O %O\n",
311 Lcut(dtime(time())),
Zesstra8d245512020-04-28 20:35:34 +0200312 snooper, snoopee, environment(snoopee) ),
MG Mud User88f12472016-06-24 23:31:02 +0200313 100000 );
314
315 if (snooper0)
Zesstra6c333cc2020-04-28 20:18:45 +0200316 {
Zesstra8d245512020-04-28 20:35:34 +0200317 CHMASTER->send( "Snoop", snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200318 sprintf( "%s *OFF* %s (%O).",
Zesstra8d245512020-04-28 20:35:34 +0200319 capitalize(getuid(snooper)),
MG Mud User88f12472016-06-24 23:31:02 +0200320 capitalize(getuid(snooper0)),
321 environment(snooper0) ) );
Zesstra6c333cc2020-04-28 20:18:45 +0200322 }
MG Mud User88f12472016-06-24 23:31:02 +0200323
Zesstra8d245512020-04-28 20:35:34 +0200324 CHMASTER->send( "Snoop", snooper, sprintf( "%s -> %s (%O).",
325 capitalize(getuid(snooper)),
326 capitalize(getuid(snoopee)),
327 environment(snoopee) ) );
MG Mud User88f12472016-06-24 23:31:02 +0200328 }
Zesstra6c333cc2020-04-28 20:18:45 +0200329 else
330 {
Zesstra8d245512020-04-28 20:35:34 +0200331 if ( efun::interactive_info(snoopee, II_SNOOP_NEXT) == snooper )
Zesstra6c333cc2020-04-28 20:18:45 +0200332 {
MG Mud User88f12472016-06-24 23:31:02 +0200333 log_file( ASNOOPLOGFILE, sprintf( "%s: %O %O %O\n",
334 Lcut(dtime(time())),
Zesstra8d245512020-04-28 20:35:34 +0200335 snooper, snoopee, environment(snoopee) ),
MG Mud User88f12472016-06-24 23:31:02 +0200336 100000 );
337 }
338 }
339
Zesstra8d245512020-04-28 20:35:34 +0200340 if ( ret && query_wiz_grp(snooper) <= query_wiz_grp(snoopee) &&
341 !IS_DEPUTY(snooper) )
342 tell_object( snoopee, "*** " + NAME(snooper) + " snoopt Dich!\n" );
MG Mud User88f12472016-06-24 23:31:02 +0200343
344 return ret;
345 }
Zesstra6c333cc2020-04-28 20:18:45 +0200346 else
347 {
Zesstra8d245512020-04-28 20:35:34 +0200348 if ( (snooper == PO ||
349 query_wiz_grp(geteuid(PO)) > query_wiz_grp(snooper) ||
350 (query_wiz_grp(geteuid(PO)) == query_wiz_grp(snooper) &&
351 efun::interactive_info(PO, II_SNOOP_NEXT) == snooper)) && snooper0 )
Zesstra6c333cc2020-04-28 20:18:45 +0200352 {
Zesstra8d245512020-04-28 20:35:34 +0200353 if ( !IS_DEPUTY(snooper) )
Zesstra6c333cc2020-04-28 20:18:45 +0200354 {
MG Mud User88f12472016-06-24 23:31:02 +0200355 log_file( SNOOPLOGFILE, sprintf( "%s: %O %O %O *OFF*\n",
Zesstra8d245512020-04-28 20:35:34 +0200356 Lcut(dtime(time())), snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200357 snooper0,
358 environment(snooper0) ),
359 100000 );
360
Zesstra8d245512020-04-28 20:35:34 +0200361 CHMASTER->send( "Snoop", snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200362 sprintf( "%s *OFF* %s (%O).",
Zesstra8d245512020-04-28 20:35:34 +0200363 capitalize(getuid(snooper)),
MG Mud User88f12472016-06-24 23:31:02 +0200364 capitalize(getuid(snooper0)),
365 environment(snooper0) ) );
366 }
Zesstra6c333cc2020-04-28 20:18:45 +0200367 else
368 {
MG Mud User88f12472016-06-24 23:31:02 +0200369 log_file( ASNOOPLOGFILE, sprintf( "%s: %O %O %O *OFF*\n",
Zesstra8d245512020-04-28 20:35:34 +0200370 Lcut(dtime(time())), snooper,
MG Mud User88f12472016-06-24 23:31:02 +0200371 snooper0,
372 environment(snooper0) ),
373 100000 );
374 }
Zesstra8d245512020-04-28 20:35:34 +0200375 return efun::snoop(snooper);
MG Mud User88f12472016-06-24 23:31:02 +0200376 }
377 }
378 return 0;
379}
380
381
382
383// * Emulation des 'alten' explode durch das neue
384string *old_explode(string str, string del) {
385 int s, t;
386 string *strs;
387
388 if (!stringp(str)) {
389 set_this_object(previous_object());
390 raise_error(sprintf(
391 "Invalid argument 1 to old_explode()! Expected <string>, got: "
392 "%.30O\n",str));
393 }
394 if (!stringp(del)) {
395 set_this_object(previous_object());
396 raise_error(sprintf(
397 "Invalid argument 2 to old_explode()! Expected <string>, got: "
398 "%.30O\n",del));
399 }
400 if(del == "")
401 return ({str});
402 strs=efun::explode(str, del);
403 t=sizeof(strs)-1;
404 while(s<=t && strs[s++] == "");s--;
405 while(t>=0 && strs[t--] == "");t++;
406 if(s<=t)
407 return strs[s..t];
408 return ({});
409}
410
411int file_time(string path) {
412 mixed *v;
413
414 set_this_object(previous_object());
415 if (sizeof(v=get_dir(path,GETDIR_DATES))) return v[0];
416 return(0); //sonst
417}
418
MG Mud User88f12472016-06-24 23:31:02 +0200419// * Magier-Level abfragen
420int query_wiz_level(mixed player) {
Zesstra4dbb9882019-11-26 21:26:36 +0100421 return "/secure/master"->query_wiz_level(player);
MG Mud User88f12472016-06-24 23:31:02 +0200422}
423
424#ifdef __ALISTS__
425// * Element aus Alist loeschen (by key)
426mixed *remove_alist(mixed key,mixed *alist)
427{
428 int i,j;
429
430 if (!pointerp(alist) || !sizeof(alist))
431 return alist;
432 if (!pointerp(alist[0]))
433 {
434 if ((i=assoc(key,alist))<0)
435 return alist;
436 return alist[0..i-1]+alist[i+1..];
437 }
438 i = assoc(key,alist[0]);
439 if ((i=assoc(key,alist[0]))<0)
440 return alist;
441 alist=alist[0..];
442 for (j=sizeof(alist)-1;j>=0;j--)
443 alist[j]=alist[j][0..i-1]+alist[j][i+1..];
444 return alist;
445}
446
447// * Element aus Alist loeschen (by pos)
448mixed *exclude_alist(int i,mixed *alist)
449{
450 int j;
451 if (!pointerp(alist) || !sizeof(alist) || i<0)
452 return alist;
453 if (!pointerp(alist[0]))
454 return alist[0..i-1]+alist[i+1..];
455 alist=alist[0..]; /* Create PHYSICAL copy of alist */
456 for (j=sizeof(alist)-1;j>=0;j--)
457 alist[j]=alist[j][0..i-1]+alist[j][i+1..];
458 return alist; /* order_alist is NOT necessary - see /doc/LPC/alist */
459}
460#endif // __ALISTS__
461
462// * German version of ctime()
463#define TAGE ({"Son","Mon","Die","Mit","Don","Fre","Sam"})
464#define MONATE ({"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug",\
465 "Sep","Okt","Nov","Dez"})
466string dtime(int wann) {
467
468 if (wann == dtime_cache[0])
469 return(dtime_cache[1]);
470
471 int *lt = localtime(wann);
472 return sprintf("%s, %2d. %s %d, %02d:%02d:%02d",
473 TAGE[lt[TM_WDAY]], lt[TM_MDAY], MONATE[lt[TM_MON]],
474 lt[TM_YEAR],lt[TM_HOUR], lt[TM_MIN], lt[TM_SEC]);
475}
476
477// wenn strftime im Driver nicht existiert, ist dies hier ein Alias auf dtime(),
478// zwar stimmt das Format dann nicht, aber die Mudlib buggt nicht und schreibt
479// ein ordentliches Datum/Uhrzeit.
480#if !__EFUN_DEFINED__(strftime)
481varargs string strftime(mixed fmt, int clock, int localized) {
482 if (intp(clock) && clock >= 0)
483 return dtime(clock);
484 else if (intp(fmt) && fmt >= 0)
485 return dtime(fmt);
486
487 return dtime(time());
488}
489#endif //!__EFUN_DEFINED__(strftime)
490
491// * Shutdown mit zusaetzlichem logging
492nomask int shutdown(string reason)
493{
494 string name;
495 string obname;
496 string output;
497
498 if (!reason)
499 return 0;
500 if ( !ARCH_SECURITY && getuid(previous_object())!=ROOTID &&
501 object_name(previous_object())!="/obj/shut" )
502 {
503 write("You have no permission to shut down the gamedriver!\n");
504 return 0;
505 }
506 if ((this_interactive())&&(name=getuid(this_interactive())))
507 {
508 name=capitalize(name);
509 filter(users(),#'tell_object,//'
510 capitalize(name)+" faehrt das Spiel herunter!\n");
511 }
512 else
513 name="ANONYMOUS";
514 if (previous_object()) obname=capitalize(getuid(previous_object()));
515 output=name;
516 if (obname && name!=obname) output=output+" ("+obname+")";
517 if (previous_object()&&object_name(previous_object())=="/obj/shut"){
518 output+=" faehrt das Spiel via Armageddon herunter.\n";
519 output=dtime(time())+": "+output;
520 log_file("GAME_LOG",output+"\n",-1);
521 efun::shutdown();
522 return 1;
523 }
524 output=ctime(time())+": "+output+" faehrt das Spiel herunter.\n";
525 output+=" Grund: "+reason;
526 log_file("GAME_LOG",output+"\n",-1);
527 efun::shutdown();
528 return 1;
529}
530
531// * lowerchar
532
533int lowerchar(int char) {
534 if (char<'A' || char>'Z') return char;
535 return char+32;
536}
537
538// * upperstring
539
540string upperstring(string s)
541{
542#if __EFUN_DEFINED__(upper_case)
543 return(upper_case(s));
544#else
545 int i;
546 if (!stringp(s)) return 0;
547 for (i=sizeof(s)-1;i>=0;i--) s[i]=((s[i]<'a'||s[i]>'z')?s[i]:s[i]-32);
548 return s;
549#endif
550}
551
552// * lowerstring
553
554string lowerstring(string s)
555{
556 if (!stringp(s)) return 0;
557 return lower_case(s);
558}
559
560
561// * GD version
562string version()
563{
564 return __VERSION__;
565}
566
567// * break_string
568// stretch() -- stretch a line to fill a given width
569private string stretch(string s, int width) {
570 int len=sizeof(s);
571 if (len==width) return s;
572
573 // reine Leerzeilen, direkt zurueckgeben
574 string trimmed=trim(s,TRIM_LEFT," ");
575 if (trimmed=="") return s;
576 int start_spaces = len - sizeof(trimmed);
577
578 string* words = explode(trimmed, " ");
579 // der letzte kriegt keine Spaces
580 int word_count=sizeof(words) - 1;
581 // wenn Zeile nur aus einem Wort, wird das Wort zurueckgegeben
582 if (!word_count)
583 return " "*start_spaces + words[0];
584
585 int space_count = width - len;
586
587 int space_per_word=(word_count+space_count) / word_count;
588 // Anz.Woerter mit Zusatz-Space
589 int rest=(word_count+space_count) % word_count;
590 // Rest-Spaces Verteilen
591 foreach (int pos : rest) words[pos]+=" ";
592 return (" "*start_spaces) + implode( words, " "*space_per_word );
593}
594
595// aus Geschwindigkeitsgruenden hat der Blocksatz fuer break_string eine
596// eigene Funktion bekommen:
597private varargs string block_string(string s, int width, int flags) {
598 // wenn BS_LEAVE_MY_LFS, aber kein BS_NO_PARINDENT, dann werden Zeilen mit
599 // einem Leerzeichen begonnen.
600 // BTW: Wenn !BS_LEAVE_MY_LFS, hat der Aufrufer bereits alle \n durch " "
601 // ersetzt.
602 if ( (flags & BS_LEAVE_MY_LFS)
603 && !(flags & BS_NO_PARINDENT))
604 {
605 s = " "+regreplace(s,"\n","\n ",1);
606 }
607
608 // sprintf fuellt die letzte Zeile auf die Feldbreite (hier also
609 // Zeilenbreite) mit Fuellzeichen auf, wenn sie NICHT mit \n umgebrochen
610 // ist. Es wird an die letzte Zeile aber kein Zeilenumbruch angehaengt.
611 // Eigentlich ist das Auffuellen doof, aber vermutlich ist es unnoetig, es
612 // wieder rueckgaengig zu machen.
613 s = sprintf( "%-*=s", width, s);
614
615 string *tmp=explode(s, "\n");
616 // Nur wenn s mehrzeilig ist, Blocksatz draus machen. Die letzte Zeile wird
617 // natuerlich nicht gedehnt. Sie ist dafuer schon von sprintf() aufgefuellt
618 // worden. BTW: Die letzte Zeile endet u.U. noch nicht mit einem \n (bzw.
619 // nur dann, wenn BS_LEAVE_MY_LFS und der uebergebene Text schon nen \n am
620 // Ende der letzten Zeile hat), das macht der Aufrufer...
621 if (sizeof(tmp) > 1)
622 return implode( map( tmp[0..<2], #'stretch/*'*/, width ), "\n" )
623 + "\n" + tmp[<1];
624
625 return s;
626}
627
628public varargs string break_string(string s, int w, mixed indent, int flags)
629{
630 if ( !s || s == "" ) return "";
631
632 if ( !w ) w=78;
633
634 if( intp(indent) )
635 indent = indent ? " "*indent : "";
636
637 int indentlen=stringp(indent) ? sizeof(indent) : 0;
638
639 if (indentlen>w) {
640 set_this_object(previous_object());
641 raise_error(sprintf("break_string: indent longer %d than width %d\n",
642 indentlen,w));
643 // w=((indentlen/w)+1)*w;
644 }
645
646 if (!(flags & BS_LEAVE_MY_LFS))
647 s=regreplace( s, "\n", " ", 1 );
648
649 if ( flags & BS_SINGLE_SPACE )
650 s = regreplace( s, "(^|\n| ) *", "\\1", 1 );
651
652 string prefix="";
653 if (indentlen && flags & BS_PREPEND_INDENT) {
654 if (indentlen+sizeof(s) > w ||
655 (flags & BS_LEAVE_MY_LFS) && strstr(s,"\n")>-1) {
656 prefix=indent+"\n";
657 indent=(flags & BS_NO_PARINDENT) ? "" : " ";
658 indentlen=sizeof(indent);
659 }
660 }
661
662 if ( flags & BS_BLOCK ) {
663 /*
664 s = implode( map( explode( s, "\n" ),
665 #'block_string, w, indentlen, flags),
666 "" );
667 */
668 s = block_string( s , w - indentlen, flags );
669 }
670 else {
671 s = sprintf("%-1.*=s",w-indentlen,s);
672 }
673 if ( s[<1] != '\n' ) s += "\n";
674
675 if ( !indentlen ) return prefix + s;
676
677 string indent2 = ( flags & BS_INDENT_ONCE ) ? (" "*indentlen) :indent;
678
679 return prefix + indent +
680 regreplace( s[0..<2], "\n", "\n"+indent2, 1 ) + "\n";
681 /*
682 string *buf;
683
684 buf = explode( s, "\n" );
685 return prefix + indent + implode( buf[0..<2], "\n"+indent2 ) + buf[<1] + "\n";
686 */
687}
688
689// * Elemente aus mapping loeschen - mapping vorher kopieren
690
691mapping m_copy_delete(mapping m, mixed key) {
692 return m_delete(copy(m), key);
693}
694
695// * times
696int last_reboot_time()
697{
698 return __BOOT_TIME__;
699}
700
701int first_boot_time()
702{
703 return 701517600;
704}
705
706int exist_days()
707{
708 return (((time()-first_boot_time())/8640)+5)/10;
709}
710
711// * uptime :)
712string uptime()
713{
714 int t;
715 int tmp;
716 string s;
717
718 t=time()-__BOOT_TIME__;
719 s="";
720 if (t>=86400)
721 s+=sprintf("%d Tag%s, ",tmp=t/86400,(tmp==1?"":"e"));
722 if (t>=3600)
723 s+=sprintf("%d Stunde%s, ",tmp=(t=t%86400)/3600,(tmp==1?"":"n"));
724 if (t>60)
725 s+=sprintf("%d Minute%s und ",tmp=(t=t%3600)/60,(tmp==1?"":"n"));
726 return s+sprintf("%d Sekunde%s",t=t%60,(t==1?"":"n"));
727}
728
729// * Was tun bei 'dangling' lfun-closures ?
730void dangling_lfun_closure() {
731 raise_error("dangling lfun closure\n");
732}
733
734// * Sperren ausser fuer master/simul_efun
735
736#if __EFUN_DEFINED__(set_environment)
737nomask void set_environment(object o1, object o2) {
738 raise_error("Available only for root\n");
739}
740#endif
741
742nomask void set_this_player(object pl) {
743 raise_error("Available only for root\n");
744}
745
746#if __EFUN_DEFINED__(export_uid)
747nomask void export_uid(object ob) {
748 raise_error("Available only for root\n");
749}
750#endif
751
752// * Jetzt auch closures
753int process_flag;
754
755public nomask int process_call()
756{
757 if (process_flag>0)
758 return process_flag;
759 else return(0);
760}
761
762private nomask string _process_string(string str,object po) {
763 set_this_object(po);
764 return(efun::process_string(str));
765}
766
767nomask string process_string( mixed str )
768{
769 string tmp, err;
770 int flag;
771
772 if ( closurep(str) ) {
773 set_this_object( previous_object() );
774 return funcall(str);
775 }
776 else if (str==0)
Zesstra4dbb9882019-11-26 21:26:36 +0100777 return(str);
MG Mud User88f12472016-06-24 23:31:02 +0200778 else if ( !stringp(str) ) {
779 return to_string(str);
780 }
781
782 if ( !(flag = process_flag > time() - 60))
783 process_flag=time();
784
785 err = catch(tmp = funcall(#'_process_string,str,previous_object()); publish);
786
787 if ( !flag )
788 process_flag=0;
789
790 if (err) {
791 // Verarbeitung abbrechen
792 set_this_object(previous_object());
793 raise_error(err);
794 }
795 return tmp;
796}
797
798// 'mkdir -p' - erzeugt eine komplette Hierarchie von Verzeichnissen.
799// wenn das Verzeichnis angelegt wurde oder schon existiert, wird 1
800// zurueckgeliefert, sonst 0.
801// Wirft einen Fehler, wenn das angebene Verzeichnis nicht absolut ist!
802public int mkdirp(string dir) {
803 // wenn es nur keinen fuehrenden / gibt, ist das ein Fehler.
804 if (strstr(dir, "/") != 0)
805 raise_error("mkdirp(): Pfad ist nicht absolute.\n");
806 // cut off trailing /...
807 if (dir[<1]=='/')
808 dir = dir[0..<2];
809
810 int fstat = file_size(dir);
811 // wenn es schon existiert, tun wir einfach so, als haetten wir es angelegt.
812 if (fstat == FSIZE_DIR)
813 return 1;
814 // wenn schon ne Datei existiert, geht es nicht.
815 if (fstat != FSIZE_NOFILE)
816 return 0;
817 // wenn es nur einen / gibt (den fuehrenden), dann ist es ein
818 // toplevel-verzeichnis, was direkt angelegt wird.
819 if (strrstr(dir,"/")==0) {
820 return funcall(bind_lambda(#'efun::mkdir, previous_object()), dir);
821 }
822
823 // mkdir() nicht direkt rufen, sondern vorher als closure ans aufrufende
824 // Objekt binden. Sonst laeuft die Rechtepruefung in valid_write() im Master
825 // unter der Annahme, dass die simul_efun.c mit ihrer root id was will.
826
827 // jetzt rekursiv die Verzeichnishierarchie anlegen. Wenn das erfolgreich
828 // ist, dann koennen wir jetzt mit mkdir das tiefste Verzeichnis anlegen
829 if (mkdirp(dir[0..strrstr(dir,"/")-1]) == 1)
830 return funcall(bind_lambda(#'efun::mkdir, previous_object()), dir);
Zesstraa25c9c12018-11-17 00:09:25 +0100831 return 0;
MG Mud User88f12472016-06-24 23:31:02 +0200832}
833
834
835// * Properties ggfs. mitspeichern
836mixed save_object(mixed name)
837{
838 mapping properties;
839 mapping save;
840 mixed index, res;
841 int i;
Zesstraa012d622017-01-29 20:38:48 +0100842 string oldpath;
MG Mud User88f12472016-06-24 23:31:02 +0200843
844 // nur Strings und 0 zulassen
845 if ((!stringp(name) || !sizeof(name)) &&
846 (!intp(name) || name!=0)) {
847 set_this_object(previous_object());
848 raise_error(sprintf(
849 "Only non-empty strings and 0 may be used as filename in "
850 "sefun::save_object()! Argument was %O\n",name));
851 }
852
Zesstraa012d622017-01-29 20:38:48 +0100853 if (stringp(name)) {
854 // abs. Pfad erzeugen. *seufz*
855 if (name[0]!='/')
856 name = "/" + name;
Zesstraa012d622017-01-29 20:38:48 +0100857 // automatisch in LIBDATADIR speichern
858 if (strstr(name,"/"LIBDATADIR"/") != 0) {
Zesstra60aed3e2017-01-31 16:03:51 +0100859 oldpath = name;
Zesstraa012d622017-01-29 20:38:48 +0100860 name = "/"LIBDATADIR + name;
861 // wenn das Verzeichnis nicht existiert, ggf. anlegen
862 string dir = name[0..strrstr(name,"/")-1];
863 if (file_size(dir) != FSIZE_DIR) {
864 if (mkdirp(dir) != 1)
865 raise_error("save_object(): kann Verzeichnis " + dir
866 + " nicht anlegen!");
867 }
868 }
869 }
870
MG Mud User88f12472016-06-24 23:31:02 +0200871 save = m_allocate(0, 2);
Zesstra4dbb9882019-11-26 21:26:36 +0100872 properties = previous_object()->QueryProperties();
MG Mud User88f12472016-06-24 23:31:02 +0200873
874 if(mappingp(properties))
875 {
876 // delete all entries in mapping properties without SAVE flag!
877 index = m_indices(properties);
878 for(i = sizeof(index)-1; i>=0;i--)
879 {
880 if(properties[index[i], F_MODE] & SAVE)
881 {
882 save[index[i]] = properties[index[i]];
883 save[index[i], F_MODE] =
884 properties[index[i], F_MODE] &
885 (~(SETMNOTFOUND|QUERYMNOTFOUND|QUERYCACHED|SETCACHED));
886 }
887 }
888 }
889 else save = ([]);
890
891 // save object!
892 previous_object()->_set_save_data(save);
893 // format: wie definiert in config.h
Zesstraa012d622017-01-29 20:38:48 +0100894 if (stringp(name)) {
MG Mud User88f12472016-06-24 23:31:02 +0200895 res = funcall(bind_lambda(#'efun::save_object, previous_object()), name,
896 __LIB__SAVE_FORMAT_VERSION__);
Zesstraa012d622017-01-29 20:38:48 +0100897 // wenn erfolgreich und noch nen Savefile existiert, was nicht unter
898 // /data/ liegt, wird das geloescht.
899 if (!res && oldpath
900 && file_size(oldpath+".o") >= 0)
901 rm(oldpath+".o");
902 }
MG Mud User88f12472016-06-24 23:31:02 +0200903 else
904 res = funcall(bind_lambda(#'efun::save_object, previous_object()),
905 __LIB__SAVE_FORMAT_VERSION__);
Zesstraa012d622017-01-29 20:38:48 +0100906
MG Mud User88f12472016-06-24 23:31:02 +0200907 previous_object()->_set_save_data(0);
908
909#ifdef IOSTATS
910 // Stats...
911 struct iostat_s stat = (<iostat_s>);
912 stat->oname = object_name(previous_object());
913 stat->time = time();
914 //stat->size = (int)object_info(previous_object(),OINFO_MEMORY,
915 // OIM_TOTAL_DATA_SIZE);
916 if (stringp(name))
917 stat->size = file_size(name + ".o");
918 else
919 stat->sizeof(res);
920 //debug_message("saveo: "+saveo_stat[0]+"\n");
921 saveo_stat[1][saveo_stat[0]] = stat;
922 saveo_stat[0] = (saveo_stat[0] + 1) % sizeof(saveo_stat[1]);
923 //debug_message("saveo 2: "+saveo_stat[0]+"\n");
924#endif
925
926 return res;
927}
928
929// * Auch Properties laden
930int restore_object(string name)
931{
932 int result;
933 mixed index;
934 mixed save;
935 mapping properties;
936 int i;
937 closure cl;
938
Zesstra94381a42017-01-29 22:37:52 +0100939 if (sizeof(name) < 1)
940 {
941 set_this_object(previous_object());
942 raise_error("Bad arg 1 to restore_object(): expected non-empty "
943 "'string'.\n");
944 }
945
Zesstraa012d622017-01-29 20:38:48 +0100946 // Wenn name vermutlich ein Pfad (also nicht mit #x:y anfaengt)
947 if (name[0] != '#')
948 {
949 // abs. Pfad erzeugen *seufz*
Zesstra984bde92017-01-29 22:19:52 +0100950 if (name[0]!='/')
Zesstraa012d622017-01-29 20:38:48 +0100951 name = "/" + name;
952
Zesstraf2a0d492017-04-29 11:05:46 +0200953 // .c am Ende loeschen, sonst wird das File ggf. nicht gefunden.
954 if(name[<2..]==".c")
955 name=name[..<3];
956
Zesstraa012d622017-01-29 20:38:48 +0100957 // wenn kein /data/ vorn steht, erstmal gucken, ob das Savefile unter
958 // /data/ existiert. Wenn ja, wird das geladen.
Zesstra984bde92017-01-29 22:19:52 +0100959 if (strstr(name,"/"LIBDATADIR"/") != 0)
Zesstraa012d622017-01-29 20:38:48 +0100960 {
961 string path = "/"LIBDATADIR + name;
962 if (file_size(path + ".o") >= 0)
963 name = path;
964 }
965 }
966
MG Mud User88f12472016-06-24 23:31:02 +0200967 // get actual property settings (by create())
Zesstra4dbb9882019-11-26 21:26:36 +0100968 properties = previous_object()->QueryProperties();
MG Mud User88f12472016-06-24 23:31:02 +0200969
970// DEBUG(sprintf("RESTORE %O\n",name));
971 // restore object
972 result=funcall(bind_lambda(#'efun::restore_object, previous_object()), name);
973 //'))
974 //_get_save_data liefert tatsaechlich mixed zurueck, wenn das auch immer ein
975 //mapping sein sollte.
Zesstra4dbb9882019-11-26 21:26:36 +0100976 save = previous_object()->_get_save_data();
977 if((save))
MG Mud User88f12472016-06-24 23:31:02 +0200978 {
979 index = m_indices(save);
980 for(i = sizeof(index)-1; i>=0; i--)
981 {
982 properties[index[i]] = save[index[i]];
983 properties[index[i], F_MODE] = save[index[i], F_MODE]
984 &~(SETCACHED|QUERYCACHED);
985 }
986 }
987 else properties = ([]);
988
989 // restore properties
990 funcall(
991 bind_lambda(
992 unbound_lambda(({'arg}), //'})
993 ({#'call_other,({#'this_object}),
994 "SetProperties",'arg})),//')
995 previous_object()),properties);
996 previous_object()->_set_save_data(0);
997
998#ifdef IOSTATS
999 // Stats...
1000 //debug_message("restoreo: "+restoreo_stat[0]+"\n");
1001 struct iostat_s stat = (<iostat_s>);
1002 stat->oname = object_name(previous_object());
1003 stat->time = time();
1004 //stat->size = (int)object_info(previous_object(),OINFO_MEMORY,
1005 // OIM_TOTAL_DATA_SIZE);
1006 stat->size = file_size(name + ".o");
1007 restoreo_stat[1][restoreo_stat[0]] = stat;
1008
1009 restoreo_stat[0] = (restoreo_stat[0] + 1) % sizeof(restoreo_stat[1]);
1010#endif
1011
1012 return result;
1013}
1014
1015// * HB eines Objektes ein/ausschalten
1016int set_object_heart_beat(object ob, int flag)
1017{
1018 if (objectp(ob))
1019 return funcall(bind_lambda(#'efun::configure_object,ob), ob, OC_HEART_BEAT, flag);
1020}
1021
1022// * Magierlevelgruppen ermitteln
1023int query_wiz_grp(mixed wiz)
1024{
1025 int lev;
1026
1027 lev=query_wiz_level(wiz);
1028 if (lev<SEER_LVL) return 0;
1029 if (lev>=GOD_LVL) return lev;
1030 if (lev>=ARCH_LVL) return ARCH_GRP;
1031 if (lev>=ELDER_LVL) return ELDER_GRP;
1032 if (lev>=LORD_LVL) return LORD_GRP;
1033 if (lev>=SPECIAL_LVL) return SPECIAL_GRP;
1034 if (lev>=DOMAINMEMBER_LVL) return DOMAINMEMBER_GRP;
1035 if (lev>=WIZARD_LVL) return WIZARD_GRP;
1036 if (lev>=LEARNER_LVL) return LEARNER_GRP;
1037 return SEER_GRP;
1038}
1039
1040mixed *wizlist_info()
1041{
1042 if (ARCH_SECURITY || !extern_call())
1043 return efun::wizlist_info();
1044 return 0;
1045}
1046
1047// * wizlist ausgeben
1048varargs void wizlist(string name, int sortkey ) {
1049
1050 if (!name)
1051 {
1052 if (this_player())
1053 name = getuid(this_player());
1054 if (!name)
1055 return;
1056 }
1057
1058 // Schluessel darf nur in einem gueltigen Bereich sein
1059 if (sortkey<WL_NAME || sortkey>=WL_SIZE) sortkey=WL_COST;
1060
1061 mixed** wl = efun::wizlist_info();
1062 // nach <sortkey> sortieren
1063 wl = sort_array(wl, function int (mixed a, mixed b)
1064 {return a[sortkey] < b[sortkey]; } );
1065
1066 // Summe ueber alle Kommandos ermitteln.
1067 int total_cmd, i;
1068 int pos=-1;
1069 foreach(mixed entry : wl)
1070 {
1071 total_cmd += entry[WL_COMMANDS];
1072 if (entry[WL_NAME] == name)
1073 pos = i;
1074 ++i;
1075 }
1076
1077 if (pos < 0 && name != "ALL" && name != "TOP100")
1078 return;
1079
1080 if (name == "TOP100")
1081 {
1082 if (sizeof(wl) > 100)
1083 wl = wl[0..100];
1084 else
1085 wl = wl;
1086 }
1087 // um name herum schneiden
1088 else if (name != "ALL")
1089 {
1090 if (sizeof(wl) <= 21)
1091 wl = wl;
1092 else if (pos + 10 < sizeof(wl) && pos - 10 > 0)
1093 wl = wl[pos-10..pos+10];
1094 else if (pos <=21)
1095 wl = wl[0..20];
1096 else if (pos >= sizeof(wl) - 21)
1097 wl = wl[<21..];
1098 else
1099 wl = wl;
1100 }
1101
1102 write("\nWizard top score list\n\n");
1103 if (total_cmd == 0)
1104 total_cmd = 1;
1105 printf("%-20s %-6s %-3s %-17s %-6s %-6s %-6s\n",
1106 "EUID", "cmds", "%", "Costs", "HB", "Arrays","Mapp.");
1107 foreach(mixed e: wl)
1108 {
1109 printf("%-:20s %:6d %:2d%% [%:6dk,%:6dG] %:6d %:6d %:6d\n",
1110 e[WL_NAME], e[WL_COMMANDS], e[WL_COMMANDS] * 100 / total_cmd,
1111 e[WL_COST] / 1000, e[WL_TOTAL_GIGACOST],
1112 e[WL_HEART_BEATS], e[WL_ARRAY_TOTAL], e[WL_MAPPING_TOTAL]
1113 );
1114 }
1115 printf("\nTotal %7d (%d)\n\n", total_cmd, sizeof(wl));
1116}
1117
1118
1119// Ab hier folgen Hilfsfunktionen fuer call_out() bzw. fuer deren Begrenzung
1120
1121// ermittelt das Objekt des Callouts.
1122private object _call_out_obj( mixed call_out ) {
1123 return pointerp(call_out) ? call_out[0] : 0;
1124}
1125
1126private void _same_object( object ob, mapping m ) {
1127 // ist nicht so bloed, wie es aussieht, s. nachfolgede Verwendung von m
1128 if ( m[ob] )
1129 m[ob] += ({ ob });
1130 else
1131 m[ob] = ({ ob });
1132}
1133
1134// alle Objekte im Mapping m zusammenfassen, die den gleichen Loadname (Name der
1135// Blueprint) haben, also alle Clones der BP, die Callouts laufen haben.
1136// Objekte werden auch mehrfach erfasst, je nach Anzahl ihrer Callouts.
1137private void _same_path( object key, object *obs, mapping m ) {
1138 string path;
1139 if (!objectp(key) || !pointerp(obs)) return;
1140
1141 path = load_name(key);
1142
1143 if ( m[path] )
1144 m[path] += obs;
1145 else
1146 m[path] = obs;
1147}
1148
1149// key kann object oder string sein.
1150private int _too_many( mixed key, mapping m, int i ) {
1151 return sizeof(m[key]) >= i;
1152}
1153
1154// alle Objekte in obs zerstoeren und Fehlermeldung ausgeben. ACHTUNG: Die
1155// Objekte werden idR zu einer BP gehoeren, muessen aber nicht! In dem Fall
1156// wird auf der Ebene aber nur ein Objekt in der Fehlermeldung erwaehnt.
1157private void _destroy( mixed key, object *obs, string text, int uid ) {
1158 if (!pointerp(obs)) return;
1159 // Array mit unique Eintraege erzeugen.
1160 obs = m_indices( mkmapping(obs) );
1161 // Fehlermeldung auf der Ebene ausgeben, im catch() mit publish, damit es
1162 // auf der Ebene direkt scrollt, der backtrace verfuegbar ist (im
1163 // gegensatz zur Loesung mittels Callout), die Ausfuehrung aber weiter
1164 // laeuft.
1165 catch( efun::raise_error(
1166 sprintf( text,
Zesstra4dbb9882019-11-26 21:26:36 +01001167 uid ? master()->creator_file(key) : key,
MG Mud User88f12472016-06-24 23:31:02 +02001168 sizeof(obs), object_name(obs[<1]) ) );publish);
1169 // Und weg mit dem Kram...
1170 filter( obs, #'efun::destruct/*'*/ );
1171}
1172
1173// Alle Objekt einer UID im Mapping m mit UID als KEys zusammenfassen. Objekt
1174// sind dabei nicht unique.
1175private void _same_uid( string key, object *obs, mapping m, closure cf ) {
1176 string uid;
1177
1178 if ( !pointerp(obs) || !sizeof(obs) )
1179 return;
1180
1181 uid = funcall( cf, key );
1182
1183 if ( m[uid] )
1184 m[uid] += obs; // obs ist nen Array
1185 else
1186 m[uid] = obs;
1187}
1188
1189private int _log_call_out(mixed co)
1190{
1191 log_file("TOO_MANY_CALLOUTS",
1192 sprintf("%s::%O (%d)\n",object_name(co[0]),co[1],co[2]),
1193 200000);
1194 return 0;
1195}
1196
1197private int last_callout_log=0;
1198
1199nomask varargs void call_out( varargs mixed *args )
1200{
1201 mixed tmp, *call_outs;
1202
1203 // Bei >600 Callouts alle Objekte killen, die mehr als 30 Callouts laufen
1204 // haben.
1205 if ( efun::driver_info(DI_NUM_CALLOUTS) > 600
1206 && geteuid(previous_object()) != ROOTID )
1207 {
1208 // Log erzeugen...
1209 if (last_callout_log <
1210 efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES) - 10
1211 && get_eval_cost() > 200000)
1212 {
1213 last_callout_log = efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES);
1214 log_file("TOO_MANY_CALLOUTS",
1215 sprintf("\n%s: ############ Too many callouts: %d ##############\n",
1216 strftime("%y%m%d-%H%M%S"),
1217 efun::driver_info(DI_NUM_CALLOUTS)));
1218 filter(efun::call_out_info(), #'_log_call_out);
1219 }
1220 // Objekte aller Callouts ermitteln
1221 call_outs = map( efun::call_out_info(), #'_call_out_obj );
1222 mapping objectmap = ([]);
1223 filter( call_outs, #'_same_object, &objectmap );
1224 // Master nicht grillen...
1225 efun::m_delete( objectmap, master(1) );
1226 // alle Objekte raussuchen, die zuviele haben...
1227 mapping res = filter_indices( objectmap, #'_too_many, objectmap, 29 );
1228 // und ueber alle Keys gehen, an _destroy() werden Key und Array mit
1229 // Objekten uebergeben (in diesem Fall sind Keys und Array mit
1230 // Objekten jeweils das gleiche Objekt).
1231 if ( sizeof(res) )
1232 walk_mapping(res, #'_destroy, "CALL_OUT overflow by single "
1233 "object [%O]. Destructed %d objects. [%s]\n", 0 );
1234
1235 // Bei (auch nach dem ersten Aufraeumen noch) >800 Callouts alle
1236 // Objekte killen, die mehr als 50 Callouts laufen haben - und
1237 // diesmal zaehlen Clones nicht eigenstaendig! D.h. es werden alle
1238 // Clones einer BP gekillt, die Callouts laufen haben, falls alle
1239 // diese Objekte _zusammen_ mehr als 50 Callouts haben!
1240 if ( efun::driver_info(DI_NUM_CALLOUTS) > 800 ) {
1241 // zerstoerte Objekte von der letzten Aktion sind in objectmap nicht
1242 // mehr drin, da sie dort als Keys verwendet wurden.
1243 mapping pathmap=([]);
1244 // alle Objekt einer BP in res sortieren, BP-Name als Key, Arrays
1245 // von Objekten als Werte.
1246 walk_mapping( objectmap, #'_same_path, &pathmap);
1247 // alle BPs (und ihre Objekte) raussuchen, die zuviele haben...
1248 res = filter_indices( pathmap, #'_too_many/*'*/, pathmap, 50 );
1249 // und ueber alle Keys gehen, an _destroy() werden die Clones
1250 // uebergeben, die Callouts haben.
1251 if ( sizeof(res) )
1252 walk_mapping( res, #'_destroy/*'*/, "CALL_OUT overflow by file "
1253 "'%s'. Destructed %d objects. [%s]\n", 0 );
1254
1255 // Wenn beide Aufraeumarbeiten nichts gebracht haben und immer
1256 // noch >1000 Callouts laufen, werden diesmal alle Callouts
1257 // einer UID zusammengezaehlt.
1258 // Alle Objekte einer UID, die es in Summe aller ihrer Objekt mit
1259 // Callouts auf mehr als 100 Callouts bringt, werden geroestet.
1260 if (efun::driver_info(DI_NUM_CALLOUTS) > 1000)
1261 {
1262 // das nach BP-Namen vorgefilterte Mapping jetzt nach UIDs
1263 // zusammensortieren. Zerstoerte Clones filter _same_uid()
1264 // raus.
1265 mapping uidmap=([]);
1266 walk_mapping( pathmap, #'_same_uid, &uidmap,
1267 symbol_function( "creator_file",
1268 "/secure/master" ) );
1269 // In res nun UIDs als Keys und Arrays von Objekten als Werte.
1270 // Die rausfiltern, die mehr als 100 Objekte (non-unique, d.h.
1271 // 100 Callouts!) haben.
1272 res = filter_indices( uidmap, #'_too_many, uidmap, 100 );
1273 // und erneut ueber die Keys laufen und jeweils die Arrays mit
1274 // den Objekten zur Zerstoerung an _destroy()...
1275 if ( sizeof(res) )
1276 walk_mapping( res, #'_destroy, "CALL_OUT overflow by "
1277 "UID '%s'. Destructed %d objects. [%s]\n",
1278 1 );
1279 }
1280 }
1281 }
1282
1283 // Falls das aufrufende Objekt zerstoert wurde beim Aufraeumen
1284 if ( !previous_object() )
1285 return;
1286
1287 set_this_object( previous_object() );
1288 apply( #'efun::call_out, args );
1289 return;
1290}
1291
1292mixed call_out_info() {
1293
1294 object po = previous_object();
1295 mixed coi = efun::call_out_info();
1296
1297 // ungefilterten Output nur fuer bestimmte Objekte, Objekte in /std oder
1298 // /obj haben die BackboneID.
1299 if (query_wiz_level(getuid(po)) >= ARCH_LVL
Zesstra4dbb9882019-11-26 21:26:36 +01001300 || master()->creator_file(load_name(po)) == BACKBONEID ) {
MG Mud User88f12472016-06-24 23:31:02 +02001301 return coi;
1302 }
1303 else {
1304 return filter(coi, function mixed (mixed arr) {
1305 if (pointerp(arr) && arr[0]==po)
1306 return 1;
1307 else return 0; });
1308 }
1309}
1310
1311// * Zu einer closure das Objekt, an das sie gebunden ist, suchen
1312// NICHT das Objekt, was ggf. die lfun definiert!
1313mixed query_closure_object(closure c) {
1314 return
1315 CLOSURE_IS_UNBOUND_LAMBDA(get_type_info(c, 1)) ?
1316 0 :
1317 (to_object(c) || -1);
1318}
1319
1320// * Wir wollen nur EIN Argument ... ausserdem checks fuer den Netztotenraum
1321varargs void move_object(mixed what, mixed where)
1322{
1323 object po,tmp;
1324
1325 po=previous_object();
1326 if (!where)
1327 {
1328 where=what;
1329 what=po;
1330 }
1331 if (((stringp(where) && where==NETDEAD_ROOM ) ||
1332 (objectp(where) && where==find_object(NETDEAD_ROOM))) &&
1333 objectp(what) && object_name(what)!="/obj/sperrer")
1334 {
1335 if (!query_once_interactive(what))
1336 {
1337 what->remove();
1338 if (what) destruct(what);
1339 return;
1340 }
1341 if (living(what) || interactive(what))
1342 {
1343 log_file("NDEAD2",sprintf("TRYED TO MOVE TO NETDEAD: %O\n",what));
1344 return;
1345 }
1346 set_object_heart_beat(what,0);
1347 }
1348 tmp=what;
1349 while (tmp=environment(tmp))
1350 // Ja. Man ruft die _set_xxx()-Funktionen eigentlich nicht direkt auf.
1351 // Aber das Lichtsystem ist schon *so* rechenintensiv und gerade der
1352 // P_LAST_CONTENT_CHANGE-Cache wird *so* oft benoetigt, dass es mir
1353 // da um jedes bisschen Rechenzeit geht.
1354 // Der Zweck heiligt ja bekanntlich die Mittel. ;-)
1355 //
1356 // Tiamak
1357 tmp->_set_last_content_change();
1358 funcall(bind_lambda(#'efun::move_object,po),what,where);
1359 if (tmp=what)
1360 while (tmp=environment(tmp))
1361 tmp->_set_last_content_change();
1362}
1363
1364
1365void start_simul_efun() {
1366 mixed *info;
1367
1368 // Falls noch nicht getan, extra_wizinfo initialisieren
1369 if ( !pointerp(info = get_extra_wizinfo(0)) )
1370 set_extra_wizinfo(0, info = allocate(BACKBONE_WIZINFO_SIZE));
1371
1372 InitLivingData(info);
1373
1374 set_next_reset(10); // direkt mal aufraeumen
1375}
1376
1377protected void reset() {
1378 set_next_reset(7200);
1379 CleanLivingData();
1380}
1381
1382#if !__EFUN_DEFINED__(absolute_hb_count)
1383int absolute_hb_count() {
1384 return efun::driver_info(DI_NUM_HEARTBEAT_TOTAL_CYCLES);
1385}
1386#endif
1387
1388void __set_environment(object ob, mixed target)
1389{
1390 string path;
1391 object obj;
1392
1393 if (!objectp(ob))
1394 return;
1395 if (!IS_ARCH(geteuid(previous_object())) || !ARCH_SECURITY )
1396 return;
1397 if (objectp(target))
1398 {
1399 efun::set_environment(ob,target);
1400 return;
1401 }
Zesstra4dbb9882019-11-26 21:26:36 +01001402 path=master()->make_path_absolute(target);
MG Mud User88f12472016-06-24 23:31:02 +02001403 if (stringp(path) && file_size(path+".c")>=0 &&
1404 !catch(load_object(path);publish) )
1405 {
1406 obj=find_object(path);
1407 efun::set_environment(ob,obj);
1408 return;
1409 }
1410}
1411
1412void _dump_wizlist(string file, int sortby) {
1413 int i;
1414 mixed *a;
1415
1416 if (!LORD_SECURITY)
1417 return;
1418 if (!master()->valid_write(file,geteuid(previous_object()),"write_file"))
1419 {
1420 write("NO WRITE PERMISSION\n");
1421 return;
1422 }
1423 a = wizlist_info();
1424 a = sort_array(a, lambda( ({'a,'b}),
1425 ({#'<,
1426 ({#'[,'a,sortby}),
1427 ({#'[,'b,sortby})
1428 })));
1429 rm(file);
1430 for (i=sizeof(a)-1;i>=0;i--)
1431 write_file(file,sprintf("%-11s: eval=%-8d cmds=%-6d HBs=%-5d array=%-5d mapping=%-7d\n",
1432 a[i][WL_NAME],a[i][WL_TOTAL_COST],a[i][WL_COMMANDS],a[i][WL_HEART_BEATS],
1433 a[i][WL_ARRAY_TOTAL],a[i][WL_CALL_OUT]));
1434}
1435
1436public varargs object deep_present(mixed what, object ob) {
1437
1438 if(!objectp(ob))
1439 ob=previous_object();
1440 // Wenn ein Objekt gesucht wird: Alle Envs dieses Objekts ermitteln und
1441 // schauen, ob in diesen ob vorkommt. Dann ist what in ob enthalten.
1442 if(objectp(what)) {
1443 object *envs=all_environment(what);
1444 // wenn ob kein Environment hat, ist es offensichtlich nicht in what
1445 // enthalten.
1446 if (!pointerp(envs)) return 0;
1447 if (member(envs, ob) != -1) return what;
1448 }
1449 // sonst wirds teurer, ueber alle Objekte im (deep) Inv laufen und per id()
1450 // testen. Dabei muss aber die gewuenschte Nr. ("flasche 3") abgeschnitten
1451 // werden und selber gezaehlt werden, welche das entsprechende Objekt ist.
1452 else if (stringp(what)) {
1453 int cnt;
1454 string newwhat;
1455 if(sscanf(what,"%s %d",newwhat,cnt)!=2)
1456 cnt=1;
1457 else
1458 what=newwhat;
1459 foreach(object invob: deep_inventory(ob)) {
1460 if (invob->id(what) && !--cnt)
1461 return invob;
1462 }
1463 }
1464 else {
1465 set_this_object(previous_object());
1466 raise_error(sprintf("Wrong argument 1 to deep_present(). "
1467 "Expected \"object\" or \"string\", got %.50O.\n",
1468 what));
1469 }
1470 return 0;
1471}
1472
1473mapping dump_ip_mapping()
1474{
1475 return 0;
1476}
1477
1478nomask void swap(object obj)
1479{
1480 write("Your are not allowed to swap objects by hand!\n");
1481 return;
1482}
1483
1484nomask varargs void garbage_collection(string str)
1485{
1486 if(previous_object()==0 || !IS_ARCH(geteuid(previous_object()))
1487 || !ARCH_SECURITY)
1488 {
1489 write("Call GC now and the mud will crash in 5-6 hours. DONT DO IT!\n");
1490 return;
1491 }
1492 else if (stringp(str))
1493 {
1494 return efun::garbage_collection(str);
1495 }
1496 else
1497 return efun::garbage_collection();
1498}
1499
1500varargs void notify_fail(mixed nf, int prio) {
1501 object po,oldo;
1502 int oldprio;
1503
1504 if (!PL || !objectp(po=previous_object())) return;
1505 if (!stringp(nf) && !closurep(nf)) {
1506 set_this_object(po);
1507 raise_error(sprintf(
1508 "Only strings and closures allowed for notify_fail! "
1509 "Argument was: %.50O...\n",nf));
1510 }
1511
1512 // falls ein Objekt bereits nen notify_fail() setzte, Prioritaeten abschaetzen
1513 // und vergleichen.
1514 if (objectp(oldo=query_notify_fail(1)) && po!=oldo) {
1515 if (!prio) {
1516 //Prioritaet dieses notify_fail() 'abschaetzen'
1517 if (po==PL) // Spieler-interne (soul-cmds)
1518 prio=NF_NL_OWN;
1519 else if (living(po))
1520 prio=NF_NL_LIVING;
Zesstra4dbb9882019-11-26 21:26:36 +01001521 else if (po->IsRoom())
MG Mud User88f12472016-06-24 23:31:02 +02001522 prio=NF_NL_ROOM;
1523 else
1524 prio=NF_NL_THING;
1525 }
1526 //Prioritaet des alten Setzers abschaetzen
1527 if (oldo==PL)
1528 oldprio=NF_NL_OWN;
1529 else if (living(oldo))
1530 oldprio=NF_NL_LIVING;
Zesstra4dbb9882019-11-26 21:26:36 +01001531 else if (oldo->IsRoom())
MG Mud User88f12472016-06-24 23:31:02 +02001532 oldprio=NF_NL_ROOM;
1533 else
1534 oldprio=NF_NL_THING;
1535 }
1536 else // wenn es noch kein Notify_fail gibt:
1537 oldprio=NF_NL_NONE;
1538
1539 //vergleichen und ggf. setzen
1540 if (prio >= oldprio) {
1541 set_this_object(po);
1542 efun::notify_fail(nf);
1543 }
1544
1545 return;
1546}
1547
1548void _notify_fail(string str)
1549{
1550 //query_notify_fail() benutzen, um das Objekt
1551 //des letzten notify_fail() zu ermitteln
1552 object o;
1553 if ((o=query_notify_fail(1)) && o!=previous_object())
1554 return;
1555 //noch kein notify_fail() fuer dieses Kommando gesetzt, auf gehts.
1556 set_this_object(previous_object());
1557 efun::notify_fail(str);
1558 return;
1559}
1560
1561string time2string( string format, int zeit )
1562{
1563 int i,ch,maxunit,dummy;
1564 string *parts, fmt;
1565
1566 int secs = zeit;
1567 int mins = (zeit/60);
1568 int hours = (zeit/3600);
1569 int days = (zeit/86400);
1570 int weeks = (zeit/604800);
1571 int months = (zeit/2419200);
1572 int abbr = 0;
1573
1574 parts = regexplode( format, "\(%\(-|\)[0-9]*[nwdhmsxNWDHMSX]\)|\(%%\)" );
1575
1576 for( i=1; i<sizeof(parts); i+=2 )
1577 {
1578 ch = parts[i][<1];
1579 switch( parts[i][<1] )
1580 {
1581 case 'x': case 'X':
1582 abbr = sscanf( parts[i], "%%%d", dummy ) && dummy==0;
1583 // NO break !
1584 case 'n': case 'N':
1585 maxunit |= 31;
1586 break;
1587 case 'w': case 'W':
1588 maxunit |= 15;
1589 break;
1590 case 'd': case 'D':
1591 maxunit |= 7;
1592 break;
1593 case 'h': case 'H':
1594 maxunit |= 3;
1595 break;
1596 case 'm': case 'M':
1597 maxunit |= 1;
1598 break;
1599 }
1600 }
1601 if( maxunit & 16 ) weeks %= 4;
1602 if( maxunit & 8 ) days %= 7;
1603 if( maxunit & 4 ) hours %= 24;
1604 if( maxunit & 2 ) mins %= 60;
1605 if( maxunit ) secs %= 60;
1606
1607 for( i=1; i<sizeof(parts); i+=2 )
1608 {
1609 fmt = parts[i][0..<2];
1610 ch = parts[i][<1];
1611 if( ch=='x' )
1612 {
1613 if (months > 0) ch='n';
1614 else if( weeks>0 ) ch='w';
1615 else if( days>0 ) ch='d';
1616 else if( hours>0 ) ch='h';
1617 else if(mins > 0) ch = 'm';
1618 else ch = 's';
1619 }
1620 else if( ch=='X' )
1621 {
1622 if (months > 0) ch='N';
1623 else if( weeks>0 ) ch='W';
1624 else if( days>0 ) ch='D';
1625 else if( hours>0 ) ch='H';
1626 else if(mins > 0) ch = 'M';
1627 else ch = 'S';
1628 }
1629
1630 switch( ch )
1631 {
1632 case 'n': parts[i] = sprintf( fmt+"d", months ); break;
1633 case 'w': parts[i] = sprintf( fmt+"d", weeks ); break;
1634 case 'd': parts[i] = sprintf( fmt+"d", days ); break;
1635 case 'h': parts[i] = sprintf( fmt+"d", hours ); break;
1636 case 'm': parts[i] = sprintf( fmt+"d", mins ); break;
1637 case 's': parts[i] = sprintf( fmt+"d", secs ); break;
1638 case 'N':
1639 if(abbr) parts[i] = "M";
1640 else parts[i] = sprintf( fmt+"s", (months==1) ? "Monat" : "Monate" );
1641 break;
1642 case 'W':
1643 if(abbr) parts[i] = "w"; else
1644 parts[i] = sprintf( fmt+"s", (weeks==1) ? "Woche" : "Wochen" );
1645 break;
1646 case 'D':
1647 if(abbr) parts[i] = "d"; else
1648 parts[i] = sprintf( fmt+"s", (days==1) ? "Tag" : "Tage" );
1649 break;
1650 case 'H':
1651 if(abbr) parts[i] = "h"; else
1652 parts[i] = sprintf( fmt+"s", (hours==1) ? "Stunde" : "Stunden" );
1653 break;
1654 case 'M':
1655 if(abbr) parts[i] = "m"; else
1656 parts[i] = sprintf( fmt+"s", (mins==1) ? "Minute" : "Minuten" );
1657 break;
1658 case 'S':
1659 if(abbr) parts[i] = "s"; else
1660 parts[i] = sprintf( fmt+"s", (secs==1) ? "Sekunde" : "Sekunden" );
1661 break;
1662 case '%':
1663 parts[i] = "%";
1664 break;
1665 }
1666 }
1667 return implode( parts, "" );
1668}
1669
1670nomask mixed __create_player_dummy(string name)
1671{
1672 string err;
1673 object ob;
1674 mixed m;
1675 //hat nen Scherzkeks die Blueprint bewegt?
1676 if ((ob=find_object("/secure/login")) && environment(ob))
1677 catch(destruct(ob);publish);
1678 err = catch(ob = clone_object("secure/login");publish);
1679 if (err)
1680 {
1681 write("Fehler beim Laden von /secure/login.c\n"+err+"\n");
1682 return 0;
1683 }
Zesstra4dbb9882019-11-26 21:26:36 +01001684 if (objectp(m=ob->new_logon(name))) netdead[name]=m;
MG Mud User88f12472016-06-24 23:31:02 +02001685 return m;
1686}
1687
1688nomask int secure_level()
1689{
1690 int *level;
1691 //kette der Caller durchlaufen, den niedrigsten Level in der Kette
1692 //zurueckgeben. Zerstoerte Objekte (Selbstzerstoerer) fuehren zur Rueckgabe
1693 //von 0.
1694 //caller_stack(1) fuegt dem Rueckgabearray this_interactive() hinzu bzw. 0,
1695 //wenn es keinen Interactive gibt. Die 0 fuehrt dann wie bei zerstoerten
1696 //Objekten zur Rueckgabe von 0, was gewuenscht ist, da es hier einen
1697 //INteractive geben muss.
1698 level=map(caller_stack(1),function int (object caller)
1699 {if (objectp(caller))
1700 return(query_wiz_level(geteuid(caller)));
1701 return(0); // kein Objekt da, 0.
1702 } );
1703 return(min(level)); //den kleinsten Wert im Array zurueckgeben (ggf. 0)
1704}
1705
1706nomask string secure_euid()
1707{
1708 string euid;
1709
1710 if (!this_interactive()) // Es muss einen interactive geben
1711 return 0;
1712 euid=geteuid(this_interactive());
1713 // ueber alle Caller iterieren. Wenn eines davon eine andere euid hat als
1714 // der Interactive und diese nicht die ROOTID ist, wird 0 zurueckgeben.
1715 // Ebenso, falls ein Selbstzerstoerer irgendwo in der Kette ist.
1716 foreach(object caller: caller_stack()) {
1717 if (!objectp(caller) ||
1718 (geteuid(caller)!=euid && geteuid(caller)!=ROOTID))
1719 return 0;
1720 }
1721 return euid; // 'sichere' euid zurueckgeben
1722}
1723
1724// INPUT_PROMPT und nen Leerprompt hinzufuegen, wenn keins uebergeben wird.
1725// Das soll dazu dienen, dass alle ggf. ein EOR am Promptende kriegen...
1726//#if __BOOT_TIME__ < 1360017213
1727varargs void input_to( mixed fun, int flags, varargs mixed *args )
1728{
1729 mixed *arr;
1730 int i;
1731
1732 if ( !this_player() || !previous_object() )
1733 return;
1734
1735 // TODO: input_to(...,INPUT_PROMPT, "", ...), wenn kein INPUT_PROMPT
1736 // vorkommt...
1737 if ( flags&INPUT_PROMPT ) {
1738 arr = ({ fun, flags }) + args;
1739 }
1740 else {
1741 // ggf. ein INPUT_PROMPT hinzufuegen und nen Leerstring als Prompt.
1742 flags |= INPUT_PROMPT;
1743 arr = ({ fun, flags, "" }) + args;
1744 }
1745
1746 // Arrays gegen flatten quoten.
1747 for ( i = sizeof(arr) - 1; i > 1; i-- )
1748 if ( pointerp(arr[i]) )
1749 arr[i] = quote(arr[i]);
1750
1751 apply( bind_lambda( unbound_lambda( ({}),
1752 ({ #'efun::input_to/*'*/ }) + arr ),
1753 previous_object() ) );
1754}
1755//#endif
1756
1757nomask int set_light(int i)
1758// erhoeht das Lichtlevel eines Objekts um i
1759// result: das Lichtlevel innerhalb des Objekts
1760{
1761 object ob, *inv;
1762 int lall, light, dark, tmp;
1763
1764 if (!(ob=previous_object())) return 0; // ohne das gehts nicht.
1765
1766 // aus kompatibilitaetsgruenden kann man auch den Lichtlevel noch setzen
1767 if (i!=0) ob->SetProp(P_LIGHT, ob->QueryProp(P_LIGHT)+i);
1768
1769 // Lichtberechnung findet eigentlich in der Mudlib statt.
Zesstra4dbb9882019-11-26 21:26:36 +01001770 return ob->QueryProp(P_INT_LIGHT);
MG Mud User88f12472016-06-24 23:31:02 +02001771}
1772
MG Mud User88f12472016-06-24 23:31:02 +02001773public varargs string CountUp( string *s, string sep, string lastsep )
1774{
1775 string ret;
1776
1777 if ( !pointerp(s) )
1778 return "";
1779
1780 if (!sep) sep = ", ";
1781 if (!lastsep) lastsep = " und ";
1782
1783 switch (sizeof(s)) {
1784 case 0: ret=""; break;
1785 case 1: ret=s[0]; break;
1786 default:
1787 ret = implode(s[0..<2], sep);
1788 ret += lastsep + s[<1];
1789 }
1790 return ret;
1791}
1792
1793nomask varargs int query_next_reset(object ob) {
1794
1795 // Typpruefung: etwas anderes als Objekte oder 0 sollen Fehler sein.
1796 if (ob && !objectp(ob))
1797 raise_error(sprintf("Bad arg 1 to query_next_reset(): got %.20O, "
1798 "expected object.\n",ob));
1799
1800 // Defaultobjekt PO, wenn 0 uebergeben.
1801 if ( !objectp(ob) )
1802 ob = previous_object();
1803
1804 return efun::object_info(ob, OI_NEXT_RESET_TIME);
1805}
1806
1807
MG Mud User88f12472016-06-24 23:31:02 +02001808
1809// ### Ersatzaufloesung in Strings ###
Arathorn066820b2019-11-27 19:47:19 +01001810varargs string replace_personal(string str, <string|object>* obs, int caps) {
1811 string* parts = regexplode(str, "@WE[A-SU]*[0-9]");
1812 int i = sizeof(parts);
MG Mud User88f12472016-06-24 23:31:02 +02001813
Zesstra19102132016-09-01 22:50:36 +02001814 if (i>1)
1815 {
MG Mud User88f12472016-06-24 23:31:02 +02001816 int j, t;
MG Mud User88f12472016-06-24 23:31:02 +02001817 t = j = sizeof(obs);
1818
Arathorn066820b2019-11-27 19:47:19 +01001819 <string|closure>* name_cls = allocate(j);
1820 while (j--) {
MG Mud User88f12472016-06-24 23:31:02 +02001821 if (objectp(obs[j]))
1822 name_cls[j] = symbol_function("name", obs[j]);
1823 else if (stringp(obs[j]))
1824 name_cls[j] = obs[j];
Arathorn066820b2019-11-27 19:47:19 +01001825 }
MG Mud User88f12472016-06-24 23:31:02 +02001826
Zesstra19102132016-09-01 22:50:36 +02001827 while ((i-= 2)>0)
1828 {
MG Mud User88f12472016-06-24 23:31:02 +02001829 // zu ersetzendes Token in Fall und Objektindex aufspalten
Arathorn066820b2019-11-27 19:47:19 +01001830 int ob_nr = parts[i][<1]-'1';
MG Mud User88f12472016-06-24 23:31:02 +02001831 if (ob_nr<0 || ob_nr>=t) {
1832 set_this_object(previous_object());
1833 raise_error(sprintf("replace_personal: using wrong object index %d\n",
1834 ob_nr));
1835 return implode(parts, "");
1836 }
1837
1838 // casus kann man schon hier entscheiden
1839 int casus;
1840 string part = parts[i];
1841 switch (part[3]) {
1842 case 'R': casus = WER; break;
1843 case 'S': casus = WESSEN; break;
1844 case 'M': casus = WEM; break;
1845 case 'N': casus = WEN; break;
1846 default: continue; // passt schon jetzt nicht in das Hauptmuster
1847 }
1848
1849 // und jetzt die einzelnen Keywords ohne fuehrendes "@WE", beendende Id
1850 mixed tmp;
1851 switch (part[3..<2]) {
1852 case "R": case "SSEN": case "M": case "N": // Name
1853 parts[i] = funcall(name_cls[ob_nr], casus, 1); break;
1854 case "RU": case "SSENU": case "MU": case "NU": // unbestimmt
1855 parts[i] = funcall(name_cls[ob_nr], casus); break;
1856 case "RQP": case "SSENQP": case "MQP": case "NQP": // Pronoun
1857 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001858 parts[i] = tmp->QueryPronoun(casus);
MG Mud User88f12472016-06-24 23:31:02 +02001859 break;
1860 case "RQA": case "SSENQA": case "MQA": case "NQA": // Article
1861 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001862 tmp = tmp->QueryArticle(casus, 1, 1);
MG Mud User88f12472016-06-24 23:31:02 +02001863 if (stringp(tmp) && !(tmp[<1]^' '))
1864 tmp = tmp[0..<2]; // Extra-Space wieder loeschen
1865 break;
1866 case "RQPPMS": case "SSENQPPMS": case "MQPPMS": case "NQPPMS":
1867 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001868 parts[i] = tmp->QueryPossPronoun(MALE, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001869 break;
1870 case "RQPPFS": case "SSENQPPFS": case "MQPPFS": case "NQPPFS":
1871 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001872 parts[i] = tmp->QueryPossPronoun(FEMALE, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001873 break;
1874 case "RQPPNS": case "SSENQPPNS": case "MQPPNS": case "NQPPNS":
1875 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001876 parts[i] = tmp->QueryPossPronoun(NEUTER, casus, SINGULAR);
MG Mud User88f12472016-06-24 23:31:02 +02001877 break;
1878 case "RQPPMP": case "SSENQPPMP": case "MQPPMP": case "NQPPMP":
1879 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001880 parts[i] = tmp->QueryPossPronoun(MALE, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001881 break;
1882 case "RQPPFP": case "SSENQPPFP": case "MQPPFP": case "NQPPFP":
1883 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001884 parts[i] = tmp->QueryPossPronoun(FEMALE, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001885 break;
1886 case "RQPPNP": case "SSENQPPNP": case "MQPPNP": case "NQPPNP":
1887 if (objectp(tmp = obs[ob_nr]))
Zesstra4dbb9882019-11-26 21:26:36 +01001888 parts[i] = tmp->QueryPossPronoun(NEUTER, casus, PLURAL);
MG Mud User88f12472016-06-24 23:31:02 +02001889 break;
1890 default:
1891 continue;
1892 }
Zesstra19102132016-09-01 22:50:36 +02001893
MG Mud User88f12472016-06-24 23:31:02 +02001894 // wenn tmp ein String war, weisen wir es hier pauschal zu
1895 if (stringp(tmp))
1896 parts[i] = tmp;
1897
1898 // auf Wunsch wird nach Satzenden gross geschrieben
1899 if (caps)
Zesstra19102132016-09-01 22:50:36 +02001900 {
1901 // Wenn das vorhergehende parts[i] == "" ist, sind wir am Anfang vom
1902 // String und dies wird wie ein Satzende vorher behandelt.
1903 if (parts[i-1] == "")
MG Mud User88f12472016-06-24 23:31:02 +02001904 parts[i] = capitalize(parts[i]);
Zesstra19102132016-09-01 22:50:36 +02001905 else
1906 {
1907 switch (parts[i-1][<2..])
1908 {
1909 case ". ": case "! ": case "? ":
1910 case ".": case "!": case "?":
1911 case ".\n": case "!\n": case "?\n":
1912 case "\" ": case "\"\n":
1913 parts[i] = capitalize(parts[i]);
1914 break;
1915 }
MG Mud User88f12472016-06-24 23:31:02 +02001916 }
Zesstra19102132016-09-01 22:50:36 +02001917 }
MG Mud User88f12472016-06-24 23:31:02 +02001918 }
1919 return implode(parts, "");
1920 }
1921 return str;
1922}
1923
MG Mud User88f12472016-06-24 23:31:02 +02001924//replacements for dropped efuns in LD
1925#if !__EFUN_DEFINED__(extract)
1926deprecated varargs string extract(string str, int from, int to) {
1927
1928 if(!stringp(str)) {
1929 set_this_object(previous_object());
1930 raise_error(sprintf("Bad argument 1 to extract(): %O",str));
1931 }
1932 if (intp(from) && intp(to)) {
1933 if (from>=0 && to>=0)
1934 return(str[from .. to]);
1935 else if (from>=0 && to<0)
1936 return(str[from .. <abs(to)]);
1937 else if (from<0 && to>=0)
1938 return(str[<abs(from) .. to]);
1939 else
1940 return(str[<abs(from) .. <abs(to)]);
1941 }
1942 else if (intp(from)) {
1943 if (from>=0)
1944 return(str[from .. ]);
1945 else
1946 return(str[<abs(from) .. ]);
1947 }
1948 else {
1949 return(str);
1950 }
1951}
1952#endif // !__EFUN_DEFINED__(extract)
1953
1954#if !__EFUN_DEFINED__(slice_array)
1955deprecated varargs mixed slice_array(mixed array, int from, int to) {
1956
1957 if(!pointerp(array)) {
1958 set_this_object(previous_object());
1959 raise_error(sprintf("Bad argument 1 to slice_array(): %O",array));
1960 }
1961 if (intp(from) && intp(to)) {
1962 if (from>=0 && to>=0)
1963 return(array[from .. to]);
1964 else if (from>=0 && to<0)
1965 return(array[from .. <abs(to)]);
1966 else if (from<0 && to>=0)
1967 return(array[<abs(from) .. to]);
1968 else
1969 return(array[<abs(from) .. <abs(to)]);
1970 }
1971 else if (intp(from)) {
1972 if (from>=0)
1973 return(array[from .. ]);
1974 else
1975 return(array[<abs(from) .. ]);
1976 }
1977 else {
1978 return(array);
1979 }
1980}
1981#endif // !__EFUN_DEFINED__(slice_array)
1982
1983#if !__EFUN_DEFINED__(member_array)
1984deprecated int member_array(mixed item, mixed arraystring) {
1985
1986 if (pointerp(arraystring)) {
1987 return(efun::member(arraystring,item));
1988 }
1989 else if (stringp(arraystring)) {
1990 return(efun::member(arraystring,to_int(item)));
1991 }
1992 else {
1993 set_this_object(previous_object());
1994 raise_error(sprintf("Bad argument 1 to member_array(): %O",arraystring));
1995 }
1996}
1997#endif // !__EFUN_DEFINED__(member_array)
1998
1999// The digit at the i'th position is the number of bits set in 'i'.
2000string count_table =
2001 "0112122312232334122323342334344512232334233434452334344534454556";
2002int broken_count_bits( string s ) {
2003 int i, res;
2004 if( !stringp(s) || !(i=sizeof(s)) ) return 0;
2005 for( ; i-->0; ) {
2006 // We are counting 6 bits at a time using a precompiled table.
2007 res += count_table[(s[i]-' ')&63]-'0';
2008 }
2009 return res;
2010}
2011
2012#if !__EFUN_DEFINED__(count_bits)
2013int count_bits( string s ) {
2014 return(broken_count_bits(s));
2015}
2016#endif
2017
2018
2019// * Teile aus einem Array entfernen *** OBSOLETE
2020deprecated mixed *exclude_array(mixed *arr,int from,int to)
2021{
2022 if (to<from)
2023 to = from;
2024 return arr[0..from-1]+arr[to+1..];
2025}
2026