aboutsummaryrefslogtreecommitdiff
path: root/src/rpop3.c
blob: 311183dc8ca693dc266f2bce16da90ed3a802026 (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/*
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 1999-2008 Hiroyuki Yamamoto
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "defs.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkhbbox.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkstock.h>
#include <gtk/gtktreemodel.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtktreeview.h>
#include <gtk/gtktreeselection.h>
#include <gtk/gtkcellrenderertext.h>
#include <string.h>

#include "rpop3.h"
#include "mainwindow.h"
#include "folderview.h"
#include "prefs_account.h"
#include "pop.h"
#include "procheader.h"
#include "procmsg.h"
#include "folder.h"
#include "inc.h"
#include "utils.h"
#include "gtkutils.h"
#include "manage_window.h"
#include "alertpanel.h"
#include "prefs_common.h"

#define POP3_TOP	(N_POP3_STATE + 1000)
#define POP3_TOP_RECV	(N_POP3_STATE + 1001)
#define POP3_IDLE	(N_POP3_STATE + 1002)

enum
{
	COL_NUMBER,
	COL_SUBJECT,
	COL_FROM,
	COL_DATE,
	COL_SIZE,
	COL_MSGINFO,
	COL_DELETED,
	N_COLS
};

static struct RPop3Window {
	GtkWidget *window;

	GtkWidget *treeview;
	GtkListStore *store;

	GtkWidget *status_label;

	GtkWidget *recv_btn;
	GtkWidget *open_btn;
	GtkWidget *delete_btn;
	GtkWidget *stop_btn;
	GtkWidget *close_btn;

	Pop3Session *session;
	gboolean stop_load;
	gboolean cancelled;
	gboolean finished;
	GArray *delete_array;
	gint delete_cur;
	GArray *recv_array;
	gint recv_cur;
} rpop3_window;

static void rpop3_window_create	(PrefsAccount	*account);

gint pop3_greeting_recv          (Pop3Session *session,
                                  const gchar *msg);
gint pop3_getauth_user_send      (Pop3Session *session);
gint pop3_getauth_pass_send      (Pop3Session *session);
gint pop3_getauth_apop_send      (Pop3Session *session);
#if USE_SSL
gint pop3_stls_send              (Pop3Session *session);
gint pop3_stls_recv              (Pop3Session *session);
#endif
gint pop3_getrange_stat_send     (Pop3Session *session);
gint pop3_getrange_stat_recv     (Pop3Session *session,
                                  const gchar *msg);
gint pop3_getrange_last_send     (Pop3Session *session);
gint pop3_getrange_last_recv     (Pop3Session *session,
                                  const gchar *msg);
gint pop3_getrange_uidl_send     (Pop3Session *session);
gint pop3_getrange_uidl_recv     (Pop3Session *session,
                                  const gchar *data,
                                  guint        len);
gint pop3_getsize_list_send      (Pop3Session *session);
gint pop3_getsize_list_recv      (Pop3Session *session,
                                  const gchar *data,
                                  guint        len);
gint pop3_retr_send              (Pop3Session *session);
gint pop3_retr_recv              (Pop3Session *session,
                                  FILE        *fp,
                                  guint        len);
gint pop3_logout_send            (Pop3Session *session);

void pop3_gen_send               (Pop3Session    *session,
                                  const gchar    *format, ...);

gint pop3_write_msg_to_file	(const gchar	*file,
				 FILE		*src_fp,
				 guint		 len);

Pop3ErrorValue pop3_ok		(Pop3Session	*session,
				 const gchar	*msg);

static gint rpop3_start			(Session	*session);
static void rpop3_status_label_set	(const gchar	*fmt,
					 ...) G_GNUC_PRINTF(1, 2);
static void rpop3_clear_list		(void);

static gint rpop3_top_send	(Pop3Session	*session);
static gint rpop3_top_recv	(Pop3Session	*session,
				 FILE		*fp,
				 guint		 len);
static gint rpop3_retr_send	(Pop3Session	*session);
static gint rpop3_retr_recv	(Pop3Session	*session,
				 FILE		*fp,
				 guint		 len);
static gint rpop3_delete_send	(Pop3Session	*session);
static gint rpop3_delete_recv	(Pop3Session	*session);

static gint rpop3_session_recv_msg		(Session	*session,
						 const gchar	*msg);
static gint rpop3_session_recv_data_finished	(Session	*session,
						 guchar		*data,
						 guint		 len);
static gint rpop3_session_recv_data_as_file_finished
						(Session	*session,
						 FILE		*fp,
						 guint		 len);

static gint window_deleted	(GtkWidget	*widget,
				 GdkEventAny	*event,
				 gpointer	 data);
static gboolean key_pressed	(GtkWidget	*widget,
				 GdkEventKey	*event,
				 gpointer	 data);

static void rpop3_row_activated	(GtkTreeView		*treeview,
				 GtkTreePath		*path,
				 GtkTreeViewColumn	*column,
				 gpointer		 data);

static void rpop3_recv		(GtkButton	*button,
				 gpointer	 data);
static void rpop3_open		(GtkButton	*button,
				 gpointer	 data);
static void rpop3_delete	(GtkButton	*button,
				 gpointer	 data);
static void rpop3_stop		(GtkButton	*button,
				 gpointer	 data);
static void rpop3_close		(GtkButton	*button,
				 gpointer	 data);


gint rpop3_account(PrefsAccount *account)
{
	Session *session;
	gint ret;

	if (!account || account->protocol != A_POP3)
		return -1;
	if (inc_is_active())
		return -1;
	if (rpop3_window.window)
		return -1;

	inc_lock();

	rpop3_window_create(account);

	session = pop3_session_new(account);
	rpop3_window.session = POP3_SESSION(session);
	rpop3_window.stop_load = FALSE;
	rpop3_window.cancelled = FALSE;
	rpop3_window.finished = FALSE;

	/* override Pop3Session handlers */
	session->recv_msg = rpop3_session_recv_msg;
	session->send_data_finished = NULL;
	session->recv_data_finished = rpop3_session_recv_data_finished;
	session->recv_data_as_file_finished =
		rpop3_session_recv_data_as_file_finished;

	if (!POP3_SESSION(session)->pass) {
		gchar *pass;

		pass = input_query_password(account->recv_server,
					    account->userid);
		if (pass) {
			account->tmp_pass = g_strdup(pass);
			POP3_SESSION(session)->pass = pass;
		}
	}

	session_set_timeout(session, prefs_common.io_timeout_secs * 1000);

	ret = rpop3_start(session);

	while (!rpop3_window.finished)
		gtk_main_iteration();

	session_destroy(session);
	rpop3_clear_list();
	gtk_widget_destroy(rpop3_window.window);
	memset(&rpop3_window, 0, sizeof(rpop3_window));

	inc_unlock();

	return ret;
}

