1
/* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: telnet.pike,v 1.1.1.1 2006/03/27 12:40:15 exodusd Exp $
20
constant cvs_version="$Id: telnet.pike,v 1.1.1.1 2006/03/27 12:40:15 exodusd Exp $";
22
inherit "/net/coal/login";
23
inherit "/net/base/readline";
24
inherit "/net/base/cmd";
31
#include <attributes.h>
34
#define MODE_CONNECT 0
38
#define MODE_MORE_INPUT 4
41
mapping mMode= ([ "cmd":0 ]);
42
static int passwdtries;
43
static object tmpUser;
45
static mapping(string:string) mCreate = ([]);
54
* creates an new user and activates him. adduser is called in Mode_Create several times.
56
int cmd_adduser(string cmd, string|void args)
58
if (iMode == MODE_CMD) {
59
iMode = MODE_MORE_INPUT;
61
mCreate["name"] = args;
62
send_message("Weitere Daten werden zum Anlegen des Nutzers \"" + args + "\" ben�tigt.\n");
63
send_message("Passwort des Nutzers:");
67
mCreate["name"] = args;
69
send_message("Passwort des Nutzers:");
74
send_message("E-Mail des Nutzers:");
77
mCreate["email"] = cmd;
78
LOG("now fetching factory\n");
79
object factory = _Server->get_factory(CLASS_USER);
80
LOG("Now executing factory\n");
81
object user = factory->execute(mCreate);
82
LOG("Factory executed");
84
send_message("Ein Fehler ist aufgetreten. Nutzer wurde nicht angelegt\n");
86
LOG("Now activating user.\n");
87
if (user->activate_user(factory->get_activation())) {
88
send_message("Nutzer wurde angelegt und aktiviert.\n");
90
send_message("Nutzer wurde angelegt, aber nicht aktiviert!\n");
101
void addlink(object to)
103
mapping (string:object) mName = ([]);
104
mName["link_to"] = to;
105
object factory = _Server->get_factory(CLASS_LINK);
106
object link = factory->execute(mName);
108
send_message("Ein Fehler ist aufgetreten. Der Link konnte nicht angelegt werden.\n");
110
send_message("Der Link wurde angelegt. Seine ID ist " + link->get_object_id() );
115
void create_object(string type, string name)
119
mapping (string:string) mName = ([]);
120
mName["name"] = name;
121
object factory = _Server->get_factory(type);
124
send_message("Es gibt keine Klasse vom typ '"+type+"'.\n");
127
err = catch{ newobject = factory->execute(mName); };
130
send_message("Ein Fehler ist aufgetreten. Das Objekt konnte nicht angelegt werden:\n"+err[0]+"\n");
131
LOG(sprintf("%O", err));
135
send_message("Das Objekt wurde angelegt. Seine ID ist " + newobject->get_object_id() );
136
newobject->move(oUser->get_environment());
142
* Shows the inventory of the given object
143
* grouped by the object classes
145
* @param object obj - the target object
146
* @return the inventory formatted as string
147
* @author <a href="mailto:joergh@upb.de">Joerg Halbsgut</a>)
149
static string show_inventory(object obj) {
152
// NOT COMPLETE !(see also "classes.h")
153
mapping classes_names = ([ CLASS_USER:CLASS_NAME_USER,
154
CLASS_OBJECT:CLASS_NAME_OBJECT,
155
CLASS_CONTAINER:CLASS_NAME_CONTAINER,
156
CLASS_ROOM:CLASS_NAME_ROOM,
157
CLASS_DOCUMENT:CLASS_NAME_DOCUMENT,
158
CLASS_LINK:CLASS_NAME_LINK,
159
CLASS_GROUP:CLASS_NAME_GROUP,
160
CLASS_EXIT:CLASS_NAME_EXIT,
162
CLASS_MESSAGEBOARD:"Messageboard",
163
CLASS_GHOST:CLASS_NAME_GHOST,
164
CLASS_TRASHBIN:CLASS_NAME_TRASHBIN,
165
/*,CLASS_SHADOW:"Shadow"*/ ]);
170
if (arrayp(obj->get_inventory())) {
171
res = res +"\nThis is the inventory of "
172
+ obj->get_identifier() +":\n";
173
res += "----------------------------------------------------\n";
174
foreach (indices(classes_names), int cl) {
175
array(object) inventory_class =
176
obj->get_inventory_by_class(cl);
179
if (arrayp(inventory_class) && sizeof(inventory_class)>0) {
180
res += " " + classes_names[cl] +":\n";
181
foreach (inventory_class, object inv_obj) {
183
string ident = inv_obj->get_identifier();
184
int id = inv_obj->get_object_id();
187
res += sprintf (" %s[%d]", ident, id);
194
res += "\n No objects in the inventory.\n";
197
res = res +"\""+obj->get_identifier()+"\" has no inventory.\n";
203
static void send_room(object room)
205
if ( objectp(room) ) {
207
"[#"+room->get_object_id() + ","+_FILEPATH->object_to_filename(room)+"]\n"+
208
"You are in a large area called " + room->get_identifier() + ".\n"+
209
"There are the following exits:\n");
210
array(object) inv = room->get_inventory();
211
foreach(inv, object o) {
212
if ( o->get_object_class() & CLASS_EXIT )
213
send_message(o->get_identifier()+",");
215
send_message("\nThere are the following people:\n");
216
array(object) users = room->get_inventory_by_class(CLASS_USER);
217
foreach(users, object u) {
218
send_message(u->get_identifier()+",");
223
send_message("You are in the big black void.\n");
227
static void enter_room(object room, void|object from)
230
oUser->dispose_event(EVENT_SAY|EVENT_LEAVE_INVENTORY|EVENT_ENTER_INVENTORY, from);
232
oUser->listen_to_event(EVENT_SAY, room);
233
oUser->listen_to_event(EVENT_LEAVE_INVENTORY, room);
234
oUser->listen_to_event(EVENT_ENTER_INVENTORY, room);
237
void notify(int event, mixed ... args)
239
LOG("SAY: "+sprintf("%O\n", args));
240
object user = this_user();
241
LOG("oUser="+sprintf("%O", oUser));
242
LOG("user="+sprintf("%O", user));
243
if ( !objectp(oUser) || !objectp(user) )
245
LOG("sending event response !");
249
send_message(user->get_identifier() + " tells you: " + args[2]+"\n");
253
send_message("You say: "+args[2]+"\n");
255
send_message(user->get_identifier() + " says: "+args[2]+"\n");
257
case EVENT_ENTER_INVENTORY:
258
if ( args[1] == oUser ) {
259
send_message("You move to " + args[0]->get_identifier()+"\n");
262
send_message(args[1]->get_identifier() + " enters the room.\n");
263
send_message("%O:%O\n", args[1]->get_status(), args[1]->get_status()& CLIENT_FEATURES_MOVE);
266
case EVENT_LEAVE_INVENTORY:
267
send_message(args[1]->get_identifier() + " leaves the room.\n");
272
int cmd_delete(string cmd, string args)
275
sscanf(args,"#%i %*",id);
277
object oTmp=find_object(id);
280
mixed err = catch { oTmp->delete(); };
281
if (err!=0) send_message("Failed to delete Object #"+id+"\r\n");
282
else send_message("Deleted Object #"+id+"\r\n");
284
else send_message("Unable to find Object #"+id+"\r\n");
288
int cmd_take(string cmd, string args)
291
sscanf(args,"#%i %*",id);
293
object oTmp=find_object(id);
296
mixed err = catch { oTmp->move(oUser); };
297
if (err!=0) send_message("Cannot take object #"+id+"\r\n");
298
else send_message("Object #"+id+" is now in your inventory!\r\n");
300
else send_message("Can't find Object #"+id+"\r\n");
304
int cmd_drop(string cmd, string args)
307
sscanf(args,"#%i %*",id);
309
object oTmp=find_object(id);
313
//check if object is in user's inventory
314
array(object) oaInv = oUser->get_inventory();
315
foreach( oaInv, object item )
317
if ( item->get_object_id() == id )
319
mixed err = catch { oTmp->move(oUser->get_environment()); };
320
if (err!=0) send_message("Cannot drop object #"+id+"\r\n");
321
else send_message("Dropped Object #"+id+"\r\n");
325
// foreach did not find object in user's inventory
326
send_message("Object #"+id+" is not in your inventory!\r\n");
328
else send_message("Can't find Object #"+id+"\r\n");
332
int cmd_quit(string cmd, string args)
335
oUser->set_attribute("telnet_history", readln->readline->historyobj->encode()/"\n");
336
send_message("Bye %s, see you again soon!\n", oUser->get_identifier());
342
int cmd_look(string cmd, string args)
344
//send_room(oUser->get_environment());
345
object oRoom = oUser->get_environment();
346
send_message(show_inventory(oRoom));
350
int cmd_inv(string cmd, string args)
354
if ( sscanf(args, "#%d",oid) == 1)
355
obj = find_object(oid);
358
LOG("Inventory of User");
361
send_message(show_inventory(obj));
365
int cmd_say(string cmd, string args)
367
object env = oUser->get_environment();
372
int cmd_tell(string cmd, string args)
377
if ( sscanf(args, "%s %s", user, msg) != 2 ) {
378
send_message("Usage is tell <user> <message>.\n");
381
target = _Persistence->lookup_user(user);
382
if ( !objectp(target) ){
383
send_message("Failed to find user '"+user+"'.\n");
386
target->message(msg);
387
send_message("You told " + user + ": "+msg+"\n");
391
int cmd_move(string cmd, string args)
393
object env = oUser->get_environment();
398
send_message("Going home now...");
399
exit = oUser->query_attribute(USER_WORKROOM);
401
else if ( sscanf(args, "#%d", id) == 1 )
402
exit = find_object(id);
403
else if(sizeof(args))
404
exit = env->get_object_byname(args);
406
send_message(cmd + " where?\n");
410
mixed err = catch { oUser->move(exit); };
413
send_message("Failed to move there...\n");
417
enter_room(oUser->get_environment(), env);
418
//send_room(oUser->get_environment());
419
send_message(show_inventory(oUser->get_environment()));
422
else if(sizeof(args))
423
send_message("The exit '" + args + "' was not found.\n");
428
int cmd_create(string cmd, string args)
431
create_object(tmp[0], tmp[1..]*" ");
435
int cmd_addroom(string cmd, string args)
437
create_object("Room", args);
441
int cmd_addcontainer(string cmd, string args)
443
create_object("Container", args);
447
int cmd_addlink(string cmd, string args)
451
if ( sscanf(args, "#%d",objectid) == 1) {
452
room = find_object(objectid);
455
send_message("Es gibt keinen Raum mit der Angegebenen ID.");
460
int cmd_execute_cmd(string cmd, string args)
462
send_message("\n"+execute(" "+args)+"\n");
466
// copied from masterlist.pike
468
int cmd_load(string cmd, string args)
471
sscanf(args,"#%i %*",iOID);
473
find_object(iOID)->get_identifier();
477
int cmd_upgrade(string cmd, string args)
481
[option, iOID] = array_sscanf(args,"%s#%i %*");
483
if(option=="--force " || option=="-f ")
489
pOID = find_object(iOID);
491
if(!pOID && master()->programs[option])
492
target=master()->programs[option];
493
else if (pOID->status() <= PSTAT_DISK) {
494
send_message("Use Load instead\n");
498
target=object_program(pOID->get_object());
501
send_message("could not find program\n");
505
send_message("upgrading...\n");
507
mixed res = master()->upgrade(target, force);
509
send_message("Upgrade failed, try --force\n");
511
send_message("Result: %s\n", (string)res);
516
mapping(string:function) run_commands = ([ "l":cmd_look,
526
"adduser":cmd_adduser,
527
"addroom":cmd_addroom,
528
"addcontainer":cmd_addcontainer,
529
"addlink":cmd_addlink,
531
"upgrade":cmd_upgrade,
533
"cmd":cmd_execute_cmd,
540
static int handle_command(string cmd)
543
sscanf(cmd, "%s %s", cmd, args);
544
mMode->cmd=run_commands[cmd];
545
if(run_commands[cmd])
546
return run_commands[cmd](cmd, args);
550
static void process_command(string cmd)
555
send_message("Welcome to sTeam\n: ");
556
send_message("Login: ");
560
tmpUser = _Persistence->lookup_user(cmd);
561
if ( !objectp(tmpUser) )
563
send_message("User '"+cmd+"' does not exist !\n");
564
send_message("Login: ");
568
readln->set_secret( 1 );
569
send_message("Password for "+cmd+": ");
574
if ( objectp(tmpUser) && tmpUser->check_user_password(cmd) ) {
575
send_message("Hi, " + tmpUser->get_identifier() + " - last seen "+
577
ctime(tmpUser->query_attribute(USER_LAST_LOGIN)));
580
enter_room(tmpUser->get_environment());
581
//send_room(tmpUser->get_environment());
582
send_message(show_inventory(tmpUser->get_environment()));
584
readln->set_secret( 0 );
585
tmpUser->listen_to_event(EVENT_TELL, tmpUser);
588
array history=tmpUser->query_attribute("telnet_history");
591
readln->readline->historyobj=readln->readline->History(512, history+({ "" }));
593
readln->readline->historyobj->delta_history(sizeof(history));
600
send_message("Wrong password, please try again: ");
603
tmpUser->disconnect();
608
case MODE_MORE_INPUT:
612
if ( strlen(cmd) != 0 && !handle_command(cmd) )
613
send_message("The command %O was not understood.\n", cmd);
614
send_message("["+_FILEPATH->object_to_path(oUser)+"] > ");
619
int get_client_features() { return CLIENT_FEATURES_ALL; }
620
string get_socket_name() { return "telnet"; }
622
// allow user to store stuff somewhere
623
mapping temp_cmd=([]);
625
int cmd_man(string cmd, string args)
627
string filename = "server/net/manpages/" + args + ".man";
628
if(Stdio.exist(filename))
630
write ("\n" + Stdio.read_file(filename) + "\n");
634
write("There is no command \"" + args + "\"\n");
635
write("usage : man <command>\n");
641
int cmd_help(string cmd, string args)
643
send_message("\nthe following commands are available:\n%s\n",
644
sort(indices(run_commands))*" ");