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;
155 StrBuf_ServGetln(ReadBuf);
156 if ( (StrLength(ReadBuf)==3) &&
157 !strcmp(ChrPtr(ReadBuf), "000")) {
162 if ((ChrPtr(ReadBuf)[0] == ' ') &&
163 (LastData != NULL)) {
164 StrBufAppendBuf(LastData->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);
196 * display preferences dialog
198 void load_preferences(void)
205 ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
206 if (goto_config_room(ReadBuf) != 0) {
207 FreeStrBuf(&ReadBuf);
208 return; /* oh well. */
211 serv_puts("MSGS ALL|0|1");
212 StrBuf_ServGetln(ReadBuf);
213 if (GetServerStatus(ReadBuf, NULL) == 8) {
214 serv_puts("subj|__ WebCit Preferences __");
218 StrBuf_ServGetln(ReadBuf)) {
219 if ( (StrLength(ReadBuf)==3) &&
220 !strcmp(ChrPtr(ReadBuf), "000")) {
224 msgnum = StrTol(ReadBuf);
228 serv_printf("MSG0 %ld", msgnum);
229 StrBuf_ServGetln(ReadBuf);
230 if (GetServerStatus(ReadBuf, NULL) == 1) {
231 while (StrBuf_ServGetln(ReadBuf),
232 (strcmp(ChrPtr(ReadBuf), "text") &&
233 strcmp(ChrPtr(ReadBuf), "000"))) {
235 if (!strcmp(ChrPtr(ReadBuf), "text")) {
236 ParsePref(&WCC->hash_prefs, ReadBuf);
241 /* Go back to the room we're supposed to be in */
242 if (StrLength(WCC->wc_roomname) > 0) {
243 serv_printf("GOTO %s", ChrPtr(WCC->wc_roomname));
244 StrBuf_ServGetln(ReadBuf);
245 GetServerStatus(ReadBuf, NULL);
247 FreeStrBuf(&ReadBuf);
251 * \brief Goto the user's configuration room, creating it if necessary.
252 * \return 0 on success or nonzero upon failure.
254 int goto_config_room(StrBuf *Buf)
256 serv_printf("GOTO %s", USERCONFIGROOM);
257 StrBuf_ServGetln(Buf);
258 if (GetServerStatus(Buf, NULL) != 2) { /* try to create the config room if not there */
259 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
260 StrBuf_ServGetln(Buf);
261 GetServerStatus(Buf, NULL);
263 serv_printf("GOTO %s", USERCONFIGROOM);
264 StrBuf_ServGetln(Buf);
265 if (GetServerStatus(Buf, NULL) != 2)
271 void WritePrefsToServer(HashList *Hash)
279 StrBuf *SubBuf = NULL;
281 Hash = WCC->hash_prefs;
282 #ifdef DBG_PREFS_HASH
283 dbg_PrintHash(Hash, PrintPref, NULL);
285 HashPos = GetNewHashPos(Hash, 0);
286 while (GetNextHashPos(Hash, HashPos, &len, &Key, &vPref)!=0)
291 Pref = (Preference*) vPref;
292 nchars = StrLength(Pref->Val);
295 size_t offset, nchars;
297 SubBuf = NewStrBufPlain(NULL, SIZ);
306 nchars = StrBufSub(SubBuf, Pref->Val, offset, nchars);
309 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(SubBuf));
311 serv_printf(" %s", ChrPtr(SubBuf));
314 nchars = StrLength(Pref->Val) - offset;
320 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(Pref->Val));
324 DeleteHashPos(&HashPos);
328 * \brief save the modifications
330 void save_preferences(void)
337 ReadBuf = NewStrBuf();
338 if (goto_config_room(ReadBuf) != 0) {
339 FreeStrBuf(&ReadBuf);
340 return; /* oh well. */
342 serv_puts("MSGS ALL|0|1");
343 StrBuf_ServGetln(ReadBuf);
344 if (GetServerStatus(ReadBuf, NULL) == 8) {
345 serv_puts("subj|__ WebCit Preferences __");
349 StrBuf_ServGetln(ReadBuf)) {
350 if ( (StrLength(ReadBuf)==3) &&
351 !strcmp(ChrPtr(ReadBuf), "000")) {
355 msgnum = StrTol(ReadBuf);
359 serv_printf("DELE %ld", msgnum);
360 StrBuf_ServGetln(ReadBuf);
361 GetServerStatus(ReadBuf, NULL);
364 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
365 StrBuf_ServGetln(ReadBuf);
366 if (GetServerStatus(ReadBuf, NULL) == 4) {
368 WritePrefsToServer(WCC->hash_prefs);
373 /** Go back to the room we're supposed to be in */
374 if (StrLength(WCC->wc_roomname) > 0) {
375 serv_printf("GOTO %s", ChrPtr(WCC->wc_roomname));
376 StrBuf_ServGetln(ReadBuf);
377 GetServerStatus(ReadBuf, NULL);
379 FreeStrBuf(&ReadBuf);
383 * \brief query the actual setting of key in the citadel database
384 * \param key config key to query
385 * \param keylen length of the key string
386 * \param value StrBuf-value to the key to get
389 int get_pref_backend(const char *key, size_t keylen, Preference **Pref)
391 void *hash_value = NULL;
392 #ifdef DBG_PREFS_HASH
393 dbg_PrintHash(WC->hash_prefs, PrintPref, NULL);
395 if (GetHash(WC->hash_prefs, key, keylen, &hash_value) == 0) {
400 *Pref = (Preference*) hash_value;
405 int get_PREFERENCE(const char *key, size_t keylen, StrBuf **value)
410 Ret = get_pref_backend(key, keylen, &Pref);
419 * \brief Write a key into the webcit preferences database for this user
421 * \params key key whichs value is to be modified
422 * \param keylen length of the key string
423 * \param value value to set
424 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
426 void set_preference_backend(const char *key, size_t keylen,
437 Pref = (Preference*) malloc(sizeof(Preference));
438 memset(Pref, 0, sizeof(Preference));
439 Pref->Key = NewStrBufPlain(key, keylen);
441 if ((PrefType == NULL) &&
442 GetHash(PreferenceHooks, SKEY(Pref->Key), &vPrefDef) &&
444 PrefType = (PrefDef*) vPrefDef;
446 if (PrefType != NULL)
448 Pref->Type = PrefType;
449 if (Pref->Type->Type != lPrefType)
450 lprintf(1, "warning: saving preference with wrong type [%s] %ld != %ld \n",
451 key, Pref->Type->Type, lPrefType);
452 switch (Pref->Type->Type)
461 if (Pref->Val == NULL)
462 Pref->Val = NewStrBufPlain(NULL, 64);
463 StrBufPrintf(Pref->Val, "%ld", lvalue);
467 Pref->DeQPed = value;
468 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
469 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
475 Pref->Val = NewStrBufPlain(HKEY("yes"));
477 Pref->Val = NewStrBufPlain(HKEY("no"));
481 if (Pref->Type->OnLoad != NULL)
482 Pref->Type->OnLoad(Pref->Val, Pref->lval);
494 if (Pref->Val == NULL)
495 Pref->Val = NewStrBufPlain(NULL, 64);
496 StrBufPrintf(Pref->Val, "%ld", lvalue);
500 Pref->DeQPed = value;
501 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
502 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
508 Pref->Val = NewStrBufPlain(HKEY("yes"));
510 Pref->Val = NewStrBufPlain(HKEY("no"));
515 Put(WCC->hash_prefs, key, keylen, Pref, DestroyPreference);
517 if (save_to_server) WCC->SavePrefsToServer = 1;
520 void set_PREFERENCE(const char *key, size_t keylen, StrBuf *value, int save_to_server)
522 set_preference_backend(key, keylen, 0, value, PRF_STRING, save_to_server, NULL);
525 int get_PREF_LONG(const char *key, size_t keylen, long *value, long Default)
530 Ret = get_pref_backend(key, keylen, &Pref);
539 *value = Pref->lval = atol(ChrPtr(Pref->Val));
546 void set_PREF_LONG(const char *key, size_t keylen, long value, int save_to_server)
548 set_preference_backend(key, keylen, value, NULL, PRF_INT, save_to_server, NULL);
551 int get_PREF_YESNO(const char *key, size_t keylen, int *value, int Default)
556 Ret = get_pref_backend(key, keylen, &Pref);
565 *value = Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
571 void set_PREF_YESNO(const char *key, size_t keylen, long value, int save_to_server)
573 set_preference_backend(key, keylen, value, NULL, PRF_YESNO, save_to_server, NULL);
576 int get_room_prefs_backend(const char *key, size_t keylen,
582 pref_name = NewStrBufPlain (HKEY("ROOM:"));
583 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
584 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
585 StrBufAppendBufPlain(pref_name, key, keylen, 0);
586 Ret = get_pref_backend(SKEY(pref_name), Pref);
587 FreeStrBuf(&pref_name);
592 const StrBuf *get_X_PREFS(const char *key, size_t keylen,
593 const char *xkey, size_t xkeylen)
599 pref_name = NewStrBufPlain (HKEY("XPREF:"));
600 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
601 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
602 StrBufAppendBufPlain(pref_name, key, keylen, 0);
604 ret = get_pref_backend(SKEY(pref_name), &Prf);
605 FreeStrBuf(&pref_name);
612 void set_X_PREFS(const char *key, size_t keylen, const char *xkey, size_t xkeylen, StrBuf *value, int save_to_server)
616 pref_name = NewStrBufPlain (HKEY("XPREF:"));
617 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
618 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
619 StrBufAppendBufPlain(pref_name, key, keylen, 0);
621 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
622 FreeStrBuf(&pref_name);
626 StrBuf *get_ROOM_PREFS(const char *key, size_t keylen)
631 Ret = get_room_prefs_backend(key, keylen, &Pref);
640 void set_ROOM_PREFS(const char *key, size_t keylen, StrBuf *value, int save_to_server)
644 pref_name = NewStrBufPlain (HKEY("ROOM:"));
645 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
646 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
647 StrBufAppendBufPlain(pref_name, key, keylen, 0);
648 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
649 FreeStrBuf(&pref_name);
653 void GetPreferences(HashList *Setting)
665 Tmp = WCC->hash_prefs;
666 WCC->hash_prefs = Setting;
668 It = GetNewHashPos(PreferenceHooks, 0);
669 while (GetNextHashPos(PreferenceHooks, It, &len, &Key, &vSetting)) {
670 PrefType = (PrefDef*) vSetting;
672 if (!HaveBstr(SKEY(PrefType->Setting)))
674 switch (PrefType->Type) {
676 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
677 set_preference_backend(SKEY(PrefType->Setting),
685 lval = LBstr(SKEY(PrefType->Setting));
686 set_preference_backend(SKEY(PrefType->Setting),
694 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
695 set_preference_backend(SKEY(PrefType->Setting),
703 lval = YesBstr(SKEY(PrefType->Setting));
704 set_preference_backend(SKEY(PrefType->Setting),
713 WCC->hash_prefs = Tmp;
719 * \brief Commit new preferences and settings
721 void set_preferences(void)
723 if (!havebstr("change_button")) {
724 safestrncpy(WC->ImportantMessage,
725 _("Cancelled. No settings were changed."),
726 sizeof WC->ImportantMessage);
730 GetPreferences(WC->hash_prefs);
735 void tmplput_CFG_Value(StrBuf *Target, WCTemplputParams *TP)
738 if (get_pref_backend(TKEY(0), &Pref))
740 if (Pref->Type == NULL) {
741 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
743 switch (Pref->Type->Type)
746 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
749 if (Pref->decoded != 1) {
750 if (Pref->Val == NULL)
751 Pref->Val = NewStrBufPlain(NULL, 64);
752 StrBufPrintf(Pref->Val, "%ld", Pref->lval);
755 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
758 if (Pref->decoded != 1) {
759 if (Pref->DeQPed == NULL)
760 Pref->DeQPed = NewStrBufPlain(NULL, StrLength(Pref->Val));
762 StrBufEUid_unescapize(Pref->DeQPed, Pref->Val);
765 StrBufAppendTemplate(Target, TP, Pref->DeQPed, 1);
768 if (Pref->decoded != 1) {
769 Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
772 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
778 void tmplput_CFG_Descr(StrBuf *Target, WCTemplputParams *TP)
780 const char *SettingStr;
781 SettingStr = PrefGetLocalStr(TKEY(0));
782 if (SettingStr != NULL)
783 StrBufAppendBufPlain(Target, SettingStr, -1, 0);
785 void tmplput_CFG_RoomValue(StrBuf *Target, WCTemplputParams *TP)
787 StrBuf *pref = get_ROOM_PREFS(TKEY(0));
789 StrBufAppendBuf(Target, pref, 0);
791 int ConditionalHasRoomPreference(StrBuf *Target, WCTemplputParams *TP)
793 if (get_ROOM_PREFS(TP->Tokens->Params[0]->Start,
794 TP->Tokens->Params[0]->len) != NULL)
800 int ConditionalPreference(StrBuf *Target, WCTemplputParams *TP)
804 if (!get_PREFERENCE(TKEY(2), &Pref))
807 if (TP->Tokens->nParameters == 3) {
810 else if (TP->Tokens->Params[3]->Type == TYPE_STR)
811 return ((TP->Tokens->Params[3]->len == StrLength(Pref)) &&
812 (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(Pref)) == 0));
814 return (StrTol(Pref) == TP->Tokens->Params[3]->lvalue);
817 int ConditionalHasPreference(StrBuf *Target, WCTemplputParams *TP)
821 if (!get_PREFERENCE(TKEY(2), &Pref) ||
829 /********************************************************************************
830 * preferences stored discrete in citserver
831 ********************************************************************************/
832 HashList *GetGVEAHash(StrBuf *Target, WCTemplputParams *TP)
835 HashList *List = NULL;
842 StrBuf_ServGetln(Rcp);
843 if (GetServerStatus(Rcp, NULL) == 1) {
845 List = NewHash(1, NULL);
846 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
847 if ( (StrLength(Rcp)==3) &&
848 !strcmp(ChrPtr(Rcp), "000"))
853 i = snprintf(N, sizeof(N), "%d", n);
855 Put(List, N, i, Rcp, HFreeStrBuf);
864 void DeleteGVEAHash(HashList **KillMe)
869 HashList *GetGVSNHash(StrBuf *Target, WCTemplputParams *TP)
872 HashList *List = NULL;
879 StrBuf_ServGetln(Rcp);
880 if (GetServerStatus(Rcp, NULL) == 1) {
882 List = NewHash(1, NULL);
883 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
884 if ( (StrLength(Rcp)==3) &&
885 !strcmp(ChrPtr(Rcp), "000"))
890 i = snprintf(N, sizeof(N), "%d", n);
892 Put(List, N, i, Rcp, HFreeStrBuf);
901 void DeleteGVSNHash(HashList **KillMe)
910 * Offer to make any page the user's "start page."
912 void offer_start_page(StrBuf *Target, WCTemplputParams *TP)
914 wprintf("<a href=\"change_start_page?startpage=");
915 urlescputs(ChrPtr(WC->Hdr->this_page));
917 wprintf(_("Make this my start page"));
920 wprintf("<br/><a href=\"rss?room=");
921 urlescputs(ChrPtr(WC->wc_roomname));
922 wprintf("\" title=\"RSS 2.0 feed for ");
923 escputs(ChrPtr(WC->wc_roomname));
924 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\" width=\"36\" height=\"14\" /></a>\n");
930 * Change the user's start page
932 void change_start_page(void)
934 if (!havebstr("startpage")) {
935 set_preference_backend(HKEY("startpage"),
937 NewStrBufPlain(HKEY("")),
941 safestrncpy(WC->ImportantMessage,
942 _("You no longer have a start page selected."),
943 sizeof( WC->ImportantMessage));
948 set_preference_backend(HKEY("startpage"),
950 NewStrBufDup(sbstr("startpage")),
955 output_headers(1, 1, 0, 0, 0, 0);
956 do_template("newstartpage", NULL);
961 void LoadStartpage(StrBuf *URL, long lvalue)
964 pch = strchr(ChrPtr(URL), '?');
966 /* purge the sins of the past... */
967 pch = strchr(ChrPtr(URL), '&');
969 StrBufPeek(URL, pch, -1, '?');
970 WC->SavePrefsToServer = 1;
977 InitModule_PREFERENCES
980 WebcitAddUrlHandler(HKEY("set_preferences"), set_preferences, 0);
981 WebcitAddUrlHandler(HKEY("change_start_page"), change_start_page, 0);
983 RegisterPreference("startpage", _("Prefered startpage"), PRF_STRING, LoadStartpage);
985 RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, CTX_NONE);
986 RegisterNamespace("PREF:ROOM:VALUE", 1, 2, tmplput_CFG_RoomValue, CTX_NONE);
987 RegisterNamespace("PREF:VALUE", 1, 2, tmplput_CFG_Value, CTX_NONE);
988 RegisterNamespace("PREF:DESCR", 1, 1, tmplput_CFG_Descr, CTX_NONE);
990 RegisterConditional(HKEY("COND:PREF"), 4, ConditionalPreference, CTX_NONE);
991 RegisterConditional(HKEY("COND:PREF:SET"), 4, ConditionalHasPreference, CTX_NONE);
992 RegisterConditional(HKEY("COND:ROOM:SET"), 4, ConditionalHasRoomPreference, CTX_NONE);
994 RegisterIterator("PREF:VALID:EMAIL:ADDR", 0, NULL,
995 GetGVEAHash, NULL, DeleteGVEAHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
996 RegisterIterator("PREF:VALID:EMAIL:NAME", 0, NULL,
997 GetGVSNHash, NULL, DeleteGVSNHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
1003 ServerStartModule_PREFERENCES
1006 PreferenceHooks = NewHash(1, NULL);
1012 ServerShutdownModule_PREFERENCES
1015 DeleteHash(&PreferenceHooks);
1019 SessionDetachModule__PREFERENCES
1022 if (sess->SavePrefsToServer) {
1024 sess->SavePrefsToServer = 0;
1029 SessionNewModule_PREFERENCES
1032 sess->hash_prefs = NewHash(1,NULL);
1036 SessionDestroyModule_PREFERENCES
1039 DeleteHash(&sess->hash_prefs);