message.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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. /*! \file
  19. *
  20. * \brief Out-of-call text message support
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/datastore.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/manager.h"
  34. #include "asterisk/strings.h"
  35. #include "asterisk/astobj2.h"
  36. #include "asterisk/vector.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/taskprocessor.h"
  39. #include "asterisk/message.h"
  40. /*** DOCUMENTATION
  41. <function name="MESSAGE" language="en_US">
  42. <synopsis>
  43. Create a message or read fields from a message.
  44. </synopsis>
  45. <syntax argsep="/">
  46. <parameter name="argument" required="true">
  47. <para>Field of the message to get or set.</para>
  48. <enumlist>
  49. <enum name="to">
  50. <para>Read-only. The destination of the message. When processing an
  51. incoming message, this will be set to the destination listed as
  52. the recipient of the message that was received by Asterisk.</para>
  53. </enum>
  54. <enum name="from">
  55. <para>Read-only. The source of the message. When processing an
  56. incoming message, this will be set to the source of the message.</para>
  57. </enum>
  58. <enum name="custom_data">
  59. <para>Write-only. Mark or unmark all message headers for an outgoing
  60. message. The following values can be set:</para>
  61. <enumlist>
  62. <enum name="mark_all_outbound">
  63. <para>Mark all headers for an outgoing message.</para>
  64. </enum>
  65. <enum name="clear_all_outbound">
  66. <para>Unmark all headers for an outgoing message.</para>
  67. </enum>
  68. </enumlist>
  69. </enum>
  70. <enum name="body">
  71. <para>Read/Write. The message body. When processing an incoming
  72. message, this includes the body of the message that Asterisk
  73. received. When MessageSend() is executed, the contents of this
  74. field are used as the body of the outgoing message. The body
  75. will always be UTF-8.</para>
  76. </enum>
  77. </enumlist>
  78. </parameter>
  79. </syntax>
  80. <description>
  81. <para>This function will read from or write a value to a text message.
  82. It is used both to read the data out of an incoming message, as well as
  83. modify or create a message that will be sent outbound.</para>
  84. </description>
  85. <see-also>
  86. <ref type="application">MessageSend</ref>
  87. </see-also>
  88. </function>
  89. <function name="MESSAGE_DATA" language="en_US">
  90. <synopsis>
  91. Read or write custom data attached to a message.
  92. </synopsis>
  93. <syntax argsep="/">
  94. <parameter name="argument" required="true">
  95. <para>Field of the message to get or set.</para>
  96. </parameter>
  97. </syntax>
  98. <description>
  99. <para>This function will read from or write a value to a text message.
  100. It is used both to read the data out of an incoming message, as well as
  101. modify a message that will be sent outbound.</para>
  102. <note>
  103. <para>If you want to set an outbound message to carry data in the
  104. current message, do
  105. Set(MESSAGE_DATA(<replaceable>key</replaceable>)=${MESSAGE_DATA(<replaceable>key</replaceable>)}).</para>
  106. </note>
  107. </description>
  108. <see-also>
  109. <ref type="application">MessageSend</ref>
  110. </see-also>
  111. </function>
  112. <application name="MessageSend" language="en_US">
  113. <synopsis>
  114. Send a text message.
  115. </synopsis>
  116. <syntax>
  117. <parameter name="to" required="true">
  118. <para>A To URI for the message.</para>
  119. <xi:include xpointer="xpointer(/docs/info[@name='PJSIPMessageToInfo'])" />
  120. <xi:include xpointer="xpointer(/docs/info[@name='SIPMessageToInfo'])" />
  121. <xi:include xpointer="xpointer(/docs/info[@name='XMPPMessageToInfo'])" />
  122. </parameter>
  123. <parameter name="from" required="false">
  124. <para>A From URI for the message if needed for the
  125. message technology being used to send this message.</para>
  126. <xi:include xpointer="xpointer(/docs/info[@name='PJSIPMessageFromInfo'])" />
  127. <xi:include xpointer="xpointer(/docs/info[@name='SIPMessageFromInfo'])" />
  128. <xi:include xpointer="xpointer(/docs/info[@name='XMPPMessageFromInfo'])" />
  129. </parameter>
  130. </syntax>
  131. <description>
  132. <para>Send a text message. The body of the message that will be
  133. sent is what is currently set to <literal>MESSAGE(body)</literal>.
  134. The technology chosen for sending the message is determined
  135. based on a prefix to the <literal>to</literal> parameter.</para>
  136. <para>This application sets the following channel variables:</para>
  137. <variablelist>
  138. <variable name="MESSAGE_SEND_STATUS">
  139. <para>This is the message delivery status returned by this application.</para>
  140. <value name="INVALID_PROTOCOL">
  141. No handler for the technology part of the URI was found.
  142. </value>
  143. <value name="INVALID_URI">
  144. The protocol handler reported that the URI was not valid.
  145. </value>
  146. <value name="SUCCESS">
  147. Successfully passed on to the protocol handler, but delivery has not necessarily been guaranteed.
  148. </value>
  149. <value name="FAILURE">
  150. The protocol handler reported that it was unabled to deliver the message for some reason.
  151. </value>
  152. </variable>
  153. </variablelist>
  154. </description>
  155. </application>
  156. <manager name="MessageSend" language="en_US">
  157. <synopsis>
  158. Send an out of call message to an endpoint.
  159. </synopsis>
  160. <syntax>
  161. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  162. <parameter name="To" required="true">
  163. <para>The URI the message is to be sent to.</para>
  164. <xi:include xpointer="xpointer(/docs/info[@name='PJSIPMessageToInfo'])" />
  165. <xi:include xpointer="xpointer(/docs/info[@name='SIPMessageToInfo'])" />
  166. <xi:include xpointer="xpointer(/docs/info[@name='XMPPMessageToInfo'])" />
  167. </parameter>
  168. <parameter name="From">
  169. <para>A From URI for the message if needed for the
  170. message technology being used to send this message.</para>
  171. <xi:include xpointer="xpointer(/docs/info[@name='PJSIPMessageFromInfo'])" />
  172. <xi:include xpointer="xpointer(/docs/info[@name='SIPMessageFromInfo'])" />
  173. <xi:include xpointer="xpointer(/docs/info[@name='XMPPMessageFromInfo'])" />
  174. </parameter>
  175. <parameter name="Body">
  176. <para>The message body text. This must not contain any newlines as that
  177. conflicts with the AMI protocol.</para>
  178. </parameter>
  179. <parameter name="Base64Body">
  180. <para>Text bodies requiring the use of newlines have to be base64 encoded
  181. in this field. Base64Body will be decoded before being sent out.
  182. Base64Body takes precedence over Body.</para>
  183. </parameter>
  184. <parameter name="Variable">
  185. <para>Message variable to set, multiple Variable: headers are
  186. allowed. The header value is a comma separated list of
  187. name=value pairs.</para>
  188. </parameter>
  189. </syntax>
  190. </manager>
  191. ***/
  192. struct msg_data {
  193. AST_DECLARE_STRING_FIELDS(
  194. AST_STRING_FIELD(name);
  195. AST_STRING_FIELD(value);
  196. );
  197. unsigned int send; /* Whether to send out on outbound messages */
  198. };
  199. AST_LIST_HEAD_NOLOCK(outhead, msg_data);
  200. /*!
  201. * \brief A message.
  202. */
  203. struct ast_msg {
  204. AST_DECLARE_STRING_FIELDS(
  205. /*! Where the message is going */
  206. AST_STRING_FIELD(to);
  207. /*! Where we "say" the message came from */
  208. AST_STRING_FIELD(from);
  209. /*! The text to send */
  210. AST_STRING_FIELD(body);
  211. /*! The dialplan context for the message */
  212. AST_STRING_FIELD(context);
  213. /*! The dialplan extension for the message */
  214. AST_STRING_FIELD(exten);
  215. /*! An endpoint associated with this message */
  216. AST_STRING_FIELD(endpoint);
  217. /*! The technology of the endpoint associated with this message */
  218. AST_STRING_FIELD(tech);
  219. );
  220. /*! Technology/dialplan specific variables associated with the message */
  221. struct ao2_container *vars;
  222. };
  223. /*! \brief Lock for \c msg_techs vector */
  224. static ast_rwlock_t msg_techs_lock;
  225. /*! \brief Vector of message technologies */
  226. AST_VECTOR(, const struct ast_msg_tech *) msg_techs;
  227. /*! \brief Lock for \c msg_handlers vector */
  228. static ast_rwlock_t msg_handlers_lock;
  229. /*! \brief Vector of received message handlers */
  230. AST_VECTOR(, const struct ast_msg_handler *) msg_handlers;
  231. static struct ast_taskprocessor *msg_q_tp;
  232. static const char app_msg_send[] = "MessageSend";
  233. static void msg_ds_destroy(void *data);
  234. static const struct ast_datastore_info msg_datastore = {
  235. .type = "message",
  236. .destroy = msg_ds_destroy,
  237. };
  238. static int msg_func_read(struct ast_channel *chan, const char *function,
  239. char *data, char *buf, size_t len);
  240. static int msg_func_write(struct ast_channel *chan, const char *function,
  241. char *data, const char *value);
  242. static struct ast_custom_function msg_function = {
  243. .name = "MESSAGE",
  244. .read = msg_func_read,
  245. .write = msg_func_write,
  246. };
  247. static int msg_data_func_read(struct ast_channel *chan, const char *function,
  248. char *data, char *buf, size_t len);
  249. static int msg_data_func_write(struct ast_channel *chan, const char *function,
  250. char *data, const char *value);
  251. static struct ast_custom_function msg_data_function = {
  252. .name = "MESSAGE_DATA",
  253. .read = msg_data_func_read,
  254. .write = msg_data_func_write,
  255. };
  256. static struct ast_frame *chan_msg_read(struct ast_channel *chan);
  257. static int chan_msg_write(struct ast_channel *chan, struct ast_frame *fr);
  258. static int chan_msg_indicate(struct ast_channel *chan, int condition,
  259. const void *data, size_t datalen);
  260. static int chan_msg_send_digit_begin(struct ast_channel *chan, char digit);
  261. static int chan_msg_send_digit_end(struct ast_channel *chan, char digit,
  262. unsigned int duration);
  263. /*!
  264. * \internal
  265. * \brief A bare minimum channel technology
  266. *
  267. * This will not be registered as we never want anything to try
  268. * to create Message channels other than internally in this file.
  269. */
  270. static const struct ast_channel_tech msg_chan_tech_hack = {
  271. .type = "Message",
  272. .description = "Internal Text Message Processing",
  273. .read = chan_msg_read,
  274. .write = chan_msg_write,
  275. .indicate = chan_msg_indicate,
  276. .send_digit_begin = chan_msg_send_digit_begin,
  277. .send_digit_end = chan_msg_send_digit_end,
  278. };
  279. /*!
  280. * \internal
  281. * \brief ast_channel_tech read callback
  282. *
  283. * This should never be called. However, we say that about chan_iax2's
  284. * read callback, too, and it seems to randomly get called for some
  285. * reason. If it does, a simple NULL frame will suffice.
  286. */
  287. static struct ast_frame *chan_msg_read(struct ast_channel *chan)
  288. {
  289. return &ast_null_frame;
  290. }
  291. /*!
  292. * \internal
  293. * \brief ast_channel_tech write callback
  294. *
  295. * Throw all frames away. We don't care about any of them.
  296. */
  297. static int chan_msg_write(struct ast_channel *chan, struct ast_frame *fr)
  298. {
  299. return 0;
  300. }
  301. /*!
  302. * \internal
  303. * \brief ast_channel_tech indicate callback
  304. *
  305. * The indicate callback is here just so it can return success.
  306. * We don't want any callers of ast_indicate() to think something
  307. * has failed. We also don't want ast_indicate() itself to try
  308. * to generate inband tones since we didn't tell it that we took
  309. * care of it ourselves.
  310. */
  311. static int chan_msg_indicate(struct ast_channel *chan, int condition,
  312. const void *data, size_t datalen)
  313. {
  314. return 0;
  315. }
  316. /*!
  317. * \internal
  318. * \brief ast_channel_tech send_digit_begin callback
  319. *
  320. * This is here so that just in case a digit comes at a message channel
  321. * that the Asterisk core doesn't waste any time trying to generate
  322. * inband DTMF in audio. It's a waste of resources.
  323. */
  324. static int chan_msg_send_digit_begin(struct ast_channel *chan, char digit)
  325. {
  326. return 0;
  327. }
  328. /*!
  329. * \internal
  330. * \brief ast_channel_tech send_digit_end callback
  331. *
  332. * This is here so that just in case a digit comes at a message channel
  333. * that the Asterisk core doesn't waste any time trying to generate
  334. * inband DTMF in audio. It's a waste of resources.
  335. */
  336. static int chan_msg_send_digit_end(struct ast_channel *chan, char digit,
  337. unsigned int duration)
  338. {
  339. return 0;
  340. }
  341. static void msg_ds_destroy(void *data)
  342. {
  343. struct ast_msg *msg = data;
  344. ao2_ref(msg, -1);
  345. }
  346. static int msg_data_hash_fn(const void *obj, const int flags)
  347. {
  348. const struct msg_data *data = obj;
  349. return ast_str_case_hash(data->name);
  350. }
  351. static int msg_data_cmp_fn(void *obj, void *arg, int flags)
  352. {
  353. const struct msg_data *one = obj, *two = arg;
  354. return !strcasecmp(one->name, two->name) ? CMP_MATCH | CMP_STOP : 0;
  355. }
  356. static void msg_data_destructor(void *obj)
  357. {
  358. struct msg_data *data = obj;
  359. ast_string_field_free_memory(data);
  360. }
  361. static void msg_destructor(void *obj)
  362. {
  363. struct ast_msg *msg = obj;
  364. ast_string_field_free_memory(msg);
  365. ao2_ref(msg->vars, -1);
  366. }
  367. struct ast_msg *ast_msg_alloc(void)
  368. {
  369. struct ast_msg *msg;
  370. if (!(msg = ao2_alloc(sizeof(*msg), msg_destructor))) {
  371. return NULL;
  372. }
  373. if (ast_string_field_init(msg, 128)) {
  374. ao2_ref(msg, -1);
  375. return NULL;
  376. }
  377. if (!(msg->vars = ao2_container_alloc(1, msg_data_hash_fn, msg_data_cmp_fn))) {
  378. ao2_ref(msg, -1);
  379. return NULL;
  380. }
  381. ast_string_field_set(msg, context, "default");
  382. return msg;
  383. }
  384. struct ast_msg *ast_msg_ref(struct ast_msg *msg)
  385. {
  386. ao2_ref(msg, 1);
  387. return msg;
  388. }
  389. struct ast_msg *ast_msg_destroy(struct ast_msg *msg)
  390. {
  391. ao2_ref(msg, -1);
  392. return NULL;
  393. }
  394. int ast_msg_set_to(struct ast_msg *msg, const char *fmt, ...)
  395. {
  396. va_list ap;
  397. va_start(ap, fmt);
  398. ast_string_field_build_va(msg, to, fmt, ap);
  399. va_end(ap);
  400. return 0;
  401. }
  402. int ast_msg_set_from(struct ast_msg *msg, const char *fmt, ...)
  403. {
  404. va_list ap;
  405. va_start(ap, fmt);
  406. ast_string_field_build_va(msg, from, fmt, ap);
  407. va_end(ap);
  408. return 0;
  409. }
  410. int ast_msg_set_body(struct ast_msg *msg, const char *fmt, ...)
  411. {
  412. va_list ap;
  413. va_start(ap, fmt);
  414. ast_string_field_build_va(msg, body, fmt, ap);
  415. va_end(ap);
  416. return 0;
  417. }
  418. int ast_msg_set_context(struct ast_msg *msg, const char *fmt, ...)
  419. {
  420. va_list ap;
  421. va_start(ap, fmt);
  422. ast_string_field_build_va(msg, context, fmt, ap);
  423. va_end(ap);
  424. return 0;
  425. }
  426. int ast_msg_set_exten(struct ast_msg *msg, const char *fmt, ...)
  427. {
  428. va_list ap;
  429. va_start(ap, fmt);
  430. ast_string_field_build_va(msg, exten, fmt, ap);
  431. va_end(ap);
  432. return 0;
  433. }
  434. int ast_msg_set_tech(struct ast_msg *msg, const char *fmt, ...)
  435. {
  436. va_list ap;
  437. va_start(ap, fmt);
  438. ast_string_field_build_va(msg, tech, fmt, ap);
  439. va_end(ap);
  440. return 0;
  441. }
  442. int ast_msg_set_endpoint(struct ast_msg *msg, const char *fmt, ...)
  443. {
  444. va_list ap;
  445. va_start(ap, fmt);
  446. ast_string_field_build_va(msg, endpoint, fmt, ap);
  447. va_end(ap);
  448. return 0;
  449. }
  450. const char *ast_msg_get_body(const struct ast_msg *msg)
  451. {
  452. return msg->body;
  453. }
  454. const char *ast_msg_get_from(const struct ast_msg *msg)
  455. {
  456. return msg->from;
  457. }
  458. const char *ast_msg_get_to(const struct ast_msg *msg)
  459. {
  460. return msg->to;
  461. }
  462. const char *ast_msg_get_tech(const struct ast_msg *msg)
  463. {
  464. return msg->tech;
  465. }
  466. const char *ast_msg_get_endpoint(const struct ast_msg *msg)
  467. {
  468. return msg->endpoint;
  469. }
  470. static struct msg_data *msg_data_alloc(void)
  471. {
  472. struct msg_data *data;
  473. if (!(data = ao2_alloc(sizeof(*data), msg_data_destructor))) {
  474. return NULL;
  475. }
  476. if (ast_string_field_init(data, 32)) {
  477. ao2_ref(data, -1);
  478. return NULL;
  479. }
  480. return data;
  481. }
  482. static struct msg_data *msg_data_find(struct ao2_container *vars, const char *name)
  483. {
  484. struct msg_data tmp = {
  485. .name = name,
  486. };
  487. return ao2_find(vars, &tmp, OBJ_POINTER);
  488. }
  489. static int msg_set_var_full(struct ast_msg *msg, const char *name, const char *value, unsigned int outbound)
  490. {
  491. struct msg_data *data;
  492. if (!(data = msg_data_find(msg->vars, name))) {
  493. if (ast_strlen_zero(value)) {
  494. return 0;
  495. }
  496. if (!(data = msg_data_alloc())) {
  497. return -1;
  498. };
  499. ast_string_field_set(data, name, name);
  500. ast_string_field_set(data, value, value);
  501. data->send = outbound;
  502. ao2_link(msg->vars, data);
  503. } else {
  504. if (ast_strlen_zero(value)) {
  505. ao2_unlink(msg->vars, data);
  506. } else {
  507. ast_string_field_set(data, value, value);
  508. data->send = outbound;
  509. }
  510. }
  511. ao2_ref(data, -1);
  512. return 0;
  513. }
  514. int ast_msg_set_var_outbound(struct ast_msg *msg, const char *name, const char *value)
  515. {
  516. return msg_set_var_full(msg, name, value, 1);
  517. }
  518. int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value)
  519. {
  520. return msg_set_var_full(msg, name, value, 0);
  521. }
  522. const char *ast_msg_get_var(struct ast_msg *msg, const char *name)
  523. {
  524. struct msg_data *data;
  525. const char *val = NULL;
  526. if (!(data = msg_data_find(msg->vars, name))) {
  527. return NULL;
  528. }
  529. /* Yep, this definitely looks like val would be a dangling pointer
  530. * after the ref count is decremented. As long as the message structure
  531. * is used in a thread safe manner, this will not be the case though.
  532. * The ast_msg holds a reference to this object in the msg->vars container. */
  533. val = data->value;
  534. ao2_ref(data, -1);
  535. return val;
  536. }
  537. struct ast_msg_var_iterator {
  538. struct ao2_iterator iter;
  539. struct msg_data *current_used;
  540. };
  541. struct ast_msg_var_iterator *ast_msg_var_iterator_init(const struct ast_msg *msg)
  542. {
  543. struct ast_msg_var_iterator *iter;
  544. iter = ast_calloc(1, sizeof(*iter));
  545. if (!iter) {
  546. return NULL;
  547. }
  548. iter->iter = ao2_iterator_init(msg->vars, 0);
  549. return iter;
  550. }
  551. int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *iter, const char **name, const char **value)
  552. {
  553. struct msg_data *data;
  554. if (!iter) {
  555. return 0;
  556. }
  557. /* Skip any that aren't marked for sending out */
  558. while ((data = ao2_iterator_next(&iter->iter)) && !data->send) {
  559. ao2_ref(data, -1);
  560. }
  561. if (!data) {
  562. return 0;
  563. }
  564. if (data->send) {
  565. *name = data->name;
  566. *value = data->value;
  567. }
  568. /* Leave the refcount to be cleaned up by the caller with
  569. * ast_msg_var_unref_current after they finish with the pointers to the data */
  570. iter->current_used = data;
  571. return 1;
  572. }
  573. void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter)
  574. {
  575. ao2_cleanup(iter->current_used);
  576. iter->current_used = NULL;
  577. }
  578. void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter)
  579. {
  580. if (iter) {
  581. ao2_iterator_destroy(&iter->iter);
  582. ast_msg_var_unref_current(iter);
  583. ast_free(iter);
  584. }
  585. }
  586. static struct ast_channel *create_msg_q_chan(void)
  587. {
  588. struct ast_channel *chan;
  589. struct ast_datastore *ds;
  590. chan = ast_channel_alloc(1, AST_STATE_UP,
  591. NULL, NULL, NULL,
  592. NULL, NULL, NULL, NULL, 0,
  593. "%s", "Message/ast_msg_queue");
  594. if (!chan) {
  595. return NULL;
  596. }
  597. ast_channel_tech_set(chan, &msg_chan_tech_hack);
  598. ast_channel_unlock(chan);
  599. ast_channel_unlink(chan);
  600. if (!(ds = ast_datastore_alloc(&msg_datastore, NULL))) {
  601. ast_hangup(chan);
  602. return NULL;
  603. }
  604. ast_channel_lock(chan);
  605. ast_channel_datastore_add(chan, ds);
  606. ast_channel_unlock(chan);
  607. return chan;
  608. }
  609. /*!
  610. * \internal
  611. * \brief Run the dialplan for message processing
  612. *
  613. * \pre The message has already been set up on the msg datastore
  614. * on this channel.
  615. */
  616. static void msg_route(struct ast_channel *chan, struct ast_msg *msg)
  617. {
  618. struct ast_pbx_args pbx_args;
  619. ast_explicit_goto(chan, msg->context, S_OR(msg->exten, "s"), 1);
  620. memset(&pbx_args, 0, sizeof(pbx_args));
  621. pbx_args.no_hangup_chan = 1,
  622. ast_pbx_run_args(chan, &pbx_args);
  623. }
  624. /*!
  625. * \internal
  626. * \brief Clean up ast_channel after each message
  627. *
  628. * Reset various bits of state after routing each message so the same ast_channel
  629. * can just be reused.
  630. */
  631. static void chan_cleanup(struct ast_channel *chan)
  632. {
  633. struct ast_datastore *msg_ds, *ds;
  634. struct varshead *headp;
  635. struct ast_var_t *vardata;
  636. ast_channel_lock(chan);
  637. /*
  638. * Remove the msg datastore. Free its data but keep around the datastore
  639. * object and just reuse it.
  640. */
  641. if ((msg_ds = ast_channel_datastore_find(chan, &msg_datastore, NULL)) && msg_ds->data) {
  642. ast_channel_datastore_remove(chan, msg_ds);
  643. ao2_ref(msg_ds->data, -1);
  644. msg_ds->data = NULL;
  645. }
  646. /*
  647. * Destroy all other datastores.
  648. */
  649. while ((ds = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
  650. ast_datastore_free(ds);
  651. }
  652. /*
  653. * Destroy all channel variables.
  654. */
  655. headp = ast_channel_varshead(chan);
  656. while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) {
  657. ast_var_delete(vardata);
  658. }
  659. /*
  660. * Restore msg datastore.
  661. */
  662. if (msg_ds) {
  663. ast_channel_datastore_add(chan, msg_ds);
  664. }
  665. /*
  666. * Clear softhangup flags.
  667. */
  668. ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_ALL);
  669. ast_channel_unlock(chan);
  670. }
  671. static void destroy_msg_q_chan(void *data)
  672. {
  673. struct ast_channel **chan = data;
  674. if (!*chan) {
  675. return;
  676. }
  677. ast_channel_release(*chan);
  678. }
  679. AST_THREADSTORAGE_CUSTOM(msg_q_chan, NULL, destroy_msg_q_chan);
  680. /*! \internal \brief Handle a message bound for the dialplan */
  681. static int dialplan_handle_msg_cb(struct ast_msg *msg)
  682. {
  683. struct ast_channel **chan_p, *chan;
  684. struct ast_datastore *ds;
  685. if (!(chan_p = ast_threadstorage_get(&msg_q_chan, sizeof(struct ast_channel *)))) {
  686. return -1;
  687. }
  688. if (!*chan_p) {
  689. if (!(*chan_p = create_msg_q_chan())) {
  690. return -1;
  691. }
  692. }
  693. chan = *chan_p;
  694. ast_channel_lock(chan);
  695. if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) {
  696. ast_channel_unlock(chan);
  697. return -1;
  698. }
  699. ao2_ref(msg, +1);
  700. ds->data = msg;
  701. ast_channel_unlock(chan);
  702. msg_route(chan, msg);
  703. chan_cleanup(chan);
  704. return 0;
  705. }
  706. /*! \internal \brief Determine if a message has a destination in the dialplan */
  707. static int dialplan_has_destination_cb(const struct ast_msg *msg)
  708. {
  709. if (ast_strlen_zero(msg->context)) {
  710. return 0;
  711. }
  712. return ast_exists_extension(NULL, msg->context, S_OR(msg->exten, "s"), 1, NULL);
  713. }
  714. static struct ast_msg_handler dialplan_msg_handler = {
  715. .name = "dialplan",
  716. .handle_msg = dialplan_handle_msg_cb,
  717. .has_destination = dialplan_has_destination_cb,
  718. };
  719. /*!
  720. * \internal
  721. * \brief Message queue task processor callback
  722. *
  723. * \retval 0 success
  724. * \retval non-zero failure
  725. *
  726. * \note Even though this returns a value, the taskprocessor code ignores the value.
  727. */
  728. static int msg_q_cb(void *data)
  729. {
  730. struct ast_msg *msg = data;
  731. int res = 1;
  732. int i;
  733. ast_rwlock_rdlock(&msg_handlers_lock);
  734. for (i = 0; i < AST_VECTOR_SIZE(&msg_handlers); i++) {
  735. const struct ast_msg_handler *handler = AST_VECTOR_GET(&msg_handlers, i);
  736. if (!handler->has_destination(msg)) {
  737. ast_debug(5, "Handler %s doesn't want message, moving on\n", handler->name);
  738. continue;
  739. }
  740. ast_debug(5, "Dispatching message to %s handler\n", handler->name);
  741. res &= handler->handle_msg(msg);
  742. }
  743. ast_rwlock_unlock(&msg_handlers_lock);
  744. if (res != 0) {
  745. ast_log(LOG_WARNING, "No handler processed message from %s to %s\n",
  746. S_OR(msg->from, "<unknown>"), S_OR(msg->to, "<unknown>"));
  747. }
  748. ao2_ref(msg, -1);
  749. return res;
  750. }
  751. int ast_msg_has_destination(const struct ast_msg *msg)
  752. {
  753. int i;
  754. int result = 0;
  755. ast_rwlock_rdlock(&msg_handlers_lock);
  756. for (i = 0; i < AST_VECTOR_SIZE(&msg_handlers); i++) {
  757. const struct ast_msg_handler *handler = AST_VECTOR_GET(&msg_handlers, i);
  758. ast_debug(5, "Seeing if %s can handle message\n", handler->name);
  759. if (handler->has_destination(msg)) {
  760. ast_debug(5, "%s can handle message\n", handler->name);
  761. result = 1;
  762. break;
  763. }
  764. }
  765. ast_rwlock_unlock(&msg_handlers_lock);
  766. return result;
  767. }
  768. int ast_msg_queue(struct ast_msg *msg)
  769. {
  770. int res;
  771. res = ast_taskprocessor_push(msg_q_tp, msg_q_cb, msg);
  772. if (res == -1) {
  773. ao2_ref(msg, -1);
  774. }
  775. return res;
  776. }
  777. /*!
  778. * \internal
  779. * \brief Find or create a message datastore on a channel
  780. *
  781. * \pre chan is locked
  782. *
  783. * \param chan the relevant channel
  784. *
  785. * \return the channel's message datastore, or NULL on error
  786. */
  787. static struct ast_datastore *msg_datastore_find_or_create(struct ast_channel *chan)
  788. {
  789. struct ast_datastore *ds;
  790. if ((ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) {
  791. return ds;
  792. }
  793. if (!(ds = ast_datastore_alloc(&msg_datastore, NULL))) {
  794. return NULL;
  795. }
  796. if (!(ds->data = ast_msg_alloc())) {
  797. ast_datastore_free(ds);
  798. return NULL;
  799. }
  800. ast_channel_datastore_add(chan, ds);
  801. return ds;
  802. }
  803. static int msg_func_read(struct ast_channel *chan, const char *function,
  804. char *data, char *buf, size_t len)
  805. {
  806. struct ast_datastore *ds;
  807. struct ast_msg *msg;
  808. if (!chan) {
  809. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  810. return -1;
  811. }
  812. ast_channel_lock(chan);
  813. if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) {
  814. ast_channel_unlock(chan);
  815. ast_log(LOG_ERROR, "No MESSAGE data found on the channel to read.\n");
  816. return -1;
  817. }
  818. msg = ds->data;
  819. ao2_ref(msg, +1);
  820. ast_channel_unlock(chan);
  821. ao2_lock(msg);
  822. if (!strcasecmp(data, "to")) {
  823. ast_copy_string(buf, msg->to, len);
  824. } else if (!strcasecmp(data, "from")) {
  825. ast_copy_string(buf, msg->from, len);
  826. } else if (!strcasecmp(data, "body")) {
  827. ast_copy_string(buf, msg->body, len);
  828. } else {
  829. ast_log(LOG_WARNING, "Invalid argument to MESSAGE(): '%s'\n", data);
  830. }
  831. ao2_unlock(msg);
  832. ao2_ref(msg, -1);
  833. return 0;
  834. }
  835. static int msg_func_write(struct ast_channel *chan, const char *function,
  836. char *data, const char *value)
  837. {
  838. struct ast_datastore *ds;
  839. struct ast_msg *msg;
  840. if (!chan) {
  841. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  842. return -1;
  843. }
  844. ast_channel_lock(chan);
  845. if (!(ds = msg_datastore_find_or_create(chan))) {
  846. ast_channel_unlock(chan);
  847. return -1;
  848. }
  849. msg = ds->data;
  850. ao2_ref(msg, +1);
  851. ast_channel_unlock(chan);
  852. ao2_lock(msg);
  853. if (!strcasecmp(data, "to")) {
  854. ast_msg_set_to(msg, "%s", value);
  855. } else if (!strcasecmp(data, "from")) {
  856. ast_msg_set_from(msg, "%s", value);
  857. } else if (!strcasecmp(data, "body")) {
  858. ast_msg_set_body(msg, "%s", value);
  859. } else if (!strcasecmp(data, "custom_data")) {
  860. int outbound = -1;
  861. if (!strcasecmp(value, "mark_all_outbound")) {
  862. outbound = 1;
  863. } else if (!strcasecmp(value, "clear_all_outbound")) {
  864. outbound = 0;
  865. } else {
  866. ast_log(LOG_WARNING, "'%s' is not a valid value for custom_data\n", value);
  867. }
  868. if (outbound != -1) {
  869. struct msg_data *hdr_data;
  870. struct ao2_iterator iter = ao2_iterator_init(msg->vars, 0);
  871. while ((hdr_data = ao2_iterator_next(&iter))) {
  872. hdr_data->send = outbound;
  873. ao2_ref(hdr_data, -1);
  874. }
  875. ao2_iterator_destroy(&iter);
  876. }
  877. } else {
  878. ast_log(LOG_WARNING, "'%s' is not a valid write argument.\n", data);
  879. }
  880. ao2_unlock(msg);
  881. ao2_ref(msg, -1);
  882. return 0;
  883. }
  884. static int msg_data_func_read(struct ast_channel *chan, const char *function,
  885. char *data, char *buf, size_t len)
  886. {
  887. struct ast_datastore *ds;
  888. struct ast_msg *msg;
  889. const char *val;
  890. if (!chan) {
  891. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  892. return -1;
  893. }
  894. ast_channel_lock(chan);
  895. if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) {
  896. ast_channel_unlock(chan);
  897. ast_log(LOG_ERROR, "No MESSAGE data found on the channel to read.\n");
  898. return -1;
  899. }
  900. msg = ds->data;
  901. ao2_ref(msg, +1);
  902. ast_channel_unlock(chan);
  903. ao2_lock(msg);
  904. if ((val = ast_msg_get_var(msg, data))) {
  905. ast_copy_string(buf, val, len);
  906. }
  907. ao2_unlock(msg);
  908. ao2_ref(msg, -1);
  909. return 0;
  910. }
  911. static int msg_data_func_write(struct ast_channel *chan, const char *function,
  912. char *data, const char *value)
  913. {
  914. struct ast_datastore *ds;
  915. struct ast_msg *msg;
  916. if (!chan) {
  917. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  918. return -1;
  919. }
  920. ast_channel_lock(chan);
  921. if (!(ds = msg_datastore_find_or_create(chan))) {
  922. ast_channel_unlock(chan);
  923. return -1;
  924. }
  925. msg = ds->data;
  926. ao2_ref(msg, +1);
  927. ast_channel_unlock(chan);
  928. ao2_lock(msg);
  929. ast_msg_set_var_outbound(msg, data, value);
  930. ao2_unlock(msg);
  931. ao2_ref(msg, -1);
  932. return 0;
  933. }
  934. /*!
  935. * \internal \brief Find a \c ast_msg_tech by its technology name
  936. *
  937. * \param tech_name The name of the message technology
  938. *
  939. * \note \c msg_techs should be locked via \c msg_techs_lock prior to
  940. * calling this function
  941. *
  942. * \retval NULL if no \c ast_msg_tech has been registered
  943. * \retval \c ast_msg_tech if registered
  944. */
  945. static const struct ast_msg_tech *msg_find_by_tech_name(const char *tech_name)
  946. {
  947. const struct ast_msg_tech *current;
  948. int i;
  949. for (i = 0; i < AST_VECTOR_SIZE(&msg_techs); i++) {
  950. current = AST_VECTOR_GET(&msg_techs, i);
  951. if (!strcmp(current->name, tech_name)) {
  952. return current;
  953. }
  954. }
  955. return NULL;
  956. }
  957. /*!
  958. * \internal \brief Find a \c ast_msg_handler by its technology name
  959. *
  960. * \param tech_name The name of the message technology
  961. *
  962. * \note \c msg_handlers should be locked via \c msg_handlers_lock
  963. * prior to calling this function
  964. *
  965. * \retval NULL if no \c ast_msg_handler has been registered
  966. * \retval \c ast_msg_handler if registered
  967. */
  968. static const struct ast_msg_handler *msg_handler_find_by_tech_name(const char *tech_name)
  969. {
  970. const struct ast_msg_handler *current;
  971. int i;
  972. for (i = 0; i < AST_VECTOR_SIZE(&msg_handlers); i++) {
  973. current = AST_VECTOR_GET(&msg_handlers, i);
  974. if (!strcmp(current->name, tech_name)) {
  975. return current;
  976. }
  977. }
  978. return NULL;
  979. }
  980. /*!
  981. * \internal
  982. * \brief MessageSend() application
  983. */
  984. static int msg_send_exec(struct ast_channel *chan, const char *data)
  985. {
  986. struct ast_datastore *ds;
  987. struct ast_msg *msg;
  988. char *tech_name;
  989. const struct ast_msg_tech *msg_tech;
  990. char *parse;
  991. int res = -1;
  992. AST_DECLARE_APP_ARGS(args,
  993. AST_APP_ARG(to);
  994. AST_APP_ARG(from);
  995. );
  996. if (ast_strlen_zero(data)) {
  997. ast_log(LOG_WARNING, "An argument is required to MessageSend()\n");
  998. pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "INVALID_URI");
  999. return 0;
  1000. }
  1001. parse = ast_strdupa(data);
  1002. AST_STANDARD_APP_ARGS(args, parse);
  1003. if (ast_strlen_zero(args.to)) {
  1004. ast_log(LOG_WARNING, "A 'to' URI is required for MessageSend()\n");
  1005. pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "INVALID_URI");
  1006. return 0;
  1007. }
  1008. ast_channel_lock(chan);
  1009. if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) {
  1010. ast_channel_unlock(chan);
  1011. ast_log(LOG_WARNING, "No message data found on channel to send.\n");
  1012. pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "FAILURE");
  1013. return 0;
  1014. }
  1015. msg = ds->data;
  1016. ao2_ref(msg, +1);
  1017. ast_channel_unlock(chan);
  1018. tech_name = ast_strdupa(args.to);
  1019. tech_name = strsep(&tech_name, ":");
  1020. ast_rwlock_rdlock(&msg_techs_lock);
  1021. msg_tech = msg_find_by_tech_name(tech_name);
  1022. if (!msg_tech) {
  1023. ast_log(LOG_WARNING, "No message technology '%s' found.\n", tech_name);
  1024. pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "INVALID_PROTOCOL");
  1025. goto exit_cleanup;
  1026. }
  1027. /*
  1028. * The message lock is held here to safely allow the technology
  1029. * implementation to access the message fields without worrying
  1030. * that they could change.
  1031. */
  1032. ao2_lock(msg);
  1033. res = msg_tech->msg_send(msg, S_OR(args.to, ""), S_OR(args.from, ""));
  1034. ao2_unlock(msg);
  1035. pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", res ? "FAILURE" : "SUCCESS");
  1036. exit_cleanup:
  1037. ast_rwlock_unlock(&msg_techs_lock);
  1038. ao2_ref(msg, -1);
  1039. return 0;
  1040. }
  1041. static int action_messagesend(struct mansession *s, const struct message *m)
  1042. {
  1043. const char *to = ast_strdupa(astman_get_header(m, "To"));
  1044. const char *from = astman_get_header(m, "From");
  1045. const char *body = astman_get_header(m, "Body");
  1046. const char *base64body = astman_get_header(m, "Base64Body");
  1047. char base64decoded[1301] = { 0, };
  1048. char *tech_name = NULL;
  1049. struct ast_variable *vars = NULL;
  1050. struct ast_variable *data = NULL;
  1051. const struct ast_msg_tech *msg_tech;
  1052. struct ast_msg *msg;
  1053. int res = -1;
  1054. if (ast_strlen_zero(to)) {
  1055. astman_send_error(s, m, "No 'To' address specified.");
  1056. return 0;
  1057. }
  1058. if (!ast_strlen_zero(base64body)) {
  1059. ast_base64decode((unsigned char *) base64decoded, base64body, sizeof(base64decoded) - 1);
  1060. body = base64decoded;
  1061. }
  1062. tech_name = ast_strdupa(to);
  1063. tech_name = strsep(&tech_name, ":");
  1064. ast_rwlock_rdlock(&msg_techs_lock);
  1065. msg_tech = msg_find_by_tech_name(tech_name);
  1066. if (!msg_tech) {
  1067. ast_rwlock_unlock(&msg_techs_lock);
  1068. astman_send_error(s, m, "Message technology not found.");
  1069. return 0;
  1070. }
  1071. if (!(msg = ast_msg_alloc())) {
  1072. ast_rwlock_unlock(&msg_techs_lock);
  1073. astman_send_error(s, m, "Internal failure\n");
  1074. return 0;
  1075. }
  1076. data = astman_get_variables_order(m, ORDER_NATURAL);
  1077. for (vars = data; vars; vars = vars->next) {
  1078. ast_msg_set_var_outbound(msg, vars->name, vars->value);
  1079. }
  1080. ast_msg_set_body(msg, "%s", body);
  1081. res = msg_tech->msg_send(msg, S_OR(to, ""), S_OR(from, ""));
  1082. ast_rwlock_unlock(&msg_techs_lock);
  1083. ast_variables_destroy(vars);
  1084. ao2_ref(msg, -1);
  1085. if (res) {
  1086. astman_send_error(s, m, "Message failed to send.");
  1087. } else {
  1088. astman_send_ack(s, m, "Message successfully sent");
  1089. }
  1090. return 0;
  1091. }
  1092. int ast_msg_send(struct ast_msg *msg, const char *to, const char *from)
  1093. {
  1094. char *tech_name = NULL;
  1095. const struct ast_msg_tech *msg_tech;
  1096. int res = -1;
  1097. if (ast_strlen_zero(to)) {
  1098. ao2_ref(msg, -1);
  1099. return -1;
  1100. }
  1101. tech_name = ast_strdupa(to);
  1102. tech_name = strsep(&tech_name, ":");
  1103. ast_rwlock_rdlock(&msg_techs_lock);
  1104. msg_tech = msg_find_by_tech_name(tech_name);
  1105. if (!msg_tech) {
  1106. ast_log(LOG_ERROR, "Unknown message tech: %s\n", tech_name);
  1107. ast_rwlock_unlock(&msg_techs_lock);
  1108. return -1;
  1109. }
  1110. res = msg_tech->msg_send(msg, S_OR(to, ""), S_OR(from, ""));
  1111. ast_rwlock_unlock(&msg_techs_lock);
  1112. ao2_ref(msg, -1);
  1113. return res;
  1114. }
  1115. int ast_msg_tech_register(const struct ast_msg_tech *tech)
  1116. {
  1117. const struct ast_msg_tech *match;
  1118. ast_rwlock_wrlock(&msg_techs_lock);
  1119. match = msg_find_by_tech_name(tech->name);
  1120. if (match) {
  1121. ast_log(LOG_ERROR, "Message technology already registered for '%s'\n",
  1122. tech->name);
  1123. ast_rwlock_unlock(&msg_techs_lock);
  1124. return -1;
  1125. }
  1126. AST_VECTOR_APPEND(&msg_techs, tech);
  1127. ast_verb(3, "Message technology '%s' registered.\n", tech->name);
  1128. ast_rwlock_unlock(&msg_techs_lock);
  1129. return 0;
  1130. }
  1131. /*!
  1132. * \brief Comparison callback for \c ast_msg_tech vector removal
  1133. *
  1134. * \param vec_elem The element in the vector being compared
  1135. * \param srch The element being looked up
  1136. *
  1137. * \retval non-zero The items are equal
  1138. * \retval 0 The items are not equal
  1139. */
  1140. static int msg_tech_cmp(const struct ast_msg_tech *vec_elem, const struct ast_msg_tech *srch)
  1141. {
  1142. return !strcmp(vec_elem->name, srch->name);
  1143. }
  1144. int ast_msg_tech_unregister(const struct ast_msg_tech *tech)
  1145. {
  1146. int match;
  1147. ast_rwlock_wrlock(&msg_techs_lock);
  1148. match = AST_VECTOR_REMOVE_CMP_UNORDERED(&msg_techs, tech, msg_tech_cmp,
  1149. AST_VECTOR_ELEM_CLEANUP_NOOP);
  1150. ast_rwlock_unlock(&msg_techs_lock);
  1151. if (match) {
  1152. ast_log(LOG_ERROR, "No '%s' message technology found.\n", tech->name);
  1153. return -1;
  1154. }
  1155. ast_verb(2, "Message technology '%s' unregistered.\n", tech->name);
  1156. return 0;
  1157. }
  1158. int ast_msg_handler_register(const struct ast_msg_handler *handler)
  1159. {
  1160. const struct ast_msg_handler *match;
  1161. ast_rwlock_wrlock(&msg_handlers_lock);
  1162. match = msg_handler_find_by_tech_name(handler->name);
  1163. if (match) {
  1164. ast_log(LOG_ERROR, "Message handler already registered for '%s'\n",
  1165. handler->name);
  1166. ast_rwlock_unlock(&msg_handlers_lock);
  1167. return -1;
  1168. }
  1169. AST_VECTOR_APPEND(&msg_handlers, handler);
  1170. ast_verb(2, "Message handler '%s' registered.\n", handler->name);
  1171. ast_rwlock_unlock(&msg_handlers_lock);
  1172. return 0;
  1173. }
  1174. /*!
  1175. * \brief Comparison callback for \c ast_msg_handler vector removal
  1176. *
  1177. * \param vec_elem The element in the vector being compared
  1178. * \param srch The element being looked up
  1179. *
  1180. * \retval non-zero The items are equal
  1181. * \retval 0 The items are not equal
  1182. */
  1183. static int msg_handler_cmp(const struct ast_msg_handler *vec_elem, const struct ast_msg_handler *srch)
  1184. {
  1185. return !strcmp(vec_elem->name, srch->name);
  1186. }
  1187. int ast_msg_handler_unregister(const struct ast_msg_handler *handler)
  1188. {
  1189. int match;
  1190. ast_rwlock_wrlock(&msg_handlers_lock);
  1191. match = AST_VECTOR_REMOVE_CMP_UNORDERED(&msg_handlers, handler, msg_handler_cmp,
  1192. AST_VECTOR_ELEM_CLEANUP_NOOP);
  1193. ast_rwlock_unlock(&msg_handlers_lock);
  1194. if (match) {
  1195. ast_log(LOG_ERROR, "No '%s' message handler found.\n", handler->name);
  1196. return -1;
  1197. }
  1198. ast_verb(3, "Message handler '%s' unregistered.\n", handler->name);
  1199. return 0;
  1200. }
  1201. void ast_msg_shutdown(void)
  1202. {
  1203. if (msg_q_tp) {
  1204. msg_q_tp = ast_taskprocessor_unreference(msg_q_tp);
  1205. }
  1206. }
  1207. /*!
  1208. * \internal
  1209. * \brief Clean up other resources on Asterisk shutdown
  1210. *
  1211. * \note This does not include the msg_q_tp object, which must be disposed
  1212. * of prior to Asterisk checking for channel destruction in its shutdown
  1213. * sequence. The atexit handlers are executed after this occurs.
  1214. */
  1215. static void message_shutdown(void)
  1216. {
  1217. ast_msg_handler_unregister(&dialplan_msg_handler);
  1218. ast_custom_function_unregister(&msg_function);
  1219. ast_custom_function_unregister(&msg_data_function);
  1220. ast_unregister_application(app_msg_send);
  1221. ast_manager_unregister("MessageSend");
  1222. AST_VECTOR_FREE(&msg_techs);
  1223. ast_rwlock_destroy(&msg_techs_lock);
  1224. AST_VECTOR_FREE(&msg_handlers);
  1225. ast_rwlock_destroy(&msg_handlers_lock);
  1226. }
  1227. /*
  1228. * \internal
  1229. * \brief Initialize stuff during Asterisk startup.
  1230. *
  1231. * Cleanup isn't a big deal in this function. If we return non-zero,
  1232. * Asterisk is going to exit.
  1233. *
  1234. * \retval 0 success
  1235. * \retval non-zero failure
  1236. */
  1237. int ast_msg_init(void)
  1238. {
  1239. int res;
  1240. msg_q_tp = ast_taskprocessor_get("ast_msg_queue", TPS_REF_DEFAULT);
  1241. if (!msg_q_tp) {
  1242. return -1;
  1243. }
  1244. ast_rwlock_init(&msg_techs_lock);
  1245. if (AST_VECTOR_INIT(&msg_techs, 8)) {
  1246. return -1;
  1247. }
  1248. ast_rwlock_init(&msg_handlers_lock);
  1249. if (AST_VECTOR_INIT(&msg_handlers, 4)) {
  1250. return -1;
  1251. }
  1252. res = ast_msg_handler_register(&dialplan_msg_handler);
  1253. res |= __ast_custom_function_register(&msg_function, NULL);
  1254. res |= __ast_custom_function_register(&msg_data_function, NULL);
  1255. res |= ast_register_application2(app_msg_send, msg_send_exec, NULL, NULL, NULL);
  1256. res |= ast_manager_register_xml_core("MessageSend", EVENT_FLAG_MESSAGE, action_messagesend);
  1257. ast_register_cleanup(message_shutdown);
  1258. return res;
  1259. }