Add three methods for diary manipulation. --- ChangeLog Sat Feb 16 15:07:35 2002 +++ ChangeLog Sat Feb 16 15:09:24 2002 @@ -1,3 +1,13 @@ +2002-02-16 Gary Benson + + * diary.h: + * diary.c (diary_post_serve): Move entry storage code into new + function, diary_store_entry(). + + * xmlrpc-methods.c: + * sample_db/site/xmlrpc.xml: Add new XML-RPC methods diary.len(), + diary.get() and diary.set(). + 2002-02-16 Gary Benson * acct_maint.h: diff -ru sample_db/site/xmlrpc.xml sample_db/site/xmlrpc.xml --- sample_db/site/xmlrpc.xml Sat Feb 16 14:17:10 2002 +++ sample_db/site/xmlrpc.xml Sat Feb 16 14:05:00 2002 @@ -24,6 +24,22 @@ methods requiring authentication. +

Diary manipulation

+ +
+
int length = diary.len(string user)
+
Return the number of entries in a diary.
+ +
string html = diary.get(string user, int index)
+
Return a diary entry. The index is zero-based, so if + diary.len() returns 2 then valid indices are 0 and 1.
+ +
diary.set(string cookie, int index, string html)
+
Set a diary entry. Use -1 as the index to post a new entry, + although the value returned by diary.len() is also + acceptable.
+
+

Test functions

diff -ru diary.c diary.c --- diary.c Tue Nov 7 22:43:32 2000 +++ diary.c Sat Feb 16 13:43:51 2002 @@ -214,36 +214,13 @@ return render_footer_send (vr); } -static int -diary_post_serve (VirguleReq *vr) +int +diary_store_entry (VirguleReq *vr, const char *key, const char *entry) { pool *p = vr->r->pool; - table *args; - const char *entry, *diary; - const char *date, *key; + const char *date = iso_now (p); xmlDoc *entry_doc, *old_entry_doc; xmlNode *root, *tree; - int status; - char *error; - - auth_user (vr); - if (vr->u == NULL) - return send_error_page (vr, "Not logged in", "You can't post a diary entry because you're not logged in."); - - diary = ap_psprintf (p, "acct/%s/diary", vr->u); - - args = get_args_table (vr); - - if (ap_table_get (args, "preview")) - return diary_preview_serve (vr); - - key = validate_key(vr, diary, ap_table_get (args, "key")); - if (key == NULL) - return send_error_page (vr, "Invalid Key", "An invalid diary key was submitted."); - - date = iso_now (p); - entry = ap_table_get (args, "entry"); - entry = nice_htext (vr, entry, &error); entry_doc = db_xml_doc_new (p); root = xmlNewDocNode (entry_doc, NULL, "entry", NULL); @@ -269,8 +246,39 @@ tree = xmlNewChild (root, NULL, "update", date); } - status = db_xml_put (p, vr->db, key, entry_doc); + return db_xml_put (p, vr->db, key, entry_doc); +} + +static int +diary_post_serve (VirguleReq *vr) +{ + pool *p = vr->r->pool; + table *args; + const char *entry, *diary; + const char *key; + int status; + char *error; + + auth_user (vr); + if (vr->u == NULL) + return send_error_page (vr, "Not logged in", "You can't post a diary entry because you're not logged in."); + + diary = ap_psprintf (p, "acct/%s/diary", vr->u); + + args = get_args_table (vr); + + if (ap_table_get (args, "preview")) + return diary_preview_serve (vr); + + key = validate_key(vr, diary, ap_table_get (args, "key")); + if (key == NULL) + return send_error_page (vr, "Invalid Key", "An invalid diary key was submitted."); + + entry = ap_table_get (args, "entry"); + entry = nice_htext (vr, entry, &error); + status = diary_store_entry (vr, key, entry); + if (status) return send_error_page (vr, "Error storing diary entry", diff -ru diary.h diary.h --- diary.h Sun Mar 5 00:17:50 2000 +++ diary.h Sat Feb 16 13:43:41 2002 @@ -9,3 +9,6 @@ int diary_export (VirguleReq *vr, xmlNode *root, char *u); + +int +diary_store_entry (VirguleReq *vr, const char *key, const char *entry); diff -ru xmlrpc-methods.c xmlrpc-methods.c --- xmlrpc-methods.c Sat Feb 16 14:17:10 2002 +++ xmlrpc-methods.c Sat Feb 16 13:55:35 2002 @@ -17,7 +17,11 @@ #include "buffer.h" #include "db.h" #include "req.h" +#include "util.h" #include "acct_maint.h" +#include "diary.h" +#include "db_xml.h" +#include "xml_util.h" #include "xmlrpc.h" #include "xmlrpc-methods.h" @@ -46,6 +50,81 @@ } +/* Diary methods + */ +static int +diary_len (VirguleReq *vr, xmlNode *params) +{ + char *user; + const char *key; + int ret; + + ret = xmlrpc_unmarshal_params (vr, params, "s", &user); + if (ret != OK) + return ret; + + key = ap_psprintf (vr->r->pool, "acct/%s/diary", user); + return xmlrpc_response (vr, "i", db_dir_max (vr->db, key) + 1); +} + +static int +diary_get (VirguleReq *vr, xmlNode *params) +{ + char *user; + int index; + const char *key; + xmlDoc *entry; + int ret; + + ret = xmlrpc_unmarshal_params (vr, params, "si", &user, &index); + if (ret != OK) + return ret; + + key = ap_psprintf (vr->r->pool, "acct/%s/diary/_%d", user, index); + entry = db_xml_get (vr->r->pool, vr->db, key); + if (entry == NULL) + return xmlrpc_fault (vr, 1, "entry %d not found", index); + + return xmlrpc_response (vr, "s", xml_get_string_contents (entry->root)); +} + +static int +diary_set (VirguleReq *vr, xmlNode *params) +{ + char *cookie, *entry; + const char *user; + int index, max; + const char *key; + char *error; + int ret; + + ret = xmlrpc_unmarshal_params (vr, params, "sis", &cookie, &index, &entry); + if (ret != OK) + return ret; + ret = xmlrpc_auth_user (vr, cookie); + if (ret != OK) + return ret; + user = vr->u; + + key = ap_psprintf (vr->r->pool, "acct/%s/diary", user); + max = db_dir_max (vr->db, key) + 1; + if (index == -1) + index = max; + if (index < 0 || index > max) + return xmlrpc_fault (vr, 1, "invalid entry key %d", index); + key = ap_psprintf (vr->r->pool, "acct/%s/diary/_%d", user, index); + + entry = nice_htext (vr, entry, &error); + if (error) + return xmlrpc_fault (vr, 1, "%s", error); + ret = diary_store_entry (vr, key, entry); + if (ret) + return xmlrpc_fault (vr, 1, "error storing diary entry"); + + return xmlrpc_response (vr, "i", 1); +} + + /* Simple functions to test the stuff in xmlrpc.c */ @@ -122,6 +201,9 @@ */ xmlrpc_method xmlrpc_method_table[] = { { "authenticate", authenticate }, + { "diary.len", diary_len }, + { "diary.get", diary_get }, + { "diary.set", diary_set }, { "test.guess", test_guess }, { "test.square", test_square }, { "test.sumprod", test_sumprod },