1 /* spmfilter - mail filtering framework
2  * Copyright (C) 2009-2012 Axel Steiner and SpaceNet AG
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*!
19  * @file smf_core.h
20  * @brief Various helper functions
21  */
22 
23 #ifndef _SMF_CORE_H
24 #define	_SMF_CORE_H
25 
26 /*!
27  * @fn char *smf_core_strstrip(char *s)
28  * @brief Removes leading and trailing whitespace from a string.
29  *        This function doesn't allocate or reallocate any memory; it
30  *        modifies string in place. The pointer to string is returned
31  *        to allow the nesting of functions.
32  * @param s String to parse.
33  * @return intput string
34  */
35 char *smf_core_strstrip(char *s);
36 
37 /*!
38  * @fn char *smf_core_strlwc(char *s)
39  * @brief Convert a string to lowercase.
40  * @param s String to convert.
41  * @return ptr to string
42  */
43 char *smf_core_strlwc(char *s);
44 
45 /*!
46  * @fn char *smf_core_strcat_printf(char **s, const char *fmt, ...)
47  * @brief Append format string to string and reallocate memory.
48  *        The pointer to string is returned to allow the nesting of functions.
49  * @param s string to append to
50  * @param fmt format string
51  * @return new appended string
52  */
53 char *smf_core_strcat_printf(char **s, const char *fmt, ...);
54 
55 /*!
56  * @fn char **smf_core_strsplit(const char *s, char *sep, int *nelems)
57  * @brief Split a given string
58  * @param s String to split
59  * @param sep separator
60  * @param nelems If set to non-NULL, then the function stores here the number of
61  *               elements in the resulting array.
62  * @return a newly-allocated NULL-terminated array of strings.
63  */
64 char **smf_core_strsplit(const char *s, char *sep, int *nelems);
65 
66 /*!
67  * @fn int smf_core_gen_queue_file(const char *queue_dir, char **tempname, const char *sid)
68  * @brief Generate a new queue file name
69  * @param queue_dir path to queue directory
70  * @param tempname pointer to unallocated buffer for filename, needs to
71  * free'd by caller if not required anymore
72  * @param sid current session id
73  * @return 0 on success or -1 in case of error
74  */
75 int smf_core_gen_queue_file(const char *queue_dir, char **tempname, const char *sid);
76 
77 /*!
78  * @fn char *smf_core_md5sum(const char *data)
79  * @brief Generate md5 hexdigest for string
80  * @param data String to generate md5sum for
81  * @returns Pointer to hexdigest string on success, NULL on error
82  */
83 char *smf_core_md5sum(const char *data);
84 
85 /*!
86  * @fn char *smf_core_get_maildir_filename(void)
87  * @brief Generates a unique maildir filename
88  * @return newly allocated pointer with generated filename
89  * or NULL in case of error
90  */
91 char *smf_core_get_maildir_filename(void);
92 
93 /*!
94  * @fn int smf_core_expand_string(const char *format, const char *addr, char **buf)
95  * @brief expands placeholders in a user querystring
96  * @param format format string to use as input
97  * @param addr email address to use for replacements
98  * @param buf pointer to unallocated buffer for expanded format string,
99  *        needs to free'd by caller if not required anymore
100  * @return the number of replacements made or -1 in case of error
101  */
102 int smf_core_expand_string(const char *format, const char *addr, char **buf);
103 
104 /*!
105  * @fn smf_core_copy_file(const char *source, const char *dest)
106  * @brief Copies the content of the source file into dest
107  * @param source Path of the source file
108  * @param dest Path of the destination file. If file already exists, then the current
109  *             content is replaced with the content of the source file.
110  * @return the number of bytes copied
111  */
112 int smf_core_copy_file(const char *source, const char *dest);
113 
114 /*!
115  * @fn int smf_core_copy_to_fd(const char *source, int dest)
116  * @brief Copies the content of the source file into an already open file-descriptor.
117  * This function is similar to smf_core_copy_file(). But rather than opening the destination
118  * file, this function uses an already open file-descriptor as a destination.
119  * @param source Path of the source file
120  * @param dest On open file-descriptior, where the content is written into.
121  * @return the number of bytes copied
122  */
123 int smf_core_copy_to_fd(const char *source, int dest);
124 
125 #endif	/* _SMF_CORE_H */
126 
127