static void rpop3_window_create(PrefsAccount *account)
{
	GtkWidget *window;
	GtkWidget *vbox;
	GtkWidget *scrwin;
	GtkWidget *treeview;
	GtkListStore *store;
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;
	GtkTreeSelection *selection;
	GtkWidget *hbox;
	GtkWidget *status_label;
	GtkWidget *hbbox;
	GtkWidget *recv_btn;
	GtkWidget *open_btn;
	GtkWidget *delete_btn;
	GtkWidget *stop_btn;
	GtkWidget *close_btn;
	gchar buf[BUFFSIZE];

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_snprintf(buf, sizeof(buf), _("%s - Remote POP3 mailbox"),
		   account->account_name ? account->account_name : "");
	gtk_window_set_title(GTK_WINDOW(window), buf);
	gtk_widget_set_size_request(window, 640, -1);
	gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
	gtk_container_set_border_width(GTK_CONTAINER (window), 8);
	g_signal_connect(G_OBJECT(window), "delete_event",
			 G_CALLBACK(window_deleted), NULL);
	g_signal_connect(G_OBJECT(window), "key_press_event",
			 G_CALLBACK(key_pressed), NULL);
	MANAGE_WINDOW_SIGNALS_CONNECT(window);

	vbox = gtk_vbox_new(FALSE, 6);
	gtk_container_add(GTK_CONTAINER(window), vbox);

	scrwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_box_pack_start(GTK_BOX(vbox), scrwin, TRUE, TRUE, 0);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrwin),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrwin),
					    GTK_SHADOW_IN);
	gtk_widget_set_size_request(scrwin, -1, 320);

	store = gtk_list_store_new(N_COLS, G_TYPE_INT, G_TYPE_STRING,
				   G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
				   G_TYPE_POINTER, G_TYPE_BOOLEAN);
	treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
	g_object_unref(store);
	gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);

	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
	gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);

	gtk_container_add(GTK_CONTAINER(scrwin), treeview);

