/
swad_RSS.c
307 lines (248 loc) · 11 KB
/
swad_RSS.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// swad_RSS.c: RSS feeds
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2024 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************** Headers **********************************/
/*****************************************************************************/
#include <linux/limits.h> // For PATH_MAX
#include <stddef.h> // For NULL
#include <string.h>
#include "swad_autolink.h"
#include "swad_call_for_exam_database.h"
#include "swad_changelog.h"
#include "swad_database.h"
#include "swad_error.h"
#include "swad_global.h"
#include "swad_notice.h"
#include "swad_notice_database.h"
#include "swad_RSS.h"
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
static void RSS_WriteNotices (FILE *FileRSS,struct Hie_Node *Crs);
static void RSS_WriteCallsForExams (FILE *FileRSS,struct Hie_Node *Crs);
/*****************************************************************************/
/******* Update RSS archive with active notices in the current course ********/
/*****************************************************************************/
void RSS_UpdateRSSFileForACrs (struct Hie_Node *Crs)
{
char PathRelPublRSSDir[PATH_MAX + 1];
char PathRelPublRSSFile[PATH_MAX + 1 + NAME_MAX + 1];
FILE *FileRSS;
char RSSLink[WWW_MAX_BYTES_WWW + 1];
struct tm *tm;
time_t t = Dat_GetStartExecutionTimeUTC ();
/***** Create RSS directory if not exists *****/
snprintf (PathRelPublRSSDir,sizeof (PathRelPublRSSDir),"%s/%ld/%s",
Cfg_PATH_CRS_PUBLIC,Crs->HieCod,Cfg_RSS_FOLDER);
Fil_CreateDirIfNotExists (PathRelPublRSSDir);
/***** Create RSS file *****/
snprintf (PathRelPublRSSFile,sizeof (PathRelPublRSSFile),"%s/%s",
PathRelPublRSSDir,Cfg_RSS_FILE);
if ((FileRSS = fopen (PathRelPublRSSFile,"wb")) == NULL)
Err_ShowErrorAndExit ("Can not create RSS file.");
/***** Write RSS header *****/
fprintf (FileRSS,"<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"
"<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n");
/***** Write channel header *****/
fprintf (FileRSS,"<channel>\n");
fprintf (FileRSS,"<atom:link href=\"");
RSS_BuildRSSLink (RSSLink,Crs->HieCod);
fprintf (FileRSS,"%s",RSSLink);
fprintf (FileRSS,"\" rel=\"self\" type=\"application/rss+xml\" />\n");
fprintf (FileRSS,"<title>%s: %s</title>\n",
Cfg_PLATFORM_SHORT_NAME,Crs->ShrtName);
fprintf (FileRSS,"<link>%s/?crs=%ld</link>\n",
Cfg_URL_SWAD_CGI,Crs->HieCod);
fprintf (FileRSS,"<description>%s</description>\n",
Crs->FullName);
fprintf (FileRSS,"<language>%s</language>\n",
Cfg_RSS_LANGUAGE);
fprintf (FileRSS,"<webMaster>%s (%s)</webMaster>\n",
Cfg_PLATFORM_RESPONSIBLE_EMAIL,Cfg_PLATFORM_RESPONSIBLE_NAME);
fprintf (FileRSS,"<image>\n");
fprintf (FileRSS,"<url>%s/swad112x32.png</url>\n",
Cfg_URL_ICON_PUBLIC);
fprintf (FileRSS,"<title>%s: %s</title>\n",
Cfg_PLATFORM_SHORT_NAME,Crs->ShrtName);
fprintf (FileRSS,"<link>%s/?crs=%ld</link>\n",
Cfg_URL_SWAD_CGI,Crs->HieCod);
fprintf (FileRSS,"<width>112</width>\n"
"<height>32</height>\n"
"</image>\n");
// All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred)
fprintf (FileRSS,"<pubDate>");
tm = gmtime (&t);
Dat_WriteRFC822DateFromTM (FileRSS,tm);
fprintf (FileRSS,"</pubDate>\n");
fprintf (FileRSS,"<lastBuildDate>");
tm = gmtime (&t);
Dat_WriteRFC822DateFromTM (FileRSS,tm);
fprintf (FileRSS,"</lastBuildDate>\n");
fprintf (FileRSS,"<generator>%s</generator>\n",
Log_PLATFORM_VERSION);
fprintf (FileRSS,"<docs>http://www.rssboard.org/rss-specification</docs>\n");
/***** Write notices as RSS items *****/
RSS_WriteNotices (FileRSS,Crs);
/***** Write exam announcements as RSS items *****/
RSS_WriteCallsForExams (FileRSS,Crs);
/***** Write channel footer *****/
fprintf (FileRSS,"</channel>\n");
/***** Write RSS footer *****/
fprintf (FileRSS,"</rss>\n");
/***** Close RSS file *****/
fclose (FileRSS);
}
/*****************************************************************************/
/************* Write notices of a course as items of RSS file ****************/
/*****************************************************************************/
static void RSS_WriteNotices (FILE *FileRSS,struct Hie_Node *Crs)
{
extern const char *Txt_Notice;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
struct Usr_Data UsrDat;
struct tm *tm;
time_t CreatTimeUTC;
long NotCod;
unsigned NumNotices;
unsigned NumNot;
char Content[Cns_MAX_BYTES_TEXT + 1];
/***** Write items with active notices *****/
if ((NumNotices = Not_DB_GetActiveNotices (&mysql_res,Crs->HieCod)))
{
Usr_UsrDataConstructor (&UsrDat);
for (NumNot = 0;
NumNot < NumNotices;
NumNot++)
{
/***** Get data of the notice *****/
row = mysql_fetch_row (mysql_res);
/* Get notice code (row[0]) */
if (sscanf (row[0],"%ld",&NotCod) != 1)
Err_WrongNoticeExit ();
/* Get UTC date-time of publication (row[1]) */
CreatTimeUTC = 0L;
if (row[1])
sscanf (row[1],"%ld",&CreatTimeUTC);
/* Get author (row[2]) */
UsrDat.UsrCod = Str_ConvertStrCodToLongCod (row[2]);
Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, // Get author's data from database
Usr_DONT_GET_PREFS,
Usr_DONT_GET_ROLE_IN_CRS);
/***** Write item with notice *****/
fprintf (FileRSS,"<item>\n");
/* Write title (first characters) of the notice */
Str_Copy (Content,row[3],sizeof (Content) - 1);
Str_LimitLengthHTMLStr (Content,40); // Remove when notice has a Subject
fprintf (FileRSS,"<title>%s: ",Txt_Notice);
Str_FilePrintStrChangingBRToRetAndNBSPToSpace (FileRSS,Content);
fprintf (FileRSS,"</title>\n");
/* Write link to the notice */
fprintf (FileRSS,"<link>%s/?crs=%ld</link>\n",
Cfg_URL_SWAD_CGI,Crs->HieCod);
/* Write full content of the notice */
Str_Copy (Content,row[3],sizeof (Content) - 1);
ALn_InsertLinks (Content,Cns_MAX_BYTES_TEXT,40);
fprintf (FileRSS,"<description><![CDATA[<p><em>%s %s %s:</em></p><p>%s</p>]]></description>\n",
UsrDat.FrstName,UsrDat.Surname1,UsrDat.Surname2,Content);
/* Write author */
if (UsrDat.Email[0])
fprintf (FileRSS,"<author>%s (%s %s %s)</author>\n",
UsrDat.Email,UsrDat.FrstName,UsrDat.Surname1,UsrDat.Surname2);
/* Write unique string for this item */
fprintf (FileRSS,"<guid isPermaLink=\"false\">%s, course #%ld, notice #%ld</guid>\n",
Cfg_URL_SWAD_CGI,Crs->HieCod,NotCod);
/* Write publication date */
fprintf (FileRSS,"<pubDate>");
tm = gmtime (&CreatTimeUTC);
Dat_WriteRFC822DateFromTM (FileRSS,tm);
fprintf (FileRSS,"</pubDate>\n");
fprintf (FileRSS,"</item>\n");
}
Usr_UsrDataDestructor (&UsrDat);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/********** Write calls for exams of a course as items of RSS file ***********/
/*****************************************************************************/
static void RSS_WriteCallsForExams (FILE *FileRSS,struct Hie_Node *Crs)
{
extern const char *Txt_Exam;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
struct Usr_Data UsrDat;
struct tm *tm;
time_t CallTimeUTC;
long ExaCod;
unsigned NumExams;
unsigned NumExa;
if (DB_CheckIfDatabaseIsOpen ())
{
/***** Write exam announcements (only future exams) in current course *****/
if ((NumExams = Cfe_DB_GetFutureCallsForExamsInCurrentCrs (&mysql_res)))
{
Usr_UsrDataConstructor (&UsrDat);
for (NumExa = 0;
NumExa < NumExams;
NumExa++)
{
/***** Get data of the call for exam *****/
row = mysql_fetch_row (mysql_res);
/* Get call for exam code (row[0]) */
if (sscanf (row[0],"%ld",&ExaCod) != 1)
Err_WrongCallForExamExit ();
/* Get UTC date-time of publication (row[1]) */
CallTimeUTC = 0L;
if (row[1])
sscanf (row[1],"%ld",&CallTimeUTC);
/***** Write item with exam announcement *****/
fprintf (FileRSS,"<item>\n");
/* Write title (exam date) */
fprintf (FileRSS,"<title>%s: %s</title>\n",Txt_Exam,row[2]);
/* Write link to the notice */
fprintf (FileRSS,"<link>%s/?crs=%ld</link>\n",
Cfg_URL_SWAD_CGI,Crs->HieCod);
/* Write full content of the exam announcement (row[2]) */
fprintf (FileRSS,"<description><![CDATA[<p><em>Fecha examen: %s</em></p>]]></description>\n", // TODO: Need translation!
row[2]);
/* Write unique string for this item */
fprintf (FileRSS,"<guid isPermaLink=\"false\">%s, course #%ld, exam #%ld</guid>\n",
Cfg_URL_SWAD_CGI,Crs->HieCod,ExaCod);
/* Write publication date */
fprintf (FileRSS,"<pubDate>");
tm = gmtime (&CallTimeUTC);
Dat_WriteRFC822DateFromTM (FileRSS,tm);
fprintf (FileRSS,"</pubDate>\n");
fprintf (FileRSS,"</item>\n");
}
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
}
/*****************************************************************************/
/********* Write URL to RSS archive with active notices in a course **********/
/*****************************************************************************/
void RSS_BuildRSSLink (char RSSLink[WWW_MAX_BYTES_WWW + 1],long CrsCod)
{
snprintf (RSSLink,WWW_MAX_BYTES_WWW + 1,"%s/%ld/%s/%s",
Cfg_URL_CRS_PUBLIC,CrsCod,Cfg_RSS_FOLDER,Cfg_RSS_FILE);
}