4 * Manage user preferences with a little help from the Citadel server.
12 HashList *PreferenceHooks;
14 typedef struct _PrefDef {
22 typedef struct _Preference {
32 void DestroyPrefDef(void *vPrefDef)
34 PrefDef *Prefdef = (PrefDef*) vPrefDef;
35 FreeStrBuf(&Prefdef->Setting);
36 FreeStrBuf(&Prefdef->OnLoadName);
40 void DestroyPreference(void *vPref)
42 Preference *Pref = (Preference*) vPref;
43 FreeStrBuf(&Pref->Key);
44 FreeStrBuf(&Pref->Val);
45 FreeStrBuf(&Pref->DeQPed);
49 void _RegisterPreference(const char *Setting, long SettingLen,
53 const char *OnLoadName)
55 PrefDef *Newpref = (PrefDef*) malloc(sizeof(PrefDef));
56 Newpref->Setting = NewStrBufPlain(Setting, SettingLen);
57 Newpref->PrefStr = PrefStr;
59 Newpref->OnLoad = OnLoad;
60 if (Newpref->OnLoad != NULL) {
61 Newpref->OnLoadName = NewStrBufPlain(OnLoadName, -1);
64 Newpref->OnLoadName = NULL;
65 Put(PreferenceHooks, Setting, SettingLen, Newpref, DestroyPrefDef);
68 const char *PrefGetLocalStr(const char *Setting, long len)
71 if (GetHash(PreferenceHooks, Setting, len, &hash_value) != 0) {
72 PrefDef *Newpref = (PrefDef*) hash_value;
73 return _(Newpref->PrefStr);
80 inline const char *PrintPref(void *vPref)
82 Preference *Pref = (Preference*) vPref;
83 if (Pref->DeQPed != NULL)
84 return ChrPtr(Pref->DeQPed);
86 return ChrPtr(Pref->Val);
90 void GetPrefTypes(HashList *List)
100 It = GetNewHashPos(List, 0);
101 while (GetNextHashPos(List, It, &len, &Key, &vSetting))
103 Pref = (Preference*) vSetting;
104 if (GetHash(PreferenceHooks, SKEY(Pref->Key), &vPrefDef) &&
107 PrefType = (PrefDef*) vPrefDef;
108 Pref->Type = PrefType;
110 lprintf(1, "Loading [%s]with type [%ld] [\"%s\"]\n",
115 switch (Pref->Type->Type)
121 Pref->lval = StrTol(Pref->Val);
125 Pref->DeQPed = NewStrBufPlain(NULL, StrLength(Pref->Val));
126 StrBufEUid_unescapize(Pref->DeQPed, Pref->Val);
130 Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
135 if (PrefType->OnLoad != NULL){
137 lprintf(1, "Loading with: -> %s(\"%s\", %ld)\n",
138 ChrPtr(PrefType->OnLoadName),
141 PrefType->OnLoad(Pref->Val, Pref->lval);
148 void ParsePref(HashList **List, StrBuf *ReadBuf)
151 Preference *Data = NULL;
152 Preference *LastData = NULL;
154 while (!Done && StrBuf_ServGetln(ReadBuf))
156 if ( (StrLength(ReadBuf)==3) &&
157 !strcmp(ChrPtr(ReadBuf), "000")) {
162 if ((ChrPtr(ReadBuf)[0] == ' ') &&
164 StrBufAppendBuf(Data->Val, ReadBuf, 1);
167 LastData = Data = malloc(sizeof(Preference));
168 memset(Data, 0, sizeof(Preference));
169 Data->Key = NewStrBuf();
170 Data->Val = NewStrBuf();
171 StrBufExtract_token(Data->Key, ReadBuf, 0, '|');
172 StrBufExtract_token(Data->Val, ReadBuf, 1, '|');
173 if (!IsEmptyStr(ChrPtr(Data->Key)))
183 lprintf(1, "ignoring spurious preference line: [%s]\n",
185 DestroyPreference(Data);
195 * display preferences dialog
197 void load_preferences(void)
204 ReadBuf = NewStrBuf();
205 if (goto_config_room(ReadBuf) != 0) {
206 FreeStrBuf(&ReadBuf);
207 return; /* oh well. */
210 serv_puts("MSGS ALL|0|1");
211 StrBuf_ServGetln(ReadBuf);
212 if (GetServerStatus(ReadBuf, NULL) == 8) {
213 serv_puts("subj|__ WebCit Preferences __");
217 StrBuf_ServGetln(ReadBuf)) {
218 if ( (StrLength(ReadBuf)==3) &&
219 !strcmp(ChrPtr(ReadBuf), "000")) {
223 msgnum = StrTol(ReadBuf);
227 serv_printf("MSG0 %ld", msgnum);
228 StrBuf_ServGetln(ReadBuf);
229 if (GetServerStatus(ReadBuf, NULL) == 1) {
230 while (StrBuf_ServGetln(ReadBuf),
231 (strcmp(ChrPtr(ReadBuf), "text") &&
232 strcmp(ChrPtr(ReadBuf), "000"))) {
234 if (!strcmp(ChrPtr(ReadBuf), "text")) {
235 ParsePref(&WCC->hash_prefs, ReadBuf);
240 /* Go back to the room we're supposed to be in */
241 if (StrLength(WCC->wc_roomname) > 0) {
242 serv_printf("GOTO %s", ChrPtr(WCC->wc_roomname));
243 StrBuf_ServGetln(ReadBuf);
244 GetServerStatus(ReadBuf, NULL);
246 FreeStrBuf(&ReadBuf);
250 * \brief Goto the user's configuration room, creating it if necessary.
251 * \return 0 on success or nonzero upon failure.
253 int goto_config_room(StrBuf *Buf)
255 serv_printf("GOTO %s", USERCONFIGROOM);
256 StrBuf_ServGetln(Buf);
257 if (GetServerStatus(Buf, NULL) != 2) { /* try to create the config room if not there */
258 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
259 StrBuf_ServGetln(Buf);
260 GetServerStatus(Buf, NULL);
262 serv_printf("GOTO %s", USERCONFIGROOM);
263 StrBuf_ServGetln(Buf);
264 if (GetServerStatus(Buf, NULL) != 2)
270 void WritePrefsToServer(HashList *Hash)
278 StrBuf *SubBuf = NULL;
280 Hash = WCC->hash_prefs;
281 #ifdef DBG_PREFS_HASH
282 dbg_PrintHash(Hash, PrintPref, NULL);
284 HashPos = GetNewHashPos(Hash, 0);
285 while (GetNextHashPos(Hash, HashPos, &len, &Key, &vPref)!=0)
290 Pref = (Preference*) vPref;
291 nchars = StrLength(Pref->Val);
294 size_t offset, nchars;
296 SubBuf = NewStrBuf();
305 nchars = StrBufSub(SubBuf, Pref->Val, offset, nchars);
308 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(SubBuf));
310 serv_printf(" %s", ChrPtr(SubBuf));
313 nchars = StrLength(Pref->Val) - offset;
319 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(Pref->Val));
323 DeleteHashPos(&HashPos);
327 * \brief save the modifications
329 void save_preferences(void)
336 ReadBuf = NewStrBuf();
337 if (goto_config_room(ReadBuf) != 0) {
338 FreeStrBuf(&ReadBuf);
339 return; /* oh well. */
341 serv_puts("MSGS ALL|0|1");
342 StrBuf_ServGetln(ReadBuf);
343 if (GetServerStatus(ReadBuf, NULL) == 8) {
344 serv_puts("subj|__ WebCit Preferences __");
348 StrBuf_ServGetln(ReadBuf)) {
349 if ( (StrLength(ReadBuf)==3) &&
350 !strcmp(ChrPtr(ReadBuf), "000")) {
354 msgnum = StrTol(ReadBuf);
358 serv_printf("DELE %ld", msgnum);
359 StrBuf_ServGetln(ReadBuf);
360 GetServerStatus(ReadBuf, NULL);
363 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
364 StrBuf_ServGetln(ReadBuf);
365 if (GetServerStatus(ReadBuf, NULL) == 4) {
367 WritePrefsToServer(WCC->hash_prefs);
372 /** Go back to the room we're supposed to be in */
373 if (StrLength(WCC->wc_roomname) > 0) {
374 serv_printf("GOTO %s", ChrPtr(WCC->wc_roomname));
375 StrBuf_ServGetln(ReadBuf);
376 GetServerStatus(ReadBuf, NULL);
378 FreeStrBuf(&ReadBuf);
382 * \brief query the actual setting of key in the citadel database
383 * \param key config key to query
384 * \param keylen length of the key string
385 * \param value StrBuf-value to the key to get
388 int get_pref_backend(const char *key, size_t keylen, Preference **Pref)
390 void *hash_value = NULL;
391 #ifdef DBG_PREFS_HASH
392 dbg_PrintHash(WC->hash_prefs, PrintPref, NULL);
394 if (GetHash(WC->hash_prefs, key, keylen, &hash_value) == 0) {
399 *Pref = (Preference*) hash_value;
404 int get_PREFERENCE(const char *key, size_t keylen, StrBuf **value)
409 Ret = get_pref_backend(key, keylen, &Pref);
418 * \brief Write a key into the webcit preferences database for this user
420 * \params key key whichs value is to be modified
421 * \param keylen length of the key string
422 * \param value value to set
423 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
425 void set_preference_backend(const char *key, size_t keylen,
436 Pref = (Preference*) malloc(sizeof(Preference));
437 memset(Pref, 0, sizeof(Preference));
438 Pref->Key = NewStrBufPlain(key, keylen);
440 if ((PrefType == NULL) &&
441 GetHash(PreferenceHooks, SKEY(Pref->Key), &vPrefDef) &&
443 PrefType = (PrefDef*) vPrefDef;
445 if (PrefType != NULL)
447 Pref->Type = PrefType;
448 if (Pref->Type->Type != lPrefType)
449 lprintf(1, "warning: saving preference with wrong type [%s] %ld != %ld \n",
450 key, Pref->Type->Type, lPrefType);
451 switch (Pref->Type->Type)
460 if (Pref->Val == NULL)
461 Pref->Val = NewStrBufPlain(NULL, 64);
462 StrBufPrintf(Pref->Val, "%ld", lvalue);
466 Pref->DeQPed = value;
467 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
468 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
474 Pref->Val = NewStrBufPlain(HKEY("yes"));
476 Pref->Val = NewStrBufPlain(HKEY("no"));
480 if (Pref->Type->OnLoad != NULL)
481 Pref->Type->OnLoad(Pref->Val, Pref->lval);
493 if (Pref->Val == NULL)
494 Pref->Val = NewStrBufPlain(NULL, 64);
495 StrBufPrintf(Pref->Val, "%ld", lvalue);
499 Pref->DeQPed = value;
500 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
501 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
507 Pref->Val = NewStrBufPlain(HKEY("yes"));
509 Pref->Val = NewStrBufPlain(HKEY("no"));
514 Put(WCC->hash_prefs, key, keylen, Pref, DestroyPreference);
516 if (save_to_server) WCC->SavePrefsToServer = 1;
519 void set_PREFERENCE(const char *key, size_t keylen, StrBuf *value, int save_to_server)
521 set_preference_backend(key, keylen, 0, value, PRF_STRING, save_to_server, NULL);
524 int get_PREF_LONG(const char *key, size_t keylen, long *value, long Default)
529 Ret = get_pref_backend(key, keylen, &Pref);
538 *value = Pref->lval = atol(ChrPtr(Pref->Val));
545 void set_PREF_LONG(const char *key, size_t keylen, long value, int save_to_server)
547 set_preference_backend(key, keylen, value, NULL, PRF_INT, save_to_server, NULL);
550 int get_PREF_YESNO(const char *key, size_t keylen, int *value, int Default)
555 Ret = get_pref_backend(key, keylen, &Pref);
564 *value = Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
570 void set_PREF_YESNO(const char *key, size_t keylen, long value, int save_to_server)
572 set_preference_backend(key, keylen, value, NULL, PRF_YESNO, save_to_server, NULL);
575 int get_room_prefs_backend(const char *key, size_t keylen,
581 pref_name = NewStrBufPlain (HKEY("ROOM:"));
582 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
583 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
584 StrBufAppendBufPlain(pref_name, key, keylen, 0);
585 Ret = get_pref_backend(SKEY(pref_name), Pref);
586 FreeStrBuf(&pref_name);
591 const StrBuf *get_X_PREFS(const char *key, size_t keylen,
592 const char *xkey, size_t xkeylen)
598 pref_name = NewStrBufPlain (HKEY("XPREF:"));
599 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
600 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
601 StrBufAppendBufPlain(pref_name, key, keylen, 0);
603 ret = get_pref_backend(SKEY(pref_name), &Prf);
604 FreeStrBuf(&pref_name);
611 void set_X_PREFS(const char *key, size_t keylen, const char *xkey, size_t xkeylen, StrBuf *value, int save_to_server)
615 pref_name = NewStrBufPlain (HKEY("XPREF:"));
616 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
617 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
618 StrBufAppendBufPlain(pref_name, key, keylen, 0);
620 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
621 FreeStrBuf(&pref_name);
625 StrBuf *get_ROOM_PREFS(const char *key, size_t keylen)
630 Ret = get_room_prefs_backend(key, keylen, &Pref);
639 void set_ROOM_PREFS(const char *key, size_t keylen, StrBuf *value, int save_to_server)
643 pref_name = NewStrBufPlain (HKEY("ROOM:"));
644 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
645 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
646 StrBufAppendBufPlain(pref_name, key, keylen, 0);
647 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
648 FreeStrBuf(&pref_name);
652 void GetPreferences(HashList *Setting)
664 Tmp = WCC->hash_prefs;
665 WCC->hash_prefs = Setting;
667 It = GetNewHashPos(PreferenceHooks, 0);
668 while (GetNextHashPos(PreferenceHooks, It, &len, &Key, &vSetting)) {
669 PrefType = (PrefDef*) vSetting;
671 if (!HaveBstr(SKEY(PrefType->Setting)))
673 switch (PrefType->Type) {
675 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
676 set_preference_backend(SKEY(PrefType->Setting),
684 lval = LBstr(SKEY(PrefType->Setting));
685 set_preference_backend(SKEY(PrefType->Setting),
693 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
694 set_preference_backend(SKEY(PrefType->Setting),
702 lval = YesBstr(SKEY(PrefType->Setting));
703 set_preference_backend(SKEY(PrefType->Setting),
712 WCC->hash_prefs = Tmp;
718 * \brief Commit new preferences and settings
720 void set_preferences(void)
722 if (!havebstr("change_button")) {
723 safestrncpy(WC->ImportantMessage,
724 _("Cancelled. No settings were changed."),
725 sizeof WC->ImportantMessage);
729 GetPreferences(WC->hash_prefs);
734 void tmplput_CFG_Value(StrBuf *Target, WCTemplputParams *TP)
737 if (get_pref_backend(TKEY(0), &Pref))
739 if (Pref->Type == NULL) {
740 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
742 switch (Pref->Type->Type)
745 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
748 if (Pref->decoded != 1) {
749 if (Pref->Val == NULL)
750 Pref->Val = NewStrBufPlain(NULL, 64);
751 StrBufPrintf(Pref->Val, "%ld", Pref->lval);
754 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
757 if (Pref->decoded != 1) {
758 if (Pref->DeQPed == NULL)
759 Pref->DeQPed = NewStrBufPlain(NULL, StrLength(Pref->Val));
761 StrBufEUid_unescapize(Pref->DeQPed, Pref->Val);
764 StrBufAppendTemplate(Target, TP, Pref->DeQPed, 1);
767 if (Pref->decoded != 1) {
768 Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
771 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
777 void tmplput_CFG_Descr(StrBuf *Target, WCTemplputParams *TP)
779 const char *SettingStr;
780 SettingStr = PrefGetLocalStr(TKEY(0));
781 if (SettingStr != NULL)
782 StrBufAppendBufPlain(Target, SettingStr, -1, 0);
784 void tmplput_CFG_RoomValue(StrBuf *Target, WCTemplputParams *TP)
786 StrBuf *pref = get_ROOM_PREFS(TKEY(0));
788 StrBufAppendBuf(Target, pref, 0);
790 int ConditionalHasRoomPreference(StrBuf *Target, WCTemplputParams *TP)
792 if (get_ROOM_PREFS(TP->Tokens->Params[0]->Start,
793 TP->Tokens->Params[0]->len) != NULL)
798 void CfgZoneTempl(StrBuf *TemplBuffer, WCTemplputParams *TP)
800 StrBuf *Zone = (StrBuf*) CTX;
802 SVPutBuf("ZONENAME", Zone, 1);
805 int ConditionalPreference(StrBuf *Target, WCTemplputParams *TP)
809 if (!get_PREFERENCE(TKEY(2), &Pref))
812 if (TP->Tokens->nParameters == 3) {
815 else if (TP->Tokens->Params[3]->Type == TYPE_STR)
816 return ((TP->Tokens->Params[3]->len == StrLength(Pref)) &&
817 (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(Pref)) == 0));
819 return (StrTol(Pref) == TP->Tokens->Params[3]->lvalue);
822 int ConditionalHasPreference(StrBuf *Target, WCTemplputParams *TP)
826 if (!get_PREFERENCE(TKEY(2), &Pref) ||
834 /********************************************************************************
835 * preferences stored discrete in citserver
836 ********************************************************************************/
837 HashList *GetGVEAHash(StrBuf *Target, WCTemplputParams *TP)
840 HashList *List = NULL;
847 StrBuf_ServGetln(Rcp);
848 if (GetServerStatus(Rcp, NULL) == 1) {
850 List = NewHash(1, NULL);
851 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
852 if ( (StrLength(Rcp)==3) &&
853 !strcmp(ChrPtr(Rcp), "000"))
858 i = snprintf(N, sizeof(N), "%d", n);
860 Put(List, N, i, Rcp, HFreeStrBuf);
869 void DeleteGVEAHash(HashList **KillMe)
874 HashList *GetGVSNHash(StrBuf *Target, WCTemplputParams *TP)
877 HashList *List = NULL;
884 StrBuf_ServGetln(Rcp);
885 if (GetServerStatus(Rcp, NULL) == 1) {
887 List = NewHash(1, NULL);
888 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
889 if ( (StrLength(Rcp)==3) &&
890 !strcmp(ChrPtr(Rcp), "000"))
895 i = snprintf(N, sizeof(N), "%d", n);
897 Put(List, N, i, Rcp, HFreeStrBuf);
906 void DeleteGVSNHash(HashList **KillMe)
915 * Offer to make any page the user's "start page."
917 void offer_start_page(StrBuf *Target, WCTemplputParams *TP)
919 wprintf("<a href=\"change_start_page?startpage=");
920 urlescputs(ChrPtr(WC->this_page));
922 wprintf(_("Make this my start page"));
925 wprintf("<br/><a href=\"rss?room=");
926 urlescputs(ChrPtr(WC->wc_roomname));
927 wprintf("\" title=\"RSS 2.0 feed for ");
928 escputs(ChrPtr(WC->wc_roomname));
929 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
935 * Change the user's start page
937 void change_start_page(void)
939 if (!havebstr("startpage")) {
940 set_preference_backend(HKEY("startpage"),
942 NewStrBufPlain(HKEY("")),
946 safestrncpy(WC->ImportantMessage,
947 _("You no longer have a start page selected."),
948 sizeof( WC->ImportantMessage));
953 set_preference_backend(HKEY("startpage"),
955 NewStrBufDup(sbstr("startpage")),
960 output_headers(1, 1, 0, 0, 0, 0);
961 do_template("newstartpage", NULL);
967 InitModule_PREFERENCES
970 WebcitAddUrlHandler(HKEY("set_preferences"), set_preferences, 0);
971 WebcitAddUrlHandler(HKEY("change_start_page"), change_start_page, 0);
974 RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, CTX_NONE);
975 RegisterNamespace("PREF:ROOM:VALUE", 1, 2, tmplput_CFG_RoomValue, CTX_NONE);
976 RegisterNamespace("PREF:VALUE", 1, 2, tmplput_CFG_Value, CTX_NONE);
977 RegisterNamespace("PREF:DESCR", 1, 1, tmplput_CFG_Descr, CTX_NONE);
978 RegisterIterator("PREF:ZONE", 0, ZoneHash, NULL, CfgZoneTempl, NULL, CTX_PREF, CTX_NONE, IT_NOFLAG);
980 RegisterConditional(HKEY("COND:PREF"), 4, ConditionalPreference, CTX_NONE);
981 RegisterConditional(HKEY("COND:PREF:SET"), 4, ConditionalHasPreference, CTX_NONE);
982 RegisterConditional(HKEY("COND:ROOM:SET"), 4, ConditionalHasRoomPreference, CTX_NONE);
984 RegisterIterator("PREF:VALID:EMAIL:ADDR", 0, NULL,
985 GetGVEAHash, NULL, DeleteGVEAHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
986 RegisterIterator("PREF:VALID:EMAIL:NAME", 0, NULL,
987 GetGVSNHash, NULL, DeleteGVSNHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);