#define APPEND_COLUMN(label, col, width)				\
{									\
        renderer = gtk_cell_renderer_text_new();			\
        column = gtk_tree_view_column_new_with_attributes		\
		(label, renderer, "text", col,				\
		 "strikethrough", COL_DELETED, NULL);			\
	gtk_tree_view_column_set_resizable(column, TRUE);		\
        if (width) {							\
                gtk_tree_view_column_set_sizing				\
                        (column, GTK_TREE_VIEW_COLUMN_FIXED);		\
                gtk_tree_view_column_set_fixed_width(column, width);	\
	}								\
	gtk_tree_view_column_set_sort_column_id(column, col);		\
	gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);	\
}

        APPEND_COLUMN(_("No."), COL_NUMBER, 0);
        APPEND_COLUMN(_("Subject"), COL_SUBJECT, 200);
        APPEND_COLUMN(_("From"), COL_FROM, 160);
        APPEND_COLUMN(_("Date"), COL_DATE, 0);
        APPEND_COLUMN(_("Size"), COL_SIZE, 0);

	gtk_widget_show_all(scrwin);

	g_signal_connect(G_OBJECT(treeview), "row-activated",
			 G_CALLBACK(rpop3_row_activated), NULL);

	hbox = gtk_hbox_new(FALSE, 8);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

	status_label = gtk_label_new("");
	gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);

	hbbox = gtk_hbutton_box_new();
	gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
	gtk_box_set_spacing(GTK_BOX(hbbox), 6);
	gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);

	recv_btn = gtk_button_new_with_mnemonic(_("_Get"));
	gtk_box_pack_start(GTK_BOX(hbbox), recv_btn, FALSE, FALSE, 0);
	gtk_widget_set_sensitive(recv_btn, FALSE);

	open_btn = gtk_button_new_from_stock(GTK_STOCK_OPEN);
	gtk_box_pack_start(GTK_BOX(hbbox), open_btn, FALSE, FALSE, 0);
	gtk_widget_set_sensitive(open_btn, FALSE);

	delete_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
	gtk_box_pack_start(GTK_BOX(hbbox), delete_btn, FALSE, FALSE, 0);
	gtk_widget_set_sensitive(delete_btn, FALSE);

	stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
	gtk_box_pack_start(GTK_BOX(hbbox), stop_btn, FALSE, FALSE, 0);
	gtk_widget_set_sensitive(stop_btn, FALSE);

	close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
	gtk_box_pack_start(GTK_BOX(hbbox), close_btn, FALSE, FALSE, 0);

	g_signal_connect(G_OBJECT(recv_btn), "clicked",
			 G_CALLBACK(rpop3_recv), NULL);
	g_signal_connect(G_OBJECT(open_btn), "clicked",
			 G_CALLBACK(rpop3_open), NULL);
	g_signal_connect(G_OBJECT(delete_btn), "clicked",
			 G_CALLBACK(rpop3_delete), NULL);
	g_signal_connect(G_OBJECT(stop_btn), "clicked",
			 G_CALLBACK(rpop3_stop), NULL);
	g_signal_connect(G_OBJECT(close_btn), "clicked",
			 G_CALLBACK(rpop3_close), NULL);

	gtk_widget_show_all(window);

	rpop3_window.window = window;
	rpop3_window.treeview = treeview;
	rpop3_window.store = store;
	rpop3_window.status_label = status_label;
	rpop3_window.recv_btn = recv_btn;
	rpop3_window.open_btn = open_btn;
	rpop3_window.delete_btn = delete_btn;
	rpop3_window.stop_btn = stop_btn;
	rpop3_window.close_btn = close_btn;
}

