summaryrefslogtreecommitdiffstats
path: root/privatemsg.module
blob: 17abe631522f20e515848e0aed5ae6a46d0a0ed4 (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
<?php
// $Id$

define('PRIVATEMSG_FOLDER_RECYCLE_BIN', -1);
define('PRIVATEMSG_FOLDER_INBOX', 0);
define('PRIVATEMSG_FOLDER_SENT', 1);

/**
 * Implementation of hook_help().
 */
function privatemsg_help($section) {
  switch ($section) {
    case 'admin/help#privatemsg':
      $output = '<p>'. t('The private messaging module allows users to send messages to each other without having to share email addresses. An inbox link will appear in the navigation menu. The "write to author" links are included in posts, allowing users to write a private message instead of commenting openly. Allowing users to communicate directly is an important part of building the strength of the community.') .'</p>';
      $output .= '<p>'. t('The contacts list contains only users that you have previously messaged. To contact users not in your list, you need to know their local user name. Administrators can set messaging options such as frequency of emails, message status display, and number of messages to display per page. They can also configure \'Write to Author\' options.') .'</p>';
      $output .= t('<p>You can</p>
<ul>
<li>administer privatemsg at <a href="!admin-settings-privatemsg">administer &gt;&gt; settings &gt;&gt; private message</a>.</li>
<li>view your private messages at <a href="!privatemsg">view inbox</a>.</li>
</ul>', array('!admin-settings-privatemsg' => url('admin/settings/privatemsg'), '!privatemsg' => url('privatemsg')));
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="!privatemsg">Privatemsg page</a>.', array('!privatemsg' => 'http://drupal.org/handbook/modules/privatemsg/')) .'</p>';

      return $output;
  }
}

/**
 * Implementation of hook_menu().
 */
function privatemsg_menu($may_cache) {
  global $user;

  $items = array();
  $access = user_access('access private messages');
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/privatemsg',
      'title' => t('Privatemsg'),
      'description' => t('Configure Privatemsg settings.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('privatemsg_configure'),
      'access' => user_access('administer private messages'),
    );
    $items[] = array(
      'path' => 'privatemsg',
      'title' => t('Private messages'),
      'callback' => 'privatemsg_list',
      'access' => !$user->uid || $access,
      'type' => MENU_SUGGESTED_ITEM,
    );
    $items[] = array(
      'path' => 'privatemsg/list',
      'title' => t('List'),
      'callback' => 'privatemsg_list',
      'callback arguments' => array(NULL),
      'access' => !$user->uid || $access,
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items[] = array(
      'path' => 'privatemsg/new',
      'title' => t('Compose'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('privatemsg_new_form'),
      'access' => $access,
      'type' => MENU_LOCAL_TASK,
      'weight' => -5,
    );
    $items[] = array(
      'path' => 'privatemsg/contacts',
      'title' => t('Contacts'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('privatemsg_contacts_form'),
      'access' => $access,
      'type' => MENU_LOCAL_TASK,
      'weight' => 0,
    );
    $items[] = array(
      'path' => 'privatemsg/folders',
      'title' => t('Manage folders'),
      'callback' => 'privatemsg_manage_folders',
      'access' => user_access('create new folder') && $user->uid,
      'type' => MENU_LOCAL_TASK,
      'weight' => 5,
    );
    $items[] = array(
      'path' => 'privatemsg/folders/movetonew',
      'title' => t('Move to new folder'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('privatemsg_new_folder_form'),
      'access' => user_access('create new folder') && $user->uid,
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'privatemsg/autocomplete',
      'title' => t('Privatemsg autocomplete'),
      'callback' => 'privatemsg_autocomplete',
      'access' => $access,
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'privatemsg/delete',
      'callback' => 'privatemsg_delete',
      'access' => $access && $user->uid, // No guest access
      'type' => MENU_CALLBACK
    );
  }
  else {
    if (!isset($user->privatemsg_allow)) {
      _privatemsg_user_add_defaults($user);
    }
    if (arg(0) == 'privatemsg' && arg(1) == 'view' && intval(arg(2)) > 0) {
      $items[] = array(
        'path' => 'privatemsg/view/'. arg(2),
        'title' => t('Read message'),
        'callback' => 'privatemsg_view',
        'callback arguments' => array(intval(arg(2)), FALSE),
        'access' => $access && $user->uid, // Check access/redirect in callback
        'type' => MENU_CALLBACK,
        'weight' => -10,
      );
      $items[] = array(
        'path' => 'privatemsg/view/'. arg(2) .'/read',
        'title' => t('Read message'),
        'access' => $access && $user->uid, // Check access/redirect in callback
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'weight' => -10,
      );
      $items[] = array(
        'path' => 'privatemsg/view/'. arg(2) .'/back',
        'title' => t('Back to list'),
        'callback' => 'privatemsg_back_to_list',
        'callback arguments' => array(intval(arg(2))),
        'access' => TRUE, // Check access/redirect in callback
        'type' => MENU_LOCAL_TASK,
        'weight' => 0,
      );
    }

    if (arg(0) == 'privatemsg' && arg(1) == 'reply' && intval(arg(2)) > 0) {
      $items[] = array(
        'path' => 'privatemsg/reply/'. arg(2),
        'title' => t('Write a reply'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array('privatemsg_new_form'),
        'access' => $access,
        'type' => MENU_CALLBACK,
      );
    }

    if (arg(0) == 'privatemsg' && arg(1) == 'new' && intval(arg(2)) > 0) {
      $items[] = array(
        'path' => 'privatemsg/new/'. arg(2),
        'title' => t('Write a new message'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array('privatemsg_new_form'),
        'access' => $access,
        'type' => MENU_CALLBACK,
      );
    }
    if ($user-> uid != arg(2) && arg(0) == 'privatemsg' && arg(1) == 'block' && ($account = user_load(array('uid' => arg(2))))) {
      $blocked = privatemsg_user_blocked($account->uid);
      $title_args = array('@user' => $account->name);
      $items[] = array(
        'path' => 'privatemsg/block/'. arg(2),
        'title' => $blocked ? t('Unblock @user', $title_args) : t('Block @user', $title_args),
        'callback' => 'drupal_get_form',
        'callback arguments' => array($blocked ? 'privatemsg_unblock_user_form' : 'privatemsg_block_user_form', $account),
        'type' => MENU_CALLBACK,
      );
    }

    $new = _privatemsg_get_new_messages();
    $items[] = array(
      'path' => 'privatemsg/inbox',
      'title' => variable_get('privatemsg_menu_link', t('My inbox')) . ($new ? ' ('. $new .')' : ''),
      'callback' => 'drupal_goto',
      'callback arguments' => array('privatemsg'),
      'type' => $user->uid && $user->privatemsg_allow ? MENU_DYNAMIC_ITEM : MENU_CALLBACK,
    );

    if ($new && strncmp($_GET['q'], 'privatemsg', 10) && $user->privatemsg_setmessage_notify && user_access('access private messages')) {
      $m = drupal_set_message();
      if (empty($m)) {
        drupal_set_message(strtr(format_plural($new, 'You have a new <a href="!url">private message</a>.', 'You have @count new <a href="!url">private messages</a>.'), array('!url' => url('privatemsg'))));
      }
    }

    if (arg(0) == 'privatemsg' && arg(1) == 'folders' && intval(arg(2)) > 1) {
      $modify_folder = privatemsg_folder_access($user->uid, arg(2));
      if (!$modify_folder) {
        drupal_goto('privatemsg/folders');
      }

      $items[] = array(
        'path' => 'privatemsg/folders/'. arg(2) .'/rename',
        'title' => t('Rename folder'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array('privatemsg_rename_folder_form', arg(2)),
        'access' => $access,
        'type' => MENU_CALLBACK,
      );
      $items[] = array(
        'path' => 'privatemsg/folders/'. arg(2) .'/empty',
        'title' => t('Empty folder?'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array('privatemsg_empty_folder_form', arg(2)),
        'access' => $access,
        'type' => MENU_CALLBACK,
      );
      $items[] = array(
        'path' => 'privatemsg/folders/'. arg(2) .'/delete',
        'title' => t('Delete folder?'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array('privatemsg_delete_folder_form', arg(2)),
        'access' => $access,
        'type' => MENU_CALLBACK,
      );
    }
    if (is_numeric(arg(1)) && (arg(0) == 'privatemsg' || arg(0) == 'user') && ($user->uid == arg(1) || user_access('administer private messages'))) {
      if (arg(0) == 'user' && arg(2) == 'privatemsg') {
        $account = $user->uid == arg(1) ? $user : user_load(array('uid' => arg(1)));
        $items[] = array(
          'path' => 'user/'. arg(1) .'/privatemsg',
          'title' => t('Privatemsg'),
          'callback' => 'privatemsg_list',
          'callback arguments' => array(arg(1)),
          'access' => $account->privatemsg_allow,
          'type' => MENU_LOCAL_TASK,
        );
      }
      if (arg(0) == 'privatemsg') {
        if ($user->uid == arg(1)) {
          $account = $user;
          $title = t('Private messages');
        }
        else {
          $account = user_load(array('uid' => arg(1)));
          $title = t('Private messages for @name', array('@name' => $account->name));
        }

        $items[] = array(
          'path' => 'privatemsg/'. arg(1),
          'title' => $title,
          'callback' => 'privatemsg_list',
          'callback arguments' => array(arg(1)),
          'access' => $account->privatemsg_allow,
          'type' => MENU_CALLBACK,
        );
      }
    }

    drupal_add_css('./'. drupal_get_path('module', 'privatemsg') .'/privatemsg.css');
  }

  return $items;
}

/**
 * Implementation of hook_perm().
 */
function privatemsg_perm() {
  return array('access private messages', 'administer private messages', 'create new folder');
}

/**
 * Implementation of hook_cron().
 */
function privatemsg_cron() {
  // Perform these actions just once per day.
  if (variable_get('privatemsg_last_cron', 0) < (time() - 3600*24)) {
    _privatemsg_prune();
    variable_set('privatemsg_last_cron', time());
  }
}

/**
 * Implementation of hook_link().
 */
function privatemsg_link($type, $node = NULL, $teaser = FALSE) {
  global $user;
  static $access = array();
  $links = array();
  $uid = $node->uid;
  if ($type == 'comment' && $node->nid) {
    $node = node_load($node->nid);
  }
  if (user_access('access private messages') && in_array($teaser ? 'teaser' : $type, variable_get('privatemsg_link_'. $node->type, array())) && $uid != $user->uid && $user->privatemsg_allow) {
    if (!isset($access[$uid])) {
      $author = user_load(array('uid' => $uid));
      $access[$uid] = user_access('access private messages', $author) && $author->uid && $author->privatemsg_allow;
    }
    if ($access[$uid]) {
      $links['privatemsg_write_to_author'] = array(
        'title' => t('Write to author'),
        'href' => 'privatemsg/new/'. $uid,
      );
    }
  }
  return $links;
}

/**
 * Implementation of hook_user().
 */
function privatemsg_user($type, &$edit, &$account, $category = NULL) {
  global $user;

  switch ($type) {
    case 'load':
      _privatemsg_user_add_defaults($account);
      break;
    case 'view':
      if (user_access('access private messages')) {
        if (privatemsg_message_allowed($account->uid)) {
          $return[t('Private messages')][] = array(
            'value' => l(t('Write private message'), 'privatemsg/new/'. $account->uid, array(
             'title' => t('Send private message to @name', array('@name' => $account->name)))),
            'class' => 'send-message');
        }
        if ($account->uid != $user->uid) {
          if (!privatemsg_user_blocked($account->uid) ) {
            $return[t('Private messages')][] = array(
              'value' => l(t('Block messages'), 'privatemsg/block/'. $account->uid, array(
              'title' => t('Block private messages from @name', array('@name' => $account->name)))),
              'class' => 'block-message'
            );
          }
          else {
            $return[t('Private messages')][] = array(
              'value' => l(t('Unblock messages'), 'privatemsg/block/'. $account->uid, array(
              'title' => t('Unblock private messages from @name', array('@name' => $account->name)))),
              'class' => 'unblock-message');
          }
        }

        return $return;
      }
      elseif ($user->uid) {
        return;
      }
      elseif ($account->privatemsg_allow) {
        if (variable_get('user_register', 1)) {
          return array(t('Private messages') => array(array(
            'value' => t('<a href="!login">login</a> or <a href="!register">register</a> to send private messages to this user', array('!login' => url('user/login'), '!register' => url('user/register'))),
            'class' => 'need-login')));
        }
        else {
          return array(t('Private messages') => array(array(
            'value' => t('<a href="!login">login</a> to send private messages to this user', array('!login' => url('user/login'))),
            'class' => 'need-login')));
        }
      }
      break;

    case 'form':
      if (user_access('access private messages') && $category == 'account') {
        $form = array();
        $form['privatemsg_settings'] = array(
            '#type' => 'fieldset',
            '#title' => t('Private message settings'),
            '#weight' => 4,
            '#collapsible' => TRUE
            );
        $form['privatemsg_settings']['privatemsg_allow'] = array(
            '#type' => 'checkbox',
            '#title' => t('Allow private messages'),
            '#default_value' => isset($edit['privatemsg_allow']) ? $edit['privatemsg_allow'] : 1,
            '#description' => t('Check this box to allow users to send you private messages.')
            );
        $form['privatemsg_settings']['privatemsg_setmessage_notify'] = array(
            '#type' => 'checkbox',
            '#title' => t('Aggressive notification of new messages'),
            '#default_value' => isset($edit['privatemsg_setmessage_notify']) ? $edit['privatemsg_setmessage_notify'] : 1,
            '#description' => t('Show status message on every page until new messages are read.')
            );
        return $form;
      }
      break;

    case 'insert':
      if ($welcome_message = trim(variable_get('privatemsg_welcome_message', ''))) {
        $subject = variable_get('privatemsg_welcome_subject', t('Welcome'));
        $format = variable_get('privatemsg_welcome_format', FILTER_FORMAT_DEFAULT);
        $sender = user_load(array('uid' => variable_get('privatemsg_welcome_sender', 1)));
        if ($sender->uid) {
          _privatemsg_send($sender, $account, $subject, $welcome_message, $format);
        }
      }
      break;

    case 'delete':
      db_query('DELETE FROM {privatemsg} WHERE recipient = %d', $account->uid);
      db_query('DELETE FROM {privatemsg_archive} WHERE recipient = %d', $account->uid);
      db_query('DELETE FROM {privatemsg_folder} WHERE uid = %d', $account->uid);
      db_query('UPDATE {privatemsg} SET author = 0 WHERE author = %d', $account->uid);
      db_query('UPDATE {privatemsg_archive} SET author = 0 WHERE author = %d', $account->uid);
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function privatemsg_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'node_type_form':
      $link = variable_get('privatemsg_link_'. $form['#node_type']->type, array());
      $form['workflow']['privatemsg_link'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Private message "Write to author" links'),
        '#weight' => 30,
        'node' => array(
          '#type' => 'checkbox',
          '#title' => t('Link on node'),
          '#default_value' => in_array('node', $link),
        ),
        'teaser' => array(
          '#type' => 'checkbox',
          '#title' => t('Link on teaser'),
          '#default_value' => in_array('teaser', $link),
        ),
        'comment' => array(
          '#type' => module_exists('comment') ? 'checkbox' : 'hidden',
          '#title' => t('Link on comments'),
          '#default_value' => in_array('comment', $link),
        ),
      );
      break;
  }
}

/**
 * Implementation of hook_block().
 */
function privatemsg_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Private messages');
    return $blocks;
  }
  else if ($op == 'view') {
    switch ($delta) {
      case 0:
        return _privatemsg_block_inbox();
    }
  }
}

/**
 * Implementation of hook privatemsg_alter().
 */
function privatemsg_privatemsg_alter($sender = NULL, $recipient = NULL, $subject = NULL, $body = NULL, $format = NULL, $thread = NULL, $type = NULL) {
  $key = _privatemsg_get_type_key($type);
  // use isset() and ! here to determine if the user has actually set the preference to FALSE
  // explicitly - allow message otherwise (if the user has specified no preference)
  if (isset($recipient->$key) && !$recipient->$key) {
    return FALSE;
  }
  return TRUE;
}

function privatemsg_message_allowed($recipient, $author = NULL) {
  global $user;

  $author = $author ? $author : $user->uid;
  $account = user_load(array('uid' => $recipient));
  if (privatemsg_user_blocked($author, $recipient)) {
    return FALSE;
  }
  if (isset($account->privatemsg_allow) && !$account->privatemsg_allow) {
    return FALSE;
  }
  return TRUE;
}

function privatemsg_user_blocked($author, $recipient = NULL) {
  global $user;
  $recipient = ($recipient) ? $recipient : $user->uid;
  return db_result(db_query("SELECT COUNT(*) FROM {privatemsg_block_user} WHERE author = %d AND recipient = %d", $author, $recipient));
}


function privatemsg_block_user($author, $recipient = NULL) {
  global $user;
  $recipient = ($recipient) ? $recipient : $user->uid;
  if (!privatemsg_user_blocked($author, $recipient)) {
    db_query("INSERT INTO {privatemsg_block_user} (author, recipient) VALUES (%d, %d)", $author, $recipient);
    drupal_set_message(t('User has been blocked'));
  }
}

function privatemsg_unblock_user($author, $recipient = NULL) {
  global $user;
  $recipient = ($recipient) ? $recipient : $user->uid;
  if (privatemsg_user_blocked($author, $recipient)) {
    db_query("DELETE FROM {privatemsg_block_user} WHERE author = %d AND recipient = %d", $author, $recipient);
    drupal_set_message(t('User has been unblocked'));
  }
}

function privatemsg_block_user_form($account) {
  $form = array();
  $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  return confirm_form($form, t('Are you sure you want to block !user from sending you any more messages?', array('!user' => $account->name)), 'user/'. $account->uid, '');
}

function privatemsg_block_user_form_submit($form_id, $form_values) {
  privatemsg_block_user($form_values['uid']);
  return 'user/'. $form_values['uid'];
}

function privatemsg_unblock_user_form($account) {
  $form = array();
  $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  return confirm_form($form, t('Are you sure you want to allow !user to send you messages?', array('!user' => $account->name)), 'user/'. $account->uid, '');
}

function privatemsg_unblock_user_form_submit($form_id, $form_values) {
  privatemsg_unblock_user($form_values['uid']);
  return 'user/'. $form_values['uid'];
}

function _privatemsg_block_inbox() {
  global $user;
  if ($user->uid && user_access('access private messages') && $user->privatemsg_allow) {
    $new = _privatemsg_get_new_messages();

    $block = theme('privatemsg_block_inbox', $new);
    return $block;
  }
}

function privatemsg_configure() {
  $form['privatemsg_max_rate'] = array(
    '#type' => 'select',
    '#title' => t('Private messaging max rate'),
    '#default_value' => variable_get('privatemsg_max_rate', 15),
    '#options' => drupal_map_assoc(array(5, 10, 15, 20, 30, 60), 'format_interval'),
    '#description' => t('Max submit rate for private messaging. To prevent abuse.'),
  );
  $form['privatemsg_sent_status'] = array(
    '#type' => 'select',
    '#title' => t('Sent message status'),
    '#default_value' => variable_get('privatemsg_sent_status', 1),
    '#options' => array(t('Disabled'), t('Enabled')),
    '#description' => t('If enabled users can see whether a message has been read or not.'),
  );
  $form['privatemsg_per_page'] = array(
    '#type' => 'select',
    '#title' => t('Messages per page'),
    '#default_value' => variable_get('privatemsg_per_page', 10),
    '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100)),
    '#description' => t('The maximum number of messages displayed per page; links to browse messages automatically appear.'),
  );
  $form['privatemsg_online_status'] = array(
    '#type' => 'select',
    '#title' => t('Show Online Status'),
    '#default_value' => variable_get('privatemsg_online_status', 1),
    '#options' => array('No', 'Yes'),
    '#description' => t('Whether to allow users to see if message author was recently online'),
  );
  $form['privatemsg_menu_link'] = array(
    '#type' => 'textfield',
    '#title' => t('Menu link'),
    '#default_value' => variable_get('privatemsg_menu_link', t('My inbox')),
    '#description' => t("Text shown in link to user's inbox."),
  );
  $options = array(
    '1' => t('Above the message'),
    '2' => t('Below the message'),
    '3' => t('Above and below the message'),
    '4' => t('Neither (show the old links)'),
  );
  $form['privatemsg_view_actions'] = array(
    '#type' => 'radios',
    '#title' => t('Display action form on view screens'),
    '#description' => t('Choose where to display the actions form on the view message screen.'),
    '#options' => $options,
    '#default_value' => variable_get('privatemsg_view_actions', '3'),
  );
  $options = array(
    '1' => t('Before other buttons'),
    '2' => t('After other buttons'),
  );
  $form['privatemsg_actions_loc'] = array(
    '#type' => 'radios',
    '#title' => t('Display delete button'),
    '#description' => t('Choose where to display the actions select on the folder view.'),
    '#options' => $options,
    '#default_value' => variable_get('privatemsg_actions_loc', '1'),
  );

  $form['privatemsg_threaded_view'] = array(
    '#type' => 'radios',
    '#title' => t('How messages are displayed on the detail screen'),
    '#description' => t('You can choose between displaying a thread of messages (like for example on facebook) or just a single message.'),
    '#options' => array(t('Single message'), t('Full thread')),
    '#default_value' => variable_get('privatemsg_threaded_view', 0),
  );


  $form['links'] = array(
    '#type' => 'fieldset',
    '#title' => t('"Write to author" links'),
    '#description' => t('Edit <a href="!settings-url">content types</a> to select where these links are displayed.', array('!settings-url' => url('admin/content/types'))),
  );

  $form['welcome'] = array(
    '#type' => 'fieldset',
    '#title' => t('Welcome message for new users'),
  );
  $form['welcome']['privatemsg_welcome_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => variable_get('privatemsg_welcome_subject', t('Welcome')),
    '#size' => 50,
    '#maxlength' => 64,
  );
  $form['welcome']['privatemsg_welcome_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#default_value' => variable_get('privatemsg_welcome_message', ''),
    '#cols' => 80,
    '#rows' => 5,
    '#description' => t('Leave blank to disable this feature.'),
  );
  $form['welcome']['filterform'] = filter_form(variable_get('privatemsg_welcome_format', FILTER_FORMAT_DEFAULT));
  $sender = user_load(array('uid' => variable_get('privatemsg_welcome_sender', 1)));
  $form['welcome']['privatemsg_welcome_sender'] = array(
    '#type' => 'textfield',
    '#title' => t('Sender'),
    '#default_value' => $sender->name,
    '#autocomplete_path' => 'privatemsg/autocomplete',
    '#size' => 50,
    '#maxlength' => 64,
    '#required' => TRUE,
  );
  $form['admin_default'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default user settings'),
    '#description' => 'Set the default values for user settings. These can be changed by each user.',
    '#collapsible' => TRUE,
  );

  $form['admin_default']['privatemsg_default_allow'] = array(
    '#type' => 'checkbox',
    '#title' => t("'Allow private messages' default"),
    '#default_value' => variable_get('privatemsg_default_allow', 1),
    '#description' => t("Check this box to set the default value of the 'Allow private messages' user setting."),
  );
  $form['admin_default']['privatemsg_default_setmessage_notify'] = array(
    '#type' => 'checkbox',
    '#title' => t("'Aggressive notification of new messages' default"),
    '#default_value' => variable_get('privatemsg_default_setmessage_notify', 1),
    '#description' => t("Check this box to set the default value of the 'Aggressive notification of new messages' user setting."),
  );
  return system_settings_form($form);
}

function privatemsg_configure_validate($form_id, $form_values, $form) {
  if (!empty($form_values['privatemsg_welcome_sender'])) {
    $sender = user_load(array('name' => $form_values['privatemsg_welcome_sender']));
    if ($sender->uid) {
      form_set_value($form['welcome']['privatemsg_welcome_sender'], $sender->uid);
    }
    else {
      form_set_error('privatemsg_welcome_sender', t('The <em>Sender</em> does not exist.'));
    }
  }
}

function privatemsg_list($uid = NULL) {
  global $user;

  if (!empty($uid) && is_numeric($uid) && user_access('administer private messages')) {
    $account = user_load(array('uid' => $uid));
  }
  else {
    $account = $user;
  }
  // Notification emails link here, but the user may not be logged in yet.
  if ($account && !$account->uid) {
    drupal_goto('user/login', 'destination=privatemsg');
  }
  // wha? Disallow users who have set their pref to "don't contact me" from
  // seeing their inbox? Surely not: privatemsg_allow only determines whether a
  // user receives direct private messages, there are still other types of
  // messages that arrive in a user's inbox.
  if (!user_access('access private messages')) {// || !$account->privatemsg_allow) {
    return drupal_access_denied();
  }

  $args = array($account->uid);
  if (isset($_SESSION['privatemsg_type'])) {
    $type = " AND type = '%s'";
    $args[] = $_SESSION['privatemsg_type'];
  }
  else {
    $type = '';
  }

  $current_folder = intval(arg(2));
  $folder_name = _privatemsg_folder_map($current_folder, $account->uid);
  $count_query = NULL;
  if ($current_folder == PRIVATEMSG_FOLDER_SENT) {
    $where_field = 'author';
    $join_field = 'recipient';
  }
  else {
    $where_field = 'recipient';
    $join_field = 'author';
  }
  $sql1 = "SELECT id, subject, p.timestamp, u.uid, u.name, newmsg, type FROM {privatemsg} p INNER JOIN {users} u ON ";
  $sql = $sql1 ."p.$join_field = u.uid WHERE p.$where_field = %d $type AND p.{$where_field}_del = %d";
  if ($current_folder == PRIVATEMSG_FOLDER_RECYCLE_BIN) {
    $args[] = 1;
    $sql .= " UNION $sql1 p.recipient = u.uid WHERE p.author = %d $type AND p.author_del = %d";
    $args = array_merge($args, $args);
    $count_query = "SELECT (SELECT COUNT(*) FROM {privatemsg} p INNER JOIN {users} u ON p.author = u.uid WHERE p.recipient = %d AND p.recipient_del = %d) + (SELECT COUNT(*) FROM {privatemsg} p INNER JOIN {users} u ON p.recipient = u.uid WHERE p.author = %d AND p.author_del = %d)";
  }
  else {
    $args[] = 0;
    if ($current_folder != PRIVATEMSG_FOLDER_SENT) {
      $args[] = $current_folder;
      $sql .= " AND folder = %d";
    }
  }

  $header = _privatemsg_list_header($current_folder);
  $ts = tablesort_init($header);
  $_SESSION['privatemsg_list_sql'] = array(
    'sql' => $sql,
    'args' => $args,
    'field' => db_escape_string($ts['sql']),
    'sort' =>  drupal_strtoupper(db_escape_string($ts['sort'])),
  );
  $result = pager_query($sql . tablesort_sql($header), variable_get('privatemsg_per_page', 10), 0, $count_query, $args);
  $messages = array();
  while ($message = db_fetch_object($result)) {
    if ($current_folder == PRIVATEMSG_FOLDER_SENT) {
      $message->newmsg = 0;
    }
    $messages[] = $message;
  }

  $folders = privatemsg_load_folders($account->uid);

  $output = drupal_get_form('privatemsg_cur_folder_form', $folders, $current_folder);
  $output .= drupal_get_form('privatemsg_list_form', $messages, $folders, $current_folder, $account);

  return $output;
}

function _privatemsg_folder_map($fid = NULL, $uid = NULL) {
  $map = array(
    PRIVATEMSG_FOLDER_SENT => t('Sent messages'),
    PRIVATEMSG_FOLDER_INBOX => t('Inbox'),
    PRIVATEMSG_FOLDER_RECYCLE_BIN => t('Recycle bin'),
  );
  if (!isset($fid)) {
    return $map;
  }
  return isset($map[$fid]) ? $map[$fid] : db_result(db_query('SELECT name FROM {privatemsg_folder} WHERE fid = %d AND uid = %d', $current_folder, $uid));
}

function _privatemsg_list_header($current_folder) {
  return array(
    array(),
    array('data' => ($current_folder == 1) ? t('To') : t('From'), 'field' => 'name'),
    array('data' => t('Subject'), 'field' => 'subject'),
    array('data' => t('Date'), 'field' => 'timestamp', 'sort' => 'desc'),
  );
}

function privatemsg_cur_folder_form($folders, $current_folder) {
  global $user;

  foreach ($folders as $folder) {
    $options[$folder['fid']] = $folder['name'];
  }

  $form['header'] = array(
    '#type' => 'fieldset',
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['header']['folder_select'] = array(
    '#type' => 'select',
    '#title' => t('Current folder'),
    '#options' => $options,
    '#default_value' => $current_folder,
    '#attributes' => array('class' => 'pm-add-folder-select'),
  );
  $form['header']['go_folder'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#attributes' => array('class' => 'js-hide'),
  );
  $actor = $current_folder == 1 ? 'author' : 'recipient';
  $result = db_query("SELECT DISTINCT type FROM {privatemsg} WHERE folder = %d AND {$actor}_del = 0 AND {$actor} = %d", $current_folder, $user->uid);
  $types = array(
    '_all' => t('All'),
    '_read' => t('Read'),
    '_unread' => t('Unread'),
    '_none' => t('None'),
    '_invert' => t('Invert'),
    '_privatemsg_delimiter' => '----------------',
    'all types' => t('All types'),
  );
  $filter_types = array('all types' => t('All types'));
  while ($t = db_fetch_object($result)) {
    $type = check_plain($t->type);
    $types[$type] = $type;
    $filter_types[$type] = $type;
  }
  $form['header']['type'] = array(
    '#type' => 'select',
    '#title' => t('Select'),
    '#options' => $types,
    '#attributes' => array('class' => 'pm-filter-select'),
    '#default_value' => '_privatemsg_delimiter',
  );
  $form['header']['filter_type'] = array(
    '#type' => 'select',
    '#title' => t('Filter'),
    '#options' => $filter_types,
    '#default_value' => isset($_SESSION['privatemsg_type']) ? $_SESSION['privatemsg_type'] : 'all types',
  );
  if (isset($_SESSION['privatemsg_type'])) {
    $form['header']['type']['#default_value'] = $_SESSION['privatemsg_type'];
  }
  $form['header']['type_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
    '#attributes' => array('class' => 'js-hide'),
  );

  return $form;
}

function privatemsg_cur_folder_form_submit($form_id, $form_values) {
  switch ($form_values['op']) {
    case t('Go'):
      $redirect = 'privatemsg';
      if ($form_values['folder_select']) {
        $redirect .= '/'. (is_numeric(arg(1)) ? arg(1) : 'list');
        $redirect .= '/'. $form_values['folder_select'];
      }
      unset($_SESSION['privatemsg_type']);
      return $redirect;
    case t('Filter'):
      $type = $form_values['filter_type'];
      // all other '_' will be handled by JS.
      if ($type[0] != '_') {
        if ($type == 'all types') {
          unset($_SESSION['privatemsg_type']);
        }
        else {
          $_SESSION['privatemsg_type'] = $form_values['filter_type'];
        }
      }
      break;
  }
}

function privatemsg_list_form($messages, $folders, $current_folder, $account) {
  global $user;

  $form['account'] = array('#type' => 'value', '#value' => $account);

  $form['messages'] = array(
    '#theme' => 'privatemsg_message_table',
    '#tree' => TRUE
  );
  $form['messages']['current_folder'] = array(
    '#type' => 'value',
    '#value' => $current_folder
  );
  foreach ($messages as $message) {
    if ($current_folder != 1) {
      $new = $message->newmsg;
      $new_text = theme('mark');
    }
    else {
      $new = variable_get('privatemsg_sent_status', 1) ? $message->newmsg : 0;
      $new_text = ' <span class="marker">'. t('unread') .'</span>';
    }

    $form['messages'][$message->id]['message'] = array(
      '#type' => 'value',
      '#value' => $message,
    );

    $form['messages'][$message->id]['#new'] = $new;

    $form['messages'][$message->id]['selected'] = array(
      '#type' => 'checkbox',
    );
    $form['messages'][$message->id]['avatar'] = array(
      '#value' => '<div class="pm-inbox-avatar">'. theme('privatemsg_user_picture', user_load(array('uid' => $message->uid))).'</div>',
    );
    $form['messages'][$message->id]['user'] = array(
      '#type' => 'value',
      '#value' => '<div class="pm-username">'. theme('privatemsg_username', $message) .'</div>',
    );
    $form['messages'][$message->id]['subject'] = array(
      '#type' => 'value',
      '#value' => l($message->subject, 'privatemsg/view/'. $message->id, array(), NULL, 'message-'. $message->id) . ($new ? (' '. $new_text) : ''),
    );
    $form['messages'][$message->id]['date'] = array(
      '#type' => 'value',
      '#value' => format_date($message->timestamp, 'small'),
    );
  }
  $form['pager'] = array(
    '#value' => theme('pager', array(), variable_get('privatemsg_per_page', 10)),
  );

  if (count($messages) > 0) {
    $js = array('deleteMessage' => t('Are you sure you want to delete these messages?'));
    $del = variable_get('privatemsg_actions_loc', 1);
    $form['selected'] = array(
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
       '#prefix' => '<div class="container-inline" id="privatemsg-action-buttons">',
      '#suffix' => '</div>',
    );
    switch ($current_folder) {
      case PRIVATEMSG_FOLDER_RECYCLE_BIN:
        $actions['permanent'] = array(
          '#type' => 'submit',
          '#value' => t('Delete permanently'),
        );
        $actions['undelete'] = array(
          '#type' => 'submit',
          '#value' => t('Restore'),
        );
        $actions['empty'] = array(
          '#type' => 'submit',
          '#value' => t('Empty recycle bin'),
        );
        break;
      default:
        $actions['mark_read'] = array(
          '#type' => 'submit',
          '#value' => t('Mark as read'),
        );
        $actions['mark_unread'] = array(
          '#type' => 'submit',
          '#value' => t('Mark as unread'),
        );
        // deliberate no break.
      case PRIVATEMSG_FOLDER_SENT:
        $actions['delete'] = array(
          '#type' => 'submit',
          '#value' => t('Delete'),
        );
    }

    if ($del == 1) {
      $form['selected']['actions'] = $actions;
    }

    if (user_access('create new folder')) {
      $extra_folders = array();
      foreach ($folders as $folder) {
        // Do not display the recycle bin in the move to folder drop down as the
        // delete button is already available. Similarly, avoid displaying
        // the sent items folder as it is for internal use only.
        if ($folder['fid'] != PRIVATEMSG_FOLDER_SENT && $folder['fid'] != PRIVATEMSG_FOLDER_RECYCLE_BIN && $folder['fid'] != $current_folder) {
          $extra_folders[$folder['fid']] = $folder['name'];
        }
      }
      $extra_folders['new'] = t('New folder...');
  
      // Control to move messages to a new folder. Display only for inbox and
      // custom folders.
      if ((arg(1) == NULL || arg(1) == 'list') && count($extra_folders) > 0 && $current_folder != PRIVATEMSG_FOLDER_RECYCLE_BIN && $current_folder != PRIVATEMSG_FOLDER_SENT) {
        $form['selected']['folder'] = array(
          '#prefix' => '<div class="pm-spacer">',
          '#type' => 'select',
          '#options' => $extra_folders,
        );
        $form['selected']['extra_folders'] = array('#type' => 'value', '#value' => $extra_folders);
        $form['selected']['move_messages'] = array(
          '#type' => 'submit',
          '#value' => t('Move to folder'),
          '#suffix' => '</div>',
        );
      }
    }

    if ($del == 2) {
      $form['selected']['actions'] = $actions;
    }
  }

  $form['js_bypass'] = array(
    '#type' => 'hidden',
    '#value' => 0,
  );

  drupal_add_js(drupal_get_path('module', 'privatemsg') .'/privatemsg.js');
  drupal_add_js(array('privatemsg' => $js), 'setting');

  return $form;
}

function privatemsg_list_form_submit($form_id, $form_values) {
  global $user;

  $selected = $placeholders = array();
  foreach ($form_values['messages'] as $mid => $message) {
    if ($message['selected']) {
      $selected[$mid] = $message;
      $placeholders[] = '%d';
    }
  }
  if (empty($selected) && ($form_values['op'] != t('Empty recycle bin'))) {
    drupal_set_message(t('No messages selected.'), 'error');
    return;
  }
  else {
    $args = array_keys($selected);
    $args[] = $form_values['account']->uid;
    $where1 = 'WHERE id IN ('. implode(', ', $placeholders) .')  AND ';
    $where = $where1 .'recipient = %d';
    $new = 0;
  }

  switch ($form_values['op']) {
    case t('Delete'):
      if ($_POST['js_bypass'] == 1) {
        foreach ($selected as $mid => $message) {
          _privatemsg_delete($mid);
        }
        drupal_set_message(t('The selected messages have been deleted.'));
      }
      else {
        $_SESSION['privatemsg'] = $form_values['messages'];
        $_SESSION['fromfolder'] = intval(arg(2));
        drupal_goto('privatemsg/delete/multiple');
      }
      break;
    case t('Mark as unread'):
      $new = 1;
      // deliberate no break;
    case t('Mark as read'):
      array_unshift($args, $new);
      db_query('UPDATE {privatemsg} SET newmsg = %d '. $where, $args);

      drupal_set_message(t('The selected messages have been updated.'));
      break;
    case t('Move to folder'):
      $fid = $form_values['folder'];
      if ($fid == 'new') {
        $_SESSION['privatemsg'] = $form_values['messages'];
        $_SESSION['fromfolder'] = intval(arg(2));
        drupal_goto('privatemsg/folders/movetonew');
      }
      _privatemsg_move_folder($where, $args, $fid, $form_values['extra_folders']);
      break;
    case t('Delete permanently'):
      foreach (array_keys($selected) as $mid) {
        _privatemsg_delete($mid, 2);
      }
      drupal_set_message(t('The selected messages have been deleted permanently.'));
      break;
    case t('Restore'):
      db_query('UPDATE {privatemsg} SET recipient_del = 0 '. $where, $args);
      $where = $where1 .'author = %d';
      db_query('UPDATE {privatemsg} SET author_del = 0 '. $where, $args);

      drupal_set_message(t('The selected messages have been restored.'));

      break;
    case t('Empty recycle bin'):
      $result = db_query('SELECT id FROM {privatemsg} p WHERE p.recipient = %d AND p.recipient_del = 1 UNION SELECT id FROM {privatemsg} p WHERE p.author = %d AND p.author_del = 1', $user->uid, $user->uid);
      while ($message = db_fetch_object($result)) {
        _privatemsg_delete($message->id, 2);
      }
      drupal_set_message('Recycle bin emptied.');
      break;
  }
}

function _privatemsg_move_folder($where, $args, $fid, $extra_folders) {
  array_unshift($args, $fid);
  db_query('UPDATE {privatemsg} SET folder = %d '. $where, $args);
  $msg_count = count($args);

  $folder = $extra_folders[$fid];
  $link = l(t('%folder', array('%folder' => $folder)), 'privatemsg/list/'. $fid, array(), NULL, NULL, FALSE, TRUE);

  if ($msg_count == 1) {
    drupal_set_message(t('Your message has been moved to !link.', array('!link' => $link)));
  }
  elseif ($msg_count > 1) {
    drupal_set_message(t('Your messages have been moved to !link.', array('!link' => $link)));
  }
}

/**
 * Display the console that lets users manage their private message folders.
 */
function privatemsg_manage_folders() {
  global $user;

  $header = array(t('Name'), t('Messages'), t('Operations'));

  $folders = privatemsg_load_folders($user->uid);
  foreach ($folders as $folder) {
    $fid = $folder['fid'];
    $operations = array();
    switch ($fid) {
      case PRIVATEMSG_FOLDER_RECYCLE_BIN:
        // Deleted messages are not necessarily stored in a folder with folder
        // ID -1. Instead, they retain the folder ID of their original folders.
        $msg_count = db_result(db_query("SELECT COUNT(*) FROM {privatemsg} WHERE recipient = %d AND recipient_del = 1", $user->uid));
        break;
      case PRIVATEMSG_FOLDER_SENT:
        $msg_count = db_result(db_query("SELECT COUNT(*) FROM {privatemsg} WHERE author = %d AND author_del = 0", $user->uid, $fid));
        break;
      case PRIVATEMSG_FOLDER_INBOX:
        $msg_count = db_result(db_query("SELECT COUNT(*) FROM {privatemsg} WHERE recipient = %d AND folder = %d AND recipient_del = 0", $user->uid, $fid));
        break;
      default:
        // Custom folders.
        $msg_count = db_result(db_query("SELECT COUNT(*) FROM {privatemsg} WHERE recipient = %d AND folder = %d AND recipient_del = 0", $user->uid, $fid));
        $operations[] = l(t('Rename'), 'privatemsg/folders/'. $fid .'/rename');
        $operations[] = l(t('Empty'), 'privatemsg/folders/'. $fid .'/empty');
        $operations[] = l(t('Delete'), 'privatemsg/folders/'. $fid .'/delete');
        break;
    }

    $row = array(
      l($folder['name'], $fid != 0 ? 'privatemsg/list/'. $fid : 'privatemsg'),
      $msg_count
    );
    $row = array_merge($row , !empty($operations) ? $operations : array(array('data' => '', 'colspan' => 3)));

    $rows[] = $row;
  }

  $output = theme('table', $header, $rows, array('class' => 'pm-manage-folders')) . drupal_get_form('privatemsg_new_folder_form');

  return $output;
}

/**
 * Form to add a new folder.
 */
function privatemsg_new_folder_form() {
  if (arg(2) == 'movetonew') {
    $movetonew = TRUE;
  }
  else {
    $movetonew = FALSE;
  }

  if (isset($_SESSION['privatemsg'])) {
    $name_desc = t('Enter a name for the new folder.');
    $form['messages'] = array('#tree' => TRUE);
    foreach ($_SESSION['privatemsg'] as $mid => $message) {
      if ($message['selected']) {
        $form['messages'][$mid] = array(
          '#type' => 'hidden',
          '#value' => $mid
        );
      }
    }
  }
  else {
    if ($movetonew) {
      drupal_goto('privatemsg/folders');
    }
  }

  $to_fid = $_SESSION['fromfolder'];
  if (empty($to_fid)) {
    $path = 'privatemsg';
  }
  elseif (substr($to_fid, 0, 4) == 'view') {
    $path = 'privatemsg/'. $to_fid;
  }
  elseif (is_numeric($to_fid)) {
    $path = 'privatemsg/list/'. $to_fid;
  }
  else {
    $path = 'privatemsg';
  }

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('New folder'),
    '#description' => isset($name_desc) ? $name_desc : '',
    '#size' => 24,
    '#maxlength' => 64,
    '#prefix' => $movetonew ? '' : '<fieldset><div class="container-inline">',
  );
  $form['op'] = array(
    '#type' => 'submit',
    '#value' => $movetonew ? t('Create folder') : t('Add'),
    '#suffix' => $movetonew ? l(t('Cancel'), $path) : '</div></fieldset>',
  );
  $form['#access'] = user_access('create new folder');

  return $form;
}

function privatemsg_new_folder_form_validate($form_id, $form_values) {
  global $user;
  if (!empty($form_values['name']) && db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE name = '%s' AND uid = %d", $form_values['name'], $user->uid))) {
    form_set_error('name', t('A folder with this name already exists.'));
  }
  if (empty($form_values['name'])) {
    form_set_error('name', t('You must specify a name for the new folder.'));
  }
}

function privatemsg_new_folder_form_submit($form_id, $form_values) {
  global $user;
  $fid = db_next_id('{privatemsg_folder}_fid');
  db_query("INSERT INTO {privatemsg_folder} (fid, uid, name) VALUES (%d, %d, '%s')", $fid, $user->uid, $form_values['name']);

  if (isset($form_values['messages']) && is_array($form_values['messages'])) {
    unset($_SESSION['privatemsg']);
    foreach ($form_values['messages'] as $key => $value) {
      db_query("UPDATE {privatemsg} SET folder = %d WHERE id = %d AND recipient = %d", $fid, $key, $user->uid);
    }

    $link = l(t('%folder', array('%folder' => $form_values['name'])), 'privatemsg/list/'. $fid, array(), NULL, NULL, FALSE, TRUE);

    if (count($form_values['messages']) == 1) {
      drupal_set_message(t('Your message has been moved to !link.', array('!link' => $link)));
    }
    elseif (count($form_values['messages']) > 1) {
      drupal_set_message(t('Your messages have been moved to !link.', array('!link' => $link)));
    }

    $to_fid = $_SESSION['fromfolder'];
    unset($_SESSION['fromfolder']);
    if (empty($to_fid)) {
      drupal_goto('privatemsg');
    }
    elseif (substr($to_fid, 0, 4) == 'view') {
      drupal_goto('privatemsg/'. $to_fid);
    }
    elseif (is_numeric($to_fid)) {
      drupal_goto('privatemsg/list/'. $to_fid);
    }
    else {
      drupal_goto('privatemsg');
    }
  }
  else {
    drupal_set_message(t('Folder created successfully.'));
    if (isset($_SESSION['privatemsg'])) {
      unset($_SESSION['privatemsg']);
      $to_fid = $_SESSION['fromfolder'];
      unset($_SESSION['fromfolder']);
      if (empty($to_fid)) {
        drupal_goto('privatemsg');
      }
      else {
        drupal_goto('privatemsg/list/'. $to_fid);
      }
    }
  }
}

/**
 * Display form to rename a folder.
 */
function privatemsg_rename_folder_form($fid) {
  $folder = db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE fid = %d", $fid));
  if (empty($folder)) {
    drupal_set_message(t('Folder does not exist.'));
    drupal_goto('privatemsg/folders');
  }

  $form['old_name'] = array(
    '#type' => 'hidden',
    '#value' => $folder,
  );
  $form['new_name'] = array(
    '#type' => 'textfield',
    '#title' => t('New folder name'),
    '#size' => 24,
    '#maxlength' => 64,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Rename folder'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => l(t('Cancel'), 'privatemsg/folders') .'</div>',
  );

  return $form;
}

function privatemsg_rename_folder_form_validate($form_id, $form_values) {
  global $user;
  if (!empty($form_values['new_name']) &&
      $form_values['new_name'] != $form_values['old_name'] &&
      db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE name = '%s' AND uid = %d", $form_values['new_name'], $user->uid))) {
    form_set_error('new_name', t('A folder with this name already exists.'));
  }
  if (empty($form_values['new_name'])) {
    form_set_error('new_name', t('You must specify a name for the new folder.'));
  }
}

function privatemsg_rename_folder_form_submit($form_id, $form_values) {
  if ($form_values['new_name'] != $form_values['old_name']) {
    db_query("UPDATE {privatemsg_folder} SET name = '%s' WHERE fid = %d", $form_values['new_name'], intval(arg(2)));
    drupal_set_message(t('Folder name changed from %old_name to %new_name.',
                         array('%old_name' => $form_values['old_name'],
                               '%new_name' => $form_values['new_name'])));
  }
  else {
    drupal_set_message(t('Folder name left the same.'));
  }
  drupal_goto('privatemsg/folders');
}

/**
 * Prompt to delete all the messages in a folder.
 */
function privatemsg_empty_folder_form($fid) {
  $folder = db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE fid = %d", $fid));
  if (empty($folder)) {
    drupal_set_message(t('Folder does not exist.'));
    drupal_goto('privatemsg/folders');
  }

  $message = t('Are you sure you want to delete every message in %folder?', array('%folder' => $folder));

  $form = confirm_form(array(), t('Empty folder?'), 'privatemsg/folders',
                       $message, t('Empty'), t('Cancel'), 'empty');

  return $form;
}

function privatemsg_empty_folder_form_submit($form_id, $form_values) {
  global $user;
  $fid = intval(arg(2));

  if ($fid == 0 || $form_values['empty'] != 1) {
    drupal_goto('privatemsg/folders');
  }

  if ($fid == 1) {
    db_query("UPDATE {privatemsg} SET author_del = 1 WHERE author = %d", $user->uid);
  }
  else if ($fid > 1 && !db_result(db_query('SELECT fid FROM {privatemsg_folder} WHERE fid = %d AND uid = %d', $fid, $user->uid))) {
    watchdog('privatemsg', t('Attempted use of unauthorized folder'), WATCHDOG_WARNING);
    return drupal_access_denied();
  }
  else {
    db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = %d AND recipient = %d", $fid, $user->uid);
  }

  drupal_set_message(t('Folder emptied.'));
  drupal_goto('privatemsg/folders');
}

/**
 * Prompt to delete a folder and all its messages.
 */
function privatemsg_delete_folder_form($fid) {
  $folder = db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE fid = %d", $fid));
  if (empty($folder)) {
    drupal_set_message(t('Folder does not exist.'));
    drupal_goto('privatemsg/folders');
  }

  $message = t('Are you sure you wish to delete %folder and all its messages?', array('%folder' => $folder));

  $form = confirm_form(array(), t('Delete folder?'), 'privatemsg/folders',
                       $message, t('Delete'), t('Cancel'), 'delete');

  return $form;
}

function privatemsg_delete_folder_form_submit($form_id, $form_values) {
  global $user;
  $fid = intval(arg(2));

  if ($fid == 0 || $form_values['delete'] != 1) {
    drupal_goto('privatemsg/folders');
  }

  if (!db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = %d AND uid = %d", $fid, $user->uid))) {
    watchdog('privatemsg', t('Attempted use of unauthorized folder'), WATCHDOG_WARNING);
    return drupal_access_denied();
  }
  db_query("DELETE FROM {privatemsg_folder} WHERE fid = %d", $fid);
  db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = %d", $fid);

  drupal_set_message(t('Folder deleted.'));
  drupal_goto('privatemsg/folders');
}

/**
 * Provides a form to write a private message.
 */
function privatemsg_new_form($message = NULL) {
  global $user;
  if (!isset($message)) {
    $message = 0;

    $op = arg(1);
    if ($op == 'reply') {
      $message = arg(2);
    }
    else if (($uid = arg(2)) && ($msg->recipient = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid)))) {
      if (!privatemsg_message_allowed($uid, $user->uid)) {
        drupal_set_message(t('You cannot contact %recipient', array('%recipient' => $msg->recipient)));
        drupal_goto("user/$uid");
      }
      $message = $msg;
    }
  }

  if ($message && !is_object($message)) {
    // This is a reply to another message
    $message = db_fetch_object(db_query('SELECT thread, subject, message, u.name AS recipient FROM {privatemsg} p INNER JOIN {users} u ON u.uid = p.author WHERE id = %d AND recipient = %d', $message, $user->uid));

    if (!stristr($message->subject, t('Re:'))) {
      $message->subject = t('Re: ') . $message->subject;
    }

    // quoting; [quote] if default input format uses bbcode or quote, else > quoting
    foreach (filter_list_format(filter_resolve_format(FILTER_FORMAT_DEFAULT)) as $filter) {
      if ($filter->module == 'bbcode' || $filter->module == 'quote') {
        $bbcode = TRUE;
        break;
      }
    }
    if (isset($bbcode)) {
      $message->message = "\n\n[quote=". $message->recipient .']'. $message->message .'[/quote]';
    }
    else {
      $message->message = "\n\n\n". str_replace("\n", "\n> ", "\n". $message->message);
    }
  }
  if (!is_object($message)) {
    $message = new stdClass();
  }
  // The first if can provide a partial message, so we fill it in to avoid
  // notices. Also we remove the message if it's a reply in a threaded view,
  // the original message will be seen anyways.
  if (!isset($message->subject) || variable_get('privatemsg_threaded_view', 0)) {
    $message->message = '';
  }

  if (isset($_SESSION['recipients'])) {
    $recipient = implode(', ', $_SESSION['recipients']);
    unset($_SESSION['recipients']);
  }

  $form['thread'] = array('#type' => 'value', '#value' => isset($message->thread) ? $message->thread : 0);
  $form['header']['#theme'] = 'privatemsg_new_msg_header';
  $form['header']['recipient'] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#description' => t('Separate multiple names with commas.'),
    '#default_value' => isset($recipient) ? $recipient : $message->recipient,
    '#autocomplete_path' => 'privatemsg/autocomplete',
    '#size' => 50,
    '#maxlength' => 1000,
    '#required' => TRUE,
  );
  $form['header']['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => $message->subject,
    '#size' => 50,
    '#maxlength' => 64,
    '#required' => TRUE,
  );
  $form['privatemsgbody'] = array(
    '#type' => 'textarea',
    '#title' => empty($message->thread) ? t('Message') : t('Reply'),
    '#default_value' => $message->message,
    '#cols' => 80,
    '#rows' => 6,
  );
  $form['filter_form'] = filter_form($message->format);
  $form['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview'),
    '#prefix' => '<div class="pm-controls">'
  );
  $form['send'] = array(
    '#type' => 'submit',
    '#value' => t('Send private message')
  );
  $form['cancel'] = array(
    '#value' => l(t('Cancel'), arg(1) == 'reply' ? 'privatemsg/view/'. arg(2) : 'privatemsg'),
    '#suffix' => '</div>'
  );

  $form['#after_build'] = array('_privatemsg_new_preview');

  drupal_add_js(drupal_get_path('module', 'privatemsg') .'/privatemsg.js');

  return $form;
}

function _privatemsg_new_preview($form, $form_values) {
  if ($form_values['op'] == t('Preview')) {
    $pass = TRUE;
    $recipients = explode(',', $form_values['recipient']);
    foreach ($recipients as $recipient) {
      $recipient = user_load(array('name' => trim($recipient)));
      if (!$recipient->uid) {
        $pass = FALSE;
      }
    }
    if ($pass) {
      global $user;
      $message->subject = $form_values['subject'];
      $message->message = $form_values['privatemsgbody'];
      $message->format = $form_values['format'];
      $message->timestamp = time();
      $message->newmsg = TRUE;
      $message->folder = 1;
      $message->uid = $message->author = $user->uid;
      $message->name = $user->name;
      $message->recipient = implode(', ', $recipients);
      $message->preview = TRUE;
      // Allow other modules to change the message.
      privatemsg_invoke_privatemsg($message, 'view');
      $form['preview'] = array(
        '#prefix' => '<div class="preview">',
        '#value' => theme('privatemsg_view', $message, TRUE),
        '#suffix' => '</div>');
      drupal_set_title(t('Preview message'));
    }
  }
  return $form;
}

function privatemsg_new_form_validate($form_id, $form_values) {
  if (!empty($form_values['recipient'])) {
    $recipients = explode(',', $form_values['recipient']);
    foreach ($recipients as $recipient) {
      $account = user_load(array('name' => trim($recipient)));
      if (!$account) {
        form_set_error('recipient', t('The recipient %name does not exist.', array('%name' => $recipient)));
      }
      else if (!$account->status || !$account->privatemsg_allow) {
        form_set_error('recipient', t('%name does not accept private messages.', array('%name' => $account->name)));
      }
    }
  }
  if (!isset($form_values['format']) || !filter_access($form_values['format'])) {
    form_set_error('format', t('The supplied input format is invalid.'));
  }
  if (empty($form_values['privatemsgbody'])) {
    form_set_error('privatemsgbody', t('Message field is required.'));
  }
}

function privatemsg_new_form_submit($form_id, $form_values) {
  global $user;
  static $seen = array();
  if ($form_values['op'] != t('Send private message')) {
    return FALSE;
  }

  $recipients = explode(',', $form_values['recipient']);
  $sent = FALSE;
  foreach ($recipients as $recipient) {
    $recipient = trim($recipient);
    if (isset($seen[$recipient])) {
      continue;
    }
    $seen[$recipient] = TRUE;
    $recipient = user_load(array('name' => $recipient));
    $message_id = _privatemsg_send($user, $recipient, $form_values['subject'], $form_values['privatemsgbody'], $form_values['format'], $form_values['thread']);
    if ($message_id) {
      // Load the message for consistency.
      $message = privatemsg_load($message_id);
      // Tell the other modules a new private message has been sent.
      privatemsg_invoke_privatemsg($message, 'sent');
      $sent = TRUE;
    }
  }

  if ($sent) {
    drupal_set_message(t('Message sent.'));
  }
  drupal_goto($user->uid ? 'privatemsg' : '');
}

/**
 * Send private message. Sender is the current user.
 *
 * @param $recipient
 *   A user object containing the recipient.
 * @param $subject
 *   The subject of the message.
 * @param $body
 *   The body of the message.
 * @param $format
 *   Format of the message.
 * @param $thread
 *   The thread this message belongs to (take care to preserve on replies).
 * @param $type
 *   Type of the message as defined on admin/build/privatemsg .
 * @param $variables
 *   If this message is a subscriptions mail, the variables used to compose the
 * message.
 **/
function privatemsg_send_privatemsg($recipient, $subject, $body, $format = FILTER_FORMAT_DEFAULT, $thread = 0, $type = 'private-message', $variables = array()) {
  global $user;
  return _privatemsg_send($user, $recipient, $subject, $body, $format, $thread, $type, $variables);
}

function _privatemsg_send($sender, $recipient, $subject, $body, $format, $thread = 0, $type = 'private-message', $original_variables = array()) {
  if (!privatemsg_message_allowed($recipient->uid, $sender->uid)) {
    drupal_set_message(t('You cannot contact %recipient', array('%recipient' => $recipient->name)));
    return;
  }

  // Hook to allow other modules to alter any aspect of a privatemsg by
  // accepting these params by reference. Any module can cancel a privatemsg by
  // returning false here
  foreach (module_implements('privatemsg_alter') as $name) {
    $function = $name .'_privatemsg_alter';
    if (!$function($sender, $recipient, $subject, $body, $format, $thread, $type)) {
      return FALSE;
    }
  }
  
  $message_id = db_next_id('{privatemsg}_id');
  if (!$thread) {
    $thread = db_next_id('{privatemsg}_thread');
  }
  $variables = array();
  foreach ($original_variables as $name => $value) {
    $variables[str_replace('!', '!original_', $name)] = $value;
  }
  $result = db_query("INSERT INTO {privatemsg} (id, author, recipient, subject, message, timestamp, newmsg, hostname, format, thread, type, variables) VALUES (%d, %d, %d, '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s')", $message_id, $sender->uid, $recipient->uid, $subject, $body, time(), 1, getenv('REMOTE_ADDR'), $format, $thread, $type, serialize($variables));
  if ($points = variable_get('privatemsg_userpoints', 0)) {
    module_invoke('userpoints', 'userpointsapi', 'points', $points, $sender->uid, 'privatemsg');
  }
  module_invoke('pm_subscriptions', 'subscriptions_handle', privatemsg_load($message_id));

  return $result ? $message_id : $result;
}

/**
 * Return a version of type that can be used as an object property key
 * (change whitespace to _)
 */
function _privatemsg_get_type_key($type) {
  return 'privatemsg_allow_'. preg_replace('/\s+/', '_', $type);
}

/**
 * Display a user's contacts list and let them write a PM to multiple contacts.
 */
function privatemsg_contacts_form() {
  $contacts = array();
  global $user;

  // Add people who have messaged the user to the contact list.
  $result = db_query("SELECT DISTINCT u.name, u.uid FROM {privatemsg} AS p "
                    ."LEFT JOIN {users} AS u ON p.author = u.uid WHERE "
                    ."p.recipient = %d AND p.author != %d AND p.timestamp > "
                    ."(UNIX_TIMESTAMP(NOW()) - (3600 * 24 * 30)) ORDER BY "
                    ."u.name", $user->uid, $user->uid);
  while ($author = db_fetch_object($result)) {
    $contacts[check_plain($author->name)] = $author->uid;
  }

  // Add people the user has messaged to the contact list.
  $result = db_query("SELECT DISTINCT u.name, u.uid FROM {privatemsg} AS p "
                    ."LEFT JOIN {users} AS u ON p.recipient = u.uid WHERE "
                    ."p.author = %d AND p.timestamp > "
                    ."(UNIX_TIMESTAMP(NOW()) - (3600 * 24 * 30)) ORDER BY "
                    ."u.name", $user->uid, $user->uid);
  while ($author = db_fetch_object($result)) {
    $contacts[check_plain($author->name)] = $author->uid;
  }

  if (module_exists('buddylist')) {
    $result = db_query("SELECT u.name, u.uid FROM {buddylist} AS b LEFT JOIN "
                      ."{users} AS u ON b.buddy = u.uid WHERE b.uid = %d", $user->uid);
    while ($buddy = db_fetch_object($result)) {
      $contacts[check_plain($buddy->name)] = $buddy->uid;
    }
  }

  ksort($contacts);

  $form['contacts'] = array(
    '#theme' => 'privatemsg_contacts_table',
    '#tree' => TRUE
  );
  foreach ($contacts as $name => $uid) {
    $form['contacts'][$uid]['selected'] = array(
      '#type' => 'checkbox',
    );
    $form['contacts'][$uid]['name'] = array(
      '#type' => 'value',
      '#value' => $name,
    );
    $form['contacts'][$uid]['contact'] = array(
      '#type' => 'value',
      '#value' => theme('privatemsg_username', (object) array('uid' => $uid, 'name' => $name)),
    );
    $form['contacts'][$uid]['operations'] = array(
      '#type' => 'value',
      '#value' => l(t('Write private message'), 'privatemsg/new/'. $uid),
    );
  }

  if (count($contacts) > 0) {
    $form['selected'] = array(
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
    );
    $form['selected']['label'] = array(
      '#value' => '<div><strong>'. t('With selected:') .'</strong> &nbsp; &nbsp;</div>',
    );
    $form['selected']['write_message'] = array(
      '#type' => 'submit',
      '#value' => t('Write private message'),
    );
  }

  return $form;
}

function privatemsg_contacts_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Write private message')) {
    foreach ($form_values['contacts'] as $uid => $contact) {
      if ($contact['selected']) {
        $recipients[] = $contact['name'];
      }
    }
    if (count($recipients) > 0) {
      $_SESSION['recipients'] = $recipients;
      drupal_goto('privatemsg/new');
    }
  }
}

/**
 * Display a private message to a user.
 *
 * We calculate previous and next message here. Let's assume the order is from
 * oldest to newest. The next message is then the first row from:
 * SELECT id FROM privatemsg WHERE timestamp > %d ORDER BY timestamp ASC
 * The code below is the abstraction of this to every field and ASC/DESC.
 */
function privatemsg_view($message_id) {
  global $user;
  if ($message = privatemsg_load($message_id)) {
    $threaded_view = variable_get('privatemsg_threaded_view', 0);
    if (isset($_SESSION['privatemsg_list_sql'])) {
      // This is the SQL assembled in privatemsg_list
      $sql = $_SESSION['privatemsg_list_sql']['sql'];
      // These are the arguments, again from privatemsg_list
      $args = $_SESSION['privatemsg_list_sql']['args'];
      // This is the field we are ordering on.
      $field = $_SESSION['privatemsg_list_sql']['field'];
      // This is either ASC or DESC.
      $sort = $_SESSION['privatemsg_list_sql']['sort'];
      if ($threaded_view) {
        $sql .= ' AND thread != %d';
        $args[] = $message->thread;
      }
      $placeholder = ($field == 'type' || $field == 'subject') ? "'%s'" : '%d';
      $args[] = $message->$field;
      if ($sort == 'ASC') {
        $prev_operator = '<';
        $next_operator = '>';
        $prev_sort = 'DESC';
        $next_sort = 'ASC';
      }
      else {
        $prev_operator = '>';
        $next_operator = '<';
        $prev_sort = 'ASC';
        $next_sort = 'DESC';
      }
      if ($previous_id = db_result(db_query_range("$sql AND $field $prev_operator $placeholder ORDER BY $field $prev_sort", $args, 0, 1))) {
        $previous_link = l(t('Previous'), 'privatemsg/view/'. $previous_id, array(), NULL, 'message-'. $previous_id);
      }
      else {
        $previous_link = '';
      }
      if ($next_id = db_result(db_query_range("$sql AND $field $next_operator $placeholder ORDER BY $field $next_sort", $args, 0, 1))) {
        $next_link =  l(t('Next'), 'privatemsg/view/'. $next_id, array(), NULL, 'message-'. $next_id);
      }
      else {
        $next_link = '';
      }
    }
    if ($threaded_view && $message->thread) {
      $result = db_query('SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.folder, p.format FROM {privatemsg} p INNER JOIN {users} u ON p.author = u.uid WHERE p.thread = %d ORDER BY timestamp ASC', $message->thread);
      $messages = '';
      while ($message = db_fetch_object($result)) {
        if (!isset($first)) {
          $first = $message;
        }
        if ($message->recipient == $user->uid) {
          $last = $message;
        }
        $messages .= _privatemsg_view($message);
      }
    }
    else {
      $first = $message;
      $messages = _privatemsg_view($message);
    }
    $output = theme('privatemsg_links', $previous_link, $next_link);
    $output .= $messages;
    if ($last->recipient == $user->uid) {
      $output .= drupal_get_form('privatemsg_new_form', $last->id);
    }
    return $output;
  }
  drupal_goto('privatemsg');
}

function _privatemsg_view($message) {
  global $user;
  // Allow other modules to change the message.
  privatemsg_invoke_privatemsg($message, 'view');
  if ($message->newmsg && $user->uid == $message->recipient) {
    db_query("UPDATE {privatemsg} SET newmsg = 0 WHERE id = %d", $message->id);
  }
  return theme('privatemsg_view', $message, FALSE);
}

/**
 * Add the action form to the view message screen.
 */
function privatemsg_view_form($message) {
  global $user;

  $form['pm_id'] = array(
    '#type' => 'hidden',
    '#value' => $message->id,
  );
  $form['pm_fid'] = array(
    '#type' => 'hidden',
    '#value' => $message->folder,
  );
  $form['author']= array(
    '#type' => 'value',
    '#value' => $message->author,
  );
  $form['js_bypass'] = array(
    '#type' => 'hidden',
    '#value' => 0,
  );

  $form['actions'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#attributes' => array('class' => 'container-inline'),
  );
  $form['actions']['text'] = array(
    '#value' => '<div><strong>'. t('With this message:') .'</strong> </div>',
  );
  if ($user->uid == $message->recipient) {
    $form['actions']['reply'] = array(
      '#type' => 'submit',
      '#value' => t('Reply'),
      '#attributes' => array('class' => 'pm-spacer'),
    );
  }

  $del = variable_get('privatemsg_actions_loc', '1');

  if ($del == '1') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#prefix' => ' &nbsp; &nbsp;',
      '#attributes' => array('class' => 'pm-add-delete pm-spacer'),
    );
  }

  $folders = privatemsg_load_folders($user->uid);
  if (user_access('create new folder')) {
    $extra_folders = array();
    foreach ($folders as $folder) {
      // Do not display the recycle bin in the move to folder drop down as the
      // delete button is already available. Similarly, avoid displaying
      // the sent items folder as it is for internal use only.
      if ($folder['fid'] != PRIVATEMSG_FOLDER_SENT && $folder['fid'] != PRIVATEMSG_FOLDER_RECYCLE_BIN && $folder['fid'] != $message->folder) {
        $extra_folders[$folder['fid']] = $folder['name'];
      }
    }
    $extra_folders['new'] = t('New folder...');

    if ($user->uid == $message->recipient) {
      $form['actions']['folder'] = array(
        '#type' => 'select',
        '#options' => $extra_folders,
        '#attributes' => array('class' => 'pm-spacer'),
      );
      $form['actions']['extra_folders'] = array('#type' => 'value', '#value' => $extra_folders);
      $form['actions']['move_messages'] = array(
        '#type' => 'submit',
        '#value' => t('Move to folder'),
      );
    }
  }

  if ($del == '2') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#prefix' => ' &nbsp; &nbsp;',
      '#attributes' => array('class' => 'pm-add-delete pm-spacer'),
    );
  }

  // only add the JS once, otherwise we get dupllicated JS confirm messages on thread views
  static $count = 0;
  if ($count == 0) {
    $js = array('deleteMessage' => t('Are you sure you want to delete this message?'));

    drupal_add_js(drupal_get_path('module', 'privatemsg') .'/privatemsg.js');
    drupal_add_js(array('privatemsg' => $js), 'setting');
    ++$count;
  }

  return $form;
}

