app_minivm.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. * and Edvina AB, Sollentuna, Sweden
  6. *
  7. * Mark Spencer <markster@digium.com> (Comedian Mail)
  8. * and Olle E. Johansson, Edvina.net <oej@edvina.net> (Mini-Voicemail changes)
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief MiniVoiceMail - A Minimal Voicemail System for Asterisk
  23. *
  24. * A voicemail system in small building blocks, working together
  25. * based on the Comedian Mail voicemail system (app_voicemail.c).
  26. *
  27. * \par See also
  28. * \arg \ref Config_minivm_examples
  29. * \arg \ref App_minivm
  30. *
  31. * \ingroup applications
  32. *
  33. * \page App_minivm Asterisk Mini-voicemail - A minimal voicemail system
  34. *
  35. * This is a minimal voicemail system, building blocks for something
  36. * else. It is built for multi-language systems.
  37. * The current version is focused on accounts where voicemail is
  38. * forwarded to users in e-mail. It's work in progress, with loosed ends hanging
  39. * around from the old voicemail system and it's configuration.
  40. *
  41. * Hopefully, we can expand this to be a full replacement of voicemail() and voicemailmain()
  42. * in the future.
  43. *
  44. * Dialplan applications
  45. * - minivmRecord - record voicemail and send as e-mail ( \ref minivm_record_exec() )
  46. * - minivmGreet - Play user's greeting or default greeting ( \ref minivm_greet_exec() )
  47. * - minivmNotify - Notify user of message ( \ref minivm_notify_exec() )
  48. * - minivmDelete - Delete voicemail message ( \ref minivm_delete_exec() )
  49. * - minivmAccMess - Record personal messages (busy | unavailable | temporary)
  50. *
  51. * Dialplan functions
  52. * - MINIVMACCOUNT() - A dialplan function
  53. * - MINIVMCOUNTER() - Manage voicemail-related counters for accounts or domains
  54. *
  55. * CLI Commands
  56. * - minivm list accounts
  57. * - minivm list zones
  58. * - minivm list templates
  59. * - minivm show stats
  60. * - minivm show settings
  61. *
  62. * Some notes
  63. * - General configuration in minivm.conf
  64. * - Users in realtime or configuration file
  65. * - Or configured on the command line with just the e-mail address
  66. *
  67. * Voicemail accounts are identified by userid and domain
  68. *
  69. * Language codes are like setlocale - langcode_countrycode
  70. * \note Don't use language codes like the rest of Asterisk, two letter countrycode. Use
  71. * language_country like setlocale().
  72. *
  73. * Examples:
  74. * - Swedish, Sweden sv_se
  75. * - Swedish, Finland sv_fi
  76. * - English, USA en_us
  77. * - English, GB en_gb
  78. *
  79. * \par See also
  80. * \arg \ref Config_minivm
  81. * \arg \ref Config_minivm_examples
  82. * \arg \ref Minivm_directories
  83. * \arg \ref app_minivm.c
  84. * \arg Comedian mail: app_voicemail.c
  85. * \arg \ref descrip_minivm_accmess
  86. * \arg \ref descrip_minivm_greet
  87. * \arg \ref descrip_minivm_record
  88. * \arg \ref descrip_minivm_delete
  89. * \arg \ref descrip_minivm_notify
  90. *
  91. * \arg \ref App_minivm_todo
  92. */
  93. /*! \page Minivm_directories Asterisk Mini-Voicemail Directory structure
  94. *
  95. * The directory structure for storing voicemail
  96. * - AST_SPOOL_DIR - usually /var/spool/asterisk (configurable in asterisk.conf)
  97. * - MVM_SPOOL_DIR - should be configurable, usually AST_SPOOL_DIR/voicemail
  98. * - Domain MVM_SPOOL_DIR/domain
  99. * - Username MVM_SPOOL_DIR/domain/username
  100. * - /greet : Recording of account owner's name
  101. * - /busy : Busy message
  102. * - /unavailable : Unavailable message
  103. * - /temp : Temporary message
  104. *
  105. * For account anita@localdomain.xx the account directory would as a default be
  106. * \b /var/spool/asterisk/voicemail/localdomain.xx/anita
  107. *
  108. * To avoid transcoding, these sound files should be converted into several formats
  109. * They are recorded in the format closest to the incoming streams
  110. *
  111. *
  112. * Back: \ref App_minivm
  113. */
  114. /*! \page Config_minivm_examples Example dialplan for Mini-Voicemail
  115. * \section Example dialplan scripts for Mini-Voicemail
  116. * \verbinclude extensions_minivm.conf.sample
  117. *
  118. * Back: \ref App_minivm
  119. */
  120. /*! \page App_minivm_todo Asterisk Mini-Voicemail - todo
  121. * - configure accounts from AMI?
  122. * - test, test, test, test
  123. * - fix "vm-theextensionis.gsm" voiceprompt from Allison in various formats
  124. * "The extension you are calling"
  125. * - For trunk, consider using channel storage for information passing between small applications
  126. * - Set default directory for voicemail
  127. * - New app for creating directory for account if it does not exist
  128. * - Re-insert code for IMAP storage at some point
  129. * - Jabber integration for notifications
  130. * - Figure out how to handle video in voicemail
  131. * - Integration with the HTTP server
  132. * - New app for moving messages between mailboxes, and optionally mark it as "new"
  133. *
  134. * For Asterisk 1.4/trunk
  135. * - Use string fields for minivm_account
  136. *
  137. * Back: \ref App_minivm
  138. */
  139. /*** MODULEINFO
  140. <support_level>extended</support_level>
  141. ***/
  142. #include "asterisk.h"
  143. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  144. #include <ctype.h>
  145. #include <sys/time.h>
  146. #include <sys/stat.h>
  147. #include <sys/mman.h>
  148. #include <time.h>
  149. #include <dirent.h>
  150. #include <locale.h>
  151. #include "asterisk/paths.h" /* use various paths */
  152. #include "asterisk/lock.h"
  153. #include "asterisk/file.h"
  154. #include "asterisk/channel.h"
  155. #include "asterisk/pbx.h"
  156. #include "asterisk/config.h"
  157. #include "asterisk/say.h"
  158. #include "asterisk/module.h"
  159. #include "asterisk/app.h"
  160. #include "asterisk/dsp.h"
  161. #include "asterisk/localtime.h"
  162. #include "asterisk/cli.h"
  163. #include "asterisk/utils.h"
  164. #include "asterisk/linkedlists.h"
  165. #include "asterisk/callerid.h"
  166. #include "asterisk/stasis.h"
  167. #include "asterisk/stasis_channels.h"
  168. #include "asterisk/json.h"
  169. /*** DOCUMENTATION
  170. <application name="MinivmRecord" language="en_US">
  171. <synopsis>
  172. Receive Mini-Voicemail and forward via e-mail.
  173. </synopsis>
  174. <syntax>
  175. <parameter name="mailbox" required="true" argsep="@">
  176. <argument name="username" required="true">
  177. <para>Voicemail username</para>
  178. </argument>
  179. <argument name="domain" required="true">
  180. <para>Voicemail domain</para>
  181. </argument>
  182. </parameter>
  183. <parameter name="options" required="false">
  184. <optionlist>
  185. <option name="0">
  186. <para>Jump to the <literal>o</literal> extension in the current dialplan context.</para>
  187. </option>
  188. <option name="*">
  189. <para>Jump to the <literal>a</literal> extension in the current dialplan context.</para>
  190. </option>
  191. <option name="g">
  192. <argument name="gain">
  193. <para>Amount of gain to use</para>
  194. </argument>
  195. <para>Use the specified amount of gain when recording the voicemail message.
  196. The units are whole-number decibels (dB).</para>
  197. </option>
  198. </optionlist>
  199. </parameter>
  200. </syntax>
  201. <description>
  202. <para>This application is part of the Mini-Voicemail system, configured in <filename>minivm.conf</filename></para>
  203. <para>MiniVM records audio file in configured format and forwards message to e-mail and pager.</para>
  204. <para>If there's no user account for that address, a temporary account will be used with default options.</para>
  205. <para>The recorded file name and path will be stored in <variable>MVM_FILENAME</variable> and the duration
  206. of the message will be stored in <variable>MVM_DURATION</variable></para>
  207. <note><para>If the caller hangs up after the recording, the only way to send the message and clean up is to
  208. execute in the <literal>h</literal> extension. The application will exit if any of the following DTMF digits
  209. are received and the requested extension exist in the current context.</para></note>
  210. <variablelist>
  211. <variable name="MVM_RECORD_STATUS">
  212. <para>This is the status of the record operation</para>
  213. <value name="SUCCESS" />
  214. <value name="USEREXIT" />
  215. <value name="FAILED" />
  216. </variable>
  217. </variablelist>
  218. </description>
  219. </application>
  220. <application name="MinivmGreet" language="en_US">
  221. <synopsis>
  222. Play Mini-Voicemail prompts.
  223. </synopsis>
  224. <syntax>
  225. <parameter name="mailbox" required="true" argsep="@">
  226. <argument name="username" required="true">
  227. <para>Voicemail username</para>
  228. </argument>
  229. <argument name="domain" required="true">
  230. <para>Voicemail domain</para>
  231. </argument>
  232. </parameter>
  233. <parameter name="options" required="false">
  234. <optionlist>
  235. <option name="b">
  236. <para>Play the <literal>busy</literal> greeting to the calling party.</para>
  237. </option>
  238. <option name="s">
  239. <para>Skip the playback of instructions for leaving a message to the calling party.</para>
  240. </option>
  241. <option name="u">
  242. <para>Play the <literal>unavailable</literal> greeting.</para>
  243. </option>
  244. </optionlist>
  245. </parameter>
  246. </syntax>
  247. <description>
  248. <para>This application is part of the Mini-Voicemail system, configured in minivm.conf.</para>
  249. <para>MinivmGreet() plays default prompts or user specific prompts for an account.</para>
  250. <para>Busy and unavailable messages can be choosen, but will be overridden if a temporary
  251. message exists for the account.</para>
  252. <variablelist>
  253. <variable name="MVM_GREET_STATUS">
  254. <para>This is the status of the greeting playback.</para>
  255. <value name="SUCCESS" />
  256. <value name="USEREXIT" />
  257. <value name="FAILED" />
  258. </variable>
  259. </variablelist>
  260. </description>
  261. </application>
  262. <application name="MinivmNotify" language="en_US">
  263. <synopsis>
  264. Notify voicemail owner about new messages.
  265. </synopsis>
  266. <syntax>
  267. <parameter name="mailbox" required="true" argsep="@">
  268. <argument name="username" required="true">
  269. <para>Voicemail username</para>
  270. </argument>
  271. <argument name="domain" required="true">
  272. <para>Voicemail domain</para>
  273. </argument>
  274. </parameter>
  275. <parameter name="options" required="false">
  276. <optionlist>
  277. <option name="template">
  278. <para>E-mail template to use for voicemail notification</para>
  279. </option>
  280. </optionlist>
  281. </parameter>
  282. </syntax>
  283. <description>
  284. <para>This application is part of the Mini-Voicemail system, configured in minivm.conf.</para>
  285. <para>MiniVMnotify forwards messages about new voicemail to e-mail and pager. If there's no user
  286. account for that address, a temporary account will be used with default options (set in
  287. <filename>minivm.conf</filename>).</para>
  288. <para>If the channel variable <variable>MVM_COUNTER</variable> is set, this will be used in the message
  289. file name and available in the template for the message.</para>
  290. <para>If no template is given, the default email template will be used to send email and default pager
  291. template to send paging message (if the user account is configured with a paging address.</para>
  292. <variablelist>
  293. <variable name="MVM_NOTIFY_STATUS">
  294. <para>This is the status of the notification attempt</para>
  295. <value name="SUCCESS" />
  296. <value name="FAILED" />
  297. </variable>
  298. </variablelist>
  299. </description>
  300. </application>
  301. <application name="MinivmDelete" language="en_US">
  302. <synopsis>
  303. Delete Mini-Voicemail voicemail messages.
  304. </synopsis>
  305. <syntax>
  306. <parameter name="filename" required="true">
  307. <para>File to delete</para>
  308. </parameter>
  309. </syntax>
  310. <description>
  311. <para>This application is part of the Mini-Voicemail system, configured in <filename>minivm.conf</filename>.</para>
  312. <para>It deletes voicemail file set in MVM_FILENAME or given filename.</para>
  313. <variablelist>
  314. <variable name="MVM_DELETE_STATUS">
  315. <para>This is the status of the delete operation.</para>
  316. <value name="SUCCESS" />
  317. <value name="FAILED" />
  318. </variable>
  319. </variablelist>
  320. </description>
  321. </application>
  322. <application name="MinivmAccMess" language="en_US">
  323. <synopsis>
  324. Record account specific messages.
  325. </synopsis>
  326. <syntax>
  327. <parameter name="mailbox" required="true" argsep="@">
  328. <argument name="username" required="true">
  329. <para>Voicemail username</para>
  330. </argument>
  331. <argument name="domain" required="true">
  332. <para>Voicemail domain</para>
  333. </argument>
  334. </parameter>
  335. <parameter name="options" required="false">
  336. <optionlist>
  337. <option name="u">
  338. <para>Record the <literal>unavailable</literal> greeting.</para>
  339. </option>
  340. <option name="b">
  341. <para>Record the <literal>busy</literal> greeting.</para>
  342. </option>
  343. <option name="t">
  344. <para>Record the temporary greeting.</para>
  345. </option>
  346. <option name="n">
  347. <para>Account name.</para>
  348. </option>
  349. </optionlist>
  350. </parameter>
  351. </syntax>
  352. <description>
  353. <para>This application is part of the Mini-Voicemail system, configured in <filename>minivm.conf</filename>.</para>
  354. <para>Use this application to record account specific audio/video messages for busy, unavailable
  355. and temporary messages.</para>
  356. <para>Account specific directories will be created if they do not exist.</para>
  357. <variablelist>
  358. <variable name="MVM_ACCMESS_STATUS">
  359. <para>This is the result of the attempt to record the specified greeting.</para>
  360. <para><literal>FAILED</literal> is set if the file can't be created.</para>
  361. <value name="SUCCESS" />
  362. <value name="FAILED" />
  363. </variable>
  364. </variablelist>
  365. </description>
  366. </application>
  367. <application name="MinivmMWI" language="en_US">
  368. <synopsis>
  369. Send Message Waiting Notification to subscriber(s) of mailbox.
  370. </synopsis>
  371. <syntax>
  372. <parameter name="mailbox" required="true" argsep="@">
  373. <argument name="username" required="true">
  374. <para>Voicemail username</para>
  375. </argument>
  376. <argument name="domain" required="true">
  377. <para>Voicemail domain</para>
  378. </argument>
  379. </parameter>
  380. <parameter name="urgent" required="true">
  381. <para>Number of urgent messages in mailbox.</para>
  382. </parameter>
  383. <parameter name="new" required="true">
  384. <para>Number of new messages in mailbox.</para>
  385. </parameter>
  386. <parameter name="old" required="true">
  387. <para>Number of old messages in mailbox.</para>
  388. </parameter>
  389. </syntax>
  390. <description>
  391. <para>This application is part of the Mini-Voicemail system, configured in <filename>minivm.conf</filename>.</para>
  392. <para>MinivmMWI is used to send message waiting indication to any devices whose channels have
  393. subscribed to the mailbox passed in the first parameter.</para>
  394. </description>
  395. </application>
  396. <function name="MINIVMCOUNTER" language="en_US">
  397. <synopsis>
  398. Reads or sets counters for MiniVoicemail message.
  399. </synopsis>
  400. <syntax argsep=":">
  401. <parameter name="account" required="true">
  402. <para>If account is given and it exists, the counter is specific for the account.</para>
  403. <para>If account is a domain and the domain directory exists, counters are specific for a domain.</para>
  404. </parameter>
  405. <parameter name="name" required="true">
  406. <para>The name of the counter is a string, up to 10 characters.</para>
  407. </parameter>
  408. <parameter name="operand">
  409. <para>The counters never goes below zero. Valid operands for changing the value of a counter when assigning a value are:</para>
  410. <enumlist>
  411. <enum name="i"><para>Increment by value.</para></enum>
  412. <enum name="d"><para>Decrement by value.</para></enum>
  413. <enum name="s"><para>Set to value.</para></enum>
  414. </enumlist>
  415. </parameter>
  416. </syntax>
  417. <description>
  418. <para>The operation is atomic and the counter is locked while changing the value. The counters are stored as text files in the minivm account directories. It might be better to use realtime functions if you are using a database to operate your Asterisk.</para>
  419. </description>
  420. <see-also>
  421. <ref type="application">MinivmRecord</ref>
  422. <ref type="application">MinivmGreet</ref>
  423. <ref type="application">MinivmNotify</ref>
  424. <ref type="application">MinivmDelete</ref>
  425. <ref type="application">MinivmAccMess</ref>
  426. <ref type="application">MinivmMWI</ref>
  427. <ref type="function">MINIVMACCOUNT</ref>
  428. </see-also>
  429. </function>
  430. <function name="MINIVMACCOUNT" language="en_US">
  431. <synopsis>
  432. Gets MiniVoicemail account information.
  433. </synopsis>
  434. <syntax argsep=":">
  435. <parameter name="account" required="true" />
  436. <parameter name="item" required="true">
  437. <para>Valid items are:</para>
  438. <enumlist>
  439. <enum name="path">
  440. <para>Path to account mailbox (if account exists, otherwise temporary mailbox).</para>
  441. </enum>
  442. <enum name="hasaccount">
  443. <para>1 is static Minivm account exists, 0 otherwise.</para>
  444. </enum>
  445. <enum name="fullname">
  446. <para>Full name of account owner.</para>
  447. </enum>
  448. <enum name="email">
  449. <para>Email address used for account.</para>
  450. </enum>
  451. <enum name="etemplate">
  452. <para>Email template for account (default template if none is configured).</para>
  453. </enum>
  454. <enum name="ptemplate">
  455. <para>Pager template for account (default template if none is configured).</para>
  456. </enum>
  457. <enum name="accountcode">
  458. <para>Account code for the voicemail account.</para>
  459. </enum>
  460. <enum name="pincode">
  461. <para>Pin code for voicemail account.</para>
  462. </enum>
  463. <enum name="timezone">
  464. <para>Time zone for voicemail account.</para>
  465. </enum>
  466. <enum name="language">
  467. <para>Language for voicemail account.</para>
  468. </enum>
  469. <enum name="&lt;channel variable name&gt;">
  470. <para>Channel variable value (set in configuration for account).</para>
  471. </enum>
  472. </enumlist>
  473. </parameter>
  474. </syntax>
  475. <description>
  476. <para />
  477. </description>
  478. <see-also>
  479. <ref type="application">MinivmRecord</ref>
  480. <ref type="application">MinivmGreet</ref>
  481. <ref type="application">MinivmNotify</ref>
  482. <ref type="application">MinivmDelete</ref>
  483. <ref type="application">MinivmAccMess</ref>
  484. <ref type="application">MinivmMWI</ref>
  485. <ref type="function">MINIVMCOUNTER</ref>
  486. </see-also>
  487. </function>
  488. <managerEvent language="en_US" name="MiniVoiceMail">
  489. <managerEventInstance class="EVENT_FLAG_CALL">
  490. <synopsis>Raised when a notification is sent out by a MiniVoiceMail application</synopsis>
  491. <syntax>
  492. <channel_snapshot/>
  493. <parameter name="Action">
  494. <para>What action was taken. Currently, this will always be <literal>SentNotification</literal></para>
  495. </parameter>
  496. <parameter name="Mailbox">
  497. <para>The mailbox that the notification was about, specified as <literal>mailbox</literal>@<literal>context</literal></para>
  498. </parameter>
  499. <parameter name="Counter">
  500. <para>A message counter derived from the <literal>MVM_COUNTER</literal> channel variable.</para>
  501. </parameter>
  502. </syntax>
  503. </managerEventInstance>
  504. </managerEvent>
  505. ***/
  506. #ifndef TRUE
  507. #define TRUE 1
  508. #endif
  509. #ifndef FALSE
  510. #define FALSE 0
  511. #endif
  512. #define MVM_REVIEW (1 << 0) /*!< Review message */
  513. #define MVM_OPERATOR (1 << 1) /*!< Operator exit during voicemail recording */
  514. #define MVM_REALTIME (1 << 2) /*!< This user is a realtime account */
  515. #define MVM_SVMAIL (1 << 3)
  516. #define MVM_ENVELOPE (1 << 4)
  517. #define MVM_PBXSKIP (1 << 9)
  518. #define MVM_ALLOCED (1 << 13)
  519. /*! \brief Default mail command to mail voicemail. Change it with the
  520. mailcmd= command in voicemail.conf */
  521. #define SENDMAIL "/usr/sbin/sendmail -t"
  522. #define SOUND_INTRO "vm-intro"
  523. #define B64_BASEMAXINLINE 256 /*!< Buffer size for Base 64 attachment encoding */
  524. #define B64_BASELINELEN 72 /*!< Line length for Base 64 endoded messages */
  525. #define EOL "\r\n"
  526. #define MAX_DATETIME_FORMAT 512
  527. #define MAX_NUM_CID_CONTEXTS 10
  528. #define ERROR_LOCK_PATH -100
  529. #define VOICEMAIL_DIR_MODE 0700
  530. #define VOICEMAIL_CONFIG "minivm.conf"
  531. #define ASTERISK_USERNAME "asterisk" /*!< Default username for sending mail is asterisk\@localhost */
  532. /*! \brief Message types for notification */
  533. enum mvm_messagetype {
  534. MVM_MESSAGE_EMAIL,
  535. MVM_MESSAGE_PAGE
  536. /* For trunk: MVM_MESSAGE_JABBER, */
  537. };
  538. static char MVM_SPOOL_DIR[PATH_MAX];
  539. /* Module declarations */
  540. static char *app_minivm_record = "MinivmRecord"; /* Leave a message */
  541. static char *app_minivm_greet = "MinivmGreet"; /* Play voicemail prompts */
  542. static char *app_minivm_notify = "MinivmNotify"; /* Notify about voicemail by using one of several methods */
  543. static char *app_minivm_delete = "MinivmDelete"; /* Notify about voicemail by using one of several methods */
  544. static char *app_minivm_accmess = "MinivmAccMess"; /* Record personal voicemail messages */
  545. static char *app_minivm_mwi = "MinivmMWI";
  546. enum minivm_option_flags {
  547. OPT_SILENT = (1 << 0),
  548. OPT_BUSY_GREETING = (1 << 1),
  549. OPT_UNAVAIL_GREETING = (1 << 2),
  550. OPT_TEMP_GREETING = (1 << 3),
  551. OPT_NAME_GREETING = (1 << 4),
  552. OPT_RECORDGAIN = (1 << 5),
  553. };
  554. enum minivm_option_args {
  555. OPT_ARG_RECORDGAIN = 0,
  556. OPT_ARG_ARRAY_SIZE = 1,
  557. };
  558. AST_APP_OPTIONS(minivm_app_options, {
  559. AST_APP_OPTION('s', OPT_SILENT),
  560. AST_APP_OPTION('b', OPT_BUSY_GREETING),
  561. AST_APP_OPTION('u', OPT_UNAVAIL_GREETING),
  562. AST_APP_OPTION_ARG('g', OPT_RECORDGAIN, OPT_ARG_RECORDGAIN),
  563. });
  564. AST_APP_OPTIONS(minivm_accmess_options, {
  565. AST_APP_OPTION('b', OPT_BUSY_GREETING),
  566. AST_APP_OPTION('u', OPT_UNAVAIL_GREETING),
  567. AST_APP_OPTION('t', OPT_TEMP_GREETING),
  568. AST_APP_OPTION('n', OPT_NAME_GREETING),
  569. });
  570. /*!\internal
  571. * \brief Structure for linked list of Mini-Voicemail users: \ref minivm_accounts */
  572. struct minivm_account {
  573. char username[AST_MAX_CONTEXT]; /*!< Mailbox username */
  574. char domain[AST_MAX_CONTEXT]; /*!< Voicemail domain */
  575. char pincode[10]; /*!< Secret pin code, numbers only */
  576. char fullname[120]; /*!< Full name, for directory app */
  577. char email[80]; /*!< E-mail address - override */
  578. char pager[80]; /*!< E-mail address to pager (no attachment) */
  579. char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Voicemail account account code */
  580. char serveremail[80]; /*!< From: Mail address */
  581. char externnotify[160]; /*!< Configurable notification command */
  582. char language[MAX_LANGUAGE]; /*!< Config: Language setting */
  583. char zonetag[80]; /*!< Time zone */
  584. char uniqueid[20]; /*!< Unique integer identifier */
  585. char exit[80]; /*!< Options for exiting from voicemail() */
  586. char attachfmt[80]; /*!< Format for voicemail audio file attachment */
  587. char etemplate[80]; /*!< Pager template */
  588. char ptemplate[80]; /*!< Voicemail format */
  589. unsigned int flags; /*!< MVM_ flags */
  590. struct ast_variable *chanvars; /*!< Variables for e-mail template */
  591. double volgain; /*!< Volume gain for voicemails sent via e-mail */
  592. AST_LIST_ENTRY(minivm_account) list;
  593. };
  594. /*!\internal
  595. * \brief The list of e-mail accounts */
  596. static AST_LIST_HEAD_STATIC(minivm_accounts, minivm_account);
  597. /*!\internal
  598. * \brief Linked list of e-mail templates in various languages
  599. * These are used as templates for e-mails, pager messages and jabber messages
  600. * \ref message_templates
  601. */
  602. struct minivm_template {
  603. char name[80]; /*!< Template name */
  604. char *body; /*!< Body of this template */
  605. char fromaddress[100]; /*!< Who's sending the e-mail? */
  606. char serveremail[80]; /*!< From: Mail address */
  607. char subject[100]; /*!< Subject line */
  608. char charset[32]; /*!< Default character set for this template */
  609. char locale[20]; /*!< Locale for setlocale() */
  610. char dateformat[80]; /*!< Date format to use in this attachment */
  611. int attachment; /*!< Attachment of media yes/no - no for pager messages */
  612. AST_LIST_ENTRY(minivm_template) list; /*!< List mechanics */
  613. };
  614. /*! \brief The list of e-mail templates */
  615. static AST_LIST_HEAD_STATIC(message_templates, minivm_template);
  616. /*! \brief Options for leaving voicemail with the voicemail() application */
  617. struct leave_vm_options {
  618. unsigned int flags;
  619. signed char record_gain;
  620. };
  621. /*! \brief Structure for base64 encoding */
  622. struct b64_baseio {
  623. int iocp;
  624. int iolen;
  625. int linelength;
  626. int ateof;
  627. unsigned char iobuf[B64_BASEMAXINLINE];
  628. };
  629. /*! \brief Voicemail time zones */
  630. struct minivm_zone {
  631. char name[80]; /*!< Name of this time zone */
  632. char timezone[80]; /*!< Timezone definition */
  633. char msg_format[BUFSIZ]; /*!< Not used in minivm ...yet */
  634. AST_LIST_ENTRY(minivm_zone) list; /*!< List mechanics */
  635. };
  636. /*! \brief The list of e-mail time zones */
  637. static AST_LIST_HEAD_STATIC(minivm_zones, minivm_zone);
  638. /*! \brief Structure for gathering statistics */
  639. struct minivm_stats {
  640. int voicemailaccounts; /*!< Number of static accounts */
  641. int timezones; /*!< Number of time zones */
  642. int templates; /*!< Number of templates */
  643. struct timeval reset; /*!< Time for last reset */
  644. int receivedmessages; /*!< Number of received messages since reset */
  645. struct timeval lastreceived; /*!< Time for last voicemail sent */
  646. };
  647. /*! \brief Statistics for voicemail */
  648. static struct minivm_stats global_stats;
  649. AST_MUTEX_DEFINE_STATIC(minivmlock); /*!< Lock to protect voicemail system */
  650. AST_MUTEX_DEFINE_STATIC(minivmloglock); /*!< Lock to protect voicemail system log file */
  651. static FILE *minivmlogfile; /*!< The minivm log file */
  652. static int global_vmminmessage; /*!< Minimum duration of messages */
  653. static int global_vmmaxmessage; /*!< Maximum duration of message */
  654. static int global_maxsilence; /*!< Maximum silence during recording */
  655. static int global_maxgreet; /*!< Maximum length of prompts */
  656. static int global_silencethreshold = 128;
  657. static char global_mailcmd[160]; /*!< Configurable mail cmd */
  658. static char global_externnotify[160]; /*!< External notification application */
  659. static char global_logfile[PATH_MAX]; /*!< Global log file for messages */
  660. static char default_vmformat[80];
  661. static struct ast_flags globalflags = {0}; /*!< Global voicemail flags */
  662. static int global_saydurationminfo;
  663. static double global_volgain; /*!< Volume gain for voicmemail via e-mail */
  664. /*!\internal
  665. * \brief Default dateformat, can be overridden in configuration file */
  666. #define DEFAULT_DATEFORMAT "%A, %B %d, %Y at %r"
  667. #define DEFAULT_CHARSET "ISO-8859-1"
  668. /* Forward declarations */
  669. static char *message_template_parse_filebody(const char *filename);
  670. static char *message_template_parse_emailbody(const char *body);
  671. static int create_vmaccount(char *name, struct ast_variable *var, int realtime);
  672. static struct minivm_account *find_user_realtime(const char *domain, const char *username);
  673. static char *handle_minivm_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  674. /*!\internal
  675. * \brief Create message template */
  676. static struct minivm_template *message_template_create(const char *name)
  677. {
  678. struct minivm_template *template;
  679. template = ast_calloc(1, sizeof(*template));
  680. if (!template)
  681. return NULL;
  682. /* Set some defaults for templates */
  683. ast_copy_string(template->name, name, sizeof(template->name));
  684. ast_copy_string(template->dateformat, DEFAULT_DATEFORMAT, sizeof(template->dateformat));
  685. ast_copy_string(template->charset, DEFAULT_CHARSET, sizeof(template->charset));
  686. ast_copy_string(template->subject, "New message in mailbox ${MVM_USERNAME}@${MVM_DOMAIN}", sizeof(template->subject));
  687. template->attachment = TRUE;
  688. return template;
  689. }
  690. /*!\internal
  691. * \brief Release memory allocated by message template */
  692. static void message_template_free(struct minivm_template *template)
  693. {
  694. if (template->body)
  695. ast_free(template->body);
  696. ast_free (template);
  697. }
  698. /*!\internal
  699. * \brief Build message template from configuration */
  700. static int message_template_build(const char *name, struct ast_variable *var)
  701. {
  702. struct minivm_template *template;
  703. int error = 0;
  704. template = message_template_create(name);
  705. if (!template) {
  706. ast_log(LOG_ERROR, "Out of memory, can't allocate message template object %s.\n", name);
  707. return -1;
  708. }
  709. while (var) {
  710. ast_debug(3, "Configuring template option %s = \"%s\" for template %s\n", var->name, var->value, name);
  711. if (!strcasecmp(var->name, "fromaddress")) {
  712. ast_copy_string(template->fromaddress, var->value, sizeof(template->fromaddress));
  713. } else if (!strcasecmp(var->name, "fromemail")) {
  714. ast_copy_string(template->serveremail, var->value, sizeof(template->serveremail));
  715. } else if (!strcasecmp(var->name, "subject")) {
  716. ast_copy_string(template->subject, var->value, sizeof(template->subject));
  717. } else if (!strcasecmp(var->name, "locale")) {
  718. ast_copy_string(template->locale, var->value, sizeof(template->locale));
  719. } else if (!strcasecmp(var->name, "attachmedia")) {
  720. template->attachment = ast_true(var->value);
  721. } else if (!strcasecmp(var->name, "dateformat")) {
  722. ast_copy_string(template->dateformat, var->value, sizeof(template->dateformat));
  723. } else if (!strcasecmp(var->name, "charset")) {
  724. ast_copy_string(template->charset, var->value, sizeof(template->charset));
  725. } else if (!strcasecmp(var->name, "templatefile")) {
  726. if (template->body)
  727. ast_free(template->body);
  728. template->body = message_template_parse_filebody(var->value);
  729. if (!template->body) {
  730. ast_log(LOG_ERROR, "Error reading message body definition file %s\n", var->value);
  731. error++;
  732. }
  733. } else if (!strcasecmp(var->name, "messagebody")) {
  734. if (template->body)
  735. ast_free(template->body);
  736. template->body = message_template_parse_emailbody(var->value);
  737. if (!template->body) {
  738. ast_log(LOG_ERROR, "Error parsing message body definition:\n %s\n", var->value);
  739. error++;
  740. }
  741. } else {
  742. ast_log(LOG_ERROR, "Unknown message template configuration option \"%s=%s\"\n", var->name, var->value);
  743. error++;
  744. }
  745. var = var->next;
  746. }
  747. if (error)
  748. ast_log(LOG_ERROR, "-- %d errors found parsing message template definition %s\n", error, name);
  749. AST_LIST_LOCK(&message_templates);
  750. AST_LIST_INSERT_TAIL(&message_templates, template, list);
  751. AST_LIST_UNLOCK(&message_templates);
  752. global_stats.templates++;
  753. return error;
  754. }
  755. /*!\internal
  756. * \brief Find named template */
  757. static struct minivm_template *message_template_find(const char *name)
  758. {
  759. struct minivm_template *this, *res = NULL;
  760. if (ast_strlen_zero(name))
  761. return NULL;
  762. AST_LIST_LOCK(&message_templates);
  763. AST_LIST_TRAVERSE(&message_templates, this, list) {
  764. if (!strcasecmp(this->name, name)) {
  765. res = this;
  766. break;
  767. }
  768. }
  769. AST_LIST_UNLOCK(&message_templates);
  770. return res;
  771. }
  772. /*!\internal
  773. * \brief Clear list of templates */
  774. static void message_destroy_list(void)
  775. {
  776. struct minivm_template *this;
  777. AST_LIST_LOCK(&message_templates);
  778. while ((this = AST_LIST_REMOVE_HEAD(&message_templates, list))) {
  779. message_template_free(this);
  780. }
  781. AST_LIST_UNLOCK(&message_templates);
  782. }
  783. /*!\internal
  784. * \brief read buffer from file (base64 conversion) */
  785. static int b64_inbuf(struct b64_baseio *bio, FILE *fi)
  786. {
  787. int l;
  788. if (bio->ateof)
  789. return 0;
  790. if ((l = fread(bio->iobuf, 1, B64_BASEMAXINLINE, fi)) != B64_BASEMAXINLINE) {
  791. bio->ateof = 1;
  792. if (l == 0) {
  793. /* Assume EOF */
  794. return 0;
  795. }
  796. }
  797. bio->iolen = l;
  798. bio->iocp = 0;
  799. return 1;
  800. }
  801. /*!\internal
  802. * \brief read character from file to buffer (base64 conversion) */
  803. static int b64_inchar(struct b64_baseio *bio, FILE *fi)
  804. {
  805. if (bio->iocp >= bio->iolen) {
  806. if (!b64_inbuf(bio, fi))
  807. return EOF;
  808. }
  809. return bio->iobuf[bio->iocp++];
  810. }
  811. /*!\internal
  812. * \brief write buffer to file (base64 conversion) */
  813. static int b64_ochar(struct b64_baseio *bio, int c, FILE *so)
  814. {
  815. if (bio->linelength >= B64_BASELINELEN) {
  816. if (fputs(EOL,so) == EOF)
  817. return -1;
  818. bio->linelength= 0;
  819. }
  820. if (putc(((unsigned char) c), so) == EOF)
  821. return -1;
  822. bio->linelength++;
  823. return 1;
  824. }
  825. /*!\internal
  826. * \brief Encode file to base64 encoding for email attachment (base64 conversion) */
  827. static int base_encode(char *filename, FILE *so)
  828. {
  829. unsigned char dtable[B64_BASEMAXINLINE];
  830. int i,hiteof= 0;
  831. FILE *fi;
  832. struct b64_baseio bio;
  833. memset(&bio, 0, sizeof(bio));
  834. bio.iocp = B64_BASEMAXINLINE;
  835. if (!(fi = fopen(filename, "rb"))) {
  836. ast_log(LOG_WARNING, "Failed to open file: %s: %s\n", filename, strerror(errno));
  837. return -1;
  838. }
  839. for (i= 0; i<9; i++) {
  840. dtable[i]= 'A'+i;
  841. dtable[i+9]= 'J'+i;
  842. dtable[26+i]= 'a'+i;
  843. dtable[26+i+9]= 'j'+i;
  844. }
  845. for (i= 0; i < 8; i++) {
  846. dtable[i+18]= 'S'+i;
  847. dtable[26+i+18]= 's'+i;
  848. }
  849. for (i= 0; i < 10; i++) {
  850. dtable[52+i]= '0'+i;
  851. }
  852. dtable[62]= '+';
  853. dtable[63]= '/';
  854. while (!hiteof){
  855. unsigned char igroup[3], ogroup[4];
  856. int c,n;
  857. igroup[0]= igroup[1]= igroup[2]= 0;
  858. for (n= 0; n < 3; n++) {
  859. if ((c = b64_inchar(&bio, fi)) == EOF) {
  860. hiteof= 1;
  861. break;
  862. }
  863. igroup[n]= (unsigned char)c;
  864. }
  865. if (n> 0) {
  866. ogroup[0]= dtable[igroup[0]>>2];
  867. ogroup[1]= dtable[((igroup[0]&3)<<4) | (igroup[1]>>4)];
  868. ogroup[2]= dtable[((igroup[1]&0xF)<<2) | (igroup[2]>>6)];
  869. ogroup[3]= dtable[igroup[2]&0x3F];
  870. if (n<3) {
  871. ogroup[3]= '=';
  872. if (n<2)
  873. ogroup[2]= '=';
  874. }
  875. for (i= 0;i<4;i++)
  876. b64_ochar(&bio, ogroup[i], so);
  877. }
  878. }
  879. /* Put end of line - line feed */
  880. if (fputs(EOL, so) == EOF)
  881. return 0;
  882. fclose(fi);
  883. return 1;
  884. }
  885. static int get_date(char *s, int len)
  886. {
  887. struct ast_tm tm;
  888. struct timeval now = ast_tvnow();
  889. ast_localtime(&now, &tm, NULL);
  890. return ast_strftime(s, len, "%a %b %e %r %Z %Y", &tm);
  891. }
  892. /*!\internal
  893. * \brief Free user structure - if it's allocated */
  894. static void free_user(struct minivm_account *vmu)
  895. {
  896. if (vmu->chanvars)
  897. ast_variables_destroy(vmu->chanvars);
  898. ast_free(vmu);
  899. }
  900. /*!\internal
  901. * \brief Prepare for voicemail template by adding channel variables
  902. * to the channel
  903. */
  904. static void prep_email_sub_vars(struct ast_channel *channel, const struct minivm_account *vmu, const char *cidnum, const char *cidname, const char *dur, const char *date, const char *counter)
  905. {
  906. char callerid[256];
  907. struct ast_variable *var;
  908. if (!channel) {
  909. ast_log(LOG_ERROR, "No allocated channel, giving up...\n");
  910. return;
  911. }
  912. for (var = vmu->chanvars ; var ; var = var->next) {
  913. pbx_builtin_setvar_helper(channel, var->name, var->value);
  914. }
  915. /* Prepare variables for substition in email body and subject */
  916. pbx_builtin_setvar_helper(channel, "MVM_NAME", vmu->fullname);
  917. pbx_builtin_setvar_helper(channel, "MVM_DUR", dur);
  918. pbx_builtin_setvar_helper(channel, "MVM_DOMAIN", vmu->domain);
  919. pbx_builtin_setvar_helper(channel, "MVM_USERNAME", vmu->username);
  920. pbx_builtin_setvar_helper(channel, "MVM_CALLERID", ast_callerid_merge(callerid, sizeof(callerid), cidname, cidnum, "Unknown Caller"));
  921. pbx_builtin_setvar_helper(channel, "MVM_CIDNAME", (cidname ? cidname : "an unknown caller"));
  922. pbx_builtin_setvar_helper(channel, "MVM_CIDNUM", (cidnum ? cidnum : "an unknown caller"));
  923. pbx_builtin_setvar_helper(channel, "MVM_DATE", date);
  924. if (!ast_strlen_zero(counter))
  925. pbx_builtin_setvar_helper(channel, "MVM_COUNTER", counter);
  926. }
  927. /*!\internal
  928. * \brief Set default values for Mini-Voicemail users */
  929. static void populate_defaults(struct minivm_account *vmu)
  930. {
  931. ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL);
  932. ast_copy_string(vmu->attachfmt, default_vmformat, sizeof(vmu->attachfmt));
  933. vmu->volgain = global_volgain;
  934. }
  935. /*!\internal
  936. * \brief Allocate new vm user and set default values */
  937. static struct minivm_account *mvm_user_alloc(void)
  938. {
  939. struct minivm_account *new;
  940. new = ast_calloc(1, sizeof(*new));
  941. if (!new)
  942. return NULL;
  943. populate_defaults(new);
  944. return new;
  945. }
  946. /*!\internal
  947. * \brief Clear list of users */
  948. static void vmaccounts_destroy_list(void)
  949. {
  950. struct minivm_account *this;
  951. AST_LIST_LOCK(&minivm_accounts);
  952. while ((this = AST_LIST_REMOVE_HEAD(&minivm_accounts, list)))
  953. ast_free(this);
  954. AST_LIST_UNLOCK(&minivm_accounts);
  955. }
  956. /*!\internal
  957. * \brief Find user from static memory object list */
  958. static struct minivm_account *find_account(const char *domain, const char *username, int createtemp)
  959. {
  960. struct minivm_account *vmu = NULL, *cur;
  961. if (ast_strlen_zero(domain) || ast_strlen_zero(username)) {
  962. ast_log(LOG_NOTICE, "No username or domain? \n");
  963. return NULL;
  964. }
  965. ast_debug(3, "Looking for voicemail user %s in domain %s\n", username, domain);
  966. AST_LIST_LOCK(&minivm_accounts);
  967. AST_LIST_TRAVERSE(&minivm_accounts, cur, list) {
  968. /* Is this the voicemail account we're looking for? */
  969. if (!strcasecmp(domain, cur->domain) && !strcasecmp(username, cur->username))
  970. break;
  971. }
  972. AST_LIST_UNLOCK(&minivm_accounts);
  973. if (cur) {
  974. ast_debug(3, "Found account for %s@%s\n", username, domain);
  975. vmu = cur;
  976. } else
  977. vmu = find_user_realtime(domain, username);
  978. if (createtemp && !vmu) {
  979. /* Create a temporary user, send e-mail and be gone */
  980. vmu = mvm_user_alloc();
  981. ast_set2_flag(vmu, TRUE, MVM_ALLOCED);
  982. if (vmu) {
  983. ast_copy_string(vmu->username, username, sizeof(vmu->username));
  984. ast_copy_string(vmu->domain, domain, sizeof(vmu->domain));
  985. ast_debug(1, "Created temporary account\n");
  986. }
  987. }
  988. return vmu;
  989. }
  990. /*!\internal
  991. * \brief Find user in realtime storage
  992. * \return pointer to minivm_account structure
  993. */
  994. static struct minivm_account *find_user_realtime(const char *domain, const char *username)
  995. {
  996. struct ast_variable *var;
  997. struct minivm_account *retval;
  998. char name[MAXHOSTNAMELEN];
  999. retval = mvm_user_alloc();
  1000. if (!retval)
  1001. return NULL;
  1002. if (username)
  1003. ast_copy_string(retval->username, username, sizeof(retval->username));
  1004. populate_defaults(retval);
  1005. var = ast_load_realtime("minivm", "username", username, "domain", domain, SENTINEL);
  1006. if (!var) {
  1007. ast_free(retval);
  1008. return NULL;
  1009. }
  1010. snprintf(name, sizeof(name), "%s@%s", username, domain);
  1011. create_vmaccount(name, var, TRUE);
  1012. ast_variables_destroy(var);
  1013. return retval;
  1014. }
  1015. /*!\internal
  1016. * \brief Check if the string would need encoding within the MIME standard, to
  1017. * avoid confusing certain mail software that expects messages to be 7-bit
  1018. * clean.
  1019. */
  1020. static int check_mime(const char *str)
  1021. {
  1022. for (; *str; str++) {
  1023. if (*str > 126 || *str < 32 || strchr("()<>@,:;/\"[]?.=", *str)) {
  1024. return 1;
  1025. }
  1026. }
  1027. return 0;
  1028. }
  1029. /*!\internal
  1030. * \brief Encode a string according to the MIME rules for encoding strings
  1031. * that are not 7-bit clean or contain control characters.
  1032. *
  1033. * Additionally, if the encoded string would exceed the MIME limit of 76
  1034. * characters per line, then the encoding will be broken up into multiple
  1035. * sections, separated by a space character, in order to facilitate
  1036. * breaking up the associated header across multiple lines.
  1037. *
  1038. * \param end An expandable buffer for holding the result
  1039. * \param maxlen \see ast_str
  1040. * \param charset Character set in which the result should be encoded
  1041. * \param start A string to be encoded
  1042. * \param preamble The length of the first line already used for this string,
  1043. * to ensure that each line maintains a maximum length of 76 chars.
  1044. * \param postamble the length of any additional characters appended to the
  1045. * line, used to ensure proper field wrapping.
  1046. * \return The encoded string.
  1047. */
  1048. static const char *ast_str_encode_mime(struct ast_str **end, ssize_t maxlen, const char *charset, const char *start, size_t preamble, size_t postamble)
  1049. {
  1050. struct ast_str *tmp = ast_str_alloca(80);
  1051. int first_section = 1;
  1052. ast_str_reset(*end);
  1053. ast_str_set(&tmp, -1, "=?%s?Q?", charset);
  1054. for (; *start; start++) {
  1055. int need_encoding = 0;
  1056. if (*start < 33 || *start > 126 || strchr("()<>@,:;/\"[]?.=_", *start)) {
  1057. need_encoding = 1;
  1058. }
  1059. if ((first_section && need_encoding && preamble + ast_str_strlen(tmp) > 70) ||
  1060. (first_section && !need_encoding && preamble + ast_str_strlen(tmp) > 72) ||
  1061. (!first_section && need_encoding && ast_str_strlen(tmp) > 70) ||
  1062. (!first_section && !need_encoding && ast_str_strlen(tmp) > 72)) {
  1063. /* Start new line */
  1064. ast_str_append(end, maxlen, "%s%s?=", first_section ? "" : " ", ast_str_buffer(tmp));
  1065. ast_str_set(&tmp, -1, "=?%s?Q?", charset);
  1066. first_section = 0;
  1067. }
  1068. if (need_encoding && *start == ' ') {
  1069. ast_str_append(&tmp, -1, "_");
  1070. } else if (need_encoding) {
  1071. ast_str_append(&tmp, -1, "=%hhX", *start);
  1072. } else {
  1073. ast_str_append(&tmp, -1, "%c", *start);
  1074. }
  1075. }
  1076. ast_str_append(end, maxlen, "%s%s?=%s", first_section ? "" : " ", ast_str_buffer(tmp), ast_str_strlen(tmp) + postamble > 74 ? " " : "");
  1077. return ast_str_buffer(*end);
  1078. }
  1079. /*!\internal
  1080. * \brief Wraps a character sequence in double quotes, escaping occurences of quotes within the string.
  1081. * \param from The string to work with.
  1082. * \param buf The destination buffer to write the modified quoted string.
  1083. * \param maxlen Always zero. \see ast_str
  1084. *
  1085. * \return The destination string with quotes wrapped on it (the to field).
  1086. */
  1087. static const char *ast_str_quote(struct ast_str **buf, ssize_t maxlen, const char *from)
  1088. {
  1089. const char *ptr;
  1090. /* We're only ever passing 0 to maxlen, so short output isn't possible */
  1091. ast_str_set(buf, maxlen, "\"");
  1092. for (ptr = from; *ptr; ptr++) {
  1093. if (*ptr == '"' || *ptr == '\\') {
  1094. ast_str_append(buf, maxlen, "\\%c", *ptr);
  1095. } else {
  1096. ast_str_append(buf, maxlen, "%c", *ptr);
  1097. }
  1098. }
  1099. ast_str_append(buf, maxlen, "\"");
  1100. return ast_str_buffer(*buf);
  1101. }
  1102. /*!\internal
  1103. * \brief Send voicemail with audio file as an attachment */
  1104. static int sendmail(struct minivm_template *template, struct minivm_account *vmu, char *cidnum, char *cidname, const char *filename, char *format, int duration, int attach_user_voicemail, enum mvm_messagetype type, const char *counter)
  1105. {
  1106. RAII_VAR(struct ast_str *, str1, ast_str_create(16), ast_free);
  1107. RAII_VAR(struct ast_str *, str2, ast_str_create(16), ast_free);
  1108. FILE *p = NULL;
  1109. int pfd;
  1110. char email[256] = "";
  1111. char who[256] = "";
  1112. char date[256];
  1113. char bound[256];
  1114. char fname[PATH_MAX];
  1115. char dur[PATH_MAX];
  1116. char tmp[80] = "/tmp/astmail-XXXXXX";
  1117. char mail_cmd_buffer[PATH_MAX];
  1118. char sox_gain_tmpdir[PATH_MAX] = ""; /* Only used with volgain */
  1119. char *file_to_delete = NULL, *dir_to_delete = NULL;
  1120. struct timeval now;
  1121. struct ast_tm tm;
  1122. struct minivm_zone *the_zone = NULL;
  1123. struct ast_channel *chan = NULL;
  1124. char *fromaddress;
  1125. char *fromemail;
  1126. int res = -1;
  1127. if (!str1 || !str2) {
  1128. return -1;
  1129. }
  1130. if (type == MVM_MESSAGE_EMAIL) {
  1131. if (vmu && !ast_strlen_zero(vmu->email)) {
  1132. ast_copy_string(email, vmu->email, sizeof(email));
  1133. } else if (!ast_strlen_zero(vmu->username) && !ast_strlen_zero(vmu->domain))
  1134. snprintf(email, sizeof(email), "%s@%s", vmu->username, vmu->domain);
  1135. } else if (type == MVM_MESSAGE_PAGE) {
  1136. ast_copy_string(email, vmu->pager, sizeof(email));
  1137. }
  1138. if (ast_strlen_zero(email)) {
  1139. ast_log(LOG_WARNING, "No address to send message to.\n");
  1140. return -1;
  1141. }
  1142. ast_debug(3, "Sending mail to %s@%s - Using template %s\n", vmu->username, vmu->domain, template->name);
  1143. if (!strcmp(format, "wav49"))
  1144. format = "WAV";
  1145. /* If we have a gain option, process it now with sox */
  1146. if (type == MVM_MESSAGE_EMAIL && (vmu->volgain < -.001 || vmu->volgain > .001) ) {
  1147. char sox_gain_cmd[PATH_MAX];
  1148. ast_copy_string(sox_gain_tmpdir, "/tmp/minivm-gain-XXXXXX", sizeof(sox_gain_tmpdir));
  1149. ast_debug(3, "sox_gain_tmpdir: %s\n", sox_gain_tmpdir);
  1150. if (!mkdtemp(sox_gain_tmpdir)) {
  1151. ast_log(LOG_WARNING, "Failed to create temporary directory for volgain: %d\n", errno);
  1152. return -1;
  1153. }
  1154. snprintf(fname, sizeof(fname), "%s/output.%s", sox_gain_tmpdir, format);
  1155. snprintf(sox_gain_cmd, sizeof(sox_gain_cmd), "sox -v %.4f %s.%s %s", vmu->volgain, filename, format, fname);
  1156. ast_safe_system(sox_gain_cmd);
  1157. ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", filename, format, vmu->volgain, vmu->username);
  1158. /* Mark some things for deletion */
  1159. file_to_delete = fname;
  1160. dir_to_delete = sox_gain_tmpdir;
  1161. } else {
  1162. snprintf(fname, sizeof(fname), "%s.%s", filename, format);
  1163. }
  1164. if (template->attachment)
  1165. ast_debug(1, "Attaching file '%s', format '%s', uservm is '%d'\n", fname, format, attach_user_voicemail);
  1166. /* Make a temporary file instead of piping directly to sendmail, in case the mail
  1167. command hangs */
  1168. pfd = mkstemp(tmp);
  1169. if (pfd > -1) {
  1170. p = fdopen(pfd, "w");
  1171. if (!p) {
  1172. close(pfd);
  1173. pfd = -1;
  1174. }
  1175. ast_debug(1, "Opening temp file for e-mail: %s\n", tmp);
  1176. }
  1177. if (!p) {
  1178. ast_log(LOG_WARNING, "Unable to open temporary file '%s'\n", tmp);
  1179. goto out;
  1180. }
  1181. /* Allocate channel used for chanvar substitution */
  1182. chan = ast_dummy_channel_alloc();
  1183. if (!chan) {
  1184. goto out;
  1185. }
  1186. snprintf(dur, sizeof(dur), "%d:%02d", duration / 60, duration % 60);
  1187. /* Does this user have a timezone specified? */
  1188. if (!ast_strlen_zero(vmu->zonetag)) {
  1189. /* Find the zone in the list */
  1190. struct minivm_zone *z;
  1191. AST_LIST_LOCK(&minivm_zones);
  1192. AST_LIST_TRAVERSE(&minivm_zones, z, list) {
  1193. if (strcmp(z->name, vmu->zonetag))
  1194. continue;
  1195. the_zone = z;
  1196. }
  1197. AST_LIST_UNLOCK(&minivm_zones);
  1198. }
  1199. now = ast_tvnow();
  1200. ast_localtime(&now, &tm, the_zone ? the_zone->timezone : NULL);
  1201. ast_strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", &tm);
  1202. /* Start printing the email to the temporary file */
  1203. fprintf(p, "Date: %s\n", date);
  1204. /* Set date format for voicemail mail */
  1205. ast_strftime(date, sizeof(date), template->dateformat, &tm);
  1206. /* Populate channel with channel variables for substitution */
  1207. prep_email_sub_vars(chan, vmu, cidnum, cidname, dur, date, counter);
  1208. /* Find email address to use */
  1209. /* If there's a server e-mail address in the account, use that, otherwise template */
  1210. fromemail = ast_strlen_zero(vmu->serveremail) ? template->serveremail : vmu->serveremail;
  1211. /* Find name to user for server e-mail */
  1212. fromaddress = ast_strlen_zero(template->fromaddress) ? "" : template->fromaddress;
  1213. /* If needed, add hostname as domain */
  1214. if (ast_strlen_zero(fromemail))
  1215. fromemail = "asterisk";
  1216. if (strchr(fromemail, '@'))
  1217. ast_copy_string(who, fromemail, sizeof(who));
  1218. else {
  1219. char host[MAXHOSTNAMELEN];
  1220. gethostname(host, sizeof(host)-1);
  1221. snprintf(who, sizeof(who), "%s@%s", fromemail, host);
  1222. }
  1223. if (ast_strlen_zero(fromaddress)) {
  1224. fprintf(p, "From: Asterisk PBX <%s>\n", who);
  1225. } else {
  1226. ast_debug(4, "Fromaddress template: %s\n", fromaddress);
  1227. ast_str_substitute_variables(&str1, 0, chan, fromaddress);
  1228. if (check_mime(ast_str_buffer(str1))) {
  1229. int first_line = 1;
  1230. char *ptr;
  1231. ast_str_encode_mime(&str2, 0, template->charset, ast_str_buffer(str1), strlen("From: "), strlen(who) + 3);
  1232. while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
  1233. *ptr = '\0';
  1234. fprintf(p, "%s %s\n", first_line ? "From:" : "", ast_str_buffer(str2));
  1235. first_line = 0;
  1236. /* Substring is smaller, so this will never grow */
  1237. ast_str_set(&str2, 0, "%s", ptr + 1);
  1238. }
  1239. fprintf(p, "%s %s <%s>\n", first_line ? "From:" : "", ast_str_buffer(str2), who);
  1240. } else {
  1241. fprintf(p, "From: %s <%s>\n", ast_str_quote(&str2, 0, ast_str_buffer(str1)), who);
  1242. }
  1243. }
  1244. fprintf(p, "Message-ID: <Asterisk-%u-%s-%d-%s>\n", (unsigned int)ast_random(), vmu->username, (int)getpid(), who);
  1245. if (ast_strlen_zero(vmu->email)) {
  1246. snprintf(email, sizeof(email), "%s@%s", vmu->username, vmu->domain);
  1247. } else {
  1248. ast_copy_string(email, vmu->email, sizeof(email));
  1249. }
  1250. if (check_mime(vmu->fullname)) {
  1251. int first_line = 1;
  1252. char *ptr;
  1253. ast_str_encode_mime(&str2, 0, template->charset, vmu->fullname, strlen("To: "), strlen(email) + 3);
  1254. while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
  1255. *ptr = '\0';
  1256. fprintf(p, "%s %s\n", first_line ? "To:" : "", ast_str_buffer(str2));
  1257. first_line = 0;
  1258. /* Substring is smaller, so this will never grow */
  1259. ast_str_set(&str2, 0, "%s", ptr + 1);
  1260. }
  1261. fprintf(p, "%s %s <%s>\n", first_line ? "To:" : "", ast_str_buffer(str2), email);
  1262. } else {
  1263. fprintf(p, "To: %s <%s>\n", ast_str_quote(&str2, 0, vmu->fullname), email);
  1264. }
  1265. if (!ast_strlen_zero(template->subject)) {
  1266. ast_str_substitute_variables(&str1, 0, chan, template->subject);
  1267. if (check_mime(ast_str_buffer(str1))) {
  1268. int first_line = 1;
  1269. char *ptr;
  1270. ast_str_encode_mime(&str2, 0, template->charset, ast_str_buffer(str1), strlen("Subject: "), 0);
  1271. while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
  1272. *ptr = '\0';
  1273. fprintf(p, "%s %s\n", first_line ? "Subject:" : "", ast_str_buffer(str2));
  1274. first_line = 0;
  1275. /* Substring is smaller, so this will never grow */
  1276. ast_str_set(&str2, 0, "%s", ptr + 1);
  1277. }
  1278. fprintf(p, "%s %s\n", first_line ? "Subject:" : "", ast_str_buffer(str2));
  1279. } else {
  1280. fprintf(p, "Subject: %s\n", ast_str_buffer(str1));
  1281. }
  1282. } else {
  1283. fprintf(p, "Subject: New message in mailbox %s@%s\n", vmu->username, vmu->domain);
  1284. ast_debug(1, "Using default subject for this email \n");
  1285. }
  1286. if (DEBUG_ATLEAST(3))
  1287. fprintf(p, "X-Asterisk-debug: template %s user account %s@%s\n", template->name, vmu->username, vmu->domain);
  1288. fprintf(p, "MIME-Version: 1.0\n");
  1289. /* Something unique. */
  1290. snprintf(bound, sizeof(bound), "voicemail_%s%d%u", vmu->username, (int)getpid(), (unsigned int)ast_random());
  1291. fprintf(p, "Content-Type: multipart/mixed; boundary=\"%s\"\n\n\n", bound);
  1292. fprintf(p, "--%s\n", bound);
  1293. fprintf(p, "Content-Type: text/plain; charset=%s\nContent-Transfer-Encoding: 8bit\n\n", template->charset);
  1294. if (!ast_strlen_zero(template->body)) {
  1295. ast_str_substitute_variables(&str1, 0, chan, template->body);
  1296. ast_debug(3, "Message now: %s\n-----\n", ast_str_buffer(str1));
  1297. fprintf(p, "%s\n", ast_str_buffer(str1));
  1298. } else {
  1299. fprintf(p, "Dear %s:\n\n\tJust wanted to let you know you were just left a %s long message \n"
  1300. "in mailbox %s from %s, on %s so you might\n"
  1301. "want to check it when you get a chance. Thanks!\n\n\t\t\t\t--Asterisk\n\n", vmu->fullname,
  1302. dur, vmu->username, (cidname ? cidname : (cidnum ? cidnum : "an unknown caller")), date);
  1303. ast_debug(3, "Using default message body (no template)\n-----\n");
  1304. }
  1305. /* Eww. We want formats to tell us their own MIME type */
  1306. if (template->attachment) {
  1307. char *ctype = "audio/x-";
  1308. ast_debug(3, "Attaching file to message: %s\n", fname);
  1309. if (!strcasecmp(format, "ogg"))
  1310. ctype = "application/";
  1311. fprintf(p, "--%s\n", bound);
  1312. fprintf(p, "Content-Type: %s%s; name=\"voicemailmsg.%s\"\n", ctype, format, format);
  1313. fprintf(p, "Content-Transfer-Encoding: base64\n");
  1314. fprintf(p, "Content-Description: Voicemail sound attachment.\n");
  1315. fprintf(p, "Content-Disposition: attachment; filename=\"voicemail%s.%s\"\n\n", counter ? counter : "", format);
  1316. base_encode(fname, p);
  1317. fprintf(p, "\n\n--%s--\n.\n", bound);
  1318. }
  1319. fclose(p);
  1320. chan = ast_channel_unref(chan);
  1321. if (file_to_delete && dir_to_delete) {
  1322. /* We can't delete these files ourselves because the mail command will execute in
  1323. the background and we'll end up deleting them out from under it. */
  1324. res = snprintf(mail_cmd_buffer, sizeof(mail_cmd_buffer),
  1325. "( %s < %s ; rm -f %s %s ; rmdir %s ) &",
  1326. global_mailcmd, tmp, tmp, file_to_delete, dir_to_delete);
  1327. } else {
  1328. res = snprintf(mail_cmd_buffer, sizeof(mail_cmd_buffer),
  1329. "( %s < %s ; rm -f %s ) &",
  1330. global_mailcmd, tmp, tmp);
  1331. }
  1332. if (res < sizeof(mail_cmd_buffer)) {
  1333. file_to_delete = dir_to_delete = NULL;
  1334. } else {
  1335. ast_log(LOG_ERROR, "Could not send message, command line too long\n");
  1336. res = -1;
  1337. goto out;
  1338. }
  1339. ast_safe_system(mail_cmd_buffer);
  1340. ast_debug(1, "Sent message to %s with command '%s'%s\n", vmu->email, global_mailcmd, template->attachment ? " - (media attachment)" : "");
  1341. ast_debug(3, "Actual command used: %s\n", mail_cmd_buffer);
  1342. res = 0;
  1343. out:
  1344. if (file_to_delete) {
  1345. unlink(file_to_delete);
  1346. }
  1347. if (dir_to_delete) {
  1348. rmdir(dir_to_delete);
  1349. }
  1350. return res;
  1351. }
  1352. /*!\internal
  1353. * \brief Create directory based on components */
  1354. static int make_dir(char *dest, int len, const char *domain, const char *username, const char *folder)
  1355. {
  1356. return snprintf(dest, len, "%s%s/%s%s%s", MVM_SPOOL_DIR, domain, username, ast_strlen_zero(folder) ? "" : "/", folder ? folder : "");
  1357. }
  1358. /*!\internal
  1359. * \brief Checks if directory exists. Does not create directory, but builds string in dest
  1360. * \param dest String. base directory.
  1361. * \param len Int. Length base directory string.
  1362. * \param domain String. Ignored if is null or empty string.
  1363. * \param username String. Ignored if is null or empty string.
  1364. * \param folder String. Ignored if is null or empty string.
  1365. * \return 0 on failure, 1 on success.
  1366. */
  1367. static int check_dirpath(char *dest, int len, char *domain, char *username, char *folder)
  1368. {
  1369. struct stat filestat;
  1370. make_dir(dest, len, domain, username, folder ? folder : "");
  1371. if (stat(dest, &filestat)== -1)
  1372. return FALSE;
  1373. else
  1374. return TRUE;
  1375. }
  1376. /*!\internal
  1377. * \brief basically mkdir -p $dest/$domain/$username/$folder
  1378. * \param dest String. base directory.
  1379. * \param len Length of directory string
  1380. * \param domain String. Ignored if is null or empty string.
  1381. * \param folder String. Ignored if is null or empty string.
  1382. * \param username String. Ignored if is null or empty string.
  1383. * \return -1 on failure, 0 on success.
  1384. */
  1385. static int create_dirpath(char *dest, int len, char *domain, char *username, char *folder)
  1386. {
  1387. int res;
  1388. make_dir(dest, len, domain, username, folder);
  1389. if ((res = ast_mkdir(dest, 0777))) {
  1390. ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res));
  1391. return -1;
  1392. }
  1393. ast_debug(2, "Creating directory for %s@%s folder %s : %s\n", username, domain, folder, dest);
  1394. return 0;
  1395. }
  1396. /*!\internal
  1397. * \brief Play intro message before recording voicemail
  1398. */
  1399. static int invent_message(struct ast_channel *chan, char *domain, char *username, int busy, char *ecodes)
  1400. {
  1401. int res;
  1402. char fn[PATH_MAX];
  1403. ast_debug(2, "Still preparing to play message ...\n");
  1404. snprintf(fn, sizeof(fn), "%s%s/%s/greet", MVM_SPOOL_DIR, domain, username);
  1405. if (ast_fileexists(fn, NULL, NULL) > 0) {
  1406. res = ast_streamfile(chan, fn, ast_channel_language(chan));
  1407. if (res)
  1408. return -1;
  1409. res = ast_waitstream(chan, ecodes);
  1410. if (res)
  1411. return res;
  1412. } else {
  1413. int numericusername = 1;
  1414. char *i = username;
  1415. ast_debug(2, "No personal prompts. Using default prompt set for language\n");
  1416. while (*i) {
  1417. ast_debug(2, "Numeric? Checking %c\n", *i);
  1418. if (!isdigit(*i)) {
  1419. numericusername = FALSE;
  1420. break;
  1421. }
  1422. i++;
  1423. }
  1424. if (numericusername) {
  1425. if (ast_streamfile(chan, "vm-theperson", ast_channel_language(chan)))
  1426. return -1;
  1427. if ((res = ast_waitstream(chan, ecodes)))
  1428. return res;
  1429. res = ast_say_digit_str(chan, username, ecodes, ast_channel_language(chan));
  1430. if (res)
  1431. return res;
  1432. } else {
  1433. if (ast_streamfile(chan, "vm-theextensionis", ast_channel_language(chan)))
  1434. return -1;
  1435. if ((res = ast_waitstream(chan, ecodes)))
  1436. return res;
  1437. }
  1438. }
  1439. res = ast_streamfile(chan, busy ? "vm-isonphone" : "vm-isunavail", ast_channel_language(chan));
  1440. if (res)
  1441. return -1;
  1442. res = ast_waitstream(chan, ecodes);
  1443. return res;
  1444. }
  1445. /*!\internal
  1446. * \brief Delete media files and attribute file */
  1447. static int vm_delete(char *file)
  1448. {
  1449. int res;
  1450. ast_debug(1, "Deleting voicemail file %s\n", file);
  1451. res = unlink(file); /* Remove the meta data file */
  1452. res |= ast_filedelete(file, NULL); /* remove the media file */
  1453. return res;
  1454. }
  1455. /*!\internal
  1456. * \brief Record voicemail message & let caller review or re-record it, or set options if applicable */
  1457. static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt,
  1458. int outsidecaller, struct minivm_account *vmu, int *duration, int *sound_duration, const char *unlockdir,
  1459. signed char record_gain)
  1460. {
  1461. int cmd = 0;
  1462. int max_attempts = 3;
  1463. int attempts = 0;
  1464. int recorded = 0;
  1465. int message_exists = 0;
  1466. signed char zero_gain = 0;
  1467. char *acceptdtmf = "#";
  1468. char *canceldtmf = "";
  1469. /* Note that urgent and private are for flagging messages as such in the future */
  1470. /* barf if no pointer passed to store duration in */
  1471. if (duration == NULL) {
  1472. ast_log(LOG_WARNING, "Error play_record_review called without duration pointer\n");
  1473. return -1;
  1474. }
  1475. cmd = '3'; /* Want to start by recording */
  1476. while ((cmd >= 0) && (cmd != 't')) {
  1477. switch (cmd) {
  1478. case '1':
  1479. ast_verb(3, "Saving message as is\n");
  1480. ast_stream_and_wait(chan, "vm-msgsaved", "");
  1481. cmd = 't';
  1482. break;
  1483. case '2':
  1484. /* Review */
  1485. ast_verb(3, "Reviewing the message\n");
  1486. ast_streamfile(chan, recordfile, ast_channel_language(chan));
  1487. cmd = ast_waitstream(chan, AST_DIGIT_ANY);
  1488. break;
  1489. case '3':
  1490. message_exists = 0;
  1491. /* Record */
  1492. if (recorded == 1)
  1493. ast_verb(3, "Re-recording the message\n");
  1494. else
  1495. ast_verb(3, "Recording the message\n");
  1496. if (recorded && outsidecaller)
  1497. cmd = ast_play_and_wait(chan, "beep");
  1498. recorded = 1;
  1499. /* After an attempt has been made to record message, we have to take care of INTRO and beep for incoming messages, but not for greetings */
  1500. if (record_gain)
  1501. ast_channel_setoption(chan, AST_OPTION_RXGAIN, &record_gain, sizeof(record_gain), 0);
  1502. if (ast_test_flag(vmu, MVM_OPERATOR))
  1503. canceldtmf = "0";
  1504. cmd = ast_play_and_record_full(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, 0, global_silencethreshold, global_maxsilence, unlockdir, acceptdtmf, canceldtmf, 0, AST_RECORD_IF_EXISTS_OVERWRITE);
  1505. if (record_gain)
  1506. ast_channel_setoption(chan, AST_OPTION_RXGAIN, &zero_gain, sizeof(zero_gain), 0);
  1507. if (cmd == -1) /* User has hung up, no options to give */
  1508. return cmd;
  1509. if (cmd == '0')
  1510. break;
  1511. else if (cmd == '*')
  1512. break;
  1513. else {
  1514. /* If all is well, a message exists */
  1515. message_exists = 1;
  1516. cmd = 0;
  1517. }
  1518. break;
  1519. case '4':
  1520. case '5':
  1521. case '6':
  1522. case '7':
  1523. case '8':
  1524. case '9':
  1525. case '*':
  1526. case '#':
  1527. cmd = ast_play_and_wait(chan, "vm-sorry");
  1528. break;
  1529. case '0':
  1530. if(!ast_test_flag(vmu, MVM_OPERATOR)) {
  1531. cmd = ast_play_and_wait(chan, "vm-sorry");
  1532. break;
  1533. }
  1534. if (message_exists || recorded) {
  1535. cmd = ast_play_and_wait(chan, "vm-saveoper");
  1536. if (!cmd)
  1537. cmd = ast_waitfordigit(chan, 3000);
  1538. if (cmd == '1') {
  1539. ast_play_and_wait(chan, "vm-msgsaved");
  1540. cmd = '0';
  1541. } else {
  1542. ast_play_and_wait(chan, "vm-deleted");
  1543. vm_delete(recordfile);
  1544. cmd = '0';
  1545. }
  1546. }
  1547. return cmd;
  1548. default:
  1549. /* If the caller is an ouside caller, and the review option is enabled,
  1550. allow them to review the message, but let the owner of the box review
  1551. their OGM's */
  1552. if (outsidecaller && !ast_test_flag(vmu, MVM_REVIEW))
  1553. return cmd;
  1554. if (message_exists) {
  1555. cmd = ast_play_and_wait(chan, "vm-review");
  1556. } else {
  1557. cmd = ast_play_and_wait(chan, "vm-torerecord");
  1558. if (!cmd)
  1559. cmd = ast_waitfordigit(chan, 600);
  1560. }
  1561. if (!cmd && outsidecaller && ast_test_flag(vmu, MVM_OPERATOR)) {
  1562. cmd = ast_play_and_wait(chan, "vm-reachoper");
  1563. if (!cmd)
  1564. cmd = ast_waitfordigit(chan, 600);
  1565. }
  1566. if (!cmd)
  1567. cmd = ast_waitfordigit(chan, 6000);
  1568. if (!cmd) {
  1569. attempts++;
  1570. }
  1571. if (attempts > max_attempts) {
  1572. cmd = 't';
  1573. }
  1574. }
  1575. }
  1576. if (outsidecaller)
  1577. ast_play_and_wait(chan, "vm-goodbye");
  1578. if (cmd == 't')
  1579. cmd = 0;
  1580. return cmd;
  1581. }
  1582. /*! \brief Run external notification for voicemail message */
  1583. static void run_externnotify(struct ast_channel *chan, struct minivm_account *vmu)
  1584. {
  1585. char fquser[AST_MAX_CONTEXT * 2];
  1586. char *argv[5] = { NULL };
  1587. struct ast_party_caller *caller;
  1588. char *cid;
  1589. int idx;
  1590. if (ast_strlen_zero(vmu->externnotify) && ast_strlen_zero(global_externnotify)) {
  1591. return;
  1592. }
  1593. snprintf(fquser, sizeof(fquser), "%s@%s", vmu->username, vmu->domain);
  1594. caller = ast_channel_caller(chan);
  1595. idx = 0;
  1596. argv[idx++] = ast_strlen_zero(vmu->externnotify) ? global_externnotify : vmu->externnotify;
  1597. argv[idx++] = fquser;
  1598. cid = S_COR(caller->id.name.valid, caller->id.name.str, NULL);
  1599. if (cid) {
  1600. argv[idx++] = cid;
  1601. }
  1602. cid = S_COR(caller->id.number.valid, caller->id.number.str, NULL);
  1603. if (cid) {
  1604. argv[idx++] = cid;
  1605. }
  1606. argv[idx] = NULL;
  1607. ast_debug(1, "Executing: %s %s %s %s\n",
  1608. argv[0], argv[1], argv[2] ?: "", argv[3] ?: "");
  1609. ast_safe_execvp(1, argv[0], argv);
  1610. }
  1611. /*!\internal
  1612. * \brief Send message to voicemail account owner */
  1613. static int notify_new_message(struct ast_channel *chan, const char *templatename, struct minivm_account *vmu, const char *filename, long duration, const char *format, char *cidnum, char *cidname)
  1614. {
  1615. RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
  1616. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  1617. RAII_VAR(struct ast_mwi_state *, mwi_state, NULL, ao2_cleanup);
  1618. char *stringp;
  1619. struct minivm_template *etemplate;
  1620. char *messageformat;
  1621. int res = 0;
  1622. char oldlocale[100];
  1623. const char *counter;
  1624. if (!ast_strlen_zero(vmu->attachfmt)) {
  1625. if (strstr(format, vmu->attachfmt)) {
  1626. format = vmu->attachfmt;
  1627. } else {
  1628. ast_log(LOG_WARNING, "Attachment format '%s' is not one of the recorded formats '%s'. Falling back to default format for '%s@%s'.\n", vmu->attachfmt, format, vmu->username, vmu->domain);
  1629. }
  1630. }
  1631. etemplate = message_template_find(vmu->etemplate);
  1632. if (!etemplate)
  1633. etemplate = message_template_find(templatename);
  1634. if (!etemplate)
  1635. etemplate = message_template_find("email-default");
  1636. /* Attach only the first format */
  1637. stringp = messageformat = ast_strdupa(format);
  1638. strsep(&stringp, "|");
  1639. if (!ast_strlen_zero(etemplate->locale)) {
  1640. char *new_locale;
  1641. ast_copy_string(oldlocale, setlocale(LC_TIME, NULL), sizeof(oldlocale));
  1642. ast_debug(2, "Changing locale from %s to %s\n", oldlocale, etemplate->locale);
  1643. new_locale = setlocale(LC_TIME, etemplate->locale);
  1644. if (new_locale == NULL) {
  1645. ast_log(LOG_WARNING, "-_-_- Changing to new locale did not work. Locale: %s\n", etemplate->locale);
  1646. }
  1647. }
  1648. /* Read counter if available */
  1649. ast_channel_lock(chan);
  1650. if ((counter = pbx_builtin_getvar_helper(chan, "MVM_COUNTER"))) {
  1651. counter = ast_strdupa(counter);
  1652. }
  1653. ast_channel_unlock(chan);
  1654. if (ast_strlen_zero(counter)) {
  1655. ast_debug(2, "MVM_COUNTER not found\n");
  1656. } else {
  1657. ast_debug(2, "MVM_COUNTER found - will use it with value %s\n", counter);
  1658. }
  1659. res = sendmail(etemplate, vmu, cidnum, cidname, filename, messageformat, duration, etemplate->attachment, MVM_MESSAGE_EMAIL, counter);
  1660. if (res == 0 && !ast_strlen_zero(vmu->pager)) {
  1661. /* Find template for paging */
  1662. etemplate = message_template_find(vmu->ptemplate);
  1663. if (!etemplate)
  1664. etemplate = message_template_find("pager-default");
  1665. if (!ast_strlen_zero(etemplate->locale)) {
  1666. ast_copy_string(oldlocale, setlocale(LC_TIME, ""), sizeof(oldlocale));
  1667. setlocale(LC_TIME, etemplate->locale);
  1668. }
  1669. res = sendmail(etemplate, vmu, cidnum, cidname, filename, messageformat, duration, etemplate->attachment, MVM_MESSAGE_PAGE, counter);
  1670. }
  1671. mwi_state = ast_mwi_create(vmu->username, vmu->domain);
  1672. if (!mwi_state) {
  1673. goto notify_cleanup;
  1674. }
  1675. mwi_state->snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
  1676. json_object = ast_json_pack("{s: s, s: s, s: s}",
  1677. "Event", "MiniVoiceMail",
  1678. "Action", "SentNotification",
  1679. "Counter", counter ?: "");
  1680. if (!json_object) {
  1681. goto notify_cleanup;
  1682. }
  1683. message = ast_mwi_blob_create(mwi_state, ast_mwi_vm_app_type(), json_object);
  1684. if (!message) {
  1685. goto notify_cleanup;
  1686. }
  1687. stasis_publish(ast_mwi_topic(mwi_state->uniqueid), message);
  1688. notify_cleanup:
  1689. run_externnotify(chan, vmu); /* Run external notification */
  1690. if (!ast_strlen_zero(etemplate->locale)) {
  1691. setlocale(LC_TIME, oldlocale); /* Reset to old locale */
  1692. }
  1693. return res;
  1694. }
  1695. /*!\internal
  1696. * \brief Record voicemail message, store into file prepared for sending e-mail */
  1697. static int leave_voicemail(struct ast_channel *chan, char *username, struct leave_vm_options *options)
  1698. {
  1699. char tmptxtfile[PATH_MAX];
  1700. char callerid[256];
  1701. FILE *txt;
  1702. int res = 0, txtdes;
  1703. int duration = 0;
  1704. int sound_duration = 0;
  1705. char date[256];
  1706. char tmpdir[PATH_MAX];
  1707. char ext_context[256] = "";
  1708. char fmt[80];
  1709. char *domain;
  1710. char tmp[256] = "";
  1711. struct minivm_account *vmu;
  1712. int userdir;
  1713. ast_copy_string(tmp, username, sizeof(tmp));
  1714. username = tmp;
  1715. domain = strchr(tmp, '@');
  1716. if (domain) {
  1717. *domain = '\0';
  1718. domain++;
  1719. }
  1720. if (!(vmu = find_account(domain, username, TRUE))) {
  1721. /* We could not find user, let's exit */
  1722. ast_log(LOG_ERROR, "Can't allocate temporary account for '%s@%s'\n", username, domain);
  1723. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED");
  1724. return 0;
  1725. }
  1726. /* Setup pre-file if appropriate */
  1727. if (strcmp(vmu->domain, "localhost"))
  1728. snprintf(ext_context, sizeof(ext_context), "%s@%s", username, vmu->domain);
  1729. else
  1730. ast_copy_string(ext_context, vmu->domain, sizeof(ext_context));
  1731. /* The meat of recording the message... All the announcements and beeps have been played*/
  1732. if (ast_strlen_zero(vmu->attachfmt))
  1733. ast_copy_string(fmt, default_vmformat, sizeof(fmt));
  1734. else
  1735. ast_copy_string(fmt, vmu->attachfmt, sizeof(fmt));
  1736. if (ast_strlen_zero(fmt)) {
  1737. ast_log(LOG_WARNING, "No format for saving voicemail? Default %s\n", default_vmformat);
  1738. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED");
  1739. return res;
  1740. }
  1741. userdir = check_dirpath(tmpdir, sizeof(tmpdir), vmu->domain, username, "tmp");
  1742. /* If we have no user directory, use generic temporary directory */
  1743. if (!userdir) {
  1744. create_dirpath(tmpdir, sizeof(tmpdir), "0000_minivm_temp", "mediafiles", "");
  1745. ast_debug(3, "Creating temporary directory %s\n", tmpdir);
  1746. }
  1747. snprintf(tmptxtfile, sizeof(tmptxtfile), "%s/XXXXXX", tmpdir);
  1748. /* XXX This file needs to be in temp directory */
  1749. txtdes = mkstemp(tmptxtfile);
  1750. if (txtdes < 0) {
  1751. ast_log(LOG_ERROR, "Unable to create message file %s: %s\n", tmptxtfile, strerror(errno));
  1752. res = ast_streamfile(chan, "vm-mailboxfull", ast_channel_language(chan));
  1753. if (!res)
  1754. res = ast_waitstream(chan, "");
  1755. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED");
  1756. return res;
  1757. }
  1758. if (res >= 0) {
  1759. /* Unless we're *really* silent, try to send the beep */
  1760. res = ast_streamfile(chan, "beep", ast_channel_language(chan));
  1761. if (!res)
  1762. res = ast_waitstream(chan, "");
  1763. }
  1764. /* OEJ XXX Maybe this can be turned into a log file? Hmm. */
  1765. /* Store information */
  1766. ast_debug(2, "Open file for metadata: %s\n", tmptxtfile);
  1767. res = play_record_review(chan, NULL, tmptxtfile, global_vmmaxmessage, fmt, 1, vmu, &duration, &sound_duration, NULL, options->record_gain);
  1768. txt = fdopen(txtdes, "w+");
  1769. if (!txt) {
  1770. ast_log(LOG_WARNING, "Error opening text file for output\n");
  1771. } else {
  1772. struct ast_tm tm;
  1773. struct timeval now = ast_tvnow();
  1774. char timebuf[30];
  1775. char logbuf[BUFSIZ];
  1776. get_date(date, sizeof(date));
  1777. ast_localtime(&now, &tm, NULL);
  1778. ast_strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm);
  1779. ast_callerid_merge(callerid, sizeof(callerid),
  1780. S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
  1781. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
  1782. "Unknown");
  1783. snprintf(logbuf, sizeof(logbuf),
  1784. /* "Mailbox:domain:macrocontext:exten:priority:callerchan:callerid:origdate:origtime:duration:durationstatus:accountcode" */
  1785. "%s:%s:%s:%s:%d:%s:%s:%s:%s:%d:%s:%s\n",
  1786. username,
  1787. ast_channel_context(chan),
  1788. ast_channel_macrocontext(chan),
  1789. ast_channel_exten(chan),
  1790. ast_channel_priority(chan),
  1791. ast_channel_name(chan),
  1792. callerid,
  1793. date,
  1794. timebuf,
  1795. duration,
  1796. duration < global_vmminmessage ? "IGNORED" : "OK",
  1797. vmu->accountcode
  1798. );
  1799. fprintf(txt, "%s", logbuf);
  1800. if (minivmlogfile) {
  1801. ast_mutex_lock(&minivmloglock);
  1802. fprintf(minivmlogfile, "%s", logbuf);
  1803. ast_mutex_unlock(&minivmloglock);
  1804. }
  1805. if (sound_duration < global_vmminmessage) {
  1806. ast_verb(3, "Recording was %d seconds long but needs to be at least %d - abandoning\n", sound_duration, global_vmminmessage);
  1807. fclose(txt);
  1808. ast_filedelete(tmptxtfile, NULL);
  1809. unlink(tmptxtfile);
  1810. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED");
  1811. return 0;
  1812. }
  1813. fclose(txt); /* Close log file */
  1814. if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) {
  1815. ast_debug(1, "The recorded media file is gone, so we should remove the .txt file too!\n");
  1816. unlink(tmptxtfile);
  1817. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED");
  1818. if(ast_test_flag(vmu, MVM_ALLOCED))
  1819. free_user(vmu);
  1820. return 0;
  1821. }
  1822. /* Set channel variables for the notify application */
  1823. pbx_builtin_setvar_helper(chan, "MVM_FILENAME", tmptxtfile);
  1824. snprintf(timebuf, sizeof(timebuf), "%d", duration);
  1825. pbx_builtin_setvar_helper(chan, "MVM_DURATION", timebuf);
  1826. pbx_builtin_setvar_helper(chan, "MVM_FORMAT", fmt);
  1827. }
  1828. global_stats.lastreceived = ast_tvnow();
  1829. global_stats.receivedmessages++;
  1830. #if 0
  1831. /* Go ahead and delete audio files from system, they're not needed any more */
  1832. if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) {
  1833. ast_filedelete(tmptxtfile, NULL);
  1834. /* Even not being used at the moment, it's better to convert ast_log to ast_debug anyway */
  1835. ast_debug(2, "-_-_- Deleted audio file after notification :: %s \n", tmptxtfile);
  1836. }
  1837. #endif
  1838. if (res > 0)
  1839. res = 0;
  1840. if(ast_test_flag(vmu, MVM_ALLOCED))
  1841. free_user(vmu);
  1842. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "SUCCESS");
  1843. return res;
  1844. }
  1845. /*!\internal
  1846. * \brief Queue a message waiting event */
  1847. static void queue_mwi_event(const char *channel_id, const char *mbx, const char *ctx, int urgent, int new, int old)
  1848. {
  1849. char *mailbox, *context;
  1850. mailbox = ast_strdupa(mbx);
  1851. context = ast_strdupa(ctx);
  1852. if (ast_strlen_zero(context)) {
  1853. context = "default";
  1854. }
  1855. ast_publish_mwi_state_channel(mailbox, context, new + urgent, old, channel_id);
  1856. }
  1857. /*!\internal
  1858. * \brief Send MWI using interal Asterisk event subsystem */
  1859. static int minivm_mwi_exec(struct ast_channel *chan, const char *data)
  1860. {
  1861. int argc;
  1862. char *argv[4];
  1863. int res = 0;
  1864. char *tmpptr;
  1865. char tmp[PATH_MAX];
  1866. char *mailbox;
  1867. char *domain;
  1868. if (ast_strlen_zero(data)) {
  1869. ast_log(LOG_ERROR, "Minivm needs at least an account argument \n");
  1870. return -1;
  1871. }
  1872. tmpptr = ast_strdupa((char *)data);
  1873. argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
  1874. if (argc < 4) {
  1875. ast_log(LOG_ERROR, "%d arguments passed to MiniVM_MWI, need 4.\n", argc);
  1876. return -1;
  1877. }
  1878. ast_copy_string(tmp, argv[0], sizeof(tmp));
  1879. mailbox = tmp;
  1880. domain = strchr(tmp, '@');
  1881. if (domain) {
  1882. *domain = '\0';
  1883. domain++;
  1884. }
  1885. if (ast_strlen_zero(domain) || ast_strlen_zero(mailbox)) {
  1886. ast_log(LOG_ERROR, "Need mailbox@context as argument. Sorry. Argument 0 %s\n", argv[0]);
  1887. return -1;
  1888. }
  1889. queue_mwi_event(ast_channel_uniqueid(chan), mailbox, domain, atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
  1890. return res;
  1891. }
  1892. /*!\internal
  1893. * \brief Notify voicemail account owners - either generic template or user specific */
  1894. static int minivm_notify_exec(struct ast_channel *chan, const char *data)
  1895. {
  1896. int argc;
  1897. char *argv[2];
  1898. int res = 0;
  1899. char tmp[PATH_MAX];
  1900. char *domain;
  1901. char *tmpptr;
  1902. struct minivm_account *vmu;
  1903. char *username;
  1904. const char *template = "";
  1905. const char *filename;
  1906. const char *format;
  1907. const char *duration_string;
  1908. if (ast_strlen_zero(data)) {
  1909. ast_log(LOG_ERROR, "Minivm needs at least an account argument \n");
  1910. return -1;
  1911. }
  1912. tmpptr = ast_strdupa((char *)data);
  1913. argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
  1914. if (argc == 2 && !ast_strlen_zero(argv[1]))
  1915. template = argv[1];
  1916. ast_copy_string(tmp, argv[0], sizeof(tmp));
  1917. username = tmp;
  1918. domain = strchr(tmp, '@');
  1919. if (domain) {
  1920. *domain = '\0';
  1921. domain++;
  1922. }
  1923. if (ast_strlen_zero(domain) || ast_strlen_zero(username)) {
  1924. ast_log(LOG_ERROR, "Need username@domain as argument. Sorry. Argument 0 %s\n", argv[0]);
  1925. return -1;
  1926. }
  1927. if(!(vmu = find_account(domain, username, TRUE))) {
  1928. /* We could not find user, let's exit */
  1929. ast_log(LOG_WARNING, "Could not allocate temporary memory for '%s@%s'\n", username, domain);
  1930. pbx_builtin_setvar_helper(chan, "MVM_NOTIFY_STATUS", "FAILED");
  1931. return -1;
  1932. }
  1933. ast_channel_lock(chan);
  1934. if ((filename = pbx_builtin_getvar_helper(chan, "MVM_FILENAME"))) {
  1935. filename = ast_strdupa(filename);
  1936. }
  1937. ast_channel_unlock(chan);
  1938. /* Notify of new message to e-mail and pager */
  1939. if (!ast_strlen_zero(filename)) {
  1940. ast_channel_lock(chan);
  1941. if ((format = pbx_builtin_getvar_helper(chan, "MVM_FORMAT"))) {
  1942. format = ast_strdupa(format);
  1943. }
  1944. if ((duration_string = pbx_builtin_getvar_helper(chan, "MVM_DURATION"))) {
  1945. duration_string = ast_strdupa(duration_string);
  1946. }
  1947. ast_channel_unlock(chan);
  1948. res = notify_new_message(chan, template, vmu, filename, atoi(duration_string),
  1949. format,
  1950. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
  1951. S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL));
  1952. }
  1953. pbx_builtin_setvar_helper(chan, "MVM_NOTIFY_STATUS", res == 0 ? "SUCCESS" : "FAILED");
  1954. if(ast_test_flag(vmu, MVM_ALLOCED))
  1955. free_user(vmu);
  1956. /* Ok, we're ready to rock and roll. Return to dialplan */
  1957. return res;
  1958. }
  1959. /*!\internal
  1960. * \brief Dialplan function to record voicemail */
  1961. static int minivm_record_exec(struct ast_channel *chan, const char *data)
  1962. {
  1963. int res = 0;
  1964. char *tmp;
  1965. struct leave_vm_options leave_options;
  1966. int argc;
  1967. char *argv[2];
  1968. struct ast_flags flags = { 0 };
  1969. char *opts[OPT_ARG_ARRAY_SIZE];
  1970. memset(&leave_options, 0, sizeof(leave_options));
  1971. /* Answer channel if it's not already answered */
  1972. if (ast_channel_state(chan) != AST_STATE_UP)
  1973. ast_answer(chan);
  1974. if (ast_strlen_zero(data)) {
  1975. ast_log(LOG_ERROR, "Minivm needs at least an account argument \n");
  1976. return -1;
  1977. }
  1978. tmp = ast_strdupa((char *)data);
  1979. argc = ast_app_separate_args(tmp, ',', argv, ARRAY_LEN(argv));
  1980. if (argc == 2) {
  1981. if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1])) {
  1982. return -1;
  1983. }
  1984. ast_copy_flags(&leave_options, &flags, OPT_SILENT | OPT_BUSY_GREETING | OPT_UNAVAIL_GREETING );
  1985. if (ast_test_flag(&flags, OPT_RECORDGAIN)) {
  1986. int gain;
  1987. if (sscanf(opts[OPT_ARG_RECORDGAIN], "%30d", &gain) != 1) {
  1988. ast_log(LOG_WARNING, "Invalid value '%s' provided for record gain option\n", opts[OPT_ARG_RECORDGAIN]);
  1989. return -1;
  1990. } else
  1991. leave_options.record_gain = (signed char) gain;
  1992. }
  1993. }
  1994. /* Now run the appliation and good luck to you! */
  1995. res = leave_voicemail(chan, argv[0], &leave_options);
  1996. if (res == ERROR_LOCK_PATH) {
  1997. ast_log(LOG_ERROR, "Could not leave voicemail. The path is already locked.\n");
  1998. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED");
  1999. res = 0;
  2000. }
  2001. pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "SUCCESS");
  2002. return res;
  2003. }
  2004. /*!\internal
  2005. * \brief Play voicemail prompts - either generic or user specific */
  2006. static int minivm_greet_exec(struct ast_channel *chan, const char *data)
  2007. {
  2008. struct leave_vm_options leave_options = { 0, '\0'};
  2009. int argc;
  2010. char *argv[2];
  2011. struct ast_flags flags = { 0 };
  2012. char *opts[OPT_ARG_ARRAY_SIZE];
  2013. int res = 0;
  2014. int ausemacro = 0;
  2015. int ousemacro = 0;
  2016. int ouseexten = 0;
  2017. char tmp[PATH_MAX];
  2018. char dest[PATH_MAX];
  2019. char prefile[PATH_MAX] = "";
  2020. char tempfile[PATH_MAX] = "";
  2021. char ext_context[256] = "";
  2022. char *domain;
  2023. char ecodes[16] = "#";
  2024. char *tmpptr;
  2025. struct minivm_account *vmu;
  2026. char *username;
  2027. if (ast_strlen_zero(data)) {
  2028. ast_log(LOG_ERROR, "Minivm needs at least an account argument \n");
  2029. return -1;
  2030. }
  2031. tmpptr = ast_strdupa((char *)data);
  2032. argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
  2033. if (argc == 2) {
  2034. if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1]))
  2035. return -1;
  2036. ast_copy_flags(&leave_options, &flags, OPT_SILENT | OPT_BUSY_GREETING | OPT_UNAVAIL_GREETING );
  2037. }
  2038. ast_copy_string(tmp, argv[0], sizeof(tmp));
  2039. username = tmp;
  2040. domain = strchr(tmp, '@');
  2041. if (domain) {
  2042. *domain = '\0';
  2043. domain++;
  2044. }
  2045. if (ast_strlen_zero(domain) || ast_strlen_zero(username)) {
  2046. ast_log(LOG_ERROR, "Need username@domain as argument. Sorry. Argument: %s\n", argv[0]);
  2047. return -1;
  2048. }
  2049. ast_debug(1, "Trying to find configuration for user %s in domain %s\n", username, domain);
  2050. if (!(vmu = find_account(domain, username, TRUE))) {
  2051. ast_log(LOG_ERROR, "Could not allocate memory. \n");
  2052. return -1;
  2053. }
  2054. /* Answer channel if it's not already answered */
  2055. if (ast_channel_state(chan) != AST_STATE_UP)
  2056. ast_answer(chan);
  2057. /* Setup pre-file if appropriate */
  2058. if (strcmp(vmu->domain, "localhost"))
  2059. snprintf(ext_context, sizeof(ext_context), "%s@%s", username, vmu->domain);
  2060. else
  2061. ast_copy_string(ext_context, vmu->domain, sizeof(ext_context));
  2062. if (ast_test_flag(&leave_options, OPT_BUSY_GREETING)) {
  2063. res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "busy");
  2064. if (res)
  2065. snprintf(prefile, sizeof(prefile), "%s%s/%s/busy", MVM_SPOOL_DIR, vmu->domain, username);
  2066. } else if (ast_test_flag(&leave_options, OPT_UNAVAIL_GREETING)) {
  2067. res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "unavail");
  2068. if (res)
  2069. snprintf(prefile, sizeof(prefile), "%s%s/%s/unavail", MVM_SPOOL_DIR, vmu->domain, username);
  2070. }
  2071. /* Check for temporary greeting - it overrides busy and unavail */
  2072. snprintf(tempfile, sizeof(tempfile), "%s%s/%s/temp", MVM_SPOOL_DIR, vmu->domain, username);
  2073. if (!(res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "temp"))) {
  2074. ast_debug(2, "Temporary message directory does not exist, using default (%s)\n", tempfile);
  2075. ast_copy_string(prefile, tempfile, sizeof(prefile));
  2076. }
  2077. ast_debug(2, "Preparing to play message ...\n");
  2078. /* Check current or macro-calling context for special extensions */
  2079. if (ast_test_flag(vmu, MVM_OPERATOR)) {
  2080. if (!ast_strlen_zero(vmu->exit)) {
  2081. if (ast_exists_extension(chan, vmu->exit, "o", 1,
  2082. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  2083. strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
  2084. ouseexten = 1;
  2085. }
  2086. } else if (ast_exists_extension(chan, ast_channel_context(chan), "o", 1,
  2087. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  2088. strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
  2089. ouseexten = 1;
  2090. }
  2091. else if (!ast_strlen_zero(ast_channel_macrocontext(chan))
  2092. && ast_exists_extension(chan, ast_channel_macrocontext(chan), "o", 1,
  2093. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  2094. strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
  2095. ousemacro = 1;
  2096. }
  2097. }
  2098. if (!ast_strlen_zero(vmu->exit)) {
  2099. if (ast_exists_extension(chan, vmu->exit, "a", 1,
  2100. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  2101. strncat(ecodes, "*", sizeof(ecodes) - strlen(ecodes) - 1);
  2102. }
  2103. } else if (ast_exists_extension(chan, ast_channel_context(chan), "a", 1,
  2104. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  2105. strncat(ecodes, "*", sizeof(ecodes) - strlen(ecodes) - 1);
  2106. } else if (!ast_strlen_zero(ast_channel_macrocontext(chan))
  2107. && ast_exists_extension(chan, ast_channel_macrocontext(chan), "a", 1,
  2108. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  2109. strncat(ecodes, "*", sizeof(ecodes) - strlen(ecodes) - 1);
  2110. ausemacro = 1;
  2111. }
  2112. res = 0; /* Reset */
  2113. /* Play the beginning intro if desired */
  2114. if (!ast_strlen_zero(prefile)) {
  2115. if (ast_streamfile(chan, prefile, ast_channel_language(chan)) > -1)
  2116. res = ast_waitstream(chan, ecodes);
  2117. } else {
  2118. ast_debug(2, "%s doesn't exist, doing what we can\n", prefile);
  2119. res = invent_message(chan, vmu->domain, username, ast_test_flag(&leave_options, OPT_BUSY_GREETING), ecodes);
  2120. }
  2121. if (res < 0) {
  2122. ast_debug(2, "Hang up during prefile playback\n");
  2123. pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "FAILED");
  2124. if(ast_test_flag(vmu, MVM_ALLOCED))
  2125. free_user(vmu);
  2126. return -1;
  2127. }
  2128. if (res == '#') {
  2129. /* On a '#' we skip the instructions */
  2130. ast_set_flag(&leave_options, OPT_SILENT);
  2131. res = 0;
  2132. }
  2133. if (!res && !ast_test_flag(&leave_options, OPT_SILENT)) {
  2134. res = ast_streamfile(chan, SOUND_INTRO, ast_channel_language(chan));
  2135. if (!res)
  2136. res = ast_waitstream(chan, ecodes);
  2137. if (res == '#') {
  2138. ast_set_flag(&leave_options, OPT_SILENT);
  2139. res = 0;
  2140. }
  2141. }
  2142. if (res > 0)
  2143. ast_stopstream(chan);
  2144. /* Check for a '*' here in case the caller wants to escape from voicemail to something
  2145. other than the operator -- an automated attendant or mailbox login for example */
  2146. if (res == '*') {
  2147. ast_channel_exten_set(chan, "a");
  2148. if (!ast_strlen_zero(vmu->exit)) {
  2149. ast_channel_context_set(chan, vmu->exit);
  2150. } else if (ausemacro && !ast_strlen_zero(ast_channel_macrocontext(chan))) {
  2151. ast_channel_context_set(chan, ast_channel_macrocontext(chan));
  2152. }
  2153. ast_channel_priority_set(chan, 0);
  2154. pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "USEREXIT");
  2155. res = 0;
  2156. } else if (res == '0') { /* Check for a '0' here */
  2157. if(ouseexten || ousemacro) {
  2158. ast_channel_exten_set(chan, "o");
  2159. if (!ast_strlen_zero(vmu->exit)) {
  2160. ast_channel_context_set(chan, vmu->exit);
  2161. } else if (ousemacro && !ast_strlen_zero(ast_channel_macrocontext(chan))) {
  2162. ast_channel_context_set(chan, ast_channel_macrocontext(chan));
  2163. }
  2164. ast_play_and_wait(chan, "transfer");
  2165. ast_channel_priority_set(chan, 0);
  2166. pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "USEREXIT");
  2167. }
  2168. res = 0;
  2169. } else if (res < 0) {
  2170. pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "FAILED");
  2171. res = -1;
  2172. } else
  2173. pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "SUCCESS");
  2174. if(ast_test_flag(vmu, MVM_ALLOCED))
  2175. free_user(vmu);
  2176. /* Ok, we're ready to rock and roll. Return to dialplan */
  2177. return res;
  2178. }
  2179. /*!\internal
  2180. * \brief Dialplan application to delete voicemail */
  2181. static int minivm_delete_exec(struct ast_channel *chan, const char *data)
  2182. {
  2183. int res = 0;
  2184. char filename[BUFSIZ];
  2185. if (!ast_strlen_zero(data)) {
  2186. ast_copy_string(filename, (char *) data, sizeof(filename));
  2187. } else {
  2188. ast_channel_lock(chan);
  2189. ast_copy_string(filename, pbx_builtin_getvar_helper(chan, "MVM_FILENAME"), sizeof(filename));
  2190. ast_channel_unlock(chan);
  2191. }
  2192. if (ast_strlen_zero(filename)) {
  2193. ast_log(LOG_ERROR, "No filename given in application arguments or channel variable MVM_FILENAME\n");
  2194. return res;
  2195. }
  2196. /* Go ahead and delete audio files from system, they're not needed any more */
  2197. /* We should look for both audio and text files here */
  2198. if (ast_fileexists(filename, NULL, NULL) > 0) {
  2199. res = vm_delete(filename);
  2200. if (res) {
  2201. ast_debug(2, "Can't delete file: %s\n", filename);
  2202. pbx_builtin_setvar_helper(chan, "MVM_DELETE_STATUS", "FAILED");
  2203. } else {
  2204. ast_debug(2, "Deleted voicemail file :: %s \n", filename);
  2205. pbx_builtin_setvar_helper(chan, "MVM_DELETE_STATUS", "SUCCESS");
  2206. }
  2207. } else {
  2208. ast_debug(2, "Filename does not exist: %s\n", filename);
  2209. pbx_builtin_setvar_helper(chan, "MVM_DELETE_STATUS", "FAILED");
  2210. }
  2211. return res;
  2212. }
  2213. /*! \brief Record specific messages for voicemail account */
  2214. static int minivm_accmess_exec(struct ast_channel *chan, const char *data)
  2215. {
  2216. int argc = 0;
  2217. char *argv[2];
  2218. char filename[PATH_MAX];
  2219. char tmp[PATH_MAX];
  2220. char *domain;
  2221. char *tmpptr = NULL;
  2222. struct minivm_account *vmu;
  2223. char *username;
  2224. struct ast_flags flags = { 0 };
  2225. char *opts[OPT_ARG_ARRAY_SIZE];
  2226. int error = FALSE;
  2227. char *message = NULL;
  2228. char *prompt = NULL;
  2229. int duration;
  2230. if (ast_strlen_zero(data)) {
  2231. ast_log(LOG_ERROR, "MinivmAccmess needs at least two arguments: account and option\n");
  2232. error = TRUE;
  2233. } else {
  2234. tmpptr = ast_strdupa((char *)data);
  2235. argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
  2236. }
  2237. if (argc <=1) {
  2238. ast_log(LOG_ERROR, "MinivmAccmess needs at least two arguments: account and option\n");
  2239. error = TRUE;
  2240. }
  2241. if (!error && strlen(argv[1]) > 1) {
  2242. ast_log(LOG_ERROR, "MinivmAccmess can only handle one option at a time. Bad option string: %s\n", argv[1]);
  2243. error = TRUE;
  2244. }
  2245. if (!error && ast_app_parse_options(minivm_accmess_options, &flags, opts, argv[1])) {
  2246. ast_log(LOG_ERROR, "Can't parse option %s\n", argv[1]);
  2247. error = TRUE;
  2248. }
  2249. if (error) {
  2250. pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "FAILED");
  2251. return -1;
  2252. }
  2253. ast_copy_string(tmp, argv[0], sizeof(tmp));
  2254. username = tmp;
  2255. domain = strchr(tmp, '@');
  2256. if (domain) {
  2257. *domain = '\0';
  2258. domain++;
  2259. }
  2260. if (ast_strlen_zero(domain) || ast_strlen_zero(username)) {
  2261. ast_log(LOG_ERROR, "Need username@domain as argument. Sorry. Argument 0 %s\n", argv[0]);
  2262. pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "FAILED");
  2263. return -1;
  2264. }
  2265. if(!(vmu = find_account(domain, username, TRUE))) {
  2266. /* We could not find user, let's exit */
  2267. ast_log(LOG_WARNING, "Could not allocate temporary memory for '%s@%s'\n", username, domain);
  2268. pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "FAILED");
  2269. return -1;
  2270. }
  2271. /* Answer channel if it's not already answered */
  2272. if (ast_channel_state(chan) != AST_STATE_UP)
  2273. ast_answer(chan);
  2274. /* Here's where the action is */
  2275. if (ast_test_flag(&flags, OPT_BUSY_GREETING)) {
  2276. message = "busy";
  2277. prompt = "vm-rec-busy";
  2278. } else if (ast_test_flag(&flags, OPT_UNAVAIL_GREETING)) {
  2279. message = "unavailable";
  2280. prompt = "vm-rec-unv";
  2281. } else if (ast_test_flag(&flags, OPT_TEMP_GREETING)) {
  2282. message = "temp";
  2283. prompt = "vm-rec-temp";
  2284. } else if (ast_test_flag(&flags, OPT_NAME_GREETING)) {
  2285. message = "greet";
  2286. prompt = "vm-rec-name";
  2287. }
  2288. snprintf(filename,sizeof(filename), "%s%s/%s/%s", MVM_SPOOL_DIR, vmu->domain, vmu->username, message);
  2289. /* Maybe we should check the result of play_record_review ? */
  2290. play_record_review(chan, prompt, filename, global_maxgreet, default_vmformat, 0, vmu, &duration, NULL, NULL, FALSE);
  2291. ast_debug(1, "Recorded new %s message in %s (duration %d)\n", message, filename, duration);
  2292. if(ast_test_flag(vmu, MVM_ALLOCED))
  2293. free_user(vmu);
  2294. pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "SUCCESS");
  2295. /* Ok, we're ready to rock and roll. Return to dialplan */
  2296. return 0;
  2297. }
  2298. /*! \brief Append new mailbox to mailbox list from configuration file */
  2299. static int create_vmaccount(char *name, struct ast_variable *var, int realtime)
  2300. {
  2301. struct minivm_account *vmu;
  2302. char *domain;
  2303. char *username;
  2304. char accbuf[BUFSIZ];
  2305. ast_debug(3, "Creating %s account for [%s]\n", realtime ? "realtime" : "static", name);
  2306. ast_copy_string(accbuf, name, sizeof(accbuf));
  2307. username = accbuf;
  2308. domain = strchr(accbuf, '@');
  2309. if (domain) {
  2310. *domain = '\0';
  2311. domain++;
  2312. }
  2313. if (ast_strlen_zero(domain)) {
  2314. ast_log(LOG_ERROR, "No domain given for mini-voicemail account %s. Not configured.\n", name);
  2315. return 0;
  2316. }
  2317. ast_debug(3, "Creating static account for user %s domain %s\n", username, domain);
  2318. /* Allocate user account */
  2319. vmu = ast_calloc(1, sizeof(*vmu));
  2320. if (!vmu)
  2321. return 0;
  2322. ast_copy_string(vmu->domain, domain, sizeof(vmu->domain));
  2323. ast_copy_string(vmu->username, username, sizeof(vmu->username));
  2324. populate_defaults(vmu);
  2325. ast_debug(3, "...Configuring account %s\n", name);
  2326. while (var) {
  2327. ast_debug(3, "Configuring %s = \"%s\" for account %s\n", var->name, var->value, name);
  2328. if (!strcasecmp(var->name, "serveremail")) {
  2329. ast_copy_string(vmu->serveremail, var->value, sizeof(vmu->serveremail));
  2330. } else if (!strcasecmp(var->name, "email")) {
  2331. ast_copy_string(vmu->email, var->value, sizeof(vmu->email));
  2332. } else if (!strcasecmp(var->name, "accountcode")) {
  2333. ast_copy_string(vmu->accountcode, var->value, sizeof(vmu->accountcode));
  2334. } else if (!strcasecmp(var->name, "pincode")) {
  2335. ast_copy_string(vmu->pincode, var->value, sizeof(vmu->pincode));
  2336. } else if (!strcasecmp(var->name, "domain")) {
  2337. ast_copy_string(vmu->domain, var->value, sizeof(vmu->domain));
  2338. } else if (!strcasecmp(var->name, "language")) {
  2339. ast_copy_string(vmu->language, var->value, sizeof(vmu->language));
  2340. } else if (!strcasecmp(var->name, "timezone")) {
  2341. ast_copy_string(vmu->zonetag, var->value, sizeof(vmu->zonetag));
  2342. } else if (!strcasecmp(var->name, "externnotify")) {
  2343. ast_copy_string(vmu->externnotify, var->value, sizeof(vmu->externnotify));
  2344. } else if (!strcasecmp(var->name, "etemplate")) {
  2345. ast_copy_string(vmu->etemplate, var->value, sizeof(vmu->etemplate));
  2346. } else if (!strcasecmp(var->name, "ptemplate")) {
  2347. ast_copy_string(vmu->ptemplate, var->value, sizeof(vmu->ptemplate));
  2348. } else if (!strcasecmp(var->name, "fullname")) {
  2349. ast_copy_string(vmu->fullname, var->value, sizeof(vmu->fullname));
  2350. } else if (!strcasecmp(var->name, "setvar")) {
  2351. char *varval;
  2352. char *varname = ast_strdupa(var->value);
  2353. struct ast_variable *tmpvar;
  2354. if ((varval = strchr(varname, '='))) {
  2355. *varval = '\0';
  2356. varval++;
  2357. if ((tmpvar = ast_variable_new(varname, varval, ""))) {
  2358. tmpvar->next = vmu->chanvars;
  2359. vmu->chanvars = tmpvar;
  2360. }
  2361. }
  2362. } else if (!strcasecmp(var->name, "pager")) {
  2363. ast_copy_string(vmu->pager, var->value, sizeof(vmu->pager));
  2364. } else if (!strcasecmp(var->name, "volgain")) {
  2365. sscanf(var->value, "%30lf", &vmu->volgain);
  2366. } else {
  2367. ast_log(LOG_ERROR, "Unknown configuration option for minivm account %s : %s\n", name, var->name);
  2368. }
  2369. var = var->next;
  2370. }
  2371. ast_debug(3, "...Linking account %s\n", name);
  2372. AST_LIST_LOCK(&minivm_accounts);
  2373. AST_LIST_INSERT_TAIL(&minivm_accounts, vmu, list);
  2374. AST_LIST_UNLOCK(&minivm_accounts);
  2375. global_stats.voicemailaccounts++;
  2376. ast_debug(2, "MVM :: Created account %s@%s - tz %s etemplate %s %s\n", username, domain, ast_strlen_zero(vmu->zonetag) ? "" : vmu->zonetag, ast_strlen_zero(vmu->etemplate) ? "" : vmu->etemplate, realtime ? "(realtime)" : "");
  2377. return 0;
  2378. }
  2379. /*! \brief Free Mini Voicemail timezone */
  2380. static void free_zone(struct minivm_zone *z)
  2381. {
  2382. ast_free(z);
  2383. }
  2384. /*! \brief Clear list of timezones */
  2385. static void timezone_destroy_list(void)
  2386. {
  2387. struct minivm_zone *this;
  2388. AST_LIST_LOCK(&minivm_zones);
  2389. while ((this = AST_LIST_REMOVE_HEAD(&minivm_zones, list)))
  2390. free_zone(this);
  2391. AST_LIST_UNLOCK(&minivm_zones);
  2392. }
  2393. /*! \brief Add time zone to memory list */
  2394. static int timezone_add(const char *zonename, const char *config)
  2395. {
  2396. struct minivm_zone *newzone;
  2397. char *msg_format, *timezone_str;
  2398. newzone = ast_calloc(1, sizeof(*newzone));
  2399. if (newzone == NULL)
  2400. return 0;
  2401. msg_format = ast_strdupa(config);
  2402. timezone_str = strsep(&msg_format, "|");
  2403. if (!msg_format) {
  2404. ast_log(LOG_WARNING, "Invalid timezone definition : %s\n", zonename);
  2405. ast_free(newzone);
  2406. return 0;
  2407. }
  2408. ast_copy_string(newzone->name, zonename, sizeof(newzone->name));
  2409. ast_copy_string(newzone->timezone, timezone_str, sizeof(newzone->timezone));
  2410. ast_copy_string(newzone->msg_format, msg_format, sizeof(newzone->msg_format));
  2411. AST_LIST_LOCK(&minivm_zones);
  2412. AST_LIST_INSERT_TAIL(&minivm_zones, newzone, list);
  2413. AST_LIST_UNLOCK(&minivm_zones);
  2414. global_stats.timezones++;
  2415. return 0;
  2416. }
  2417. /*! \brief Read message template from file */
  2418. static char *message_template_parse_filebody(const char *filename) {
  2419. char buf[BUFSIZ * 6];
  2420. char readbuf[BUFSIZ];
  2421. char filenamebuf[BUFSIZ];
  2422. char *writepos;
  2423. char *messagebody;
  2424. FILE *fi;
  2425. int lines = 0;
  2426. if (ast_strlen_zero(filename))
  2427. return NULL;
  2428. if (*filename == '/')
  2429. ast_copy_string(filenamebuf, filename, sizeof(filenamebuf));
  2430. else
  2431. snprintf(filenamebuf, sizeof(filenamebuf), "%s/%s", ast_config_AST_CONFIG_DIR, filename);
  2432. if (!(fi = fopen(filenamebuf, "r"))) {
  2433. ast_log(LOG_ERROR, "Can't read message template from file: %s\n", filenamebuf);
  2434. return NULL;
  2435. }
  2436. writepos = buf;
  2437. while (fgets(readbuf, sizeof(readbuf), fi)) {
  2438. lines ++;
  2439. if (writepos != buf) {
  2440. *writepos = '\n'; /* Replace EOL with new line */
  2441. writepos++;
  2442. }
  2443. ast_copy_string(writepos, readbuf, sizeof(buf) - (writepos - buf));
  2444. writepos += strlen(readbuf) - 1;
  2445. }
  2446. fclose(fi);
  2447. messagebody = ast_calloc(1, strlen(buf + 1));
  2448. ast_copy_string(messagebody, buf, strlen(buf) + 1);
  2449. ast_debug(4, "---> Size of allocation %d\n", (int) strlen(buf + 1) );
  2450. ast_debug(4, "---> Done reading message template : \n%s\n---- END message template--- \n", messagebody);
  2451. return messagebody;
  2452. }
  2453. /*! \brief Parse emailbody template from configuration file */
  2454. static char *message_template_parse_emailbody(const char *configuration)
  2455. {
  2456. char *tmpread, *tmpwrite;
  2457. char *emailbody = ast_strdup(configuration);
  2458. /* substitute strings \t and \n into the apropriate characters */
  2459. tmpread = tmpwrite = emailbody;
  2460. while ((tmpwrite = strchr(tmpread,'\\'))) {
  2461. int len = strlen("\n");
  2462. switch (tmpwrite[1]) {
  2463. case 'n':
  2464. memmove(tmpwrite + len, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
  2465. tmpwrite[0] = '\n';
  2466. break;
  2467. case 't':
  2468. memmove(tmpwrite + len, tmpwrite + 2, strlen(tmpwrite + 2) + 1);
  2469. tmpwrite[0] = '\t';
  2470. break;
  2471. default:
  2472. ast_log(LOG_NOTICE, "Substitution routine does not support this character: %c\n", tmpwrite[1]);
  2473. }
  2474. tmpread = tmpwrite + len;
  2475. }
  2476. return emailbody;
  2477. }
  2478. /*! \brief Apply general configuration options */
  2479. static int apply_general_options(struct ast_variable *var)
  2480. {
  2481. int error = 0;
  2482. while (var) {
  2483. /* Mail command */
  2484. if (!strcmp(var->name, "mailcmd")) {
  2485. ast_copy_string(global_mailcmd, var->value, sizeof(global_mailcmd)); /* User setting */
  2486. } else if (!strcmp(var->name, "maxgreet")) {
  2487. global_maxgreet = atoi(var->value);
  2488. } else if (!strcmp(var->name, "maxsilence")) {
  2489. global_maxsilence = atoi(var->value);
  2490. if (global_maxsilence > 0)
  2491. global_maxsilence *= 1000;
  2492. } else if (!strcmp(var->name, "logfile")) {
  2493. if (!ast_strlen_zero(var->value) ) {
  2494. if(*(var->value) == '/')
  2495. ast_copy_string(global_logfile, var->value, sizeof(global_logfile));
  2496. else
  2497. snprintf(global_logfile, sizeof(global_logfile), "%s/%s", ast_config_AST_LOG_DIR, var->value);
  2498. }
  2499. } else if (!strcmp(var->name, "externnotify")) {
  2500. /* External voicemail notify application */
  2501. ast_copy_string(global_externnotify, var->value, sizeof(global_externnotify));
  2502. } else if (!strcmp(var->name, "silencetreshold")) {
  2503. /* Silence treshold */
  2504. global_silencethreshold = atoi(var->value);
  2505. } else if (!strcmp(var->name, "maxmessage")) {
  2506. int x;
  2507. if (sscanf(var->value, "%30d", &x) == 1) {
  2508. global_vmmaxmessage = x;
  2509. } else {
  2510. error ++;
  2511. ast_log(LOG_WARNING, "Invalid max message time length\n");
  2512. }
  2513. } else if (!strcmp(var->name, "minmessage")) {
  2514. int x;
  2515. if (sscanf(var->value, "%30d", &x) == 1) {
  2516. global_vmminmessage = x;
  2517. if (global_maxsilence <= global_vmminmessage)
  2518. ast_log(LOG_WARNING, "maxsilence should be less than minmessage or you may get empty messages\n");
  2519. } else {
  2520. error ++;
  2521. ast_log(LOG_WARNING, "Invalid min message time length\n");
  2522. }
  2523. } else if (!strcmp(var->name, "format")) {
  2524. ast_copy_string(default_vmformat, var->value, sizeof(default_vmformat));
  2525. } else if (!strcmp(var->name, "review")) {
  2526. ast_set2_flag((&globalflags), ast_true(var->value), MVM_REVIEW);
  2527. } else if (!strcmp(var->name, "operator")) {
  2528. ast_set2_flag((&globalflags), ast_true(var->value), MVM_OPERATOR);
  2529. }
  2530. var = var->next;
  2531. }
  2532. return error;
  2533. }
  2534. /*! \brief Load minivoicemail configuration */
  2535. static int load_config(int reload)
  2536. {
  2537. struct ast_config *cfg;
  2538. struct ast_variable *var;
  2539. char *cat;
  2540. const char *chanvar;
  2541. int error = 0;
  2542. struct minivm_template *template;
  2543. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  2544. cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags);
  2545. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  2546. return 0;
  2547. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  2548. ast_log(LOG_ERROR, "Config file " VOICEMAIL_CONFIG " is in an invalid format. Aborting.\n");
  2549. return 0;
  2550. }
  2551. ast_mutex_lock(&minivmlock);
  2552. /* Destroy lists to reconfigure */
  2553. message_destroy_list(); /* Destroy list of voicemail message templates */
  2554. timezone_destroy_list(); /* Destroy list of timezones */
  2555. vmaccounts_destroy_list(); /* Destroy list of voicemail accounts */
  2556. ast_debug(2, "Destroyed memory objects...\n");
  2557. /* First, set some default settings */
  2558. global_externnotify[0] = '\0';
  2559. global_logfile[0] = '\0';
  2560. global_vmmaxmessage = 2000;
  2561. global_maxgreet = 2000;
  2562. global_vmminmessage = 0;
  2563. strcpy(global_mailcmd, SENDMAIL);
  2564. global_maxsilence = 0;
  2565. global_saydurationminfo = 2;
  2566. ast_copy_string(default_vmformat, "wav", sizeof(default_vmformat));
  2567. ast_set2_flag((&globalflags), FALSE, MVM_REVIEW);
  2568. ast_set2_flag((&globalflags), FALSE, MVM_OPERATOR);
  2569. /* Reset statistics */
  2570. memset(&global_stats, 0, sizeof(global_stats));
  2571. global_stats.reset = ast_tvnow();
  2572. global_silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
  2573. /* Make sure we could load configuration file */
  2574. if (!cfg) {
  2575. ast_log(LOG_WARNING, "Failed to load configuration file. Module activated with default settings.\n");
  2576. ast_mutex_unlock(&minivmlock);
  2577. return 0;
  2578. }
  2579. ast_debug(2, "Loaded configuration file, now parsing\n");
  2580. /* General settings */
  2581. cat = ast_category_browse(cfg, NULL);
  2582. while (cat) {
  2583. ast_debug(3, "Found configuration section [%s]\n", cat);
  2584. if (!strcasecmp(cat, "general")) {
  2585. /* Nothing right now */
  2586. error += apply_general_options(ast_variable_browse(cfg, cat));
  2587. } else if (!strncasecmp(cat, "template-", 9)) {
  2588. /* Template */
  2589. char *name = cat + 9;
  2590. /* Now build and link template to list */
  2591. error += message_template_build(name, ast_variable_browse(cfg, cat));
  2592. } else {
  2593. var = ast_variable_browse(cfg, cat);
  2594. if (!strcasecmp(cat, "zonemessages")) {
  2595. /* Timezones in this context */
  2596. while (var) {
  2597. timezone_add(var->name, var->value);
  2598. var = var->next;
  2599. }
  2600. } else {
  2601. /* Create mailbox from this */
  2602. error += create_vmaccount(cat, var, FALSE);
  2603. }
  2604. }
  2605. /* Find next section in configuration file */
  2606. cat = ast_category_browse(cfg, cat);
  2607. }
  2608. /* Configure the default email template */
  2609. message_template_build("email-default", NULL);
  2610. template = message_template_find("email-default");
  2611. /* Load date format config for voicemail mail */
  2612. if ((chanvar = ast_variable_retrieve(cfg, "general", "emaildateformat")))
  2613. ast_copy_string(template->dateformat, chanvar, sizeof(template->dateformat));
  2614. if ((chanvar = ast_variable_retrieve(cfg, "general", "emailfromstring")))
  2615. ast_copy_string(template->fromaddress, chanvar, sizeof(template->fromaddress));
  2616. if ((chanvar = ast_variable_retrieve(cfg, "general", "emailaaddress")))
  2617. ast_copy_string(template->serveremail, chanvar, sizeof(template->serveremail));
  2618. if ((chanvar = ast_variable_retrieve(cfg, "general", "emailcharset")))
  2619. ast_copy_string(template->charset, chanvar, sizeof(template->charset));
  2620. if ((chanvar = ast_variable_retrieve(cfg, "general", "emailsubject")))
  2621. ast_copy_string(template->subject, chanvar, sizeof(template->subject));
  2622. if ((chanvar = ast_variable_retrieve(cfg, "general", "emailbody")))
  2623. template->body = message_template_parse_emailbody(chanvar);
  2624. template->attachment = TRUE;
  2625. message_template_build("pager-default", NULL);
  2626. template = message_template_find("pager-default");
  2627. if ((chanvar = ast_variable_retrieve(cfg, "general", "pagerfromstring")))
  2628. ast_copy_string(template->fromaddress, chanvar, sizeof(template->fromaddress));
  2629. if ((chanvar = ast_variable_retrieve(cfg, "general", "pageraddress")))
  2630. ast_copy_string(template->serveremail, chanvar, sizeof(template->serveremail));
  2631. if ((chanvar = ast_variable_retrieve(cfg, "general", "pagercharset")))
  2632. ast_copy_string(template->charset, chanvar, sizeof(template->charset));
  2633. if ((chanvar = ast_variable_retrieve(cfg, "general", "pagersubject")))
  2634. ast_copy_string(template->subject, chanvar,sizeof(template->subject));
  2635. if ((chanvar = ast_variable_retrieve(cfg, "general", "pagerbody")))
  2636. template->body = message_template_parse_emailbody(chanvar);
  2637. template->attachment = FALSE;
  2638. if (error)
  2639. ast_log(LOG_ERROR, "--- A total of %d errors found in mini-voicemail configuration\n", error);
  2640. ast_mutex_unlock(&minivmlock);
  2641. ast_config_destroy(cfg);
  2642. /* Close log file if it's open and disabled */
  2643. if(minivmlogfile)
  2644. fclose(minivmlogfile);
  2645. /* Open log file if it's enabled */
  2646. if(!ast_strlen_zero(global_logfile)) {
  2647. minivmlogfile = fopen(global_logfile, "a");
  2648. if(!minivmlogfile)
  2649. ast_log(LOG_ERROR, "Failed to open minivm log file %s : %s\n", global_logfile, strerror(errno));
  2650. if (minivmlogfile)
  2651. ast_debug(3, "Opened log file %s \n", global_logfile);
  2652. }
  2653. return 0;
  2654. }
  2655. /*! \brief CLI routine for listing templates */
  2656. static char *handle_minivm_list_templates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2657. {
  2658. struct minivm_template *this;
  2659. #define HVLT_OUTPUT_FORMAT "%-15s %-10s %-10s %-15.15s %-50s\n"
  2660. int count = 0;
  2661. switch (cmd) {
  2662. case CLI_INIT:
  2663. e->command = "minivm list templates";
  2664. e->usage =
  2665. "Usage: minivm list templates\n"
  2666. " Lists message templates for e-mail, paging and IM\n";
  2667. return NULL;
  2668. case CLI_GENERATE:
  2669. return NULL;
  2670. }
  2671. if (a->argc > 3)
  2672. return CLI_SHOWUSAGE;
  2673. AST_LIST_LOCK(&message_templates);
  2674. if (AST_LIST_EMPTY(&message_templates)) {
  2675. ast_cli(a->fd, "There are no message templates defined\n");
  2676. AST_LIST_UNLOCK(&message_templates);
  2677. return CLI_FAILURE;
  2678. }
  2679. ast_cli(a->fd, HVLT_OUTPUT_FORMAT, "Template name", "Charset", "Locale", "Attach media", "Subject");
  2680. ast_cli(a->fd, HVLT_OUTPUT_FORMAT, "-------------", "-------", "------", "------------", "-------");
  2681. AST_LIST_TRAVERSE(&message_templates, this, list) {
  2682. ast_cli(a->fd, HVLT_OUTPUT_FORMAT, this->name,
  2683. S_OR(this->charset, "-"),
  2684. S_OR(this->locale, "-"),
  2685. this->attachment ? "Yes" : "No",
  2686. S_OR(this->subject, "-"));
  2687. count++;
  2688. }
  2689. AST_LIST_UNLOCK(&message_templates);
  2690. ast_cli(a->fd, "\n * Total: %d minivoicemail message templates\n", count);
  2691. return CLI_SUCCESS;
  2692. }
  2693. static char *complete_minivm_show_users(const char *line, const char *word, int pos, int state)
  2694. {
  2695. int which = 0;
  2696. int wordlen;
  2697. struct minivm_account *vmu;
  2698. const char *domain = "";
  2699. /* 0 - minivm; 1 - list; 2 - accounts; 3 - for; 4 - <domain> */
  2700. if (pos > 4)
  2701. return NULL;
  2702. wordlen = strlen(word);
  2703. AST_LIST_TRAVERSE(&minivm_accounts, vmu, list) {
  2704. if (!strncasecmp(word, vmu->domain, wordlen)) {
  2705. if (domain && strcmp(domain, vmu->domain) && ++which > state)
  2706. return ast_strdup(vmu->domain);
  2707. /* ignore repeated domains ? */
  2708. domain = vmu->domain;
  2709. }
  2710. }
  2711. return NULL;
  2712. }
  2713. /*! \brief CLI command to list voicemail accounts */
  2714. static char *handle_minivm_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2715. {
  2716. struct minivm_account *vmu;
  2717. #define HMSU_OUTPUT_FORMAT "%-23s %-15s %-15s %-10s %-10s %-50s\n"
  2718. int count = 0;
  2719. switch (cmd) {
  2720. case CLI_INIT:
  2721. e->command = "minivm list accounts [for]";
  2722. e->usage =
  2723. "Usage: minivm list accounts [for <domain>]\n"
  2724. " Lists all mailboxes currently set up\n";
  2725. return NULL;
  2726. case CLI_GENERATE:
  2727. return complete_minivm_show_users(a->line, a->word, a->pos, a->n);
  2728. }
  2729. if ((a->argc < 3) || (a->argc > 5) || (a->argc == 4))
  2730. return CLI_SHOWUSAGE;
  2731. if ((a->argc == 5) && strcmp(a->argv[3],"for"))
  2732. return CLI_SHOWUSAGE;
  2733. AST_LIST_LOCK(&minivm_accounts);
  2734. if (AST_LIST_EMPTY(&minivm_accounts)) {
  2735. ast_cli(a->fd, "There are no voicemail users currently defined\n");
  2736. AST_LIST_UNLOCK(&minivm_accounts);
  2737. return CLI_FAILURE;
  2738. }
  2739. ast_cli(a->fd, HMSU_OUTPUT_FORMAT, "User", "E-Template", "P-template", "Zone", "Format", "Full name");
  2740. ast_cli(a->fd, HMSU_OUTPUT_FORMAT, "----", "----------", "----------", "----", "------", "---------");
  2741. AST_LIST_TRAVERSE(&minivm_accounts, vmu, list) {
  2742. char tmp[256] = "";
  2743. if ((a->argc == 3) || ((a->argc == 5) && !strcmp(a->argv[4], vmu->domain))) {
  2744. count++;
  2745. snprintf(tmp, sizeof(tmp), "%s@%s", vmu->username, vmu->domain);
  2746. ast_cli(a->fd, HMSU_OUTPUT_FORMAT, tmp, S_OR(vmu->etemplate, "-"),
  2747. S_OR(vmu->ptemplate, "-"),
  2748. S_OR(vmu->zonetag, "-"),
  2749. S_OR(vmu->attachfmt, "-"),
  2750. vmu->fullname);
  2751. }
  2752. }
  2753. AST_LIST_UNLOCK(&minivm_accounts);
  2754. ast_cli(a->fd, "\n * Total: %d minivoicemail accounts\n", count);
  2755. return CLI_SUCCESS;
  2756. }
  2757. /*! \brief Show a list of voicemail zones in the CLI */
  2758. static char *handle_minivm_show_zones(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2759. {
  2760. struct minivm_zone *zone;
  2761. #define HMSZ_OUTPUT_FORMAT "%-15s %-20s %-45s\n"
  2762. char *res = CLI_SUCCESS;
  2763. switch (cmd) {
  2764. case CLI_INIT:
  2765. e->command = "minivm list zones";
  2766. e->usage =
  2767. "Usage: minivm list zones\n"
  2768. " Lists zone message formats\n";
  2769. return NULL;
  2770. case CLI_GENERATE:
  2771. return NULL;
  2772. }
  2773. if (a->argc != e->args)
  2774. return CLI_SHOWUSAGE;
  2775. AST_LIST_LOCK(&minivm_zones);
  2776. if (!AST_LIST_EMPTY(&minivm_zones)) {
  2777. ast_cli(a->fd, HMSZ_OUTPUT_FORMAT, "Zone", "Timezone", "Message Format");
  2778. ast_cli(a->fd, HMSZ_OUTPUT_FORMAT, "----", "--------", "--------------");
  2779. AST_LIST_TRAVERSE(&minivm_zones, zone, list) {
  2780. ast_cli(a->fd, HMSZ_OUTPUT_FORMAT, zone->name, zone->timezone, zone->msg_format);
  2781. }
  2782. } else {
  2783. ast_cli(a->fd, "There are no voicemail zones currently defined\n");
  2784. res = CLI_FAILURE;
  2785. }
  2786. AST_LIST_UNLOCK(&minivm_zones);
  2787. return res;
  2788. }
  2789. /*! \brief CLI Show settings */
  2790. static char *handle_minivm_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2791. {
  2792. switch (cmd) {
  2793. case CLI_INIT:
  2794. e->command = "minivm show settings";
  2795. e->usage =
  2796. "Usage: minivm show settings\n"
  2797. " Display Mini-Voicemail general settings\n";
  2798. return NULL;
  2799. case CLI_GENERATE:
  2800. return NULL;
  2801. }
  2802. ast_cli(a->fd, "* Mini-Voicemail general settings\n");
  2803. ast_cli(a->fd, " -------------------------------\n");
  2804. ast_cli(a->fd, "\n");
  2805. ast_cli(a->fd, " Mail command (shell): %s\n", global_mailcmd);
  2806. ast_cli(a->fd, " Max silence: %d\n", global_maxsilence);
  2807. ast_cli(a->fd, " Silence threshold: %d\n", global_silencethreshold);
  2808. ast_cli(a->fd, " Max message length (secs): %d\n", global_vmmaxmessage);
  2809. ast_cli(a->fd, " Min message length (secs): %d\n", global_vmminmessage);
  2810. ast_cli(a->fd, " Default format: %s\n", default_vmformat);
  2811. ast_cli(a->fd, " Extern notify (shell): %s\n", global_externnotify);
  2812. ast_cli(a->fd, " Logfile: %s\n", global_logfile[0] ? global_logfile : "<disabled>");
  2813. ast_cli(a->fd, " Operator exit: %s\n", ast_test_flag(&globalflags, MVM_OPERATOR) ? "Yes" : "No");
  2814. ast_cli(a->fd, " Message review: %s\n", ast_test_flag(&globalflags, MVM_REVIEW) ? "Yes" : "No");
  2815. ast_cli(a->fd, "\n");
  2816. return CLI_SUCCESS;
  2817. }
  2818. /*! \brief Show stats */
  2819. static char *handle_minivm_show_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2820. {
  2821. struct ast_tm timebuf;
  2822. char buf[BUFSIZ];
  2823. switch (cmd) {
  2824. case CLI_INIT:
  2825. e->command = "minivm show stats";
  2826. e->usage =
  2827. "Usage: minivm show stats\n"
  2828. " Display Mini-Voicemail counters\n";
  2829. return NULL;
  2830. case CLI_GENERATE:
  2831. return NULL;
  2832. }
  2833. ast_cli(a->fd, "* Mini-Voicemail statistics\n");
  2834. ast_cli(a->fd, " -------------------------\n");
  2835. ast_cli(a->fd, "\n");
  2836. ast_cli(a->fd, " Voicemail accounts: %5d\n", global_stats.voicemailaccounts);
  2837. ast_cli(a->fd, " Templates: %5d\n", global_stats.templates);
  2838. ast_cli(a->fd, " Timezones: %5d\n", global_stats.timezones);
  2839. if (global_stats.receivedmessages == 0) {
  2840. ast_cli(a->fd, " Received messages since last reset: <none>\n");
  2841. } else {
  2842. ast_cli(a->fd, " Received messages since last reset: %d\n", global_stats.receivedmessages);
  2843. ast_localtime(&global_stats.lastreceived, &timebuf, NULL);
  2844. ast_strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &timebuf);
  2845. ast_cli(a->fd, " Last received voicemail: %s\n", buf);
  2846. }
  2847. ast_localtime(&global_stats.reset, &timebuf, NULL);
  2848. ast_strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &timebuf);
  2849. ast_cli(a->fd, " Last reset: %s\n", buf);
  2850. ast_cli(a->fd, "\n");
  2851. return CLI_SUCCESS;
  2852. }
  2853. /*! \brief ${MINIVMACCOUNT()} Dialplan function - reads account data */
  2854. static int minivm_account_func_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  2855. {
  2856. struct minivm_account *vmu;
  2857. char *username, *domain, *colname;
  2858. username = ast_strdupa(data);
  2859. if ((colname = strchr(username, ':'))) {
  2860. *colname = '\0';
  2861. colname++;
  2862. } else {
  2863. colname = "path";
  2864. }
  2865. if ((domain = strchr(username, '@'))) {
  2866. *domain = '\0';
  2867. domain++;
  2868. }
  2869. if (ast_strlen_zero(username) || ast_strlen_zero(domain)) {
  2870. ast_log(LOG_ERROR, "This function needs a username and a domain: username@domain\n");
  2871. return 0;
  2872. }
  2873. if (!(vmu = find_account(domain, username, TRUE)))
  2874. return 0;
  2875. if (!strcasecmp(colname, "hasaccount")) {
  2876. ast_copy_string(buf, (ast_test_flag(vmu, MVM_ALLOCED) ? "0" : "1"), len);
  2877. } else if (!strcasecmp(colname, "fullname")) {
  2878. ast_copy_string(buf, vmu->fullname, len);
  2879. } else if (!strcasecmp(colname, "email")) {
  2880. if (!ast_strlen_zero(vmu->email))
  2881. ast_copy_string(buf, vmu->email, len);
  2882. else
  2883. snprintf(buf, len, "%s@%s", vmu->username, vmu->domain);
  2884. } else if (!strcasecmp(colname, "pager")) {
  2885. ast_copy_string(buf, vmu->pager, len);
  2886. } else if (!strcasecmp(colname, "etemplate")) {
  2887. if (!ast_strlen_zero(vmu->etemplate))
  2888. ast_copy_string(buf, vmu->etemplate, len);
  2889. else
  2890. ast_copy_string(buf, "email-default", len);
  2891. } else if (!strcasecmp(colname, "language")) {
  2892. ast_copy_string(buf, vmu->language, len);
  2893. } else if (!strcasecmp(colname, "timezone")) {
  2894. ast_copy_string(buf, vmu->zonetag, len);
  2895. } else if (!strcasecmp(colname, "ptemplate")) {
  2896. if (!ast_strlen_zero(vmu->ptemplate))
  2897. ast_copy_string(buf, vmu->ptemplate, len);
  2898. else
  2899. ast_copy_string(buf, "email-default", len);
  2900. } else if (!strcasecmp(colname, "accountcode")) {
  2901. ast_copy_string(buf, vmu->accountcode, len);
  2902. } else if (!strcasecmp(colname, "pincode")) {
  2903. ast_copy_string(buf, vmu->pincode, len);
  2904. } else if (!strcasecmp(colname, "path")) {
  2905. check_dirpath(buf, len, vmu->domain, vmu->username, NULL);
  2906. } else { /* Look in channel variables */
  2907. struct ast_variable *var;
  2908. for (var = vmu->chanvars ; var ; var = var->next)
  2909. if (!strcmp(var->name, colname)) {
  2910. ast_copy_string(buf, var->value, len);
  2911. break;
  2912. }
  2913. }
  2914. if(ast_test_flag(vmu, MVM_ALLOCED))
  2915. free_user(vmu);
  2916. return 0;
  2917. }
  2918. /*! \brief lock directory
  2919. only return failure if ast_lock_path returns 'timeout',
  2920. not if the path does not exist or any other reason
  2921. */
  2922. static int vm_lock_path(const char *path)
  2923. {
  2924. switch (ast_lock_path(path)) {
  2925. case AST_LOCK_TIMEOUT:
  2926. return -1;
  2927. default:
  2928. return 0;
  2929. }
  2930. }
  2931. /*! \brief Access counter file, lock directory, read and possibly write it again changed
  2932. \param directory Directory to crate file in
  2933. \param countername filename
  2934. \param value If set to zero, we only read the variable
  2935. \param operand 0 to read, 1 to set new value, 2 to change
  2936. \return -1 on error, otherwise counter value
  2937. */
  2938. static int access_counter_file(char *directory, char *countername, int value, int operand)
  2939. {
  2940. char filename[BUFSIZ];
  2941. char readbuf[BUFSIZ];
  2942. FILE *counterfile;
  2943. int old = 0, counter = 0;
  2944. /* Lock directory */
  2945. if (vm_lock_path(directory)) {
  2946. return -1; /* Could not lock directory */
  2947. }
  2948. snprintf(filename, sizeof(filename), "%s/%s.counter", directory, countername);
  2949. if (operand != 1) {
  2950. counterfile = fopen(filename, "r");
  2951. if (counterfile) {
  2952. if(fgets(readbuf, sizeof(readbuf), counterfile)) {
  2953. ast_debug(3, "Read this string from counter file: %s\n", readbuf);
  2954. old = counter = atoi(readbuf);
  2955. }
  2956. fclose(counterfile);
  2957. }
  2958. }
  2959. switch (operand) {
  2960. case 0: /* Read only */
  2961. ast_unlock_path(directory);
  2962. ast_debug(2, "MINIVM Counter %s/%s: Value %d\n", directory, countername, counter);
  2963. return counter;
  2964. break;
  2965. case 1: /* Set new value */
  2966. counter = value;
  2967. break;
  2968. case 2: /* Change value */
  2969. counter += value;
  2970. if (counter < 0) /* Don't allow counters to fall below zero */
  2971. counter = 0;
  2972. break;
  2973. }
  2974. /* Now, write the new value to the file */
  2975. counterfile = fopen(filename, "w");
  2976. if (!counterfile) {
  2977. ast_log(LOG_ERROR, "Could not open counter file for writing : %s - %s\n", filename, strerror(errno));
  2978. ast_unlock_path(directory);
  2979. return -1; /* Could not open file for writing */
  2980. }
  2981. fprintf(counterfile, "%d\n\n", counter);
  2982. fclose(counterfile);
  2983. ast_unlock_path(directory);
  2984. ast_debug(2, "MINIVM Counter %s/%s: Old value %d New value %d\n", directory, countername, old, counter);
  2985. return counter;
  2986. }
  2987. /*! \brief ${MINIVMCOUNTER()} Dialplan function - read counters */
  2988. static int minivm_counter_func_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  2989. {
  2990. char *username, *domain, *countername;
  2991. char userpath[BUFSIZ];
  2992. int res;
  2993. *buf = '\0';
  2994. username = ast_strdupa(data);
  2995. if ((countername = strchr(username, ':'))) {
  2996. *countername = '\0';
  2997. countername++;
  2998. }
  2999. if ((domain = strchr(username, '@'))) {
  3000. *domain = '\0';
  3001. domain++;
  3002. }
  3003. /* If we have neither username nor domain now, let's give up */
  3004. if (ast_strlen_zero(username) && ast_strlen_zero(domain)) {
  3005. ast_log(LOG_ERROR, "No account given\n");
  3006. return -1;
  3007. }
  3008. if (ast_strlen_zero(countername)) {
  3009. ast_log(LOG_ERROR, "This function needs two arguments: Account:countername\n");
  3010. return -1;
  3011. }
  3012. /* We only have a domain, no username */
  3013. if (!ast_strlen_zero(username) && ast_strlen_zero(domain)) {
  3014. domain = username;
  3015. username = NULL;
  3016. }
  3017. /* If we can't find account or if the account is temporary, return. */
  3018. if (!ast_strlen_zero(username) && !find_account(domain, username, FALSE)) {
  3019. ast_log(LOG_ERROR, "Minivm account does not exist: %s@%s\n", username, domain);
  3020. return 0;
  3021. }
  3022. create_dirpath(userpath, sizeof(userpath), domain, username, NULL);
  3023. /* We have the path, now read the counter file */
  3024. res = access_counter_file(userpath, countername, 0, 0);
  3025. if (res >= 0)
  3026. snprintf(buf, len, "%d", res);
  3027. return 0;
  3028. }
  3029. /*! \brief ${MINIVMCOUNTER()} Dialplan function - changes counter data */
  3030. static int minivm_counter_func_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  3031. {
  3032. char *username, *domain, *countername, *operand;
  3033. char userpath[BUFSIZ];
  3034. int change = 0;
  3035. int operation = 0;
  3036. if(!value)
  3037. return -1;
  3038. change = atoi(value);
  3039. username = ast_strdupa(data);
  3040. if ((countername = strchr(username, ':'))) {
  3041. *countername = '\0';
  3042. countername++;
  3043. }
  3044. if ((operand = strchr(countername, ':'))) {
  3045. *operand = '\0';
  3046. operand++;
  3047. }
  3048. if ((domain = strchr(username, '@'))) {
  3049. *domain = '\0';
  3050. domain++;
  3051. }
  3052. /* If we have neither username nor domain now, let's give up */
  3053. if (ast_strlen_zero(username) && ast_strlen_zero(domain)) {
  3054. ast_log(LOG_ERROR, "No account given\n");
  3055. return -1;
  3056. }
  3057. /* We only have a domain, no username */
  3058. if (!ast_strlen_zero(username) && ast_strlen_zero(domain)) {
  3059. domain = username;
  3060. username = NULL;
  3061. }
  3062. if (ast_strlen_zero(operand) || ast_strlen_zero(countername)) {
  3063. ast_log(LOG_ERROR, "Writing to this function requires three arguments: Account:countername:operand\n");
  3064. return -1;
  3065. }
  3066. /* If we can't find account or if the account is temporary, return. */
  3067. if (!ast_strlen_zero(username) && !find_account(domain, username, FALSE)) {
  3068. ast_log(LOG_ERROR, "Minivm account does not exist: %s@%s\n", username, domain);
  3069. return 0;
  3070. }
  3071. create_dirpath(userpath, sizeof(userpath), domain, username, NULL);
  3072. /* Now, find out our operator */
  3073. if (*operand == 'i') /* Increment */
  3074. operation = 2;
  3075. else if (*operand == 'd') {
  3076. change = change * -1;
  3077. operation = 2;
  3078. } else if (*operand == 's')
  3079. operation = 1;
  3080. else {
  3081. ast_log(LOG_ERROR, "Unknown operator: %s\n", operand);
  3082. return -1;
  3083. }
  3084. /* We have the path, now read the counter file */
  3085. access_counter_file(userpath, countername, change, operation);
  3086. return 0;
  3087. }
  3088. /*! \brief CLI commands for Mini-voicemail */
  3089. static struct ast_cli_entry cli_minivm[] = {
  3090. AST_CLI_DEFINE(handle_minivm_show_users, "List defined mini-voicemail boxes"),
  3091. AST_CLI_DEFINE(handle_minivm_show_zones, "List zone message formats"),
  3092. AST_CLI_DEFINE(handle_minivm_list_templates, "List message templates"),
  3093. AST_CLI_DEFINE(handle_minivm_reload, "Reload Mini-voicemail configuration"),
  3094. AST_CLI_DEFINE(handle_minivm_show_stats, "Show some mini-voicemail statistics"),
  3095. AST_CLI_DEFINE(handle_minivm_show_settings, "Show mini-voicemail general settings"),
  3096. };
  3097. static struct ast_custom_function minivm_counter_function = {
  3098. .name = "MINIVMCOUNTER",
  3099. .read = minivm_counter_func_read,
  3100. .write = minivm_counter_func_write,
  3101. };
  3102. static struct ast_custom_function minivm_account_function = {
  3103. .name = "MINIVMACCOUNT",
  3104. .read = minivm_account_func_read,
  3105. };
  3106. /*! \brief Load mini voicemail module */
  3107. static int load_module(void)
  3108. {
  3109. int res;
  3110. res = ast_register_application_xml(app_minivm_record, minivm_record_exec);
  3111. res = ast_register_application_xml(app_minivm_greet, minivm_greet_exec);
  3112. res = ast_register_application_xml(app_minivm_notify, minivm_notify_exec);
  3113. res = ast_register_application_xml(app_minivm_delete, minivm_delete_exec);
  3114. res = ast_register_application_xml(app_minivm_accmess, minivm_accmess_exec);
  3115. res = ast_register_application_xml(app_minivm_mwi, minivm_mwi_exec);
  3116. ast_custom_function_register(&minivm_account_function);
  3117. ast_custom_function_register(&minivm_counter_function);
  3118. if (res)
  3119. return(res);
  3120. if ((res = load_config(0)))
  3121. return(res);
  3122. ast_cli_register_multiple(cli_minivm, ARRAY_LEN(cli_minivm));
  3123. /* compute the location of the voicemail spool directory */
  3124. snprintf(MVM_SPOOL_DIR, sizeof(MVM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR);
  3125. return res;
  3126. }
  3127. /*! \brief Reload mini voicemail module */
  3128. static int reload(void)
  3129. {
  3130. return(load_config(1));
  3131. }
  3132. /*! \brief Reload cofiguration */
  3133. static char *handle_minivm_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3134. {
  3135. switch (cmd) {
  3136. case CLI_INIT:
  3137. e->command = "minivm reload";
  3138. e->usage =
  3139. "Usage: minivm reload\n"
  3140. " Reload mini-voicemail configuration and reset statistics\n";
  3141. return NULL;
  3142. case CLI_GENERATE:
  3143. return NULL;
  3144. }
  3145. reload();
  3146. ast_cli(a->fd, "\n-- Mini voicemail re-configured \n");
  3147. return CLI_SUCCESS;
  3148. }
  3149. /*! \brief Unload mini voicemail module */
  3150. static int unload_module(void)
  3151. {
  3152. int res;
  3153. res = ast_unregister_application(app_minivm_record);
  3154. res |= ast_unregister_application(app_minivm_greet);
  3155. res |= ast_unregister_application(app_minivm_notify);
  3156. res |= ast_unregister_application(app_minivm_delete);
  3157. res |= ast_unregister_application(app_minivm_accmess);
  3158. res |= ast_unregister_application(app_minivm_mwi);
  3159. ast_cli_unregister_multiple(cli_minivm, ARRAY_LEN(cli_minivm));
  3160. ast_custom_function_unregister(&minivm_account_function);
  3161. ast_custom_function_unregister(&minivm_counter_function);
  3162. message_destroy_list(); /* Destroy list of voicemail message templates */
  3163. timezone_destroy_list(); /* Destroy list of timezones */
  3164. vmaccounts_destroy_list(); /* Destroy list of voicemail accounts */
  3165. return res;
  3166. }
  3167. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Mini VoiceMail (A minimal Voicemail e-mail System)",
  3168. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  3169. .load = load_module,
  3170. .unload = unload_module,
  3171. .reload = reload,
  3172. );