~ubuntu-branches/ubuntu/utopic/libesmtp/utopic

« back to all changes in this revision

Viewing changes to message-callbacks.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy T. Bouse
  • Date: 2002-03-06 08:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020306083748-ihmt32mddsslvg5i
Tags: upstream-0.8.11
ImportĀ upstreamĀ versionĀ 0.8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of libESMTP, a library for submission of RFC 2822
 
3
 *  formatted electronic mail messages using the SMTP protocol described
 
4
 *  in RFC 2821.
 
5
 *
 
6
 *  Copyright (C) 2001,2002  Brian Stafford  <brian@stafford.uklinux.net>
 
7
 *
 
8
 *  This library is free software; you can redistribute it and/or
 
9
 *  modify it under the terms of the GNU Lesser General Public
 
10
 *  License as published by the Free Software Foundation; either
 
11
 *  version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 *  This library is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 *  Lesser General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU Lesser General Public
 
19
 *  License along with this library; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 */
 
22
 
 
23
/* Standard callback functions for use by message-source.c
 
24
   An application requiring anything more sophisticated than either of
 
25
   these will need to supply its own callback.  */
 
26
#ifdef HAVE_CONFIG_H
 
27
#include <config.h>
 
28
#endif
 
29
 
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
#include "libesmtp.h"
 
34
 
 
35
/* Callback function to read the message from a file.  The file MUST be
 
36
   formatted according to RFC 2822 and lines MUST be terminated with the
 
37
   canonical CRLF sequence.  Furthermore, RFC 2821 line length
 
38
   limitations must be observed (1000 octets maximum). */
 
39
#define BUFLEN  8192
 
40
 
 
41
const char *
 
42
_smtp_message_fp_cb (void **ctx, int *len, void *arg)
 
43
{
 
44
  if (*ctx == NULL)
 
45
    *ctx = malloc (BUFLEN);
 
46
 
 
47
  if (len == NULL)
 
48
    {
 
49
      rewind ((FILE *) arg);
 
50
      return NULL;
 
51
    }
 
52
 
 
53
  *len = fread (*ctx, 1, BUFLEN, (FILE *) arg);
 
54
  return *ctx;
 
55
}
 
56
 
 
57
struct state
 
58
  {
 
59
    int state;
 
60
    int length;
 
61
  };
 
62
 
 
63
const char *
 
64
_smtp_message_str_cb (void **ctx, int *len, void *arg)
 
65
{
 
66
  const char *string = arg;
 
67
  struct state *state;
 
68
 
 
69
  if (*ctx == NULL)
 
70
    *ctx = malloc (sizeof (struct state));
 
71
  state = *ctx;
 
72
 
 
73
  if (len == NULL)
 
74
    {
 
75
      state->state = 0;
 
76
      state->length = strlen (string);
 
77
      return NULL;
 
78
    }
 
79
 
 
80
  switch (state->state)
 
81
    {
 
82
    case 0:
 
83
      state->state = 1;
 
84
      *len = state->length;
 
85
      break;
 
86
 
 
87
    default:
 
88
      *len = 0;
 
89
      break;
 
90
    }
 
91
  return string;
 
92
}