adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame^] | 1 | /* |
| 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 | * |
| 35 | * $Id: email.c,v 1.1 2003/03/19 14:13:32 adamdunkels Exp $ |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | |
| 40 | #include "ctk.h" |
| 41 | #include "dispatcher.h" |
| 42 | #include "smtp.h" |
| 43 | #include "uip_main.h" |
| 44 | #include "petsciiconv.h" |
| 45 | |
| 46 | #define MAXNUMMSGS 6 |
| 47 | |
| 48 | static struct ctk_menu menu; |
| 49 | unsigned char menuitem_open, menuitem_setup; |
| 50 | |
| 51 | /* The main window. */ |
| 52 | static struct ctk_window mainwindow; |
| 53 | |
| 54 | static struct ctk_button newmailbutton = |
| 55 | {CTK_BUTTON(26, 0, 8, "New mail")}; |
| 56 | static struct ctk_button checkbutton = |
| 57 | {CTK_BUTTON(0, 0, 10, "Check mail")}; |
| 58 | |
| 59 | static struct ctk_button msgbuttons[MAXNUMMSGS]; |
| 60 | static struct ctk_label msglabels[MAXNUMMSGS]; |
| 61 | static char msgtitles[20][MAXNUMMSGS]; |
| 62 | static struct ctk_separator sep1 = |
| 63 | {CTK_SEPARATOR(0, 7, 36)}; |
| 64 | static struct ctk_separator sep2 = |
| 65 | {CTK_SEPARATOR(0, 20, 36)}; |
| 66 | static struct ctk_label statuslabel = |
| 67 | {CTK_LABEL(6, 21, 23, 1, "")}; |
| 68 | |
| 69 | |
| 70 | static struct ctk_label tolabel = |
| 71 | {CTK_LABEL(0, 8, 3, 1, "To:")}; |
| 72 | static char to[40]; |
| 73 | static struct ctk_textentry totextentry = |
| 74 | {CTK_TEXTENTRY(8, 8, 26, 1, to, 38)}; |
| 75 | |
| 76 | static struct ctk_label cclabel = |
| 77 | {CTK_LABEL(0, 9, 3, 1, "Cc:")}; |
| 78 | static char cc[40]; |
| 79 | static struct ctk_textentry cctextentry = |
| 80 | {CTK_TEXTENTRY(8, 9, 26, 1, cc, 38)}; |
| 81 | |
| 82 | static struct ctk_label subjectlabel = |
| 83 | {CTK_LABEL(0, 10, 8, 1, "Subject:")}; |
| 84 | static char subject[40]; |
| 85 | static struct ctk_textentry subjecttextentry = |
| 86 | {CTK_TEXTENTRY(8, 10, 26, 1, subject, 38)}; |
| 87 | |
| 88 | static char mail[36*9]; |
| 89 | static struct ctk_textentry mailtextentry = |
| 90 | {CTK_TEXTENTRY(0, 11, 34, 9, mail, 36)}; |
| 91 | |
| 92 | static struct ctk_button sendbutton = |
| 93 | {CTK_BUTTON(0, 21, 4, "Send")}; |
| 94 | static struct ctk_button erasebutton = |
| 95 | {CTK_BUTTON(29, 21, 5, "Erase")}; |
| 96 | |
| 97 | /* The "Really cancel message?" dialog. */ |
| 98 | static struct ctk_window canceldialog; |
| 99 | static struct ctk_label canceldialoglabel1 = |
| 100 | {CTK_LABEL(2, 1, 22, 1, "Really cancel message?")}; |
| 101 | static struct ctk_label canceldialoglabel2 = |
| 102 | {CTK_LABEL(0, 2, 26, 1, "All contents will be lost.")}; |
| 103 | static struct ctk_button cancelyesbutton = |
| 104 | {CTK_BUTTON(4, 4, 3, "Yes")}; |
| 105 | static struct ctk_button cancelnobutton = |
| 106 | {CTK_BUTTON(18, 8, 2, "No")}; |
| 107 | |
| 108 | /* The setup window. */ |
| 109 | static struct ctk_window setupwindow; |
| 110 | static struct ctk_label fromaddresslabel = |
| 111 | {CTK_LABEL(0, 0, 25, 1, "Name and e-mail address")}; |
| 112 | static char fromaddress[40]; |
| 113 | static struct ctk_textentry fromaddresstextentry = |
| 114 | {CTK_TEXTENTRY(0, 1, 25, 1, fromaddress, 39)}; |
| 115 | |
| 116 | static struct ctk_label smtpserverlabel = |
| 117 | {CTK_LABEL(0, 3, 20, 1, "Outgoing mailserver")}; |
| 118 | static char smtpserver[40]; |
| 119 | static struct ctk_textentry smtpservertextentry = |
| 120 | {CTK_TEXTENTRY(0, 4, 25, 1, smtpserver, 39)}; |
| 121 | |
| 122 | static struct ctk_label pop3serverlabel = |
| 123 | {CTK_LABEL(0, 6, 20, 1, "Incoming mailserver")}; |
| 124 | static char pop3server[40]; |
| 125 | static struct ctk_textentry pop3servertextentry = |
| 126 | {CTK_TEXTENTRY(0, 7, 25, 1, pop3server, 39)}; |
| 127 | |
| 128 | static struct ctk_label pop3userlabel = |
| 129 | {CTK_LABEL(0, 9, 20, 1, "Mailserver username")}; |
| 130 | static char pop3user[40]; |
| 131 | static struct ctk_textentry pop3usertextentry = |
| 132 | {CTK_TEXTENTRY(0, 10, 25, 1, pop3user, 39)}; |
| 133 | |
| 134 | static struct ctk_label pop3passwordlabel = |
| 135 | {CTK_LABEL(0, 12, 20, 1, "Mailserver password")}; |
| 136 | static char pop3password[40]; |
| 137 | static struct ctk_textentry pop3passwordtextentry = |
| 138 | {CTK_TEXTENTRY(0, 13, 25, 1, pop3password, 39)}; |
| 139 | |
| 140 | |
| 141 | static struct ctk_button setupokbutton = |
| 142 | {CTK_BUTTON(24, 15, 2, "Ok")}; |
| 143 | |
| 144 | static void sighandler(ek_signal_t s, ek_data_t data); |
| 145 | static struct dispatcher_proc p = |
| 146 | {DISPATCHER_PROC("E-mail client", NULL, sighandler, smtp_appcall)}; |
| 147 | static ek_id_t id; |
| 148 | |
| 149 | /*-----------------------------------------------------------------------------------*/ |
| 150 | static void |
| 151 | make_window(void) |
| 152 | { |
| 153 | unsigned char i; |
| 154 | struct ctk_button *button; |
| 155 | struct ctk_label *label; |
| 156 | |
| 157 | /* Create the main window. */ |
| 158 | ctk_window_new(&mainwindow, 36, 22, "E-mail"); |
| 159 | ctk_window_move(&mainwindow, 1, 0); |
| 160 | |
| 161 | CTK_WIDGET_ADD(&mainwindow, &checkbutton); |
| 162 | CTK_WIDGET_FOCUS(&mainwindow, &checkbutton); |
| 163 | CTK_WIDGET_ADD(&mainwindow, &newmailbutton); |
| 164 | CTK_WIDGET_ADD(&mainwindow, &sep1); |
| 165 | CTK_WIDGET_ADD(&mainwindow, &sep2); |
| 166 | CTK_WIDGET_ADD(&mainwindow, &statuslabel); |
| 167 | |
| 168 | for(i = 0; i < MAXNUMMSGS; ++i) { |
| 169 | button = &msgbuttons[i]; |
| 170 | CTK_BUTTON_NEW(button, 0, i + 1, 1, " "); |
| 171 | CTK_WIDGET_ADD(&mainwindow, button); |
| 172 | label = &msglabels[i]; |
| 173 | CTK_LABEL_NEW(label, 3, i + 1, 33, 1, msgtitles[i]); |
| 174 | CTK_WIDGET_ADD(&mainwindow, label); |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | /*-----------------------------------------------------------------------------------*/ |
| 179 | static void |
| 180 | make_composer(void) |
| 181 | { |
| 182 | CTK_WIDGET_ADD(&mainwindow, &tolabel); |
| 183 | CTK_WIDGET_ADD(&mainwindow, &cclabel); |
| 184 | CTK_WIDGET_ADD(&mainwindow, &subjectlabel); |
| 185 | |
| 186 | CTK_WIDGET_ADD(&mainwindow, &totextentry); |
| 187 | CTK_WIDGET_FOCUS(&mainwindow, &totextentry); |
| 188 | CTK_WIDGET_ADD(&mainwindow, &cctextentry); |
| 189 | CTK_WIDGET_ADD(&mainwindow, &subjecttextentry); |
| 190 | |
| 191 | CTK_WIDGET_ADD(&mainwindow, &mailtextentry); |
| 192 | |
| 193 | CTK_WIDGET_ADD(&mainwindow, &sendbutton); |
| 194 | CTK_WIDGET_ADD(&mainwindow, &erasebutton); |
| 195 | |
| 196 | memset(mail, ' ', sizeof(mail)); |
| 197 | } |
| 198 | /*-----------------------------------------------------------------------------------*/ |
| 199 | static void |
| 200 | make_read(void) |
| 201 | { |
| 202 | CTK_WIDGET_ADD(&mainwindow, &tolabel); |
| 203 | CTK_WIDGET_ADD(&mainwindow, &cclabel); |
| 204 | CTK_WIDGET_ADD(&mainwindow, &subjectlabel); |
| 205 | |
| 206 | CTK_WIDGET_ADD(&mainwindow, &totextentry); |
| 207 | /* CTK_WIDGET_FOCUS(&mainwindow, &totextentry); */ |
| 208 | CTK_WIDGET_ADD(&mainwindow, &cctextentry); |
| 209 | CTK_WIDGET_ADD(&mainwindow, &subjecttextentry); |
| 210 | |
| 211 | CTK_WIDGET_ADD(&mainwindow, &mailtextentry); |
| 212 | |
| 213 | CTK_WIDGET_ADD(&mainwindow, &sendbutton); |
| 214 | CTK_WIDGET_ADD(&mainwindow, &erasebutton); |
| 215 | } |
| 216 | /*-----------------------------------------------------------------------------------*/ |
| 217 | void |
| 218 | email_init(void) |
| 219 | { |
| 220 | if(id == EK_ID_NONE) { |
| 221 | id = dispatcher_start(&p); |
| 222 | |
| 223 | /* Create the "Really cancel message?" dialog. */ |
| 224 | ctk_dialog_new(&canceldialog, 26, 6); |
| 225 | CTK_WIDGET_ADD(&canceldialog, &canceldialoglabel1); |
| 226 | CTK_WIDGET_ADD(&canceldialog, &canceldialoglabel2); |
| 227 | CTK_WIDGET_ADD(&canceldialog, &cancelyesbutton); |
| 228 | CTK_WIDGET_ADD(&canceldialog, &cancelnobutton); |
| 229 | CTK_WIDGET_FOCUS(&canceldialog, &cancelnobutton); |
| 230 | |
| 231 | /* Create setup window. */ |
| 232 | ctk_window_new(&setupwindow, 28, 16, "E-mail setup"); |
| 233 | ctk_window_move(&setupwindow, 5, 3); |
| 234 | |
| 235 | CTK_WIDGET_ADD(&setupwindow, &fromaddresslabel); |
| 236 | CTK_WIDGET_ADD(&setupwindow, &fromaddresstextentry); |
| 237 | CTK_WIDGET_ADD(&setupwindow, &smtpserverlabel); |
| 238 | CTK_WIDGET_ADD(&setupwindow, &smtpservertextentry); |
| 239 | CTK_WIDGET_ADD(&setupwindow, &pop3serverlabel); |
| 240 | CTK_WIDGET_ADD(&setupwindow, &pop3servertextentry); |
| 241 | CTK_WIDGET_ADD(&setupwindow, &pop3userlabel); |
| 242 | CTK_WIDGET_ADD(&setupwindow, &pop3usertextentry); |
| 243 | CTK_WIDGET_ADD(&setupwindow, &pop3passwordlabel); |
| 244 | CTK_WIDGET_ADD(&setupwindow, &pop3passwordtextentry); |
| 245 | CTK_WIDGET_ADD(&setupwindow, &setupokbutton); |
| 246 | |
| 247 | CTK_WIDGET_FOCUS(&setupwindow, &fromaddresstextentry); |
| 248 | |
| 249 | |
| 250 | /* Create main window. */ |
| 251 | make_window(); |
| 252 | make_composer(); |
| 253 | |
| 254 | /* Create and add the menu */ |
| 255 | ctk_menu_new(&menu, "E-mail"); |
| 256 | menuitem_setup = ctk_menuitem_add(&menu, "Setup"); |
| 257 | menuitem_open = ctk_menuitem_add(&menu, "Open"); |
| 258 | ctk_menu_add(&menu); |
| 259 | |
| 260 | /* Attach listeners to signals. */ |
| 261 | dispatcher_listen(ctk_signal_button_activate); |
| 262 | dispatcher_listen(ctk_signal_menu_activate); |
| 263 | |
| 264 | /* Open setup window */ |
| 265 | ctk_window_open(&setupwindow); |
| 266 | } else { |
| 267 | ctk_window_open(&mainwindow); |
| 268 | } |
| 269 | } |
| 270 | /*-----------------------------------------------------------------------------------*/ |
| 271 | static void |
| 272 | applyconfig(void) |
| 273 | { |
| 274 | u16_t addr[2]; |
| 275 | char *cptr; |
| 276 | |
| 277 | for(cptr = smtpserver; *cptr != ' ' && *cptr != 0; ++cptr); |
| 278 | *cptr = 0; |
| 279 | |
| 280 | if(uip_main_ipaddrconv(smtpserver, (unsigned char *)addr)) { |
| 281 | smtp_configure("contiki", addr); |
| 282 | } |
| 283 | } |
| 284 | /*-----------------------------------------------------------------------------------*/ |
| 285 | static void |
| 286 | prepare_message(void) |
| 287 | { |
| 288 | char *mptr1, *mptr2; |
| 289 | |
| 290 | /* Convert fields to ASCII. */ |
| 291 | petsciiconv_toascii(to, sizeof(to)); |
| 292 | petsciiconv_toascii(subject, sizeof(subject)); |
| 293 | petsciiconv_toascii(mail, 255); |
| 294 | petsciiconv_toascii(mail + 255, sizeof(mail) - 255); |
| 295 | |
| 296 | /* Insert line delimiters. */ |
| 297 | |
| 298 | } |
| 299 | /*-----------------------------------------------------------------------------------*/ |
| 300 | static void |
| 301 | sighandler(ek_signal_t s, ek_data_t data) |
| 302 | { |
| 303 | struct ctk_widget *w; |
| 304 | unsigned char i; |
| 305 | |
| 306 | if(s == ctk_signal_button_activate) { |
| 307 | w = (struct ctk_widget *)data; |
| 308 | if(w == (struct ctk_widget *)&newmailbutton) { |
| 309 | /* ctk_window_open(&composerwindow);*/ |
| 310 | ctk_window_close(&mainwindow); |
| 311 | make_window(); |
| 312 | make_composer(); |
| 313 | ctk_window_open(&mainwindow); |
| 314 | ctk_window_redraw(&mainwindow); |
| 315 | #if 0 |
| 316 | } else if(w == &replybutton) { |
| 317 | /* XXX Fiddle in the from and subject fields into the new |
| 318 | mail. */ |
| 319 | ctk_window_open(&composerwindow); |
| 320 | ctk_redraw(); |
| 321 | #endif |
| 322 | } else if(w == (struct ctk_widget *)&checkbutton) { |
| 323 | /* XXX Should actually check email. */ |
| 324 | ctk_label_set_text(&statuslabel, "Checking mail..."); |
| 325 | ctk_window_redraw(&mainwindow); |
| 326 | } else if(w == (struct ctk_widget *)&sendbutton) { |
| 327 | prepare_message(); |
| 328 | smtp_send(to, fromaddress, subject, mail, sizeof(mail)); |
| 329 | ctk_label_set_text(&statuslabel, "Sending message..."); |
| 330 | ctk_redraw(); |
| 331 | } else if(w == (struct ctk_widget *)&erasebutton) { |
| 332 | ctk_dialog_open(&canceldialog); |
| 333 | ctk_window_redraw(&mainwindow); |
| 334 | } else if(w == (struct ctk_widget *)&cancelyesbutton) { |
| 335 | ctk_dialog_close(); |
| 336 | ctk_redraw(); |
| 337 | } else if(w == (struct ctk_widget *)&cancelnobutton) { |
| 338 | ctk_dialog_close(); |
| 339 | ctk_redraw(); |
| 340 | } else if(w == (struct ctk_widget *)&setupokbutton) { |
| 341 | applyconfig(); |
| 342 | ctk_window_close(&setupwindow); |
| 343 | ctk_window_open(&mainwindow); |
| 344 | ctk_redraw(); |
| 345 | } else { |
| 346 | for(i = 0; i < MAXNUMMSGS; ++i) { |
| 347 | if(w == (struct ctk_widget *)&msgbuttons[i]) { |
| 348 | ctk_window_close(&mainwindow); |
| 349 | make_window(); |
| 350 | /* make_read(); download(i); */ |
| 351 | ctk_window_open(&mainwindow); |
| 352 | ctk_window_redraw(&mainwindow); |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } else if(s == ctk_signal_menu_activate) { |
| 358 | if((struct ctk_menu *)data == &menu) { |
| 359 | if(menu.active == menuitem_open) { |
| 360 | ctk_window_open(&mainwindow); |
| 361 | } else if(menu.active == menuitem_setup) { |
| 362 | ctk_window_open(&setupwindow); |
| 363 | } |
| 364 | ctk_redraw(); |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | } |
| 369 | /*-----------------------------------------------------------------------------------*/ |
| 370 | void |
| 371 | smtp_done(unsigned char error) |
| 372 | { |
| 373 | ctk_label_set_text(&statuslabel, "SMTP done"); |
| 374 | ctk_redraw(); |
| 375 | } |
| 376 | /*-----------------------------------------------------------------------------------*/ |