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)
203 ReadBuf = NewStrBuf();
204 if (goto_config_room(ReadBuf) != 0) {
205 FreeStrBuf(&ReadBuf);
206 return; /* oh well. */
209 serv_puts("MSGS ALL|0|1");
210 StrBuf_ServGetln(ReadBuf);
211 if (GetServerStatus(ReadBuf, NULL) == 8) {
212 serv_puts("subj|__ WebCit Preferences __");
216 StrBuf_ServGetln(ReadBuf)) {
217 if ( (StrLength(ReadBuf)==3) &&
218 !strcmp(ChrPtr(ReadBuf), "000")) {
222 msgnum = StrTol(ReadBuf);
226 serv_printf("MSG0 %ld", msgnum);
227 StrBuf_ServGetln(ReadBuf);
228 if (GetServerStatus(ReadBuf, NULL) == 1) {
229 while (StrBuf_ServGetln(ReadBuf),
230 (strcmp(ChrPtr(ReadBuf), "text") &&
231 strcmp(ChrPtr(ReadBuf), "000"))) {
233 if (!strcmp(ChrPtr(ReadBuf), "text")) {
234 ParsePref(&WC->hash_prefs, ReadBuf);
239 /* Go back to the room we're supposed to be in */
240 serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
241 StrBuf_ServGetln(ReadBuf);
242 GetServerStatus(ReadBuf, NULL);
243 FreeStrBuf(&ReadBuf);
247 * \brief Goto the user's configuration room, creating it if necessary.
248 * \return 0 on success or nonzero upon failure.
250 int goto_config_room(StrBuf *Buf)
252 serv_printf("GOTO %s", USERCONFIGROOM);
253 StrBuf_ServGetln(Buf);
254 if (GetServerStatus(Buf, NULL) != 2) { /* try to create the config room if not there */
255 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
256 StrBuf_ServGetln(Buf);
257 GetServerStatus(Buf, NULL);
259 serv_printf("GOTO %s", USERCONFIGROOM);
260 StrBuf_ServGetln(Buf);
261 if (GetServerStatus(Buf, NULL) != 2)
267 void WritePrefsToServer(HashList *Hash)
274 StrBuf *SubBuf = NULL;
276 Hash = WC->hash_prefs;
277 #ifdef DBG_PREFS_HASH
278 dbg_PrintHash(Hash, PrintPref, NULL);
280 HashPos = GetNewHashPos(Hash, 0);
281 while (GetNextHashPos(Hash, HashPos, &len, &Key, &vPref)!=0)
286 Pref = (Preference*) vPref;
287 nchars = StrLength(Pref->Val);
290 size_t offset, nchars;
292 SubBuf = NewStrBuf();
301 nchars = StrBufSub(SubBuf, Pref->Val, offset, nchars);
304 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(SubBuf));
306 serv_printf(" %s", ChrPtr(SubBuf));
309 nchars = StrLength(Pref->Val) - offset;
315 serv_printf("%s|%s", ChrPtr(Pref->Key), ChrPtr(Pref->Val));
319 DeleteHashPos(&HashPos);
323 * \brief save the modifications
325 void save_preferences(void)
331 ReadBuf = NewStrBuf();
332 if (goto_config_room(ReadBuf) != 0) {
333 FreeStrBuf(&ReadBuf);
334 return; /* oh well. */
336 serv_puts("MSGS ALL|0|1");
337 StrBuf_ServGetln(ReadBuf);
338 if (GetServerStatus(ReadBuf, NULL) == 8) {
339 serv_puts("subj|__ WebCit Preferences __");
343 StrBuf_ServGetln(ReadBuf)) {
344 if ( (StrLength(ReadBuf)==3) &&
345 !strcmp(ChrPtr(ReadBuf), "000")) {
349 msgnum = StrTol(ReadBuf);
353 serv_printf("DELE %ld", msgnum);
354 StrBuf_ServGetln(ReadBuf);
355 GetServerStatus(ReadBuf, NULL);
358 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
359 StrBuf_ServGetln(ReadBuf);
360 if (GetServerStatus(ReadBuf, NULL) == 4) {
362 WritePrefsToServer(WC->hash_prefs);
367 /** Go back to the room we're supposed to be in */
368 serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
369 StrBuf_ServGetln(ReadBuf);
370 FreeStrBuf(&ReadBuf);
374 * \brief query the actual setting of key in the citadel database
375 * \param key config key to query
376 * \param keylen length of the key string
377 * \param value StrBuf-value to the key to get
380 int get_pref_backend(const char *key, size_t keylen, Preference **Pref)
382 void *hash_value = NULL;
383 #ifdef DBG_PREFS_HASH
384 dbg_PrintHash(WC->hash_prefs, PrintPref, NULL);
386 if (GetHash(WC->hash_prefs, key, keylen, &hash_value) == 0) {
391 *Pref = (Preference*) hash_value;
396 int get_PREFERENCE(const char *key, size_t keylen, StrBuf **value)
401 Ret = get_pref_backend(key, keylen, &Pref);
410 * \brief Write a key into the webcit preferences database for this user
412 * \params key key whichs value is to be modified
413 * \param keylen length of the key string
414 * \param value value to set
415 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
417 void set_preference_backend(const char *key, size_t keylen,
427 Pref = (Preference*) malloc(sizeof(Preference));
428 memset(Pref, 0, sizeof(Preference));
429 Pref->Key = NewStrBufPlain(key, keylen);
431 if ((PrefType == NULL) &&
432 GetHash(PreferenceHooks, SKEY(Pref->Key), &vPrefDef) &&
434 PrefType = (PrefDef*) vPrefDef;
436 if (PrefType != NULL)
438 Pref->Type = PrefType;
439 if (Pref->Type->Type != lPrefType)
440 lprintf(1, "warning: saving preference with wrong type [%s] %ld != %ld \n",
441 key, Pref->Type->Type, lPrefType);
442 switch (Pref->Type->Type)
451 if (Pref->Val == NULL)
452 Pref->Val = NewStrBufPlain(NULL, 64);
453 StrBufPrintf(Pref->Val, "%ld", lvalue);
457 Pref->DeQPed = value;
458 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
459 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
465 Pref->Val = NewStrBufPlain(HKEY("yes"));
467 Pref->Val = NewStrBufPlain(HKEY("no"));
471 if (Pref->Type->OnLoad != NULL)
472 Pref->Type->OnLoad(Pref->Val, Pref->lval);
484 if (Pref->Val == NULL)
485 Pref->Val = NewStrBufPlain(NULL, 64);
486 StrBufPrintf(Pref->Val, "%ld", lvalue);
490 Pref->DeQPed = value;
491 Pref->Val = NewStrBufPlain(NULL, StrLength(Pref->DeQPed) * 3);
492 StrBufEUid_escapize(Pref->Val, Pref->DeQPed);
498 Pref->Val = NewStrBufPlain(HKEY("yes"));
500 Pref->Val = NewStrBufPlain(HKEY("no"));
505 Put(WC->hash_prefs, key, keylen, Pref, DestroyPreference);
507 if (save_to_server) WC->SavePrefsToServer = 1;
510 void set_PREFERENCE(const char *key, size_t keylen, StrBuf *value, int save_to_server)
512 set_preference_backend(key, keylen, 0, value, PRF_STRING, save_to_server, NULL);
515 int get_PREF_LONG(const char *key, size_t keylen, long *value, long Default)
520 Ret = get_pref_backend(key, keylen, &Pref);
529 *value = Pref->lval = atol(ChrPtr(Pref->Val));
536 void set_PREF_LONG(const char *key, size_t keylen, long value, int save_to_server)
538 set_preference_backend(key, keylen, value, NULL, PRF_INT, save_to_server, NULL);
541 int get_PREF_YESNO(const char *key, size_t keylen, int *value, int Default)
546 Ret = get_pref_backend(key, keylen, &Pref);
555 *value = Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
561 void set_PREF_YESNO(const char *key, size_t keylen, long value, int save_to_server)
563 set_preference_backend(key, keylen, value, NULL, PRF_YESNO, save_to_server, NULL);
566 int get_room_prefs_backend(const char *key, size_t keylen,
572 pref_name = NewStrBufPlain (HKEY("ROOM:"));
573 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
574 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
575 StrBufAppendBufPlain(pref_name, key, keylen, 0);
576 Ret = get_pref_backend(SKEY(pref_name), Pref);
577 FreeStrBuf(&pref_name);
582 const StrBuf *get_X_PREFS(const char *key, size_t keylen,
583 const char *xkey, size_t xkeylen)
589 pref_name = NewStrBufPlain (HKEY("XPREF:"));
590 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
591 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
592 StrBufAppendBufPlain(pref_name, key, keylen, 0);
594 ret = get_pref_backend(SKEY(pref_name), &Prf);
595 FreeStrBuf(&pref_name);
602 void set_X_PREFS(const char *key, size_t keylen, const char *xkey, size_t xkeylen, StrBuf *value, int save_to_server)
606 pref_name = NewStrBufPlain (HKEY("XPREF:"));
607 StrBufAppendBufPlain(pref_name, xkey, xkeylen, 0);
608 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
609 StrBufAppendBufPlain(pref_name, key, keylen, 0);
611 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
612 FreeStrBuf(&pref_name);
616 StrBuf *get_ROOM_PREFS(const char *key, size_t keylen)
621 Ret = get_room_prefs_backend(key, keylen, &Pref);
630 void set_ROOM_PREFS(const char *key, size_t keylen, StrBuf *value, int save_to_server)
634 pref_name = NewStrBufPlain (HKEY("ROOM:"));
635 StrBufAppendBuf(pref_name, WC->wc_roomname, 0);
636 StrBufAppendBufPlain(pref_name, HKEY(":"), 0);
637 StrBufAppendBufPlain(pref_name, key, keylen, 0);
638 set_preference_backend(SKEY(pref_name), 0, value, PRF_STRING, save_to_server, NULL);
639 FreeStrBuf(&pref_name);
643 void GetPreferences(HashList *Setting)
655 Tmp = WCC->hash_prefs;
656 WCC->hash_prefs = Setting;
658 It = GetNewHashPos(PreferenceHooks, 0);
659 while (GetNextHashPos(PreferenceHooks, It, &len, &Key, &vSetting)) {
660 PrefType = (PrefDef*) vSetting;
662 if (!HaveBstr(SKEY(PrefType->Setting)))
664 switch (PrefType->Type) {
666 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
667 set_preference_backend(SKEY(PrefType->Setting),
675 lval = LBstr(SKEY(PrefType->Setting));
676 set_preference_backend(SKEY(PrefType->Setting),
684 Buf = NewStrBufDup(SBstr(SKEY(PrefType->Setting)));
685 set_preference_backend(SKEY(PrefType->Setting),
693 lval = YesBstr(SKEY(PrefType->Setting));
694 set_preference_backend(SKEY(PrefType->Setting),
703 WCC->hash_prefs = Tmp;
709 * \brief Commit new preferences and settings
711 void set_preferences(void)
713 if (!havebstr("change_button")) {
714 safestrncpy(WC->ImportantMessage,
715 _("Cancelled. No settings were changed."),
716 sizeof WC->ImportantMessage);
720 GetPreferences(WC->hash_prefs);
725 void tmplput_CFG_Value(StrBuf *Target, WCTemplputParams *TP)
728 if (get_pref_backend(TKEY(0), &Pref))
730 if (Pref->Type == NULL) {
731 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
733 switch (Pref->Type->Type)
736 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
739 if (Pref->decoded != 1) {
740 if (Pref->Val == NULL)
741 Pref->Val = NewStrBufPlain(NULL, 64);
742 StrBufPrintf(Pref->Val, "%ld", Pref->lval);
745 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
748 if (Pref->decoded != 1) {
749 if (Pref->DeQPed == NULL)
750 Pref->DeQPed = NewStrBufPlain(NULL, StrLength(Pref->Val));
752 StrBufEUid_unescapize(Pref->DeQPed, Pref->Val);
755 StrBufAppendTemplate(Target, TP, Pref->DeQPed, 1);
758 if (Pref->decoded != 1) {
759 Pref->lval = strcmp(ChrPtr(Pref->Val), "yes") == 0;
762 StrBufAppendTemplate(Target, TP, Pref->Val, 1);
768 void tmplput_CFG_Descr(StrBuf *Target, WCTemplputParams *TP)
770 const char *SettingStr;
771 SettingStr = PrefGetLocalStr(TKEY(0));
772 if (SettingStr != NULL)
773 StrBufAppendBufPlain(Target, SettingStr, -1, 0);
775 void tmplput_CFG_RoomValue(StrBuf *Target, WCTemplputParams *TP)
777 StrBuf *pref = get_ROOM_PREFS(TKEY(0));
779 StrBufAppendBuf(Target, pref, 0);
781 int ConditionalHasRoomPreference(StrBuf *Target, WCTemplputParams *TP)
783 if (get_ROOM_PREFS(TP->Tokens->Params[0]->Start,
784 TP->Tokens->Params[0]->len) != NULL)
789 void CfgZoneTempl(StrBuf *TemplBuffer, WCTemplputParams *TP)
791 StrBuf *Zone = (StrBuf*) CTX;
793 SVPutBuf("ZONENAME", Zone, 1);
796 int ConditionalPreference(StrBuf *Target, WCTemplputParams *TP)
800 if (!get_PREFERENCE(TKEY(2), &Pref))
803 if (TP->Tokens->nParameters == 3) {
806 else if (TP->Tokens->Params[3]->Type == TYPE_STR)
807 return ((TP->Tokens->Params[3]->len == StrLength(Pref)) &&
808 (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(Pref)) == 0));
810 return (StrTol(Pref) == TP->Tokens->Params[3]->lvalue);
813 int ConditionalHasPreference(StrBuf *Target, WCTemplputParams *TP)
817 if (!get_PREFERENCE(TKEY(2), &Pref) ||
825 /********************************************************************************
826 * preferences stored discrete in citserver
827 ********************************************************************************/
828 HashList *GetGVEAHash(StrBuf *Target, WCTemplputParams *TP)
831 HashList *List = NULL;
838 StrBuf_ServGetln(Rcp);
839 if (GetServerStatus(Rcp, NULL) == 1) {
841 List = NewHash(1, NULL);
842 while (!Done && (StrBuf_ServGetln(Rcp)>=0)) {
843 if ( (StrLength(Rcp)==3) &&
844 !strcmp(ChrPtr(Rcp), "000"))
849 i = snprintf(N, sizeof(N), "%d", n);
851 Put(List, N, i, Rcp, HFreeStrBuf);
860 void DeleteGVEAHash(HashList **KillMe)
865 HashList *GetGVSNHash(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 DeleteGVSNHash(HashList **KillMe)
906 * Offer to make any page the user's "start page."
908 void offer_start_page(StrBuf *Target, WCTemplputParams *TP)
910 wprintf("<a href=\"change_start_page?startpage=");
911 urlescputs(ChrPtr(WC->this_page));
913 wprintf(_("Make this my start page"));
916 wprintf("<br/><a href=\"rss?room=");
917 urlescputs(ChrPtr(WC->wc_roomname));
918 wprintf("\" title=\"RSS 2.0 feed for ");
919 escputs(ChrPtr(WC->wc_roomname));
920 wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
926 * Change the user's start page
928 void change_start_page(void)
930 if (!havebstr("startpage")) {
931 set_preference_backend(HKEY("startpage"),
933 NewStrBufPlain(HKEY("")),
937 safestrncpy(WC->ImportantMessage,
938 _("You no longer have a start page selected."),
939 sizeof( WC->ImportantMessage));
944 set_preference_backend(HKEY("startpage"),
946 NewStrBufDup(sbstr("startpage")),
951 output_headers(1, 1, 0, 0, 0, 0);
952 do_template("newstartpage", NULL);
958 InitModule_PREFERENCES
961 WebcitAddUrlHandler(HKEY("set_preferences"), set_preferences, 0);
962 WebcitAddUrlHandler(HKEY("change_start_page"), change_start_page, 0);
965 RegisterNamespace("OFFERSTARTPAGE", 0, 0, offer_start_page, CTX_NONE);
966 RegisterNamespace("PREF:ROOM:VALUE", 1, 2, tmplput_CFG_RoomValue, CTX_NONE);
967 RegisterNamespace("PREF:VALUE", 1, 2, tmplput_CFG_Value, CTX_NONE);
968 RegisterNamespace("PREF:DESCR", 1, 1, tmplput_CFG_Descr, CTX_NONE);
969 RegisterIterator("PREF:ZONE", 0, ZoneHash, NULL, CfgZoneTempl, NULL, CTX_PREF, CTX_NONE, IT_NOFLAG);
971 RegisterConditional(HKEY("COND:PREF"), 4, ConditionalPreference, CTX_NONE);
972 RegisterConditional(HKEY("COND:PREF:SET"), 4, ConditionalHasPreference, CTX_NONE);
973 RegisterConditional(HKEY("COND:ROOM:SET"), 4, ConditionalHasRoomPreference, CTX_NONE);
975 RegisterIterator("PREF:VALID:EMAIL:ADDR", 0, NULL,
976 GetGVEAHash, NULL, DeleteGVEAHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
977 RegisterIterator("PREF:VALID:EMAIL:NAME", 0, NULL,
978 GetGVSNHash, NULL, DeleteGVSNHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);