aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/chat/chat.c
blob: de607f7fea6a33900abba6b698f97bf13ae0832f (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
/*
     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"

const GNUNET_RSA_PublicKey pub_key;
const struct GNUNET_RSA_PrivateKey *priv_key;

/**
 * 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)
{
  fprintf (stdout, "%s: %s\n", senderNick, message);
  return GNUNET_OK;
}

void
init_chat ()
{
  GtkWidget *tab;
	GtkWidget *button;
	GtkWidget *hbox;
	
	priv_key = GNUNET_RSA_create_key ();
  GNUNET_RSA_get_public_key (priv_key, &pub_key);
  
	g_print("g_print works!\n");
  hbox =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "chatHBox");
  
  
  tab =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "chatnotebookvbox");
    
  button = gtk_button_new_with_label("hello world");

  gtk_container_add(GTK_CONTAINER(tab), button);

  gtk_widget_show (tab);
}

void
on_chat_room_name_key_press_event_chat (GtkWidget *widget, GdkEventKey *event, gpointer func_data)
{
	g_print("Key pressed....\n");
}

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);
}

int
create_chat_room_tab(char *room_name,char *nick)
{
	GladeXML *chatXML;
	GtkWidget *newChatWindow;
	GtkWidget *chatnotebook;
	GtkWidget *chatLabel;
	
	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);  
  	
	GNUNET_CHAT_gui_join_room (nick,room_name,&pub_key,priv_key,&receive_callback,NULL);
	return 1;
}

int
remove_chat_room_tab(char *room_name)
{
	
	return -1;
}

/**
 * Join a chat room.
 *
 * @param nickname the nick you want to use
 * @param memberInfo public information about you
 * @param callback which function to call if a message has
 *        been received?
 * @param cls argument to callback
 * @return NULL on error
 */
struct GNUNET_CHAT_Room *
GNUNET_CHAT_gui_join_room (const char *nickname,
                       const char *room_name,
                       const GNUNET_RSA_PublicKey * me,
                       const struct GNUNET_RSA_PrivateKey *key,
                       GNUNET_CHAT_MessageCallback callback, void *cls)
{
#if 0
  CS_chat_JOIN_MESSAGE *join_msg;
  GNUNET_MessageHeader csHdr;

  GNUNET_HashCode hash_of_me;
  GNUNET_HashCode hash_of_room_name;

  struct GNUNET_CHAT_Room *chat_room;
  struct GNUNET_ClientServerConnection *sock;

  int size_of_join;

  csHdr.size = htons (sizeof (CS_chat_JOIN_MESSAGE));
  csHdr.type = htons (GNUNET_CS_PROTO_CHAT_JOIN_MSG);

  sock = GNUNET_client_connection_create (ectx, cfg);

  if (sock == NULL)
    {
      fprintf (stderr, _("Error establishing connection with gnunetd.\n"));
      return NULL;
    }

  // connect
  GNUNET_hash (me, sizeof (GNUNET_RSA_PublicKey), &hash_of_me);
  GNUNET_hash (room_name, strlen (room_name), &hash_of_room_name);

  size_of_join =
    sizeof (CS_chat_JOIN_MESSAGE) + strlen (nickname) +
    sizeof (GNUNET_RSA_PublicKey) + strlen (room_name);
  join_msg = GNUNET_malloc (size_of_join);

  join_msg->nick_len = htons (strlen (nickname));
  join_msg->pubkey_len = htons (sizeof (GNUNET_RSA_PublicKey));
  //join_msg->room_name_len = htonl (strlen (room_name));


  memcpy (&join_msg->nick[0], nickname, strlen (nickname));
  memcpy (&join_msg->nick[strlen (nickname)], me,
          sizeof (GNUNET_RSA_PublicKey));
  memcpy (&join_msg->nick[strlen (nickname) + sizeof (GNUNET_RSA_PublicKey)],
          room_name, strlen (room_name));

  join_msg->header = csHdr;
  join_msg->header.size = htons (size_of_join);

  if (GNUNET_SYSERR ==
      GNUNET_client_connection_write (sock, &join_msg->header))
    {
      /* ALREADY LOGGED */
      fprintf (stderr, _("Error writing to socket.\n"));
      GNUNET_free (join_msg);
      return NULL;
    }

  GNUNET_free (join_msg);

  // allocate & init room struct
  chat_room = GNUNET_malloc (sizeof (struct GNUNET_CHAT_Room));
  chat_room->nickname = GNUNET_malloc (strlen (nickname) + 1);
  strncpy (chat_room->nickname, nickname, strlen (nickname) + 1);

  chat_room->room_name = GNUNET_malloc (strlen (room_name) + 1);
  strncpy (chat_room->room_name, room_name, strlen (room_name) + 1);

  chat_room->room_name_hash = hash_of_room_name;
  chat_room->my_public_key = me;
  chat_room->my_public_key_hash = hash_of_me;
  chat_room->my_private_key = key;
  chat_room->callback = callback;
  chat_room->callback_cls = cls;
  chat_room->ectx = ectx;
  chat_room->cfg = cfg;
  chat_room->memberInfo = GNUNET_malloc (strlen (memberInfo) + 1);
  strncpy (chat_room->memberInfo, memberInfo, strlen (memberInfo) + 1);
  chat_room->sock = sock;

  // create pthread
  chat_room->listen_thread =
    GNUNET_thread_create (&poll_thread, chat_room, 1024 * 2);

  return chat_room;
#endif
}

/* end of chat.c */