expose our hello to plugins
[oweals/gnunet.git] / src / fs / gnunet-search.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file fs/gnunet-search.c
22  * @brief searching for files on GNUnet
23  * @author Christian Grothoff
24  * @author Krista Bennett
25  * @author James Blackwell
26  * @author Igor Wronsky
27  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30
31 static int ret;
32
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35 static struct GNUNET_SCHEDULER_Handle *sched;
36
37 static struct GNUNET_FS_Handle *ctx;
38
39 static struct GNUNET_FS_SearchContext *sc;
40
41 static char *output_filename;
42
43 static struct GNUNET_FS_DirectoryBuilder *db;
44
45 static unsigned int anonymity = 1;
46
47 static int verbose;
48
49 static int local_only;
50
51 /**
52  * Type of a function that libextractor calls for each
53  * meta data item found.
54  *
55  * @param cls closure (user-defined, unused)
56  * @param plugin_name name of the plugin that produced this value;
57  *        special values can be used (i.e. '<zlib>' for zlib being
58  *        used in the main libextractor library and yielding
59  *        meta data).
60  * @param type libextractor-type describing the meta data
61  * @param format basic format information about data 
62  * @param data_mime_type mime-type of data (not of the original file);
63  *        can be NULL (if mime-type is not known)
64  * @param data actual meta-data found
65  * @param data_size number of bytes in data
66  * @return 0 to continue extracting, 1 to abort
67  */ 
68 static int
69 item_printer (void *cls,
70               const char *plugin_name,
71               enum EXTRACTOR_MetaType type, 
72               enum EXTRACTOR_MetaFormat format,
73               const char *data_mime_type,
74               const char *data,
75               size_t data_size)
76 {
77   if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
78        (format != EXTRACTOR_METAFORMAT_C_STRING) )
79     return 0;
80   printf ("\t%20s: %s\n",
81           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
82                     EXTRACTOR_metatype_to_string (type)),
83           data);
84   return 0;
85 }
86
87
88 static void
89 clean_task (void *cls,
90             const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   size_t dsize;
93   void *ddata;
94
95   GNUNET_FS_stop (ctx);
96   ctx = NULL;
97   if (output_filename == NULL)
98     return;
99   if (GNUNET_OK !=
100       GNUNET_FS_directory_builder_finish (db,
101                                           &dsize,       
102                                           &ddata))
103     {
104       GNUNET_break (0);
105       GNUNET_free (output_filename);    
106       return;
107     }
108   if (dsize != 
109       GNUNET_DISK_fn_write (output_filename,
110                             ddata,
111                             dsize,
112                             GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))
113     {
114       fprintf (stderr,
115                _("Failed to write directory with search results to `%s'\n"),
116                output_filename);
117     }
118   GNUNET_free_non_null (ddata);
119   GNUNET_free (output_filename);
120 }
121
122
123 /**
124  * Called by FS client to give information about the progress of an 
125  * operation.
126  *
127  * @param cls closure
128  * @param info details about the event, specifying the event type
129  *        and various bits about the event
130  * @return client-context (for the next progress call
131  *         for this operation; should be set to NULL for
132  *         SUSPEND and STOPPED events).  The value returned
133  *         will be passed to future callbacks in the respective
134  *         field in the GNUNET_FS_ProgressInfo struct.
135  */
136 static void *
137 progress_cb (void *cls,
138              const struct GNUNET_FS_ProgressInfo *info)
139 {
140   static unsigned int cnt;
141   char *uri;
142   char *dotdot;
143   char *filename;
144
145   switch (info->status)
146     {
147     case GNUNET_FS_STATUS_SEARCH_START:
148       break;
149     case GNUNET_FS_STATUS_SEARCH_RESULT:
150       if (db != NULL)
151         GNUNET_FS_directory_builder_add (db,
152                                          info->value.search.specifics.result.uri,
153                                          info->value.search.specifics.result.meta,
154                                          NULL);
155       uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
156       printf ("#%u:\n", cnt++);
157       filename =
158         GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
159                                                 EXTRACTOR_METATYPE_FILENAME);
160       if (filename != NULL)
161         {
162           while (NULL != (dotdot = strstr (filename, "..")))
163             dotdot[0] = dotdot[1] = '_';
164           printf ("gnunet-download -o \"%s\" %s\n", 
165                   filename, 
166                   uri);
167         }
168       else
169         printf ("gnunet-download %s\n", uri);
170       if (verbose)
171         GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result.meta, 
172                                             &item_printer,
173                                             NULL);
174       printf ("\n"); 
175       fflush(stdout);
176       GNUNET_free_non_null (filename);
177       GNUNET_free (uri);
178       break;
179     case GNUNET_FS_STATUS_SEARCH_UPDATE:
180       break;
181     case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
182       /* ignore */
183       break;
184     case GNUNET_FS_STATUS_SEARCH_ERROR:
185       fprintf (stderr,
186                _("Error searching: %s.\n"),
187                info->value.search.specifics.error.message);
188       GNUNET_SCHEDULER_shutdown (sched);
189       break;
190     case GNUNET_FS_STATUS_SEARCH_STOPPED: 
191       GNUNET_SCHEDULER_add_continuation (sched,
192                                          &clean_task, 
193                                          NULL,
194                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195       break;      
196     default:
197       fprintf (stderr,
198                _("Unexpected status: %d\n"),
199                info->status);
200       break;
201     }
202   return NULL;
203 }
204
205
206 static void
207 shutdown_task (void *cls,
208                const struct GNUNET_SCHEDULER_TaskContext *tc)
209 {
210   if (sc != NULL)
211     {
212       GNUNET_FS_search_stop (sc); 
213       sc = NULL;
214     }
215 }
216
217
218 /**
219  * Main function that will be run by the scheduler.
220  *
221  * @param cls closure
222  * @param s the scheduler to use
223  * @param args remaining command-line arguments
224  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225  * @param c configuration
226  */
227 static void
228 run (void *cls,
229      struct GNUNET_SCHEDULER_Handle *s,
230      char *const *args,
231      const char *cfgfile,
232      const struct GNUNET_CONFIGURATION_Handle *c)
233 {
234   struct GNUNET_FS_Uri *uri;
235   unsigned int argc;
236   enum GNUNET_FS_SearchOptions options;
237
238   sched = s;
239   argc = 0;
240   while (NULL != args[argc])
241     argc++;
242   uri = GNUNET_FS_uri_ksk_create_from_args (argc,
243                                             (const char **) args);
244   if (NULL == uri)
245     {
246       fprintf (stderr,
247                _("Could not create keyword URI from arguments.\n"));
248       ret = 1;
249       GNUNET_FS_uri_destroy (uri);
250       return;
251     }
252   cfg = c;
253   ctx = GNUNET_FS_start (sched,
254                          cfg,
255                          "gnunet-search",
256                          &progress_cb,
257                          NULL,
258                          GNUNET_FS_FLAGS_NONE,
259                          GNUNET_FS_OPTIONS_END);
260   if (NULL == ctx)
261     {
262       fprintf (stderr,
263                _("Could not initialize `%s' subsystem.\n"),
264                "FS");
265       GNUNET_FS_uri_destroy (uri);
266       ret = 1;
267       return;
268     }
269   if (output_filename != NULL)
270     db = GNUNET_FS_directory_builder_create (NULL);
271   options = GNUNET_FS_SEARCH_OPTION_NONE;
272   if (local_only)
273     options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
274   sc = GNUNET_FS_search_start (ctx,
275                                uri,
276                                anonymity,
277                                options,
278                                NULL);
279   GNUNET_FS_uri_destroy (uri);
280   if (NULL == sc)
281     {
282       fprintf (stderr,
283                _("Could not start searching.\n"));
284       GNUNET_FS_stop (ctx);
285       ret = 1;
286       return;
287     }
288   GNUNET_SCHEDULER_add_delayed (sched,
289                                 GNUNET_TIME_UNIT_FOREVER_REL,
290                                 &shutdown_task,
291                                 NULL);
292 }
293
294
295 /**
296  * The main function to search GNUnet.
297  *
298  * @param argc number of arguments from the command line
299  * @param argv command line arguments
300  * @return 0 ok, 1 on error
301  */
302 int
303 main (int argc, char *const *argv)
304 {
305   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
306     {'a', "anonymity", "LEVEL",
307      gettext_noop ("set the desired LEVEL of receiver-anonymity"),
308      1, &GNUNET_GETOPT_set_uint, &anonymity},
309     {'n', "no-network", NULL,
310      gettext_noop ("only search the local peer (no P2P network search)"),
311      1, &GNUNET_GETOPT_set_uint, &local_only},
312     {'o', "output", "PREFIX",
313      gettext_noop
314      ("write search results to file starting with PREFIX"),
315      1, &GNUNET_GETOPT_set_string, &output_filename}, 
316     {'V', "verbose", NULL,
317      gettext_noop ("be verbose (print progress information)"),
318      0, &GNUNET_GETOPT_set_one, &verbose},
319     GNUNET_GETOPT_OPTION_END
320   };
321   return (GNUNET_OK ==
322           GNUNET_PROGRAM_run (argc,
323                               argv,
324                               "gnunet-search",
325                               gettext_noop
326                               ("Search GNUnet."),
327                               options, &run, NULL)) ? ret : 1;
328 }
329
330 /* end of gnunet-search.c */
331