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