aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/util/Makefile.am32
-rw-r--r--src/util/gnunet-qr.c314
-rwxr-xr-xsrc/util/gnunet-qr.in46
-rw-r--r--src/util/gnunet-qr.py110
4 files changed, 328 insertions, 174 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index cd14fb4ca..89d0462c5 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -204,26 +204,15 @@ libexec_PROGRAMS = \
204 gnunet-timeout \ 204 gnunet-timeout \
205 $(W32CONSOLEHELPER) 205 $(W32CONSOLEHELPER)
206 206
207do_subst = $(SED) -e 's,[@]PREFIX[@],$(prefix),g'
208
209gnunet-qr: gnunet-qr.in Makefile
210 $(do_subst) < $(srcdir)/gnunet-qr.in > gnunet-qr
211 chmod +x gnunet-qr
212
213CLEANFILES = gnunet-qr
214
215pkgdata_DATA = \
216 gnunet-qr.py
217
218bin_SCRIPTS =\
219 gnunet-qr
220
221bin_PROGRAMS = \ 207bin_PROGRAMS = \
222 gnunet-resolver \ 208 gnunet-resolver \
223 gnunet-config \ 209 gnunet-config \
224 $(GNUNET_ECC) \ 210 $(GNUNET_ECC) \
225 $(GNUNET_SCRYPT) \ 211 $(GNUNET_SCRYPT) \
226 gnunet-uri 212 gnunet-uri
213if HAVE_ZBAR
214bin_PROGRAMS += gnunet-qr
215endif
227 216
228noinst_PROGRAMS = \ 217noinst_PROGRAMS = \
229 gnunet-config-diff \ 218 gnunet-config-diff \
@@ -283,13 +272,22 @@ gnunet_config_LDADD = \
283 libgnunetutil.la \ 272 libgnunetutil.la \
284 $(GN_LIBINTL) 273 $(GN_LIBINTL)
285 274
286
287gnunet_uri_SOURCES = \ 275gnunet_uri_SOURCES = \
288 gnunet-uri.c 276 gnunet-uri.c
289gnunet_uri_LDADD = \ 277gnunet_uri_LDADD = \
290 libgnunetutil.la \ 278 libgnunetutil.la \
291 $(GN_LIBINTL) 279 $(GN_LIBINTL)
292 280
281
282gnunet_qr_SOURCES = \
283 gnunet-qr.c \
284 gnunet-qr-utils.h
285gnunet_qr_LDADD = \
286 libgnunetutil.la \
287 $(GN_LIBINTL)
288gnunet_qr_LDFLAGS= $(libzbar_LIBS)
289gnunet_qr_CFLAGS = $(libzbar_CFLAGS) -DBINDIR=\"@bindir@/\"
290
293plugin_LTLIBRARIES = \ 291plugin_LTLIBRARIES = \
294 libgnunet_plugin_test.la 292 libgnunet_plugin_test.la
295 293
@@ -670,6 +668,4 @@ EXTRA_DIST = \
670 test_program_data.conf \ 668 test_program_data.conf \
671 test_resolver_api_data.conf \ 669 test_resolver_api_data.conf \
672 test_service_data.conf \ 670 test_service_data.conf \
673 test_speedup_data.conf \ 671 test_speedup_data.conf
674 gnunet-qr.in \
675 gnunet-qr.py
diff --git a/src/util/gnunet-qr.c b/src/util/gnunet-qr.c
new file mode 100644
index 000000000..fa95d6c05
--- /dev/null
+++ b/src/util/gnunet-qr.c
@@ -0,0 +1,314 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013-2019 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19*/
20/**
21 * @file util/gnunet-qr.c
22 * @author Hartmut Goebel (original implementation)
23 * @author Martin Schanzenbach (integrate gnunet-uri)
24 * @author Christian Grothoff (error handling)
25 */
26#include <stdio.h>
27#include <zbar.h>
28#include <stdbool.h>
29#include "platform.h"
30#include "gnunet_util_lib.h"
31
32#define LOG(fmt, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
33
34/**
35 * Video device to capture from. Sane default for GNU/Linux systems.
36 */
37static char* device = "/dev/video0";
38
39/**
40 * --verbose option
41 */
42static int verbose = false;
43
44/**
45 * --silent option
46 */
47static int silent = false;
48
49/**
50 * Handler exit code
51 */
52static long unsigned int exit_code = 1;
53
54/**
55 * Helper process we started.
56 */
57static struct GNUNET_OS_Process *p;
58
59
60/**
61 * Pipe used to communicate child death via signal.
62 */
63static struct GNUNET_DISK_PipeHandle *sigpipe;
64
65
66/**
67 * Task triggered whenever we receive a SIGCHLD (child
68 * process died) or when user presses CTRL-C.
69 *
70 * @param cls closure, NULL
71 */
72static void
73maint_child_death (void *cls)
74{
75 enum GNUNET_OS_ProcessStatusType type;
76
77 if ( (GNUNET_OK !=
78 GNUNET_OS_process_status (p, &type, &exit_code)) ||
79 (type != GNUNET_OS_PROCESS_EXITED) )
80 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
81 GNUNET_OS_process_destroy (p);
82}
83
84
85/**
86 * Dispatch URIs to the appropriate GNUnet helper process
87 *
88 * @param cls closure
89 * @param uri uri to dispatch
90 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
91 * @param cfg configuration
92 */
93static void
94gnunet_uri (void *cls,
95 const char *uri,
96 const char *cfgfile,
97 const struct GNUNET_CONFIGURATION_Handle *cfg)
98{
99 const char *orig_uri;
100 const char *slash;
101 char *subsystem;
102 char *program;
103 struct GNUNET_SCHEDULER_Task * rt;
104
105 orig_uri = uri;
106 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) {
107 fprintf (stderr,
108 _("Invalid URI: does not start with `%s'\n"),
109 "gnunet://");
110 return;
111 }
112 uri += strlen ("gnunet://");
113 if (NULL == (slash = strchr (uri, '/')))
114 {
115 fprintf (stderr,
116 _("Invalid URI: fails to specify subsystem\n"));
117 return;
118 }
119 subsystem = GNUNET_strndup (uri, slash - uri);
120 if (GNUNET_OK !=
121 GNUNET_CONFIGURATION_get_value_string (cfg,
122 "uri",
123 subsystem,
124 &program))
125 {
126 fprintf (stderr,
127 _("No handler known for subsystem `%s'\n"),
128 subsystem);
129 GNUNET_free (subsystem);
130 return;
131 }
132 GNUNET_free (subsystem);
133 rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
134 GNUNET_DISK_pipe_handle (sigpipe,
135 GNUNET_DISK_PIPE_END_READ),
136 &maint_child_death, NULL);
137 p = GNUNET_OS_start_process (GNUNET_NO, 0,
138 NULL, NULL, NULL,
139 program,
140 program,
141 orig_uri,
142 NULL);
143 GNUNET_free (program);
144 if (NULL == p)
145 GNUNET_SCHEDULER_cancel (rt);
146}
147
148
149/**
150 * Obtain QR code 'symbol' from @a proc.
151 *
152 * @param proc zbar processor to use
153 * @return NULL on error
154 */
155static const zbar_symbol_t *
156get_symbol (zbar_processor_t *proc)
157{
158 const zbar_symbol_set_t* symbols;
159 int rc;
160 int n;
161
162 if (0 !=
163 zbar_processor_parse_config (proc, "enable"))
164 {
165 GNUNET_break (0);
166 return NULL;
167 }
168
169 /* initialize the Processor */
170 if (0 !=
171 (rc = zbar_processor_init(proc, device, 1)))
172 {
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 "Failed to open device `%s': %d\n",
175 device,
176 rc);
177 return NULL;
178 }
179
180 /* enable the preview window */
181 if ( (0 != (rc = zbar_processor_set_visible (proc, 1))) ||
182 (0 != (rc = zbar_processor_set_active(proc, 1))) )
183 {
184 GNUNET_break (0);
185 return NULL;
186 }
187
188 /* read at least one barcode (or until window closed) */
189 LOG ("Capturing\n");
190 n = zbar_process_one (proc, -1);
191
192 /* hide the preview window */
193 (void) zbar_processor_set_active (proc, 0);
194 (void) zbar_processor_set_visible (proc, 0);
195 if (-1 == n)
196 return NULL; /* likely user closed the window */
197 LOG ("Got %i images\n",
198 n);
199 /* extract results */
200 symbols = zbar_processor_get_results (proc);
201 if (NULL == symbols)
202 {
203 GNUNET_break (0);
204 return NULL;
205 }
206 return zbar_symbol_set_first_symbol (symbols);
207}
208
209
210/**
211 * Run zbar QR code parser.
212 *
213 * @return NULL on error, otherwise the URI that we found
214 */
215static char *
216run_zbar ()
217{
218 zbar_processor_t *proc;
219 const char *data;
220 char *ret;
221 const zbar_symbol_t* symbol;
222
223 /* configure the Processor */
224 proc = zbar_processor_create (1);
225 if (NULL == proc)
226 {
227 GNUNET_break (0);
228 return NULL;
229 }
230
231 symbol = get_symbol (proc);
232 if (NULL == symbol)
233 {
234 zbar_processor_destroy (proc);
235 return NULL;
236 }
237 data = zbar_symbol_get_data (symbol);
238 if (NULL == data)
239 {
240 GNUNET_break (0);
241 zbar_processor_destroy (proc);
242 return NULL;
243 }
244 LOG ("Found %s \"%s\"\n",
245 zbar_get_symbol_name (zbar_symbol_get_type(symbol)),
246 data);
247 ret = GNUNET_strdup (data);
248 /* clean up */
249 zbar_processor_destroy(proc);
250 return ret;
251}
252
253
254/**
255 * Main function that will be run by the scheduler.
256 *
257 * @param cls closure
258 * @param args remaining command-line arguments
259 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
260 * @param cfg configuration
261 */
262static void
263run (void *cls,
264 char *const *args,
265 const char *cfgfile,
266 const struct GNUNET_CONFIGURATION_Handle *cfg)
267{
268 char *data;
269
270 data = run_zbar ();
271 if (NULL == data)
272 return;
273 gnunet_uri (cls,
274 data,
275 cfgfile,
276 cfg);
277 if (exit_code != 0)
278 {
279 printf ("Failed to add URI %s\n",
280 data);
281 }
282 else
283 {
284 printf ("Added URI %s\n",
285 data);
286 }
287 GNUNET_free (data);
288};
289
290
291int
292main (int argc, char *const *argv)
293{
294 int ret;
295 struct GNUNET_GETOPT_CommandLineOption options[] = {
296 GNUNET_GETOPT_option_string ('d', "device", "DEVICE",
297 gettext_noop ("use video-device DEVICE (default: /dev/video0"),
298 &device),
299 GNUNET_GETOPT_option_flag ('\0', "verbose",
300 gettext_noop ("be verbose"),
301 &verbose),
302 GNUNET_GETOPT_option_flag ('s', "silent",
303 gettext_noop ("do not show preview windows"),
304 &silent),
305 GNUNET_GETOPT_OPTION_END
306 };
307
308 ret = GNUNET_PROGRAM_run (argc,
309 argv,
310 "gnunet-qr",
311 gettext_noop ("Scan a QR code using a video device and import the uri read"),
312 options, &run, NULL);
313 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
314}
diff --git a/src/util/gnunet-qr.in b/src/util/gnunet-qr.in
deleted file mode 100755
index ce7a19b69..000000000
--- a/src/util/gnunet-qr.in
+++ /dev/null
@@ -1,46 +0,0 @@
1#!/bin/sh
2#
3# From curl's buildconf, making this script subject to the
4# curl license: https://curl.haxx.se/docs/copyright.html
5# Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
6# Copyright (C) 2019 GNUnet e.V.
7
8# findtool works like which without relying on which (which is a problem
9# for some limited shells.
10findtool(){
11 file="$1"
12
13 if { echo "$file" | grep "/" >/dev/null 2>&1; } then
14 # when file is given with a path check it first
15 if test -f "$file"; then
16 echo "$file"
17 return
18 fi
19 fi
20
21 old_IFS=$IFS; IFS=':'
22 for path in $PATH
23 do
24 IFS=$old_IFS
25 # echo "checks for $file in $path" >&2
26 if test "$path" -a "$path" != '.' -a -f "$path/$file"; then
27 echo "$path/$file"
28 return
29 fi
30 done
31 IFS=$old_IFS
32}
33
34# end curl licensed code
35pythonize=`findtool python2.7 2>/dev/null`
36if test ! -x "$pythonize"; then
37 pythonize=`findtool ${PYTHON2:-python2.7}`
38fi
39
40if test -z "$pythonize"; then
41 echo "ERROR: python2.7 not found."
42 echo " You need python2.7 installed."
43 exit 1
44fi
45
46${pythonize} @PREFIX@/share/gnunet/gnunet-qr.py $@
diff --git a/src/util/gnunet-qr.py b/src/util/gnunet-qr.py
deleted file mode 100644
index 0ee0b9507..000000000
--- a/src/util/gnunet-qr.py
+++ /dev/null
@@ -1,110 +0,0 @@
1import sys
2import getopt
3import subprocess
4from sys import argv
5try:
6 import zbar
7except ImportError as e:
8 print('Cannot run gnunet-qr, please install the zbar module.')
9 print('For Debian, you can obtain it as "python-zbar".')
10 print('Upstream: http://zbar.sourceforge.net/')
11 sys.exit(1)
12
13
14def help():
15 print('gnunet-qr\n\
16Scan a QR code using a video device and import\n\
17Arguments mandatory for long options are also mandatory for short options.\n\
18 -c, --config FILENAME use configuration file FILENAME\n\
19 -d, --device DEVICE use device DEVICE\n\
20 -s, --silent do not show preview windows\n\
21 -h, --help print this help\n\
22 -v, --verbose be verbose\n\
23Report bugs to gnunet-developers@gnu.org.\n\
24GNUnet home page: https://gnunet.org/\n\
25General help using GNU software: https://www.gnu.org/gethelp/')
26
27
28if __name__ == '__main__':
29 configuration = ''
30 device = '/dev/video0'
31 url = ''
32 verbose = False
33 silent = False
34 # Parse arguments
35 try:
36 opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
37 except getopt.GetoptError as e:
38 help()
39 print(str(e))
40 exit(1)
41 for o, a in opts:
42 if o in ("-h", "--help"):
43 help()
44 sys.exit(0)
45 elif o in ("-c", "--config"):
46 configuration = a
47 elif o in ("-d", "--device"):
48 device = a
49 elif o in ("-s", "--silent"):
50 silent = True
51 elif o in ("-v", "--verbose"):
52 verbose = True
53 if (True == verbose):
54 print('Initializing')
55 # create a Processor
56 proc = zbar.Processor()
57
58 # configure the Processor
59 proc.parse_config('enable')
60
61 # initialize the Processor
62 try:
63 if (True == verbose):
64 print('Opening video device ' + device)
65 proc.init(device)
66 except Exception as e:
67 print('Failed to open device ' + device)
68 exit(1)
69
70 # enable the preview window
71 # if (True == silent):
72 # proc.visible = True
73 # else:
74 # proc.visible = False
75
76 proc.visible = True
77 # read at least one barcode (or until window closed)
78 try:
79 if (True == verbose):
80 print('Capturing')
81 proc.process_one()
82 except Exception as e:
83 # Window was closed without finding code
84 exit(1)
85
86 # hide the preview window
87 proc.visible = False
88
89 # extract results
90 for symbol in proc.results:
91 # do something useful with results
92 if (True == verbose):
93 print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
94 args = list()
95 args.append("gnunet-uri")
96 if (configuration != ''):
97 args.append(str("-c " + str(configuration)))
98 args.append(str(symbol.data))
99 cmd = ''
100 for a in args:
101 cmd += " " + str(a)
102 if (verbose):
103 print('Running `' + cmd +'`')
104 res = subprocess.call(args)
105 if (0 != res):
106 print('Failed to add URI ' + str(symbol.data))
107 else:
108 print('Added URI ' + str(symbol.data))
109 exit(res)
110 exit(1)