nodes.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include "imap4d.h"
  6. /*
  7. * iterated over all of the items in the message set.
  8. * errors are accumulated, but processing continues.
  9. * if uids, then ignore non-existent messages.
  10. * otherwise, that's an error
  11. */
  12. int
  13. forMsgs(Box *box, MsgSet *ms, ulong max, int uids, int (*f)(Box*, Msg*, int, void*), void *rock)
  14. {
  15. Msg *m;
  16. ulong id;
  17. int ok, rok;
  18. ok = 1;
  19. for(; ms != nil; ms = ms->next){
  20. id = ms->from;
  21. rok = 0;
  22. for(m = box->msgs; m != nil && m->seq <= max; m = m->next){
  23. if(!uids && m->seq > id
  24. || uids && m->uid > ms->to)
  25. break;
  26. if(!uids && m->seq == id
  27. || uids && m->uid >= id){
  28. if(!(*f)(box, m, uids, rock))
  29. ok = 0;
  30. if(uids)
  31. id = m->uid;
  32. if(id >= ms->to){
  33. rok = 1;
  34. break;
  35. }
  36. if(ms->to == ~0UL)
  37. rok = 1;
  38. id++;
  39. }
  40. }
  41. if(!uids && !rok)
  42. ok = 0;
  43. }
  44. return ok;
  45. }
  46. Store *
  47. mkStore(int sign, int op, int flags)
  48. {
  49. Store *st;
  50. st = binalloc(&parseBin, sizeof(Store), 1);
  51. if(st == nil)
  52. parseErr("out of memory");
  53. st->sign = sign;
  54. st->op = op;
  55. st->flags = flags;
  56. return st;
  57. }
  58. Fetch *
  59. mkFetch(int op, Fetch *next)
  60. {
  61. Fetch *f;
  62. f = binalloc(&parseBin, sizeof(Fetch), 1);
  63. if(f == nil)
  64. parseErr("out of memory");
  65. f->op = op;
  66. f->next = next;
  67. return f;
  68. }
  69. Fetch*
  70. revFetch(Fetch *f)
  71. {
  72. Fetch *last, *next;
  73. last = nil;
  74. for(; f != nil; f = next){
  75. next = f->next;
  76. f->next = last;
  77. last = f;
  78. }
  79. return last;
  80. }
  81. NList*
  82. mkNList(ulong n, NList *next)
  83. {
  84. NList *nl;
  85. nl = binalloc(&parseBin, sizeof(NList), 0);
  86. if(nl == nil)
  87. parseErr("out of memory");
  88. nl->n = n;
  89. nl->next = next;
  90. return nl;
  91. }
  92. NList*
  93. revNList(NList *nl)
  94. {
  95. NList *last, *next;
  96. last = nil;
  97. for(; nl != nil; nl = next){
  98. next = nl->next;
  99. nl->next = last;
  100. last = nl;
  101. }
  102. return last;
  103. }
  104. SList*
  105. mkSList(char *s, SList *next)
  106. {
  107. SList *sl;
  108. sl = binalloc(&parseBin, sizeof(SList), 0);
  109. if(sl == nil)
  110. parseErr("out of memory");
  111. sl->s = s;
  112. sl->next = next;
  113. return sl;
  114. }
  115. SList*
  116. revSList(SList *sl)
  117. {
  118. SList *last, *next;
  119. last = nil;
  120. for(; sl != nil; sl = next){
  121. next = sl->next;
  122. sl->next = last;
  123. last = sl;
  124. }
  125. return last;
  126. }
  127. int
  128. BNList(Biobuf *b, NList *nl, char *sep)
  129. {
  130. char *s;
  131. int n;
  132. s = "";
  133. n = 0;
  134. for(; nl != nil; nl = nl->next){
  135. n += Bprint(b, "%s%lud", s, nl->n);
  136. s = sep;
  137. }
  138. return n;
  139. }
  140. int
  141. BSList(Biobuf *b, SList *sl, char *sep)
  142. {
  143. char *s;
  144. int n;
  145. s = "";
  146. n = 0;
  147. for(; sl != nil; sl = sl->next){
  148. n += Bprint(b, "%s", s);
  149. n += Bimapstr(b, sl->s);
  150. s = sep;
  151. }
  152. return n;
  153. }
  154. int
  155. Bimapdate(Biobuf *b, Tm *tm)
  156. {
  157. char buf[64];
  158. if(tm == nil)
  159. tm = localtime(time(nil));
  160. imap4date(buf, sizeof(buf), tm);
  161. return Bimapstr(b, buf);
  162. }
  163. int
  164. Brfc822date(Biobuf *b, Tm *tm)
  165. {
  166. char buf[64];
  167. if(tm == nil)
  168. tm = localtime(time(nil));
  169. rfc822date(buf, sizeof(buf), tm);
  170. return Bimapstr(b, buf);
  171. }
  172. int
  173. Bimapstr(Biobuf *b, char *s)
  174. {
  175. char *t;
  176. int c;
  177. if(s == nil)
  178. return Bprint(b, "NIL");
  179. for(t = s; ; t++){
  180. c = *t;
  181. if(c == '\0')
  182. return Bprint(b, "\"%s\"", s);
  183. if(t - s > 64 || c >= 0x7f || strchr("\"\\\r\n", c) != nil)
  184. break;
  185. }
  186. return Bprint(b, "{%lud}\r\n%s", strlen(s), s);
  187. }