message.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \brief Out-of-call text message support
  22. *
  23. * \author Russell Bryant <russell@digium.com>
  24. *
  25. * The purpose of this API is to provide support for text messages that
  26. * are not session based. The messages are passed into the Asterisk core
  27. * to be routed through the dialplan or another interface and potentially
  28. * sent back out through a message technology that has been registered
  29. * through this API.
  30. */
  31. #ifndef __AST_MESSAGE_H__
  32. #define __AST_MESSAGE_H__
  33. #if defined(__cplusplus) || defined(c_plusplus)
  34. extern "C" {
  35. #endif
  36. /*!
  37. * \brief A text message.
  38. *
  39. * This is an opaque type that represents a text message.
  40. */
  41. struct ast_msg;
  42. /*!
  43. * \brief A message technology
  44. *
  45. * A message technology is capable of transmitting text messages.
  46. */
  47. struct ast_msg_tech {
  48. /*!
  49. * \brief Name of this message technology
  50. *
  51. * This is the name that comes at the beginning of a URI for messages
  52. * that should be sent to this message technology implementation.
  53. * For example, messages sent to "xmpp:rbryant@digium.com" would be
  54. * passed to the ast_msg_tech with a name of "xmpp".
  55. */
  56. const char * const name;
  57. /*!
  58. * \brief Send a message.
  59. *
  60. * \param msg the message to send
  61. * \param to the URI of where the message is being sent
  62. * \param from the URI of where the message was sent from
  63. *
  64. * The fields of the ast_msg are guaranteed not to change during the
  65. * duration of this function call.
  66. *
  67. * \retval 0 success
  68. * \retval non-zero failure
  69. */
  70. int (* const msg_send)(const struct ast_msg *msg, const char *to, const char *from);
  71. };
  72. /*!
  73. * \brief Register a message technology
  74. *
  75. * \retval 0 success
  76. * \retval non-zero failure
  77. */
  78. int ast_msg_tech_register(const struct ast_msg_tech *tech);
  79. /*!
  80. * \brief Unregister a message technology.
  81. *
  82. * \retval 0 success
  83. * \retval non-zero failure
  84. */
  85. int ast_msg_tech_unregister(const struct ast_msg_tech *tech);
  86. /*!
  87. * \brief An external processor of received messages
  88. * \since 12.5.0
  89. */
  90. struct ast_msg_handler {
  91. /*!
  92. * \brief Name of the message handler
  93. */
  94. const char *name;
  95. /*!
  96. * \brief The function callback that will handle the message
  97. *
  98. * \param msg The message to handle
  99. *
  100. * \retval 0 The handler processed the message successfull
  101. * \retval non-zero The handler passed or could not process the message
  102. */
  103. int (* const handle_msg)(struct ast_msg *msg);
  104. /*!
  105. * \brief Return whether or not the message has a valid destination
  106. *
  107. * A message may be delivered to the dialplan and/or other locations,
  108. * depending on whether or not other handlers have been registered. This
  109. * function is called by the message core to determine if any handler can
  110. * process a message.
  111. *
  112. * \param msg The message to inspect
  113. *
  114. * \retval 0 The message does not have a valid destination
  115. * \retval 1 The message has a valid destination
  116. */
  117. int (* const has_destination)(const struct ast_msg *msg);
  118. };
  119. /*!
  120. * \brief Register a \c ast_msg_handler
  121. * \since 12.5.0
  122. *
  123. * \param handler The handler to register
  124. *
  125. * \retval 0 Success
  126. * \retval non-zero Error
  127. */
  128. int ast_msg_handler_register(const struct ast_msg_handler *handler);
  129. /*!
  130. * \brief Unregister a \c ast_msg_handler
  131. * \since 12.5.0
  132. *
  133. * \param handler The handler to unregister
  134. *
  135. * \retval 0 Success
  136. * \retval non-zero Error
  137. */
  138. int ast_msg_handler_unregister(const struct ast_msg_handler *handler);
  139. /*!
  140. * \brief Allocate a message.
  141. *
  142. * Allocate a message for the purposes of passing it into the Asterisk core
  143. * to be routed through the dialplan. If ast_msg_queue() is not called, this
  144. * message must be destroyed using ast_msg_destroy(). Otherwise, the message
  145. * core code will take care of it.
  146. *
  147. * \return A message object. This function will return NULL if an allocation
  148. * error occurs.
  149. */
  150. struct ast_msg *ast_msg_alloc(void);
  151. /*!
  152. * \brief Destroy an ast_msg
  153. *
  154. * This should only be called on a message if it was not
  155. * passed on to ast_msg_queue().
  156. *
  157. * \return NULL, always.
  158. */
  159. struct ast_msg *ast_msg_destroy(struct ast_msg *msg);
  160. /*!
  161. * \brief Bump a msg's ref count
  162. */
  163. struct ast_msg *ast_msg_ref(struct ast_msg *msg);
  164. /*!
  165. * \brief Set the 'to' URI of a message
  166. *
  167. * \retval 0 success
  168. * \retval -1 failure
  169. */
  170. int __attribute__((format(printf, 2, 3)))
  171. ast_msg_set_to(struct ast_msg *msg, const char *fmt, ...);
  172. /*!
  173. * \brief Set the 'from' URI of a message
  174. *
  175. * \retval 0 success
  176. * \retval -1 failure
  177. */
  178. int __attribute__((format(printf, 2, 3)))
  179. ast_msg_set_from(struct ast_msg *msg, const char *fmt, ...);
  180. /*!
  181. * \brief Set the 'body' text of a message (in UTF-8)
  182. *
  183. * \retval 0 success
  184. * \retval -1 failure
  185. */
  186. int __attribute__((format(printf, 2, 3)))
  187. ast_msg_set_body(struct ast_msg *msg, const char *fmt, ...);
  188. /*!
  189. * \brief Set the dialplan context for this message
  190. *
  191. * \retval 0 success
  192. * \retval -1 failure
  193. */
  194. int __attribute__((format(printf, 2, 3)))
  195. ast_msg_set_context(struct ast_msg *msg, const char *fmt, ...);
  196. /*!
  197. * \brief Set the dialplan extension for this message
  198. *
  199. * \retval 0 success
  200. * \retval -1 failure
  201. */
  202. int __attribute__((format(printf, 2, 3)))
  203. ast_msg_set_exten(struct ast_msg *msg, const char *fmt, ...);
  204. /*!
  205. * \brief Set the technology associated with this message
  206. *
  207. * \since 12.5.0
  208. *
  209. * \retval 0 success
  210. * \retval -1 failure
  211. */
  212. int __attribute__((format(printf, 2, 3)))
  213. ast_msg_set_tech(struct ast_msg *msg, const char *fmt, ...);
  214. /*!
  215. * \brief Set the technology's endpoint associated with this message
  216. *
  217. * \since 12.5.0
  218. *
  219. * \retval 0 success
  220. * \retval -1 failure
  221. */
  222. int __attribute__((format(printf, 2, 3)))
  223. ast_msg_set_endpoint(struct ast_msg *msg, const char *fmt, ...);
  224. /*!
  225. * \brief Set a variable on the message going to the dialplan.
  226. * \note Setting a variable that already exists overwrites the existing variable value
  227. *
  228. * \param msg
  229. * \param name Name of variable to set
  230. * \param value Value of variable to set
  231. *
  232. * \retval 0 success
  233. * \retval -1 failure
  234. */
  235. int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value);
  236. /*!
  237. * \brief Set a variable on the message being sent to a message tech directly.
  238. * \note Setting a variable that already exists overwrites the existing variable value
  239. *
  240. * \param msg
  241. * \param name Name of variable to set
  242. * \param value Value of variable to set
  243. *
  244. * \retval 0 success
  245. * \retval -1 failure
  246. */
  247. int ast_msg_set_var_outbound(struct ast_msg *msg, const char *name, const char *value);
  248. /*!
  249. * \brief Get the specified variable on the message
  250. * \note The return value is valid only as long as the ast_message is valid. Hold a reference
  251. * to the message if you plan on storing the return value. Do re-set the same
  252. * message var name while holding a pointer to the result of this function.
  253. *
  254. * \return The value associated with variable "name". NULL if variable not found.
  255. */
  256. const char *ast_msg_get_var(struct ast_msg *msg, const char *name);
  257. /*!
  258. * \brief Get the body of a message.
  259. * \note The return value is valid only as long as the ast_message is valid. Hold a reference
  260. * to the message if you plan on storing the return value.
  261. *
  262. * \return The body of the messsage, encoded in UTF-8.
  263. */
  264. const char *ast_msg_get_body(const struct ast_msg *msg);
  265. /*!
  266. * \brief Retrieve the source of this message
  267. *
  268. * \since 12.5.0
  269. *
  270. * \param msg The message to get the soure from
  271. *
  272. * \retval The source of the message
  273. * \retval NULL or empty string if the message has no source
  274. */
  275. const char *ast_msg_get_from(const struct ast_msg *msg);
  276. /*!
  277. * \brief Retrieve the destination of this message
  278. *
  279. * \since 12.5.0
  280. *
  281. * \param msg The message to get the destination from
  282. *
  283. * \retval The destination of the message
  284. * \retval NULL or empty string if the message has no destination
  285. */
  286. const char *ast_msg_get_to(const struct ast_msg *msg);
  287. /*!
  288. * \brief Retrieve the technology associated with this message
  289. *
  290. * \since 12.5.0
  291. *
  292. * \param msg The message to get the technology from
  293. *
  294. * \retval The technology of the message
  295. * \retval NULL or empty string if the message has no associated technology
  296. */
  297. const char *ast_msg_get_tech(const struct ast_msg *msg);
  298. /*!
  299. * \brief Retrieve the endpoint associated with this message
  300. *
  301. * \since 12.5.0
  302. *
  303. * \param msg The message to get the endpoint from
  304. *
  305. * \retval The endpoint associated with the message
  306. * \retval NULL or empty string if the message has no associated endpoint
  307. */
  308. const char *ast_msg_get_endpoint(const struct ast_msg *msg);
  309. /*!
  310. * \brief Determine if a particular message has a destination via some handler
  311. *
  312. * \since 12.5.0
  313. *
  314. * \param msg The message to check
  315. *
  316. * \retval 0 if the message has no handler that can find a destination
  317. * \retval 1 if the message has a handler that can find a destination
  318. */
  319. int ast_msg_has_destination(const struct ast_msg *msg);
  320. /*!
  321. * \brief Queue a message for routing through the dialplan.
  322. *
  323. * Regardless of the return value of this function, this funciton will take
  324. * care of ensuring that the message object is properly destroyed when needed.
  325. *
  326. * \retval 0 message successfully queued
  327. * \retval non-zero failure, message not sent to dialplan
  328. */
  329. int ast_msg_queue(struct ast_msg *msg);
  330. /*!
  331. * \brief Send a msg directly to an endpoint.
  332. *
  333. * Regardless of the return value of this function, this funciton will take
  334. * care of ensuring that the message object is properly destroyed when needed.
  335. *
  336. * \retval 0 message successfully queued to be sent out
  337. * \retval non-zero failure, message not get sent out.
  338. */
  339. int ast_msg_send(struct ast_msg *msg, const char *to, const char *from);
  340. /*!
  341. * \brief Opaque iterator for msg variables
  342. */
  343. struct ast_msg_var_iterator;
  344. /*!
  345. * \brief Create a new message variable iterator
  346. * \param msg A message whose variables are to be iterated over
  347. *
  348. * \return An opaque pointer to the new iterator
  349. */
  350. struct ast_msg_var_iterator *ast_msg_var_iterator_init(const struct ast_msg *msg);
  351. /*!
  352. * \brief Get the next variable name and value that is set for sending outbound
  353. * \param msg The message with the variables
  354. * \param iter An iterator created with ast_msg_var_iterator_init
  355. * \param name A pointer to the name result pointer
  356. * \param value A pointer to the value result pointer
  357. *
  358. * \retval 0 No more entries
  359. * \retval 1 Valid entry
  360. */
  361. int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *iter, const char **name, const char **value);
  362. /*!
  363. * \brief Destroy a message variable iterator
  364. * \param iter Iterator to be destroyed
  365. */
  366. void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter);
  367. /*!
  368. * \brief Unref a message var from inside an iterator loop
  369. */
  370. void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter);
  371. /*! \defgroup ast_msg_data Enhanced Messaging
  372. * @{
  373. * \page Messaging Enhanced Messaging
  374. *
  375. * The basic messaging framework has a basic drawback... It can only pass
  376. * a text string through the core. This causes several issues:
  377. * \li Only a content type of text/plain can be passed.
  378. * \li If a softmix bridge is used, the original sender identity is lost.
  379. *
  380. * The Enhanced Messaging framework allows attributes, such as "From", "To"
  381. * and "Content-Type" to be attached to the message by the incoming channel
  382. * tech which can then be used by the outgoing channel tech to construct
  383. * the appropriate technology-specific outgoing message.
  384. */
  385. /*!
  386. * \brief Structure used to transport an enhanced message through the frame core
  387. * \since 13.22.0
  388. * \since 15.5.0
  389. */
  390. struct ast_msg_data;
  391. enum ast_msg_data_source_type {
  392. AST_MSG_DATA_SOURCE_TYPE_UNKNOWN = 0,
  393. AST_MSG_DATA_SOURCE_TYPE_T140,
  394. AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG,
  395. AST_MSG_DATA_SOURCE_TYPE_OUT_OF_DIALOG,
  396. __AST_MSG_DATA_SOURCE_TYPE_LAST,
  397. };
  398. enum ast_msg_data_attribute_type {
  399. AST_MSG_DATA_ATTR_TO = 0,
  400. AST_MSG_DATA_ATTR_FROM,
  401. AST_MSG_DATA_ATTR_CONTENT_TYPE,
  402. AST_MSG_DATA_ATTR_BODY,
  403. __AST_MSG_DATA_ATTR_LAST,
  404. };
  405. struct ast_msg_data_attribute {
  406. enum ast_msg_data_attribute_type type;
  407. char *value;
  408. };
  409. /*!
  410. * \brief Allocates an ast_msg_data structure.
  411. * \since 13.22.0
  412. * \since 15.5.0
  413. *
  414. * \param source The source type of the message
  415. * \param attributes A pointer to an array of ast_msg_data_attribute structures
  416. * \param count The number of elements in the array
  417. *
  418. * \return Pointer to msg structure or NULL on allocation failure.
  419. * Caller must call ast_free when done.
  420. */
  421. struct ast_msg_data *ast_msg_data_alloc(enum ast_msg_data_source_type source,
  422. struct ast_msg_data_attribute attributes[], size_t count);
  423. /*!
  424. * \brief Clone an ast_msg_data structure
  425. * \since 13.22.0
  426. * \since 15.5.0
  427. *
  428. * \param msg The message to clone
  429. *
  430. * \return New message structure or NULL if there was an allocation failure.
  431. * Caller must call ast_free when done.
  432. */
  433. struct ast_msg_data *ast_msg_data_dup(struct ast_msg_data *msg);
  434. /*!
  435. * \brief Get length of the structure
  436. * \since 13.22.0
  437. * \since 15.5.0
  438. *
  439. * \param msg Pointer to ast_msg_data structure
  440. *
  441. * \return The length of the structure itself plus the dynamically allocated attribute buffer.
  442. */
  443. size_t ast_msg_data_get_length(struct ast_msg_data *msg);
  444. /*!
  445. * \brief Get "source type" from ast_msg_data
  446. * \since 13.22.0
  447. * \since 15.5.0
  448. *
  449. * \param msg Pointer to ast_msg_data structure
  450. *
  451. * \return The source type field.
  452. */
  453. enum ast_msg_data_source_type ast_msg_data_get_source_type(struct ast_msg_data *msg);
  454. /*!
  455. * \brief Get attribute from ast_msg_data
  456. * \since 13.22.0
  457. * \since 15.5.0
  458. *
  459. * \param msg Pointer to ast_msg_data structure
  460. * \param attribute_type One of ast_msg_data_attribute_type
  461. *
  462. * \return The attribute or an empty string ("") if the attribute wasn't set.
  463. */
  464. const char *ast_msg_data_get_attribute(struct ast_msg_data *msg,
  465. enum ast_msg_data_attribute_type attribute_type);
  466. /*!
  467. * \brief Queue an AST_FRAME_TEXT_DATA frame containing an ast_msg_data structure
  468. * \since 13.22.0
  469. * \since 15.5.0
  470. *
  471. * \param channel The channel on which to queue the frame
  472. * \param msg Pointer to ast_msg_data structure
  473. *
  474. * \retval -1 Error
  475. * \retval 0 Success
  476. */
  477. int ast_msg_data_queue_frame(struct ast_channel *channel, struct ast_msg_data *msg);
  478. /*!
  479. * @}
  480. */
  481. #if defined(__cplusplus) || defined(c_plusplus)
  482. }
  483. #endif
  484. #endif /* __AST_MESSAGE_H__ */