function privatemsg_view_form_submit($form_id, $form_values) {
  global $user;

  switch ($form_values['op']) {
    case t('Reply'):
      drupal_goto('privatemsg/reply/'. $form_values['pm_id']);
    case t('Delete'):
      if ($_POST['js_bypass'] == 1) {
        drupal_goto('privatemsg/delete/'. $form_values['pm_id']);
      }
      drupal_goto('privatemsg/delete/confirm/'. $form_values['pm_id']);
    case t('Move to folder'):
      $fid = $form_values['folder'];
      if ($fid == 'new') {
        $_SESSION['privatemsg'] = array(
          $form_values['pm_id'] => array('selected' => TRUE)
        );
        $_SESSION['fromfolder'] = 'view/'. intval($form_values['pm_id']);
        drupal_goto('privatemsg/folders/movetonew');
      }

      db_query("UPDATE {privatemsg} SET folder = %d WHERE id = %d AND recipient = %d", $fid, $form_values['pm_id'], $user->uid);
      $link = l(t('%folder', array('%folder' => $form_values['extra_folders'][$fid])), 'privatemsg/list/'. $fid, array(), NULL, NULL, FALSE, TRUE);
      drupal_set_message(t('Your message has been moved to !link.', array('!link' => $link)));

      break;
  }
}