static gint rpop3_start(Session *session)
{
	SessionState state;

	g_return_val_if_fail(session != NULL, -1);

	rpop3_status_label_set(_("Connecting to %s:%d ..."),
			       session->server, session->port);

	if (session_connect(session, session->server, session->port) < 0) {
		manage_window_focus_in(rpop3_window.window, NULL, NULL);
		alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
				 session->server, session->port);
		return -1;
	}

	while (session_is_connected(session)) {
		if (session->state == SESSION_READY && rpop3_window.cancelled)
			break;
		gtk_main_iteration();
	}

	state = session->state;
	session_disconnect(session);

	switch (POP3_SESSION(session)->error_val) {
	case PS_AUTHFAIL:
		manage_window_focus_in(rpop3_window.window, NULL, NULL);
		rpop3_status_label_set(_("Authentication failed."));
		if (POP3_SESSION(session)->error_msg)
			alertpanel_error(_("Authentication failed:\n%s"),
					 POP3_SESSION(session)->error_msg);
		else
			alertpanel_error(_("Authentication failed."));
		return -1;
	case PS_SUCCESS:
		break;
	default:
		manage_window_focus_in(rpop3_window.window, NULL, NULL);
		rpop3_status_label_set(_("Error occurred during POP3 session."));
		if (POP3_SESSION(session)->error_msg)
			alertpanel_error
				(_("Error occurred during POP3 session:\n%s"),
				 POP3_SESSION(session)->error_msg);
		else
			alertpanel_error(_("Error occurred during POP3 session."));
		return -1;
	}

	switch (state) {
	case SESSION_EOF:
		manage_window_focus_in(rpop3_window.window, NULL, NULL);
		rpop3_status_label_set
			(_("Connection closed by the remote host."));
		alertpanel_error(_("Connection closed by the remote host."));
		return -1;
	case SESSION_TIMEOUT:
		manage_window_focus_in(rpop3_window.window, NULL, NULL);
		rpop3_status_label_set(_("Session timed out."));
		alertpanel_error(_("Session timed out."));
		return -1;
	case SESSION_ERROR:
		manage_window_focus_in(rpop3_window.window, NULL, NULL);
		if (POP3_SESSION(session)->state == POP3_READY) {
			rpop3_status_label_set
				(_("Can't connect to POP3 server: %s:%d"),
				 session->server, session->port);
			alertpanel_error
				(_("Can't connect to POP3 server: %s:%d"),
				 session->server, session->port);
		} else {
			rpop3_status_label_set
				(_("Error occurred during POP3 session."));
			alertpanel_error
				(_("Error occurred during POP3 session."));
		}
		return -1;
	case SESSION_READY:
		debug_print("Session cancelled.\n");
		return -1;
	default:
		break;
	}

	return 0;
}

static void rpop3_status_label_set(const gchar *fmt, ...)
{
	va_list args;
	gchar buf[1024];

	va_start(args, fmt);
	g_vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

	gtk_label_set_text(GTK_LABEL(rpop3_window.status_label), buf);
}

static gboolean clear_func(GtkTreeModel *model, GtkTreePath *path,
			   GtkTreeIter *iter, gpointer data)
{
	MsgInfo *msginfo;

	gtk_tree_model_get(model, iter, COL_MSGINFO, &msginfo, -1);
	procmsg_msginfo_free(msginfo);

	return FALSE;
}

static void rpop3_clear_list(void)
{
	
	gtk_tree_model_foreach(GTK_TREE_MODEL(rpop3_window.store), clear_func,
			       NULL);
	gtk_list_store_clear(rpop3_window.store);
}

static gint rpop3_top_send(Pop3Session *session)
{
	session->state = POP3_TOP;
	pop3_gen_send(session, "TOP %d 0", session->cur_msg);
	return PS_SUCCESS;
}

static gint rpop3_top_recv(Pop3Session *session, FILE *fp, guint len)
{
	MsgInfo *msginfo;
	MsgFlags flags = {0, 0};
	GtkTreeIter iter;
	const gchar *subject, *from, *date;
	gchar buf[1024];

	debug_print("rpop3_top_recv(): %d / %d (size %u)\n", session->cur_msg, session->count, len);

	msginfo = procheader_parse_stream(fp, flags, FALSE);

	subject = msginfo->subject ? msginfo->subject : _("(No Subject)");
	from = msginfo->from ? msginfo->from : _("(No From)");
	if (msginfo->date_t) {
		procheader_date_get_localtime(buf, sizeof(buf),
					      msginfo->date_t);
		date = buf;
	} else if (msginfo->date)
		date = msginfo->date;
	else
		date = _("(No Date)");

	gtk_list_store_append(rpop3_window.store, &iter);
	gtk_list_store_set(rpop3_window.store, &iter,
			   COL_NUMBER, session->cur_msg,
			   COL_SUBJECT, subject,
			   COL_FROM, from,
			   COL_DATE, date,
			   COL_SIZE, to_human_readable
				(session->msg[session->cur_msg].size),
			   COL_MSGINFO, msginfo,
			   COL_DELETED, FALSE,
			   -1);

	rpop3_status_label_set(_("Retrieving message headers (%d / %d) ..."),
			       session->cur_msg, session->count);

	return PS_SUCCESS;
}

