[go: up one dir, main page]

File: send.c

package info (click to toggle)
linpopup 0.9.3.pre4-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 440 kB
  • ctags: 151
  • sloc: ansic: 2,102; sh: 197; makefile: 171
file content (404 lines) | stat: -rw-r--r-- 12,648 bytes parent folder | download
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
403
404
/* LinPopUp - A Linux enhanced port of Winpopup, running over Samba.
 * By Jean-Marc Jacquet <littleigloo@chez.com> 
 * Copyright (C) 1998 Little Igloo Org  http://www.chez.com/littleigloo
 *
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "protos.h"


static GtkWidget *msg_file_sel;



static int
samba_post (gchar * header_text, char *temp_filename)
{
  FILE *output;
  gchar command_line[2048];
  gchar buf_line[80];
  int i;

  /* -- launch smbclient and read output from pipe -- */
  sprintf (command_line, "cat %s | smbclient -M \"%s\"", temp_filename, header_text);
  output = popen (command_line, "r");
  i = fread (command_line, 1, sizeof (command_line), output);
  pclose (output);
  command_line[i] = 0;

  /* -- sommaire investigation of the output -- */
  if (strstr (command_line, "nknown host"))
    {
      sprintf (buf_line, "\n\nUnknown host : %s", header_text);
      alert_ok ("Error", buf_line, "ok");
      return FALSE;
    }
  if (strstr (command_line, "error connecting to"))
    {
      sprintf (buf_line, "\n\nHost unreachable: %s", header_text);
      alert_ok ("Error", buf_line, "ok");
      return FALSE;
    }
  if (strstr (command_line, "Control-D\nsent") && strstr (command_line, "bytes (status was"))
    {
      alert_ok ("LinPopUp", "\n\nMessage succesfully sent.", "ok");
      return TRUE;
    }

  alert_ok_text (command_line);
  return TRUE;
}



void
send_send (GtkWidget * widget, gpointer * data)
{
  GtkWidget *send_text;
  GtkWidget *send_header;
  gchar *header_text;
  gchar message_text[2048];
  guint message_length;
  char IS_TRUNCATED = FALSE;
  char *temp_filename;
  int file_handle_temp;


/*-- retrieving send_header and send_text data previously marqued with gtk_object_set_data(...) --*/
  send_header = gtk_object_get_data (GTK_OBJECT (send_window), "send_header");
  send_text = gtk_object_get_data (GTK_OBJECT (send_window), "text");

  /* -- retrieving header text, and checking for a  destination -- */
  if (!strlen (header_text = gtk_entry_get_text (GTK_ENTRY (send_header))))
    {
      alert_ok (" Error", "\nPlease enter a destination.", "Ok");
      return;
    }

  /* -- retrieving message text, cut big texts in parts -- */
  message_length = gtk_text_get_length (GTK_TEXT (send_text));
  if (message_length > 1500)
    {
      IS_TRUNCATED = TRUE;
      strcpy (message_text, gtk_editable_get_chars (GTK_EDITABLE (send_text), 0, 1500));
      message_length = 1500;
      while (message_text[message_length] != 10)
	--message_length;
      message_text[message_length] = 0;
      strcat (message_text, "\n=== TO BE CONTINUED ===");

      gtk_text_freeze (GTK_TEXT (send_text));
      gtk_text_set_point (GTK_TEXT (send_text), message_length);
      gtk_text_backward_delete (GTK_TEXT (send_text), message_length);
      gtk_text_set_point (GTK_TEXT (send_text), 0);

      gtk_text_thaw (GTK_TEXT (send_text));
    }
  else
    {
      strcpy (message_text, gtk_editable_get_chars (GTK_EDITABLE (send_text), 0, -1));
    }

  /* -- write message text to a tempory file -- */
  temp_filename = tmpnam (NULL);
  file_handle_temp = open (temp_filename, O_RDWR | O_CREAT | O_TRUNC, DATA_PERM);
  write (file_handle_temp, message_text, strlen (message_text));
  close (file_handle_temp);

  /* -- cowardly posting it via samba, and close the send window in many case-- */
  if (samba_post (header_text, temp_filename) && !IS_TRUNCATED)
    gtk_widget_destroy (send_window);

  /* -- then remove the tempory file -- */
  unlink (temp_filename);
}



static void
send_cancel (GtkWidget * widget, gpointer * data)
{
  GtkWidget *send_text;

  /*-- retrieving send_text data previously marqued with gtk_object_set_data(...) --*/
  send_text = gtk_object_get_data (GTK_OBJECT (send_window), "text");

  /* -- if a message was entered, asking for confirmation -- */
  if (gtk_text_get_length (GTK_TEXT (send_text)))
    if (alert_dialog ("", "\nA new message was entered.\n\nCancel anyway ?", "Yes", "No", 2) == 2)
      return;

  gtk_widget_destroy (send_window);
}




