blob: 802d78f1ddbd6c1e8f3b18c5881631855c3e3d5e [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);
Zesstrac6c18da2019-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;
Zesstrac6c18da2019-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];
Zesstrac6c18da2019-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]=='')
Zesstrac6c18da2019-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
744static string last_file, *last_file_buffer;
745static int last_file_date, last_file_size, last_file_complete;
746
747static string sread_line(int num)
748{
749 if (!morefile) return "";
750 if (last_file!=morefile || last_file_date!=file_time(morefile)) {
751 if (!(last_file=read_bytes(morefile, 0, 50000))) return "";
752 last_file_date=file_time(morefile);
753 last_file_buffer=explode(last_file, "\n");
754 last_file_size=sizeof(last_file_buffer);
755 if (sizeof(last_file)==50000 && last_file[<1]!='\n') {
756 last_file_size--; // letzte Zeile nicht vollstaendig gelesen
757 last_file_complete=0;
758 }
759 else if (file_size(morefile)>50000)
760 last_file_complete=0;
761 else last_file_complete=1;
762 last_file=morefile;
763 }
764 if (num==0) num=1;
765 // bei zu grossen Files nicht mehr alles buffern...
766 if (num>last_file_size) {
767 if (last_file_complete) return "";
768 return (read_file(morefile, num, 1) || "");
769 }
770 return last_file_buffer[num-1]+"\n";
771}
772
773static int CatFile()
774{
775 int end;
776 string l;
777
778 end=moreoffset+morelines;
779 while(moreoffset<end)
MG Mud User88f12472016-06-24 23:31:02 +0200780 if((l=sread_line(moreoffset))!="")
781 {
782 moreoffset++;
783 W(l);
784 }
785 else
786 return FALSE;
Zesstra985baf52019-11-06 23:36:23 +0100787
MG Mud User88f12472016-06-24 23:31:02 +0200788 if(sread_line(moreoffset+1)!="")
789 return TRUE;
790 else
791 return FALSE;
792}
793
794static int XGrepFile(string pat, string file, int mode)
795{
MG Mud User88f12472016-06-24 23:31:02 +0200796 SECURE2(FALSE);
797 TK("XGrepFile: pat: "+pat+" file: "+file+" mode: "+mode);
798 if(!(pat&&file))
799 return FALSE;
Zesstra9f0401a2019-11-06 22:06:02 +0100800
801 // max. Anzahl von Zeilen pro Portion (ueberschlag: 100 Bytes pro Zeile)
802 int maxlines = driver_info(DI_CURRENT_RUNTIME_LIMITS)[LIMIT_FILE] / 100;
803 int start;
804 string buf;
805 // File portionsweise einlesen und verarbeiten
806 while(buf = read_file(file, start, maxlines))
807 {
808 string *lines = strip_explode(buf,"\n");
809 int f; // Pro Treffer erhoeht, benutzt zur einmaligen Ausgabe des Files
810 // ueber alle Zeilen laufen und regexpen
811 foreach(string line : lines)
MG Mud User88f12472016-06-24 23:31:02 +0200812 {
Zesstra9f0401a2019-11-06 22:06:02 +0100813 string *ts=regexp(({(mode&XGREP_ICASE?lower_case(line):line)}),
814 pat);
815 if(sizeof(ts))
MG Mud User88f12472016-06-24 23:31:02 +0200816 {
Zesstra9f0401a2019-11-06 22:06:02 +0100817 if(!(mode&XGREP_REVERT))
818 {
819 if(!f++)
820 write_file(TMP_FILE, "*** File: "+file+" ***\n");
821 write_file(TMP_FILE, line+"\n");
822 }
823 }
824 else if(mode&XGREP_REVERT)
825 {
826 if(!f++)
827 write_file(TMP_FILE, "*** File: "+file+" ***\n");
828 write_file(TMP_FILE, line+"\n");
MG Mud User88f12472016-06-24 23:31:02 +0200829 }
830 }
Zesstra9f0401a2019-11-06 22:06:02 +0100831 if (sizeof(lines) < maxlines)
832 break;
833 else
834 start += sizeof(lines);
835 }
MG Mud User88f12472016-06-24 23:31:02 +0200836 return TRUE;
837}
838
839static void XExecFile(int line)
840{
841 int i;
842
843 if(!scriptline)
844 return;
845 for(i=line; i<scriptsize&&i<line+EXEC_LINES; i++)
846 {
847 if(!scriptline[i])
848 continue;
849 if(!Command(scriptline[i]))
850 {
851 scriptline=NULL;
852 return;
853 }
854 }
855 if(i<scriptsize)
856 call_out("XExecFile", EXEC_TIME, i);
857 else
858 scriptline=NULL;
859}
860
861static void XmtpScript(string dir, string file, string opt)
862{
863 int s, t;
864 string *files;
865
866 s=sizeof(files=get_dir(dir+"/*"));
867 while(s--)
868 {
869 t=sizeof(files[s])-1;
870 if(files[s] == ".." || files[s] == "." || files[s][t] == '~' ||
871 (files[s][0] == '#' && files[s][t] == '#'))
872 continue;
873 if(file_size(dir+"/"+files[s])==-2)
874 {
875 write_file(file, "mkdir "+files[s]+" ; cd "+files[s]+"\n");
876 XmtpScript(dir+"/"+files[s], file, opt);
877 write_file(file, "cd ..\n");
878 }
879 else
880 write_file(file, "mtp -r "+opt+" "+dir+"/"+files[s]+"\n");
881 }
882}
883
884/*----------------------------------------------------------------------
885 * player properties handling
886 */
887
888static string PlayerIdle(object obj)
889{
890 string str;
891 int i, tmp;
892
893 if(!obj)
894 return NULL;
895 if((i=query_idle(obj))>=60)
896 {
897 str=ARIGHT(""+(i/3600), 2, "0");
898 i-=(i/3600)*3600;
899 str+="'"+ARIGHT(""+(i/60), 2, "0");
900 }
901 else
902 str=".....";
903 return str;
904}
905
906static string PlayerAge(object obj)
907{
908 string str;
909 int i, tmp;
910
911 if(!obj)
912 return NULL;
913 i=(int)obj->QueryProp(P_AGE);
914 str=" "+ARIGHT(""+(i/43200), 4, ".");
915 i-=(i/43200)*43200;
916 return str+":"+ARIGHT(""+(i/1800), 2, "0");
917}
918
919static string crname(object who)
920{
921 string uid, lname;
922
923 if((uid=getuid(who))==ROOTID &&
924 object_name(who)[0..7]=="/secure/" &&
925 (lname=(string)who->loginname()))
926 return CAP(lname);
927 return CAP(uid);
928}
929
930static string PlayerWho(object obj)
931{
932 object tmp;
933 string str, stmp;
934 str=ARIGHT(""+LEVEL(obj) , 3, " ");
935 str+=ALEFT(" "+crname(obj)+" ", 12, ".");
936 str+=PlayerAge(obj);
937 str+=((int)obj->QueryProp(P_GENDER)==1 ? " m " : " f ");
938 str+=(obj->QueryProp(P_FROG)) ? "f" : ".";
939 str+=(obj->QueryProp(P_GHOST)) ? "g" : ".";
940 str+=(obj->QueryProp(P_INVIS)) ? "i" : ".";
941 str+=(query_editing(obj)||query_input_pending(obj) ? "e" : ".");
942 str+=(obj->QueryProp(P_AWAY)) ? "a" : ".";
943 str+=" "+PlayerIdle(obj)+" ";
944 str+=(tmp=ENV(obj)) ? ObjFile(tmp) : "- fabric of space -";
945 return str;
946}
947
948static string PlayerMail(object obj, int flag)
949{
950 string pre;
951
952 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
953 return pre+"mail: "+obj->QueryProp(P_MAILADDR);
954}
955
956static string PlayerIP(object obj, int flag)
957{
958 string pre;
959
960 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
961 return pre+"host: "+query_ip_name(obj)+" ("+query_ip_number(obj)+")";
962}
963
964static string PlayerRace(object obj, int flag)
965{
966 string tmp, pre;
967
968 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
969 pre=pre+"race: "+ALEFT(obj->QueryProp(P_RACE)+" ", 10, ".")+" guild: ";
970 tmp=(string)obj->QueryProp(P_GUILD);
971 return tmp ? pre+tmp : pre+"- none -";
972}
973
974static string PlayerDomain(object obj, int flag)
975{
976 int i, s;
977 mixed *uinfo;
978 string *domains, pre;
979
980 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
981 pre+="domainlord of: ";
982 uinfo=(mixed*)MASTER->get_userinfo(getuid(obj));
983 if(uinfo&&(domains=uinfo[3])&&(s=sizeof(domains)))
984 {
985 for(i=0; i<s; i++)
986 {
987 pre += CAP(domains[i]);
988 if(i<s-1)
989 pre += ", ";
990 }
991 }
992 return pre;
993}
994
995static string PlayerStats(object obj, int flag)
996{
997 string pre;
998
999 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1000 pre+="hp="+ARIGHT(obj->QueryProp(P_HP), 3, "0");
1001 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_HP), 3, "0");
1002 pre+=" sp="+ARIGHT(obj->QueryProp(P_SP), 3, "0");
1003 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_SP), 3, "0");
1004 pre+=" food="+ARIGHT(obj->QueryProp(P_FOOD), 3, "0");
1005 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_FOOD), 3, "0");
1006 pre+=" drink="+ARIGHT(obj->QueryProp(P_DRINK), 3, "0");
1007 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_DRINK), 3, "0");
1008 pre+=" exps="+obj->QueryProp(P_XP);
1009 return pre;
1010}
1011
1012static string PlayerSnoop(object obj, int flag)
1013{
1014 string tmp, pre;
1015 object victim;
1016
1017 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1018 pre=pre+"is snooped by: ";
1019 if(victim=query_snoop(obj))
1020 pre+=ARIGHT(" "+crname(victim), 12, ".");
1021 else
1022 pre+="............";
1023 return pre;
1024}
1025
1026static string PlayerCmdAvg(object obj, int flag)
1027{
1028 string pre;
1029
1030 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1031 return pre+"cmdavg: "+(int)obj->_query_command_average();
1032}
1033
1034
1035/*----------------------------------------------------------------------
1036 * msg input to objects
1037 */
1038
1039static void XMsgSay(string str)
1040{
1041 if(str=="."||str=="**")
1042 {
1043 WLN("[End of message]");
1044 say("[End of message]\n");
1045 }
1046 else
1047 {
1048 say(str+"\n");
1049 input_to("XMsgSay");
1050 }
1051}
1052
1053static void XMsgTell(string str)
1054{
1055 if(str=="."||str=="**")
1056 {
1057 WLN("[End of message]");
1058 tell_object(msgto, "[End of message]\n");
1059 }
1060 else
1061 {
1062 tell_object(msgto, str+"\n");
1063 input_to("XMsgTell");
1064 }
1065}
1066
1067static void XMsgShout(string str)
1068{
1069 if(str=="."||str=="**")
1070 {
1071 WLN("[End of message]");
1072 shout("[End of message]\n");
1073 }
1074 else
1075 {
1076 shout(str+"\n");
1077 input_to("XMsgShout");
1078 }
1079}
1080
1081/*----------------------------------------------------------------------
1082 * own object moving
1083 */
1084
1085int move(mixed dest)
1086{
1087 move_object(ME, cloner?cloner:dest);
1088 return TRUE;
1089}
1090
1091/*----------------------------------------------------------------------
1092 * object id
1093 */
1094
1095int id(string str)
1096{
1097 if(!security()&&MODE(MODE_SCANCHK)&&RTP&&!IS_ARCH(RTP))
1098 WDLN(crname(RTP)+" scanned you (id) ["+query_verb()+"] "+
1099 (PREV ? ObjFile(PREV) : "[destructed object]"));
1100 return LOWER(str)==LOWER(TOOL_NAME);
1101}
1102
1103/*----------------------------------------------------------------------
1104 * short and long description
1105 */
1106
1107string short()
1108{
1109 return _query_short()+".\n";
1110}
1111
1112string _query_short()
1113{
1114 string sh; // added by Rumata
1115 if(cloner)
1116 {
1117 if((!security())&&MODE(MODE_SCANCHK)&&!IS_ARCH(RTP))
1118 WDLN(crname(RTP)+" scanned you (short) ["+query_verb()+"] "+
1119 (PREV ? ObjFile(PREV) : "[destructed object]"));
1120 if( sh=Query(P_SHORT) ) return sh; // added by Rumata
1121 return cloner->name(WESSEN)+" "+TOOL_TITLE+" ["+
1122 ctime(time())[11..18]+"]";
1123 }
1124 return TOOL_TITLE;
1125}
1126
1127string long()
1128{
1129 return _query_long();
1130}
1131
1132string _query_long()
1133{
1134 if(cloner&&!security()&&MODE(MODE_SCANCHK)&&!IS_ARCH(RTP))
1135 {
1136 WDLN(crname(RTP)+" scanned you (long) ["+query_verb()+"] "+
1137 (PREV ? ObjFile(PREV) : "[destructed object]"));
1138 }
1139 return
1140 "This is "+TOOL_NAME+" version "+TOOL_VERSION+
1141 " (maintained by Kirk@MorgenGrauen)\n"+
1142 "Original copyright held by Hyp.\n"+
1143 "Gamedriver patchlevel: "+__VERSION__+" master object: "+__MASTER_OBJECT__+
1144 "\n\nDo 'xhelp' for more information.\n";
1145}
1146
1147string name(mixed dummy1, mixed dummy2)
1148{
1149 return _query_short();
1150}
1151
1152/*----------------------------------------------------------------------
1153 * light stuff
1154 */
1155
1156int _query_light()
1157{
1158 return xlight;
1159}
1160
1161int _set_light(int x)
1162{
1163 return xlight;
1164}
1165
1166/*----------------------------------------------------------------------
1167 * Autoloading
1168 */
1169
1170mixed *_query_autoloadobj()
1171{
1172 return AUTOLOAD_ARGS;
1173}
1174
1175void _set_autoloadobj(mixed *args)
1176{
1177 WLN(TOOL_TITLE+" ...");
1178 if(!pointerp(args))
1179 ;
1180 else if(sizeof(args)!=3)
1181 ;
1182 else if(!stringp(args[0]))
1183 ;
1184 else if(!intp(args[1]))
1185 ;
1186 else if(!intp(args[2]))
1187 ;
1188 else
1189 {
1190 if((string)args[0]!=TOOL_INTERNAL)
1191 {
1192 WLN("*****************************");
1193 WLN("*** NEW EDITION ***");
1194 WLN("*** do 'xtool news' for ***");
1195 WLN("*** more information ***");
1196 WLN("*****************************");
1197 }
1198 modi=(int)args[1];
1199 morelines=(int)args[2];
1200 return;
1201 }
1202 W("(bad autoload, using default)\n");
1203}
1204
1205/*----------------------------------------------------------------------
1206 * creation, updating and initialization stuff
1207 */
1208
1209void update_tool(mixed *args, object obj)
1210{
1211 SECURE1();
1212 if(!(obj&&args))
1213 return;
1214 Destruct(PREV);
1215 _set_autoloadobj(args);
1216 move(obj);
1217}
1218
1219void create()
1220{
1221 object obj;
1222
1223 if(member(object_name(),'#')<0)
1224 return;
1225 if(!cloner&&!((cloner=TP)||(cloner=ENV(ME)))&&!interactive(cloner))
1226 destruct(ME);
1227 if(!IS_LEARNER(cloner))
1228 destruct(ME);
1229 SetProp(P_NODROP,"Das waere zu gefaehrlich.\n");
1230 SetProp(P_NEVERDROP,1);
1231 SetProp(P_NOBUY,1);
1232 if(file_size(SAVE_FILE+".o")>0)
1233 {
1234 WDLN("Loading "+TOOL_TITLE+" settings");
1235 restore_object(SAVE_FILE);
1236 }
1237 if(MODE(MODE_FIRST))
1238 call_out("move",0,cloner);
Zesstra7e95e3f2019-10-19 11:15:05 +02001239 call_out("add_insert_hook",1);
MG Mud User88f12472016-06-24 23:31:02 +02001240}
1241
1242void TK(string str)
1243{
1244 if (!xtk)
1245 return;
1246 tell_object(cloner,"XTOOL: "+str+"\n");
1247}
1248
1249int Xtk(string str)
1250{
1251 xtk=!xtk;
1252 WDLN("Xtool internal tracing "+(xtk?"enabled":"disabled"));
1253 return TRUE;
1254}
1255
MG Mud User88f12472016-06-24 23:31:02 +02001256void init()
1257{
1258 object first, prev;
1259
1260 if(member(object_name(),'#')<0) return;
1261 first=first_inventory(ENV(ME));
1262 if(MODE(MODE_PROTECT)&&is_player(first)&&!IS_ARCH(first))
1263 {
1264 WDLN("WARNING: "+crname(first)+" tried to move into your inventory");
1265 tell_object(first, "You cannot move yourself into "+
1266 crname(cloner)+"'s inventory.\n");
1267 call_out("DropObj",0,first);
1268 return;
1269 }
1270 else if(MODE(MODE_FIRST)&&first!=ME)
1271 move(cloner);
1272 else actions();
1273}
1274
1275void DropObj(object obj)
1276{
1277 if(!obj||!objectp(obj))
1278 return;
1279 obj->move(ENV(cloner),M_NOCHECK|M_NO_SHOW);
1280}
1281
1282#define ACTIONS\
1283([\
1284 "xcallouts" : "Xcallouts";0;1,\
1285 "xcall" : "Xcall";0;1,\
1286 "xcat" : "Xcat";1;1,\
1287 "xcd" : "Xcd";0;0,\
1288 "xclean" : "Xclean";0;0,\
1289 "xclone" : "Xclone";0;0,\
1290 "xuclone" : "Xuclone";0;0,\
1291 "xcmds" : "Xcmds";0;1,\
1292 "xdbg" : "Xdbg";0;0,\
1293 "xdclean" : "Xdclean";0;0,\
1294 "xddes" : "Xddes";0;0,\
1295 "xdes" : "Xdes";0;0,\
1296 "xdest" : "Xdes";0;0,\
1297 "xdlook" : "Xdlook";0;1,\
1298 "xdo" : "Xdo";0;0,\
1299 "xdupdate" : "Xdupdate";0;0,\
1300 "xecho" : "Xecho";0;0,\
1301 "xeval" : "Xeval";0;1,\
1302 "xforall" : "Xforall";0;0,\
1303 "xgoto" : "Xgoto";0;0,\
1304 "xhbeats" : "Xhbeats";0;1,\
1305 "xgrep" : "Xgrep";1;1,\
1306 "xhead" : "Xhead";1;1,\
1307 "xhelp" : "Xhelp";0;0,\
1308 "xinventory": "Xinventory";0;1,\
1309 "xids" : "Xids";0;0,\
1310 "xinfo" : "Xinfo";0;0,\
1311 "xinherit" : "Xinherit";0;1,\
1312 "xlag" : "Xlag";0;0,\
1313 "xlight" : "Xlight";0;0,\
1314 "xload" : "Xload";0;0,\
1315 "xlook" : "Xlook";0;1,\
1316 "xlpc" : "Xlpc";0;0,\
1317 "xman" : "Xman";0;0,\
1318 "xmore" : "Xmore";1;0,\
1319 "xmove" : "Xmove";0;0,\
1320 "xmsg" : "Xmsg";1;0,\
1321 "xmtp" : "Xmtp";0;0,\
1322 "xproc" : "Xproc";0;1,\
1323 "xprof" : "Xprof";0;0,\
1324 "xprops" : "Xprops";0;1,\
1325 "xquit" : "Xquit";0;0,\
1326 "xscan" : "Xscan";0;0,\
1327 "xset" : "Xset";0;0,\
1328 "xsh" : "Xsh";0;0,\
1329 "xsort" : "Xsort";1;1,\
1330 "xtail" : "Xtail";1;1,\
1331 "xtk" : "Xtk";0;0,\
1332 "xtool" : "Xtool";0;0,\
1333 "xtrace" : "Xtrace";0;0,\
1334 "xtrans" : "Xtrans";0;0,\
1335 "xupdate" : "Xupdate";0;0,\
1336 "xwc" : "Xwc";1;0,\
1337 "xwho" : "Xwho";0;1,\
1338 ])
1339
1340static string PrepareLine(string str)
1341{
1342 return str;
1343}
1344
1345static string QuoteLine(string str)
1346{
1347 string *tmp,*tmp2;
1348 int i, i2, len, len2, qd, qs;
1349
Zesstrac6c18da2019-11-06 22:38:18 +01001350 str=string_replace(str,"\\\\","\u00B0""BSHL");
1351 str=string_replace(str,"\\\"","\u00B0""DBLQ");
1352 str=string_replace(str,"\\\'","\u00B0""SGLQ");
1353 str=string_replace(str,"\\|","\u00B0""PIPE");
1354 str=string_replace(str,"||","\u00B0""OROR");
1355 str=string_replace(str,"->","\u00B0""LARR");
1356 str=string_replace(str,"\\$","\u00B0""DOLR");
MG Mud User88f12472016-06-24 23:31:02 +02001357 tmp=regexplode(str,"(\"|')");
1358 len=sizeof(tmp);
1359 qd=qs=0;
1360 for(i=0;i<len;i++)
1361 {
1362 if(i%2)
1363 {
1364 if(tmp[i]=="'")
1365 qd=(!qs?!qd:qd);
1366 else
1367 qs=(!qd?!qs:qs);
1368 if((tmp[i]=="\""&&!qd)||(tmp[i]=="'"&&!qs))
1369 tmp[i]="";
1370 }
1371 else
1372 {
1373 if(!qd)
1374 {
1375 len2=sizeof(tmp2=regexplode(tmp[i],"\\$[^ ][^ ]*"));
1376 for(i2=0;i2<len2;i2++)
1377 if(i2%2)
1378 {
1379 TK("QuoteLine: "+tmp2[i2][1..]);
1380 tmp2[i2]=(string)XFindObj((tmp2[i2])[1..]);
1381 }
1382 tmp[i]=implode(tmp2,"");
1383 }
1384 }
1385 }
1386 if(qd||qs)
1387 return NULL;
1388 str=implode(tmp,"");
1389 TK("QuoteLine: str: "+str);
1390 return str;
1391}
1392
1393static string UnquoteLine(string str)
1394{
Zesstrac6c18da2019-11-06 22:38:18 +01001395 str=string_replace(str,"\u00B0""BSHL","\\");
1396 str=string_replace(str,"\u00B0""DBLQ","\"");
1397 str=string_replace(str,"\u00B0""SGLQ","\'");
1398 str=string_replace(str,"\u00B0""DQUO","");
1399 str=string_replace(str,"\u00B0""SQUO","");
1400 str=string_replace(str,"\u00B0""PIPE","|");
1401 str=string_replace(str,"\u00B0""OROR","||");
1402 str=string_replace(str,"\u00B0""LARR","->");
1403 str=string_replace(str,"\u00B0""DOLR","$");
MG Mud User88f12472016-06-24 23:31:02 +02001404 TK("UnquoteLine: str: "+str);
1405 return str;
1406}
1407
1408static string *ExplodeCmds(string str)
1409{
1410 string *tmp;
1411
1412 tmp=regexplode(str,"\\||>|>>");
1413 while(tmp[<1]=="")
1414 tmp=tmp[0..<2];
1415 return tmp;
1416}
1417
1418varargs int ParseLine(string str)
1419{
1420 string verb, arg;
1421 int ret;
1422
1423 TK("ParseLine: str: "+(str?str:""));
1424 if(!sizeof(cmds))
1425 {
1426 // this is a single command or beginning of a command pipe
1427 verb=query_verb();
1428
1429 // return on unknown commands
1430 if(!verb||!sizeof(verb)||!GetFunc(verb,TRUE))
1431 return FALSE;
1432
1433 str=(string)this_player()->_unparsed_args();
1434 pipe_in=FALSE;
1435 pipe_of=NULL;
1436 pipe_ovr=TRUE;
1437 pipe_out=FALSE;
1438 pipe_of=NULL;
1439 // pass arguments to some special functions unparsed
1440 if(member(({"xlpc","xcall","xeval"}),verb)>=0)
1441 {
1442#ifdef XDBG
1443 TK("ParseLine: special func: "+verb);
1444#endif
1445 ret=CallFunc(verb,str);
1446 SafeReturn(ret);
1447 }
1448 // ok, here we go
1449 pipe_in=pipe_out=FALSE;
1450 pipe_if=pipe_of=NULL;
1451 pipe_ovr=TRUE;
1452 if(file_size(PIPE_FILE)>=0)
1453 rm(PIPE_FILE);
1454 if (str&&str!="")
1455 {
1456 if(!(str=QuoteLine(str)))
1457 {
1458 WDLN("Unterminated quotation");
1459 SafeReturn(TRUE);
1460 }
1461 cmds=ExplodeCmds(str);
1462 }
1463 else
1464 cmds=({""});
1465 arg=strip_string(cmds[0]);
1466 }
1467 else
1468 {
1469 cmds[0]=strip_string(cmds[0]);
1470 TK("ParseLine: cmds[0]: "+cmds[0]);
1471 if(sscanf(cmds[0],"%s %s",verb,arg)!=2)
1472 {
1473 verb=cmds[0];
1474 arg="";
1475 }
1476 }
1477 cmds=cmds[1..];
1478 TK("ParseLine: verb: "+verb);
1479 if (!verb||!sizeof(verb)||!GetFunc(verb,TRUE))
1480 SafeReturn(FALSE);
1481 TK("ParseLine(1): arg: "+arg+" cmds: "+sprintf("%O",cmds));
1482 switch(sizeof(cmds))
1483 {
1484 case 0:
1485 ret=CallFunc(verb,strip_string(UnquoteLine(arg)));
1486 SafeReturn(ret);
1487 break;
1488
1489 case 1:
1490 WDLN("Missing rhs of command pipe");
1491 SafeReturn(TRUE);
1492 break;
1493
1494 default:
1495 pipe_out=TRUE;
1496 switch(cmds[0])
1497 {
1498 case "|":
1499 pipe_of=PIPE_FILE;
1500 pipe_ovr=TRUE;
1501 cmds=cmds[1..];
1502 break;
1503
1504 case ">":
1505 pipe_ovr=TRUE;
1506 if(sizeof(cmds)!=2)
1507 {
1508 WDLN("Illegal IO redirection");
1509 SafeReturn(TRUE);
1510 }
1511 pipe_of=cmds[1];
1512 cmds=({});
1513 break;
1514
1515 case ">>":
1516 pipe_ovr=FALSE;
1517 if(sizeof(cmds)!=2)
1518 {
1519 WDLN("Illegal IO redirection");
1520 SafeReturn(TRUE);
1521 }
1522 pipe_of=cmds[1];
1523 cmds=({});
1524 break;
1525 }
1526 }
1527 TK("ParseLine(2): arg: "+arg+" cmds: "+sprintf("%O",cmds));
1528 if(!CallFunc(verb,strip_string(arg)))
1529 SafeReturn(FALSE);
1530 pipe_in=pipe_out;
1531 pipe_if=pipe_of;
1532 pipe_ovr=FALSE;
1533 pipe_out=FALSE;
1534 pipe_of=NULL;
1535 if(sizeof(cmds))
1536 call_out("ParseLine",0);
1537 else
1538 SafeReturn(TRUE);
1539 return TRUE;
1540}
1541
1542static int CallFunc(string verb, string str)
1543{
1544 string fun;
1545
1546 fun=GetFunc(verb,FALSE);
1547#ifdef XDBG
1548 TK("CallFunc: verb: "+verb+" str: "+str);
1549 TK("CallFunc: resolved function: "+(fun?fun:"(unresolved)"));
1550#endif
1551 if(str=="")
1552 str=NULL;
1553 return fun?(int)call_other(ME,fun,str):FALSE;
1554}
1555
1556static string GetFunc(string verb, int test)
1557{
1558 string fun,*keys,key;
1559 int i,len;
1560
1561 TK("GetFunc: verb: "+verb);
1562
1563 if(verb[0..0]!="x") // Assume all commands start with "x"
1564 return 0;
1565
1566 if (!(fun=(string)ACTIONS[verb,0])) { // Try exact hit first
1567 key="";
1568 len=sizeof(verb);
1569 for (i=sizeof(keys=m_indices(ACTIONS))-1;i>=0;i--) {
1570 TK(" trying: "+keys[i]);
1571 if(sizeof(keys[i])>=len&&keys[i][0..len-1]==verb) {
1572 if(sizeof(key)) {
1573 WLN("Das ist nicht eindeutig ...");
1574 return 0;
1575 }
1576 fun=ACTIONS[keys[i],0];
1577 key=keys[i];
1578 //break;
1579 }
1580 }
1581 } else
1582 key=verb;
1583
1584 if(test)
1585 return fun;
1586
1587 if (key) {
1588#ifdef XDBG
1589 TK("GetFunc: fun: "+fun+" (key: "+key+")\n"+
1590 "pipe_in: "+(pipe_in?"TRUE ":"FALSE ")+(pipe_if?pipe_if:"(NULL)")+"\n"+
1591 "pipe_out: "+(pipe_out?"TRUE ":"FALSE ")+(pipe_of?pipe_of:"(NULL)")+"\n"+
1592 "pipe_ovr: "+(pipe_ovr?"TRUE":"FALSE"));
1593#endif
1594 if (pipe_in&&!ACTIONS[key,PIPE_IN])
1595 {
1596 // this command does not read pipes
1597#ifdef XDBG
1598 TK("Illegal rhs of command pipe \""+fun+"\"\n");
1599#endif
1600 notify_fail("Illegal rhs of command pipe \""+fun+"\"\n");
1601 return 0;
1602 }
1603 else if (pipe_out&&!ACTIONS[key,PIPE_OUT])
1604 {
1605 // this command does not feed pipes
1606#ifdef XDBG
1607 TK("Illegal lhs of command pipe \""+fun+"\"\n");
1608#endif
1609 notify_fail("Illegal lhs of command pipe \""+fun+"\"\n");
1610 return 0;
1611 }
1612 }
1613 return fun;
1614}
1615
1616void actions()
1617{
1618 if (!cloner||!RTP||cloner==RTP||query_wiz_level(cloner)<=query_wiz_level(RTP))
1619 add_action("ParseLine","",1);
1620 add_action("CommandScan", "", 1);
1621}
1622
1623/*----------------------------------------------------------------------
1624 * the checking stuff
1625 */
1626
Zesstra7e95e3f2019-10-19 11:15:05 +02001627public <int|object>* insert_hook(object pl, int hookid, object ob)
MG Mud User88f12472016-06-24 23:31:02 +02001628{
Zesstra7e95e3f2019-10-19 11:15:05 +02001629 if(cloner && cloner == pl && hookid == H_HOOK_INSERT)
1630 {
1631 if(MODE(MODE_FIRST) && find_call_out("move")==-1)
1632 call_out("move",0,cloner);
1633 if(MODE(MODE_INVCHECK))
1634 write_newinvobj(ob);
1635 }
1636 return ({H_NO_MOD, ob});
1637}
1638
1639void add_insert_hook()
1640{
1641 if(objectp(cloner))
1642 cloner->HRegisterToHook(H_HOOK_INSERT, #'insert_hook,
1643 H_HOOK_LIBPRIO(2), H_LISTENER, 0);
MG Mud User88f12472016-06-24 23:31:02 +02001644}
1645
1646static void VarCheck(int show)
1647{
1648 int i, s;
1649 foreach(string k, mixed v : variable)
1650 {
1651 if (v) continue;
1652 if(show) WDLN("*** Variable $"+k+" has been destructed");
1653 m_delete(variable, k);
1654 }
1655}
1656
1657
1658int write_newinvobj(object obj)
1659{
1660 if(obj) WDLN("*** New object in inventory "+ObjFile(obj));
1661 return(1);
1662}
1663
1664/*----------------------------------------------------------------------
1665 * catch all commands, absorb forces and check history
1666 */
1667
1668int CommandScan(string arg)
1669{
1670 string verb, cmd;
1671 object rtp;
1672 rtp=RTP;
1673
1674 if(!cloner&&!(cloner=rtp)) destruct(ME);
1675
1676 if((!MODE(MODE_PROTECT))||security()||
1677 query_wiz_level(cloner)<query_wiz_level(rtp))
1678 {
1679 verb=query_verb();
1680 if(verb&&DoHistory(verb+(arg ? " "+arg : "")))
1681 return TRUE;
1682 nostore=FALSE;
1683 return FALSE;
1684 }
1685 else
1686 {
1687 if(rtp)
1688 {
1689 WDLN("Your "+TOOL_TITLE+" protects you from a force by "+crname(rtp)+
1690 " ["+query_verb()+(arg ? " "+arg+"]" : "]"));
1691 tell_object(rtp, crname(cloner)+"'s "+TOOL_TITLE+
1692 " absorbes your force.\n");
1693 }
1694 else
1695 {
1696 WDLN("Your "+TOOL_TITLE+" protects you from a force ["+
1697 query_verb()+(arg ? " "+arg+"]" : "]"));
1698 }
1699 return TRUE;
1700 }
1701}
1702
1703int DoHistory(string line)
1704{
1705 int i;
1706 string cmd, *strs;
1707
1708 SECURE2(FALSE);
1709 if(!stringp(line) || !sizeof(line))
1710 return TRUE;
1711 else if(line=="%!")
1712 {
1713 WLN("Current command history:");
1714 for(i=MAX_HISTORY; i; --i)
1715 if(history[i-1])
1716 {
1717 W(" "+ARIGHT(""+i, 2, " ")+": ");
1718 if(sizeof(history[i-1])>70)
1719 WLN(ALEFT(history[i-1], 70, " "));
1720 else
1721 WLN(history[i-1]);
1722 }
1723 return TRUE;
1724 }
1725 else if(line[0..1]=="%%" && (cmd=history[0]+line[2..<1]))
1726 {
1727 Command(cmd);
1728 return TRUE;
1729 }
1730 else if(line[0]=='^'&&(strs=strip_explode(line, "^")))
1731 {
1732 if(sizeof(strs)&&strs[0]&&(cmd=history[0]))
1733 {
1734 if(sizeof(strs)==2)
1735 cmd=string_replace(cmd, strs[0], strs[1]);
1736 else
1737 cmd=string_replace(cmd, strs[0], "");
1738 nostore--;
1739 Command(cmd);
1740 nostore++;
1741 return TRUE;
1742 }
1743 }
1744 else if(line[0]=='%' && (sscanf(line[1..<1], "%d", i)))
1745 {
1746 i= i>0 ? i : 1;
1747 i= i<=MAX_HISTORY ? i : MAX_HISTORY;
1748 if(cmd=history[i-1])
1749 Command(cmd);
1750 return TRUE;
1751 }
1752 else if(line[0]=='%')
1753 {
1754 for(i=0; i<MAX_HISTORY; i++)
1755 {
1756 if(history[i]&&
1757 history[i][0..sizeof(line)-2]==line[1.. <1])
1758 {
1759 Command(history[i]);
1760 return TRUE;
1761 }
1762 }
1763 }
1764 else if(nostore<1)
1765 history=({line})+history[0..MAX_HISTORY-2];
1766 return FALSE;
1767}
1768