function privatemsg_back_to_list($message_id) {
  global $user;
  $message = db_fetch_object(db_query("SELECT folder, recipient FROM {privatemsg} WHERE id = %d", $message_id));

  if ($message->fid > 0) {
    drupal_goto('privatemsg/list/'. $fid);
  }
  else {
    drupal_goto('privatemsg'. ($user->uid == $message->recipient ? '' : '/'. $message->recipient));
  }
}

function privatemsg_delete() {
  global $user;

  // Display the confirmation form for folks without JS.
  if (arg(2) == 'confirm' && ($mid = intval(arg(3))) > 0) {
    $test = db_result(db_query("SELECT id FROM {privatemsg} WHERE (recipient = %d OR author = %d) AND id = %d", $user->uid, $user->uid, $mid));
    if (empty($test)) {
      drupal_goto('privatemsg');
    }
    return drupal_get_form('privatemsg_delete_form', $mid);
  }

  if (arg(2) == 'multiple') {
    if (empty($_SESSION['privatemsg']) || !is_array($_SESSION['privatemsg'])) {
      drupal_goto('priatemsg');
    }
    return drupal_get_form('privatemsg_delete_multiple_form');
  }

  if (($folder = _privatemsg_delete(arg(2))) !== FALSE) {
    drupal_set_message(t('Message deleted.'));
  }
  drupal_goto($folder ? 'privatemsg/list/'. $folder : 'privatemsg');
}

