File: mailWizard.c

package info (click to toggle)
screem 0.2.1-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,748 kB
  • ctags: 2,270
  • sloc: ansic: 26,366; sh: 7,453; makefile: 512; sed: 93; perl: 18
file content (339 lines) | stat: -rw-r--r-- 10,076 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
#include <config.h>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h> 
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include <gmodule.h>
#include <gnome.h>

#include "site.h"
#include "page.h"
#include "editor.h"

extern GtkWidget *app;
extern Site *current_site;
extern Page *current_page;

#define MAXHOSTNAME 64

/* FIXME: should really allow the user to specify a mail server */
#define DEST_IP   "127.0.0.1"
#define DEST_PORT 25

static GtkWidget* createDialog();
static void clicked( GtkWidget *widget, gint button, GtkWidget **dialog );
static void delete( GtkWidget *widget, GdkEvent *event, GtkWidget **dialog );
void mailWizard();

/*********************************************************************/
G_MODULE_EXPORT const gchar*
g_module_check_init( GModule *module )
{
        g_print("mailWizard: check-init\n");
        return NULL;
}
/*********************************************************************/
G_MODULE_EXPORT void
g_module_unload( GModule *module )
{
        g_print( "mailWizard: unloaded\n" );
}
/*********************************************************************/
G_MODULE_EXPORT void 
init() 
{
	GtkWidget *button;
	GtkWidget *toolbar;

	button = gnome_stock_new_with_icon( GNOME_STOCK_PIXMAP_MAIL_SND );
	
	toolbar = gtk_object_get_data( GTK_OBJECT( app ), "wizardbar" );

        /* place a button on the wizards toolbar */
	gtk_toolbar_append_item( GTK_TOOLBAR( toolbar ), "",
                                 _("Mail Page"), "", button, mailWizard, 0 );

        g_print( "mailWizard: initialised\n" );
}
/*********************************************************************/
void mailWizard()
{
	static GtkWidget *dialog = NULL;
	Page *page;
	
	if( current_site )
		page = screem_site_get_current_page( current_site );
	else
		page = current_page;

	g_return_if_fail( page != NULL );

	dialog = createDialog();

	gtk_signal_connect( GTK_OBJECT( dialog ), "clicked", clicked, &dialog);
	gtk_signal_connect( GTK_OBJECT( dialog ), "delete_event", delete,
			    &dialog );

	gtk_widget_show_all( dialog );
}
/*********************************************************************/
GtkWidget* createDialog()
{
	GtkWidget *dialog;
	GtkWidget *dialog_vbox;
	GtkWidget *table;
	GtkWidget *to;
	GtkWidget *cc;
	GtkWidget *bcc;
	GtkWidget *subject;
	GtkWidget *label;

	dialog = gnome_dialog_new( _( "Mail Wizard" ),
				    GNOME_STOCK_PIXMAP_MAIL_SND,
				    GNOME_STOCK_BUTTON_CLOSE,
				    NULL );
	dialog_vbox = GNOME_DIALOG( dialog )->vbox;
	
	table = gtk_table_new( 2, 5, FALSE );
	gtk_box_pack_start( GTK_BOX( dialog_vbox ), table, TRUE, TRUE, 0 );
	
	to = gnome_entry_new( NULL );
	gtk_object_set_data( GTK_OBJECT( dialog ), "to", to );
	gtk_table_attach( GTK_TABLE( table ), to, 1, 2, 0, 1,
			  GTK_EXPAND | GTK_FILL, 0, GNOME_PAD, GNOME_PAD );
	
	cc = gnome_entry_new( NULL );
	gtk_object_set_data( GTK_OBJECT( dialog ), "cc", cc );
	gtk_table_attach( GTK_TABLE( table ), cc, 1, 2, 1, 2,
			  GTK_EXPAND | GTK_FILL, 0, GNOME_PAD, GNOME_PAD );
	
	bcc = gnome_entry_new( NULL );
	gtk_object_set_data( GTK_OBJECT( dialog ), "bcc", bcc );
	gtk_table_attach( GTK_TABLE( table ), bcc, 1, 2, 2, 3,
			  GTK_EXPAND | GTK_FILL, 0, GNOME_PAD, GNOME_PAD );
	
	subject = gnome_entry_new( NULL );
	gtk_object_set_data( GTK_OBJECT( dialog ), "subject", subject );
	gtk_table_attach( GTK_TABLE( table ), subject, 1, 2, 3, 4,
			  GTK_EXPAND | GTK_FILL, 0, GNOME_PAD, GNOME_PAD );
	
	label = gtk_label_new( _( "Subject:" ) );
	gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 3, 4,
			  GTK_FILL, 0, GNOME_PAD, GNOME_PAD );

	gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT );
	gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
	
	label = gtk_label_new( _("bcc:" ) );
	gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 2, 3,
			  GTK_FILL, 0, GNOME_PAD, GNOME_PAD );

	gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT );
	gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
	
	label = gtk_label_new( _( "cc:" ) );
	gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 1, 2,
			  GTK_FILL, 0, GNOME_PAD, GNOME_PAD );

	gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT );
	gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
	
	label = gtk_label_new( _( "To:" ) );
	gtk_table_attach( GTK_TABLE( table ), label, 0, 1, 0, 1,
			  GTK_FILL, 0, GNOME_PAD, GNOME_PAD );

	gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT );
	gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
	
	label = gtk_label_new( _("Info: HTML shouldn't really be used for email" ) );
	gtk_table_attach( GTK_TABLE( table ), label, 0, 2, 4, 5,
			  GTK_FILL, 0, GNOME_PAD, GNOME_PAD );
	
	return dialog;
}
/*********************************************************************/
static void clicked( GtkWidget *widget, gint button, GtkWidget **dialog )
{
	GtkWidget *to;
	GtkWidget *cc;
	GtkWidget *bcc;
	GtkWidget *subject;

	gchar *t;
	gchar *c;
	gchar *b;
	gchar *s;

	const gchar *xmailer = "SCREEM " VERSION " - Site Creating and Editing Environment\r\n";
	const gchar *xmailerorigin = "http://www.screem.org\r\n";
	const gchar *contentType = "text/html; charset=iso-8859-1\r\n";
	const gchar *contentTransferEncoding = "8bit\r\n";
	const gchar *QUIT = "QUIT\r\n";
	const gchar *DATA = "DATA\r\n";
	gchar *msgid;
	gchar *date;
	gchar *addr;
	gchar *from;
	gchar **RCPT;    /* to store the to line */
	gchar **RCPT2;   /* to store the cc line */
	gchar **RCPT3;   /* to store the bcc line */
	gchar *rcpt;
	gchar *FROM;
	gchar *HELO;
	gchar *body;
	gchar *data;  /* the constructed mail header and body */
   
	time_t ti = time( 0 );
	struct tm *tm = gmtime( &ti );
	gchar hname[ MAXHOSTNAME + 1 ];

	Page *p;
	int sd; /* socket descriptor */
	struct sockaddr_in dest; 
	const int destSize = sizeof( struct sockaddr );
	gint user;

	if( current_site )
		p = screem_site_get_current_page( current_site );
	else
		p = current_page;
	
	if( ( button == 0 ) && p) { /* Send mail was clicked */
		body = screem_editor_get_text( 0, screem_editor_get_length() );
		to = gtk_object_get_data( GTK_OBJECT( widget ), "to" );
		cc = gtk_object_get_data( GTK_OBJECT( widget ), "cc" );
		bcc = gtk_object_get_data( GTK_OBJECT( widget ), "bcc" );
		subject = gtk_object_get_data(GTK_OBJECT( widget ), "subject");

		to = gnome_entry_gtk_entry( GNOME_ENTRY( to ) );
		cc = gnome_entry_gtk_entry( GNOME_ENTRY( cc ) );
		bcc = gnome_entry_gtk_entry( GNOME_ENTRY( bcc ) );
		subject = gnome_entry_gtk_entry( GNOME_ENTRY( subject ) );

		t = gtk_entry_get_text( GTK_ENTRY( to ) );
		c = gtk_entry_get_text( GTK_ENTRY( cc ) );
		b = gtk_entry_get_text( GTK_ENTRY( bcc ) );
		s = gtk_entry_get_text( GTK_ENTRY( subject ) );

		/* FIXME: add the time zone to the end of date */
		date = g_strconcat( "Date: ", ctime( &ti ), NULL );

		/* construct a message id line */
		gethostname( hname, MAXHOSTNAME );
		msgid = g_strdup_printf( "Message-ID: <%i%i%i%i%i-screem %s-@%s>\r\n", tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, VERSION, hname );

		addr = g_strconcat( " <", g_get_user_name(), "@", hname, ">",
				    NULL );
		from = g_strconcat( "From: \"", g_get_real_name(), "\"",
				    addr, NULL );
		/* set up the HELO string */
		HELO = g_strconcat( "HELO ", hname, "\r\n", NULL );
	      
		/* set up the FROM string */
		FROM = g_strconcat( "MAIL FROM:", addr, "\r\n", NULL );

		/* set up the RCPT strings */
		RCPT = g_strsplit( t, ",", 255 );
		RCPT2 = g_strsplit( c, ",", 255 );
		RCPT3 = g_strsplit( b, ",", 255 );

		if( strlen( t ) )
			t = g_strconcat( "To: ", t, "\r\n", NULL );
		else
			t = "";
		if( strlen( c ) )
			c = g_strconcat( "cc: ", c, "\r\n", NULL );
		else
			c = "";
	
		if( strlen( s ) )
			s = g_strconcat( "Subject: ", s, "\r\n", NULL );
		else
			s = g_strdup( "Subject: <no subject>\r\n" );

		/* FIXME: we don't attach the users .sig file to the
		   end of the mail yet */
		data = g_strconcat( from, "\r\n", date,
				    t, c, s, msgid,
				    "X-Mailer: ",xmailer,
				    "X-Mailerorigin: ", xmailerorigin,
				    "MIME-Version: 1.0\r\n",
				    "Content-Type: ", contentType,
				    "Content-Transfer-Encoding: ",
				    contentTransferEncoding, "\r\n", 
				    body, "\r\n.\r\n", NULL );

		/* connect to mail server */
		sd = socket( AF_INET, SOCK_STREAM, 0 );
		if( sd ) {
#warning "no error checking performed in mail transfer"
			memset( &dest, 0, sizeof( dest ) );
			dest.sin_family = AF_INET;
			dest.sin_port = htons( DEST_PORT );
			dest.sin_addr.s_addr = inet_addr( DEST_IP );
			connect( sd, (struct sockaddr *)&dest, destSize );
			/* send HELO */
			send( sd, HELO, strlen( HELO ), 0 );
			/* send FROM */
			send( sd, FROM, strlen( FROM ), 0 );
			/* send all RCPT's */
			for( user = 0; RCPT[ user ]; user ++ ) {
				rcpt = g_strconcat( "RCPT TO:<", RCPT[ user ],
						    ">\r\n", NULL );
				send( sd, rcpt, strlen( rcpt ), 0 );
				g_free( rcpt );
			}
			for( user = 0; RCPT2[ user ]; user ++ ) {
				rcpt = g_strconcat( "RCPT TO:<", RCPT2[ user ],
						    ">\r\n", NULL );
				send( sd, rcpt, strlen( rcpt ), 0 );
				g_free( rcpt );
			}
			for( user = 0; RCPT3[ user ]; user ++ ) {
				rcpt = g_strconcat( "RCPT TO:<", RCPT3[ user ],
						    ">\r\n", NULL );
				send( sd, rcpt, strlen( rcpt ), 0 );
				g_free( rcpt );
			}
			/* send DATA */
			send( sd, DATA, strlen( DATA ), 0 );
			/* send message body */
			send( sd, data, strlen( data ), 0 );
			/* send QUIT */
			send( sd, QUIT, strlen( QUIT ), 0 );
		
			{
				gchar buffer;
				while( recv( sd, &buffer, 1, 0 ) > 0 ) {
					printf( "%c", buffer );
				}
				printf( "\n" );
			}

			close( sd );
		}
		g_free( msgid );
		g_free( from );
		g_free( addr );
		g_free( FROM );
		g_free( data );
		g_free( HELO );
		g_free( body );
		g_strfreev( RCPT );
		g_strfreev( RCPT2 );
		g_strfreev( RCPT3 );
	}

	gtk_widget_destroy( widget );
	*dialog = NULL;
}
/*********************************************************************/
static void delete( GtkWidget *widget, GdkEvent *event, GtkWidget **dialog )
{
	*dialog = NULL;
}