Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:48 2009

## 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 Tilghman Lesher.  All rights reserved.
    00005  *
    00006  * Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
    00007  *
    00008  * This code is released by the author with no restrictions on usage.
    00009  *
    00010  * See http://www.asterisk.org for more information about
    00011  * the Asterisk project. Please do not directly contact
    00012  * any of the maintainers of this project for assistance;
    00013  * the project provides a web site, mailing lists and IRC
    00014  * channels for your use.
    00015  *
    00016  */
    00017
    00018 /*! \file
    00019  *
    00020  * \brief SayUnixTime application
    00021  *
    00022  * \ingroup applications
    00023  */
    00024
    00025 #include <stdio.h>
    00026 #include <stdlib.h>
    00027 #include <unistd.h>
    00028 #include <string.h>
    00029
    00030 #include "asterisk.h"
    00031
    00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211526 $")
    00033
    00034 #include "asterisk/file.h"
    00035 #include "asterisk/logger.h"
    00036 #include "asterisk/options.h"
    00037 #include "asterisk/channel.h"
    00038 #include "asterisk/pbx.h"
    00039 #include "asterisk/module.h"
    00040 #include "asterisk/say.h"
    00041
    00042
    00043 static char *tdesc = "Say time";
    00044
    00045 static char *app_sayunixtime = "SayUnixTime";
    00046 static char *app_datetime = "DateTime";
    00047
    00048 static char *sayunixtime_synopsis = "Says a specified time in a custom format";
    00049
    00050 static char *sayunixtime_descrip =
    00051 "SayUnixTime([unixtime][|[timezone][|format]])\n"
    00052 "  unixtime: time, in seconds since Jan 1, 1970.  May be negative.\n"
    00053 "              defaults to now.\n"
    00054 "  timezone: timezone, see /usr/share/zoneinfo for a list.\n"
    00055 "              defaults to machine default.\n"
    00056 "  format:   a format the time is to be said in.  See voicemail.conf.\n"
    00057 "              defaults to \"ABdY 'digits/at' IMp\"\n";
    00058 static char *datetime_descrip =
    00059 "DateTime([unixtime][|[timezone][|format]])\n"
    00060 "  unixtime: time, in seconds since Jan 1, 1970.  May be negative.\n"
    00061 "              defaults to now.\n"
    00062 "  timezone: timezone, see /usr/share/zoneinfo for a list.\n"
    00063 "              defaults to machine default.\n"
    00064 "  format:   a format the time is to be said in.  See voicemail.conf.\n"
    00065 "              defaults to \"ABdY 'digits/at' IMp\"\n";
    00066
    00067 STANDARD_LOCAL_USER;
    00068
    00069 LOCAL_USER_DECL;
    00070
    00071 static int sayunixtime_exec(struct ast_channel *chan, void *data)
    00072 {
    00073    int res=0;
    00074    struct localuser *u;
    00075    char *s,*zone=NULL,*timec,*format;
    00076    time_t unixtime;
    00077    struct timeval tv;
    00078
    00079    LOCAL_USER_ADD(u);
    00080
    00081    tv = ast_tvnow();
    00082    unixtime = (time_t)tv.tv_sec;
    00083
    00084    if( !strcasecmp(chan->language, "da" ) ) {
    00085       format = "A dBY HMS";
    00086    } else if ( !strcasecmp(chan->language, "de" ) ) {
    00087       format = "A dBY HMS";
    00088    } else {
    00089       format = "ABdY 'digits/at' IMp";
    00090    }
    00091
    00092    if (data) {
    00093       s = data;
    00094       s = ast_strdupa(s);
    00095       if (s) {
    00096          timec = strsep(&s,"|");
    00097          if ((timec) && (*timec != '\0')) {
    00098             long timein;
    00099             if (sscanf(timec,"%30ld",&timein) == 1) {
    00100                unixtime = (time_t)timein;
    00101             }
    00102          }
    00103          if (s) {
    00104             zone = strsep(&s,"|");
    00105             if (zone && (*zone == '\0'))
    00106                zone = NULL;
    00107             if (s) {
    00108                format = s;
    00109             }
    00110          }
    00111       } else {
    00112          ast_log(LOG_ERROR, "Out of memory error\n");
    00113       }
    00114    }
    00115
    00116    if (chan->_state != AST_STATE_UP) {
    00117       res = ast_answer(chan);
    00118    }
    00119    if (!res)
    00120       res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY, chan->language, format, zone);
    00121
    00122    LOCAL_USER_REMOVE(u);
    00123    return res;
    00124 }
    00125
    00126 int unload_module(void)
    00127 {
    00128    int res;
    00129
    00130    res = ast_unregister_application(app_sayunixtime);
    00131    res |= ast_unregister_application(app_datetime);
    00132
    00133    STANDARD_HANGUP_LOCALUSERS;
    00134
    00135    return res;
    00136 }
    00137
    00138 int load_module(void)
    00139 {
    00140    int res;
    00141
    00142    res = ast_register_application(app_sayunixtime, sayunixtime_exec, sayunixtime_synopsis, sayunixtime_descrip);
    00143    res |= ast_register_application(app_datetime, sayunixtime_exec, sayunixtime_synopsis, datetime_descrip);
    00144
    00145    return res;
    00146 }
    00147
    00148 char *description(void)
    00149 {
    00150    return tdesc;
    00151 }
    00152
    00153 int usecount(void)
    00154 {
    00155    int res;
    00156    STANDARD_USECOUNT(res);
    00157    return res;
    00158 }
    00159
    00160 char *key()
    00161 {
    00162    return ASTERISK_GPL_KEY;
    00163 }