checkout_web._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
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.
7  *
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.
12  *
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
16  *
17  * $Id: checkout_web.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 inherit "pikewww/local/base/client_base";
20 inherit "base/xml_data";
21 #include <coal.h>
22 #include <macros.h>
23 #include <classes.h>
24 #include <client.h>
25 class checkout_web : public client_base,xml_data{
26 public:
27 
28 
29 
30 
31 
32 string compose_scalar(mixed s)
33 {
34  if ( objectp(s) ) {
35  string type, name;
36  if ( s->get_object_class() & CLASS_USER ) {
37  type = "User";
38  name = s->get_identifier();
39  }
40  else if ( s->get_object_class() & CLASS_GROUP ) {
41  type = "Group";
42  name = s->get_identifier();
43  }
44  else if ( s->get_object_class() & CLASS_MODULE ) {
45  type = "Module";
46  name = s->get_identifier();
47  }
48  else {
49  type = "Path";
50  int oid = set_object(mVariables["filepath:tree"]);
51  name = send_command(COAL_COMMAND, ({ "object_to_filename", ({ s }) }));
52  set_object(oid);
53  }
54  return "<object><type>"+type+"</type><id>"+name+"</id></object>";
55  }
56  else
57  return ::compose_scalar(s);
58 }
59 
60 void update_content(object obj, string path)
61 {
62  string content = send_command(COAL_COMMAND, ({ "get_content" }));
63  path = "files"+path;
64 
65  if ( !stringp(content) )
66  return;
67  // create the directory structure !
68  string dir;
69  array tokens = (path/"/");
70  dir = tokens[..sizeof(tokens)-2]*"/";
71  Stdio.mkdirhier(dir);
72 
73  Stdio.File f = Stdio.File(path, "wct");
74  f->write(content);
75  f->close();
76 }
77 
78 void xmlize_object(object obj, object xml)
79 {
80  set_object(obj);
81  xml->write("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"+
82  "<Object class=\""+
83  obj->get_object_class()+"\">\n");
84 
85  mapping attributes = send_command(COAL_COMMAND, ({ "query_attributes" }));
86 
87  xml->write("<attributes>\n"+compose_struct(attributes)+ "</attributes>\n");
88  mapping acquire_map = send_command(COAL_COMMAND,
89  ({ "get_acquired_attributes" }));
90  xml->write("<attributes-acquire>\n"+
91  compose_struct(acquire_map) + "\n</attributes-acquire>\n");
92  mapping sanction = send_command(COAL_COMMAND, ({ "get_sanction" }));
93  mapping msanction = send_command(COAL_COMMAND, ({ "get_meta_sanction" }));
94 
95  xml->write("<sanction>\n"+compose_struct(sanction) + "</sanction>\n");
96  xml->write("<sanction-meta>\n"+compose_struct(msanction)+
97  "</sanction-meta>\n");
98  xml->write("<acquire>\n"+
99  compose(send_command(COAL_COMMAND,({ "get_acquire" })))+
100  "</acquire>\n");
101  xml->write("</Object>\n");
102 }
103 
104 void object_from_server(object obj)
105 {
106  if ( obj->get_object_class() & CLASS_USER )
107  return;
108 
109  set_object(mVariables["filepath:tree"]);
110  string path = send_command(COAL_COMMAND,
111  ({ "object_to_filename", ({ obj }) }));
112  set_object(obj);
113  string id = send_command(COAL_COMMAND, ({ "get_identifier" }));
114 
115 
116  if ( obj->get_object_class() & CLASS_DOCUMENT )
117  update_content(obj, path);
118 
119  path = "xml" + path;
120  // create the directory structure !
121  string dir;
122  array tokens = (path/"/");
123  dir = tokens[..sizeof(tokens)-2]*"/";
124  Stdio.mkdirhier(dir);
125 
126  werror("Checking out " + path + "... ok\n");
127  // now write xml for the object
128  Stdio.File xml = Stdio.File(path + ".xml", "wct");
129  xmlize_object(obj, xml);
130  xml->close();
131 
132  if ( obj->get_object_class() & CLASS_CONTAINER &&
133  !(obj->get_object_class() & CLASS_MODULE) )
134  {
135  array inv = send_command(COAL_COMMAND, ({ "get_inventory" }));
136  foreach(inv, object o)
137  object_from_server(o);
138  }
139 
140 }
141 
142 int run(int argc, array argv)
143 {
144  int port = 1900;
145  string server= "localhost";
146  string directory = "/";
147  int i;
148  string file = 0;
149 
150  for ( i = 1; i < sizeof(argv); i++ ) {
151  string cmd, arg;
152  if ( sscanf(argv[i], "--%s=%s", cmd, arg) == 2 ) {
153  switch ( cmd ) {
154  case "server":
155  server = arg;
156  break;
157  case "port":
158  port = (int) arg;
159  break;
160  case "directory":
161  directory = arg;
162  break;
163  case "file":
164  file = arg;
165  break;
166  default:
167  werror(sprintf("Unknown parameter %s\n", argv[i]));
168  break;
169  }
170  }
171  }
172  if ( connect_server(server, port) ) {
173  string user = "root";
174  string pw = "steam";
175 
176 
177  Stdio.Readline rl = Stdio.Readline();
178  string iuser = rl->read("User ? ["+ user + "]: ");
179  string ipw = rl->read("Password ? ["+pw+"]: ");
180  if ( iuser != "" ) user = iuser;
181  if ( ipw != "" ) pw = ipw;
182 
183 
184  login(user, pw, CLIENT_STATUS_CONNECTED);
185  // now get the inventory from the root room
186 
187  object start = mVariables["rootroom"];
188  if ( directory != "/" ) {
189  int oid = set_object(mVariables["filepath:tree"]);
190  start = send_command(COAL_COMMAND, ({ "path_to_object",
191  ({ directory }), }));
192  }
193  else if ( stringp(file) ) {
194  start = send_cmd(mVariables["filepath:tree"], "path_to_object",file);
195  }
196 
197  object_from_server(start);
198  if ( !stringp(file) ) {
199  set_object(0);
200  array modules = send_command(COAL_COMMAND, ({ "get_module_objs" }));
201  foreach( modules, object module ) {
202  string p = "modules-xml/"+module->get_identifier();
203  Stdio.mkdirhier(dirname(p));
204 
205  write("Module: " + p + "\n");
206  object mf = Stdio.File(p,"wct");
207  xmlize_object(module, mf);
208  mf->close();
209  }
210  }
211  return 0;
212  }
213 }
214 
215 
216 };