function privatemsg_delete_form($mid) {
  $subject = db_result(db_query("SELECT subject FROM {privatemsg} WHERE id = %d", $mid));
  $message = t('Are you sure you wish to delete the message %subject?', array('%subject' => $subject));

  $form = confirm_form(array(), t('Delete message?'), 'privatemsg/view/'. $mid,
                       $message, t('Delete'), t('Cancel'), 'delete');

  return $form;
}

function privatemsg_delete_form_submit($form_id, $form_values) {
  drupal_goto('privatemsg/delete/'. arg(3));
}

function privatemsg_delete_multiple_form() {
  foreach ($_SESSION['privatemsg'] as $mid => $message) {
    if ($message['selected']) {
      $messages[] = $message['subject'];
    }
  }

  if ($_SESSION['fromfolder'] == 0) {
    $cancel_path = 'privatemsg';
  }
  else {
    $cancel_path = 'privatemsg/list/'. $_SESSION['fromfolder'];
  }

  $message = t('Are you sure you wish to delete the following messages?')
           . theme('item_list', $messages);

  $form = confirm_form(array(), t('Delete messages?'), $cancel_path,
                       $message, t('Delete'), t('Cancel'), 'delete');

  return $form;
}

function privatemsg_delete_multiple_form_submit($form_id, $form_values) {
  foreach ($_SESSION['privatemsg'] as $mid => $message) {
    if ($message['selected']) {
      _privatemsg_delete($mid);
    }
  }

  if ($_SESSION['fromfolder'] == 0) {
    $path = 'privatemsg';
  }
  else {
    $path = 'privatemsg/list/'. $_SESSION['fromfolder'];
  }
  unset($_SESSION['privatemsg'], $_SESSION['fromfolder']);

  drupal_set_message(t('Messages deleted.'));

  drupal_goto($path);
}

