• Main Page
  • Modules
  • Classes
  • Files
  • File List

recmail.cpp

00001 /*  Time Based Text - Mail Recorder
00002  *
00003  *  (C) Copyright 2006 Denis Rojo <jaromil@dyne.org>
00004  *
00005  * This is a text console editor for mails, it opens a text file
00006  * including mail headers and adds the Attach: of the .tbt recording
00007  * it is being developed for use as editor of mutt.
00008  *
00009  * This source code is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Public License as published 
00011  * by the Free Software Foundation; either version 2 of the License,
00012  * or (at your option) any later version.
00013  *
00014  * This source code is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017  * Please refer to the GNU Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Public License along with
00020  * this source code; if not, write to:
00021  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022  *
00023  */
00024 
00025 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <time.h>
00030 #include <errno.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <inttypes.h>
00034 
00035 #include <getopt.h>
00036 
00037 #include <sys/types.h>
00038 #include <sys/param.h>
00039 #include <sys/time.h>
00040 
00041 #include <assert.h>
00042 
00043 
00045 
00046 // S-Lang widgets
00047 #include <slw_console.h>
00048 #include <slw_text.h>
00049 // #include <slw_popup.h>
00050 
00051 #include <tbt.h> // Time Based Text
00052 
00053 #include <jutils.h> // my lovely utils
00054 void set_status(SLangWidget *s);
00055 
00056 static const char *help =
00057 "Usage: rectext [options] [file]\n"
00058 "\n"
00059 "  -h   print this help\n"
00060 "  -v   version information\n"
00061 "  -D   debug verbosity level - default 1\n"
00062 "  -t   include plain text in the mail\n";
00063 
00064 static const char *short_options = "-hvD:r:";
00065 
00066 int debug;
00067 char filename[512];
00068 bool plaintext = false;
00069 
00070 void cmdline(int argc, char **argv) {
00071   int res;
00072 
00073   debug = 1;
00074   filename[0] = 0x0;
00075 
00076   fprintf(stderr, "TBT - Time Based Text - recorder\n");
00077   fprintf(stderr, "      %s\n", VERSION);
00078 
00079   do {
00080     res = getopt(argc, argv, short_options);
00081     switch(res) {
00082     case 'h':
00083       fprintf(stderr, "%s", help);
00084       exit(0);
00085       break;
00086 
00087     case 'v':
00088       fprintf(stderr,"\n");
00089       exit(0);
00090       break;
00091 
00092     case 'D':
00093       debug = atoi(optarg);
00094       if(debug>3) {
00095         warning("debug verbosity ranges from 1 to 3\n");
00096         debug = 3;
00097       }
00098       break;
00099 
00100     case 't':
00101       plaintext = true;
00102       break;
00103               
00104 
00105     case 1:
00106       {
00107         FILE *fd;
00108         snprintf(filename,511,"%s", optarg);
00109         fd = fopen(filename, "r+");
00110         if(!fd) {
00111           error("can't write in %s: %s", filename, strerror(errno));
00112           exit(1);
00113         } else fclose(fd);
00114       }
00115       
00116     default: break;
00117       
00118     }
00119   } while (res != -1);
00120 
00121   for(;optind<argc;optind++)
00122     snprintf(filename, 511, "%s", argv[optind]);
00123 
00124   set_debug(debug);
00125 
00126 }
00127 
00128 // our global console
00129 SLangConsole con;
00130 
00131 // our widgets
00132 SLW_Text txt;
00133 SLW_Text status;
00134 //SLW_Popup popup;
00135  
00136 // Time Based Text object
00137 TBT tbt;
00138 
00139 int main(int argc, char** argv)
00140 {
00141   int key;
00142   bool quit = false;
00143   FILE *mailfd;
00144   char tbtfile[512];
00145 
00146   cmdline(argc, argv);
00147 
00148   if(!filename[0]) {
00149     error("no file supplied on the commandline, see usage instructions");
00150     exit(0);
00151   }
00152 
00153   func("composing mail in %s",filename);
00154 
00155   // initialize the text console
00156   if(! con.init() ) exit(-1);
00157 
00158   //  initialize the status line
00159   status.border = false;
00160   status.set_name("status box");
00161   if(! con.place(&status, 0, con.h-10, con.w, con.h) ) {
00162     error("error placing the status widget");
00163     exit(-1);
00164   }
00165   assert ( status.init() );
00166   
00167   //  set the status widget *** only after placing it! ***
00168   set_status(&status);
00169   
00170 
00171   // initialize the text canvas
00172   txt.set_name("editor");
00173   // txt.border = true;
00174   if(! con.place(&txt, 0, 0, con.w, con.h-11) ) { //  con.w, con.h-20) ) {
00175           error("error placing the text widget");
00176           exit(-1);
00177   }
00178   assert ( txt.init() );
00179 
00180   // focus the text canvas
00181   con.focused = &txt;
00182  
00183 
00184   // start the TBT engine
00185   if(! tbt.init() ) {
00186     con.close();
00187     error("fatal error: can't start the TBT engine");
00188     exit(0);
00189   }
00190 
00191   // write out to the status widget
00192   notice("TBT - console recording - press Ctrl-c when done ");
00193 
00194 
00195 
00196   // open message file read-write
00197   mailfd = fopen(filename,"r+");
00198   if(!mailfd)
00199     error("can't open file %s: %s", filename, strerror(errno) );
00200   else {
00201 
00202     // parse the headers from the input file
00203     char hdrp[1024];
00204     long pos;
00205 
00206     // go to the last header
00207     while( fgets(hdrp, 1024, mailfd) ) {
00208       if(hdrp[0] == '\n') {
00209         // final part fund
00210         pos = ftell(mailfd);
00211         fseek(mailfd, pos-1, SEEK_SET);
00212         break;
00213       }
00214       func("parsed header - %s", hdrp);
00215     }
00216 
00217     // append the attach header to the message
00218     snprintf(tbtfile,511,"Attach: %s.tbt\n",filename);
00219     fputs(tbtfile, mailfd);
00220     fclose(mailfd);
00221 
00222     // form the filename.tbt
00223     snprintf(tbtfile,511,"%s.tbt", filename);
00224   }
00225       
00226   while(!quit) {
00227 
00228     key = con.getkey();
00229 
00230     if(key) {
00231 
00232       // save the key and timestamp
00233       tbt.append(key);
00234 
00235       // display the key
00236       con.feed(key);
00237 
00238       }
00239     
00240     if( ! con.refresh() ) quit = true;
00241 
00242     jsleep(0,1);
00243 
00244   }
00245 
00246   notice("Save and quit");
00247 
00248   act("rendering %s in binary format", tbtfile);
00249   tbt.save_bin( tbtfile );
00250 
00251   con.refresh();
00252 
00253   con.close();
00254 
00255   exit(0);  
00256 }

Generated on Mon Apr 9 2012 15:29:22 for TimeBasedText_code_doc by  doxygen 1.7.1