Display roompic in the banner using the new code
[citadel.git] / webcit / graphics.c
1 /*
2  * Handles HTTP upload of graphics files into the system.
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16
17 extern void output_static(const char* What);
18
19
20
21 void display_roompic(void) {
22         off_t bytes;
23         StrBuf *Buf = NewStrBuf();
24         serv_printf("DLRI");
25         StrBuf_ServGetln(Buf);
26         if (GetServerStatus(Buf, NULL) == 6) {
27                 StrBufCutLeft(Buf, 4);
28                 bytes = StrBufExtract_long(Buf, 0, '|');
29                 StrBuf *content_type = NewStrBuf();
30                 StrBufExtract_token(content_type, Buf, 3, '|');
31                 WC->WBuf = NewStrBuf();
32                 StrBuf_ServGetBLOBBuffered(WC->WBuf, bytes);
33                 http_transmit_thing(ChrPtr(content_type), 0);
34                 FreeStrBuf(&content_type);
35         }
36         else {
37                 output_error_pic("", "");
38         }
39         FreeStrBuf(&Buf);
40 }
41
42
43 // upload your photo
44 void editpic(void)
45 {
46         if (havebstr("cancel_button")) {
47                 AppendImportantMessage(_("Graphics upload has been cancelled."), -1);
48                 display_main_menu();
49                 return;
50         }
51
52         if (WC->upload_length == 0) {
53                 AppendImportantMessage(_("You didn't upload a file."), -1);
54                 display_main_menu();
55                 return;
56         }
57         
58         serv_printf("ULUI %ld|%s", (long)WC->upload_length, GuessMimeType(ChrPtr(WC->upload), WC->upload_length));
59         StrBuf *Line = NewStrBuf();
60         StrBuf_ServGetln(Line);
61         if (GetServerStatusMsg(Line, NULL, 0, 0) == 7) {
62                 serv_write(ChrPtr(WC->upload), WC->upload_length);
63                 display_success(ChrPtr(Line) + 4);
64         }
65         else {
66                 AppendImportantMessage((ChrPtr(Line) + 4), -1);
67                 display_main_menu();
68         }
69         FreeStrBuf(&Line);
70 }
71
72
73 void display_graphics_upload(char *filename)
74 {
75         StrBuf *Line;
76
77         Line = NewStrBuf();
78         serv_printf("UIMG 0||%s", filename);
79         StrBuf_ServGetln(Line);
80         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
81                 display_main_menu();
82                 return;
83         }
84         else
85         {
86                 output_headers(1, 0, 0, 0, 1, 0);
87                 do_template("files_graphicsupload");
88                 end_burst();
89         }
90         FreeStrBuf(&Line);
91 }
92
93 void do_graphics_upload(char *filename)
94 {
95         StrBuf *Line;
96         const char *MimeType;
97         wcsession *WCC = WC;
98         int bytes_remaining;
99         int pos = 0;
100         int thisblock;
101         bytes_remaining = WCC->upload_length;
102
103         if (havebstr("cancel_button")) {
104                 AppendImportantMessage(_("Graphics upload has been cancelled."), -1);
105                 display_main_menu();
106                 return;
107         }
108
109         if (WCC->upload_length == 0) {
110                 AppendImportantMessage(_("You didn't upload a file."), -1);
111                 display_main_menu();
112                 return;
113         }
114         
115         MimeType = GuessMimeType(ChrPtr(WCC->upload), bytes_remaining);
116         serv_printf("UIMG 1|%s|%s", MimeType, filename);
117
118         Line = NewStrBuf();
119         StrBuf_ServGetln(Line);
120         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
121                 display_main_menu();
122                 FreeStrBuf(&Line);
123                 return;
124         }
125         while (bytes_remaining) {
126                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
127                 serv_printf("WRIT %d", thisblock);
128                 StrBuf_ServGetln(Line);
129                 if (GetServerStatusMsg(Line, NULL, 1, 7) != 7) {
130                         serv_puts("UCLS 0");
131                         StrBuf_ServGetln(Line);
132                         display_main_menu();
133                         FreeStrBuf(&Line);
134                         return;
135                 }
136                 thisblock = extract_int(ChrPtr(Line) +4, 0);
137                 serv_write(&ChrPtr(WCC->upload)[pos], thisblock);
138                 pos += thisblock;
139                 bytes_remaining -= thisblock;
140         }
141
142         serv_puts("UCLS 1");
143         StrBuf_ServGetln(Line);
144         if (*ChrPtr(Line) != 'x') {
145                 display_success(ChrPtr(Line) + 4);
146         
147         }
148         FreeStrBuf(&Line);
149
150 }
151
152
153 void edithellopic(void)    { do_graphics_upload("hello"); }
154 void editgoodbuyepic(void) { do_graphics_upload("UIMG 1|%s|goodbuye"); }
155
156 /* The users photo display / upload facility */
157 void display_editpic(void) {
158         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("_userpic_")));
159         putbstr("__PICDESC", NewStrBufPlain(_("your photo"), -1));
160         putbstr("__UPLURL", NewStrBufPlain(HKEY("editpic")));
161         display_graphics_upload("editpic");
162 }
163 /* room picture dispay / upload facility */
164 void display_editroompic(void) {
165         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("_roompic_")));
166         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this room"), -1));
167         putbstr("__UPLURL", NewStrBufPlain(HKEY("editroompic")));
168         display_graphics_upload("editroompic");
169 }
170
171 /* the greetingpage hello pic */
172 void display_edithello(void) {
173         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("hello")));
174         putbstr("__PICDESC", NewStrBufPlain(_("the Greetingpicture for the login prompt"), -1));
175         putbstr("__UPLURL", NewStrBufPlain(HKEY("edithellopic")));
176         display_graphics_upload("edithellopic");
177 }
178
179 /* the logoff banner */
180 void display_editgoodbyepic(void) {
181         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("UIMG 0|%s|goodbuye")));
182         putbstr("__PICDESC", NewStrBufPlain(_("the Logoff banner picture"), -1));
183         putbstr("__UPLURL", NewStrBufPlain(HKEY("editgoodbuyepic")));
184         display_graphics_upload("editgoodbuyepic");
185 }
186
187 void editroompic(void) {
188         char buf[SIZ];
189         snprintf(buf, SIZ, "_roompic_|%s",
190                  bstr("which_room"));
191         do_graphics_upload(buf);
192 }
193
194 void 
195 InitModule_GRAPHICS
196 (void)
197 {
198         WebcitAddUrlHandler(HKEY("display_editpic"), "", 0, display_editpic, 0);
199         WebcitAddUrlHandler(HKEY("editpic"), "", 0, editpic, 0);
200         WebcitAddUrlHandler(HKEY("display_editroompic"), "", 0, display_editroompic, 0);
201         WebcitAddUrlHandler(HKEY("editroompic"), "", 0, editroompic, 0);
202         WebcitAddUrlHandler(HKEY("display_edithello"), "", 0, display_edithello, 0);
203         WebcitAddUrlHandler(HKEY("edithellopic"), "", 0, edithellopic, 0);
204         WebcitAddUrlHandler(HKEY("display_editgoodbuye"), "", 0, display_editgoodbyepic, 0);
205         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), "", 0, editgoodbuyepic, 0);
206         WebcitAddUrlHandler(HKEY("roompic"), "", 0, display_roompic, 0);
207 }