aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-conversation-gtk_import.c
blob: 428894d78e3df1840c4147ed2f03ded14db860a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
     This file is part of GNUnet.
     (C) 2010-2014 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/conversation/gnunet-conversation-gtk_import.c
 * @brief handle request to import caller into address book
 * @author yids
 * @author hark
 * @author Christian Grothoff
 */
#include "gnunet-conversation-gtk.h"
#include "gnunet-conversation-gtk_contacts.h"
#include "gnunet-conversation-gtk_egos.h"

/**
 * Handle to the namestore.
 */
static struct GNUNET_NAMESTORE_Handle *ns;

/**
 * Queue entry for the 'add' operation.
 */
static struct GNUNET_NAMESTORE_QueueEntry *add_qe;


/**
 * Continuation called to notify client about result of the
 * operation.
 *
 * @param cls NULL
 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
 *                #GNUNET_NO if content was already there
 *                #GNUNET_YES (or other positive value) on success
 * @param emsg NULL on success, otherwise an error message
 */
static void
add_continuation (void *cls,
                  int32_t success,
                  const char *emsg)
{
  if (GNUNET_YES != success)
    GCG_log (_("Adding contact failed: %s\n"),
             (GNUNET_NO == success) ? _("record exists") : emsg);
  add_qe = NULL;
}


/**
 * Add new contact to the address book.
 *
 * @param name name to use for the contact
 * @param address public key of the contact
 */
void
GSC_add_contact (const gchar *name,
                 const gchar *address)
{
  struct GNUNET_GNSRECORD_Data rd;
  struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  struct GNUNET_IDENTITY_Ego *ego;
  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_pkey;

  if (NULL != add_qe)
  {
    GCG_log (_("Adding contact failed: %s\n"),
             _("previous operation still pending"));
    return;
  }

  // FIXME: what should really be the preferred address format here?
  // (CNAME? PKEY? PHONE?)
  if (GNUNET_OK !=
      GNUNET_CRYPTO_ecdsa_public_key_from_string (address,
                                                  strlen (address),
                                                  &pkey))
  {
    GCG_log (_("Adding contact failed: %s\n"),
             _("invalid address"));
    return;
  }
  rd.data = &pkey;
  rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
  rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  rd.flags
    = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION
    | GNUNET_GNSRECORD_RF_PRIVATE;
  rd.expiration_time
    = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  ego = GCG_EGOS_get_selected_ego ();
  zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
  /* FIXME: we should check if the 'name' already exists,
     and not do this if it does... */
  add_qe
    =  GNUNET_NAMESTORE_records_store (ns,
                                       zone_pkey,
                                       name,
                                       1, &rd,
                                       &add_continuation, NULL);
}


/**
 * Initialize the import subsystem.
 */
void
GCG_IMPORT_init ()
{
  ns = GNUNET_NAMESTORE_connect (GCG_get_configuration ());
}


/**
 * Shutdown the import subsystem.
 */
void
GCG_IMPORT_shutdown ()
{
  if (NULL != add_qe)
  {
    GNUNET_NAMESTORE_cancel (add_qe);
    add_qe = NULL;
  }
  if (NULL != ns)
  {
    GNUNET_NAMESTORE_disconnect (ns);
    ns = NULL;
  }
}

/* end of gnunet-conversation-gtk_import.c */