/trunk/src/post_outbox_info.c

# · C · 279 lines · 199 code · 63 blank · 17 comment · 33 complexity · 0db9fe469dc16e66e4695dd52c4f152c MD5 · raw file

  1. #include <stdio.h>
  2. #include <sys/stat.h> /* for stat() */
  3. #include <time.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "global.h"
  7. #include "manhat-lib/shared_util.h"
  8. #include "manhat-lib/shared_cs_util.h"
  9. #include "manhat-lib/shared_read_infofile.h"
  10. #include "manhat-lib/shared_lock.h"
  11. #include "manhat-lib/shared_system_data.h"
  12. static void
  13. read_parameters (char *course, char *key, long *offset, char *from_prog)
  14. {
  15. char off_set[MAX_FILENAME + 1];
  16. cs_get_required_parameter ("crs", course, MAX_PATH);
  17. cs_get_required_parameter ("id", key, MAX_KEY);
  18. cs_get_required_parameter ("loc", off_set, MAX_FILENAME);
  19. cs_get_required_parameter ("from", from_prog, MAX_PATH);
  20. *offset = atol (off_set);
  21. }
  22. /* reads the outbox file and then the corresponding *.inf file, to get everything
  23. ** ready to be displayed
  24. * */
  25. static void
  26. process_outbox_record (long offset, OUTBOX_RECORD * outbox,
  27. INFO * infofile_info, CONFIG_STRUCT *conf, SESSION *user)
  28. {
  29. char outbox_path[MAX_PATH + 1];
  30. FILE *fp;
  31. snprintf (outbox_path, MAX_PATH + 1, "%s%s/%s/%s/%s",
  32. conf->course_path, PEOPLE_DIR, user->username, MAIL_DIR, OUTBOX_FNAME);
  33. fp = fopen (outbox_path, "r");
  34. if (!fp)
  35. cs_critical_error (ERR_FOPEN_READ_FAILED, "");
  36. get_shared_lock(fileno(fp),1); /* shared_lock.c */
  37. if (fseek (fp, offset, SEEK_SET))
  38. cs_critical_error (ERR_FSEEK_FAILED, "");
  39. if (fread (outbox, sizeof (OUTBOX_RECORD), 1, fp) != 1)
  40. cs_critical_error (ERR_FREAD_FAILED, "");
  41. release_lock(fileno(fp));
  42. fclose (fp);
  43. read_post_infofile (user->username, outbox->info_fname, infofile_info, conf); /* shared_read_infofile.c */
  44. }
  45. static void
  46. set_recipient_info (OUTBOX_RECORD * outbox, INFO * infofile_info)
  47. {
  48. #define MAX_TMP_NAME 30
  49. PERSON_NODE *pptr;
  50. int i =0;
  51. char name[MAX_TMP_NAME];
  52. char timestring[MAX_TIMESTRING + 1], timestring1[MAX_TIMESTRING + 1];
  53. char *escaped_subject = (char *) 0;
  54. if(cgi_html_escape_strfunc(outbox->subject,&escaped_subject) == STATUS_OK)
  55. {
  56. cs_set_value("subject", escaped_subject);
  57. if(escaped_subject)
  58. free(escaped_subject);
  59. }
  60. else
  61. cs_set_value("subject", outbox->subject);
  62. strftime (timestring, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  63. localtime (&outbox->time_sent));
  64. cs_set_value("sent_time", timestring);
  65. /******** To: list ********/
  66. for ( pptr = person_head; pptr; pptr = pptr->next)
  67. {
  68. if (pptr->data.how_received == TO)
  69. {
  70. /* prepare time read */
  71. if (pptr->data.time_read)
  72. {
  73. snprintf(name, MAX_TMP_NAME, "to.%d.time_read", i);
  74. strftime (timestring, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  75. localtime (&pptr->data.time_read));
  76. cs_set_value(name, timestring);
  77. }
  78. /* prepare time deleted
  79. ** the test for 'VERSION24' is to maintain backward compatability
  80. ** after major changes to Post Office in version 2.4 -
  81. ** search for VERSION24 in global.h for more info.
  82. */
  83. if ( (infofile_info->version == VERSION24) && pptr->data.time_deleted)
  84. {
  85. strftime (timestring1, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  86. localtime (&pptr->data.time_deleted));
  87. snprintf(name, MAX_TMP_NAME, "to.%d.time_deleted", i);
  88. cs_set_value(name, timestring1);
  89. }
  90. snprintf(name, MAX_TMP_NAME, "to.%d.realname", i);
  91. cs_set_value(name, pptr->data.realname);
  92. i++;
  93. }
  94. }
  95. /******** CC: list ********/
  96. i =0;
  97. for ( pptr = person_head; pptr; pptr = pptr->next)
  98. {
  99. if (pptr->data.how_received == CC)
  100. {
  101. /* prepare time read */
  102. if (pptr->data.time_read)
  103. {
  104. snprintf(name, MAX_TMP_NAME, "cc.%d.time_read", i);
  105. strftime (timestring, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  106. localtime (&pptr->data.time_read));
  107. cs_set_value(name, timestring);
  108. }
  109. /* prepare time deleted */
  110. if ((infofile_info->version == VERSION24) && pptr->data.time_deleted)
  111. {
  112. strftime (timestring1, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  113. localtime (&pptr->data.time_deleted));
  114. snprintf(name, MAX_TMP_NAME, "cc.%d.time_deleted", i);
  115. cs_set_value(name, timestring1);
  116. }
  117. snprintf(name, MAX_TMP_NAME, "cc.%d.realname", i);
  118. cs_set_value(name, pptr->data.realname);
  119. i++;
  120. }
  121. }
  122. /******** BCC: list ********/
  123. i =0;
  124. for ( pptr = person_head; pptr; pptr = pptr->next)
  125. {
  126. if (pptr->data.how_received == BCC)
  127. {
  128. /* prepare time read */
  129. if (pptr->data.time_read)
  130. {
  131. snprintf(name, MAX_TMP_NAME, "bcc.%d.time_read", i);
  132. strftime (timestring, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  133. localtime (&pptr->data.time_read));
  134. cs_set_value(name, timestring);
  135. }
  136. /* prepare time deleted */
  137. if ((infofile_info->version == VERSION24) && pptr->data.time_deleted)
  138. {
  139. strftime (timestring1, MAX_TIMESTRING, system_data_str("DOW_DATE_TIME_FORMAT"),
  140. localtime (&pptr->data.time_deleted));
  141. snprintf(name, MAX_TMP_NAME, "bcc.%d.time_deleted", i);
  142. cs_set_value(name, timestring1);
  143. }
  144. snprintf(name, MAX_TMP_NAME, "bcc.%d.realname", i);
  145. cs_set_value(name, pptr->data.realname);
  146. i++;
  147. }
  148. }
  149. #undef MAX_TMP_NAME
  150. }
  151. static void
  152. set_attachment_info (INFO * infofile_info, CONFIG_STRUCT *conf, SESSION *user)
  153. {
  154. #define MAX_TMP_NAME 30
  155. ATTACHMENT_NODE *aptr;
  156. struct stat buf;
  157. char mail_dir[MAX_PATH + 1];
  158. char file_path[MAX_PATH + 1];
  159. char name[MAX_TMP_NAME];
  160. char temp[MAX_TMP_NAME];
  161. int i =0;
  162. if (infofile_info->no_attachments == -1)
  163. cs_set_int_value("website_attached", 1);
  164. else if (attachment_head)
  165. {
  166. cs_set_int_value("attachment_count", infofile_info->no_attachments );
  167. snprintf (mail_dir, MAX_PATH + 1, "%s%s/%s/%s/", conf->course_path, PEOPLE_DIR,
  168. user->username, MAIL_DIR);
  169. for (i = 0, aptr = attachment_head; aptr; aptr = aptr->next, i++)
  170. {
  171. snprintf (file_path, MAX_PATH + 1, "%s%s", mail_dir,
  172. aptr->data.unique_fname);
  173. stat (file_path, &buf);
  174. snprintf(name, MAX_TMP_NAME, "attachment.%d.filename", i);
  175. cs_set_value(name, aptr->data.orig_fname);
  176. snprintf(name, MAX_TMP_NAME, "attachment.%d.size", i);
  177. snprintf(temp, MAX_TMP_NAME, "%ld", (long)buf.st_size);
  178. cs_set_value(name, temp);
  179. }
  180. }
  181. #undef MAX_TMP_NAME
  182. }
  183. static void
  184. set_outbox_info (char *course, char *key, long offset, char *from,
  185. CONFIG_STRUCT *conf, SESSION *user)
  186. {
  187. OUTBOX_RECORD outbox;
  188. INFO infofile_info;
  189. cs_set_course_info(conf);
  190. cs_set_current_time();
  191. process_outbox_record (offset, &outbox, &infofile_info, conf, user);
  192. set_recipient_info (&outbox, &infofile_info);
  193. set_attachment_info (&infofile_info, conf, user);
  194. cs_set_int_value("is_outbox", 1);
  195. }
  196. int
  197. main (void)
  198. {
  199. char course[MAX_PATH + 1]; /* from crs=? command line */
  200. char key[MAX_KEY + 1]; /* from id=? command line */
  201. long offset; /* from loc=? command line */
  202. char from_prog[MAX_PATH + 1]; /* from from=? command line */
  203. SESSION user;
  204. CONFIG_STRUCT conf; /* the configuration read from config file */
  205. cs_cgi_init();
  206. read_parameters (course, key, &offset, from_prog);
  207. read_configuration_file (course, &conf); /* shared_util.c */
  208. validate_key (key, &user, &conf); /* shared_util.c */
  209. set_outbox_info (course, key, offset, from_prog, &conf, &user);
  210. free_person_list (); /* shared_read_infofile.c */
  211. free_attachment_list (); /* shared_read_infofile.c */
  212. /** NOTE: same cs file is used for both inbox and outbox info **/
  213. cs_cgi_display("post_inbox_outbox_info", 1);
  214. cs_cgi_destroy();
  215. return 0;
  216. }