Sat Apr 26 2014 22:01:27

Asterisk developer's documentation


app_sayunixtime.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2003, 2006 Tilghman Lesher.  All rights reserved.
00005  * Copyright (c) 2006 Digium, Inc.
00006  *
00007  * Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
00008  *
00009  * This code is released by the author with no restrictions on usage.
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief SayUnixTime application
00022  *
00023  * \author Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 356042 $")
00035 
00036 #include "asterisk/file.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/say.h"
00041 #include "asterisk/app.h"
00042 
00043 /*** DOCUMENTATION
00044    <application name="SayUnixTime" language="en_US">
00045       <synopsis>
00046          Says a specified time in a custom format.
00047       </synopsis>
00048       <syntax>
00049          <parameter name="unixtime" required="false">
00050             <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
00051          </parameter>
00052          <parameter name="timezone" required="false" >
00053             <para>timezone, see <directory>/usr/share/zoneinfo</directory> for a list. Defaults to machine default.</para>
00054          </parameter>
00055          <parameter name="format" required="false" >
00056             <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
00057             Defaults to <literal>ABdY "digits/at" IMp</literal></para>
00058          </parameter>
00059          <parameter name="options" required="false">
00060              <optionlist>
00061                <option name="j">
00062                   <para>Allow the calling user to dial digits to jump to that extension.</para>
00063                </option>
00064             </optionlist>
00065          </parameter>
00066       </syntax>
00067       <description>
00068          <para>Uses some of the sound files stored in <directory>/var/lib/asterisk/sounds</directory> to construct a phrase 
00069          saying the specified date and/or time in the specified format. </para>
00070       </description>
00071       <see-also>
00072          <ref type="function">STRFTIME</ref>
00073          <ref type="function">STRPTIME</ref>
00074          <ref type="function">IFTIME</ref>
00075       </see-also>
00076    </application>
00077    <application name="DateTime" language="en_US">
00078       <synopsis>
00079          Says a specified time in a custom format.
00080       </synopsis>
00081       <syntax>
00082          <parameter name="unixtime">
00083             <para>time, in seconds since Jan 1, 1970.  May be negative. Defaults to now.</para>
00084          </parameter>
00085          <parameter name="timezone">
00086             <para>timezone, see <filename>/usr/share/zoneinfo</filename> for a list. Defaults to machine default.</para>
00087          </parameter>
00088          <parameter name="format">
00089             <para>a format the time is to be said in.  See <filename>voicemail.conf</filename>.
00090             Defaults to <literal>ABdY "digits/at" IMp</literal></para>
00091          </parameter>
00092       </syntax>
00093       <description>
00094          <para>Say the date and time in a specified format.</para>
00095       </description>
00096    </application>
00097 
00098  ***/
00099 
00100 enum {
00101    OPT_JUMP =          (1 << 0),
00102 };
00103 
00104 enum {
00105    OPT_ARG_JUMP = 0,
00106    /* note: this entry _MUST_ be the last one in the enum */
00107    OPT_ARG_ARRAY_SIZE,
00108 };
00109 
00110 AST_APP_OPTIONS(sayunixtime_exec_options, BEGIN_OPTIONS
00111    AST_APP_OPTION_ARG('j', OPT_JUMP, OPT_ARG_JUMP),
00112 END_OPTIONS );
00113 
00114 static char *app_sayunixtime = "SayUnixTime";
00115 static char *app_datetime = "DateTime";
00116 
00117 static int sayunixtime_exec(struct ast_channel *chan, const char *data)
00118 {
00119    AST_DECLARE_APP_ARGS(args,
00120       AST_APP_ARG(timeval);
00121       AST_APP_ARG(timezone);
00122       AST_APP_ARG(format);
00123       AST_APP_ARG(options);
00124    );
00125    char *parse;
00126    int res = 0;
00127    time_t unixtime;
00128    /* New default behavior is do not jump on key pressed */
00129    const char * haltondigits = AST_DIGIT_NONE;
00130    struct ast_flags64 opts = { 0, };
00131    char *opt_args[OPT_ARG_ARRAY_SIZE];
00132 
00133    if (!data) {
00134       return 0;
00135    }
00136 
00137    parse = ast_strdupa(data);
00138 
00139    AST_STANDARD_APP_ARGS(args, parse);
00140 
00141    /* check if we had the 'j' jump flag in option list */
00142    if (!ast_strlen_zero(args.options)) {
00143       ast_app_parse_options64(sayunixtime_exec_options, &opts, opt_args, args.options);
00144       if (ast_test_flag64(&opts, OPT_JUMP)){
00145          haltondigits = AST_DIGIT_ANY;
00146       }
00147    }
00148 
00149    ast_get_time_t(ast_strlen_zero(args.timeval) ? NULL : args.timeval, &unixtime, time(NULL), NULL);
00150 
00151    if (ast_channel_state(chan) != AST_STATE_UP) {
00152       res = ast_answer(chan);
00153    }
00154 
00155    if (!res) {
00156       res = ast_say_date_with_format(chan, unixtime, haltondigits,
00157                       ast_channel_language(chan), ast_strlen_zero(args.format) ? NULL : args.format, ast_strlen_zero(args.timezone) ? NULL : args.timezone);
00158    }
00159 
00160    return res;
00161 }
00162 
00163 static int unload_module(void)
00164 {
00165    int res;
00166    
00167    res = ast_unregister_application(app_sayunixtime);
00168    res |= ast_unregister_application(app_datetime);
00169    
00170    return res;
00171 }
00172 
00173 static int load_module(void)
00174 {
00175    int res;
00176    
00177    res = ast_register_application_xml(app_sayunixtime, sayunixtime_exec);
00178    res |= ast_register_application_xml(app_datetime, sayunixtime_exec);
00179    
00180    return res;
00181 }
00182 
00183 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say time");