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