static gint rpop3_retr_send(Pop3Session *session)
{
	if (rpop3_window.recv_array) {
		g_return_val_if_fail(rpop3_window.recv_cur < rpop3_window.recv_array->len, -1);
		session->cur_msg = g_array_index(rpop3_window.recv_array, gint,
						 rpop3_window.recv_cur);
	}

	session->state = POP3_RETR;
	pop3_gen_send(session, "RETR %d", session->cur_msg);
	return PS_SUCCESS;
}

static void msgview_destroy_cb(GtkWidget *widget, gpointer data)
{
	gchar *file = (gchar *)data;

	if (file) {
		debug_print("msgview_destroy_cb: removing temporary file: %s\n", file);
		g_unlink(file);
		g_free(file);
	}
}

static gint rpop3_retr_recv(Pop3Session *session, FILE *fp, guint len)
{
	gchar *file;
	MsgInfo *msginfo;
	MsgFlags flags = {0, 0};
	MessageView *msgview;

	file = get_tmp_file();
	if (pop3_write_msg_to_file(file, fp, len) < 0) {
		g_free(file);
		session->error_val = PS_IOERR;
		return -1;
	}

	if (rpop3_window.recv_array) {
		FolderItem *inbox;

		if (session->ac_prefs->inbox) {
			inbox = folder_find_item_from_identifier
				(session->ac_prefs->inbox);
			if (!inbox)
				inbox = folder_get_default_inbox();
		} else
			inbox = folder_get_default_inbox();
		if (!inbox) {
			session->error_val = PS_IOERR;
			return -1;
		}

		if (folder_item_add_msg(inbox, file, NULL, FALSE) < 0) {
			session->error_val = PS_IOERR;
			return -1;
		}
		if (rpop3_window.recv_cur + 1 == rpop3_window.recv_array->len)
			folderview_update_item(inbox, TRUE);
		else
			folderview_update_item(inbox, FALSE);

		return PS_SUCCESS;
	}

	msginfo = procheader_parse_file(file, flags, FALSE);
	msginfo->file_path = g_strdup(file);

	msgview = messageview_create_with_new_window();
	messageview_show(msgview, msginfo, FALSE);

	g_signal_connect(G_OBJECT(msgview->window), "destroy",
			 G_CALLBACK(msgview_destroy_cb), file);

	return PS_SUCCESS;
}

static gint rpop3_delete_send(Pop3Session *session)
{
	g_return_val_if_fail(rpop3_window.delete_array != NULL, -1);
	g_return_val_if_fail
		(rpop3_window.delete_cur < rpop3_window.delete_array->len, -1);

	session->state = POP3_DELETE;
	session->cur_msg = g_array_index(rpop3_window.delete_array, gint,
					 rpop3_window.delete_cur);
	pop3_gen_send(session, "DELE %d", session->cur_msg);
	return PS_SUCCESS;
}

static gint rpop3_delete_recv(Pop3Session *session)
{
	session->msg[session->cur_msg].recv_time = RECV_TIME_DELETE;
	session->msg[session->cur_msg].deleted = TRUE;
	return PS_SUCCESS;
}

