Sat Jul 12 2014 17:18:34

Asterisk developer's documentation


message.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  *
00022  * \brief Out-of-call text message support
00023  *
00024  * \author Russell Bryant <russell@digium.com>
00025  *
00026  * The purpose of this API is to provide support for text messages that
00027  * are not session based.  The messages are passed into the Asterisk core
00028  * to be routed through the dialplan and potentially sent back out through
00029  * a message technology that has been registered through this API.
00030  */
00031 
00032 #ifndef __AST_MESSAGE_H__
00033 #define __AST_MESSAGE_H__
00034 
00035 #if defined(__cplusplus) || defined(c_plusplus)
00036 extern "C" {
00037 #endif
00038 
00039 /*!
00040  * \brief A text message.
00041  *
00042  * This is an opaque type that represents a text message.
00043  */
00044 struct ast_msg;
00045 
00046 /*!
00047  * \brief A message technology
00048  *
00049  * A message technology is capable of transmitting text messages.
00050  */
00051 struct ast_msg_tech {
00052    /*!
00053     * \brief Name of this message technology
00054     *
00055     * This is the name that comes at the beginning of a URI for messages
00056     * that should be sent to this message technology implementation.
00057     * For example, messages sent to "xmpp:rbryant@digium.com" would be
00058     * passed to the ast_msg_tech with a name of "xmpp".
00059     */
00060    const char * const name;
00061    /*!
00062     * \brief Send a message.
00063     *
00064     * \param msg the message to send
00065     * \param to the URI of where the message is being sent
00066     * \param from the URI of where the message was sent from
00067     *
00068     * The fields of the ast_msg are guaranteed not to change during the
00069     * duration of this function call.
00070     *
00071     * \retval 0 success
00072     * \retval non-zero failure
00073     */
00074    int (* const msg_send)(const struct ast_msg *msg, const char *to, const char *from);
00075 };
00076 
00077 /*!
00078  * \brief Register a message technology
00079  *
00080  * \retval 0 success
00081  * \retval non-zero failure
00082  */
00083 int ast_msg_tech_register(const struct ast_msg_tech *tech);
00084 
00085 /*!
00086  * \brief Unregister a message technology.
00087  *
00088  * \retval 0 success
00089  * \retval non-zero failure
00090  */
00091 int ast_msg_tech_unregister(const struct ast_msg_tech *tech);
00092 
00093 /*!
00094  * \brief Allocate a message.
00095  *
00096  * Allocate a message for the purposes of passing it into the Asterisk core
00097  * to be routed through the dialplan.  If ast_msg_queue() is not called, this
00098  * message must be destroyed using ast_msg_destroy().  Otherwise, the message
00099  * core code will take care of it.
00100  *
00101  * \return A message object. This function will return NULL if an allocation
00102  *         error occurs.
00103  */
00104 struct ast_msg *ast_msg_alloc(void);
00105 
00106 /*!
00107  * \brief Destroy an ast_msg
00108  *
00109  * This should only be called on a message if it was not
00110  * passed on to ast_msg_queue().
00111  *
00112  * \return NULL, always.
00113  */
00114 struct ast_msg *ast_msg_destroy(struct ast_msg *msg);
00115 
00116 /*!
00117  * \brief Bump a msg's ref count
00118  */
00119 struct ast_msg *ast_msg_ref(struct ast_msg *msg);
00120 
00121 /*!
00122  * \brief Set the 'to' URI of a message
00123  *
00124  * \retval 0 success
00125  * \retval -1 failure
00126  */
00127 int __attribute__((format(printf, 2, 3)))
00128       ast_msg_set_to(struct ast_msg *msg, const char *fmt, ...);
00129 
00130 /*!
00131  * \brief Set the 'from' URI of a message
00132  *
00133  * \retval 0 success
00134  * \retval -1 failure
00135  */
00136 int __attribute__((format(printf, 2, 3)))
00137       ast_msg_set_from(struct ast_msg *msg, const char *fmt, ...);
00138 
00139 /*!
00140  * \brief Set the 'body' text of a message (in UTF-8)
00141  *
00142  * \retval 0 success
00143  * \retval -1 failure
00144  */
00145 int __attribute__((format(printf, 2, 3)))
00146       ast_msg_set_body(struct ast_msg *msg, const char *fmt, ...);
00147 
00148 /*!
00149  * \brief Set the dialplan context for this message
00150  *
00151  * \retval 0 success
00152  * \retval -1 failure
00153  */
00154 int __attribute__((format(printf, 2, 3)))
00155       ast_msg_set_context(struct ast_msg *msg, const char *fmt, ...);
00156 
00157 /*!
00158  * \brief Set the dialplan extension for this message
00159  *
00160  * \retval 0 success
00161  * \retval -1 failure
00162  */
00163 int __attribute__((format(printf, 2, 3)))
00164       ast_msg_set_exten(struct ast_msg *msg, const char *fmt, ...);
00165    
00166 /*!
00167  * \brief Set a variable on the message going to the dialplan.
00168  * \note Setting a variable that already exists overwrites the existing variable value
00169  *
00170  * \param msg
00171  * \param name Name of variable to set
00172  * \param value Value of variable to set
00173  *
00174  * \retval 0 success
00175  * \retval -1 failure
00176  */
00177 int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value);
00178 
00179 /*!
00180  * \brief Set a variable on the message being sent to a message tech directly.
00181  * \note Setting a variable that already exists overwrites the existing variable value
00182  *
00183  * \param msg
00184  * \param name Name of variable to set
00185  * \param value Value of variable to set
00186  *
00187  * \retval 0 success
00188  * \retval -1 failure
00189  */
00190 int ast_msg_set_var_outbound(struct ast_msg *msg, const char *name, const char *value);
00191 
00192 /*!
00193  * \brief Get the specified variable on the message
00194  * \note The return value is valid only as long as the ast_message is valid. Hold a reference
00195  *       to the message if you plan on storing the return value. Do re-set the same
00196  *       message var name while holding a pointer to the result of this function.
00197  *
00198  * \return The value associated with variable "name". NULL if variable not found.
00199  */
00200 const char *ast_msg_get_var(struct ast_msg *msg, const char *name);
00201 
00202 /*!
00203  * \brief Get the body of a message.
00204  * \note The return value is valid only as long as the ast_message is valid. Hold a reference
00205  *       to the message if you plan on storing the return value. 
00206  *
00207  * \return The body of the messsage, encoded in UTF-8.
00208  */
00209 const char *ast_msg_get_body(const struct ast_msg *msg);
00210 
00211 /*!
00212  * \brief Queue a message for routing through the dialplan.
00213  *
00214  * Regardless of the return value of this function, this funciton will take
00215  * care of ensuring that the message object is properly destroyed when needed.
00216  *
00217  * \retval 0 message successfully queued
00218  * \retval non-zero failure, message not sent to dialplan
00219  */
00220 int ast_msg_queue(struct ast_msg *msg);
00221 
00222 /*!
00223  * \brief Send a msg directly to an endpoint.
00224  *
00225  * Regardless of the return value of this function, this funciton will take
00226  * care of ensuring that the message object is properly destroyed when needed.
00227  *
00228  * \retval 0 message successfully queued to be sent out
00229  * \retval non-zero failure, message not get sent out.
00230  */
00231 int ast_msg_send(struct ast_msg *msg, const char *to, const char *from);
00232 
00233 /*!
00234  * \brief Opaque iterator for msg variables
00235  */
00236 struct ast_msg_var_iterator;
00237 
00238 /*!
00239  * \brief Create a new message variable iterator
00240  * \param msg A message whose variables are to be iterated over
00241  *
00242  * \return An opaque pointer to the new iterator
00243  */
00244 struct ast_msg_var_iterator *ast_msg_var_iterator_init(const struct ast_msg *msg);
00245 
00246 /*!
00247  * \brief Get the next variable name and value that is set for sending outbound
00248  * \param msg The message with the variables
00249  * \param iter An iterator created with ast_msg_var_iterator_init
00250  * \param name A pointer to the name result pointer
00251  * \param value A pointer to the value result pointer
00252  *
00253  * \retval 0 No more entries
00254  * \retval 1 Valid entry
00255  */
00256 int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *iter, const char **name, const char **value);
00257 
00258 /*!
00259  * \brief Destroy a message variable iterator
00260  * \param iter Iterator to be destroyed
00261  */
00262 void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter);
00263 
00264 /*!
00265  * \brief Unref a message var from inside an iterator loop
00266  */
00267 void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter);
00268 
00269 #if defined(__cplusplus) || defined(c_plusplus)
00270 }
00271 #endif
00272 
00273 #endif /* __AST_MESSAGE_H__ */