function _privatemsg_delete($id, $deleted_status = 1) {
  global $user;

  $result = db_query('SELECT author, recipient, folder FROM {privatemsg} WHERE (recipient = %d OR author = %d) AND id = %d', $user->uid, $user->uid, $id);

  if ($message = db_fetch_object($result)) {
    if ($message->author == $user->uid) {
      db_query('UPDATE {privatemsg} SET author_del = %d WHERE id = %d', $deleted_status, $id);
      $retval = 1;
    }
    if ($message->recipient == $user->uid) {
      db_query('UPDATE {privatemsg} SET recipient_del = %d WHERE id = %d', $deleted_status, $id);
      $retval = $message->folder;
    }
    // Notify other modules that a message is deleted.
    privatemsg_invoke_privatemsg($message, 'delete');
    return $retval;
  }
  return FALSE;
}

function _privatemsg_get_new_messages($uid = 0) {
  global $user;
  static $cache = array();
  if ($uid == 0) {
    $uid = $user->uid;
  }
  if (!isset($cache[$uid])) {
    $cache[$uid] = (int)db_result(db_query('SELECT COUNT(*) FROM {privatemsg} WHERE recipient = %d AND newmsg = 1 AND recipient_del = 0', $uid));
  }
  return $cache[$uid];
}

