Mailing list header changes (fuck you Google)
[citadel.git] / webcit / graphics.c
1 /*
2  * Handles HTTP upload of graphics files into the system.
3  *
4  * Copyright (c) 1996-2016 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 // display the picture (icon, photo, whatever) associated with the current room
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 the picture (icon, photo, whatever) associated with the current room
44 void common_code_for_editroompic_and_editpic(char *servcmd)
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("%s %ld|%s", servcmd, (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 // upload the picture (icon, photo, whatever) associated with the current room
74 void editroompic(void)
75 {
76         common_code_for_editroompic_and_editpic("ULRI");
77 }
78
79         
80 // upload the picture (icon, photo, whatever) associated with the current user
81 void editpic(void)
82 {
83         common_code_for_editroompic_and_editpic("ULUI");
84 }
85
86
87 // display the screen for uploading graphics to the server
88 void display_graphics_upload(char *filename)
89 {
90         StrBuf *Line;
91
92         Line = NewStrBuf();
93         serv_printf("UIMG 0||%s", filename);
94         StrBuf_ServGetln(Line);
95         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
96                 display_main_menu();
97                 return;
98         }
99         else
100         {
101                 output_headers(1, 0, 0, 0, 1, 0);
102                 do_template("files_graphicsupload");
103                 end_burst();
104         }
105         FreeStrBuf(&Line);
106 }
107
108
109 void do_graphics_upload(char *filename)
110 {
111         StrBuf *Line;
112         const char *MimeType;
113         wcsession *WCC = WC;
114         int bytes_remaining;
115         int pos = 0;
116         int thisblock;
117         bytes_remaining = WCC->upload_length;
118
119         if (havebstr("cancel_button")) {
120                 AppendImportantMessage(_("Graphics upload has been cancelled."), -1);
121                 display_main_menu();
122                 return;
123         }
124
125         if (WCC->upload_length == 0) {
126                 AppendImportantMessage(_("You didn't upload a file."), -1);
127                 display_main_menu();
128                 return;
129         }
130         
131         MimeType = GuessMimeType(ChrPtr(WCC->upload), bytes_remaining);
132         serv_printf("UIMG 1|%s|%s", MimeType, filename);
133
134         Line = NewStrBuf();
135         StrBuf_ServGetln(Line);
136         if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
137                 display_main_menu();
138                 FreeStrBuf(&Line);
139                 return;
140         }
141         while (bytes_remaining) {
142                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
143                 serv_printf("WRIT %d", thisblock);
144                 StrBuf_ServGetln(Line);
145                 if (GetServerStatusMsg(Line, NULL, 1, 7) != 7) {
146                         serv_puts("UCLS 0");
147                         StrBuf_ServGetln(Line);
148                         display_main_menu();
149                         FreeStrBuf(&Line);
150                         return;
151                 }
152                 thisblock = extract_int(ChrPtr(Line) +4, 0);
153                 serv_write(&ChrPtr(WCC->upload)[pos], thisblock);
154                 pos += thisblock;
155                 bytes_remaining -= thisblock;
156         }
157
158         serv_puts("UCLS 1");
159         StrBuf_ServGetln(Line);
160         if (*ChrPtr(Line) != 'x') {
161                 display_success(ChrPtr(Line) + 4);
162         
163         }
164         FreeStrBuf(&Line);
165
166 }
167
168
169 void edithellopic(void)    { do_graphics_upload("hello"); }
170 void editgoodbuyepic(void) { do_graphics_upload("UIMG 1|%s|goodbuye"); }
171
172 /* The users photo display / upload facility */
173 void display_editpic(void) {
174         putbstr("__PICDESC", NewStrBufPlain(_("your photo"), -1));
175         putbstr("__UPLURL", NewStrBufPlain(HKEY("editpic")));
176         display_graphics_upload("editpic");
177 }
178 /* room picture dispay / upload facility */
179 void display_editroompic(void) {
180         putbstr("__PICDESC", NewStrBufPlain(_("the icon for this room"), -1));
181         putbstr("__UPLURL", NewStrBufPlain(HKEY("editroompic")));
182         display_graphics_upload("editroompic");
183 }
184
185 /* the greetingpage hello pic */
186 void display_edithello(void) {
187         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("hello")));
188         putbstr("__PICDESC", NewStrBufPlain(_("the Greetingpicture for the login prompt"), -1));
189         putbstr("__UPLURL", NewStrBufPlain(HKEY("edithellopic")));
190         display_graphics_upload("edithellopic");
191 }
192
193 /* the logoff banner */
194 void display_editgoodbyepic(void) {
195         putbstr("__WHICHPIC", NewStrBufPlain(HKEY("UIMG 0|%s|goodbuye")));
196         putbstr("__PICDESC", NewStrBufPlain(_("the Logoff banner picture"), -1));
197         putbstr("__UPLURL", NewStrBufPlain(HKEY("editgoodbuyepic")));
198         display_graphics_upload("editgoodbuyepic");
199 }
200
201
202 void 
203 InitModule_GRAPHICS
204 (void)
205 {
206         WebcitAddUrlHandler(HKEY("display_editpic"), "", 0, display_editpic, 0);
207         WebcitAddUrlHandler(HKEY("editpic"), "", 0, editpic, 0);
208         WebcitAddUrlHandler(HKEY("display_editroompic"), "", 0, display_editroompic, 0);
209         WebcitAddUrlHandler(HKEY("editroompic"), "", 0, editroompic, 0);
210         WebcitAddUrlHandler(HKEY("display_edithello"), "", 0, display_edithello, 0);
211         WebcitAddUrlHandler(HKEY("edithellopic"), "", 0, edithellopic, 0);
212         WebcitAddUrlHandler(HKEY("display_editgoodbuye"), "", 0, display_editgoodbyepic, 0);
213         WebcitAddUrlHandler(HKEY("editgoodbuyepic"), "", 0, editgoodbuyepic, 0);
214         WebcitAddUrlHandler(HKEY("roompic"), "", 0, display_roompic, 0);
215 }