Back to index

courier  0.68.2
rfc3676parser.h
Go to the documentation of this file.
00001 #ifndef       rfc3676_h
00002 #define       rfc3676_h
00003 /*
00004 ** Copyright 2011 Double Precision, Inc.  See COPYING for
00005 ** distribution information.
00006 */
00007 
00008 /*
00009 */
00010 
00011 #include      "../rfc2045/rfc2045_config.h" /* VPATH */
00012 #include      "../unicode/unicode.h"
00013 #include      <stdlib.h>
00014 #include      <string.h>
00015 
00016 #ifdef  __cplusplus
00017 extern "C" {
00018 #endif
00019 
00020 #if 0
00021 }
00022 #endif
00023 
00024 typedef struct rfc3676_parser_struct *rfc3676_parser_t;
00025 
00026 /*
00027 ** Structure passed to rfc3676_parser_init().
00028 */
00029 
00030 struct rfc3676_parser_info {
00031 
00032        const char *charset;
00033        /*
00034        ** MIME charset parameter. String not used after rfc3676_parser_init()
00035        ** returns.
00036        */
00037 
00038        /* MIME format flowed flag set */
00039        int isflowed;
00040 
00041        /* MIME delsp=yes flag is set */
00042        int isdelsp;
00043 
00044        /*
00045        ** Callback - start of a line.
00046        **
00047        ** If this callback returns 0, normal parsing continues. If this
00048        ** callback returns a non-0 value, parsing stops and
00049        ** rfc3676_parse() or rfc3676_deinit() returns the non-0 value.
00050        */
00051 
00052        int (*line_begin)(size_t quote_level, /* Line's quote level */
00053                        void *arg);
00054 
00055        /*
00056        ** Callback - contents of the line, converted to unicode.
00057        ** May be invoked multiple times, consecutively.
00058        **
00059        ** If this callback returns 0, normal parsing continues. If this
00060        ** callback returns a non-0 value, parsing stops and
00061        ** rfc3676_parse() or rfc3676_deinit() returns the non-0 value.
00062        */
00063 
00064        int (*line_contents)(const unicode_char *txt, /* Contents */
00065                           size_t txt_size,
00066                           /* Count of unicode chars in txt */
00067                           void *arg);
00068        /*
00069        ** Optional callback. If not NULL, it gets invoked when
00070        ** a line is logically flowed into the next physical line.
00071        */
00072 
00073        int (*line_flowed_notify)(void *);
00074 
00075        /*
00076        ** End of the line's contents.
00077        **
00078        ** If this callback returns 0, normal parsing continues. If this
00079        ** callback returns a non-0 value, parsing stops and
00080        ** rfc3676_parse() or rfc3676_deinit() returns the non-0 value.
00081        */
00082 
00083        int (*line_end)(void *arg);
00084 
00085        /* Argument passed through to the above callback methods */
00086 
00087        void *arg;
00088 };
00089 
00090 /*
00091 ** Begin parsing.
00092 **
00093 ** Returns an opaque parsing handle.
00094 */
00095 rfc3676_parser_t rfc3676parser_init(const struct rfc3676_parser_info *info);
00096 
00097 /*
00098 ** Parse next part of rfc3676-encoded message.
00099 **
00100 ** Returns non-0 value returned by any callback method, or 0 if all
00101 ** invoked callback methods returned 0.
00102 */
00103 
00104 int rfc3676parser(rfc3676_parser_t handle,
00105                 const char *txt,
00106                 size_t txt_cnt);
00107 
00108 /*
00109 ** End parsing.
00110 **
00111 ** The handle gets destroyed, and the parsing finishes.
00112 **
00113 ** NOTE: rfc3676_deinit() WILL LIKELY invoke some leftover callback methods.
00114 **
00115 ** Returns non-0 value returned by any callback method, or 0 if all
00116 ** invoked callback methods returned 0.
00117 */
00118 
00119 int rfc3676parser_deinit(rfc3676_parser_t handle,
00120 
00121                       /*
00122                       ** Optional, if not NULL, set to indicate unicode
00123                       ** error.
00124                       */
00125                       int *errptr);
00126 
00127 #if 0
00128 {
00129 #endif
00130 
00131 #ifdef  __cplusplus
00132 }
00133 
00134 namespace mail {
00135 
00136        extern "C" int tpp_trampoline_line_begin(size_t, void *);
00137 
00138        extern "C" int tpp_trampoline_line_contents(const unicode_char *,
00139                                               size_t, void *);
00140 
00141        extern "C" int tpp_trampoline_line_flowed_notify(void *);
00142 
00143        extern "C" int tpp_trampoline_line_end(void *);
00144 
00145        /*
00146        ** C++ binding for the parser logic
00147        */
00148        class textplainparser {
00149 
00150               rfc3676_parser_t handle;
00151 
00152        public:
00153               textplainparser();
00154               ~textplainparser();
00155 
00156               /*
00157               ** Begin parsing. Returns FALSE if the parsing could
00158               ** not be initialized (probably unknown charset).
00159               */
00160               bool begin(const std::string &charset,
00161                         bool flowed,
00162                         bool delsp);
00163 
00164               void end(
00165                       /*
00166                       ** Set to true if a unicode conversion error occured.
00167                       */
00168                       bool &unicode_errflag);
00169 
00170               void end()
00171               {
00172                      bool dummy;
00173 
00174                      return end(dummy);
00175               }
00176 
00177               /* Feed raw contents to be parsed */
00178               void operator<<(const std::string &text)
00179               {
00180                      if (handle)
00181                             rfc3676parser(handle, text.c_str(),
00182                                          text.size());
00183               }
00184 
00185 
00186               virtual void line_begin(size_t);
00187 
00188               virtual void line_contents(const unicode_char *,
00189                                       size_t);
00190 
00191               virtual void line_flowed_notify();
00192 
00193               virtual void line_end();
00194        };
00195 }
00196 #endif
00197 
00198 #endif