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