static gint rpop3_session_recv_msg(Session *session, const gchar *msg)
{
	Pop3Session *pop3_session = POP3_SESSION(session);
	gint val = PS_SUCCESS;
	const gchar *body;

	body = msg;
	if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
	    pop3_session->state != POP3_GETSIZE_LIST_RECV) {
		val = pop3_ok(pop3_session, msg);
		if (val != PS_SUCCESS) {
			if (val != PS_NOTSUPPORTED) {
				pop3_session->state = POP3_ERROR;
				return -1;
			}
		}

		if (*body == '+' || *body == '-')
			body++;
		while (g_ascii_isalpha(*body))
			body++;
		while (g_ascii_isspace(*body))
			body++;
	}

	switch (pop3_session->state) {
	case POP3_READY:
	case POP3_GREETING:
		val = pop3_greeting_recv(pop3_session, body);
		rpop3_status_label_set(_("Authenticating..."));
#if USE_SSL
		if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
			val = pop3_stls_send(pop3_session);
		else
#endif
		if (pop3_session->ac_prefs->use_apop_auth)
			val = pop3_getauth_apop_send(pop3_session);
		else
			val = pop3_getauth_user_send(pop3_session);
		break;
#if USE_SSL
        case POP3_STLS:
                if ((val = pop3_stls_recv(pop3_session)) != PS_SUCCESS)
                        return -1;
                if (pop3_session->ac_prefs->use_apop_auth)
                        val = pop3_getauth_apop_send(pop3_session);
                else
                        val = pop3_getauth_user_send(pop3_session);
                break;
#endif
	case POP3_GETAUTH_USER:
		val = pop3_getauth_pass_send(pop3_session);
		break;
	case POP3_GETAUTH_PASS:
	case POP3_GETAUTH_APOP:
		rpop3_status_label_set(_("Getting the number of messages..."));
		val = pop3_getrange_stat_send(pop3_session);
		break;
        case POP3_GETRANGE_STAT:
                if ((val = pop3_getrange_stat_recv(pop3_session, body)) < 0)
                        return -1;
                if (pop3_session->count > 0)
                        val = pop3_getrange_uidl_send(pop3_session);
                else {
			rpop3_status_label_set(_("No message"));
                        val = pop3_logout_send(pop3_session);
		}
                break;
        case POP3_GETRANGE_LAST:
                if (val == PS_NOTSUPPORTED)
                        pop3_session->error_val = PS_SUCCESS;
                else if ((val = pop3_getrange_last_recv
                                (pop3_session, body)) < 0)
                        return -1;
                if (pop3_session->cur_msg > 0)
                        val = pop3_getsize_list_send(pop3_session);
                else {
			rpop3_status_label_set(_("No message"));
                        val = pop3_logout_send(pop3_session);
		}
                break;
        case POP3_GETRANGE_UIDL:
                if (val == PS_NOTSUPPORTED) {
                        pop3_session->error_val = PS_SUCCESS;
                        val = pop3_getrange_last_send(pop3_session);
                } else {
                        pop3_session->state = POP3_GETRANGE_UIDL_RECV;
                        val = session_recv_data(session, 0, ".\r\n");
                }
                break;
        case POP3_GETSIZE_LIST:
                pop3_session->state = POP3_GETSIZE_LIST_RECV;
                val = session_recv_data(session, 0, ".\r\n");
                break;
	case POP3_TOP:
		pop3_session->state = POP3_TOP_RECV;
                val = session_recv_data_as_file(session, 0, ".\r\n");
		break;
        case POP3_RETR:
                pop3_session->state = POP3_RETR_RECV;
                val = session_recv_data_as_file(session, 0, ".\r\n");
                break;
        case POP3_DELETE:
                val = rpop3_delete_recv(pop3_session);
		if (val != PS_SUCCESS)
			break;
		if (rpop3_window.delete_cur + 1 < rpop3_window.delete_array->len) {
			rpop3_window.delete_cur++;
			val = rpop3_delete_send(pop3_session);
		} else {
			rpop3_status_label_set(_("Deleted %d messages"),
				 	       rpop3_window.delete_cur + 1);
			g_array_free(rpop3_window.delete_array, TRUE);
			rpop3_window.delete_array = NULL;
			rpop3_window.delete_cur = 0;
			pop3_session->state = POP3_IDLE;
		}
                break;
        case POP3_LOGOUT:
                pop3_session->state = POP3_DONE;
                session_disconnect(session);
                break;
	case POP3_ERROR:
	default:
		return -1;
	}

	if (val == PS_SUCCESS)
		return 0;
	else
		return -1;
}

