blob: fcea7bc8244b5596004613d1fdf71b2b4766a7fd [file] [log] [blame]
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001/*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment for the C64.
34 *
adamdunkels78c03dc2003-04-09 13:45:05 +000035 * $Id: email.c,v 1.5 2003/04/09 13:45:05 adamdunkels Exp $
adamdunkelsca9ddcb2003-03-19 14:13:31 +000036 *
37 */
38
39
40#include "ctk.h"
41#include "dispatcher.h"
42#include "smtp.h"
43#include "uip_main.h"
44#include "petsciiconv.h"
adamdunkels8af703e2003-04-08 11:50:20 +000045#include "loader.h"
46
adamdunkelsca9ddcb2003-03-19 14:13:31 +000047
48#define MAXNUMMSGS 6
49
50static struct ctk_menu menu;
adamdunkels8af703e2003-04-08 11:50:20 +000051unsigned char menuitem_open, menuitem_setup, menuitem_quit;
adamdunkelsca9ddcb2003-03-19 14:13:31 +000052
53/* The main window. */
54static struct ctk_window mainwindow;
55
56static struct ctk_button newmailbutton =
57 {CTK_BUTTON(26, 0, 8, "New mail")};
58static struct ctk_button checkbutton =
59 {CTK_BUTTON(0, 0, 10, "Check mail")};
60
61static struct ctk_button msgbuttons[MAXNUMMSGS];
62static struct ctk_label msglabels[MAXNUMMSGS];
63static char msgtitles[20][MAXNUMMSGS];
64static struct ctk_separator sep1 =
65 {CTK_SEPARATOR(0, 7, 36)};
66static struct ctk_separator sep2 =
67 {CTK_SEPARATOR(0, 20, 36)};
68static struct ctk_label statuslabel =
69 {CTK_LABEL(6, 21, 23, 1, "")};
70
71
72static struct ctk_label tolabel =
73 {CTK_LABEL(0, 8, 3, 1, "To:")};
74static char to[40];
75static struct ctk_textentry totextentry =
76 {CTK_TEXTENTRY(8, 8, 26, 1, to, 38)};
77
78static struct ctk_label cclabel =
79 {CTK_LABEL(0, 9, 3, 1, "Cc:")};
80static char cc[40];
81static struct ctk_textentry cctextentry =
82 {CTK_TEXTENTRY(8, 9, 26, 1, cc, 38)};
83
84static struct ctk_label subjectlabel =
85 {CTK_LABEL(0, 10, 8, 1, "Subject:")};
86static char subject[40];
87static struct ctk_textentry subjecttextentry =
88 {CTK_TEXTENTRY(8, 10, 26, 1, subject, 38)};
89
90static char mail[36*9];
91static struct ctk_textentry mailtextentry =
92 {CTK_TEXTENTRY(0, 11, 34, 9, mail, 36)};
93
94static struct ctk_button sendbutton =
95 {CTK_BUTTON(0, 21, 4, "Send")};
96static struct ctk_button erasebutton =
97 {CTK_BUTTON(29, 21, 5, "Erase")};
98
99/* The "Really cancel message?" dialog. */
100static struct ctk_window canceldialog;
101static struct ctk_label canceldialoglabel1 =
102 {CTK_LABEL(2, 1, 22, 1, "Really cancel message?")};
103static struct ctk_label canceldialoglabel2 =
104 {CTK_LABEL(0, 2, 26, 1, "All contents will be lost.")};
105static struct ctk_button cancelyesbutton =
106 {CTK_BUTTON(4, 4, 3, "Yes")};
107static struct ctk_button cancelnobutton =
108 {CTK_BUTTON(18, 8, 2, "No")};
109
110/* The setup window. */
111static struct ctk_window setupwindow;
112static struct ctk_label fromaddresslabel =
113 {CTK_LABEL(0, 0, 25, 1, "Name and e-mail address")};
114static char fromaddress[40];
115static struct ctk_textentry fromaddresstextentry =
116 {CTK_TEXTENTRY(0, 1, 25, 1, fromaddress, 39)};
117
118static struct ctk_label smtpserverlabel =
119 {CTK_LABEL(0, 3, 20, 1, "Outgoing mailserver")};
120static char smtpserver[40];
121static struct ctk_textentry smtpservertextentry =
122 {CTK_TEXTENTRY(0, 4, 25, 1, smtpserver, 39)};
123
124static struct ctk_label pop3serverlabel =
125 {CTK_LABEL(0, 6, 20, 1, "Incoming mailserver")};
126static char pop3server[40];
127static struct ctk_textentry pop3servertextentry =
128 {CTK_TEXTENTRY(0, 7, 25, 1, pop3server, 39)};
129
130static struct ctk_label pop3userlabel =
131 {CTK_LABEL(0, 9, 20, 1, "Mailserver username")};
132static char pop3user[40];
133static struct ctk_textentry pop3usertextentry =
134 {CTK_TEXTENTRY(0, 10, 25, 1, pop3user, 39)};
135
136static struct ctk_label pop3passwordlabel =
137 {CTK_LABEL(0, 12, 20, 1, "Mailserver password")};
138static char pop3password[40];
139static struct ctk_textentry pop3passwordtextentry =
140 {CTK_TEXTENTRY(0, 13, 25, 1, pop3password, 39)};
141
142
143static struct ctk_button setupokbutton =
144 {CTK_BUTTON(24, 15, 2, "Ok")};
145
adamdunkels78c03dc2003-04-09 13:45:05 +0000146static DISPATCHER_SIGHANDLER(email_sighandler, s, data);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000147static struct dispatcher_proc p =
adamdunkels78c03dc2003-04-09 13:45:05 +0000148 {DISPATCHER_PROC("E-mail client", NULL, email_sighandler, smtp_appcall)};
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000149static ek_id_t id;
150
151/*-----------------------------------------------------------------------------------*/
152static void
adamdunkels0137b442003-04-08 23:27:33 +0000153email_quit(void)
adamdunkels1cd53bd2003-04-08 19:24:55 +0000154{
155 ctk_window_close(&setupwindow);
156 ctk_window_close(&mainwindow);
157 ctk_menu_remove(&menu);
158 dispatcher_exit(&p);
159 id = EK_ID_NONE;
160 LOADER_UNLOAD();
161}
162/*-----------------------------------------------------------------------------------*/
163static void
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000164make_window(void)
165{
166 unsigned char i;
167 struct ctk_button *button;
168 struct ctk_label *label;
169
170 /* Create the main window. */
171 ctk_window_new(&mainwindow, 36, 22, "E-mail");
172 ctk_window_move(&mainwindow, 1, 0);
173
174 CTK_WIDGET_ADD(&mainwindow, &checkbutton);
175 CTK_WIDGET_FOCUS(&mainwindow, &checkbutton);
176 CTK_WIDGET_ADD(&mainwindow, &newmailbutton);
177 CTK_WIDGET_ADD(&mainwindow, &sep1);
178 CTK_WIDGET_ADD(&mainwindow, &sep2);
179 CTK_WIDGET_ADD(&mainwindow, &statuslabel);
180
181 for(i = 0; i < MAXNUMMSGS; ++i) {
182 button = &msgbuttons[i];
183 CTK_BUTTON_NEW(button, 0, i + 1, 1, " ");
184 CTK_WIDGET_ADD(&mainwindow, button);
185 label = &msglabels[i];
186 CTK_LABEL_NEW(label, 3, i + 1, 33, 1, msgtitles[i]);
187 CTK_WIDGET_ADD(&mainwindow, label);
188 }
189
190}
191/*-----------------------------------------------------------------------------------*/
192static void
193make_composer(void)
194{
195 CTK_WIDGET_ADD(&mainwindow, &tolabel);
196 CTK_WIDGET_ADD(&mainwindow, &cclabel);
197 CTK_WIDGET_ADD(&mainwindow, &subjectlabel);
198
199 CTK_WIDGET_ADD(&mainwindow, &totextentry);
200 CTK_WIDGET_FOCUS(&mainwindow, &totextentry);
201 CTK_WIDGET_ADD(&mainwindow, &cctextentry);
202 CTK_WIDGET_ADD(&mainwindow, &subjecttextentry);
203
204 CTK_WIDGET_ADD(&mainwindow, &mailtextentry);
205
206 CTK_WIDGET_ADD(&mainwindow, &sendbutton);
207 CTK_WIDGET_ADD(&mainwindow, &erasebutton);
208
209 memset(mail, ' ', sizeof(mail));
210}
211/*-----------------------------------------------------------------------------------*/
212static void
213make_read(void)
214{
215 CTK_WIDGET_ADD(&mainwindow, &tolabel);
216 CTK_WIDGET_ADD(&mainwindow, &cclabel);
217 CTK_WIDGET_ADD(&mainwindow, &subjectlabel);
218
219 CTK_WIDGET_ADD(&mainwindow, &totextentry);
220 /* CTK_WIDGET_FOCUS(&mainwindow, &totextentry); */
221 CTK_WIDGET_ADD(&mainwindow, &cctextentry);
222 CTK_WIDGET_ADD(&mainwindow, &subjecttextentry);
223
224 CTK_WIDGET_ADD(&mainwindow, &mailtextentry);
225
226 CTK_WIDGET_ADD(&mainwindow, &sendbutton);
227 CTK_WIDGET_ADD(&mainwindow, &erasebutton);
228}
229/*-----------------------------------------------------------------------------------*/
adamdunkels1cd53bd2003-04-08 19:24:55 +0000230LOADER_INIT_FUNC(email_init)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000231{
232 if(id == EK_ID_NONE) {
233 id = dispatcher_start(&p);
234
235 /* Create the "Really cancel message?" dialog. */
236 ctk_dialog_new(&canceldialog, 26, 6);
237 CTK_WIDGET_ADD(&canceldialog, &canceldialoglabel1);
238 CTK_WIDGET_ADD(&canceldialog, &canceldialoglabel2);
239 CTK_WIDGET_ADD(&canceldialog, &cancelyesbutton);
240 CTK_WIDGET_ADD(&canceldialog, &cancelnobutton);
241 CTK_WIDGET_FOCUS(&canceldialog, &cancelnobutton);
242
243 /* Create setup window. */
244 ctk_window_new(&setupwindow, 28, 16, "E-mail setup");
245 ctk_window_move(&setupwindow, 5, 3);
246
247 CTK_WIDGET_ADD(&setupwindow, &fromaddresslabel);
248 CTK_WIDGET_ADD(&setupwindow, &fromaddresstextentry);
249 CTK_WIDGET_ADD(&setupwindow, &smtpserverlabel);
250 CTK_WIDGET_ADD(&setupwindow, &smtpservertextentry);
251 CTK_WIDGET_ADD(&setupwindow, &pop3serverlabel);
252 CTK_WIDGET_ADD(&setupwindow, &pop3servertextentry);
253 CTK_WIDGET_ADD(&setupwindow, &pop3userlabel);
254 CTK_WIDGET_ADD(&setupwindow, &pop3usertextentry);
255 CTK_WIDGET_ADD(&setupwindow, &pop3passwordlabel);
256 CTK_WIDGET_ADD(&setupwindow, &pop3passwordtextentry);
257 CTK_WIDGET_ADD(&setupwindow, &setupokbutton);
258
259 CTK_WIDGET_FOCUS(&setupwindow, &fromaddresstextentry);
260
261
262 /* Create main window. */
263 make_window();
264 make_composer();
265
266 /* Create and add the menu */
267 ctk_menu_new(&menu, "E-mail");
268 menuitem_setup = ctk_menuitem_add(&menu, "Setup");
269 menuitem_open = ctk_menuitem_add(&menu, "Open");
adamdunkels8af703e2003-04-08 11:50:20 +0000270 menuitem_quit = ctk_menuitem_add(&menu, "Quit");
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000271 ctk_menu_add(&menu);
272
273 /* Attach listeners to signals. */
274 dispatcher_listen(ctk_signal_button_activate);
275 dispatcher_listen(ctk_signal_menu_activate);
adamdunkels8af703e2003-04-08 11:50:20 +0000276 dispatcher_listen(ctk_signal_window_close);
277
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000278 /* Open setup window */
279 ctk_window_open(&setupwindow);
280 } else {
281 ctk_window_open(&mainwindow);
282 }
283}
284/*-----------------------------------------------------------------------------------*/
285static void
286applyconfig(void)
287{
288 u16_t addr[2];
289 char *cptr;
290
291 for(cptr = smtpserver; *cptr != ' ' && *cptr != 0; ++cptr);
292 *cptr = 0;
293
294 if(uip_main_ipaddrconv(smtpserver, (unsigned char *)addr)) {
295 smtp_configure("contiki", addr);
296 }
297}
298/*-----------------------------------------------------------------------------------*/
299static void
300prepare_message(void)
301{
302 char *mptr1, *mptr2;
303
304 /* Convert fields to ASCII. */
305 petsciiconv_toascii(to, sizeof(to));
306 petsciiconv_toascii(subject, sizeof(subject));
307 petsciiconv_toascii(mail, 255);
308 petsciiconv_toascii(mail + 255, sizeof(mail) - 255);
309
310 /* Insert line delimiters. */
311
312}
313/*-----------------------------------------------------------------------------------*/
adamdunkels78c03dc2003-04-09 13:45:05 +0000314static
315DISPATCHER_SIGHANDLER(email_sighandler, s, data)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000316{
317 struct ctk_widget *w;
318 unsigned char i;
adamdunkels78c03dc2003-04-09 13:45:05 +0000319
320 DISPATCHER_SIGHANDLER_ARGS(s, data);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000321
322 if(s == ctk_signal_button_activate) {
323 w = (struct ctk_widget *)data;
324 if(w == (struct ctk_widget *)&newmailbutton) {
325 /* ctk_window_open(&composerwindow);*/
326 ctk_window_close(&mainwindow);
327 make_window();
328 make_composer();
329 ctk_window_open(&mainwindow);
330 ctk_window_redraw(&mainwindow);
331#if 0
332 } else if(w == &replybutton) {
333 /* XXX Fiddle in the from and subject fields into the new
334 mail. */
335 ctk_window_open(&composerwindow);
336 ctk_redraw();
337#endif
338 } else if(w == (struct ctk_widget *)&checkbutton) {
339 /* XXX Should actually check email. */
340 ctk_label_set_text(&statuslabel, "Checking mail...");
341 ctk_window_redraw(&mainwindow);
342 } else if(w == (struct ctk_widget *)&sendbutton) {
343 prepare_message();
344 smtp_send(to, fromaddress, subject, mail, sizeof(mail));
345 ctk_label_set_text(&statuslabel, "Sending message...");
346 ctk_redraw();
347 } else if(w == (struct ctk_widget *)&erasebutton) {
348 ctk_dialog_open(&canceldialog);
349 ctk_window_redraw(&mainwindow);
350 } else if(w == (struct ctk_widget *)&cancelyesbutton) {
351 ctk_dialog_close();
352 ctk_redraw();
353 } else if(w == (struct ctk_widget *)&cancelnobutton) {
354 ctk_dialog_close();
355 ctk_redraw();
356 } else if(w == (struct ctk_widget *)&setupokbutton) {
357 applyconfig();
358 ctk_window_close(&setupwindow);
359 ctk_window_open(&mainwindow);
360 ctk_redraw();
361 } else {
362 for(i = 0; i < MAXNUMMSGS; ++i) {
363 if(w == (struct ctk_widget *)&msgbuttons[i]) {
364 ctk_window_close(&mainwindow);
365 make_window();
366 /* make_read(); download(i); */
367 ctk_window_open(&mainwindow);
368 ctk_window_redraw(&mainwindow);
369 break;
370 }
371 }
372 }
373 } else if(s == ctk_signal_menu_activate) {
374 if((struct ctk_menu *)data == &menu) {
375 if(menu.active == menuitem_open) {
376 ctk_window_open(&mainwindow);
377 } else if(menu.active == menuitem_setup) {
378 ctk_window_open(&setupwindow);
adamdunkels8af703e2003-04-08 11:50:20 +0000379 } else if(menu.active == menuitem_quit) {
adamdunkels0137b442003-04-08 23:27:33 +0000380 email_quit();
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000381 }
382 ctk_redraw();
383 }
adamdunkels8af703e2003-04-08 11:50:20 +0000384 } else if(s == ctk_signal_window_close &&
385 data == (ek_data_t)&mainwindow) {
adamdunkels0137b442003-04-08 23:27:33 +0000386 email_quit();
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000387 }
388}
389/*-----------------------------------------------------------------------------------*/
390void
391smtp_done(unsigned char error)
392{
393 ctk_label_set_text(&statuslabel, "SMTP done");
394 ctk_redraw();
395}
396/*-----------------------------------------------------------------------------------*/