/**
 * Implementation of hook_userpoints.
 */
function privatemsg_userpoints($op, $new_points = 0, $uid = 0, $event = '') {
  switch ($op) {
    case 'setting':
      $form['privatemsg_userpoints'] = array(
        '#type' => 'textfield',
        '#title' => t('Points for sending a private message'),
        '#default_value' => variable_get('privatemsg_userpoints', 0));
      return $form;
  }
}

/**
 * Implementation of hook_simpletest.
 */
function privatemsg_simpletest() {
  $dir = drupal_get_path('module', 'privatemsg') .'/tests';
  $tests = file_scan_directory($dir, '\.test$');
  return array_keys($tests);
}

/**
 * @addtogroup theme_system
 *
 * Privatemsg module specific theme functions.
 * @{
 */

/**
 * Returns content to view a private message.
 *
 * @param message
 */
function theme_privatemsg_view($message, $preview = FALSE, $prev = 0, $next = 0) {
  $option = variable_get('privatemsg_view_actions', '3');
  global $user;

  if ($option == '4') {
    $links = array();
    if ($message->recipient == $user->uid) {
      $author = user_load(array('uid' => $message->uid));
      if ($author->uid && $author->privatemsg_allow) {
        $links['privatemsg_reply'] = array('title' => t('Reply to this message'), 'href' => 'privatemsg/reply/'. $message->id);
      }
      else {
        $links['privatemsg_noreply'] = array('title' => t('Sender does not accept replies'), 'href' => NULL);
      }
    }
    if ($message->recipient == $user->uid || variable_get('privatemsg_sent_status', 1)) {
      $links['privatemsg_delete'] = array('title' => t('Delete this message'), 'href' => 'privatemsg/delete/'. $message->id, 'attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete this message?') ."')"));
    }

    $links['privatemsg_list'] = array('title' => t('List messages'), 'href' => ($message->recipient == $user->uid && $message->folder) ? 'privatemsg/list/'. $message->folder : 'privatemsg');
    if ($message->id) {
      $link_display = '<div class="links">'. theme('links', $links) .'</div>';
    }
  }

  // From row.
  if ($preview) {
    $pm_from = $user->name;
  }
  else {
          

    $pm_from = '<div class="pm-username">'. theme('privatemsg_username', $message) .'</div>';
    $pm_from .= '<div class="pm-inbox-avatar">'. theme('privatemsg_user_picture', user_load(array('uid' => $message->uid))).'</div>';

    if ($message->author != $user->uid) {
      if (!privatemsg_user_blocked($message->author)) {
        $pm_from .= t('<span class="pm-block-link">(<a href="!block-link">Block user</a>)</span>', array('!block-link' => url('privatemsg/block/'. $message->author, drupal_get_destination())));
      }
      else {
        $pm_from .= t('<span class="pm-block-link">(<a href="!block-link">Unblock user</a>)</span>', array('!block-link' => url('privatemsg/block/'. $message->author, drupal_get_destination())));
      }
    }
  }
  $rows[] = array(
    array('data' => t('From:'), 'class' => 'title-cell', 'nowrap' => 'nowrap'),
    array('data' => $pm_from, 'class' => 'data-cell'),
  );

  // To row.
  if ($preview) {
    $pm_to = $message->recipient;
  }
  else {
    $pm_to = theme('privatemsg_username', user_load(array('uid' => $message->recipient)));
  }
  $rows[] = array(
    array('data' => t('To:'), 'class' => 'title-cell', 'nowrap' => 'nowrap'),
    array('data' => $pm_to, 'class' => 'data-cell'),
  );

  // Subject row.
  $subject = check_plain($message->subject);
  $rows[] = array(
    array('data' => t('Subject:'), 'class' => 'title-cell', 'nowrap' => 'nowrap'),
    array('data' => $subject, 'class' => 'data-cell'),
  );

  // Date row.
  $date = format_date($message->timestamp);
  $rows[] = array(
    array('data' => t('Date:'), 'class' => 'title-cell', 'nowrap' => 'nowrap'),
    array('data' => $date, 'class' => 'data-cell'),
  );

  // Message body.
  $body = '<div class="pm-body">'
        . check_markup($message->message, $message->format, FALSE) .'</div>';
