blob: 9f8512a26ec4977a830110df8dfadb086af882d1 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// channel.c -- channel client
4//
5// $Id: channel.c 9404 2015-12-13 00:21:44Z Zesstra $
6#pragma strong_types
7#pragma save_types
8#pragma range_check
9#pragma no_clone
10#pragma pedantic
11
12#define NEED_PROTOTYPES
13#include <util.h>
14#include <thing/properties.h>
15#include <living/comm.h>
16#include <player.h>
17#include <player/comm.h>
18#include <daemon.h>
19#include <player/gmcp.h>
20#undef NEED_PROTOTYPES
21
22#include <wizlevels.h>
23#include <defines.h>
24#include <properties.h>
25#include <sys_debug.h>
26#include <regexp.h>
27
28#define P_SWAP_CHANNELS "swap_channels"
29#define P_CHANNEL_SHORT "short_channels"
30
31#define CHANNELCMDS "[#@%$&()<>a-zA-Z0-9\\-]"
32
33#define DEFAULT_CHANNELS ({"Abenteuer", "Anfaenger","Grats","Tod", "ZT"})
34#define DEFAULT_SHORTCUTS \
35([ \
36 "b":"Abenteuer", \
37 "a":"Allgemein", \
38 "B":"Beileid", \
39 "q":"D-chat", \
40 "G":"Grats", \
41 "M":"Moerder", \
42 "h":"Seher", \
43 "T":"Tod", \
44])
45
46#define WIZARD_SHORTCUTS \
47([ \
48 "P":"D-code", \
49 "D":"Debug", \
50 "O":"Intercode", \
51 "I":"Intermud", \
52 "m":"Magier", \
53])
54
55
56private nosave mapping shortcut;
57private nosave int c_status;
58
59void create()
60{
61 Set(P_CHANNELS, SAVE, F_MODE);
62 Set(P_CHANNELS, DEFAULT_CHANNELS);
63 Set(P_SWAP_CHANNELS, SAVE, F_MODE);
64 Set(P_STD_CHANNEL, "Allgemein");
65 Set(P_STD_CHANNEL, SAVE, F_MODE);
66 Set(P_CHANNEL_SHORT, SAVE, F_MODE);
67 Set(P_CHANNEL_SHORT, DEFAULT_SHORTCUTS
68 + (IS_LEARNER(this_object()) ? WIZARD_SHORTCUTS : ([])));
69}
70
71static mixed _query_localcmds()
72{
73 return ({({"-","ChannelParser", 1, 0}),
74 ({"ebene", "ChannelAdmin", 0, 0}),
75 ({"ebenen", "ChannelAdmin", 1, 0}),
76 });
77}
78
79mixed RegisterChannels()
80{
81 mixed err;
82 if(extern_call() &&
83 previous_object() != find_object(CHMASTER)) return;
84 c_status = 0;
85 shortcut = QueryProp(P_CHANNEL_SHORT);
86 SetProp(P_CHANNELS, map(QueryProp(P_CHANNELS) || ({}),
87 #'lower_case));
88 err = filter(QueryProp(P_CHANNELS),
89 symbol_function("join", CHMASTER),
90 this_object());
91 if(QueryProp(P_LEVEL) < 5) return err;
92 while(sizeof(err)) {
93 CHMASTER->new(err[0], this_object());
94 err[0..0] = ({});
95 }
96 return err;
97}
98
99mixed RemoveChannels()
100{
101 closure cl;
102 mixed err=({});
103 if(extern_call() &&
104 previous_object() != find_object(CHMASTER)) return;
105 if(!c_status) c_status = 1;
106 else return ({});
107 cl=symbol_function("leave", CHMASTER);
108 if (closurep(cl)) {
109 err = filter(QueryProp(P_CHANNELS), cl, this_object());
110 SetProp(P_CHANNELS, QueryProp(P_CHANNELS) - err);
111 }
112 return err;
113}
114
115varargs private string getName(mixed x, int fall) {
116
117 mixed o = closurep(x) ? query_closure_object(x) : x;
118 if(stringp(o) && sizeof(o) && (x = find_object(o)))
119 o = x;
120
121 // Objekte
122 if (objectp(o)) {
123 // Magier sehen unsichtbare nicht nur als "Jemand"
124 if (o->QueryProp(P_INVIS) && IS_LEARNING(this_object()))
125 return "("+capitalize(getuid(o))+")";
126 // Froesche mit Namen versorgen.
127 if (o->QueryProp(P_FROG))
128 return "Frosch "+capitalize(getuid(o));
129 // Default (Unsichtbare als "Jemand" (s. Name()))
130 return o->Name(fall, 2)||"<Unbekannt>";
131 }
132 // Strings
133 else if (stringp(o) && sizeof(o)) {
134 if (o[0] == '/') {
135 // unsichtbare Objekte...
136 int p = strstr(o, "$");
137 if (p != -1) {
138 // Magier im Magiermodus kriegen den Realnamen, andere nicht.
139 if (IS_LEARNING(this_object()))
140 return o[1..p-1];
141 else
142 return o[p+1..];
143 }
144 else
145 // doch nicht unsichtbar
146 return (fall == WESSEN ? o+"s" : o);
147 }
148 else
149 // nicht unsichtbar
150 return (fall == WESSEN ? o+"s" : o);
151 }
152 // Fall-through
153 return "<Unbekannt>";
154}
155
156// <nonint> unterdrueckt die AUsgabe an den Spieler und liefert den Text
157// zurueck. Wird nur fuer die Ebenenhistory benutzt.
158string ChannelMessage(mixed* msg, int nonint)
159{
160 string channel_message;
161 string channel=msg[0];
162 object sender=msg[1];
163 string message=msg[2];
164 int msg_type = msg[3];
165
166 if ( previous_object() != find_object(CHMASTER) &&
167 previous_object() != ME )
168 return 0;
169
170 string sender_name = getName(sender, msg_type == MSG_GEMOTE ? WESSEN : WER);
171 int prepend_indent_flag=QueryProp(P_MESSAGE_PREPEND) ? BS_PREPEND_INDENT : 0;
172 switch(msg_type) {
173 case MSG_EMPTY:
174 channel_message= message+"\n";
175 break;
176 case MSG_GEMOTE:
177 case MSG_EMOTE:
178 channel_message = break_string(sender_name + " "+ message+"]",
179 78, sprintf("[%s:", channel),
180 BS_INDENT_ONCE|prepend_indent_flag);
181 break;
182 case MSG_SAY:
183 default:
184 string presay=sprintf("[%s:%s] ", channel, sender_name);
185 channel_message = break_string(message, max(78,sizeof(presay)+10),
186 presay, prepend_indent_flag);
187 break;
188 }
189 if(nonint)
190 return channel_message;
191
192 // Wenn GMCP sich um Uebertragung der Nachricht kuemmert, wird ReceiveMsg()
193 // nicht mehr aufgerufen.
194 if (GMCP_Channel(channel_message, channel, sender_name) != 1)
195 {
196 // Der Ebenenname muss in Kleinbuchstaben uebergeben werden, damit die
197 // Ignorierepruefung funktioniert. Die ignorierestrings sind naemlich alle
198 // kleingeschrieben.
199 ReceiveMsg(channel_message,
200 MT_COMM|MT_FAR|MSG_DONT_STORE|MSG_DONT_WRAP,
201 MA_CHANNEL"." + lower_case(channel), 0, sender);
202 }
203 return 0;
204}
205
206private void createList(string n, mixed a, mixed m, mixed l)
207{
208 int pos; string sh, *mem;
209 if((pos = member(map(m_values(shortcut), #'lower_case/*'*/), n)) != -1)
210 sh = m_indices(shortcut)[pos];
211 else sh = "";
212 mem=map(a[I_MEMBER],#'getName/*'*/, WER);
213 mem-=({"<MasteR>"});
214 l += ({ sprintf("%-12.12'.'s %c[%-1.1s] %|12.12' 's (%-|3' 'd) %-42.42s\n",
215 a[I_NAME], (member(m, n) != -1 ? '*' : ' '), sh,
216 a[I_MASTER] ?
217 getName(a[I_MASTER]) : getName(a[I_ACCESS]),
218 sizeof(mem),
219 (closurep(a[I_INFO]) && objectp(query_closure_object(a[I_INFO]))) ?
220 funcall(a[I_INFO]) || "- Keine Beschreibung -" :
221 (stringp(a[I_INFO]) ? a[I_INFO] : "- Keine Beschreibung -")
222 ) });
223}
224
225private mixed getChannel(string ch)
226{
227 mixed ff;
228 if(!sizeof(ch)) ch = QueryProp(P_STD_CHANNEL);
229 if(shortcut && shortcut[ch]) ch = shortcut[ch];
230 return CHMASTER->find(ch, this_object());
231}
232
233#ifndef DEBUG
234#define DEBUG(x) if (funcall(symbol_function('find_player),"zesstra"))\
235 tell_object(funcall(symbol_function('find_player),"zesstra"),\
236 "MDBG: "+x+"\n")
237#endif
238int ChannelParser(string args)
239{
240 mixed ch, cmd, tmp;
241 int pos, type, err;
242 string txt;
243 cmd = query_verb();
244 args = _unparsed_args();
245 notify_fail("Benutzung: -<Ebene>[ ]['|:|;]<Text>\n"
246 " -<Ebene>[+|-|?|!|*]\n"
247 " -?\n");
248 if(!cmd && !args) return 0;
249 if(!args) args = "";
250 cmd = cmd[1..];
251 if(sizeof(cmd = regexplode(cmd,
252 "^" CHANNELCMDS "*"
253 "([+-]|\\!|\\?|\\*)*")) > 1)
254 {
255 //z.B. cmd= ({"","allgemein",":testet"})
256 if(sizeof(cmd[1]) > 1 &&
257 strstr("+-?!*", cmd[1][<1..<1]) > -1)
258 tmp = cmd[1][0..<2];
259 else
260 tmp = cmd[1];
261 if(cmd[1] != "?" && cmd[1] != "!")
262 if(pointerp(ch = getChannel(tmp)))
263 {
264 notify_fail("Diese Angabe war nicht eindeutig! "
265 "Folgende Ebenen passen:\n"
266 +implode(ch, ", ")+"\n");
267 return 0;
268 }
269 else if(!ch) return (notify_fail("Die Ebene '"+tmp
270 +"' gibt es nicht!\n"), 0);
271 //DEBUG(sprintf("ChanCmd: %O\n",cmd));
272 if (sizeof(cmd[1])) {
273 switch(cmd[1][<1]) {
274 case '+':
275 switch(CHMASTER->join(ch, this_object()))
276 {
277 case E_ACCESS_DENIED:
278 notify_fail("Du darfst an die Ebene '"+ch+"' nicht heran.\n");
279 return 0;
280 case E_ALREADY_JOINED:
281 notify_fail("Du hast diese Ebene schon betreten!\n");
282 return 0;
283 default: break;
284 }
285 write("Du betrittst die Ebene '"+ch+"'.\n");
286 if(member(QueryProp(P_CHANNELS), ch = lower_case(ch)) == -1)
287 SetProp(P_CHANNELS, QueryProp(P_CHANNELS) + ({ ch }));
288 return 1;
289 case '-':
290 switch(CHMASTER->leave(ch, this_object()))
291 {
292 case E_ACCESS_DENIED:
293 write("Du kannst die Ebene '"+ch+"' nicht verlassen.\n");
294 break;
295 case E_NOT_MEMBER:
296 write("Wie willst Du eine Ebene verlassen, welche Du nicht "
297 "betreten hast?\n");
298 break;
299 default:
300 write("Du verlaesst die Ebene '"+ch+"'.\n");
301 SetProp(P_CHANNELS, QueryProp(P_CHANNELS) - ({ lower_case(ch), ch }));
302 break;
303 }
304 return 1;
305 case '!':
306 case '?':
307 {
308 mapping l;
309 if(mappingp(l = CHMASTER->list(this_object())))
310 if(stringp(ch) && sizeof(ch) && pointerp(l[ch = lower_case(ch)]))
311 {
312 int c; object o; string n; string *m;
313 m=sort_array(map(l[ch][I_MEMBER],#'getName/*'*/, WER),#'>/*'*/);
314 m-=({"<MasteR>"});
315 write(l[ch][I_NAME]+", "+funcall(l[ch][I_INFO])+".\n");
316 write("Du siehst "+((c = sizeof(m)) > 0
317 ? (c == 1 ? "ein Gesicht" : c+" Gesichter")
318 : "niemanden")+" auf der Ebene '"
319 +l[ch][I_NAME]+"':\n");
320 write(break_string(implode(m,", "), 78));
321 write((l[ch][I_MASTER] ?
322 getName(l[ch][I_MASTER]) : getName(l[ch][I_ACCESS], WER))
323 +" hat das Sagen auf dieser Ebene.\n");
324 }
325 else
326 {
327 mixed list; list = ({});
328 if(cmd[1][<1] == '!')
329 l -= mkmapping(m_indices(l) - QueryProp(P_CHANNELS));
330 walk_mapping(l, #'createList/*'*/, QueryProp(P_CHANNELS), &list);
331 list = sort_array(list, #'>/*'*/);
332 txt = sprintf("%-12.12' 's [A] %|12' 's (%-3' 's) %-42.42s\n",
333 "Name", "Eigner", "Sp", "Beschreibung")
334 + "-------------------------------------------------------"
335 + "-----------------------\n"
336 + implode(list, "");
337 More(txt);
338 }
339 return 1;
340 }
341 case '*':
342 {
343 mixed hist; int amount;
344 if(!pointerp(hist = CHMASTER->history(ch, this_object()))
345 || !sizeof(hist))
346 {
347 write("Es ist keine Geschichte fuer '"+ch+"' verfuegbar.\n");
348 return 1;
349 }
350
351 //(Zesstra) cmd hat offenbar immer 3 Elemente...
352 //bei -all* ({"","all*",""})
353 //bei -all*10 ({"","all*,"10"})
354 //also ist bei -all* amount immer == 0 und es funktioniert eher zufaellig.
355 /*if(sizeof(cmd) > 2)
356 amount = to_int(cmd[2]);
357 else
358 amount=sizeof(hist);*/
359 amount=to_int(cmd[2]);
360 if (amount <= 0 || amount >= sizeof(hist))
361 amount=sizeof(hist);
362
363 txt = "Folgendes ist auf '"+ch+"' passiert:\n"
364 + implode(map(hist[<amount..], #'ChannelMessage/*'*/, 1), "");
365 More(txt);
366 return 1;
367 }
368 default:
369 break;
370 }
371 }
372 }
373 if(sizeof(cmd = implode(cmd[2..], "")))
374 args = cmd + (sizeof(args) ? " " : "") + args;
375
376 // KOntrollchars ausfiltern.
377 args = regreplace(args,"[[:cntrl:]]","",RE_PCRE|RE_GLOBAL);
378 if(!sizeof(args)) return 0;
379
380 //Wenn cmd leer ist: MSG_SAY
381 if (!sizeof(cmd)) type=MSG_SAY;
382 else {
383 switch(cmd[0])
384 {
385 case ':' :
386 type = MSG_EMOTE;
387 args = args[1..];
388 break;
389 case ';' :
390 type = MSG_GEMOTE;
391 args = args[1..];
392 break;
393 case '\'':
394 args = args[1..];
395 default : type = MSG_SAY; break;
396 }
397 }
398 if(!ch || !sizeof(ch)) ch = QueryProp(P_STD_CHANNEL);
399 if((err = CHMASTER->send(ch, this_object(), args, type)) < 0)
400 if(!(err = CHMASTER->join(ch, this_object())))
401 {
402 if(member(QueryProp(P_CHANNELS), ch = lower_case(ch)) == -1)
403 SetProp(P_CHANNELS, QueryProp(P_CHANNELS) + ({ ch }));
404 err = CHMASTER->send(ch, this_object(), args, type);
405 }
406
407 switch(err)
408 {
409 case E_ACCESS_DENIED:
410 notify_fail("Auf der Ebene '"+ch+"' darfst Du nichts sagen.\n");
411 return 0;
412 case E_NOT_MEMBER:
413 notify_fail("Du hast die Ebene '"+ch+"' nicht betreten!\n");
414 return 0;
415 }
416 return 1;
417}
418
419int ChannelAdmin(string args)
420{
421 string n, descr, sh, cn;
422 mixed pa, tmp;
423 args = _unparsed_args();
424 notify_fail("Benutzung: ebene <Abkuerzung>=<Ebene>\n"
425 " ebene <Abkuerzung>=\n"
426 " ebene abkuerzungen [standard]\n"
427 " ebene standard <Ebene>\n"
428 " ebene an|ein|aus\n"
429 +(QueryProp(P_LEVEL) >= 5 ?
430 " ebene neu <Name> <Bezeichnung>\n"
431 " ebene beschreibung <Name> <Beschreibung>\n" : "")
432 +(IS_ARCH(this_object()) ?
433 " ebene kill <Name>\n"
434 " ebene clear <Name>\n": ""));
435 if(!args || !sizeof(args)) return 0;
436 if(sscanf(args, "kill %s", n) && IS_ARCH(this_object()))
437 {
438 if(!(cn = CHMASTER->find(n, this_object()))) cn = n;
439 switch(CHMASTER->remove(cn, this_object()))
440 {
441 case E_ACCESS_DENIED:
442 notify_fail("Die Ebene '"+cn+"' lies sich nicht entfernen!\n");
443 return 0;
444 }
445 write("Du entfernst die Ebene '"+cn+"'.\n");
446 return 1;
447 }
448 if(sscanf(args, "clear %s", n) && IS_ARCH(this_object()))
449 {
450 if(!(cn = CHMASTER->find(n, this_object()))) cn = n;
451 switch(CHMASTER->clear_history(cn, this_object()))
452 {
453 case E_ACCESS_DENIED:
454 notify_fail("Der Verlauf zur Ebene '"+cn+"' lies sich nicht entfernen!\n");
455 return 0;
456 }
457 write("Du entfernst den Verlauf zur Ebene '"+cn+"'.\n");
458 return 1;
459 }
460 if(sscanf(args, "neu %s %s", n, descr) == 2)
461 {
462 mixed x;
463 if(QueryProp(P_LEVEL) < 5)
464 return (notify_fail("Neue Ebenen zu erstellen ist dir verwehrt.\n"), 0);
465 if(!sizeof(regexp(({ n }), "^" CHANNELCMDS CHANNELCMDS "*")))
466 return (notify_fail("Der Name '"+n+"' ist nicht konform!\n"), 0);
467 if (sizeof(n) > 20 )
468 return(notify_fail("Der Name '"+n+"' ist zu lang.\n"), 0);
469 switch(x = CHMASTER->new(n, this_object(), descr))
470 {
471 case E_ACCESS_DENIED:
472 notify_fail("Diese Ebene darfst du nicht erschaffen!\n"); break;
473 default:
474 write("Du erschaffst die Ebene '"+n+"'.\n");
475 SetProp(P_CHANNELS, QueryProp(P_CHANNELS) + ({ lower_case(n) }));
476 return 1;
477 }
478 }
479 if(sscanf(args, "beschreibung %s %s", n, descr) == 2)
480 {
481 mixed ch;
482 cn = CHMASTER->find(n, this_object());
483 if(!cn || pointerp(cn))
484 return (notify_fail("Die Ebene '"+n+"' existiert nicht oder die Angabe "
485 "war nicht eindeutig.\n"), 0);
486 ch = CHMASTER->list(this_object());
487 if(ch[lower_case(cn)][I_MASTER] != this_object())
488 return (notify_fail("Du bist nicht berechtigt die Beschreibung der Ebene"
489 " '"+cn+"' zu aendern.\n"), 0);
490 ch[lower_case(cn)][I_INFO] = descr;
491 write("Die Ebene '"+cn+"' hat ab sofort die Beschreibung:\n"+descr+"\n");
492 return 1;
493 }
494 if(sscanf(args, "%s=%s", sh, n) == 2 && sizeof(n))
495 {
496 mapping sc;
497 if(pointerp(tmp = CHMASTER->find(n, this_object())) || !tmp)
498 return (notify_fail("Benutzung: ebene <Abkuerzung>=<Ebene>\n"
499 +(pointerp(tmp) ? implode(tmp, ", ") + "\n" :
500 "Ebene '"+n+"' nicht gefunden!\n")), 0);
501 sc = QueryProp(P_CHANNEL_SHORT);
502 if(!sc) sc = ([]);
503 sc[sh] = tmp;
504 SetProp(P_CHANNEL_SHORT, sc);
505 shortcut = QueryProp(P_CHANNEL_SHORT);
506 write("'"+sh+"' wird jetzt als Abkuerzung fuer '"+tmp+"' anerkannt.\n");
507 return 1;
508 }
509 if(sscanf(args, "%s=", sh))
510 {
511 SetProp(P_CHANNEL_SHORT, m_copy_delete(QueryProp(P_CHANNEL_SHORT) || ([]), sh));
512 shortcut = QueryProp(P_CHANNEL_SHORT);
513 write("Du loeschst die Abkuerzung '"+sh+"'.\n");
514 return 1;
515 }
516 if(args == "an" || args == "ein")
517 {
518 mixed excl;
519 if(pointerp(QueryProp(P_SWAP_CHANNELS)))
520 SetProp(P_CHANNELS, QueryProp(P_SWAP_CHANNELS));
521 else
522 SetProp(P_CHANNELS, m_indices(CHMASTER->list(this_object())));
523 excl = RegisterChannels();
524 write("Du schaltest folgende Ebenen ein:\n"
525 +break_string(implode(QueryProp(P_CHANNELS) - excl, ", "), 78));
526 SetProp(P_SWAP_CHANNELS, 0);
527 return 1;
528 }
529 if(args == "aus")
530 {
531 SetProp(P_SWAP_CHANNELS, QueryProp(P_CHANNELS));
532 RemoveChannels();
533 SetProp(P_CHANNELS, ({}));
534 write("Du stellst die Ebenen ab.\n");
535 return 1;
536 }
537 pa = old_explode(args, " ");
538 if(!strstr("abkuerzungen", pa[0]))
539 {
540 string txt; txt = "";
541 if(sizeof(pa) > 1 && !strstr("standard", pa[1]))
542 {
543 write("Die Standard Abkuerzungen werden gesetzt.\n");
544 SetProp(P_CHANNEL_SHORT, DEFAULT_SHORTCUTS
545 + (IS_LEARNER(this_object()) ? WIZARD_SHORTCUTS : ([])));
546 }
547 walk_mapping(QueryProp(P_CHANNEL_SHORT),
548 lambda(({'i/*'*/, 'c, 'r}),
549 ({#'+=, 'r/*'*/,
550 ({#'sprintf/*'*/, "%5.5s = %s\n", 'i, 'c})})),
551 &txt);
552 txt = sprintf("Folgende Abkuerzungen sind definiert:\n%-78#s\n",
553 implode(sort_array(old_explode(txt, "\n"), #'>/*'*/), "\n"));
554 More(txt);
555 return 1;
556 }
557 if(!strstr("standard", pa[0]))
558 if(sizeof(pa) < 2)
559 return (notify_fail("Benutzung: ebene standard <Ebene>\n"
560 +(QueryProp(P_STD_CHANNEL)
561 ? "Momentan ist '"+QueryProp(P_STD_CHANNEL)
562 +"' eingestellt.\n"
563 : "Es ist keine Standardebene eingestellt.\n")),0);
564 else
565 if(pointerp(tmp = CHMASTER->find(pa[1], this_object())))
566 return (notify_fail("Das war keine eindeutige Angabe! "
567 "Folgende Ebenen passen:\n"
568 +break_string(implode(tmp, ", "), 78)), 0);
569 else
570 if(!tmp) return (notify_fail("Ebene '"+pa[1]+"' nicht gefunden!\n"),0);
571 else
572 {
573 write("'"+tmp+"' ist jetzt die Standardebene.\n");
574 SetProp(P_STD_CHANNEL, tmp);
575 return 1;
576 }
577 return(0);
578}
579