2 inherit "../client/client_base";
3 inherit "base/xml_parser";
4 inherit "base/xml_data";
10 class import : public client_base,xml_parser,xml_data{
14 #define WARNING(s) werror(s)
15 #define MESSAGE(s, args...) werror(s+"\n", args);
20 array aObjects; // array of created objects
21 mapping mAttributes = ([ ]);
23 Stdio.File creatorLog = Stdio.File("creators.log", "wct");
25 mapping xmlMap(NodeXML n)
28 foreach ( n->children, NodeXML children) {
29 if ( children->name == "member" ) {
31 foreach(children->children, object o) {
33 if ( o->name == "key" )
34 key = unserialize(o->children[0]);
35 else if ( o->name == "value" )
36 value = unserialize(o->children[0]);
44 array xmlArray(NodeXML n)
47 foreach ( n->children, NodeXML children) {
48 res += ({ unserialize(children) });
53 object create_object(string id, string path)
58 MESSAGE("Creating object id="+id);
62 f = Stdio.File(sDirectory + "/__objects__/"+id+"._xml_", "r");
65 if ( err != 0 || !objectp(f) ) {
66 werror("Warning: Failed to find %s\n", id+".xml");
71 string xml = f->read();
74 NodeXML n = parse_data(xml);
76 int clnum = (int)n->get_node("/Object")->attributes["class"];
77 object factory = send_command(COAL_COMMAND, ({ "get_factory", ({
80 object linkto = n->get_node("/Object/linkto");
81 mapping vars = ([ "name": "import", ]);
82 if ( objectp(linkto) ) {
83 object lt = mVariables["filepath:tree"]->path_to_object(linkto->data);
84 if ( clnum & CLASS_EXIT )
89 obj = send_command(COAL_COMMAND, ({ "execute", ({ vars }) }) );
91 aObjects += ({ obj });
94 f = Stdio.File(sDirectory + "/__objects__/"+id, "r");
96 werror("Uploading Document");
97 send_cmd(obj, "set_content", ({ f->read() }) , 1);
99 werror(".... finished.\n");
102 object_to_server(obj, path + "objects/"+id, n);
111 f = Stdio.File(sDirectory + "/groups/groups.xml", "r");
113 if ( err != 0 || !objectp(f) )
115 NodeXML n = parse_data(f->read());
117 foreach(n->get_node("/groups/array")->get_nodes("object"), NodeXML g) {
121 MESSAGE("Group:" + g->get_node("id")->data);
122 grp_name = g->get_node("id")->data;
123 set_object(mVariables["groups"]);
124 grp = send_command(COAL_COMMAND, ({ "lookup", ({ grp_name }) }));
125 if ( !objectp(grp) ) {
126 MESSAGE("Creating: " + grp_name);
127 set_object(mVariables["Group.factory"]);
128 grp = send_command(COAL_COMMAND,
129 ({ "execute", ({([ "name": grp_name, ]) }) }) );
130 aObjects += ({ grp });
133 MESSAGE("Skipping " + grp_name + " ... group exists !");
139 object create_group(string name, string|object parent)
141 object factory = mVariables["Group.factory"];
142 if ( stringp(parent) )
143 parent = mVariables->groups->lookup(parent);
144 werror("Creating group %s, parent=%O\n", name, parent);
145 object group = factory->execute( ([
147 "parentgroup": parent, ]) );
151 object import_groups(string fname)
155 f = Stdio.File(fname, "r");
157 if ( err != 0 || !objectp(f) ) {
158 werror("Fatal: cannot read %s\n", fname);
161 string str = f->read();
167 object grp = n->get_node("/group");
168 string grpname = grp->attributes->name;
169 string identifier = grp->attributes->identifier;
170 werror("Looking up group %s\n", identifier);
171 object group = mVariables["groups"]->lookup(identifier);
172 array grps = identifier / ".";
174 if ( sizeof(grps) > 2 ) {
175 parent = grps[..sizeof(grps)-2] * ".";
178 sscanf(identifier, "%s.%*s", parent);
180 werror("Importing group " + grpname + " ... ");
181 if ( !objectp(group) ) {
182 group = create_group(grpname, parent);
183 werror("created!\n");
186 werror("found on server.\n");
188 foreach(grp->get_nodes("member"), object m) {
189 object user = mVariables->users->lookup(m->data);
190 if ( objectp(user) ) {
191 group->add_member(user);
192 if ( mappingp(m->attributes) && m->attributes->admin )
193 group->sanction_object(user, SANCTION_INSERT|SANCTION_MOVE|SANCTION_WRITE);
196 string path = dirname(fname);
197 foreach(grp->get_nodes("subgroup"), object sg) {
198 object g = import_groups(path+"/__group_"+sg->data+"__.xml");
199 group->add_member(g);
201 if ( mSwitches->rooms ) {
202 object wr = group->query_attribute("GROUP_WORKROOM");
203 string wrname = wr->get_identifier();
204 werror("Checking for %s in %s ...", wrname, path);
205 if ( Stdio.exist(path + "/" + wrname) ) {
208 container_to_server(wr, path+"/"+wrname, wrname);
216 void import_users(string fname)
220 f = Stdio.File(fname, "r");
222 if ( err != 0 || !objectp(f) )
224 string str = f->read();
230 string grp = n->get_node("/users")->attributes->group;
231 object group = send_cmd(mVariables->groups, "lookup", grp);
233 foreach(n->get_nodes("/users/user"), object u) {
234 if ( u->get_node("nickname")->data == "0" )
236 mapping attr = ([ ]);
237 foreach(u->get_children(), object s) {
238 if ( stringp(s->data) && s->data != "0" )
239 attr[s->name] = s->data;
241 object user = send_cmd(mVariables["users"],"lookup", u->get_node("nickname")->data);
242 werror("Importing %s ... ", u->get_node("nickname")->data);
243 if ( !xml.utf8_check(lower_case(attr->nickname)) || search(attr->nickname, " ") >= 0 || !stringp(attr->pw)) {
244 werror(" name not utf-8....\n");
247 if ( !objectp(user) ) {
248 user = send_cmd(mVariables["User.factory"], "execute", attr);
249 werror("New User!\n");
252 werror("exists...\n");
253 send_cmd(user, "set_user_password", ({ attr["pw"], 1}) );
254 send_cmd(user, "activate_user");
255 if ( objectp(group) )
256 send_cmd(group, "add_member", user);
265 f = Stdio.File(sDirectory + "/groups/users.xml", "r");
267 if ( err != 0 || !objectp(f) )
269 NodeXML n = parse_data(f->read());
271 foreach(n->get_node("/users/array")->get_nodes("object"), NodeXML u) {
275 MESSAGE("User:" + u->get_node("id")->data);
276 user_name = u->get_node("id")->data;
277 f = Stdio.File(sDirectory + "/groups/"+user_name+".xml", "r");
278 NodeXML user_n = parse_data(f->read());
281 set_object(mVariables["users"]);
282 user = send_command(COAL_COMMAND, ({ "lookup", ({ user_name }) }));
283 if ( !objectp(user) ) {
284 MESSAGE("Creating: " + user_name);
285 set_object(mVariables["User.factory"]);
286 user = send_command(COAL_COMMAND,
287 ({ "execute", ({([ "name": user_name,
288 "pw":user_n->get_node(
289 "/Object/password")->data,
291 object_to_server(user, sDirectory + "/groups/"+user_name);
292 aObjects += ({ user });
295 MESSAGE("Skipping " + user_name + " - user exists !");
301 mixed unserialize(NodeXML n)
314 return (float)n->data;
320 string type = n->children[0]->data;
321 string id = n->children[1]->data;
327 oid = set_object(mVariables["groups"]);
330 oid = set_object(mVariables["users"]);
333 oid = set_object(0); // set the object to _Server
334 obj = send_command(COAL_COMMAND, ({ "get_module", ({ id }) }));
336 WARNING("Failed to find module " +id + " on target server !");
341 if ( objectp(mObjects[id]) )
343 return create_object(id, sDirectory+"/objects");
346 oid = set_object(mVariables["filepath:tree"]);
347 obj = send_command(COAL_COMMAND, ({ "path_to_object", ({ id }) }));
349 WARNING("Failed to find path " +id + " on target server !\n");
354 obj = send_command(COAL_COMMAND, ({ "lookup", ({ id }) }));
362 array read_meta(string path)
364 Stdio.File f = Stdio.File(path + "/__steam__.xml", "r");
365 string meta = f->read();
367 NodeXML n = parse_data(meta);
368 n = n->get_node("/folder/files");
369 array nodes = n->get_nodes("file");
371 foreach(nodes, object node) {
372 res += ({ node->get_data() });
377 void container_to_server(object obj, string directory, string path)
380 if ( obj->get_object_class() & CLASS_USER )
382 array dir = get_dir(directory);
383 dir = read_meta(directory);
384 foreach(dir, string fname) {
385 if ( fname == "xml" || fname == "files" ) continue;
386 fname = utf8_to_string(fname);
390 f = Stdio.File(sDirectory + "/" +path + "/" + fname+"._xml_","r");
392 f = Stdio.File(sDirectory + "/" +path + "/" + fname+".xml","r");
395 werror("Failed to load file %s\n%O\n", fname, err);
397 // if the file does not exist its not part of the reference
398 // server installation
402 string xml = f->read();
408 // is that object already on the server ?
409 o = send_cmd(obj, "get_object_byname", string_to_utf8(fname));
411 if ( Stdio.is_dir(directory + "/" + fname) ) {
412 werror("- Directory:"+fname+":");
414 int cl = (int)n->attributes["class"];
417 if ( mSwitches->test ) {
418 werror("Missing: " + fname + "\n");
421 werror("Creating container/room: " + fname+"\n");
423 set_object(send_cmd(0, "get_factory", cl));
424 o = send_command(COAL_COMMAND, ({ "execute", ({
425 ([ "name": string_to_utf8(fname), ]) }) }));
427 send_cmd(o, "move", obj, 1);
428 object_to_server(o, sDirectory + path +"/"+ fname, n);
430 container_to_server(o, directory + "/"+ fname, path +"/"+fname);
432 else if ( !Stdio.exist(directory + "/" + fname) ) {
434 int classid = (int)(n->get_node("/Object")->attributes->class);
435 werror("Object class = %d\n", classid);
436 object factory = send_cmd(0, "get_factory", classid);
438 o = send_cmd(factory, "execute", ([ "name":string_to_utf8(fname), "url":"http://www.dummy.de",]));
440 o = send_cmd(factory, "execute", ([ "name":string_to_utf8(fname), ]));
442 send_cmd(o, "move", obj);
443 object_to_server(o, sDirectory+path+"/"+fname, n);
446 f = Stdio.File(directory + "/" + fname, "r");
447 werror("- File:"+fname+":");
449 if ( mSwitches->test ) {
450 werror("Missing!!!\n");
453 o = send_cmd(mVariables["Document.factory"], "execute",
455 "name": string_to_utf8(fname),
457 send_cmd(o, "move", obj);
459 werror("Failed to create document !");
463 object_to_server(o, sDirectory + path +"/"+ fname, n);
465 if ( !mSwitches->test ) {
466 send_cmd(o, "set_content", ({ f->read() }), 1);
467 if ( mSwitches->update ) {
468 object_to_server(o, sDirectory + path + "/" + fname, n);
472 werror(" found. Skipping upload.\n");
479 object annotation_to_server(NodeXML ann, string path)
481 return create_object(ann->get_node("id")->data, path);
485 int handle_error(mixed err)
490 void object_to_server(object obj, string path, NodeXML|void n)
492 MESSAGE("reading... " + path+".xml");
497 f = Stdio.File(path + ".xml", "r");
499 // if the file does not exist its not part of the reference
500 // server installation
502 werror("Not found !\n");
505 string xml = f->read();
509 mapping attributes = xmlMap(n->get_node("/Object/attributes/struct"));
510 mapping a_acquire = xmlMap(
511 n->get_node("/Object/attributes-acquire/struct"));
512 mapping sanction = xmlMap(n->get_node("/Object/sanction/struct"));
513 mapping msanction = xmlMap(n->get_node("/Object/sanction-meta/struct"));
514 mixed acquire = unserialize(n->get_node("/Object/acquire")->children[0]);
515 mapping a_lock = xmlMap(n->get_node("/Object/attributes-locked/struct"));
518 if ( obj->get_object_class() & CLASS_USER ) {
520 unserialize(n->get_node("/Object/groups/array"));
521 foreach(groups, object grp) {
522 send_cmd(grp, "add_member", obj, 1);
525 if ( obj->get_object_class() & CLASS_LINK && objectp(n->get_node("/Object/linkto")) )
527 string link = n->get_node("/Object/linkto")->data;
529 if ( stringp(link) ) {
530 object lnk = mVariables["filepath:tree"]->path_to_object(link);
532 obj->set_link_object(lnk);
534 werror("Cannot resolve the link %s\n", link);
537 werror("Link not found %O\n", n->get_node("/Object/linkto"));
539 foreach(indices(sanction), object sanc) {
540 if ( objectp(sanc) ) {
541 send_cmd(obj, "sanction_object", ({ sanc, sanction[sanc] }), 1 );
544 foreach(indices(msanction), object msanc) {
545 if ( objectp(msanc) ) {
546 send_cmd(obj, "sanction_object_meta", ({ sanction[msanc] }), 1);
553 send_cmd(obj, "unlock_attributes", ({ }), 1);
554 foreach(indices(a_acquire), key) {
555 if ( stringp(a_acquire[key]) ) // environment only, may not work *g*
556 send_cmd(obj, "set_acquire_attribute", ({ key, 1 }), 1);
558 send_cmd(obj, "set_acquire_attribute", ({ key, a_acquire[key]}),1);
560 foreach(indices(attributes), string key) {
561 send_cmd(obj, "set_attribute", ({ key, attributes[key] }), 1);
563 //send_cmd(obj, "set_attributes", attributes, 1);
564 foreach(indices(a_lock), key ) {
565 if ( a_lock[key] != 0 ) {
566 send_cmd(obj, "lock_attribute", ({ key }), 1);
570 // acquire string should be environment function, but is default setting...
571 // cannot handle functions yet
572 if ( objectp(acquire) )
573 send_cmd(obj, "set_acquire", ({ acquire }), 1);
574 mixed creator = unserialize(n->get_node("/Object/creator/object"));
575 send_cmd(obj, "set_creator",({ creator }), 1);
576 if ( objectp(creator) )
577 creatorLog->write(obj->get_object_id() + ", creator="+creator->get_identifier()+"\n");
578 NodeXML annotations = n->get_node("/Object/annotations/array");
579 if ( objectp(annotations) ) {
580 array anns = send_cmd(obj, "get_annotations", ({ }), 1);
581 // if the object already has annotations remove them !
582 if ( arrayp(anns) ) {
583 foreach(anns, object a) {
584 send_cmd(obj, "remove_annotation", a, 1);
587 werror("%d Annotations....\n", sizeof(annotations->children));
588 foreach(annotations->children, NodeXML ann) {
589 object annotation = annotation_to_server(ann, dirname(path));
590 send_cmd(obj, "add_annotation", ({ annotation }), 1);
593 foreach(indices(mAttributes), string idx) {
595 send_cmd(obj, "set_attribute", ({ idx, mAttributes[idx] }), 1);
599 int _import(string server, int port, string directory, string outPath)
601 if ( connect_server(server, port) ) {
602 string user = "root";
607 Stdio.Readline rl = Stdio.Readline();
608 string iuser = rl->read("User ? ["+ user + "]: ");
609 string ipw = rl->read("Password ? ["+pw+"]: ");
610 if ( iuser != "" ) user = iuser;
611 if ( ipw != "" ) pw = ipw;
612 if ( !login(user, pw, CLIENT_STATUS_CONNECTED) ) {
613 error("Wrong User or Password !\n");
615 // now get the inventory from the root room
616 if ( mSwitches->users ) {
617 import_users(mSwitches->users);
620 if ( mSwitches->group ) {
621 import_groups(mSwitches->group);
625 if ( search(mSwitches->create, "groups") >= 0 )
627 if ( search(mSwitches->create, "users") >= 0 )
629 if ( search(mSwitches->create, "objects") >= 0 ) {
630 object start = mVariables["rootroom"];
632 if ( (int)directory > 0 ) {
633 int dir = (int)directory;
634 start = send_cmd(0, "find_object", dir);
635 MESSAGE("Starting with %O", start);
637 else if ( directory != "/" ) {
638 int oid = set_object(mVariables["filepath:tree"]);
639 if ( (int)outPath > 0 )
640 start = send_cmd((int)outPath, "this");
642 start = send_command(COAL_COMMAND, ({ "path_to_object",
644 MESSAGE("Starting with %O", start);
646 container_to_server(start, directory, "");
648 // file of objects that are created during installation
649 Stdio.File f = Stdio.File("install.xml", "wct");
650 f->write(compose(aObjects));
652 werror("\n-- finished: Import created " + sizeof(aObjects) +
653 " new objects on server in " + (time()-t) + " seconds !\n");
655 werror("Cannot connect to server !\n");
660 int main(int argc, array argv)
663 string server= "localhost";
664 string directory = "/";
669 mSwitches = ([ "create": "users,groups,objects", ]);
671 for ( int i = 1; i < sizeof(argv); i++ ) {
674 if ( sscanf(argv[i], "--%s=%s", cmd, arg) >= 1 ) {
689 mSwitches["users"] = arg;
692 mSwitches["group"] = arg;
695 mSwitches["rooms"] = true;
701 mSwitches["test"] = true;
704 mSwitches["update"] = true;
711 //subpath is another switch
712 sDirectory = directory;
713 _import(server, port, directory, outPath);