static void
message_load_ok (GtkWidget * widget, GtkFileSelection * file_sel)
{
  char *filename;

  filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_sel));
  if (filename != NULL)
    {
      FILE *in;
      char buf[1024];

      in = fopen (filename, "r");
      if (in == NULL)
	{
	  sprintf (buf, "\n    Unable to Open '%s'    \n", filename);
	  alert_ok ("File Error", buf, "ok");
	}
      else
	{
	  GtkWidget *send_text;

	  send_text = gtk_object_get_data (GTK_OBJECT (send_window), "text");
	  gtk_text_freeze (GTK_TEXT (send_text));
	  while (fgets (buf, 1024, in))
	    {
	      gtk_text_insert (GTK_TEXT (send_text), NULL, &send_text->style->black, NULL, buf, strlen (buf));
	    }
	  gtk_text_thaw (GTK_TEXT (send_text));
	  fclose (in);
	}
    }

  gtk_widget_destroy (GTK_WIDGET (file_sel));
  msg_file_sel = NULL;
}



static void
message_load_cancel (GtkWidget * widget, GtkFileSelection * file_sel)
{
  gtk_widget_destroy (GTK_WIDGET (file_sel));
  msg_file_sel = NULL;
}



void
message_load (GtkWidget * widget, gpointer data)
{
  if (msg_file_sel == NULL)
    {
      msg_file_sel = gtk_file_selection_new ("Load Message text...");
      gtk_signal_connect (GTK_OBJECT (msg_file_sel), "destroy",
			GTK_SIGNAL_FUNC (gtk_widget_destroy), msg_file_sel);
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (msg_file_sel)->ok_button), "clicked",
			  GTK_SIGNAL_FUNC (message_load_ok), msg_file_sel);
      gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (msg_file_sel)->cancel_button), "clicked",
		       GTK_SIGNAL_FUNC (message_load_cancel), msg_file_sel);
    }

  if (GTK_WIDGET_VISIBLE (msg_file_sel))
    return;
  else
    gtk_widget_show (msg_file_sel);
}



void
reply_message (void)
{

  if (current_showed_message)
    {
      is_it_a_reply = TRUE;
      send_message ();
    }
}



