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 * Goto the user's configuration room, creating it if necessary.
252 * returns 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) {
272 void WritePrefsToServer(HashList *Hash)
280 StrBuf *SubBuf = NULL;
282 Hash = WCC->hash_prefs;
283 #ifdef DBG_PREFS_HASH
284 dbg_PrintHash(Hash, PrintPref, NULL);
286 HashPos = GetNewHashPos(Hash, 0);
287 while (GetNextHashPos(Hash, HashPos, &len, &Key, &vPref)!=0)
292 Pref = (Preference*) vPref;
293 nchars = StrLength(Pref->Val);
296 size_t offset, nchars;
298 SubBuf = NewStrBufPlain(NULL, SIZ);
307 nchars = StrBufSub(SubBuf, Pref->Val, offset, nchars);
310 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(SubBuf));
312 serv_printf(" %s", ChrPtr(SubBuf));
315 nchars = StrLength(Pref->Val) - offset;
321 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(Pref->Val));
325 DeleteHashPos(&HashPos);
329 * \brief save the modifications
331 void save_preferences(void)
338 ReadBuf = NewStrBuf();
339 if (goto_config_room(ReadBuf) != 0) {
340 FreeStrBuf(&ReadBuf);
341 return; /* oh well. */
343 serv_puts("MSGS ALL|0|1");
344 StrBuf_ServGetln(ReadBuf);
345 if (GetServerStatus(ReadBuf, NULL) == 8) {
346 serv_puts("subj|__ WebCit Preferences __");
350 StrBuf_ServGetln(ReadBuf)) {
351 if ( (StrLength(ReadBuf)==3) &&
352 !strcmp(ChrPtr(ReadBuf), "000")) {
356 msgnum = StrTol(ReadBuf);
360 serv_printf("DELE %ld", msgnum);
361 StrBuf_ServGetln(ReadBuf);
362 GetServerStatus(ReadBuf, NULL);
365 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
366 StrBuf_ServGetln(ReadBuf);
367 if (GetServerStatus(ReadBuf, NULL) == 4) {
369 WritePrefsToServer(WCC->hash_prefs);
374 /** Go back to the room we're supposed to be in */
375 if (StrLength(WCC->wc_roomname) > 0) {
376 serv_printf("GOTO %s", ChrPtr(WCC->wc_roomname));
377 StrBuf_ServGetln(ReadBuf);
378 GetServerStatus(ReadBuf, NULL);
380 FreeStrBuf(&ReadBuf);
384 * \brief query the actual setting of key in the citadel database
385 * \param key config key to query
386 * \param keylen length of the key string
387 * \param value StrBuf-value to the key to get
390 int get_pref_backend(const char *key, size_t keylen, Preference **Pref)
392 void *hash_value = NULL;
393 #ifdef DBG_PREFS_HASH
394 dbg_PrintHash(WC->hash_prefs, PrintPref, NULL);
396 if (GetHash(WC->hash_prefs, key, keylen, &hash_value) == 0) {
401 *Pref = (Preference*) hash_value;
406 int get_PREFERENCE(const char *key, size_t keylen, StrBuf **value)
411 Ret = get_pref_backend(key, keylen, &Pref);
420 * \brief Write a key into the webcit preferences database for this user
422 * \params key key whichs value is to be modified
423 * \param keylen length of the key string
424 * \param value value to set
425 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
427 void set_preference_backend(const char *key, size_t keylen,
438 Pref = (Preference*) malloc(sizeof(Preference));
439 memset(Pref, 0, sizeof(Preference));
440 Pref->Key = NewStrBufPlain(key, keylen);
442 if ((PrefType == NULL) &&
443 GetHash(PreferenceHooks, SKEY(Pref->Key), &vPrefDef) &&
445 PrefType = (PrefDef*) vPrefDef;
447 if (PrefType != NULL)
449 Pref->Type = PrefType;
450 if (Pref->Type->Type != lPrefType)
451 lprintf(1, "warning: saving preference with wrong type [%s] %ld != %ld \n",
452 key, Pref->Type->Type, lPrefType);
453 switch (Pref->Type->Type)
462 if (Pref->Val == NULL)
463 Pref->Val = NewStrBufPlain(NULL, 64);
464 StrBufPrintf(Pref->Val, "%ld", lvalue);
468 Pref->DeQPed = value;
469 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
470 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
476 Pref->Val = NewStrBufPlain(HKEY("yes"));
478 Pref->Val = NewStrBufPlain(HKEY("no"));
482 if (Pref->Type->OnLoad != NULL)
483 Pref->Type->OnLoad(Pref->Val, Pref->lval);
495 if (Pref->Val == NULL)
496 Pref->Val = NewStrBufPlain(NULL, 64);
497 StrBufPrintf(Pref->Val, "%ld", lvalue);
501 Pref->DeQPed = value;
502 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
503 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
509 Pref->Val = NewStrBufPlain(HKEY("yes"));
511 Pref->Val = NewStrBufPlain(HKEY("no"));
516 Put(WCC->hash_prefs, key, keylen, Pref, DestroyPreference);
518 if (save_to_server) WCC->SavePrefsToServer = 1;
521 void set_PREFERENCE(const char *key, size_t keylen, StrBuf *value, int save_to_server)
523 set_preference_backend(key, keylen, 0, value, PRF_STRING, save_to_server, NULL);
526 int get_PREF_LONG(const char *key, size_t keylen, long *value, long Default)
531 Ret = get_pref_backend(key, keylen, &Pref);
540 *value = Pref->lval = atol(ChrPtr(Pref->Val));
547 void set_PREF_LONG(const char *key, size_t keylen, long value, int save_to_server)
549 set_preference_backend(key, keylen, value, NULL, PRF_INT, save_to_server, NULL);
552 int get_PREF_YESNO(const char *key, size_t keylen, int *value, int Default)
557 Ret = get_pref_backend(key, keylen, &Pref);
566 *value = Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
572 void set_PREF_YESNO(const char *key, size_t keylen, long value, int save_to_server)
574 set_preference_backend(key, keylen, value, NULL, PRF_YESNO, save_to_server, NULL);
577 int get_room_prefs_backend(const char *key, size_t keylen,
583 pref_name = NewStrBufPlain (HKEY("ROOM:"));
584 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
585 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
586 StrBufAppendBufPlain(pref_name, key, keylen, 0);
587 Ret = get_pref_backend(SKEY(pref_name), Pref);
588 FreeStrBuf(&pref_name);
593 const StrBuf *get_X_PREFS(const char *key, size_t keylen,
594 const char *xkey, size_t xkeylen)
600 pref_name = NewStrBufPlain (HKEY("XPREF:"));
601 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
602 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
603 StrBufAppendBufPlain(pref_name, key, keylen, 0);
605 ret = get_pref_backend(SKEY(pref_name), &Prf);
606 FreeStrBuf(&pref_name);
613 void set_X_PREFS(const char *key, size_t keylen, const char *xkey, size_t xkeylen, StrBuf *value, int save_to_server)
617 pref_name = NewStrBufPlain (HKEY("XPREF:"));
618 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
619 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
620 StrBufAppendBufPlain(pref_name, key, keylen, 0);
622 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
623 FreeStrBuf(&pref_name);
627 long get_ROOM_PREFS_LONG(const char *key, size_t keylen, long *value, long Default)
632 Ret = get_room_prefs_backend(key, keylen, &Pref);
642 *value = Pref->lval = atol(ChrPtr(Pref->Val));
649 StrBuf *get_ROOM_PREFS(const char *key, size_t keylen)
654 Ret = get_room_prefs_backend(key, keylen, &Pref);
663 void set_ROOM_PREFS(const char *key, size_t keylen, StrBuf *value, int save_to_server)
667 pref_name = NewStrBufPlain (HKEY("ROOM:"));
668 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
669 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
670 StrBufAppendBufPlain(pref_name, key, keylen, 0);
671 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
672 FreeStrBuf(&pref_name);
676 void GetPreferences(HashList *Setting)
688 Tmp = WCC->hash_prefs;
689 WCC->hash_prefs = Setting;
691 It = GetNewHashPos(PreferenceHooks, 0);
692 while (GetNextHashPos(PreferenceHooks, It, &len, &Key, &vSetting)) {
693 PrefType = (PrefDef*) vSetting;
695 if (!HaveBstr(SKEY(PrefType->Setting)))
697 switch (PrefType->Type) {
699 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
700 set_preference_backend(SKEY(PrefType->Setting),
708 lval = LBstr(SKEY(PrefType->Setting));
709 set_preference_backend(SKEY(PrefType->Setting),
717 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
718 set_preference_backend(SKEY(PrefType->Setting),
726 lval = YesBstr(SKEY(PrefType->Setting));
727 set_preference_backend(SKEY(PrefType->Setting),
736 WCC->hash_prefs = Tmp;
742 * \brief Commit new preferences and settings
744 void set_preferences(void)
746 if (!havebstr("change_button")) {
747 safestrncpy(WC->ImportantMessage,
748 _("Cancelled. No settings were changed."),
749 sizeof WC->ImportantMessage);
753 GetPreferences(WC->hash_prefs);
758 void tmplput_CFG_Value(StrBuf *Target, WCTemplputParams *TP)
761 if (get_pref_backend(TKEY(0), &Pref))
763 if (Pref->Type == NULL) {
764 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
766 switch (Pref->Type->Type)
769 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
772 if (Pref->decoded != 1) {
773 if (Pref->Val == NULL)
774 Pref->Val = NewStrBufPlain(NULL, 64);
775 StrBufPrintf(Pref->Val, "%ld", Pref->lval);
778 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
781 if (Pref->decoded != 1) {
782 if (Pref->DeQPed == NULL)
783 Pref->DeQPed = NewStrBufPlain(NULL, StrLength(Pref->Val));
785 StrBufEUid_unescapize(Pref->DeQPed, Pref->Val);
788 StrBufAppendTemplate(Target, TP, Pref->DeQPed, 1);
791 if (Pref->decoded != 1) {
792 Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
795 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
801 void tmplput_CFG_Descr(StrBuf *Target, WCTemplputParams *TP)
803 const char *SettingStr;
804 SettingStr = PrefGetLocalStr(TKEY(0));
805 if (SettingStr != NULL)
806 StrBufAppendBufPlain(Target, SettingStr, -1, 0);
808 void tmplput_CFG_RoomValueLong(StrBuf *Target, WCTemplputParams *TP)
813 if (TP->Tokens->nParameters > 1)
814 defval = GetTemplateTokenNumber(Target, TP, 1, 0);
815 get_ROOM_PREFS_LONG(TKEY(0), &lvalue, defval);
816 StrBufAppendPrintf(Target, "%ld", lvalue);
818 void tmplput_CFG_RoomValue(StrBuf *Target, WCTemplputParams *TP)
820 StrBuf *pref = get_ROOM_PREFS(TKEY(0));
822 StrBufAppendBuf(Target, pref, 0);
824 int ConditionalHasRoomPreference(StrBuf *Target, WCTemplputParams *TP)
826 if (get_ROOM_PREFS(TP->Tokens->Params[0]->Start,
827 TP->Tokens->Params[0]->len) != NULL)
833 int ConditionalPreference(StrBuf *Target, WCTemplputParams *TP)
837 if (!get_PREFERENCE(TKEY(2), &Pref))
840 if (TP->Tokens->nParameters == 3) {
843 else if (TP->Tokens->Params[3]->Type == TYPE_STR)
844 return ((TP->Tokens->Params[3]->len == StrLength(Pref)) &&
845 (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(Pref)) == 0));
847 return (StrTol(Pref) == TP->Tokens->Params[3]->lvalue);
850 int ConditionalHasPreference(StrBuf *Target, WCTemplputParams *TP)
854 if (!get_PREFERENCE(TKEY(2), &Pref) ||
862 /********************************************************************************
863 * preferences stored discrete in citserver
864 ********************************************************************************/
865 HashList *GetGVEAHash(StrBuf *Target, WCTemplputParams *TP)
868 HashList *List = NULL;
875 StrBuf_ServGetln(Rcp);
876 if (GetServerStatus(Rcp, NULL) == 1) {
878 List = NewHash(1, NULL);
879 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
880 if ( (StrLength(Rcp)==3) &&
881 !strcmp(ChrPtr(Rcp), "000"))
886 i = snprintf(N, sizeof(N), "%d", n);
888 Put(List, N, i, Rcp, HFreeStrBuf);
897 void DeleteGVEAHash(HashList **KillMe)
902 HashList *GetGVSNHash(StrBuf *Target, WCTemplputParams *TP)
905 HashList *List = NULL;
912 StrBuf_ServGetln(Rcp);
913 if (GetServerStatus(Rcp, NULL) == 1) {
915 List = NewHash(1, NULL);
916 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
917 if ( (StrLength(Rcp)==3) &&
918 !strcmp(ChrPtr(Rcp), "000"))
923 i = snprintf(N, sizeof(N), "%d", n);
925 Put(List, N, i, Rcp, HFreeStrBuf);
934 void DeleteGVSNHash(HashList **KillMe)
943 * Offer to make any page the user's "start page."
945 void offer_start_page(StrBuf *Target, WCTemplputParams *TP)
947 wprintf("<a href=\"change_start_page?startpage=");
948 urlescputs(ChrPtr(WC->Hdr->this_page));
950 wprintf(_("Make this my start page"));
956 * Change the user's start page
958 void change_start_page(void)
960 if (!havebstr("startpage")) {
961 set_preference_backend(HKEY("startpage"),
963 NewStrBufPlain(HKEY("")),
967 safestrncpy(WC->ImportantMessage,
968 _("You no longer have a start page selected."),
969 sizeof( WC->ImportantMessage));
974 set_preference_backend(HKEY("startpage"),
976 NewStrBufDup(sbstr("startpage")),
981 output_headers(1, 1, 0, 0, 0, 0);
982 do_template("newstartpage", NULL);
987 void LoadStartpage(StrBuf *URL, long lvalue)
990 pch = strchr(ChrPtr(URL), '?');
992 /* purge the sins of the past... */
993 pch = strchr(ChrPtr(URL), '&');
995 StrBufPeek(URL, pch, -1, '?');
996 WC->SavePrefsToServer = 1;
1003 InitModule_PREFERENCES
1006 WebcitAddUrlHandler(HKEY("set_preferences"), "", 0, set_preferences, 0);
1007 WebcitAddUrlHandler(HKEY("change_start_page"), "", 0, change_start_page, 0);
1009 RegisterPreference("startpage", _("Prefered startpage"), PRF_STRING, LoadStartpage);
1011 RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, NULL, CTX_NONE);
1012 RegisterNamespace("PREF:ROOM:VALUE", 1, 2, tmplput_CFG_RoomValue, NULL, CTX_NONE);
1013 RegisterNamespace("PREF:ROOM:VALUE:INT", 1, 2, tmplput_CFG_RoomValueLong, NULL, CTX_NONE);
1014 RegisterNamespace("PREF:VALUE", 1, 2, tmplput_CFG_Value, NULL, CTX_NONE);
1016 RegisterNamespace("PREF:DESCR", 1, 1, tmplput_CFG_Descr, NULL, CTX_NONE);
1018 RegisterConditional(HKEY("COND:PREF"), 4, ConditionalPreference, CTX_NONE);
1019 RegisterConditional(HKEY("COND:PREF:SET"), 4, ConditionalHasPreference, CTX_NONE);
1020 RegisterConditional(HKEY("COND:ROOM:SET"), 4, ConditionalHasRoomPreference, CTX_NONE);
1022 RegisterIterator("PREF:VALID:EMAIL:ADDR", 0, NULL,
1023 GetGVEAHash, NULL, DeleteGVEAHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
1024 RegisterIterator("PREF:VALID:EMAIL:NAME", 0, NULL,
1025 GetGVSNHash, NULL, DeleteGVSNHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
1031 ServerStartModule_PREFERENCES
1034 PreferenceHooks = NewHash(1, NULL);
1040 ServerShutdownModule_PREFERENCES
1043 DeleteHash(&PreferenceHooks);
1047 SessionDetachModule__PREFERENCES
1050 if (sess->SavePrefsToServer) {
1052 sess->SavePrefsToServer = 0;
1057 SessionNewModule_PREFERENCES
1060 sess->hash_prefs = NewHash(1,NULL);
1064 SessionDestroyModule_PREFERENCES
1067 DeleteHash(&sess->hash_prefs);