Fossil SCM

Add the beginnings of a new "forum" command. The only subcommand currently available is "new" which is used to start a new Forum thread.

drh 2026-10-29 20:01 UTC trunk
Commit 8bdb8bc4ccc21fc442a0371f023a511b970dcd2aa5a9365e28990c31f53f1306
1 file changed +121
+121
--- src/forum.c
+++ src/forum.c
@@ -3410,5 +3410,126 @@
34103410
ajax_save_end:
34113411
manifest_destroy(pPost);
34123412
fossil_free(zNewUuid);
34133413
db_end_transaction(rc || bRollback);
34143414
}
3415
+
3416
+/*
3417
+** COMMAND: forum
3418
+**
3419
+** Usage: %fossil forum SUBCOMMAND ...
3420
+**
3421
+** Command-line access to the forum. Subcommands:
3422
+**
3423
+** > fossil forum new --title TITLE --content FILE ?OPTIONS?
3424
+**
3425
+** Create a new forum thread. The text of the initial post is
3426
+** read from FILE, or from standard input if FILE is "-".
3427
+**
3428
+** Options:
3429
+** --content FILE Read the post content from FILE. Required.
3430
+** --dry-run Do not commit changes. Just print the artifact.
3431
+** --markup FORMAT The markup format of the content. FORMAT
3432
+** is one of "markdown" (the default), "wiki",
3433
+** or "plaintext".
3434
+** --moderate Hold the new post pending moderation
3435
+** --title TITLE Title of the new thread. Required.
3436
+** -U|--user USER Post as USER instead of the default user
3437
+*/
3438
+void forum_cmd(void){
3439
+ const char *zCmd;
3440
+ size_t n;
3441
+ if( g.argc<3 ){
3442
+ usage("new ...");
3443
+ }
3444
+ zCmd = g.argv[2];
3445
+ n = strlen(zCmd);
3446
+ if( strncmp(zCmd, "new", n)==0 ){
3447
+ const char *zTitle = find_option("title", 0, 1);
3448
+ const char *zMarkup = find_option("markup", 0, 1);
3449
+ const char *zFile = find_option("content", 0, 1);
3450
+ const int bModerate = find_option("moderate", 0, 0)!=0;
3451
+ const int bDryRun = find_option("dry-run", 0, 0)!=0;
3452
+ const char *zMimetype;
3453
+ char *zDate;
3454
+ char *zUuid;
3455
+ Blob content, x, cksum, formatCheck, errMsg;
3456
+ Manifest *pPost;
3457
+ int nrid;
3458
+
3459
+ db_find_and_open_repository(0, 0);
3460
+ verify_all_options();
3461
+ if( g.argc!=3 ){
3462
+ usage("new --title TITLE --content FILE [--markup FORMAT] "
3463
+ "[--moderate] [--user USER]");
3464
+ }
3465
+ if( zTitle==0 || zTitle[0]==0 ){
3466
+ fossil_fatal("the --title option is required");
3467
+ }
3468
+ if( zFile==0 ){
3469
+ fossil_fatal("the --content option is required");
3470
+ }
3471
+ if( zMarkup==0 || fossil_strcmp(zMarkup,"markdown")==0 ){
3472
+ zMimetype = "text/x-markdown";
3473
+ }else if( fossil_strcmp(zMarkup,"wiki")==0 ){
3474
+ zMimetype = 0;
3475
+ }else if( fossil_strcmp(zMarkup,"plaintext")==0 ){
3476
+ zMimetype = "text/plain";
3477
+ }else{
3478
+ fossil_fatal("unknown --markup \"%s\". Should be one of: "
3479
+ "markdown, wiki, plaintext", zMarkup);
3480
+ }
3481
+ if( fossil_strcmp(zFile,"-")==0 ){
3482
+ blob_read_from_channel(&content, stdin, -1);
3483
+ }else{
3484
+ blob_read_from_file(&content, zFile, ExtFILE);
3485
+ }
3486
+ if( fossil_all_whitespace(blob_str(&content)) ){
3487
+ fossil_fatal("forum post content is empty");
3488
+ }
3489
+ user_select();
3490
+
3491
+ schema_forum();
3492
+ blob_init(&x, 0, 0);
3493
+ zDate = date_in_standard_format("now");
3494
+ blob_appendf(&x, "D %s\n", zDate);
3495
+ fossil_free(zDate);
3496
+ blob_appendf(&x, "H %F\n", zTitle);
3497
+ if( zMimetype ){
3498
+ blob_appendf(&x, "N %F\n", zMimetype);
3499
+ }
3500
+ blob_appendf(&x, "U %F\n", login_name());
3501
+ blob_appendf(&x, "W %d\n%s\n", blob_size(&content), blob_str(&content));
3502
+ blob_reset(&content);
3503
+ md5sum_blob(&x, &cksum);
3504
+ blob_appendf(&x, "Z %b\n", &cksum);
3505
+ blob_reset(&cksum);
3506
+
3507
+ /* Verify that the artifact we are creating is well-formed */
3508
+ blob_init(&formatCheck, 0, 0);
3509
+ blob_init(&errMsg, 0, 0);
3510
+ blob_copy(&formatCheck, &x);
3511
+ pPost = manifest_parse(&formatCheck, 0, &errMsg);
3512
+ if( pPost==0 ){
3513
+ fossil_fatal("malformed forum post artifact - %s", blob_str(&errMsg));
3514
+ }
3515
+ if( pPost->type!=CFTYPE_FORUM ){
3516
+ fossil_fatal("artifact is not a forum post");
3517
+ }
3518
+ manifest_destroy(pPost);
3519
+
3520
+ if( bDryRun ){
3521
+ fossil_print("%s", blob_str(&x));
3522
+ }else{
3523
+ db_begin_transaction();
3524
+ nrid = wiki_put(&x, 0, bModerate);
3525
+ db_end_transaction(0);
3526
+ zUuid = rid_to_uuid(nrid);
3527
+ fossil_print("New forum thread %S%s\n", zUuid,
3528
+ bModerate ? " (pending moderation)" : "");
3529
+ fossil_free(zUuid);
3530
+ }
3531
+ blob_reset(&x);
3532
+ }else{
3533
+ usage("new ...");
3534
+ }
3535
+}
34153536
--- src/forum.c
+++ src/forum.c
@@ -3410,5 +3410,126 @@
3410 ajax_save_end:
3411 manifest_destroy(pPost);
3412 fossil_free(zNewUuid);
3413 db_end_transaction(rc || bRollback);
3414 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3415
--- src/forum.c
+++ src/forum.c
@@ -3410,5 +3410,126 @@
3410 ajax_save_end:
3411 manifest_destroy(pPost);
3412 fossil_free(zNewUuid);
3413 db_end_transaction(rc || bRollback);
3414 }
3415
3416 /*
3417 ** COMMAND: forum
3418 **
3419 ** Usage: %fossil forum SUBCOMMAND ...
3420 **
3421 ** Command-line access to the forum. Subcommands:
3422 **
3423 ** > fossil forum new --title TITLE --content FILE ?OPTIONS?
3424 **
3425 ** Create a new forum thread. The text of the initial post is
3426 ** read from FILE, or from standard input if FILE is "-".
3427 **
3428 ** Options:
3429 ** --content FILE Read the post content from FILE. Required.
3430 ** --dry-run Do not commit changes. Just print the artifact.
3431 ** --markup FORMAT The markup format of the content. FORMAT
3432 ** is one of "markdown" (the default), "wiki",
3433 ** or "plaintext".
3434 ** --moderate Hold the new post pending moderation
3435 ** --title TITLE Title of the new thread. Required.
3436 ** -U|--user USER Post as USER instead of the default user
3437 */
3438 void forum_cmd(void){
3439 const char *zCmd;
3440 size_t n;
3441 if( g.argc<3 ){
3442 usage("new ...");
3443 }
3444 zCmd = g.argv[2];
3445 n = strlen(zCmd);
3446 if( strncmp(zCmd, "new", n)==0 ){
3447 const char *zTitle = find_option("title", 0, 1);
3448 const char *zMarkup = find_option("markup", 0, 1);
3449 const char *zFile = find_option("content", 0, 1);
3450 const int bModerate = find_option("moderate", 0, 0)!=0;
3451 const int bDryRun = find_option("dry-run", 0, 0)!=0;
3452 const char *zMimetype;
3453 char *zDate;
3454 char *zUuid;
3455 Blob content, x, cksum, formatCheck, errMsg;
3456 Manifest *pPost;
3457 int nrid;
3458
3459 db_find_and_open_repository(0, 0);
3460 verify_all_options();
3461 if( g.argc!=3 ){
3462 usage("new --title TITLE --content FILE [--markup FORMAT] "
3463 "[--moderate] [--user USER]");
3464 }
3465 if( zTitle==0 || zTitle[0]==0 ){
3466 fossil_fatal("the --title option is required");
3467 }
3468 if( zFile==0 ){
3469 fossil_fatal("the --content option is required");
3470 }
3471 if( zMarkup==0 || fossil_strcmp(zMarkup,"markdown")==0 ){
3472 zMimetype = "text/x-markdown";
3473 }else if( fossil_strcmp(zMarkup,"wiki")==0 ){
3474 zMimetype = 0;
3475 }else if( fossil_strcmp(zMarkup,"plaintext")==0 ){
3476 zMimetype = "text/plain";
3477 }else{
3478 fossil_fatal("unknown --markup \"%s\". Should be one of: "
3479 "markdown, wiki, plaintext", zMarkup);
3480 }
3481 if( fossil_strcmp(zFile,"-")==0 ){
3482 blob_read_from_channel(&content, stdin, -1);
3483 }else{
3484 blob_read_from_file(&content, zFile, ExtFILE);
3485 }
3486 if( fossil_all_whitespace(blob_str(&content)) ){
3487 fossil_fatal("forum post content is empty");
3488 }
3489 user_select();
3490
3491 schema_forum();
3492 blob_init(&x, 0, 0);
3493 zDate = date_in_standard_format("now");
3494 blob_appendf(&x, "D %s\n", zDate);
3495 fossil_free(zDate);
3496 blob_appendf(&x, "H %F\n", zTitle);
3497 if( zMimetype ){
3498 blob_appendf(&x, "N %F\n", zMimetype);
3499 }
3500 blob_appendf(&x, "U %F\n", login_name());
3501 blob_appendf(&x, "W %d\n%s\n", blob_size(&content), blob_str(&content));
3502 blob_reset(&content);
3503 md5sum_blob(&x, &cksum);
3504 blob_appendf(&x, "Z %b\n", &cksum);
3505 blob_reset(&cksum);
3506
3507 /* Verify that the artifact we are creating is well-formed */
3508 blob_init(&formatCheck, 0, 0);
3509 blob_init(&errMsg, 0, 0);
3510 blob_copy(&formatCheck, &x);
3511 pPost = manifest_parse(&formatCheck, 0, &errMsg);
3512 if( pPost==0 ){
3513 fossil_fatal("malformed forum post artifact - %s", blob_str(&errMsg));
3514 }
3515 if( pPost->type!=CFTYPE_FORUM ){
3516 fossil_fatal("artifact is not a forum post");
3517 }
3518 manifest_destroy(pPost);
3519
3520 if( bDryRun ){
3521 fossil_print("%s", blob_str(&x));
3522 }else{
3523 db_begin_transaction();
3524 nrid = wiki_put(&x, 0, bModerate);
3525 db_end_transaction(0);
3526 zUuid = rid_to_uuid(nrid);
3527 fossil_print("New forum thread %S%s\n", zUuid,
3528 bModerate ? " (pending moderation)" : "");
3529 fossil_free(zUuid);
3530 }
3531 blob_reset(&x);
3532 }else{
3533 usage("new ...");
3534 }
3535 }
3536

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button