| | @@ -102,19 +102,27 @@ |
| 102 | 102 | @ <input type="file" name="file"> |
| 103 | 103 | @ </div> |
| 104 | 104 | @ </div> |
| 105 | 105 | @ </form> |
| 106 | 106 | @ <hr> |
| 107 | + |
| 107 | 108 | /* New chat messages get inserted immediately after this element */ |
| 108 | 109 | @ <span id='message-inject-point'></span> |
| 110 | + |
| 111 | + /* Always in-line the javascript for the chat page */ |
| 112 | + @ <script nonce="%h(style_nonce())">/* chat.c:%d(__LINE__) */ |
| 113 | + @ let _me = "%j(g.zLogin)"; |
| 114 | + cgi_append_content(builtin_text("chat.js"),-1); |
| 115 | + @ </script> |
| 116 | + |
| 109 | 117 | style_finish_page(); |
| 110 | 118 | } |
| 111 | 119 | |
| 112 | 120 | /* Definition of repository tables used by chat |
| 113 | 121 | */ |
| 114 | 122 | static const char zChatSchema1[] = |
| 115 | | -@ CREATE TABLE chat( |
| 123 | +@ CREATE TABLE repository.chat( |
| 116 | 124 | @ msgid INTEGER PRIMARY KEY AUTOINCREMENT, |
| 117 | 125 | @ mtime JULIANDAY, |
| 118 | 126 | @ xfrom TEXT, |
| 119 | 127 | @ xmsg TEXT, |
| 120 | 128 | @ file BLOB, |
| | @@ -139,13 +147,39 @@ |
| 139 | 147 | ** |
| 140 | 148 | ** This page receives (via XHR) a new chat-message and/or a new file |
| 141 | 149 | ** to be entered into the chat history. |
| 142 | 150 | */ |
| 143 | 151 | void chat_send_webpage(void){ |
| 152 | + int nByte; |
| 153 | + const char *zMsg; |
| 144 | 154 | login_check_credentials(); |
| 145 | 155 | if( !g.perm.Chat ) return; |
| 146 | 156 | chat_create_tables(); |
| 157 | + nByte = atoi(PD("file:bytes",0)); |
| 158 | + zMsg = PD("msg",""); |
| 159 | + if( nByte==0 ){ |
| 160 | + if( zMsg[0] ){ |
| 161 | + db_multi_exec( |
| 162 | + "INSERT INTO chat(mtime,xfrom,xmsg)" |
| 163 | + "VALUES(julianday('now'),%Q,%Q)", |
| 164 | + g.zLogin, zMsg |
| 165 | + ); |
| 166 | + } |
| 167 | + }else{ |
| 168 | + Stmt q; |
| 169 | + Blob b; |
| 170 | + db_prepare(&q, |
| 171 | + "INSERT INTO chat(mtime, xfrom,xmsg,file,fname,fmime)" |
| 172 | + "VALUES(julianday('now'),%Q,%Q,:file,%Q,%Q)", |
| 173 | + g.zLogin, zMsg, PD("file:filename",""), |
| 174 | + PD("file:mimetype","application/octet-stream")); |
| 175 | + blob_init(&b, P("file"), nByte); |
| 176 | + db_bind_blob(&q, ":file", &b); |
| 177 | + db_step(&q); |
| 178 | + db_finalize(&q); |
| 179 | + blob_reset(&b); |
| 180 | + } |
| 147 | 181 | } |
| 148 | 182 | |
| 149 | 183 | /* |
| 150 | 184 | ** WEBPAGE: chat-poll |
| 151 | 185 | ** |
| | @@ -158,35 +192,97 @@ |
| 158 | 192 | ** system implements "hanging-GET" or "long-poll" style event notification. |
| 159 | 193 | ** |
| 160 | 194 | ** The reply from this webpage is JSON that describes the new content. |
| 161 | 195 | ** Format of the json: |
| 162 | 196 | ** |
| 163 | | -** { |
| 164 | | -** "msg": [ |
| 165 | | -** { |
| 166 | | -** "msgid": integer // message id |
| 167 | | -** "mtime": text // When sent: YYYY-MM-DD HH:MM:SS UTC |
| 168 | | -** "xfrom": text // Login name of sender |
| 169 | | -** "uclr": text // Color string associated with the user |
| 170 | | -** "xmsg": text // HTML text of the message |
| 171 | | -** "fsize": integer // file attachment size in bytes |
| 172 | | -** "fname": text // Name of file attachment |
| 173 | | -** "fmime": text // MIME-type of file attachment |
| 174 | | -** } |
| 175 | | -** ] |
| 176 | | -** } |
| 197 | +** | { |
| 198 | +** | "msg":[ |
| 199 | +** | { |
| 200 | +** | "msgid": integer // message id |
| 201 | +** | "mtime": text // When sent: YYYY-MM-DD HH:MM:SS UTC |
| 202 | +** | "xfrom": text // Login name of sender |
| 203 | +** | "uclr": text // Color string associated with the user |
| 204 | +** | "xmsg": text // HTML text of the message |
| 205 | +** | "fsize": integer // file attachment size in bytes |
| 206 | +** | "fname": text // Name of file attachment |
| 207 | +** | "fmime": text // MIME-type of file attachment |
| 208 | +** | } |
| 209 | +** | ] |
| 210 | +** | } |
| 177 | 211 | ** |
| 178 | 212 | ** The "fname" and "fmime" fields are only present if "fsize" is greater |
| 179 | 213 | ** than zero. The "xmsg" field may be an empty string if "fsize" is zero. |
| 180 | 214 | ** |
| 181 | 215 | ** The "msgid" values will be in increasing order. |
| 182 | 216 | */ |
| 183 | 217 | void chat_poll_webpage(void){ |
| 218 | + Blob json; /* The json to be constructed and returned */ |
| 219 | + sqlite3_int64 dataVersion; /* Data version. Used for polling. */ |
| 220 | + sqlite3_int64 newDataVers; |
| 221 | + int iDelay = 1000; /* Delay until next poll (milliseconds) */ |
| 222 | + const char *zSep = "{\"msgs\":[\n"; /* List separator */ |
| 223 | + int msgid = atoi(PD("name","0")); |
| 224 | + Stmt q1; |
| 184 | 225 | login_check_credentials(); |
| 185 | 226 | if( !g.perm.Chat ) return; |
| 186 | 227 | chat_create_tables(); |
| 187 | 228 | cgi_set_content_type("text/json"); |
| 229 | + dataVersion = db_int64(0, "PRAGMA data_version"); |
| 230 | + db_prepare(&q1, |
| 231 | + "SELECT msgid, datetime(mtime), xfrom, xmsg, length(file), fname, fmime" |
| 232 | + " FROM chat" |
| 233 | + " WHERE msgid>%d" |
| 234 | + " ORDER BY msgid", |
| 235 | + msgid |
| 236 | + ); |
| 237 | + blob_init(&json, 0, 0); |
| 238 | + while(1){ |
| 239 | + int cnt = 0; |
| 240 | + while( db_step(&q1)==SQLITE_ROW ){ |
| 241 | + int id = db_column_int(&q1, 0); |
| 242 | + const char *zDate = db_column_text(&q1, 1); |
| 243 | + const char *zFrom = db_column_text(&q1, 2); |
| 244 | + const char *zRawMsg = db_column_text(&q1, 3); |
| 245 | + char *zMsg; |
| 246 | + int nByte; |
| 247 | + cnt++; |
| 248 | + blob_append(&json, zSep, -1); |
| 249 | + zSep = ",\n"; |
| 250 | + blob_appendf(&json, "{\"msgid\":%d,\"mtime\":\"%j\",", id, zDate); |
| 251 | + blob_appendf(&json, "\"xfrom\":\"%j\",", zFrom); |
| 252 | + blob_appendf(&json, "\"uclr\":\"%j\",", hash_color(zFrom)); |
| 253 | + |
| 254 | + /* TBD: Convert the raw message into HTML, perhaps by running it |
| 255 | + ** through a text formatter, or putting markup on @name phrases, |
| 256 | + ** etc. */ |
| 257 | + zMsg = mprintf("%h", zRawMsg); |
| 258 | + blob_appendf(&json, "\"xmsg\":\"%j\",", zMsg); |
| 259 | + fossil_free(zMsg); |
| 260 | + |
| 261 | + nByte = db_column_bytes(&q1, 4); |
| 262 | + if( nByte==0 ){ |
| 263 | + blob_appendf(&json, "\"fsize\":0}"); |
| 264 | + }else{ |
| 265 | + const char *zFName = db_column_text(&q1, 5); |
| 266 | + const char *zFMime = db_column_text(&q1, 6); |
| 267 | + blob_appendf(&json, "\"fsize\":%d,\"fname\":\"%j\",\"fmime\":\"%j\"}", |
| 268 | + nByte, zFName, zFMime); |
| 269 | + } |
| 270 | + } |
| 271 | + if( cnt ){ |
| 272 | + blob_append(&json, "\n]}", 3); |
| 273 | + cgi_set_content(&json); |
| 274 | + break; |
| 275 | + } |
| 276 | + sqlite3_sleep(iDelay); |
| 277 | + while( (newDataVers = db_int64(0,"PRAGMA data_version"))==dataVersion ){ |
| 278 | + sqlite3_sleep(iDelay); |
| 279 | + } |
| 280 | + dataVersion = newDataVers; |
| 281 | + } /* Exit by "break" */ |
| 282 | + db_finalize(&q1); |
| 283 | + return; |
| 188 | 284 | } |
| 189 | 285 | |
| 190 | 286 | /* |
| 191 | 287 | ** WEBPAGE: chat-download |
| 192 | 288 | ** |
| 193 | 289 | |