aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/chat/chat.c
blob: 626b202e952cb9873b184e0bf61746badf3de7cc (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
     This file is part of GNUnet
     (C) 2008 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 2, 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/plugins/about/chat.c
 * @author Christian Grothoff
 * @author Igor Wronsky
 * @author Nathan Evans
 * 
 */

#include "chat.h"

GNUNET_RSA_PublicKey pub_key;
const struct GNUNET_RSA_PrivateKey *priv_key;

static struct GNUNET_ClientServerConnection *sock;

static struct GNUNET_Mutex *lock;

static struct GNUNET_CronManager *cron;

static struct GNUNET_GE_Context *chat_ectx;

static struct GNUNET_GC_Configuration *chat_cfg;

static struct GNUNET_CHAT_gui_chat_client *list;

static struct GNUNET_CHAT_member_list *room_member_list;

static void *
write_save_call (void *arg)
{
  struct GNUNET_CHAT_safe_write_struct *writeholder = arg;
  GtkTextBuffer *buffer;
  char *message_buf;
  int message_buf_size;

  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (writeholder->chatWindow));
  message_buf_size =
    snprintf (NULL, 0, "<%s> %s", writeholder->senderNick,
              writeholder->message);
  message_buf = GNUNET_malloc (message_buf_size + 2);
  snprintf (message_buf, message_buf_size + 2, "<%s> %s\n",
            writeholder->senderNick, writeholder->message);
  gtk_text_buffer_insert_at_cursor (buffer, message_buf,
                                    message_buf_size + 1);

  return NULL;
}

/* Safe call to add a nick to the chat room view */
static void *
write_nick_save_call (void *arg)
{
  struct GNUNET_CHAT_safe_nick_write_struct *writeholder = arg;

  GtkTextBuffer *buffer;
  char *message_buf;
  int message_buf_size;
  struct GNUNET_CHAT_member_list *pos = room_member_list;

  g_print ("received chat name %s\n", writeholder->nick);
  while (pos != NULL)
    {
      if (strcmp (pos->nick, writeholder->nick) == 0)
        return NULL;
      pos = pos->next;
    }

  g_print ("adding chat name %s to the list\n", writeholder->nick);

  pos = GNUNET_malloc (sizeof (struct GNUNET_CHAT_member_list));
  pos->nick = GNUNET_malloc (strlen (writeholder->nick) + 1);
  strcpy (pos->nick, writeholder->nick);
  pos->next = room_member_list;
  if (room_member_list != NULL)
    room_member_list->prev = pos;
  room_member_list = pos;
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (writeholder->nickList));
  message_buf_size = snprintf (NULL, 0, "%s", writeholder->nick);
  message_buf = GNUNET_malloc (message_buf_size + 2);
  snprintf (message_buf, message_buf_size + 2, "%s\n", writeholder->nick);
  gtk_text_buffer_insert_at_cursor (buffer, message_buf,
                                    message_buf_size + 1);

  return NULL;
}

/* Safe call to remove a nick from the chat room view */
static void *
remove_nick_save_call (void *arg)
{
  struct GNUNET_CHAT_safe_nick_write_struct *writeholder = arg;

  GtkTextBuffer *buffer =
    gtk_text_view_get_buffer (GTK_TEXT_VIEW (writeholder->nickList));
  char *message_buf;
  int message_buf_size;
  struct GNUNET_CHAT_member_list *pos = room_member_list;

  while (pos != NULL)
    {
      if (strcmp (pos->nick, writeholder->nick) == 0)
        {
          pos->prev->next = pos->next;
          pos->next->prev = pos->prev;
          GNUNET_free (pos);
          /*buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(writeholder->nickList)); */
          buffer = gtk_text_buffer_new (NULL);
          return NULL;
        }
      pos = pos->next;
    }

  return NULL;


  message_buf_size = snprintf (NULL, 0, "%s", writeholder->nick);
  message_buf = GNUNET_malloc (message_buf_size + 2);
  snprintf (message_buf, message_buf_size + 2, "%s\n", writeholder->nick);
  gtk_text_buffer_insert_at_cursor (buffer, message_buf,
                                    message_buf_size + 1);

  return NULL;
}

