Doxygen documentation update from oej (issue #5505)
[asterisk/asterisk.git] / image.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Image Management
22  * 
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <signal.h>
31 #include <errno.h>
32 #include <unistd.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/sched.h"
39 #include "asterisk/options.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/file.h"
43 #include "asterisk/image.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/lock.h"
47
48 static struct ast_imager *list;
49 AST_MUTEX_DEFINE_STATIC(listlock);
50
51 int ast_image_register(struct ast_imager *img)
52 {
53         if (option_verbose > 1)
54                 ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
55         ast_mutex_lock(&listlock);
56         img->next = list;
57         list = img;
58         ast_mutex_unlock(&listlock);
59         return 0;
60 }
61
62 void ast_image_unregister(struct ast_imager *img)
63 {
64         struct ast_imager *i, *prev = NULL;
65         ast_mutex_lock(&listlock);
66         i = list;
67         while(i) {
68                 if (i == img) {
69                         if (prev) 
70                                 prev->next = i->next;
71                         else
72                                 list = i->next;
73                         break;
74                 }
75                 prev = i;
76                 i = i->next;
77         }
78         ast_mutex_unlock(&listlock);
79         if (i && (option_verbose > 1))
80                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
81 }
82
83 int ast_supports_images(struct ast_channel *chan)
84 {
85         if (!chan || !chan->tech)
86                 return 0;
87         if (!chan->tech->send_image)
88                 return 0;
89         return 1;
90 }
91
92 static int file_exists(char *filename)
93 {
94         int res;
95         struct stat st;
96         res = stat(filename, &st);
97         if (!res)
98                 return st.st_size;
99         return 0;
100 }
101
102 static void make_filename(char *buf, int len, char *filename, char *preflang, char *ext)
103 {
104         if (filename[0] == '/') {
105                 if (preflang && strlen(preflang))
106                         snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
107                 else
108                         snprintf(buf, len, "%s.%s", filename, ext);
109         } else {
110                 if (preflang && strlen(preflang))
111                         snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_VAR_DIR, "images", filename, preflang, ext);
112                 else
113                         snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_VAR_DIR, "images", filename, ext);
114         }
115 }
116
117 struct ast_frame *ast_read_image(char *filename, char *preflang, int format)
118 {
119         struct ast_imager *i;
120         char buf[256];
121         char tmp[80];
122         char *e;
123         struct ast_imager *found = NULL;
124         int fd;
125         int len=0;
126         struct ast_frame *f = NULL;
127 #if 0 /* We need to have some sort of read-only lock */
128         ast_mutex_lock(&listlock);
129 #endif  
130         i = list;
131         while(!found && i) {
132                 if (i->format & format) {
133                         char *stringp=NULL;
134                         strncpy(tmp, i->exts, sizeof(tmp)-1);
135                         stringp=tmp;
136                         e = strsep(&stringp, "|");
137                         while(e) {
138                                 make_filename(buf, sizeof(buf), filename, preflang, e);
139                                 if ((len = file_exists(buf))) {
140                                         found = i;
141                                         break;
142                                 }
143                                 make_filename(buf, sizeof(buf), filename, NULL, e);
144                                 if ((len = file_exists(buf))) {
145                                         found = i;
146                                         break;
147                                 }
148                                 e = strsep(&stringp, "|");
149                         }
150                 }
151                 i = i->next;
152         }
153         if (found) {
154                 fd = open(buf, O_RDONLY);
155                 if (fd > -1) {
156                         if (!found->identify || found->identify(fd)) {
157                                 /* Reset file pointer */
158                                 lseek(fd, 0, SEEK_SET);
159                                 f = found->read_image(fd,len); 
160                         } else
161                                 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, i->name);
162                         close(fd);
163                 } else
164                         ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
165         } else
166                 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
167 #if 0
168         ast_mutex_unlock(&listlock);
169 #endif  
170         return f;
171 }
172
173
174 int ast_send_image(struct ast_channel *chan, char *filename)
175 {
176         struct ast_frame *f;
177         int res = -1;
178         if (chan->tech->send_image) {
179                 f = ast_read_image(filename, chan->language, -1);
180                 if (f) {
181                         res = chan->tech->send_image(chan, f);
182                         ast_frfree(f);
183                 }
184         }
185         return res;
186 }
187
188 static int show_image_formats(int fd, int argc, char *argv[])
189 {
190 #define FORMAT "%10s %10s %50s %10s\n"
191 #define FORMAT2 "%10s %10s %50s %10s\n"
192         struct ast_imager *i;
193         if (argc != 3)
194                 return RESULT_SHOWUSAGE;
195         ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
196         i = list;
197         while(i) {
198                 ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
199                 i = i->next;
200         };
201         return RESULT_SUCCESS;
202 }
203
204 struct ast_cli_entry show_images =
205 {
206         { "show", "image", "formats" },
207         show_image_formats,
208         "Displays image formats",
209 "Usage: show image formats\n"
210 "       displays currently registered image formats (if any)\n"
211 };
212
213
214 int ast_image_init(void)
215 {
216         ast_cli_register(&show_images);
217         return 0;
218 }
219