static gint rpop3_session_recv_data_finished(Session *session, guchar *data,
					     guint len)
{
        Pop3Session *pop3_session = POP3_SESSION(session);
        Pop3ErrorValue val = PS_SUCCESS;

        switch (pop3_session->state) {
        case POP3_GETRANGE_UIDL_RECV:
                val = pop3_getrange_uidl_recv(pop3_session, (gchar *)data, len);
                if (val == PS_SUCCESS) {
			if (rpop3_window.cancelled) {
				rpop3_status_label_set(_("Quitting..."));
				pop3_logout_send(rpop3_window.session);
			} else
				pop3_getsize_list_send(pop3_session);
                } else
                        return -1;
                break;
        case POP3_GETSIZE_LIST_RECV:
                val = pop3_getsize_list_recv(pop3_session, (gchar *)data, len);
                if (val == PS_SUCCESS) {
			pop3_session->cur_msg = 1;
			if (rpop3_window.cancelled || pop3_session->count == 0) {
				rpop3_status_label_set(_("Quitting..."));
				pop3_logout_send(rpop3_window.session);
			} else {
				gtk_widget_set_sensitive(rpop3_window.stop_btn,
							 TRUE);
				rpop3_top_send(pop3_session);
			}
		} else
                        return -1;
                break;
        case POP3_ERROR:
        default:
                return -1;
        }

        return 0;
}

static gint rpop3_session_recv_data_as_file_finished(Session *session,
						     FILE *fp, guint len)
{
        Pop3Session *pop3_session = POP3_SESSION(session);

        switch (pop3_session->state) {
	case POP3_RETR_RECV:
        	if (rpop3_retr_recv(pop3_session, fp, len) < 0)
                	return -1;
		if (rpop3_window.recv_array) {
			if (rpop3_window.recv_cur + 1 < rpop3_window.recv_array->len) {
				rpop3_window.recv_cur++;
				if (rpop3_retr_send(pop3_session) < 0)
					return -1;
			} else {
				rpop3_status_label_set(_("Retrieved %d messages"), rpop3_window.recv_cur + 1);
				g_array_free(rpop3_window.recv_array, TRUE);
				rpop3_window.recv_array = NULL;
				rpop3_window.recv_cur = 0;
				pop3_session->state = POP3_IDLE;
			}
		} else {
			rpop3_status_label_set(_("Opened message %d"),
					       pop3_session->cur_msg);
			pop3_session->state = POP3_IDLE;
		}
		break;
	case POP3_TOP_RECV:
		if (rpop3_top_recv(pop3_session, fp, len) == PS_SUCCESS) {
			if (rpop3_window.cancelled) {
				rpop3_status_label_set(_("Quitting..."));
				pop3_logout_send(rpop3_window.session);
			} else if (!rpop3_window.stop_load &&
				 (pop3_session->cur_msg < pop3_session->count)) {
				pop3_session->cur_msg++;
				rpop3_top_send(pop3_session);
			} else {
				rpop3_status_label_set
					(_("Retrieved %d message headers"),
					 pop3_session->cur_msg);
				gtk_widget_set_sensitive
					(rpop3_window.recv_btn, TRUE);
				gtk_widget_set_sensitive
					(rpop3_window.open_btn, TRUE);
				gtk_widget_set_sensitive
					(rpop3_window.delete_btn, TRUE);
				gtk_widget_set_sensitive
					(rpop3_window.stop_btn, FALSE);
				pop3_session->state = POP3_IDLE;
			}
		} else
			return -1;
		break;
	default:
		return -1;
	}

        return 0;
}

static gint window_deleted(GtkWidget *widget, GdkEventAny *event,
			   gpointer data)
{
	gtk_button_clicked(GTK_BUTTON(rpop3_window.close_btn));
	return TRUE;
}

static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
			    gpointer data)
{
	return FALSE;
}

static void rpop3_row_activated(GtkTreeView *treeview, GtkTreePath *path,
				GtkTreeViewColumn *column, gpointer data)
{
	gtk_button_clicked(GTK_BUTTON(rpop3_window.open_btn));
}