/**
 * A message was sent in the chat to us.
 *
 * @param timestamp when was the message sent?
 * @param senderNick what is the nickname of the sender? (maybe NULL)
 * @param message the message (maybe NULL, especially if confirmation
 *        is requested before delivery; the protocol will ensure
 *        that this function is called again with the full message
 *        if a confirmation is transmitted; if the message is NULL,
 *        the user is merely asked if engaging in the exchange is ok
 * @param room in which room was the message received?
 * @param options options for the message
 * @return GNUNET_OK to accept the message now, GNUNET_NO to
 *         accept (but user is away), GNUNET_SYSERR to signal denied delivery
 */
static int
receive_callback (void *cls,
                  struct GNUNET_CHAT_Room *room,
                  const char *senderNick,
                  const char *message,
                  GNUNET_CronTime timestamp, GNUNET_CHAT_MSG_OPTIONS options)
{
  struct GNUNET_CHAT_gui_chat_client *client = cls;
  GtkWidget *chattextviewer;

  GList *children;
  struct GNUNET_CHAT_safe_write_struct writearg;

  children = gtk_container_get_children (GTK_CONTAINER (client->chatFrame));
  chattextviewer =
    g_list_nth_data ((gtk_container_get_children
                      (GTK_CONTAINER
                       (g_list_nth_data
                        (gtk_container_get_children
                         (GTK_CONTAINER (children->data)), 0)))), 0);

  writearg.chatWindow = chattextviewer;
  writearg.message = message;
  writearg.senderNick = senderNick;
  GNUNET_GTK_save_call (&write_save_call, &writearg);

  /*Write the message to the appropriate room... 
   * Somehow G wants me to do this without knowing which room it's headed to, but I don't see how...
   * perhaps with the closure argument...*/


  return GNUNET_OK;
}

static int
member_list_callback (void *cls, const char *senderNick,
		      int is_joining,
                      GNUNET_CronTime timestamp)
{
  struct GNUNET_CHAT_gui_chat_client *client = cls;
  GtkWidget *chatnicklist;
  struct GNUNET_CHAT_safe_nick_write_struct writearg;

  chatnicklist =
    g_list_nth_data (gtk_container_get_children
                     (GTK_CONTAINER (client->chatFrame)), 1);
  chatnicklist =
    g_list_nth_data (gtk_container_get_children
                     (GTK_CONTAINER (chatnicklist)), 0);

  /* chatnicklist is the GtkTextView object */
  writearg.nick = senderNick;
  writearg.nickList = chatnicklist;
  
  g_print ("received nick %s calling safe write call\n", writearg.nick);
  if (is_joining)
    GNUNET_GTK_save_call (&write_nick_save_call, &writearg);
  else
    GNUNET_GTK_save_call (&remove_nick_save_call, &writearg);

  return GNUNET_OK;
}

void
init_chat (struct GNUNET_GE_Context *ectx,
           struct GNUNET_GC_Configuration *cfg)
{
  GtkWidget *tab;

  chat_ectx = ectx;
  chat_cfg = cfg;
  room_member_list = NULL;

  /*GNUNET_disable_entropy_gathering (); */
  /* FIXME: yes G, read key from disk */
  priv_key = GNUNET_RSA_create_key ();
  GNUNET_RSA_get_public_key (priv_key, &pub_key);

  sock = GNUNET_client_connection_create (chat_ectx, chat_cfg);
  lock = GNUNET_mutex_create (GNUNET_NO);
  cron = GNUNET_GTK_get_cron_manager ();

  tab =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                          "chatnotebookvbox");

  gtk_widget_show (tab);
}

void
on_chat_room_name_button_click_event_chat (GtkWidget * widget, gpointer data)
{
  GtkEntry *room_entry;
  GtkEntry *nick_entry;
  char *room_text;
  char *nick_text;

  room_entry =
    GTK_ENTRY (glade_xml_get_widget
               (GNUNET_GTK_get_main_glade_XML (), "chatRoomNameEntry"));
  nick_entry =
    GTK_ENTRY (glade_xml_get_widget
               (GNUNET_GTK_get_main_glade_XML (), "chatRoomMonikerEntry"));

  room_text = (char *) gtk_entry_get_text (room_entry);
  nick_text = (char *) gtk_entry_get_text (nick_entry);

  create_chat_room_tab (room_text, nick_text);

  gtk_entry_set_text (nick_entry, "");
  gtk_entry_set_text (room_entry, "");
}

