blob: e5c1497f3c20942cb9652be6cdcb7f4c3f403479 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001/*
2 * MGtool-1.3
3 * File: MGtool.c
4 * Maintainer: Kirk@MorgenGrauen
5 */
6
7/*------------------------------------------*/
8/* the original Xtool is copyrighted by Hyp */
9/*------------------------------------------*/
10
Zesstra7e95e3f2019-10-19 11:15:05 +020011#pragma strict_types,rtt_checks
MG Mud User88f12472016-06-24 23:31:02 +020012
Zesstra4bf2e1f2018-11-08 20:47:58 +010013protected functions virtual inherit "/std/util/path";
MG Mud User88f12472016-06-24 23:31:02 +020014inherit "/std/thing/properties";
15
Zesstra9f0401a2019-11-06 22:06:02 +010016#include <driver_info.h>
17#include <rtlimits.h>
MG Mud User88f12472016-06-24 23:31:02 +020018#include <properties.h>
Zesstra4bf2e1f2018-11-08 20:47:58 +010019
MG Mud User88f12472016-06-24 23:31:02 +020020#if !defined(QUERYCACHED)
21#define QUERYCACHED 4096
22#endif
23#if !defined (SETCACHED)
24#define SETCACHED 4096
25#endif
26#include <moving.h>
27#include "/secure/wizlevels.h"
28#include "/secure/config.h"
29#include <userinfo.h>
30#include <defines.h>
Zesstra7e95e3f2019-10-19 11:15:05 +020031#include <hook.h>
MG Mud User88f12472016-06-24 23:31:02 +020032
33#include "MGtool/tool.h"
34
35#include "MGtool/toollib.h"
36
37#define XDBG 1
38
39nosave object cloner;
40nosave object msgto=NULL;
41nosave string *manpath=({TOOL_PATH+"/man.d/",
42 "/doc/efun/",
43 "/doc/lfun/",
44 "/doc/w/",
45 "/doc/helpdir/",
46 "/doc/LPC/",
47 "/doc/std/",
48 "/doc/concepts/",
49 ""});
50nosave string morefile=NULL;
51nosave string *scriptline=NULL;
52nosave string *history=allocate(MAX_HISTORY);
53nosave int moreflag=FALSE;
54nosave int moreoffset=1;
55nosave int term=NULL;
56nosave int scriptsize=NULL;
57nosave int nostore=FALSE;
58nosave int xlight=0;
59nosave int pipe_in=FALSE;
60nosave int pipe_out=FALSE;
61nosave int pipe_ovr=TRUE;
62nosave string pipe_if=NULL;
63nosave string pipe_of=NULL;
64nosave int xtk=FALSE;
65nosave mapping variable=([]);
66nosave string *cmds;
67private nosave mapping line_buffer=([]);
68private nosave string line_buffer_name="";
69private nosave string more_searchexpr="";
70int morelines=MORE_LINES;
71int modi=(MODE_FIRST|MODE_PROTECT|MODE_SHORT);
72
73#include "MGtool/toollib.c"
74#include "MGtool/toolcmd.c"
75
76#define SafeReturn(x) \
77{ \
78 cmds=({}); \
79 pipe_in=FALSE; \
80 pipe_of=NULL; \
81 pipe_ovr=TRUE; \
82 pipe_out=FALSE; \
83 pipe_of=NULL; \
84 return (x); \
85}
86
87/*----------------------------------------------------------------------
88 * check some security aspects
89 */
90
91static int security()
92{
93 object prev;
94
95 if( process_call() || getuid()!=getuid(cloner) ) return FALSE; // Rumata
96
97 TK("UID: "+getuid(ME)+" cloner:"+getuid(cloner));
98// TK("prev: "+object_name(PREV)+" me:"+object_name(ME));
99 if(prev=PREV)
100 {
101 if(!cloner)
102 return TRUE;
103 if(getuid(prev)==ROOTID||IS_ARCH(prev))
104 return TRUE;
105 if(prev==ME)
106 return TRUE;
107 return secure_level()>=query_wiz_level(cloner); // Rumata
108 //return getuid(prev)==getuid()&&geteuid(prev)==geteuid()&&cloner==RTP;
109 }
110 else
111 return cloner==NULL;
112}
113
114/*----------------------------------------------------------------------
115 * own write function
116 */
117
118static int Write(string str)
119{
120 if(!stringp(str) || str=="")
121 return FALSE;
122 if(!cloner && objectp(this_player()))
123 write(str);
124 else
125 tell_object(cloner, str);
126 return TRUE;
127}
128
129/*----------------------------------------------------------------------
130 * own command function
131 */
132
133static int Command(string str)
134{
135 int i;
136 TK("Command: str: "+(str?str:"(NULL)"));
137 nostore++;
138 if(MODE(MODE_ECHO))
139 WLN("Doing: "+str);
140 i=(int)cloner->command_me(str);
141 nostore--;
142 return i;
143}
144
145/*----------------------------------------------------------------------
146 * object searching
147 */
148
149static varargs object XFindObj(string str, int silent)
150{
151 object obj, env;
152 string *strs;
153 int i, s, cnt;
154
155 if(!str)
156 return NULL;
157 TK("XFindObj: str: "+(str?str:"(NULL)"));
158 env=ENV(cloner);
Zesstra4daab9a2019-11-06 22:38:18 +0100159 str=string_replace(str, "\\.","\u00B0" "01");
160 str=string_replace(str, "\\^", "\u00B0" "02");
161 str=string_replace(str, "\\$", "\u00B0" "03");
MG Mud User88f12472016-06-24 23:31:02 +0200162 str=string_replace(str, "\\\\", "\\");
163 if (find_object(str)) return find_object(str);
164 if (file_size(str)>1) {
Zesstrabc791ab2017-01-30 23:06:48 +0100165 return load_object(str);
MG Mud User88f12472016-06-24 23:31:02 +0200166 }
167 s=sizeof(strs=strip_explode(str, "."));
168 while(s--)
169 {
170 if(strs[i]=="m"||strs[i]=="me")
171 strs[i]=RNAME(cloner);
172 else if(strs[i]=="h"||strs[i]=="here")
173 strs[i]=object_name(ENV(cloner));
174 if(obj=FindObj(strs[i++],env,(silent?1:0)))
175 env=obj;
176 else
177 break;
178 }
179 return obj;
180}
181
182static varargs object FindObj(string str, object env, int silent)
183{
184 object obj, *inv;
185 string tmp;
186 int num, e;
187
188 if (!stringp(str) || !sizeof(str) || !objectp(env))
189 return NULL;
Zesstra4daab9a2019-11-06 22:38:18 +0100190 str=string_replace(str, "\u00B0" "01", ".");
MG Mud User88f12472016-06-24 23:31:02 +0200191 while(str[e++]=='^')
192 ;
193 str=str[--e..<1];
Zesstra4daab9a2019-11-06 22:38:18 +0100194 str=string_replace(str, "\u00B0" "02", "^");
MG Mud User88f12472016-06-24 23:31:02 +0200195 if(obj=VarToObj(str))
196 ;
197 else if(str[0]=='')
Zesstra4daab9a2019-11-06 22:38:18 +0100198 str=string_replace(str, "\u00B0" "03", "$");
MG Mud User88f12472016-06-24 23:31:02 +0200199 else if(sscanf(str, "%d", num)&&(inv=all_inventory(env)))
200 {
201 if(num>0&&num<=sizeof(inv))
202 obj=inv[num-1];
203 else
204 {
205 WDLN("Specified object number out of range [1-"+sizeof(inv)+"]");
206 return NULL;
207 }
208 }
209 if(obj||(obj=present(str, env))||
210 (obj=find_player(LOWER(str)))||
211 (obj=find_living(str))||
212 (obj=find_object(long_path(str))))
213 {
214 while(e--)
215 {
216 if(!(obj=ENV(obj)))
217 {
218 W("Specified object has no environment [");
219 while(e--)
220 W("^");
221 WDLN(str+"]");
222 return NULL;
223 }
224 }
225 }
226 else
227 if(!silent)
228 WDLN("Specified object does not exist ["+str+"]");
229 return obj;
230}
231
232/*----------------------------------------------------------------------
233 * object variable handling
234 */
235
236static object VarToObj(string str)
237{
238 if (!stringp(str) || !sizeof(str) || str[0]!='$')
239 return NULL;
240 switch(str)
241 {
242 case "$m":
243 case "$me":
244 return cloner;
245 case "$h":
246 case "$here":
247 return ENV(cloner);
248 default:
249 return variable[str[1..<1]];
250 }
251 return(NULL); //never reached
252}
253
254static string VarToFile(string str)
255{
256 return source_file_name(VarToObj(str));
257}
258
259static string VarToPureFile(string str)
260{
261 return pure_file_name(VarToObj(str));
262}
263
264/*----------------------------------------------------------------------
265 * object description printing
266 */
267
268static void PrintObj(object obj, string file)
269{
270 object item, tmp;
271 string str;
272 int i;
273
274 SECURE1();
275 if(!obj)
276 return;
277 PrintShort("Short: ", obj, file);
278 if(!file||file=="")
279 WLN("Long :");
280 else
281 write_file(file,"Long :\n");
282 if(query_once_interactive(obj))
283 str=capitalize(getuid(obj));
284 else
285 {
286 if(object_name(obj)[0..26]=="/d/unterwelt/objekte/teddy#" &&
287 IS_ARCH(this_interactive()))
288 str="Ein Teddy (stumm)";
289 else
290 {
291 if(str=(string)obj->QueryProp(P_INT_LONG))
292 ;
293 else if(str=(string)obj->QueryProp(P_LONG))
294 ;
295 else
296 str="- no long description -\n";
297 }
298 }
299 if(!file||file=="")
300 W(str);
301 else
302 write_file(file,str);
303 FORALL(item, obj)
304 {
305 if(!i)
306 if(!file||file=="")
307 WLN("Content:");
308 else
309 write_file(file,"Content:\n");
310 PrintShort(ARIGHT(++i+". ", 4, " "), item, file);
311 }
312}
313
314static string ObjFile(object obj)
315{
316 return "["+short_path(object_name(obj))+"]";
317}
318
319static varargs void PrintShort(string pre, object obj, string file)
320{
321 string str;
322
323 SECURE1();
324 if(!obj)
325 return;
326 if(MODE(MODE_SHORT))
327 {
328 if (query_once_interactive(obj))
329 str=capitalize(getuid(obj));
330 else
331 {
332 if(!((str=(string)obj->QueryProp(P_INT_SHORT))||
333 (str=(string)obj->QueryProp(P_SHORT))))
334 if(is_player(obj))
335 str=CRNAME(obj)+" (invisible)";
336 else
337 str="- no short description -";
338 }
339 str=ALEFT(sprintf("%O ",str), 34, ".")+" ";
340 }
341 else
342 str="";
343 if(interactive(obj))
344 str+="i";
345 else if(living(obj))
346 str+="l";
347 else
348 str+=".";
349 str+=(shadow(obj, 0) ? "s" : ".");
350 if(!file||file=="")
351 WLN((pre+CAP(str)+" "+ObjFile(obj))[0..79]);
352 else
353 write_file(file,(pre+CAP(str)+" "+ObjFile(obj))[0..79]+"\n");
354}
355
356static varargs void DeepPrintShort(object env, int indent, string pre, string file)
357{
358 int i;
359 object item;
360 string str;
361
362 SECURE1();
363 if(!env)
364 return;
365 for(i=indent,str=""; i--; str+=" ");
366 if(pre)
367 str+=pre;
368 if(!file||file=="")
369 W(str);
370 else
371 write_file(file,str);
372 i++;
373 PrintShort("",env,file);
374 FORALL(item, env)
375 DeepPrintShort(item,indent+1,ARIGHT((++i)+". ",4," "),file);
376}
377
378static string break_string_hard(string str, int len, string pre)
379{
380 int s,p,t;
381 string tmp;
382
383 if(!str||!(s=sizeof(str)))
384 return "";
385 t=len-(p=sizeof(pre))-1;
386
387 tmp="";
388 while(p+s>len)
389 {
390 tmp+=pre+str[0..t]+"\n";
391 str=str[t+1..];
392 s-=t;
393 }
394 if(sizeof(str))
395 tmp+=pre+str[0..]+"\n";
396 return tmp;
397}
398
399static void dprop(string key, mixed data, object obj)
400{
401 if(pipe_out&&pipe_of)
402 write_file(pipe_of,break_string_hard(mixed_to_string(obj->QueryProp(key),MAX_RECURSION),78,ALEFT(key+" ",18,".")+" = "));
403 else
404 W(break_string_hard(mixed_to_string(obj->QueryProp(key),MAX_RECURSION),78,ALEFT(key+" ",18,".")+" = "));
405}
406
407static string propflags(string key, object ob)
408{
409 int tmp;
410 string *flags;
411 tmp=(int)ob->Query(key,1);
412 flags=({});
413 tmp&SAVE ? flags+=({"SAV"}) : flags+=({" "});
414 tmp&PROTECTED ? flags+=({"PRO"}) : flags+=({" "});
415 tmp&SECURED ? flags+=({"SEC"}) : flags+=({" "});
416 tmp&NOSETMETHOD ? flags+=({"NSM"}) : flags+=({" "});
417 tmp&SETMNOTFOUND ? flags+=({"SMN"}) : flags+=({" "});
418 tmp&QUERYMNOTFOUND ? flags+=({"QMN"}) : flags+=({" "});
419 tmp&QUERYCACHED ? flags+=({"QCA"}) : flags+=({" "});
420 tmp&SETCACHED ? flags+=({"SCA"}) : flags+=({" "});
421 return ""+implode(flags,"|");
422}
423
424static void dprop2(string key, mixed data, object ob)
425{
426 if(pipe_out&&pipe_of)
427 write_file(pipe_of,break_string_hard(mixed_to_string(propflags(key,ob),MAX_RECURSION),78,ALEFT(key+" ",18,".")+" = "));
428 else
429 W(break_string_hard(mixed_to_string(propflags(key,ob),MAX_RECURSION),78,ALEFT(key+" ",18,".")+" = "));
430}
431
432static mixed propmethods(string key, object ob)
433{
434 return (mixed)ob->Query(key,2);
435}
436
437static void dprop3(string key, mixed data, object ob)
438{
439 if(pipe_out&&pipe_of)
440 write_file(pipe_of,break_string_hard(mixed_to_string(propmethods(key,ob),MAX_RECURSION),78,ALEFT(key+" ",18,".")+" = "));
441 else
442 W(break_string_hard(mixed_to_string(propmethods(key,ob),MAX_RECURSION),78,ALEFT(key+" ",18,".")+" = "));
443}
444
445static void DumpProperties(object obj, int flag)
446{
447 SECURE1();
448 if(!obj)
449 return;
450 PIPE_DELETE(pipe_of);
451 switch (flag) {
452 case 0:
453 walk_mapping(((mapping *)(obj->__query_properties()))[0],#'dprop,obj); //')
454 break;
455 case 1:
456 walk_mapping(((mapping *)(obj->__query_properties()))[0],#'dprop2,obj); //')
457 break;
458 case 2:
459 walk_mapping(((mapping *)(obj->__query_properties()))[0],#'dprop3,obj); //')
460 break;
461 }
462}
463
464/*----------------------------------------------------------------------
465 * moving objects
466 */
467
468static int MoveObj(object obj1, object obj2, int silent)
469{
470 int err;
471 object oldenv;
472
473 SECURE2(FALSE);
474 if(!(obj1&&obj2))
475 return FALSE;
476 oldenv=ENV(obj1);
477 err=(int)obj1->move(obj2, M_SILENT|M_NOCHECK);
478 if(!silent)
479 switch(err)
480 {
481 case ME_PLAYER:
482 WDLN("Object returned ME_PLAYER");
483 if(oldenv && oldenv != ENV(obj1))
484 WDLN("Object has been moved");
485 return FALSE;
486 case ME_TOO_HEAVY:
487 WDLN("Object returned ME_TOO_HEAVY");
488 if(oldenv && oldenv != ENV(obj1))
489 WDLN("Object has been moved");
490 return FALSE;
491 case ME_CANT_TPORT_IN:
492 WDLN("Object returned ME_CANT_TPORT_IN");
493 if(oldenv && oldenv != ENV(obj1))
494 WDLN("Object has been moved");
495 return FALSE;
496 case ME_CANT_TPORT_OUT:
497 WDLN("Object returned ME_CANT_TPORT_OUT");
498 if(oldenv && oldenv != ENV(obj1))
499 WDLN("Object has been moved");
500 return FALSE;
501 default:
502 WDLN("Object returned unknown return value");
503 return FALSE;
504 }
505 return TRUE;
506}
507
508/*----------------------------------------------------------------------
509 * save destructing of objects
510 */
511
512static void Destruct(object obj)
513{
514 if(!obj || !this_object())
515 return;
516 catch(obj->remove());
517 if(objectp(obj) && !query_once_interactive(obj))
518 destruct(obj);
519}
520
521static void DeepClean(object obj)
522{
523 if(!obj)
524 return;
525 filter(filter(deep_inventory(obj), "is_not_player", ME),
526 "Destruct", ME);
527 if(is_not_player(obj))
528 Destruct(obj);
529}
530
531/*----------------------------------------------------------------------
532 * Show the inheritance tree of objects
533 */
534
535static object *SubNodes(object obj)
536{
537 int s;
538 object *objs;
539 string *inlist;
540
541 if(!obj)
542 return NULL;
543 inlist=inherit_list(obj);
544 s=sizeof(inlist);
545 objs=({});
546 while(s-->1)
547 objs=({find_object(inlist[s])})+objs;
548 return objs;
549}
550
551static void Inheritance(object obj, string func, string pre)
552{
553 int i, s;
554 object *ln;
555 string str;
556
557 if(!obj)
558 return;
559 str=pre+" "+ObjFile(obj);
560 if(func)
561 {
562 str=ALEFT(str+" ", 50, ".");
563 if(function_exists(func, obj)==object_name(obj))
564 str+=ARIGHT(" "+func, 19, ".");
565 else
566 str+=ARIGHT("", 19, ".");
567 }
568 if(pipe_out&&pipe_of)
569 write_file(pipe_of,str+"\n");
570 else
571 WLN(str);
572 ln=SubNodes(obj);
573 for(i=0; i<sizeof(ln); i++)
574 ln=ln-SubNodes(ln[i]);
575 s=sizeof(ln);
576 for(i=0; i<s; i++)
577 Inheritance(ln[i], func, pre+".....");
578}
579
580/*----------------------------------------------------------------------
581 * file name handling
582 */
583
584static string XFile(string file)
585{
586 TK("XFile: file: "+(file?file:"(NULL)"));
587 if(file)
588 switch(file[0])
589 {
590 case '@':
591 return source_file_name(XFindObj(file[1..<1]));
592 case '$':
593 return source_file_name(XFindObj(file));
594 default:
595 return old_explode(long_path(file),"#")[0];
596 }
597 return NULL;
598}
599
600static string XFindFile(string file)
601{
602 TK("XFindFile: file: "+(file?file:"(NULL)"));
603 if(file=XFile(file))
604 {
605 if(file_size(file)>=0)
606 return file;
607 if(file[<3..<1]!=".c" && file_size(file+".c")>0)
608 return file+".c";
609 }
610 WDLN("File not found or not readable ["+short_path(file)+"]");
611 return NULL;
612}
613
614/*----------------------------------------------------------------------
615 * file printing, searching and executing
616 */
617
618static void XMoreFile(string file, int flag)
619{
620 int s,size;
621
622 SECURE1();
623 if(!file)
624 return;
625
626 // term=(string)cloner->QueryProp(P_TTY)!="dumb";
627 if((size=(file_size(morefile=long_path(file))))>0)
628 {
629 if(size>100000)
630 WDLN("Warning: large file");
631 MoreFile(NULL);
632 }
633 else if(flag)
634 WDLN("Cannot read file");
635}
636
MG Mud User88f12472016-06-24 23:31:02 +0200637
638static void MoreFile(string str)
639{
640 int i, off;
641 string f, l, r;
642
643 SECURE1();
644
645 if (str /*&& sizeof(str)*/)
646 {
647 if( !sizeof(str) ) str="\0";
648 if(term)
649 W("M");
650 switch(str[0])
651 {
652 case 'q':
653 case 'x':
654 moreflag=FALSE;
655 moreoffset=1;
656 if(morefile==TMP_FILE||morefile==PIPE_FILE)
657 rm(morefile);
658 return NULL;
659 break;
660 case 'P':
661 case 'U':
662 moreflag=FALSE;
663 moreoffset=moreoffset-morelines;
664 case 'p':
665 case 'u':
666 moreoffset=moreoffset-2*morelines;
667 break;
668 case 'D':
669 case 'N':
670 moreoffset+=morelines;
671 case 0: /* RETURN */
672 case 'd':
673 if(moreflag)
674 {
675 moreflag=FALSE;
676 moreoffset=1;
677 if(morefile==TMP_FILE)
678 rm(morefile);
679 return;
680 }
681 break;
682 case '/':
683 moreoffset--;
684 more_searchexpr=str[1..<1];
685 case 'n':
686 i=moreoffset-morelines+1;
687 if(more_searchexpr=="")
688 {
689 WDLN("No previous regular expression");
690 return;
691 }
692 if(!regexp(({"dummy"}), more_searchexpr))
693 WDLN("Bad regular expression");
694 else
695 while((l=read_file(morefile, i++, 1))&&
696 !sizeof(regexp(({l}), more_searchexpr)))
697 ;
698 if(l)
699 {
700 WLN("*** Skipping ...");
701 moreoffset=i-1;
702 }
703 else
704 {
705 WLN("*** Pattern not found");
706 moreoffset-=morelines;
707 }
708 break;
709 case '0'..'9':
710 sscanf(str, "%d", i);
711 moreoffset=i;
712 break;
713 }
714 }
715 else
716 {
717 moreoffset=1;
718 moreflag=FALSE;
719 }
720 if(moreoffset<1)
721 moreoffset=1;
722 if(CatFile())
723 W("*** More: q,u,U,d,D,/<regexp>,<line> ["+(moreoffset-1)+"] *** ");
724 else
725 {
726 W("*** More: q,u,U,d,D,/<regexp>,<line> ["+(moreoffset-1)+"=EOF] *** ");
727 moreflag=TRUE;
728 }
729 input_to("MoreFile");
730 return;
731}
732
733// Schade eigentlich das ich es neuschreiben musste, aber es ist
734// schneller neu geschrieben als durch die undokumentieren alten Funktionen
735// durchzusteigen... *seufz*
Zesstra9f0401a2019-11-06 22:06:02 +0100736// Padreic
737
738// +++ Kommentarlos: Programmierer schreibt unverstaendlichen Code neu,
739// indem er unverstaendlichen Code schreibt. +++
740// +++ Kommentarlos: Programmierer ersetzt unverstaendlichen Code durch
741// unverstaendlichen Code. +++
742// Arathorn (mit Zesstra im Sinn)
MG Mud User88f12472016-06-24 23:31:02 +0200743
Zesstracda79c22019-11-06 23:42:13 +0100744// Zustandsvariablen fuer den Cache von sread_line.
745static string last_file, *last_file_lines;
746static int last_file_date, last_file_linecount, last_file_complete;
MG Mud User88f12472016-06-24 23:31:02 +0200747
Zesstracda79c22019-11-06 23:42:13 +0100748// Liest die ersten x Bytes des Files ein und cached diese (aber nur
749// vollstaendige Zeilen). Liefert dann gewuenschte Zeilen aus dem Cache ohne
750// Plattenzugriff.
751protected string sread_line(int num)
MG Mud User88f12472016-06-24 23:31:02 +0200752{
753 if (!morefile) return "";
Zesstracda79c22019-11-06 23:42:13 +0100754 // wenn sich das morefile geaendert hat, wird das neue File eingelesen.
755 if (last_file!=morefile || last_file_date!=file_time(morefile))
756 {
757 bytes byte_buf=read_bytes(morefile, 0,
758 driver_info(DI_CURRENT_RUNTIME_LIMITS)[LIMIT_BYTE]);
759 if (!byte_buf) return "";
760 // letzte unvollstaendige Zeile abschneiden
761 int linebreak_index = strrstr(byte_buf, b"\n");
762 if (linebreak_index > -1
763 && linebreak_index < sizeof(byte_buf)-1 )
764 {
765 byte_buf = byte_buf[0..linebreak_index];
766 // dann ist das File auch unvollstaendig.
767 last_file_complete=0;
MG Mud User88f12472016-06-24 23:31:02 +0200768 }
Zesstracda79c22019-11-06 23:42:13 +0100769 // aber auch wenn byte_buf mit Zeilenumbruch endet, noch schauen, ob das
770 // File nicht laenger ist als byte_buf.
771 else if (sizeof(byte_buf) < file_size(morefile))
772 last_file_complete=0;
773 // ansonsten ist es vollstaendig.
774 else
775 last_file_complete=1;
776
777 // In string konvertieren und cache speichern.
778 last_file = to_text(byte_buf, "UTF-8");
779 last_file_lines = explode(last_file, "\n");
780 // und Daten vom gecachten File speichern
781 last_file_linecount=sizeof(last_file_lines);
782 last_file_date=file_time(morefile);
783 last_file = morefile; //speichert jetzt den Filenamen
MG Mud User88f12472016-06-24 23:31:02 +0200784 }
785 if (num==0) num=1;
Zesstracda79c22019-11-06 23:42:13 +0100786
787 // wenn die angefragte Zeile nicht da ist und das File nicht vollstaendig
788 // ist, wird direkt von der Platte gelesen.
789 if (num > last_file_linecount)
790 {
MG Mud User88f12472016-06-24 23:31:02 +0200791 if (last_file_complete) return "";
Zesstracda79c22019-11-06 23:42:13 +0100792 return read_file(morefile, num, 1) || "";
MG Mud User88f12472016-06-24 23:31:02 +0200793 }
Zesstracda79c22019-11-06 23:42:13 +0100794 // Sonst kommt die Zeile aus dem Cache.
795 return last_file_lines[num-1]+"\n";
MG Mud User88f12472016-06-24 23:31:02 +0200796}
797
798static int CatFile()
799{
800 int end;
801 string l;
802
803 end=moreoffset+morelines;
804 while(moreoffset<end)
MG Mud User88f12472016-06-24 23:31:02 +0200805 if((l=sread_line(moreoffset))!="")
806 {
807 moreoffset++;
808 W(l);
809 }
810 else
811 return FALSE;
Zesstrab25537b2019-11-06 23:36:23 +0100812
MG Mud User88f12472016-06-24 23:31:02 +0200813 if(sread_line(moreoffset+1)!="")
814 return TRUE;
815 else
816 return FALSE;
817}
818
819static int XGrepFile(string pat, string file, int mode)
820{
MG Mud User88f12472016-06-24 23:31:02 +0200821 SECURE2(FALSE);
822 TK("XGrepFile: pat: "+pat+" file: "+file+" mode: "+mode);
823 if(!(pat&&file))
824 return FALSE;
Zesstra9f0401a2019-11-06 22:06:02 +0100825
826 // max. Anzahl von Zeilen pro Portion (ueberschlag: 100 Bytes pro Zeile)
827 int maxlines = driver_info(DI_CURRENT_RUNTIME_LIMITS)[LIMIT_FILE] / 100;
828 int start;
829 string buf;
830 // File portionsweise einlesen und verarbeiten
831 while(buf = read_file(file, start, maxlines))
832 {
833 string *lines = strip_explode(buf,"\n");
834 int f; // Pro Treffer erhoeht, benutzt zur einmaligen Ausgabe des Files
835 // ueber alle Zeilen laufen und regexpen
836 foreach(string line : lines)
MG Mud User88f12472016-06-24 23:31:02 +0200837 {
Zesstra9f0401a2019-11-06 22:06:02 +0100838 string *ts=regexp(({(mode&XGREP_ICASE?lower_case(line):line)}),
839 pat);
840 if(sizeof(ts))
MG Mud User88f12472016-06-24 23:31:02 +0200841 {
Zesstra9f0401a2019-11-06 22:06:02 +0100842 if(!(mode&XGREP_REVERT))
843 {
844 if(!f++)
845 write_file(TMP_FILE, "*** File: "+file+" ***\n");
846 write_file(TMP_FILE, line+"\n");
847 }
848 }
849 else if(mode&XGREP_REVERT)
850 {
851 if(!f++)
852 write_file(TMP_FILE, "*** File: "+file+" ***\n");
853 write_file(TMP_FILE, line+"\n");
MG Mud User88f12472016-06-24 23:31:02 +0200854 }
855 }
Zesstra9f0401a2019-11-06 22:06:02 +0100856 if (sizeof(lines) < maxlines)
857 break;
858 else
859 start += sizeof(lines);
860 }
MG Mud User88f12472016-06-24 23:31:02 +0200861 return TRUE;
862}
863
864static void XExecFile(int line)
865{
866 int i;
867
868 if(!scriptline)
869 return;
870 for(i=line; i<scriptsize&&i<line+EXEC_LINES; i++)
871 {
872 if(!scriptline[i])
873 continue;
874 if(!Command(scriptline[i]))
875 {
876 scriptline=NULL;
877 return;
878 }
879 }
880 if(i<scriptsize)
881 call_out("XExecFile", EXEC_TIME, i);
882 else
883 scriptline=NULL;
884}
885
886static void XmtpScript(string dir, string file, string opt)
887{
888 int s, t;
889 string *files;
890
891 s=sizeof(files=get_dir(dir+"/*"));
892 while(s--)
893 {
894 t=sizeof(files[s])-1;
895 if(files[s] == ".." || files[s] == "." || files[s][t] == '~' ||
896 (files[s][0] == '#' && files[s][t] == '#'))
897 continue;
898 if(file_size(dir+"/"+files[s])==-2)
899 {
900 write_file(file, "mkdir "+files[s]+" ; cd "+files[s]+"\n");
901 XmtpScript(dir+"/"+files[s], file, opt);
902 write_file(file, "cd ..\n");
903 }
904 else
905 write_file(file, "mtp -r "+opt+" "+dir+"/"+files[s]+"\n");
906 }
907}
908
909/*----------------------------------------------------------------------
910 * player properties handling
911 */
912
913static string PlayerIdle(object obj)
914{
915 string str;
916 int i, tmp;
917
918 if(!obj)
919 return NULL;
920 if((i=query_idle(obj))>=60)
921 {
922 str=ARIGHT(""+(i/3600), 2, "0");
923 i-=(i/3600)*3600;
924 str+="'"+ARIGHT(""+(i/60), 2, "0");
925 }
926 else
927 str=".....";
928 return str;
929}
930
931static string PlayerAge(object obj)
932{
933 string str;
934 int i, tmp;
935
936 if(!obj)
937 return NULL;
938 i=(int)obj->QueryProp(P_AGE);
939 str=" "+ARIGHT(""+(i/43200), 4, ".");
940 i-=(i/43200)*43200;
941 return str+":"+ARIGHT(""+(i/1800), 2, "0");
942}
943
944static string crname(object who)
945{
946 string uid, lname;
947
948 if((uid=getuid(who))==ROOTID &&
949 object_name(who)[0..7]=="/secure/" &&
950 (lname=(string)who->loginname()))
951 return CAP(lname);
952 return CAP(uid);
953}
954
955static string PlayerWho(object obj)
956{
957 object tmp;
958 string str, stmp;
959 str=ARIGHT(""+LEVEL(obj) , 3, " ");
960 str+=ALEFT(" "+crname(obj)+" ", 12, ".");
961 str+=PlayerAge(obj);
962 str+=((int)obj->QueryProp(P_GENDER)==1 ? " m " : " f ");
963 str+=(obj->QueryProp(P_FROG)) ? "f" : ".";
964 str+=(obj->QueryProp(P_GHOST)) ? "g" : ".";
965 str+=(obj->QueryProp(P_INVIS)) ? "i" : ".";
966 str+=(query_editing(obj)||query_input_pending(obj) ? "e" : ".");
967 str+=(obj->QueryProp(P_AWAY)) ? "a" : ".";
968 str+=" "+PlayerIdle(obj)+" ";
969 str+=(tmp=ENV(obj)) ? ObjFile(tmp) : "- fabric of space -";
970 return str;
971}
972
973static string PlayerMail(object obj, int flag)
974{
975 string pre;
976
977 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
978 return pre+"mail: "+obj->QueryProp(P_MAILADDR);
979}
980
981static string PlayerIP(object obj, int flag)
982{
983 string pre;
984
985 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
986 return pre+"host: "+query_ip_name(obj)+" ("+query_ip_number(obj)+")";
987}
988
989static string PlayerRace(object obj, int flag)
990{
991 string tmp, pre;
992
993 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
994 pre=pre+"race: "+ALEFT(obj->QueryProp(P_RACE)+" ", 10, ".")+" guild: ";
995 tmp=(string)obj->QueryProp(P_GUILD);
996 return tmp ? pre+tmp : pre+"- none -";
997}
998
999static string PlayerDomain(object obj, int flag)
1000{
Zesstraee2fb382020-01-21 21:31:19 +01001001 string pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
MG Mud User88f12472016-06-24 23:31:02 +02001002 pre+="domainlord of: ";
Zesstraee2fb382020-01-21 21:31:19 +01001003 string *domains = master()->query_userlist(getuid(obj),USER_DOMAIN);
1004 if(sizeof(domains))
1005 pre += CountUp(domains, ", ", ", ");
MG Mud User88f12472016-06-24 23:31:02 +02001006 return pre;
1007}
1008
1009static string PlayerStats(object obj, int flag)
1010{
1011 string pre;
1012
1013 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1014 pre+="hp="+ARIGHT(obj->QueryProp(P_HP), 3, "0");
1015 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_HP), 3, "0");
1016 pre+=" sp="+ARIGHT(obj->QueryProp(P_SP), 3, "0");
1017 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_SP), 3, "0");
1018 pre+=" food="+ARIGHT(obj->QueryProp(P_FOOD), 3, "0");
1019 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_FOOD), 3, "0");
1020 pre+=" drink="+ARIGHT(obj->QueryProp(P_DRINK), 3, "0");
1021 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_DRINK), 3, "0");
1022 pre+=" exps="+obj->QueryProp(P_XP);
1023 return pre;
1024}
1025
1026static string PlayerSnoop(object obj, int flag)
1027{
1028 string tmp, pre;
1029 object victim;
1030
1031 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1032 pre=pre+"is snooped by: ";
1033 if(victim=query_snoop(obj))
1034 pre+=ARIGHT(" "+crname(victim), 12, ".");
1035 else
1036 pre+="............";
1037 return pre;
1038}
1039
1040static string PlayerCmdAvg(object obj, int flag)
1041{
1042 string pre;
1043
1044 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1045 return pre+"cmdavg: "+(int)obj->_query_command_average();
1046}
1047
1048
1049/*----------------------------------------------------------------------
1050 * msg input to objects
1051 */
1052
1053static void XMsgSay(string str)
1054{
1055 if(str=="."||str=="**")
1056 {
1057 WLN("[End of message]");
1058 say("[End of message]\n");
1059 }
1060 else
1061 {
1062 say(str+"\n");
1063 input_to("XMsgSay");
1064 }
1065}
1066
1067static void XMsgTell(string str)
1068{
1069 if(str=="."||str=="**")
1070 {
1071 WLN("[End of message]");
1072 tell_object(msgto, "[End of message]\n");
1073 }
1074 else
1075 {
1076 tell_object(msgto, str+"\n");
1077 input_to("XMsgTell");
1078 }
1079}
1080
1081static void XMsgShout(string str)
1082{
1083 if(str=="."||str=="**")
1084 {
1085 WLN("[End of message]");
1086 shout("[End of message]\n");
1087 }
1088 else
1089 {
1090 shout(str+"\n");
1091 input_to("XMsgShout");
1092 }
1093}
1094
1095/*----------------------------------------------------------------------
1096 * own object moving
1097 */
1098
1099int move(mixed dest)
1100{
1101 move_object(ME, cloner?cloner:dest);
1102 return TRUE;
1103}
1104
1105/*----------------------------------------------------------------------
1106 * object id
1107 */
1108
1109int id(string str)
1110{
1111 if(!security()&&MODE(MODE_SCANCHK)&&RTP&&!IS_ARCH(RTP))
1112 WDLN(crname(RTP)+" scanned you (id) ["+query_verb()+"] "+
1113 (PREV ? ObjFile(PREV) : "[destructed object]"));
1114 return LOWER(str)==LOWER(TOOL_NAME);
1115}
1116
1117/*----------------------------------------------------------------------
1118 * short and long description
1119 */
1120
1121string short()
1122{
1123 return _query_short()+".\n";
1124}
1125
1126string _query_short()
1127{
1128 string sh; // added by Rumata
1129 if(cloner)
1130 {
1131 if((!security())&&MODE(MODE_SCANCHK)&&!IS_ARCH(RTP))
1132 WDLN(crname(RTP)+" scanned you (short) ["+query_verb()+"] "+
1133 (PREV ? ObjFile(PREV) : "[destructed object]"));
1134 if( sh=Query(P_SHORT) ) return sh; // added by Rumata
1135 return cloner->name(WESSEN)+" "+TOOL_TITLE+" ["+
1136 ctime(time())[11..18]+"]";
1137 }
1138 return TOOL_TITLE;
1139}
1140
1141string long()
1142{
1143 return _query_long();
1144}
1145
1146string _query_long()
1147{
1148 if(cloner&&!security()&&MODE(MODE_SCANCHK)&&!IS_ARCH(RTP))
1149 {
1150 WDLN(crname(RTP)+" scanned you (long) ["+query_verb()+"] "+
1151 (PREV ? ObjFile(PREV) : "[destructed object]"));
1152 }
1153 return
1154 "This is "+TOOL_NAME+" version "+TOOL_VERSION+
1155 " (maintained by Kirk@MorgenGrauen)\n"+
1156 "Original copyright held by Hyp.\n"+
1157 "Gamedriver patchlevel: "+__VERSION__+" master object: "+__MASTER_OBJECT__+
1158 "\n\nDo 'xhelp' for more information.\n";
1159}
1160
1161string name(mixed dummy1, mixed dummy2)
1162{
1163 return _query_short();
1164}
1165
1166/*----------------------------------------------------------------------
1167 * light stuff
1168 */
1169
1170int _query_light()
1171{
1172 return xlight;
1173}
1174
1175int _set_light(int x)
1176{
1177 return xlight;
1178}
1179
1180/*----------------------------------------------------------------------
1181 * Autoloading
1182 */
1183
1184mixed *_query_autoloadobj()
1185{
1186 return AUTOLOAD_ARGS;
1187}
1188
1189void _set_autoloadobj(mixed *args)
1190{
1191 WLN(TOOL_TITLE+" ...");
1192 if(!pointerp(args))
1193 ;
1194 else if(sizeof(args)!=3)
1195 ;
1196 else if(!stringp(args[0]))
1197 ;
1198 else if(!intp(args[1]))
1199 ;
1200 else if(!intp(args[2]))
1201 ;
1202 else
1203 {
1204 if((string)args[0]!=TOOL_INTERNAL)
1205 {
1206 WLN("*****************************");
1207 WLN("*** NEW EDITION ***");
1208 WLN("*** do 'xtool news' for ***");
1209 WLN("*** more information ***");
1210 WLN("*****************************");
1211 }
1212 modi=(int)args[1];
1213 morelines=(int)args[2];
1214 return;
1215 }
1216 W("(bad autoload, using default)\n");
1217}
1218
1219/*----------------------------------------------------------------------
1220 * creation, updating and initialization stuff
1221 */
1222
1223void update_tool(mixed *args, object obj)
1224{
1225 SECURE1();
1226 if(!(obj&&args))
1227 return;
1228 Destruct(PREV);
1229 _set_autoloadobj(args);
1230 move(obj);
1231}
1232
1233void create()
1234{
1235 object obj;
1236
1237 if(member(object_name(),'#')<0)
1238 return;
1239 if(!cloner&&!((cloner=TP)||(cloner=ENV(ME)))&&!interactive(cloner))
1240 destruct(ME);
1241 if(!IS_LEARNER(cloner))
1242 destruct(ME);
1243 SetProp(P_NODROP,"Das waere zu gefaehrlich.\n");
1244 SetProp(P_NEVERDROP,1);
1245 SetProp(P_NOBUY,1);
1246 if(file_size(SAVE_FILE+".o")>0)
1247 {
1248 WDLN("Loading "+TOOL_TITLE+" settings");
1249 restore_object(SAVE_FILE);
1250 }
1251 if(MODE(MODE_FIRST))
1252 call_out("move",0,cloner);
Zesstra7e95e3f2019-10-19 11:15:05 +02001253 call_out("add_insert_hook",1);
MG Mud User88f12472016-06-24 23:31:02 +02001254}
1255
1256void TK(string str)
1257{
1258 if (!xtk)
1259 return;
1260 tell_object(cloner,"XTOOL: "+str+"\n");
1261}
1262
1263int Xtk(string str)
1264{
1265 xtk=!xtk;
1266 WDLN("Xtool internal tracing "+(xtk?"enabled":"disabled"));
1267 return TRUE;
1268}
1269
MG Mud User88f12472016-06-24 23:31:02 +02001270void init()
1271{
1272 object first, prev;
1273
1274 if(member(object_name(),'#')<0) return;
1275 first=first_inventory(ENV(ME));
1276 if(MODE(MODE_PROTECT)&&is_player(first)&&!IS_ARCH(first))
1277 {
1278 WDLN("WARNING: "+crname(first)+" tried to move into your inventory");
1279 tell_object(first, "You cannot move yourself into "+
1280 crname(cloner)+"'s inventory.\n");
1281 call_out("DropObj",0,first);
1282 return;
1283 }
1284 else if(MODE(MODE_FIRST)&&first!=ME)
1285 move(cloner);
1286 else actions();
1287}
1288
1289void DropObj(object obj)
1290{
1291 if(!obj||!objectp(obj))
1292 return;
1293 obj->move(ENV(cloner),M_NOCHECK|M_NO_SHOW);
1294}
1295
1296#define ACTIONS\
1297([\
1298 "xcallouts" : "Xcallouts";0;1,\
1299 "xcall" : "Xcall";0;1,\
1300 "xcat" : "Xcat";1;1,\
1301 "xcd" : "Xcd";0;0,\
1302 "xclean" : "Xclean";0;0,\
1303 "xclone" : "Xclone";0;0,\
1304 "xuclone" : "Xuclone";0;0,\
1305 "xcmds" : "Xcmds";0;1,\
1306 "xdbg" : "Xdbg";0;0,\
1307 "xdclean" : "Xdclean";0;0,\
1308 "xddes" : "Xddes";0;0,\
1309 "xdes" : "Xdes";0;0,\
1310 "xdest" : "Xdes";0;0,\
1311 "xdlook" : "Xdlook";0;1,\
1312 "xdo" : "Xdo";0;0,\
1313 "xdupdate" : "Xdupdate";0;0,\
1314 "xecho" : "Xecho";0;0,\
1315 "xeval" : "Xeval";0;1,\
1316 "xforall" : "Xforall";0;0,\
1317 "xgoto" : "Xgoto";0;0,\
1318 "xhbeats" : "Xhbeats";0;1,\
1319 "xgrep" : "Xgrep";1;1,\
1320 "xhead" : "Xhead";1;1,\
1321 "xhelp" : "Xhelp";0;0,\
1322 "xinventory": "Xinventory";0;1,\
1323 "xids" : "Xids";0;0,\
1324 "xinfo" : "Xinfo";0;0,\
1325 "xinherit" : "Xinherit";0;1,\
1326 "xlag" : "Xlag";0;0,\
1327 "xlight" : "Xlight";0;0,\
1328 "xload" : "Xload";0;0,\
1329 "xlook" : "Xlook";0;1,\
1330 "xlpc" : "Xlpc";0;0,\
1331 "xman" : "Xman";0;0,\
1332 "xmore" : "Xmore";1;0,\
1333 "xmove" : "Xmove";0;0,\
1334 "xmsg" : "Xmsg";1;0,\
1335 "xmtp" : "Xmtp";0;0,\
1336 "xproc" : "Xproc";0;1,\
1337 "xprof" : "Xprof";0;0,\
1338 "xprops" : "Xprops";0;1,\
1339 "xquit" : "Xquit";0;0,\
1340 "xscan" : "Xscan";0;0,\
1341 "xset" : "Xset";0;0,\
1342 "xsh" : "Xsh";0;0,\
1343 "xsort" : "Xsort";1;1,\
1344 "xtail" : "Xtail";1;1,\
1345 "xtk" : "Xtk";0;0,\
1346 "xtool" : "Xtool";0;0,\
1347 "xtrace" : "Xtrace";0;0,\
1348 "xtrans" : "Xtrans";0;0,\
1349 "xupdate" : "Xupdate";0;0,\
1350 "xwc" : "Xwc";1;0,\
1351 "xwho" : "Xwho";0;1,\
1352 ])
1353
1354static string PrepareLine(string str)
1355{
1356 return str;
1357}
1358
1359static string QuoteLine(string str)
1360{
1361 string *tmp,*tmp2;
1362 int i, i2, len, len2, qd, qs;
1363
Zesstra4daab9a2019-11-06 22:38:18 +01001364 str=string_replace(str,"\\\\","\u00B0""BSHL");
1365 str=string_replace(str,"\\\"","\u00B0""DBLQ");
1366 str=string_replace(str,"\\\'","\u00B0""SGLQ");
1367 str=string_replace(str,"\\|","\u00B0""PIPE");
1368 str=string_replace(str,"||","\u00B0""OROR");
1369 str=string_replace(str,"->","\u00B0""LARR");
1370 str=string_replace(str,"\\$","\u00B0""DOLR");
MG Mud User88f12472016-06-24 23:31:02 +02001371 tmp=regexplode(str,"(\"|')");
1372 len=sizeof(tmp);
1373 qd=qs=0;
1374 for(i=0;i<len;i++)
1375 {
1376 if(i%2)
1377 {
1378 if(tmp[i]=="'")
1379 qd=(!qs?!qd:qd);
1380 else
1381 qs=(!qd?!qs:qs);
1382 if((tmp[i]=="\""&&!qd)||(tmp[i]=="'"&&!qs))
1383 tmp[i]="";
1384 }
1385 else
1386 {
1387 if(!qd)
1388 {
1389 len2=sizeof(tmp2=regexplode(tmp[i],"\\$[^ ][^ ]*"));
1390 for(i2=0;i2<len2;i2++)
1391 if(i2%2)
1392 {
1393 TK("QuoteLine: "+tmp2[i2][1..]);
1394 tmp2[i2]=(string)XFindObj((tmp2[i2])[1..]);
1395 }
1396 tmp[i]=implode(tmp2,"");
1397 }
1398 }
1399 }
1400 if(qd||qs)
1401 return NULL;
1402 str=implode(tmp,"");
1403 TK("QuoteLine: str: "+str);
1404 return str;
1405}
1406
1407static string UnquoteLine(string str)
1408{
Zesstra4daab9a2019-11-06 22:38:18 +01001409 str=string_replace(str,"\u00B0""BSHL","\\");
1410 str=string_replace(str,"\u00B0""DBLQ","\"");
1411 str=string_replace(str,"\u00B0""SGLQ","\'");
1412 str=string_replace(str,"\u00B0""DQUO","");
1413 str=string_replace(str,"\u00B0""SQUO","");
1414 str=string_replace(str,"\u00B0""PIPE","|");
1415 str=string_replace(str,"\u00B0""OROR","||");
1416 str=string_replace(str,"\u00B0""LARR","->");
1417 str=string_replace(str,"\u00B0""DOLR","$");
MG Mud User88f12472016-06-24 23:31:02 +02001418 TK("UnquoteLine: str: "+str);
1419 return str;
1420}
1421
1422static string *ExplodeCmds(string str)
1423{
1424 string *tmp;
1425
1426 tmp=regexplode(str,"\\||>|>>");
1427 while(tmp[<1]=="")
1428 tmp=tmp[0..<2];
1429 return tmp;
1430}
1431
1432varargs int ParseLine(string str)
1433{
1434 string verb, arg;
1435 int ret;
1436
1437 TK("ParseLine: str: "+(str?str:""));
1438 if(!sizeof(cmds))
1439 {
1440 // this is a single command or beginning of a command pipe
1441 verb=query_verb();
1442
1443 // return on unknown commands
1444 if(!verb||!sizeof(verb)||!GetFunc(verb,TRUE))
1445 return FALSE;
1446
1447 str=(string)this_player()->_unparsed_args();
1448 pipe_in=FALSE;
1449 pipe_of=NULL;
1450 pipe_ovr=TRUE;
1451 pipe_out=FALSE;
1452 pipe_of=NULL;
1453 // pass arguments to some special functions unparsed
1454 if(member(({"xlpc","xcall","xeval"}),verb)>=0)
1455 {
1456#ifdef XDBG
1457 TK("ParseLine: special func: "+verb);
1458#endif
1459 ret=CallFunc(verb,str);
1460 SafeReturn(ret);
1461 }
1462 // ok, here we go
1463 pipe_in=pipe_out=FALSE;
1464 pipe_if=pipe_of=NULL;
1465 pipe_ovr=TRUE;
1466 if(file_size(PIPE_FILE)>=0)
1467 rm(PIPE_FILE);
1468 if (str&&str!="")
1469 {
1470 if(!(str=QuoteLine(str)))
1471 {
1472 WDLN("Unterminated quotation");
1473 SafeReturn(TRUE);
1474 }
1475 cmds=ExplodeCmds(str);
1476 }
1477 else
1478 cmds=({""});
1479 arg=strip_string(cmds[0]);
1480 }
1481 else
1482 {
1483 cmds[0]=strip_string(cmds[0]);
1484 TK("ParseLine: cmds[0]: "+cmds[0]);
1485 if(sscanf(cmds[0],"%s %s",verb,arg)!=2)
1486 {
1487 verb=cmds[0];
1488 arg="";
1489 }
1490 }
1491 cmds=cmds[1..];
1492 TK("ParseLine: verb: "+verb);
1493 if (!verb||!sizeof(verb)||!GetFunc(verb,TRUE))
1494 SafeReturn(FALSE);
1495 TK("ParseLine(1): arg: "+arg+" cmds: "+sprintf("%O",cmds));
1496 switch(sizeof(cmds))
1497 {
1498 case 0:
1499 ret=CallFunc(verb,strip_string(UnquoteLine(arg)));
1500 SafeReturn(ret);
1501 break;
1502
1503 case 1:
1504 WDLN("Missing rhs of command pipe");
1505 SafeReturn(TRUE);
1506 break;
1507
1508 default:
1509 pipe_out=TRUE;
1510 switch(cmds[0])
1511 {
1512 case "|":
1513 pipe_of=PIPE_FILE;
1514 pipe_ovr=TRUE;
1515 cmds=cmds[1..];
1516 break;
1517
1518 case ">":
1519 pipe_ovr=TRUE;
1520 if(sizeof(cmds)!=2)
1521 {
1522 WDLN("Illegal IO redirection");
1523 SafeReturn(TRUE);
1524 }
1525 pipe_of=cmds[1];
1526 cmds=({});
1527 break;
1528
1529 case ">>":
1530 pipe_ovr=FALSE;
1531 if(sizeof(cmds)!=2)
1532 {
1533 WDLN("Illegal IO redirection");
1534 SafeReturn(TRUE);
1535 }
1536 pipe_of=cmds[1];
1537 cmds=({});
1538 break;
1539 }
1540 }
1541 TK("ParseLine(2): arg: "+arg+" cmds: "+sprintf("%O",cmds));
1542 if(!CallFunc(verb,strip_string(arg)))
1543 SafeReturn(FALSE);
1544 pipe_in=pipe_out;
1545 pipe_if=pipe_of;
1546 pipe_ovr=FALSE;
1547 pipe_out=FALSE;
1548 pipe_of=NULL;
1549 if(sizeof(cmds))
1550 call_out("ParseLine",0);
1551 else
1552 SafeReturn(TRUE);
1553 return TRUE;
1554}
1555
1556static int CallFunc(string verb, string str)
1557{
1558 string fun;
1559
1560 fun=GetFunc(verb,FALSE);
1561#ifdef XDBG
1562 TK("CallFunc: verb: "+verb+" str: "+str);
1563 TK("CallFunc: resolved function: "+(fun?fun:"(unresolved)"));
1564#endif
1565 if(str=="")
1566 str=NULL;
1567 return fun?(int)call_other(ME,fun,str):FALSE;
1568}
1569
1570static string GetFunc(string verb, int test)
1571{
1572 string fun,*keys,key;
1573 int i,len;
1574
1575 TK("GetFunc: verb: "+verb);
1576
1577 if(verb[0..0]!="x") // Assume all commands start with "x"
1578 return 0;
1579
1580 if (!(fun=(string)ACTIONS[verb,0])) { // Try exact hit first
1581 key="";
1582 len=sizeof(verb);
1583 for (i=sizeof(keys=m_indices(ACTIONS))-1;i>=0;i--) {
1584 TK(" trying: "+keys[i]);
1585 if(sizeof(keys[i])>=len&&keys[i][0..len-1]==verb) {
1586 if(sizeof(key)) {
1587 WLN("Das ist nicht eindeutig ...");
1588 return 0;
1589 }
1590 fun=ACTIONS[keys[i],0];
1591 key=keys[i];
1592 //break;
1593 }
1594 }
1595 } else
1596 key=verb;
1597
1598 if(test)
1599 return fun;
1600
1601 if (key) {
1602#ifdef XDBG
1603 TK("GetFunc: fun: "+fun+" (key: "+key+")\n"+
1604 "pipe_in: "+(pipe_in?"TRUE ":"FALSE ")+(pipe_if?pipe_if:"(NULL)")+"\n"+
1605 "pipe_out: "+(pipe_out?"TRUE ":"FALSE ")+(pipe_of?pipe_of:"(NULL)")+"\n"+
1606 "pipe_ovr: "+(pipe_ovr?"TRUE":"FALSE"));
1607#endif
1608 if (pipe_in&&!ACTIONS[key,PIPE_IN])
1609 {
1610 // this command does not read pipes
1611#ifdef XDBG
1612 TK("Illegal rhs of command pipe \""+fun+"\"\n");
1613#endif
1614 notify_fail("Illegal rhs of command pipe \""+fun+"\"\n");
1615 return 0;
1616 }
1617 else if (pipe_out&&!ACTIONS[key,PIPE_OUT])
1618 {
1619 // this command does not feed pipes
1620#ifdef XDBG
1621 TK("Illegal lhs of command pipe \""+fun+"\"\n");
1622#endif
1623 notify_fail("Illegal lhs of command pipe \""+fun+"\"\n");
1624 return 0;
1625 }
1626 }
1627 return fun;
1628}
1629
1630void actions()
1631{
1632 if (!cloner||!RTP||cloner==RTP||query_wiz_level(cloner)<=query_wiz_level(RTP))
1633 add_action("ParseLine","",1);
1634 add_action("CommandScan", "", 1);
1635}
1636
1637/*----------------------------------------------------------------------
1638 * the checking stuff
1639 */
1640
Zesstra7e95e3f2019-10-19 11:15:05 +02001641public <int|object>* insert_hook(object pl, int hookid, object ob)
MG Mud User88f12472016-06-24 23:31:02 +02001642{
Zesstra7e95e3f2019-10-19 11:15:05 +02001643 if(cloner && cloner == pl && hookid == H_HOOK_INSERT)
1644 {
1645 if(MODE(MODE_FIRST) && find_call_out("move")==-1)
1646 call_out("move",0,cloner);
1647 if(MODE(MODE_INVCHECK))
1648 write_newinvobj(ob);
1649 }
1650 return ({H_NO_MOD, ob});
1651}
1652
1653void add_insert_hook()
1654{
1655 if(objectp(cloner))
1656 cloner->HRegisterToHook(H_HOOK_INSERT, #'insert_hook,
1657 H_HOOK_LIBPRIO(2), H_LISTENER, 0);
MG Mud User88f12472016-06-24 23:31:02 +02001658}
1659
1660static void VarCheck(int show)
1661{
1662 int i, s;
1663 foreach(string k, mixed v : variable)
1664 {
1665 if (v) continue;
1666 if(show) WDLN("*** Variable $"+k+" has been destructed");
1667 m_delete(variable, k);
1668 }
1669}
1670
1671
1672int write_newinvobj(object obj)
1673{
1674 if(obj) WDLN("*** New object in inventory "+ObjFile(obj));
1675 return(1);
1676}
1677
1678/*----------------------------------------------------------------------
1679 * catch all commands, absorb forces and check history
1680 */
1681
1682int CommandScan(string arg)
1683{
1684 string verb, cmd;
1685 object rtp;
1686 rtp=RTP;
1687
1688 if(!cloner&&!(cloner=rtp)) destruct(ME);
1689
1690 if((!MODE(MODE_PROTECT))||security()||
1691 query_wiz_level(cloner)<query_wiz_level(rtp))
1692 {
1693 verb=query_verb();
1694 if(verb&&DoHistory(verb+(arg ? " "+arg : "")))
1695 return TRUE;
1696 nostore=FALSE;
1697 return FALSE;
1698 }
1699 else
1700 {
1701 if(rtp)
1702 {
1703 WDLN("Your "+TOOL_TITLE+" protects you from a force by "+crname(rtp)+
1704 " ["+query_verb()+(arg ? " "+arg+"]" : "]"));
1705 tell_object(rtp, crname(cloner)+"'s "+TOOL_TITLE+
1706 " absorbes your force.\n");
1707 }
1708 else
1709 {
1710 WDLN("Your "+TOOL_TITLE+" protects you from a force ["+
1711 query_verb()+(arg ? " "+arg+"]" : "]"));
1712 }
1713 return TRUE;
1714 }
1715}
1716
1717int DoHistory(string line)
1718{
1719 int i;
1720 string cmd, *strs;
1721
1722 SECURE2(FALSE);
1723 if(!stringp(line) || !sizeof(line))
1724 return TRUE;
1725 else if(line=="%!")
1726 {
1727 WLN("Current command history:");
1728 for(i=MAX_HISTORY; i; --i)
1729 if(history[i-1])
1730 {
1731 W(" "+ARIGHT(""+i, 2, " ")+": ");
1732 if(sizeof(history[i-1])>70)
1733 WLN(ALEFT(history[i-1], 70, " "));
1734 else
1735 WLN(history[i-1]);
1736 }
1737 return TRUE;
1738 }
1739 else if(line[0..1]=="%%" && (cmd=history[0]+line[2..<1]))
1740 {
1741 Command(cmd);
1742 return TRUE;
1743 }
1744 else if(line[0]=='^'&&(strs=strip_explode(line, "^")))
1745 {
1746 if(sizeof(strs)&&strs[0]&&(cmd=history[0]))
1747 {
1748 if(sizeof(strs)==2)
1749 cmd=string_replace(cmd, strs[0], strs[1]);
1750 else
1751 cmd=string_replace(cmd, strs[0], "");
1752 nostore--;
1753 Command(cmd);
1754 nostore++;
1755 return TRUE;
1756 }
1757 }
1758 else if(line[0]=='%' && (sscanf(line[1..<1], "%d", i)))
1759 {
1760 i= i>0 ? i : 1;
1761 i= i<=MAX_HISTORY ? i : MAX_HISTORY;
1762 if(cmd=history[i-1])
1763 Command(cmd);
1764 return TRUE;
1765 }
1766 else if(line[0]=='%')
1767 {
1768 for(i=0; i<MAX_HISTORY; i++)
1769 {
1770 if(history[i]&&
1771 history[i][0..sizeof(line)-2]==line[1.. <1])
1772 {
1773 Command(history[i]);
1774 return TRUE;
1775 }
1776 }
1777 }
1778 else if(nostore<1)
1779 history=({line})+history[0..MAX_HISTORY-2];
1780 return FALSE;
1781}
1782