Article.C 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // Article.C -- Manage newsgroup articles
  3. //
  4. // Copyright 2003 Michael Sweet
  5. // Copyright 2002-2013 Greg Ercolano
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public Licensse as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // 80 //////////////////////////////////////////////////////////////////////////
  22. #include "Article.H"
  23. // RETURN STRING VERSION OF UNSIGNED LONG
  24. static string ultos(unsigned long num)
  25. {
  26. ostringstream buffer;
  27. buffer << num;
  28. return(buffer.str());
  29. }
  30. // TRUNCATE STRING AT FIRST CR|LF
  31. static void TruncateCrlf(char *s)
  32. {
  33. char *ss = strpbrk(s, "\r\n");
  34. if ( ss ) *ss = '\0';
  35. }
  36. // SPLIT RFC822 3.1.2 HEADER INTO KEY/VALUE PAIRS
  37. // If header malformed, key == "".
  38. //
  39. static void SplitKeyValue(char *s, string& key, string& val)
  40. {
  41. key = ""; val = ""; // empty strings first
  42. char *sep = strchr(s, ':');
  43. if ( ! sep ) return; // malformed?
  44. // Parse key
  45. ++sep; // just past ':'
  46. char save = *sep;
  47. *sep = '\0'; // "To: fred" -> "To:\0fred"
  48. key = s; // key="To:"
  49. *sep = save;
  50. // Parse value, skip all leading white (if any)
  51. // eg. if s="Subject: xyz", val will be "xyz", not " xyz"
  52. //
  53. val = sep + strspn(sep, " \t");
  54. }
  55. // CREATE ABSOLUTE PATHNAME TO THE GROUP
  56. // e.g. "/var/spool/news/rush/general/1234"
  57. //
  58. string Article::_ArticlePath(unsigned long num)
  59. {
  60. // rush.general -> rush/general
  61. string pathgroup = group;
  62. replace(pathgroup.begin(), pathgroup.end(), '.', '/');
  63. if ( G_conf.MsgModDirs() )
  64. return(string(G_conf.SpoolDir()) + string("/") + // "/spooldir/fltk/general/1000/1999
  65. pathgroup + string("/") +
  66. ultos((number/1000)*1000) + "/" +
  67. ultos(number));
  68. else
  69. return(string(G_conf.SpoolDir()) + string("/") +
  70. pathgroup + string("/") + ultos(number));
  71. }
  72. // PARSE HEADER INTO CLASS
  73. // Returns -1 if header unknown.
  74. //
  75. int Article::_ParseHeader(string& key, string& val)
  76. {
  77. // fprintf(stderr, "PARSING KEY='%s' VAL='%s'\n", key.c_str(), val.c_str());
  78. // HEADER NAMES ARE CASE INSENSITIVE: INTERNET DRAFT (Son of RFC1036)
  79. const char *s = key.c_str();
  80. if ( strcasecmp(s, "Subject:" ) == 0 )
  81. { subject = val; }
  82. else if ( strcasecmp(s, "From:" ) == 0 )
  83. { from = val; }
  84. else if ( strcasecmp(s, "Date:" ) == 0 )
  85. { date = val; }
  86. else if ( strcasecmp(s, "Xref:" ) == 0 )
  87. { xref = val; }
  88. else if ( strcasecmp(s, "Message-ID:") == 0 )
  89. { messageid = val; }
  90. else if ( strcasecmp(s, "References:") == 0 )
  91. { references = val; }
  92. else if ( strcasecmp(s, "Lines:") == 0 )
  93. { lines = atoi(val.c_str()); }
  94. else
  95. return(-1);
  96. return(0);
  97. }
  98. // LOAD ARTICLE FROM SPECIFIED GROUP
  99. int Article::Load(const char *groupname, unsigned long num)
  100. {
  101. // ZERO OUT FIELDS
  102. //group = ""; // don't clear; parent may call us w/this->group
  103. filename = "";
  104. number = num;
  105. valid = 0; // assume invalid until successful
  106. from = "";
  107. date = "";
  108. messageid = "";
  109. subject = "";
  110. references = "";
  111. xref = "";
  112. bytes = 0;
  113. lines = 0;
  114. errmsg = "";
  115. if ( strlen(groupname) >= GROUP_MAX )
  116. { errmsg = "Group name too long"; return(-1); }
  117. group = groupname;
  118. filename = _ArticlePath(number);
  119. FILE *fp = fopen(filename.c_str(), "r");
  120. if ( fp == NULL )
  121. {
  122. errmsg = string("article ") + ultos(number) +
  123. string(" no longer exists: '") + filename +
  124. string("': ") + string(strerror(errno));
  125. return(-1);
  126. }
  127. // Folding/unfolding of multiline headers
  128. // RFC822 3.1.1 (LONG HEADER FIELDS)
  129. // RFC822 3.4.8 (FOLDING LONG HEADER FIELDS)
  130. //
  131. // LOAD KEY/VALUE PAIRS
  132. int done = 0;
  133. string key, val;
  134. char s[LINE_LEN];
  135. while ( !done && fgets(s, sizeof(s)-1, fp) != NULL )
  136. {
  137. TruncateCrlf(s);
  138. switch ( s[0] )
  139. {
  140. // CONTINUING TO UNFOLD MULTILINE HEADER? (RFC822 3.1.1)
  141. case '\t':
  142. case ' ':
  143. val += (s+1);
  144. if ( val.length() >= FIELD_MAX ) // prevent ram DoS
  145. { val.erase(FIELD_MAX-1, val.length()); } // truncate
  146. continue;
  147. // END OF HEADERS?
  148. case '\0':
  149. if ( key != "" ) // parse previous header, if any
  150. _ParseHeader(key, val);
  151. done = 1;
  152. break;
  153. // NEW HEADER?
  154. default:
  155. if ( key != "" ) // parse previous header, if any
  156. _ParseHeader(key, val);
  157. SplitKeyValue(s, key, val);
  158. break;
  159. }
  160. }
  161. bytes = lseek(fileno(fp), 0, SEEK_END);
  162. fclose(fp);
  163. if ( messageid == "" ) { errmsg = "No 'Message-ID' field"; return(-1); }
  164. if ( from == "" ) { errmsg = "No 'From' field"; return(-1); }
  165. valid = 1;
  166. return(0);
  167. }
  168. // LOAD ARTICLE NUMBER FROM CURRENT GROUP
  169. // Returns -1 on error, errmsg has reason.
  170. //
  171. int Article::Load(unsigned long num)
  172. {
  173. if ( group == "" )
  174. { errmsg = "No group selected"; return(-1); }
  175. return(Load(group.c_str(), num));
  176. }
  177. // SEND ARTICLE TO REMOTE VIA FD
  178. // Returns -1 on error, errmsg has reason.
  179. // head: 1=send header
  180. // body: 1=send body
  181. // If both head and body are 1, separator (blank line)
  182. // is also sent.
  183. //
  184. int Article::SendArticle(int fd, int head, int body)
  185. {
  186. FILE *fp = fopen(filename.c_str(), "r");
  187. if ( fp == NULL )
  188. {
  189. errmsg = "article ";
  190. errmsg.append(ultos(number));
  191. errmsg.append(" no longer exists: ");
  192. errmsg.append(strerror(errno));
  193. return(-1);
  194. }
  195. char s[LINE_LEN];
  196. enum Mode { MODE_HEAD, MODE_SEP, MODE_BODY };
  197. Mode mode = MODE_HEAD;
  198. while ( fgets(s, sizeof(s)-2, fp) )
  199. {
  200. if ( mode == MODE_SEP )
  201. {
  202. // MOVED OFF SEPARATOR INTO BODY
  203. mode = MODE_BODY;
  204. }
  205. else if ( s[0] == '\n' && mode == MODE_HEAD )
  206. {
  207. // END OF HEADER, AND NOT SENDING BODY? DONE
  208. mode = MODE_SEP; // end of header
  209. if ( body == 0 ) { break; } // not sending body? done
  210. }
  211. // LINE TOO LONG? -- TRUNCATE
  212. s[LINE_LEN-4] = '\n';
  213. s[LINE_LEN-3] = 0;
  214. char *ss = strpbrk(s, "\n\r"); // trun on occurance of \n or \r
  215. if ( ss )
  216. {
  217. // TERMINATE WITH CRLF
  218. *ss++ = '\r';
  219. *ss++ = '\n';
  220. *ss = '\0';
  221. }
  222. if ( ( mode == MODE_HEAD && head ) ||
  223. ( mode == MODE_BODY && body ) ||
  224. ( mode == MODE_SEP && head && body ) )
  225. {
  226. write(fd, s, strlen(s));
  227. G_conf.LogMessage(L_DEBUG, "SEND: %s", s);
  228. }
  229. }
  230. fclose(fp);
  231. return(0);
  232. }
  233. int Article::SendHead(int fd)
  234. {
  235. return(SendArticle(fd, 1, 0));
  236. }
  237. int Article::SendBody(int fd)
  238. {
  239. return(SendArticle(fd, 0, 1));
  240. }
  241. // SANITIZE OVERVIEW FIELDS
  242. // RFC 2980 2.8 says tabs in fields must fold to a single space,
  243. // since tabs are field delimiters in XOVER's output.
  244. //
  245. static string SanitizeOverview(string& val)
  246. {
  247. string s = val;
  248. replace(s.begin(), s.end(), '\t', ' ');
  249. return(s);
  250. }
  251. // RETURN NNTP OVERVIEW (RFC2980 2.8 "XOVER")
  252. string Article::Overview(const char *overview[])
  253. {
  254. string reply = ultos(Number());
  255. for ( int r=0; overview[r]; r++ )
  256. {
  257. // HEADER NAMES ARE CASE INSENSITIVE: INTERNET DRAFT (Son of RFC1036)
  258. if (!strcasecmp(overview[r], "Subject:"))
  259. { reply += string("\t") + SanitizeOverview(subject); }
  260. else if (!strcasecmp(overview[r], "From:"))
  261. { reply += string("\t") + SanitizeOverview(from); }
  262. else if (!strcasecmp(overview[r], "Date:"))
  263. { reply += string("\t") + SanitizeOverview(date); }
  264. else if (!strcasecmp(overview[r], "Message-ID:"))
  265. { reply += string("\t") + SanitizeOverview(messageid); }
  266. else if (!strcasecmp(overview[r], "References:"))
  267. { reply += string("\t") + SanitizeOverview(references); }
  268. else if (!strcasecmp(overview[r], "Lines:"))
  269. {
  270. reply += "\t";
  271. if ( lines > 0 )
  272. { reply += ultos(lines); }
  273. }
  274. else if (!strcasecmp(overview[r], "Bytes:"))
  275. { reply += string("\t") + ultos((unsigned long)bytes); }
  276. else if (!strcasecmp(overview[r], "Xref:full"))
  277. {
  278. reply += "\t";
  279. if ( xref != "" )
  280. { reply += string("Xref: ") + SanitizeOverview(xref); }
  281. }
  282. }
  283. return(reply);
  284. }