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