/*  $rows[] = array(
    array('data' => $body, 'class' => 'pm-body', 'colspan' => 5),
  );*/
  if (!$preview && $option < 4) {
    $form = drupal_get_form('privatemsg_view_form', $message, $prev, $next);
  }

  if (!$preview && ($option == '1' || $option == '3')) {
    $output .= $form;
  }
  $output .= theme('table', NULL, $rows, array('id' => 'message-'. $message->id, 'class' => 'pm-view-table'));
  $output .= $body;
  if (!$preview && ($option == '2' || $option == '3')) {
    $output .= $form;
  }
  if (!$preview && $option == '4') {
    $output .= $link_display;
  }

  return $output;
}

function theme_privatemsg_message_table($form) {
  $rows = array();

  foreach (element_children($form) as $key) {
    if ($key != 'current_folder') {
      $classes = array('pm-'. drupal_strtolower(preg_replace('/[^\w]+/i', '_', $form[$key]['type']['#value'])));

      if ($form[$key]['#new']) {
        $classes[] = 'pm-new';
      }

      $rows[] = array(
        'data' => array(
          drupal_render($form[$key]['selected']),
          drupal_render($form[$key]['avatar']),
          $form[$key]['user']['#value'],
          array('data' => $form[$key]['subject']['#value'], 'width' => '50%'),
          array('data' => $form[$key]['date']['#value'], 'nowrap' => 'nowrap'),
        ),
        'class' => implode(' ', $classes),
      );

    }
  }

  if (count($rows) == 0) {
    $rows[] = array(array('data' => t('No messages.'), 'colspan' => 5));
    $select_cell = NULL;
  }
  else {
    $select_cell = theme('table_select_header_cell');
  }

  $header = _privatemsg_list_header($form['current_folder']['#value']);
  array_unshift($header, $select_cell);

  return theme('table', $header, $rows, array('id' => 'privatemsg_message_table'));
}

