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 name Name of variable to set 00171 * \param value Value of variable to set 00172 * 00173 * \retval 0 success 00174 * \retval -1 failure 00175 */ 00176 int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value); 00177 00178 /*! 00179 * \brief Set a variable on the message being sent to a message tech directly. 00180 * \note Setting a variable that already exists overwrites the existing variable value 00181 * 00182 * \param name Name of variable to set 00183 * \param value Value of variable to set 00184 * 00185 * \retval 0 success 00186 * \retval -1 failure 00187 */ 00188 int ast_msg_set_var_outbound(struct ast_msg *msg, const char *name, const char *value); 00189 00190 /*! 00191 * \brief Get the specified variable on the message 00192 * \note The return value is valid only as long as the ast_message is valid. Hold a reference 00193 * to the message if you plan on storing the return value. Do re-set the same 00194 * message var name while holding a pointer to the result of this function. 00195 * 00196 * \return The value associated with variable "name". NULL if variable not found. 00197 */ 00198 const char *ast_msg_get_var(struct ast_msg *msg, const char *name); 00199 00200 /*! 00201 * \brief Get the body of a message. 00202 * \note The return value is valid only as long as the ast_message is valid. Hold a reference 00203 * to the message if you plan on storing the return value. 00204 * 00205 * \return The body of the messsage, encoded in UTF-8. 00206 */ 00207 const char *ast_msg_get_body(const struct ast_msg *msg); 00208 00209 /*! 00210 * \brief Queue a message for routing through the dialplan. 00211 * 00212 * Regardless of the return value of this function, this funciton will take 00213 * care of ensuring that the message object is properly destroyed when needed. 00214 * 00215 * \retval 0 message successfully queued 00216 * \retval non-zero failure, message not sent to dialplan 00217 */ 00218 int ast_msg_queue(struct ast_msg *msg); 00219 00220 /*! 00221 * \brief Send a msg directly to an endpoint. 00222 * 00223 * Regardless of the return value of this function, this funciton will take 00224 * care of ensuring that the message object is properly destroyed when needed. 00225 * 00226 * \retval 0 message successfully queued to be sent out 00227 * \retval non-zero failure, message not get sent out. 00228 */ 00229 int ast_msg_send(struct ast_msg *msg, const char *to, const char *from); 00230 00231 /*! 00232 * \brief Opaque iterator for msg variables 00233 */ 00234 struct ast_msg_var_iterator; 00235 00236 /*! 00237 * \brief Create a new message variable iterator 00238 * \param msg A message whose variables are to be iterated over 00239 * 00240 * \return An opaque pointer to the new iterator 00241 */ 00242 struct ast_msg_var_iterator *ast_msg_var_iterator_init(const struct ast_msg *msg); 00243 00244 /*! 00245 * \brief Get the next variable name and value that is set for sending outbound 00246 * \param msg The message with the variables 00247 * \param i An iterator created with ast_msg_var_iterator_init 00248 * \param name A pointer to the name result pointer 00249 * \param value A pointer to the value result pointer 00250 * 00251 * \retval 0 No more entries 00252 * \retval 1 Valid entry 00253 */ 00254 int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *i, const char **name, const char **value); 00255 00256 /*! 00257 * \brief Destroy a message variable iterator 00258 * \param i Iterator to be destroyed 00259 */ 00260 void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *i); 00261 00262 /*! 00263 * \brief Unref a message var from inside an iterator loop 00264 */ 00265 void ast_msg_var_unref_current(struct ast_msg_var_iterator *i); 00266 00267 #if defined(__cplusplus) || defined(c_plusplus) 00268 } 00269 #endif 00270 00271 #endif /* __AST_MESSAGE_H__ */