types
[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 2, 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  * TODO:
29  * - add many options (timeout, namespace search, etc.)
30  */
31 #include "platform.h"
32 #include "gnunet_fs_service.h"
33
34 static int ret;
35
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
37
38 static struct GNUNET_FS_Handle *ctx;
39
40 static struct GNUNET_FS_SearchContext *sc;
41
42 static unsigned int anonymity = 1;
43
44 static int verbose;
45
46 static int
47 item_printer (void *cls,
48               EXTRACTOR_KeywordType type, 
49               const char *data)
50 {
51   printf ("\t%20s: %s\n",
52           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
53                     EXTRACTOR_getKeywordTypeAsString (type)),
54           data);
55   return GNUNET_OK;
56 }
57
58
59 /**
60  * Called by FS client to give information about the progress of an 
61  * operation.
62  *
63  * @param cls closure
64  * @param info details about the event, specifying the event type
65  *        and various bits about the event
66  * @return client-context (for the next progress call
67  *         for this operation; should be set to NULL for
68  *         SUSPEND and STOPPED events).  The value returned
69  *         will be passed to future callbacks in the respective
70  *         field in the GNUNET_FS_ProgressInfo struct.
71  */
72 static void *
73 progress_cb (void *cls,
74              const struct GNUNET_FS_ProgressInfo *info)
75 {
76   char *uri;
77   char *dotdot;
78   char *filename;
79
80   switch (info->status)
81     {
82     case GNUNET_FS_STATUS_SEARCH_START:
83       break;
84     case GNUNET_FS_STATUS_SEARCH_RESULT:
85       uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
86       printf ("%s:\n", uri);
87       filename =
88         GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
89                                                 EXTRACTOR_FILENAME);
90       if (filename != NULL)
91         {
92           while (NULL != (dotdot = strstr (filename, "..")))
93             dotdot[0] = dotdot[1] = '_';
94           printf ("gnunet-download -o \"%s\" %s\n", 
95                   filename, 
96                   uri);
97         }
98       else
99         printf ("gnunet-download %s\n", uri);
100       if (verbose)
101         GNUNET_CONTAINER_meta_data_get_contents (info->value.search.specifics.result.meta, 
102                                                  &item_printer,
103                                                  NULL);
104       printf ("\n");
105       fflush(stdout);
106       GNUNET_free_non_null (filename);
107       GNUNET_free (uri);
108       break;
109     case GNUNET_FS_STATUS_SEARCH_UPDATE:
110       break;
111     case GNUNET_FS_STATUS_SEARCH_ERROR:
112       fprintf (stderr,
113                _("Error searching: %s.\n"),
114                info->value.search.specifics.error.message);
115       GNUNET_FS_search_stop (sc);      
116       break;
117     case GNUNET_FS_STATUS_SEARCH_STOPPED: 
118       GNUNET_FS_stop (ctx);
119       break;      
120     default:
121       fprintf (stderr,
122                _("Unexpected status: %d\n"),
123                info->status);
124       break;
125     }
126   return NULL;
127 }
128
129
130 /**
131  * Main function that will be run by the scheduler.
132  *
133  * @param cls closure
134  * @param sched the scheduler to use
135  * @param args remaining command-line arguments
136  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
137  * @param cfg configuration
138  */
139 static void
140 run (void *cls,
141      struct GNUNET_SCHEDULER_Handle *sched,
142      char *const *args,
143      const char *cfgfile,
144      const struct GNUNET_CONFIGURATION_Handle *c)
145 {
146   struct GNUNET_FS_Uri *uri;
147   unsigned int argc;
148
149   argc = 0;
150   while (NULL != args[argc])
151     argc++;
152   uri = GNUNET_FS_uri_ksk_create_from_args (argc,
153                                             (const char **) args);
154   if (NULL == uri)
155     {
156       fprintf (stderr,
157                _("Could not create keyword URI from arguments.\n"));
158       ret = 1;
159       GNUNET_FS_uri_destroy (uri);
160       return;
161     }
162   cfg = c;
163   ctx = GNUNET_FS_start (sched,
164                          cfg,
165                          "gnunet-search",
166                          &progress_cb,
167                          NULL,
168                          GNUNET_FS_FLAGS_NONE,
169                          GNUNET_FS_OPTIONS_END);
170   if (NULL == ctx)
171     {
172       fprintf (stderr,
173                _("Could not initialize `%s' subsystem.\n"),
174                "FS");
175       GNUNET_FS_uri_destroy (uri);
176       GNUNET_FS_stop (ctx);
177       ret = 1;
178       return;
179     }
180   sc = GNUNET_FS_search_start (ctx,
181                                uri,
182                                anonymity);
183   GNUNET_FS_uri_destroy (uri);
184   if (NULL == sc)
185     {
186       fprintf (stderr,
187                _("Could not start searching.\n"));
188       ret = 1;
189       return;
190     }
191 }
192
193
194 /**
195  * gnunet-search command line options
196  */
197 static struct GNUNET_GETOPT_CommandLineOption options[] = {
198   {'a', "anonymity", "LEVEL",
199    gettext_noop ("set the desired LEVEL of receiver-anonymity"),
200    1, &GNUNET_GETOPT_set_uint, &anonymity},
201   // FIXME: options!
202   GNUNET_GETOPT_OPTION_END
203 };
204
205
206 /**
207  * The main function to search GNUnet.
208  *
209  * @param argc number of arguments from the command line
210  * @param argv command line arguments
211  * @return 0 ok, 1 on error
212  */
213 int
214 main (int argc, char *const *argv)
215 {
216   return (GNUNET_OK ==
217           GNUNET_PROGRAM_run (argc,
218                               argv,
219                               "gnunet-search",
220                               gettext_noop
221                               ("Search GNUnet."),
222                               options, &run, NULL)) ? ret : 1;
223 }
224
225 /* end of gnunet-search.c */
226