void
send_message (void)
{
  GtkWidget *send_vbox;
  GtkWidget *send_statusbar;
  GtkWidget *send_frame;
  GtkWidget *send_table;
  GtkWidget *send_button_send;
  GtkWidget *send_button_cancel;
  GtkWidget *send_header;
  GtkWidget *send_text;
  int send_context_id;
  gint x, y;


  msg_file_sel = NULL;

  /* -- First, the window -- */
  send_window = gtk_window_new (GTK_WINDOW_DIALOG);
  gtk_widget_set_usize (GTK_WIDGET (send_window), 425, 400);
  gtk_widget_realize (send_window);
  gtk_window_set_title (GTK_WINDOW (send_window), "LinPopUp");
  gtk_container_border_width (GTK_CONTAINER (send_window), 1);
  gdk_window_get_origin (window->window, &x, &y);
  gtk_widget_set_uposition (send_window, x, y);

  /* -- Vbox as container -- */
  send_vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (send_window), send_vbox);
  gtk_widget_show (send_vbox);

  /* -- Top frame first packed to the vbox -- */
  send_frame = gtk_frame_new (NULL);
  gtk_frame_set_label (GTK_FRAME (send_frame), " Send to :  ");
  gtk_container_border_width (GTK_CONTAINER (send_frame), 2);
  gtk_frame_set_shadow_type (GTK_FRAME (send_frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start (GTK_BOX (send_vbox), send_frame, FALSE, TRUE, 0);

  /* -- Creating frame's widgets -- */
  send_header = gtk_entry_new ();
  gtk_entry_set_editable (GTK_ENTRY (send_header), 1);
  gtk_object_set_data (GTK_OBJECT (send_window), "send_header", send_header);
  gtk_widget_show (send_header);

  send_button_send = gtk_button_new_with_label ("Send");
  gtk_signal_connect (GTK_OBJECT (send_button_send), "clicked",
		      GTK_SIGNAL_FUNC (send_send), NULL);
  gtk_widget_show (send_button_send);

  send_button_cancel = gtk_button_new_with_label ("Cancel");
  gtk_signal_connect (GTK_OBJECT (send_button_cancel), "clicked",
		      GTK_SIGNAL_FUNC (send_cancel), NULL);
  gtk_widget_show (send_button_cancel);

/*-- attaching frame's widget to a table, and packing it to the frame --*/
  send_table = gtk_table_new (2, 4, TRUE);
  gtk_table_set_col_spacings (GTK_TABLE (send_table), 5);
  gtk_table_set_row_spacings (GTK_TABLE (send_table), 5);
  gtk_container_border_width (GTK_CONTAINER (send_table), 10);
  gtk_table_attach (GTK_TABLE (send_table), send_header, 0, 3, 0, 1,
		    GTK_FILL,
		    GTK_FILL, 0, 0);
  gtk_table_attach (GTK_TABLE (send_table), send_button_send, 3, 4, 0, 1,
		    GTK_FILL,
		    GTK_FILL, 0, 0);
  gtk_table_attach (GTK_TABLE (send_table), send_button_cancel, 3, 4, 1, 2,
		    GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
  gtk_container_add (GTK_CONTAINER (send_frame), send_table);
  gtk_widget_show (send_table);
  gtk_widget_show (send_frame);

  /* -- create a menu below the top frame via create_send_menu defined in menu.c-- */
  create_send_menu (send_vbox);

  /* -- and a text area via create_text defined in text.c -- */
  send_text = create_text (send_vbox);
  gtk_text_set_editable (GTK_TEXT (send_text), 1);
  gtk_object_set_data (GTK_OBJECT (send_window), "text", send_text);

  /* -- and finally a statusbar -- */
  send_statusbar = gtk_statusbar_new ();
  gtk_box_pack_start (GTK_BOX (send_vbox), send_statusbar, FALSE, TRUE, 0);
  gtk_widget_show (send_statusbar);
  send_context_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (send_statusbar), " ");

  /* -- show the window -- */
  gtk_widget_show (send_window);

  /* -- connecting the send_cancel function to the delete event -- */
  gtk_signal_connect (GTK_OBJECT (send_window), "delete_event", GTK_SIGNAL_FUNC (send_cancel), NULL);


  /* -- fill the deliver adress if required -- */
  switch (user_rc.deliver)
    {
    case BLANK:
      break;
    case DHOST:
      gtk_entry_set_text (GTK_ENTRY (send_header), user_rc.deliver_adr);
      gtk_editable_select_region (GTK_EDITABLE (send_header), 0, -1);
      break;
    case GUESS:
      if (!strcmp (user_rc.guess_adr, "?"))
	gtk_entry_set_text (GTK_ENTRY (send_header), user_rc.deliver_adr);
      else
	gtk_entry_set_text (GTK_ENTRY (send_header), user_rc.guess_adr);
      gtk_editable_select_region (GTK_EDITABLE (send_header), 0, -1);
      break;
    default:
      break;
    }


  /* -- If this is a reply, fill the destination and text field -- */
  if (is_it_a_reply)
    {
      struct linpopup_message this_message;
      char buf[2045];
      int char_pos_text, char_pos_buf;
      GdkColormap *colormap;

      colormap = gtk_widget_get_colormap (send_window);
      gdk_color_alloc (colormap, &user_rc.quoted_text_col);

      read_message (current_showed_message, &this_message);
      gtk_entry_set_text (GTK_ENTRY (send_header), this_message.host);	//We answer to a host not to a user

      sprintf (buf, "%c %s \n%c ---\n", user_rc.quote, user_rc.quote_string, user_rc.quote);
      strexpand (buf, "HOST", this_message.host);
      strexpand (buf, "USER", this_message.sender);

      gtk_text_freeze (GTK_TEXT (send_text));
      gtk_text_insert (GTK_TEXT (send_text), NULL, &user_rc.quoted_text_col, NULL, buf, -1);

      char_pos_buf = 0;
      buf[char_pos_buf++] = user_rc.quote;
      buf[char_pos_buf++] = 32;

      for (char_pos_text = 0; char_pos_text < strlen (this_message.text); char_pos_text++)
	{
	  buf[char_pos_buf++] = this_message.text[char_pos_text];
	  if (this_message.text[char_pos_text] == 10)
	    {
	      buf[char_pos_buf++] = user_rc.quote;
	      buf[char_pos_buf++] = 32;
	    }
	}

      buf[char_pos_buf++] = 10;
      buf[char_pos_buf++] = user_rc.quote;
      buf[char_pos_buf] = 0;

      /* -- quoted text appears in user defined color -- */
      strcat (buf, " ---");
      gtk_text_insert (GTK_TEXT (send_text), NULL, &user_rc.quoted_text_col, NULL, buf, -1);
      gtk_text_insert (GTK_TEXT (send_text), NULL, &send_text->style->black, NULL, "\n", -1);

      gtk_text_thaw (GTK_TEXT (send_text));
      gtk_text_set_editable (GTK_TEXT (send_text), 1);
      gtk_widget_grab_focus (send_text);

      is_it_a_reply = FALSE;
    }
}