Fix bug with uninitialized variable.
[cascardo/chat.git] / chat.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <gtk/gtk.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include "xmpp.h"
28 #include "message.h"
29 #include "presence.h"
30
31 static int fd;
32 static GtkWidget *textview;
33 static GtkWidget *entry_to;
34 static GtkWidget *entry_body;
35 static GtkWidget *entry_status;
36
37 static void
38 hc_xmpp_chat (hc_xmpp_t *xmpp, iks *message)
39 {
40   GtkTextBuffer *textbuffer;
41   GtkTextIter textiter;
42   char *str;
43   char *from;
44   char *to;
45   char *body;
46   int sent = 0;
47   str = iks_string (iks_stack (message), message);
48   write (fd, str, strlen (str));
49   from = iks_find_attrib (message, "from");
50   to = iks_find_attrib (message, "to");
51   if (from == NULL)
52     sent = 1;
53   if (sent && to == NULL)
54     return;
55   body = iks_cdata (iks_child (iks_find (message, "body")));
56   textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
57   gtk_text_buffer_get_end_iter (textbuffer, &textiter);
58   if (sent)
59     {
60       gtk_text_buffer_insert (textbuffer, &textiter, "> ", -1);
61       gtk_text_buffer_insert (textbuffer, &textiter, to, -1);
62     }
63   else
64     {
65       gtk_text_buffer_insert (textbuffer, &textiter, "< ", -1);
66       gtk_text_buffer_insert (textbuffer, &textiter, from, -1);
67     }
68   gtk_text_buffer_insert (textbuffer, &textiter, ": ", -1);
69   if (body)
70     gtk_text_buffer_insert (textbuffer, &textiter, body, -1);
71   gtk_text_buffer_insert (textbuffer, &textiter, "\n", -1);
72 }
73
74 static void
75 hc_xmpp_chat_send (GtkWidget *button, gpointer data)
76 {
77   hc_xmpp_t *xmpp;
78   char *to;
79   char *body;
80   xmpp = (hc_xmpp_t *) data;
81   to = (char *) gtk_entry_get_text (GTK_ENTRY (entry_to));
82   body = (char *) gtk_entry_get_text (GTK_ENTRY (entry_body));
83   hc_xmpp_send_message (xmpp, to, body);
84 }
85
86 static void
87 hc_xmpp_chat_status (GtkWidget *button, gpointer data)
88 {
89   hc_xmpp_t* xmpp;
90   char *status;
91   xmpp = (hc_xmpp_t *) data;
92   status = (char *) gtk_entry_get_text (GTK_ENTRY (entry_status));
93   hc_xmpp_send_presence (xmpp, NULL, NULL, status);
94 }
95
96 void
97 hc_xmpp_chat_open (hc_xmpp_t *xmpp)
98 {
99   GtkWidget *window;
100   GtkWidget *vbox;
101   GtkWidget *hbox;
102   GtkWidget *hbox2;
103   GtkWidget *button;
104   GtkWidget *button2;
105   fd = open ("/home/cascardo/.hcxmpp/log", O_RDWR | O_APPEND | O_CREAT);
106   if (fd < 0)
107     {
108       fprintf (stderr, "Could not open log file: %s\n", strerror (errno));
109       abort ();
110     }
111   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
112   vbox = gtk_vbox_new (FALSE, 5);
113   hbox = gtk_hbox_new (FALSE, 5);
114   hbox2 = gtk_hbox_new (FALSE, 5);
115   entry_to = gtk_entry_new ();
116   entry_body = gtk_entry_new ();
117   entry_status = gtk_entry_new ();
118   button = gtk_button_new_with_label ("Send");
119   g_signal_connect (G_OBJECT (button), "clicked",
120                     G_CALLBACK (hc_xmpp_chat_send), xmpp);
121   button2 = gtk_button_new_with_label ("Change Status");
122   g_signal_connect (G_OBJECT (button), "clicked",
123                     G_CALLBACK (hc_xmpp_chat_status), xmpp);
124   textview = gtk_text_view_new ();
125   gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (textview), GTK_WRAP_WORD_CHAR);
126   gtk_container_add (GTK_CONTAINER (window), vbox);
127   gtk_container_add (GTK_CONTAINER (vbox), textview);
128   gtk_container_add (GTK_CONTAINER (vbox), hbox);
129   gtk_container_add (GTK_CONTAINER (vbox), hbox2);
130   gtk_container_add (GTK_CONTAINER (hbox), entry_to);
131   gtk_container_add (GTK_CONTAINER (hbox), entry_body);
132   gtk_container_add (GTK_CONTAINER (hbox), button);
133   gtk_container_add (GTK_CONTAINER (hbox2), entry_status);
134   gtk_container_add (GTK_CONTAINER (hbox2), button2);
135   gtk_widget_show_all (window);
136   hc_xmpp_set_msg_hook (xmpp, hc_xmpp_chat);
137   hc_xmpp_set_sent_msg_hook (xmpp, hc_xmpp_chat);
138 }