function theme_privatemsg_contacts_table($form) {
  $rows = array();

  foreach (element_children($form) as $key) {
    $rows[] = array(
      drupal_render($form[$key]['selected']),
      $form[$key]['contact']['#value'],
      $form[$key]['operations']['#value'],
    );
  }

  if (count($rows) == 0) {
    $rows[] = array(array('data' => t('No contacts.'), 'colspan' => 3));
    $select_cell = NULL;
  }
  else {
    $select_cell = theme('table_select_header_cell');
  }

  $header = array(
    $select_cell,
    t('Contact'),
    t('Operations'),
  );

  return theme('table', $header, $rows, array('class' => 'pm-contacts-table'));
}

function theme_privatemsg_new_msg_header($form) {
  $rows = array();

  $title = $form['recipient']['#title'] .':';
  unset($form['recipient']['#title']);
  $rows[] = array(
    'data' => array(
      array('data' => $title, 'class' => 'title-cell'),
      drupal_render($form['recipient']),
    ),
    'valign' => 'top'
  );

  $title = $form['subject']['#title'] .':';
  unset($form['subject']['#title']);
  $rows[] = array(
    array('data' => $title, 'class' => 'title-cell'),
    drupal_render($form['subject']),
  );

  return theme('table', NULL, $rows, array('class' => 'pm-view-table'));
}

function theme_privatemsg_username($user) {
  return theme('username', $user);
}

function theme_privatemsg_links($previous_link, $next_link) {
  $output = "<div class=\"privatemsg-nav-links\">";
  $output .= $next_link ? "<div class=\"next\">$next_link</div>" : '';
  $output .= $previous_link ? "<div class=\"previous\">$previous_link</div>" : '';
  $output .= "</div>";
  return $output;
}

function theme_privatemsg_block_inbox($new) {
  $items = array(
    l(t('Inbox') . ($new ? " ($new)" : ''), 'privatemsg'),
    l(t('Write message'), 'privatemsg/new'),
  );

  return array('subject' => t('Private messages'), 'content' => theme('item_list', $items));
}

/**  @} End of addtogroup theme_system
 *
 */

/**
 * Return autocomplete results for usernames.
 */
function privatemsg_autocomplete($string) {
  $names = explode(',', $string);
  for ($i = 0; $i < count($names); $i++) {
    $names[$i] = trim($names[$i]);
  }
  $search = array_pop($names);

  if ($search != '') {
    $sql = "SELECT name FROM {users} u WHERE status <> 0 AND LOWER(name) LIKE LOWER('%s%%') AND ";
    $sql .= variable_get('privatemsg_default_allow', 1) ? '(data NOT LIKE \'%%:16:"privatemsg_allow";i:0%%\' OR data IS NULL)' : 'data LIKE \'%%:16:"privatemsg_allow";i:1%%\'';
    $sql .= ' ORDER BY name ASC';
    $result = db_query_range($sql, $search, 0, 10);
    $prefix = count($names) ? implode(', ', $names) .', ' : '';
    $matches = array();
    while ($user = db_fetch_object($result)) {
      $matches[$prefix . $user->name] = check_plain($user->name);
    }
    print drupal_to_js($matches);
    exit();
  }
}

/**
 * Return an array of folders for a user.
 */
function privatemsg_load_folders($uid) {
  $folders = array();
  foreach (_privatemsg_folder_map() as $fid => $name) {
    $folders[] = array('fid' => $fid, 'name' => $name);
  }
  $result = db_query('SELECT fid, name FROM {privatemsg_folder} WHERE uid = %d', $uid);
  while ($folder = db_fetch_array($result)) {
    $folders[] = $folder;
  }
  return $folders;
}

/**
 * Returns TRUE if the user can access the folder.
 */
function privatemsg_folder_access($uid, $fid) {
  // Administrators can access any folder.
  if ($uid == 1 || user_access('access all folders')) {
    return TRUE;
  }

  // Anonymous users have no access.
  if (intval($uid) == 0) {
    return FALSE;
  }

  if (intval($fid) == 0) {
    return FALSE;
  }

  // Check the database for the user ID of the folder.
  $owner = db_result(db_query("SELECT uid FROM {privatemsg_folder} WHERE fid = %d", $fid));
  if ($owner == $uid) {
    return TRUE;
  }

  return FALSE;
}

function _privatemsg_prune() {
  // move deleted message older than 1 month to archive table, and optimize table
  $result = db_query('SELECT * FROM {privatemsg} WHERE author_del = 1 AND recipient_del = 1 AND timestamp < %d', time() - 3600*24*30);
  while ($message = db_fetch_object($result)) {
    db_query("INSERT INTO {privatemsg_archive} (id, author, recipient, subject, message, timestamp, hostname, format, folder, thread, type) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s')", $message->id, $message->author, $message->recipient, $message->subject, $message->message, $message->timestamp, $message->hostname, $message->format, $message->folder, $message->thread, $message->type);
    db_query('DELETE FROM {privatemsg} WHERE id = %d', $message->id);

    // Notify other modules that a message is pruned.
    privatemsg_invoke_privatemsg($message, 'prune');
  }

  // this is MySQL-specific
  if (!strncmp($GLOBALS['db_type'], 'mysql', 5)) {
    db_query('OPTIMIZE TABLE {privatemsg}');
  }
}

/**
 * Invoke a hook_privatemsg() operation in all modules.
 *
 * @param &$message
 *   A message object.
 * @param $op
 *   A string containing the name of the message operation.
 * @return
 *   The returned value of the invoked hooks.
 */
function privatemsg_invoke_privatemsg(&$message, $op) {
  $return = array();
  foreach (module_implements('privatemsg') as $name) {
    $function = $name .'_privatemsg';
    $result = $function($message, $op);
    if (isset($result) && is_array($result)) {
      $return = array_merge($return, $result);
    }
    else if (isset($result)) {
      $return[] = $result;
    }
  }
  return $return;
}

function privatemsg_load($message_id) {
  global $user;
  $sql = 'SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.folder, p.format, p.thread, p.type FROM {privatemsg} p INNER JOIN {users} u ON p.author = u.uid WHERE p.id = %d';
  $params = array($message_id);
  if (!user_access('administer private messages')) {
    $sql .= ' AND (recipient = %d OR author = %d)';
    $params[] = $user->uid;
    $params[] = $user->uid;
  }
  $result = db_query($sql, $params);
  return db_fetch_object($result);
}

function _privatemsg_user_add_defaults(&$account) {
  if (!isset($account->privatemsg_allow)) {
    $account->privatemsg_allow = variable_get('privatemsg_default_allow', 1);
    $account->privatemsg_setmessage_notify = variable_get('privatemsg_default_setmessage_notify', 1);
  }
}

function theme_privatemsg_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {

      if (arg(0) == 'user' && is_numeric(arg(1))) { // If on user profile page       
        $picture = file_create_url($account->picture);
      }
      else { // somewhere else on the site - use small pic
        $info = image_get_info($account->picture);
        $newpicture = dirname($account->picture) . '/picture-' . $account->uid . "-avatar." . $info['extension'];
        
        if (!file_exists($newpicture) || (filectime($newpicture) < filectime($account->picture))) {
          image_scale($account->picture, $newpicture, 64, 64); 
          if (!file_exists($newpicture)) {
            $newpicture = $account->picture;
          }
        }
        
        $picture = file_create_url($newpicture);
      } 
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

    if (isset($picture)) {
      $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
      $picture = theme('image', $picture, $alt, $alt, '', FALSE);
      if (!empty($account->uid) && user_access('access user profiles')) {
        $picture = l($picture, "user/$account->uid", array('title' => t('View user profile.')), NULL, NULL, FALSE, TRUE);
      }
      if (variable_get('privatemsg_online_status', 1)) {
  // code to show online/offline status
        $time_period = variable_get('user_block_seconds_online', 1200);
        $uid = $account->uid; // get the current userid for the avatar.
        $users = db_query("SELECT uid, name, access FROM {users} WHERE access >= %d AND uid = %s", time() - $time_period, $uid);
        $total_users = db_num_rows($users);
  
        if ($total_users == 1) {
          $output = '<span style="color: #00DD45; font-weight: bold; display:block;">' . t('Online') . '</span>';
          }
        else {
          $output = '<span style="color: #AA4444; display:block;">' . t('Offline') . '</span>';     
        }
      }
      return '<div class="picture">' . $output . $picture . '</div>';
        
    }
  }
}