int
create_chat_room_tab (char *room_name, char *nick)
{
  GladeXML *chatXML;
  GtkWidget *newChatWindow;
  GtkWidget *chatnotebook;
  GtkWidget *chatLabel;
  struct GNUNET_CHAT_Room *chatRoom;
  struct GNUNET_CHAT_gui_chat_client *client;
  struct GNUNET_CHAT_gui_chat_client *pos;

  chatnotebook =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "chatnotebook");
  chatXML =
    glade_xml_new (GNUNET_GTK_get_glade_filename (), "chatFrame",
                   PACKAGE_NAME);
  GNUNET_GTK_connect_glade_with_plugins (chatXML);
  newChatWindow = extractMainWidgetFromWindow (chatXML, "chatFrame");

  chatLabel = gtk_label_new (room_name);
  gtk_notebook_insert_page (GTK_NOTEBOOK (chatnotebook), newChatWindow,
                            chatLabel, 1);

  gtk_widget_show (chatnotebook);

  client = GNUNET_malloc (sizeof (struct GNUNET_CHAT_gui_chat_client));
  client->room_name = GNUNET_malloc (strlen (room_name));
  strcpy (client->room_name, room_name);


  /* We do a silly strdup later on member_info... either check for NULL or only pass "" for member_info!! */
  chatRoom =
    GNUNET_CHAT_join_room (chat_ectx, chat_cfg, nick, room_name, &pub_key,
                           priv_key, "", &receive_callback, client,
                           &member_list_callback, client);

  client->room = chatRoom;
  client->chatFrame = newChatWindow;
  client->number =
    gtk_notebook_get_current_page (GTK_NOTEBOOK (chatnotebook));

  if (list == NULL)
    list = client;
  else
    {
      pos = list;
      while (pos->next != NULL)
        {
          pos = pos->next;
        }
      pos->next = client;
    }

  return GNUNET_OK;
}

int
remove_chat_room_tab (char *room_name)
{

  return GNUNET_SYSERR;
}

void
on_chat_frame_send_button_click_event_chat (GtkWidget * widget, gpointer data)
{
  GtkWidget *chatnotebook;
  GtkWidget *chathbox;
  GtkWidget *chatlabel;
  GtkWidget *curr_page;
  GList *children;
  GtkEntry *chat_entry;

  char *message;
  const gchar *room_name;
  struct GNUNET_CHAT_gui_chat_client *client;
  struct GNUNET_CHAT_gui_chat_client *pos;

  chatnotebook = gtk_widget_get_ancestor (widget, GTK_TYPE_NOTEBOOK);
  curr_page =
    gtk_notebook_get_nth_page (GTK_NOTEBOOK (chatnotebook),
                               gtk_notebook_get_current_page (GTK_NOTEBOOK
                                                              (chatnotebook)));
  chatlabel =
    gtk_notebook_get_tab_label (GTK_NOTEBOOK (chatnotebook), curr_page);

  room_name = gtk_label_get_text (GTK_LABEL (chatlabel));

  chathbox = gtk_widget_get_ancestor (widget, GTK_TYPE_HBOX);
  children = gtk_container_get_children (GTK_CONTAINER (chathbox));

  chat_entry = NULL;
  while (children != NULL)
    {
      if (strcmp (gtk_widget_get_name (children->data), "chatLineTextEntry")
          == 0)
        {
          chat_entry = GTK_ENTRY (children->data);
        }
      children = children->next;
    }

  message = (char *) gtk_entry_get_text (chat_entry);

  /* Almost there... need to find room in linked list (provided it exists!) 
   * before sending out the message...
   */
  if (list == NULL)
    return;
  else
    {
      pos = list;
      while ((pos != NULL) && (strcmp (pos->room_name, room_name) != 0))
        {
          pos = pos->next;
        }
    }
  if (pos != NULL)
    client = pos;
  else
    return;

  GNUNET_CHAT_send_message (client->room, message, NULL,
                            NULL, GNUNET_CHAT_MSG_OPTION_NONE, NULL);

  gtk_entry_set_text (chat_entry, "");

}

/* end of chat.c */