blob: bc5a88962886e70443768060366fac94154071bf [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{
1001 int i, s;
1002 mixed *uinfo;
1003 string *domains, pre;
1004
1005 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1006 pre+="domainlord of: ";
1007 uinfo=(mixed*)MASTER->get_userinfo(getuid(obj));
1008 if(uinfo&&(domains=uinfo[3])&&(s=sizeof(domains)))
1009 {
1010 for(i=0; i<s; i++)
1011 {
1012 pre += CAP(domains[i]);
1013 if(i<s-1)
1014 pre += ", ";
1015 }
1016 }
1017 return pre;
1018}
1019
1020static string PlayerStats(object obj, int flag)
1021{
1022 string pre;
1023
1024 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1025 pre+="hp="+ARIGHT(obj->QueryProp(P_HP), 3, "0");
1026 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_HP), 3, "0");
1027 pre+=" sp="+ARIGHT(obj->QueryProp(P_SP), 3, "0");
1028 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_SP), 3, "0");
1029 pre+=" food="+ARIGHT(obj->QueryProp(P_FOOD), 3, "0");
1030 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_FOOD), 3, "0");
1031 pre+=" drink="+ARIGHT(obj->QueryProp(P_DRINK), 3, "0");
1032 pre+="/"+ARIGHT(obj->QueryProp(P_MAX_DRINK), 3, "0");
1033 pre+=" exps="+obj->QueryProp(P_XP);
1034 return pre;
1035}
1036
1037static string PlayerSnoop(object obj, int flag)
1038{
1039 string tmp, pre;
1040 object victim;
1041
1042 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1043 pre=pre+"is snooped by: ";
1044 if(victim=query_snoop(obj))
1045 pre+=ARIGHT(" "+crname(victim), 12, ".");
1046 else
1047 pre+="............";
1048 return pre;
1049}
1050
1051static string PlayerCmdAvg(object obj, int flag)
1052{
1053 string pre;
1054
1055 pre=(flag) ? ALEFT(crname(obj)+" ", 12, ".")+" " : "";
1056 return pre+"cmdavg: "+(int)obj->_query_command_average();
1057}
1058
1059
1060/*----------------------------------------------------------------------
1061 * msg input to objects
1062 */
1063
1064static void XMsgSay(string str)
1065{
1066 if(str=="."||str=="**")
1067 {
1068 WLN("[End of message]");
1069 say("[End of message]\n");
1070 }
1071 else
1072 {
1073 say(str+"\n");
1074 input_to("XMsgSay");
1075 }
1076}
1077
1078static void XMsgTell(string str)
1079{
1080 if(str=="."||str=="**")
1081 {
1082 WLN("[End of message]");
1083 tell_object(msgto, "[End of message]\n");
1084 }
1085 else
1086 {
1087 tell_object(msgto, str+"\n");
1088 input_to("XMsgTell");
1089 }
1090}
1091
1092static void XMsgShout(string str)
1093{
1094 if(str=="."||str=="**")
1095 {
1096 WLN("[End of message]");
1097 shout("[End of message]\n");
1098 }
1099 else
1100 {
1101 shout(str+"\n");
1102 input_to("XMsgShout");
1103 }
1104}
1105
1106/*----------------------------------------------------------------------
1107 * own object moving
1108 */
1109
1110int move(mixed dest)
1111{
1112 move_object(ME, cloner?cloner:dest);
1113 return TRUE;
1114}
1115
1116/*----------------------------------------------------------------------
1117 * object id
1118 */
1119
1120int id(string str)
1121{
1122 if(!security()&&MODE(MODE_SCANCHK)&&RTP&&!IS_ARCH(RTP))
1123 WDLN(crname(RTP)+" scanned you (id) ["+query_verb()+"] "+
1124 (PREV ? ObjFile(PREV) : "[destructed object]"));
1125 return LOWER(str)==LOWER(TOOL_NAME);
1126}
1127
1128/*----------------------------------------------------------------------
1129 * short and long description
1130 */
1131
1132string short()
1133{
1134 return _query_short()+".\n";
1135}
1136
1137string _query_short()
1138{
1139 string sh; // added by Rumata
1140 if(cloner)
1141 {
1142 if((!security())&&MODE(MODE_SCANCHK)&&!IS_ARCH(RTP))
1143 WDLN(crname(RTP)+" scanned you (short) ["+query_verb()+"] "+
1144 (PREV ? ObjFile(PREV) : "[destructed object]"));
1145 if( sh=Query(P_SHORT) ) return sh; // added by Rumata
1146 return cloner->name(WESSEN)+" "+TOOL_TITLE+" ["+
1147 ctime(time())[11..18]+"]";
1148 }
1149 return TOOL_TITLE;
1150}
1151
1152string long()
1153{
1154 return _query_long();
1155}
1156
1157string _query_long()
1158{
1159 if(cloner&&!security()&&MODE(MODE_SCANCHK)&&!IS_ARCH(RTP))
1160 {
1161 WDLN(crname(RTP)+" scanned you (long) ["+query_verb()+"] "+
1162 (PREV ? ObjFile(PREV) : "[destructed object]"));
1163 }
1164 return
1165 "This is "+TOOL_NAME+" version "+TOOL_VERSION+
1166 " (maintained by Kirk@MorgenGrauen)\n"+
1167 "Original copyright held by Hyp.\n"+
1168 "Gamedriver patchlevel: "+__VERSION__+" master object: "+__MASTER_OBJECT__+
1169 "\n\nDo 'xhelp' for more information.\n";
1170}
1171
1172string name(mixed dummy1, mixed dummy2)
1173{
1174 return _query_short();
1175}
1176
1177/*----------------------------------------------------------------------
1178 * light stuff
1179 */
1180
1181int _query_light()
1182{
1183 return xlight;
1184}
1185
1186int _set_light(int x)
1187{
1188 return xlight;
1189}
1190
1191/*----------------------------------------------------------------------
1192 * Autoloading
1193 */
1194
1195mixed *_query_autoloadobj()
1196{
1197 return AUTOLOAD_ARGS;
1198}
1199
1200void _set_autoloadobj(mixed *args)
1201{
1202 WLN(TOOL_TITLE+" ...");
1203 if(!pointerp(args))
1204 ;
1205 else if(sizeof(args)!=3)
1206 ;
1207 else if(!stringp(args[0]))
1208 ;
1209 else if(!intp(args[1]))
1210 ;
1211 else if(!intp(args[2]))
1212 ;
1213 else
1214 {
1215 if((string)args[0]!=TOOL_INTERNAL)
1216 {
1217 WLN("*****************************");
1218 WLN("*** NEW EDITION ***");
1219 WLN("*** do 'xtool news' for ***");
1220 WLN("*** more information ***");
1221 WLN("*****************************");
1222 }
1223 modi=(int)args[1];
1224 morelines=(int)args[2];
1225 return;
1226 }
1227 W("(bad autoload, using default)\n");
1228}
1229
1230/*----------------------------------------------------------------------
1231 * creation, updating and initialization stuff
1232 */
1233
1234void update_tool(mixed *args, object obj)
1235{
1236 SECURE1();
1237 if(!(obj&&args))
1238 return;
1239 Destruct(PREV);
1240 _set_autoloadobj(args);
1241 move(obj);
1242}
1243
1244void create()
1245{
1246 object obj;
1247
1248 if(member(object_name(),'#')<0)
1249 return;
1250 if(!cloner&&!((cloner=TP)||(cloner=ENV(ME)))&&!interactive(cloner))
1251 destruct(ME);
1252 if(!IS_LEARNER(cloner))
1253 destruct(ME);
1254 SetProp(P_NODROP,"Das waere zu gefaehrlich.\n");
1255 SetProp(P_NEVERDROP,1);
1256 SetProp(P_NOBUY,1);
1257 if(file_size(SAVE_FILE+".o")>0)
1258 {
1259 WDLN("Loading "+TOOL_TITLE+" settings");
1260 restore_object(SAVE_FILE);
1261 }
1262 if(MODE(MODE_FIRST))
1263 call_out("move",0,cloner);
Zesstra7e95e3f2019-10-19 11:15:05 +02001264 call_out("add_insert_hook",1);
MG Mud User88f12472016-06-24 23:31:02 +02001265}
1266
1267void TK(string str)
1268{
1269 if (!xtk)
1270 return;
1271 tell_object(cloner,"XTOOL: "+str+"\n");
1272}
1273
1274int Xtk(string str)
1275{
1276 xtk=!xtk;
1277 WDLN("Xtool internal tracing "+(xtk?"enabled":"disabled"));
1278 return TRUE;
1279}
1280
MG Mud User88f12472016-06-24 23:31:02 +02001281void init()
1282{
1283 object first, prev;
1284
1285 if(member(object_name(),'#')<0) return;
1286 first=first_inventory(ENV(ME));
1287 if(MODE(MODE_PROTECT)&&is_player(first)&&!IS_ARCH(first))
1288 {
1289 WDLN("WARNING: "+crname(first)+" tried to move into your inventory");
1290 tell_object(first, "You cannot move yourself into "+
1291 crname(cloner)+"'s inventory.\n");
1292 call_out("DropObj",0,first);
1293 return;
1294 }
1295 else if(MODE(MODE_FIRST)&&first!=ME)
1296 move(cloner);
1297 else actions();
1298}
1299
1300void DropObj(object obj)
1301{
1302 if(!obj||!objectp(obj))
1303 return;
1304 obj->move(ENV(cloner),M_NOCHECK|M_NO_SHOW);
1305}
1306
1307#define ACTIONS\
1308([\
1309 "xcallouts" : "Xcallouts";0;1,\
1310 "xcall" : "Xcall";0;1,\
1311 "xcat" : "Xcat";1;1,\
1312 "xcd" : "Xcd";0;0,\
1313 "xclean" : "Xclean";0;0,\
1314 "xclone" : "Xclone";0;0,\
1315 "xuclone" : "Xuclone";0;0,\
1316 "xcmds" : "Xcmds";0;1,\
1317 "xdbg" : "Xdbg";0;0,\
1318 "xdclean" : "Xdclean";0;0,\
1319 "xddes" : "Xddes";0;0,\
1320 "xdes" : "Xdes";0;0,\
1321 "xdest" : "Xdes";0;0,\
1322 "xdlook" : "Xdlook";0;1,\
1323 "xdo" : "Xdo";0;0,\
1324 "xdupdate" : "Xdupdate";0;0,\
1325 "xecho" : "Xecho";0;0,\
1326 "xeval" : "Xeval";0;1,\
1327 "xforall" : "Xforall";0;0,\
1328 "xgoto" : "Xgoto";0;0,\
1329 "xhbeats" : "Xhbeats";0;1,\
1330 "xgrep" : "Xgrep";1;1,\
1331 "xhead" : "Xhead";1;1,\
1332 "xhelp" : "Xhelp";0;0,\
1333 "xinventory": "Xinventory";0;1,\
1334 "xids" : "Xids";0;0,\
1335 "xinfo" : "Xinfo";0;0,\
1336 "xinherit" : "Xinherit";0;1,\
1337 "xlag" : "Xlag";0;0,\
1338 "xlight" : "Xlight";0;0,\
1339 "xload" : "Xload";0;0,\
1340 "xlook" : "Xlook";0;1,\
1341 "xlpc" : "Xlpc";0;0,\
1342 "xman" : "Xman";0;0,\
1343 "xmore" : "Xmore";1;0,\
1344 "xmove" : "Xmove";0;0,\
1345 "xmsg" : "Xmsg";1;0,\
1346 "xmtp" : "Xmtp";0;0,\
1347 "xproc" : "Xproc";0;1,\
1348 "xprof" : "Xprof";0;0,\
1349 "xprops" : "Xprops";0;1,\
1350 "xquit" : "Xquit";0;0,\
1351 "xscan" : "Xscan";0;0,\
1352 "xset" : "Xset";0;0,\
1353 "xsh" : "Xsh";0;0,\
1354 "xsort" : "Xsort";1;1,\
1355 "xtail" : "Xtail";1;1,\
1356 "xtk" : "Xtk";0;0,\
1357 "xtool" : "Xtool";0;0,\
1358 "xtrace" : "Xtrace";0;0,\
1359 "xtrans" : "Xtrans";0;0,\
1360 "xupdate" : "Xupdate";0;0,\
1361 "xwc" : "Xwc";1;0,\
1362 "xwho" : "Xwho";0;1,\
1363 ])
1364
1365static string PrepareLine(string str)
1366{
1367 return str;
1368}
1369
1370static string QuoteLine(string str)
1371{
1372 string *tmp,*tmp2;
1373 int i, i2, len, len2, qd, qs;
1374
Zesstra4daab9a2019-11-06 22:38:18 +01001375 str=string_replace(str,"\\\\","\u00B0""BSHL");
1376 str=string_replace(str,"\\\"","\u00B0""DBLQ");
1377 str=string_replace(str,"\\\'","\u00B0""SGLQ");
1378 str=string_replace(str,"\\|","\u00B0""PIPE");
1379 str=string_replace(str,"||","\u00B0""OROR");
1380 str=string_replace(str,"->","\u00B0""LARR");
1381 str=string_replace(str,"\\$","\u00B0""DOLR");
MG Mud User88f12472016-06-24 23:31:02 +02001382 tmp=regexplode(str,"(\"|')");
1383 len=sizeof(tmp);
1384 qd=qs=0;
1385 for(i=0;i<len;i++)
1386 {
1387 if(i%2)
1388 {
1389 if(tmp[i]=="'")
1390 qd=(!qs?!qd:qd);
1391 else
1392 qs=(!qd?!qs:qs);
1393 if((tmp[i]=="\""&&!qd)||(tmp[i]=="'"&&!qs))
1394 tmp[i]="";
1395 }
1396 else
1397 {
1398 if(!qd)
1399 {
1400 len2=sizeof(tmp2=regexplode(tmp[i],"\\$[^ ][^ ]*"));
1401 for(i2=0;i2<len2;i2++)
1402 if(i2%2)
1403 {
1404 TK("QuoteLine: "+tmp2[i2][1..]);
1405 tmp2[i2]=(string)XFindObj((tmp2[i2])[1..]);
1406 }
1407 tmp[i]=implode(tmp2,"");
1408 }
1409 }
1410 }
1411 if(qd||qs)
1412 return NULL;
1413 str=implode(tmp,"");
1414 TK("QuoteLine: str: "+str);
1415 return str;
1416}
1417
1418static string UnquoteLine(string str)
1419{
Zesstra4daab9a2019-11-06 22:38:18 +01001420 str=string_replace(str,"\u00B0""BSHL","\\");
1421 str=string_replace(str,"\u00B0""DBLQ","\"");
1422 str=string_replace(str,"\u00B0""SGLQ","\'");
1423 str=string_replace(str,"\u00B0""DQUO","");
1424 str=string_replace(str,"\u00B0""SQUO","");
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 TK("UnquoteLine: str: "+str);
1430 return str;
1431}
1432
1433static string *ExplodeCmds(string str)
1434{
1435 string *tmp;
1436
1437 tmp=regexplode(str,"\\||>|>>");
1438 while(tmp[<1]=="")
1439 tmp=tmp[0..<2];
1440 return tmp;
1441}
1442
1443varargs int ParseLine(string str)
1444{
1445 string verb, arg;
1446 int ret;
1447
1448 TK("ParseLine: str: "+(str?str:""));
1449 if(!sizeof(cmds))
1450 {
1451 // this is a single command or beginning of a command pipe
1452 verb=query_verb();
1453
1454 // return on unknown commands
1455 if(!verb||!sizeof(verb)||!GetFunc(verb,TRUE))
1456 return FALSE;
1457
1458 str=(string)this_player()->_unparsed_args();
1459 pipe_in=FALSE;
1460 pipe_of=NULL;
1461 pipe_ovr=TRUE;
1462 pipe_out=FALSE;
1463 pipe_of=NULL;
1464 // pass arguments to some special functions unparsed
1465 if(member(({"xlpc","xcall","xeval"}),verb)>=0)
1466 {
1467#ifdef XDBG
1468 TK("ParseLine: special func: "+verb);
1469#endif
1470 ret=CallFunc(verb,str);
1471 SafeReturn(ret);
1472 }
1473 // ok, here we go
1474 pipe_in=pipe_out=FALSE;
1475 pipe_if=pipe_of=NULL;
1476 pipe_ovr=TRUE;
1477 if(file_size(PIPE_FILE)>=0)
1478 rm(PIPE_FILE);
1479 if (str&&str!="")
1480 {
1481 if(!(str=QuoteLine(str)))
1482 {
1483 WDLN("Unterminated quotation");
1484 SafeReturn(TRUE);
1485 }
1486 cmds=ExplodeCmds(str);
1487 }
1488 else
1489 cmds=({""});
1490 arg=strip_string(cmds[0]);
1491 }
1492 else
1493 {
1494 cmds[0]=strip_string(cmds[0]);
1495 TK("ParseLine: cmds[0]: "+cmds[0]);
1496 if(sscanf(cmds[0],"%s %s",verb,arg)!=2)
1497 {
1498 verb=cmds[0];
1499 arg="";
1500 }
1501 }
1502 cmds=cmds[1..];
1503 TK("ParseLine: verb: "+verb);
1504 if (!verb||!sizeof(verb)||!GetFunc(verb,TRUE))
1505 SafeReturn(FALSE);
1506 TK("ParseLine(1): arg: "+arg+" cmds: "+sprintf("%O",cmds));
1507 switch(sizeof(cmds))
1508 {
1509 case 0:
1510 ret=CallFunc(verb,strip_string(UnquoteLine(arg)));
1511 SafeReturn(ret);
1512 break;
1513
1514 case 1:
1515 WDLN("Missing rhs of command pipe");
1516 SafeReturn(TRUE);
1517 break;
1518
1519 default:
1520 pipe_out=TRUE;
1521 switch(cmds[0])
1522 {
1523 case "|":
1524 pipe_of=PIPE_FILE;
1525 pipe_ovr=TRUE;
1526 cmds=cmds[1..];
1527 break;
1528
1529 case ">":
1530 pipe_ovr=TRUE;
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 case ">>":
1541 pipe_ovr=FALSE;
1542 if(sizeof(cmds)!=2)
1543 {
1544 WDLN("Illegal IO redirection");
1545 SafeReturn(TRUE);
1546 }
1547 pipe_of=cmds[1];
1548 cmds=({});
1549 break;
1550 }
1551 }
1552 TK("ParseLine(2): arg: "+arg+" cmds: "+sprintf("%O",cmds));
1553 if(!CallFunc(verb,strip_string(arg)))
1554 SafeReturn(FALSE);
1555 pipe_in=pipe_out;
1556 pipe_if=pipe_of;
1557 pipe_ovr=FALSE;
1558 pipe_out=FALSE;
1559 pipe_of=NULL;
1560 if(sizeof(cmds))
1561 call_out("ParseLine",0);
1562 else
1563 SafeReturn(TRUE);
1564 return TRUE;
1565}
1566
1567static int CallFunc(string verb, string str)
1568{
1569 string fun;
1570
1571 fun=GetFunc(verb,FALSE);
1572#ifdef XDBG
1573 TK("CallFunc: verb: "+verb+" str: "+str);
1574 TK("CallFunc: resolved function: "+(fun?fun:"(unresolved)"));
1575#endif
1576 if(str=="")
1577 str=NULL;
1578 return fun?(int)call_other(ME,fun,str):FALSE;
1579}
1580
1581static string GetFunc(string verb, int test)
1582{
1583 string fun,*keys,key;
1584 int i,len;
1585
1586 TK("GetFunc: verb: "+verb);
1587
1588 if(verb[0..0]!="x") // Assume all commands start with "x"
1589 return 0;
1590
1591 if (!(fun=(string)ACTIONS[verb,0])) { // Try exact hit first
1592 key="";
1593 len=sizeof(verb);
1594 for (i=sizeof(keys=m_indices(ACTIONS))-1;i>=0;i--) {
1595 TK(" trying: "+keys[i]);
1596 if(sizeof(keys[i])>=len&&keys[i][0..len-1]==verb) {
1597 if(sizeof(key)) {
1598 WLN("Das ist nicht eindeutig ...");
1599 return 0;
1600 }
1601 fun=ACTIONS[keys[i],0];
1602 key=keys[i];
1603 //break;
1604 }
1605 }
1606 } else
1607 key=verb;
1608
1609 if(test)
1610 return fun;
1611
1612 if (key) {
1613#ifdef XDBG
1614 TK("GetFunc: fun: "+fun+" (key: "+key+")\n"+
1615 "pipe_in: "+(pipe_in?"TRUE ":"FALSE ")+(pipe_if?pipe_if:"(NULL)")+"\n"+
1616 "pipe_out: "+(pipe_out?"TRUE ":"FALSE ")+(pipe_of?pipe_of:"(NULL)")+"\n"+
1617 "pipe_ovr: "+(pipe_ovr?"TRUE":"FALSE"));
1618#endif
1619 if (pipe_in&&!ACTIONS[key,PIPE_IN])
1620 {
1621 // this command does not read pipes
1622#ifdef XDBG
1623 TK("Illegal rhs of command pipe \""+fun+"\"\n");
1624#endif
1625 notify_fail("Illegal rhs of command pipe \""+fun+"\"\n");
1626 return 0;
1627 }
1628 else if (pipe_out&&!ACTIONS[key,PIPE_OUT])
1629 {
1630 // this command does not feed pipes
1631#ifdef XDBG
1632 TK("Illegal lhs of command pipe \""+fun+"\"\n");
1633#endif
1634 notify_fail("Illegal lhs of command pipe \""+fun+"\"\n");
1635 return 0;
1636 }
1637 }
1638 return fun;
1639}
1640
1641void actions()
1642{
1643 if (!cloner||!RTP||cloner==RTP||query_wiz_level(cloner)<=query_wiz_level(RTP))
1644 add_action("ParseLine","",1);
1645 add_action("CommandScan", "", 1);
1646}
1647
1648/*----------------------------------------------------------------------
1649 * the checking stuff
1650 */
1651
Zesstra7e95e3f2019-10-19 11:15:05 +02001652public <int|object>* insert_hook(object pl, int hookid, object ob)
MG Mud User88f12472016-06-24 23:31:02 +02001653{
Zesstra7e95e3f2019-10-19 11:15:05 +02001654 if(cloner && cloner == pl && hookid == H_HOOK_INSERT)
1655 {
1656 if(MODE(MODE_FIRST) && find_call_out("move")==-1)
1657 call_out("move",0,cloner);
1658 if(MODE(MODE_INVCHECK))
1659 write_newinvobj(ob);
1660 }
1661 return ({H_NO_MOD, ob});
1662}
1663
1664void add_insert_hook()
1665{
1666 if(objectp(cloner))
1667 cloner->HRegisterToHook(H_HOOK_INSERT, #'insert_hook,
1668 H_HOOK_LIBPRIO(2), H_LISTENER, 0);
MG Mud User88f12472016-06-24 23:31:02 +02001669}
1670
1671static void VarCheck(int show)
1672{
1673 int i, s;
1674 foreach(string k, mixed v : variable)
1675 {
1676 if (v) continue;
1677 if(show) WDLN("*** Variable $"+k+" has been destructed");
1678 m_delete(variable, k);
1679 }
1680}
1681
1682
1683int write_newinvobj(object obj)
1684{
1685 if(obj) WDLN("*** New object in inventory "+ObjFile(obj));
1686 return(1);
1687}
1688
1689/*----------------------------------------------------------------------
1690 * catch all commands, absorb forces and check history
1691 */
1692
1693int CommandScan(string arg)
1694{
1695 string verb, cmd;
1696 object rtp;
1697 rtp=RTP;
1698
1699 if(!cloner&&!(cloner=rtp)) destruct(ME);
1700
1701 if((!MODE(MODE_PROTECT))||security()||
1702 query_wiz_level(cloner)<query_wiz_level(rtp))
1703 {
1704 verb=query_verb();
1705 if(verb&&DoHistory(verb+(arg ? " "+arg : "")))
1706 return TRUE;
1707 nostore=FALSE;
1708 return FALSE;
1709 }
1710 else
1711 {
1712 if(rtp)
1713 {
1714 WDLN("Your "+TOOL_TITLE+" protects you from a force by "+crname(rtp)+
1715 " ["+query_verb()+(arg ? " "+arg+"]" : "]"));
1716 tell_object(rtp, crname(cloner)+"'s "+TOOL_TITLE+
1717 " absorbes your force.\n");
1718 }
1719 else
1720 {
1721 WDLN("Your "+TOOL_TITLE+" protects you from a force ["+
1722 query_verb()+(arg ? " "+arg+"]" : "]"));
1723 }
1724 return TRUE;
1725 }
1726}
1727
1728int DoHistory(string line)
1729{
1730 int i;
1731 string cmd, *strs;
1732
1733 SECURE2(FALSE);
1734 if(!stringp(line) || !sizeof(line))
1735 return TRUE;
1736 else if(line=="%!")
1737 {
1738 WLN("Current command history:");
1739 for(i=MAX_HISTORY; i; --i)
1740 if(history[i-1])
1741 {
1742 W(" "+ARIGHT(""+i, 2, " ")+": ");
1743 if(sizeof(history[i-1])>70)
1744 WLN(ALEFT(history[i-1], 70, " "));
1745 else
1746 WLN(history[i-1]);
1747 }
1748 return TRUE;
1749 }
1750 else if(line[0..1]=="%%" && (cmd=history[0]+line[2..<1]))
1751 {
1752 Command(cmd);
1753 return TRUE;
1754 }
1755 else if(line[0]=='^'&&(strs=strip_explode(line, "^")))
1756 {
1757 if(sizeof(strs)&&strs[0]&&(cmd=history[0]))
1758 {
1759 if(sizeof(strs)==2)
1760 cmd=string_replace(cmd, strs[0], strs[1]);
1761 else
1762 cmd=string_replace(cmd, strs[0], "");
1763 nostore--;
1764 Command(cmd);
1765 nostore++;
1766 return TRUE;
1767 }
1768 }
1769 else if(line[0]=='%' && (sscanf(line[1..<1], "%d", i)))
1770 {
1771 i= i>0 ? i : 1;
1772 i= i<=MAX_HISTORY ? i : MAX_HISTORY;
1773 if(cmd=history[i-1])
1774 Command(cmd);
1775 return TRUE;
1776 }
1777 else if(line[0]=='%')
1778 {
1779 for(i=0; i<MAX_HISTORY; i++)
1780 {
1781 if(history[i]&&
1782 history[i][0..sizeof(line)-2]==line[1.. <1])
1783 {
1784 Command(history[i]);
1785 return TRUE;
1786 }
1787 }
1788 }
1789 else if(nostore<1)
1790 history=({line})+history[0..MAX_HISTORY-2];
1791 return FALSE;
1792}
1793