26905a0b509bbb703e0755205c2eb54a9b7bfa5b
[asterisk/asterisk.git] / apps / app_sayunixtime.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * SayUnixTime application
5  * 
6  * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
7  *
8  * Tilghman Lesher <app_sayunixtime__200309@the-tilghman.com>
9  *
10  * This code is released by the author with no restrictions on usage.
11  *
12  */
13
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/options.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/say.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26
27 static char *tdesc = "Say time";
28
29 static char *app_sayunixtime = "SayUnixTime";
30
31 static char *sayunixtime_synopsis = "Says a specified time in a custom format";
32
33 static char *sayunixtime_descrip =
34 "SayUnixTime([unixtime][|[timezone][|format]])\n"
35 "  unixtime: time, in seconds since Jan 1, 1970.  May be negative.\n"
36 "              defaults to now.\n"
37 "  timezone: timezone, see /usr/share/zoneinfo for a list.\n"
38 "              defaults to machine default.\n"
39 "  format:   a format the time is to be said in.  See voicemail.conf.\n"
40 "              defaults to \"ABdY 'digits/at' IMp\"\n"
41 "  Returns 0 or -1 on hangup.\n";
42
43 STANDARD_LOCAL_USER;
44
45 LOCAL_USER_DECL;
46
47 static int sayunixtime_exec(struct ast_channel *chan, void *data)
48 {
49         int res=0;
50         struct localuser *u;
51         char *s,*zone=NULL,*timec;
52         time_t unixtime;
53         char *format = "ABdY 'digits/at' IMp";
54         struct timeval tv;
55
56         LOCAL_USER_ADD(u);
57
58         gettimeofday(&tv,NULL);
59         unixtime = (time_t)tv.tv_sec;
60
61         if (data) {
62                 s = data;
63                 s = strdupa(s);
64                 if (s) {
65                         timec = strsep(&s,"|");
66                         if ((timec) && (*timec != '\0')) {
67                                 long timein;
68                                 if (sscanf(timec,"%ld",&timein) == 1) {
69                                         unixtime = (time_t)timein;
70                                 }
71                         }
72                         if (s) {
73                                 zone = strsep(&s,"|");
74                                 if (zone && (*zone == '\0'))
75                                         zone = NULL;
76                                 if (s) {
77                                         format = s;
78                                 }
79                         } else {
80                                 ast_log(LOG_ERROR, "Out of memory error\n");
81                         }
82                 }
83         }
84
85         res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY, chan->language, format, zone);
86
87         LOCAL_USER_REMOVE(u);
88         return res;
89 }
90
91 int unload_module(void)
92 {
93         STANDARD_HANGUP_LOCALUSERS;
94         return ast_unregister_application(app_sayunixtime);
95 }
96
97 int load_module(void)
98 {
99         return ast_register_application(app_sayunixtime, sayunixtime_exec, sayunixtime_synopsis, sayunixtime_descrip);
100 }
101
102 char *description(void)
103 {
104         return tdesc;
105 }
106
107 int usecount(void)
108 {
109         int res;
110         STANDARD_USECOUNT(res);
111         return res;
112 }
113
114 char *key()
115 {
116         return ASTERISK_GPL_KEY;
117 }