static void rpop3_recv(GtkButton *button, gpointer data)
{
	GtkTreeModel *model = GTK_TREE_MODEL(rpop3_window.store);
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GList *rows, *cur;
	gint num;
	gboolean deleted;
	GArray *array;

	if (rpop3_window.session->state != POP3_IDLE)
		return;

	selection = gtk_tree_view_get_selection
		(GTK_TREE_VIEW(rpop3_window.treeview));

	rows = gtk_tree_selection_get_selected_rows(selection, NULL);
	array = g_array_sized_new(FALSE, FALSE, sizeof(gint),
				  g_list_length(rows));

	for (cur = rows; cur != NULL; cur = cur->next) {
		gtk_tree_model_get_iter(model, &iter, (GtkTreePath *)cur->data);
		gtk_tree_model_get(model, &iter, COL_NUMBER, &num,
				   COL_DELETED, &deleted, -1);
		if (!deleted) {
			debug_print("rpop3_recv: recieving message %d\n", num);
			g_array_append_val(array, num);
		}
	}

	g_list_foreach(rows, (GFunc)gtk_tree_path_free, NULL);
	g_list_free(rows);

	if (array->len > 0) {
		rpop3_window.recv_array = array;
		rpop3_window.recv_cur = 0;
		rpop3_retr_send(rpop3_window.session);
	} else
		g_array_free(array, TRUE);
}

static void rpop3_open(GtkButton *button, gpointer data)
{
	GtkTreeModel *model = GTK_TREE_MODEL(rpop3_window.store);
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GList *rows, *cur;
	gint num;
	gboolean deleted;

	if (rpop3_window.session->state != POP3_IDLE)
		return;

	selection = gtk_tree_view_get_selection
		(GTK_TREE_VIEW(rpop3_window.treeview));

	rows = gtk_tree_selection_get_selected_rows(selection, NULL);

	for (cur = rows; cur != NULL; cur = cur->next) {
		gtk_tree_model_get_iter(model, &iter, (GtkTreePath *)cur->data);
		gtk_tree_model_get(model, &iter, COL_NUMBER, &num,
				   COL_DELETED, &deleted, -1);
		if (!deleted) {
			debug_print("rpop3_open: opening message %d\n", num);
			rpop3_status_label_set(_("Retrieving message %d ..."),
					       num);
			rpop3_window.session->cur_msg = num;
			rpop3_retr_send(rpop3_window.session);
			break;
		}
	}

	g_list_foreach(rows, (GFunc)gtk_tree_path_free, NULL);
	g_list_free(rows);
}

static void rpop3_delete(GtkButton *button, gpointer data)
{
	GtkTreeModel *model = GTK_TREE_MODEL(rpop3_window.store);
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	GList *rows, *cur;
	gint num;
	gboolean deleted;
	GArray *array;
	AlertValue val;

	if (rpop3_window.session->state != POP3_IDLE)
		return;

	val = alertpanel(_("Delete messages"),
			 _("Really delete selected messages from server?\n"
			   "This operation cannot be reverted."),
			 GTK_STOCK_YES, GTK_STOCK_NO, NULL);
	if (val != G_ALERTDEFAULT)
		return;

	selection = gtk_tree_view_get_selection
		(GTK_TREE_VIEW(rpop3_window.treeview));

	rows = gtk_tree_selection_get_selected_rows(selection, NULL);

	array = g_array_sized_new(FALSE, FALSE, sizeof(gint),
				  g_list_length(rows));

	for (cur = rows; cur != NULL; cur = cur->next) {
		gtk_tree_model_get_iter(model, &iter, (GtkTreePath *)cur->data);
		gtk_tree_model_get(model, &iter, COL_NUMBER, &num,
				   COL_DELETED, &deleted, -1);
		if (!deleted) {
			debug_print("rpop3_delete: marked %d to delete\n", num);
			g_array_append_val(array, num);
			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
					   COL_DELETED, TRUE, -1);
		}
	}

	g_list_foreach(rows, (GFunc)gtk_tree_path_free, NULL);
	g_list_free(rows);

	if (array->len > 0) {
		rpop3_window.delete_array = array;
		rpop3_window.delete_cur = 0;
		rpop3_delete_send(rpop3_window.session);
	} else
		g_array_free(array, TRUE);
}

static void rpop3_stop(GtkButton *button, gpointer data)
{
	rpop3_window.stop_load = TRUE;
}

static void rpop3_close(GtkButton *button, gpointer data)
{
	rpop3_window.finished = TRUE;

	if (rpop3_window.session->state == POP3_IDLE) {
		rpop3_status_label_set(_("Quitting..."));
		pop3_logout_send(rpop3_window.session);
	} else if (rpop3_window.session->state != POP3_DONE ||
		   rpop3_window.session->state != POP3_ERROR)
		rpop3_window.cancelled = TRUE;
}