forked from gabrielsaldana/Emacs-Identica-mode
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identica-mode.el
2475 lines (2230 loc) · 90.7 KB
/
identica-mode.el
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
;;; identica-mode.el --- Major mode API client for status.net open microblogging
;; Copyright (C) 2008-2011 Gabriel Saldana
;; Copyright (C) 2009 Bradley M. Kuhn
;; Author: Gabriel Saldana <gsaldana@gmail.com>
;; Last update: 2011-10-20
;; Version: 1.2.1
;; Keywords: identica web
;; URL: http://blog.gabrielsaldana.org/identica-mode-for-emacs/
;; Contributors:
;; Jason McBrayer <jmcbray@carcosa.net> (minor updates for working under Emacs 23)
;; Alex Schröder <kensanata@gmail.com> (mode map patches)
;; Christian Cheng (fixed long standing xml parsing bug)
;; Carlos A. Perilla from denting-mode
; Alberto Garcia <agarcia@igalia.com> (integrated patch from twittering-mode for retrieving multiplemethods)
;; Bradley M. Kuhn <bkuhn@ebb.org> (editing status from edit-buffer rather than minibuffer)
;; Jason McBrayer <jmcbray@carcosa.net> (replace group tags with hashtags on redents, longlines use)
;; Sean Neakums (patches of bugs flagged by byte-compiler)
;; Shyam Karanatt <shyam@swathanthran.in> (several patches and code cleanup, new http backend based on url.el)
;; Tezcatl Franco <tzk@riseup.net> (ur1.ca support)
;; Anthony Garcia <lagg@lavabit.com> (fix for icon-mode)
;; Alexande Oliva <oliva@lsd.ic.unicamp.br> (fix for icon placement on reverse order dents)
;; Aidan Gauland <aidalgol@no8wireless.co.nz> (variable scope code cleanup)
;; Joel J. Adamson <adamsonj@email.unc.edu> Added countdown minibuffer-prompt style
;; Kevin Granade <kevin.granade@gmail.com> (OAuth support)
;;; Commentary:
;; Identica Mode is a major mode to check friends timeline, and update your
;; status on Emacs.
;; identica-mode.el is a major mode for Identica. Based on the twittering mode
;; version 0.6 by Y. Hayamizu and Tsuyoshi CHO found at
;; <http://hayamin.com/wiliki.cgi?twittering-mode-en&l=en>
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FORCouldn't findSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth floor,
;; Boston, MA 02110-1301, USA.
;; Requirements
;; if using Emacs22 or previous, you'll need json.el
;; get it from http://edward.oconnor.cx/2006/03/json.el
;; json.el is part of Emacs23
;; To use the OAuth support, you need oauth.el
;; Downloadable from http://github.com/psanford/emacs-oauth/
;; If using Oauth with Emacs earlier than 23.3 you'll also need w3m.
;;; Install:
;; You can use M-x customize-group identica-mode to setup all settings or simply
;; add the following to your .emacs or your prefered customizations file
;; (require 'identica-mode)
;; (setq identica-username "yourusername")
;; If you want to use simple authentication add your password
;; (setq identica-password "yourpassword")
;; It is recommended to create a file ~/.authinfo with your login credentials
;; instead of storing your password in plain text, the file should have the
;; following contents:
;; machine servername login yourusername password yourpassword
;; Replace servername with your server (if Identica server use identi.ca)
;; yourusername and yourpassword with your information. If you setup your
;; authinfo file, you don't need to set identica-password variable anywhere
;; If you want to use OAuth authentication add the following
;; (setq identica-auth-mode "oauth")
;; If you want to post from the minibufer without having identica buffer active, add the following global keybinding.
;; Add this to send status updates
;; (global-set-key "\C-cip" 'identica-update-status-interactive)
;; Add this to send direct messages
;; (global-set-key "\C-cid" 'identica-direct-message-interactive)
;; If you want to connect to a custom statusnet server add this and change
;; identi.ca with your server's doman name.
;; (setq statusnet-server "identi.ca")
;; Start using with M-x identica
;; Follow me on identica: http://identi.ca/gabrielsaldana
;;; Code:
(require 'cl)
(require 'xml)
(require 'parse-time)
;;(require 'longlines)
(require 'url)
(require 'url-http)
(require 'json)
(require 'image)
(defconst identica-mode-version "1.2.1")
;;url-basepath fix for emacs22
(unless (fboundp 'url-basepath)
(defalias 'url-basepath 'url-file-directory))
;;workaround for url-unhex-string bug that was fixed in emacs 23.3
(defvar identica-unhex-broken nil
"Predicate indicating broken-ness of `url-unhex-string'.
If non-nil, indicates that `url-unhex-string' is broken and
must be worked around when using oauth.")
(defgroup identica-mode nil
"Identica Mode for microblogging"
:tag "Microblogging"
:link '(url-link http://blog.gabrielsaldana.org/identica-mode-for-emacs/)
:group 'applications )
(defun identica-mode-version ()
"Display a message for identica-mode version."
(interactive)
(let ((version-string
(format "identica-mode-v%s" identica-mode-version)))
(if (interactive-p)
(message "%s" version-string)
version-string)))
(defvar identica-mode-map (make-sparse-keymap "Identi.ca"))
(defvar menu-bar-identica-mode-menu nil)
(defvar identica-timer nil "Timer object for timeline refreshing will be stored here. DO NOT SET VALUE MANUALLY.")
(defvar identica-urlshortening-services-map
'((tinyurl . "http://tinyurl.com/api-create.php?url=")
(toly . "http://to.ly/api.php?longurl=")
(google . "http://ggl-shortener.appspot.com/?url=")
(ur1ca . "http://ur1.ca/?longurl=")
(tighturl . "http://2tu.us/?save=y&url=")
(isgd . "http://is.gd/create.php?format=simple&url="))
"Alist of tinyfy services.")
(defvar identica-new-dents-count 0
"Number of new tweets when `identica-new-dents-hook' is run.")
(defvar identica-new-dents-hook nil
"Hook run when new twits are received.
You can read `identica-new-dents-count' to get the number of new
tweets received when this hook is run.")
(defvar identica-display-max-dents nil
"How many dents to keep on the displayed timeline.
If non-nil, dents over this amount will bre removed.")
;; Menu
(unless menu-bar-identica-mode-menu
(easy-menu-define
menu-bar-identica-mode-menu identica-mode-map ""
'("Identi.ca"
["Send an update" identica-update-status-interactive t]
["Send a direct message" identica-direct-message-interactive t]
["Re-dent someone's update" identica-redent t]
["Repeat someone's update" identica-repeat t]
["Add as favorite" identica-favorite t]
["Follow user" identica-follow]
["Unfollow user" identica-unfollow]
["--" nil nil]
["Friends timeline" identica-friends-timeline t]
["Public timeline" identica-public-timeline t]
["Replies timeline" identica-replies-timeline t]
["User timeline" identica-user-timeline t]
["Tag timeline" identica-tag-timeline t]
["--" nil nil]
;; ["Group timeline" identica-group-timeline t]
;; ["Join to this group" identica-group-join t]
["Leave this group" identica-group-leave t]
)))
(defcustom identica-idle-time 20
"Idle time."
:type 'integer
:group 'identica-mode)
(defcustom identica-timer-interval 90
"Timer interval to refresh the timeline."
:type 'integer
:group 'identica-mode)
(defcustom identica-username nil
"Your identi.ca username. If nil, you will be prompted."
:type '(choice (const :tag "Ask" nil) (string))
:group 'identica-mode)
(defcustom identica-password nil
"Your identi.ca password. If nil, you will be prompted."
:type '(choice (const :tag "Ask" nil) (string))
:group 'identica-mode)
(defcustom identica-auth-mode "password"
"Authorization mode used, options are password and oauth."
:type 'string
:group 'identica-mode)
(defun identica-enable-oauth ()
"Enables oauth for identica-mode."
(interactive)
(require 'oauth)
;Test if we're running on an emacs version with broken unhex and apply workaround.
(unless (eq (url-unhex-string (url-hexify-string "²")) "²")
(setq identica-unhex-broken t)
(require 'w3m))
(setq identica-auth-mode "oauth"))
(defvar identica-mode-oauth-consumer-key
"53e8e7bf7d1be8e58ef1024b31478d2b")
(defvar identica-mode-oauth-consumer-secret
"1ab0876f14bd82c4eb450f720a0e84ae")
(defcustom statusnet-server "identi.ca"
"Statusnet instance url."
:type 'string
:group 'identica-mode)
(defcustom statusnet-request-url
"https://identi.ca/api/oauth/request_token"
"Statusnet oauth request_token url."
:type 'string
:group 'identica-mode)
(defcustom statusnet-access-url
"https://identi.ca/api/oauth/access_token"
"Statusnet oauth access_token url."
:type 'string
:group 'identica-mode)
(defcustom statusnet-authorize-url
"https://identi.ca/api/oauth/authorize"
"Statusnet authorization url."
:type 'string
:group 'identica-mode)
(defcustom statusnet-server-textlimit 140
"Number of characters allowed in a status."
:type 'integer
:group 'identica-mode)
(defvar oauth-access-token nil)
(defcustom statusnet-port 80
"Port on which StatusNet instance listens."
:type 'integer
:group 'identica-mode)
(defcustom identica-default-timeline "friends_timeline"
"Default timeline to retrieve."
:type 'string
:options '("friends_timeline" "public_timeline" "replies")
:group 'identica-mode)
(defcustom identica-statuses-count 20
"Default number of statuses to retrieve."
:type 'integer
:group 'identica-mode)
(defcustom identica-display-success-messages nil
"Display messages when the timeline is successfully retrieved."
:type 'boolean
:group 'identica-mode)
(defcustom identica-oldest-first nil
"If t, display older messages before newer ones."
:type 'boolean
:group 'identica-mode)
(defcustom identica-update-status-edit-confirm-cancellation nil
"If t, ask user if they are sure when aborting editing of an
identica status update when using an edit-buffer"
:type 'boolean
:group 'identica-mode)
(defcustom identica-soft-wrap-status t
"If non-nil, don't fill status messages in the timeline as
paragraphs. Instead, use visual-line-mode or longlines-mode if
available to wrap messages. This may work better for narrow
timeline windows."
:type 'boolean
:group 'identica-mode)
(defcustom identica-update-status-method 'minibuffer
"Method for performaing status updates.
The available choices are:
'minibuffer - edit the status update in the minibuffer.
'edit-buffer - edit the status update in an independent buffer."
:type '(choice (const :tag "Edit status in minibuffer" minibuffer)
(const :tag "Edit status in independent buffer" edit-buffer))
:group 'identica-mode)
(defcustom identica-http-get-timeout 10
"Controls how long to wait for a response from the server."
:type 'integer
:group 'identica-mode)
;; Initialize with default timeline
(defvar identica-method identica-default-timeline)
(defvar identica-method-class "statuses")
(defvar identica-remote-server nil)
(defvar identica-scroll-mode nil)
(make-variable-buffer-local 'identica-scroll-mode)
(defvar identica-source "identica-mode")
(defcustom identica-redent-format "♻"
"The format/symbol to represent redents."
:type 'string
:group 'identica-mode)
(defcustom identica-blacklist '()
"List of regexes used to filter statuses, evaluated after status formatting is applied."
:type 'string
:group 'identica-mode)
(defcustom identica-status-format "%i %s, %@:\n %h%t // from %f%L%r\n\n"
"The format used to display the status updates."
:type 'string
:group 'identica-mode)
;; %s - screen_name
;; %S - name
;; %i - profile_image
;; %d - description
;; %l - location
;; %L - " [location]"
;; %r - in reply to status
;; %u - url
;; %j - user.id
;; %p - protected?
;; %c - created_at (raw UTC string)
;; %C{time-format-str} - created_at (formatted with time-format-str)
;; %@ - X seconds ago
;; %t - text
;; %' - truncated
;; %h - favorited
;; %f - source
;; %# - id
(defcustom identica-urlshortening-service 'ur1ca
"The service to use for URL shortening.
Values understood are ur1ca, tighturl, tinyurl, toly, google and isgd."
:type 'symbol
:group 'identica-mode)
(defvar identica-buffer "*identica*")
(defun identica-buffer (&optional method)
"Create a buffer for use by identica-mode.
Initialize the global method with the default, or with METHOD, if present."
(unless method
(setq method "friends_timeline"))
(get-buffer-create identica-buffer))
(defstruct (statusnet-oauth-data
(:conc-name sn-oauth-))
"The oauth configuration associated with a statusnet account."
consumer-key ; string
consumer-secret ; string
request-url ; string
access-url ; string
authorize-url ; string
access-token ; string
)
(defstruct (statusnet-account
(:conc-name sn-account-))
"Container for account information."
server ; string
port ; integer
username ; string
auth-mode ; string, either "password" or "oauth"
password ; string
textlimit ; integer
oauth-data ; statusnet-account-oauth-data
last-timeline-retrieved ; string
)
(defvar statusnet-accounts nil
"A list of login credentials for statusnet instances.")
(defvar sn-current-account nil
"A pointer to the statusnet account being processed.")
(defvar identica-http-buffer nil
"Pointer to the current http response buffer.")
(defvar identica-timeline-data nil)
(defvar identica-timeline-last-update nil)
(defvar identica-highlighted-entries nil
"List of entry ids selected for highlighting.")
(defcustom identica-enable-highlighting nil
"If non-nil, set the background of every selected entry to the background
of identica-highlight-face."
:type 'boolean
:group 'identica-mode)
(defcustom identica-enable-striping nil
"If non-nil, set the background of every second entry to the background
of identica-stripe-face."
:type 'boolean
:group 'identica-mode)
(defcustom identica-enable-striping nil
"If non-nil, set the background of every second entry to the background
of identica-stripe-face."
:type 'boolean
:group 'identica-mode)
(defvar identica-username-face 'identica-username-face)
(defvar identica-uri-face 'identica-uri-face)
(defvar identica-reply-face 'identica-reply-face)
(defvar identica-stripe-face 'identica-stripe-face)
(defvar identica-highlight-face 'identica-highlight-face)
;;; Proxy
(defvar identica-proxy-use nil)
(defvar identica-proxy-server nil)
(defvar identica-proxy-port 8080)
(defvar identica-proxy-user nil)
(defvar identica-proxy-password nil)
(defun identica-toggle-proxy ()
"Toggle whether identica-mode uses a proxy."
(interactive)
(setq identica-proxy-use
(not identica-proxy-use))
(message "%s %s"
"Use Proxy:"
(if identica-proxy-use
"on" "off")))
(defun identica-user-agent-default-function ()
"Identica mode default User-Agent function."
(concat "Emacs/"
(int-to-string emacs-major-version) "." (int-to-string
emacs-minor-version)
" "
"Identica-mode/"
identica-mode-version))
(defvar identica-user-agent-function 'identica-user-agent-default-function)
(defun identica-user-agent ()
"Return User-Agent header string."
(funcall identica-user-agent-function))
;;; to show image files
(defvar identica-tmp-dir
(expand-file-name (concat "identicamode-images-" (user-login-name))
temporary-file-directory))
(defvar identica-icon-mode nil "You MUST NOT CHANGE this variable directory. You should change through function'identica-icon-mode'.")
(make-variable-buffer-local 'identica-icon-mode)
(defun identica-icon-mode (&optional arg)
(interactive)
(setq identica-icon-mode
(if identica-icon-mode
(if (null arg)
nil
(> (prefix-numeric-value arg) 0))
(when (or (null arg)
(and arg (> (prefix-numeric-value arg) 0)))
(when (file-writable-p identica-tmp-dir)
(progn
(if (not (file-directory-p identica-tmp-dir))
(make-directory identica-tmp-dir))
t)))))
(identica-current-timeline))
(defun identica-scroll-mode (&optional arg)
(interactive)
(setq identica-scroll-mode
(if (null arg)
(not identica-scroll-mode)
(> (prefix-numeric-value arg) 0))))
(defvar identica-image-stack nil)
(defun identica-image-type (file-name)
(cond
((string-match "\\.jpe?g" file-name) 'jpeg)
((string-match "\\.png" file-name) 'png)
((string-match "\\.gif" file-name) 'gif)
(t nil)))
(defun identica-setftime (fmt string uni)
(format-time-string fmt ; like "%Y-%m-%d %H:%M:%S"
(apply 'encode-time (parse-time-string string))
uni))
(defun identica-local-strftime (fmt string)
(identica-setftime fmt string nil))
(defun identica-global-strftime (fmt string)
(identica-setftime fmt string t))
(defvar identica-debug-mode nil)
(defvar identica-debug-buffer "*identica-debug*")
(defun identica-debug-buffer ()
(get-buffer-create identica-debug-buffer))
(defmacro debug-print (obj)
(let ((obsym (gensym)))
`(let ((,obsym ,obj))
(if identica-debug-mode
(with-current-buffer (identica-debug-buffer)
(insert (prin1-to-string ,obsym))
(newline)
,obsym)
,obsym))))
(defun identica-debug-mode ()
(interactive)
(setq identica-debug-mode
(not identica-debug-mode))
(message (if identica-debug-mode "debug mode:on" "debug mode:off")))
(defun identica-delete-notice ()
(interactive)
(let ((id (get-text-property (point) 'id))
(usern (get-text-property (point) 'username)))
(if (string= usern (sn-account-username sn-current-account))
(when (y-or-n-p "Delete this notice? ")
(identica-http-post "statuses/destroy" (number-to-string id))
(identica-get-timeline))
(message "Can't delete a notice that isn't yours"))))
(if identica-mode-map
(let ((km identica-mode-map))
(define-key km "\C-c\C-f" 'identica-friends-timeline)
;; (define-key km "\C-c\C-i" 'identica-direct-messages-timeline)
(define-key km "\C-c\C-r" 'identica-replies-timeline)
(define-key km "\C-c\C-a" 'identica-public-timeline)
(define-key km "\C-c\C-g" 'identica-group-timeline)
;; (define-ley km "\C-c\C-j" 'identica-group-join)
;; (define-ley km "\C-c\C-l" 'identica-group-leave)
(define-key km "\C-c\C-t" 'identica-tag-timeline)
(define-key km "\C-c\C-k" 'identica-stop)
(define-key km "\C-c\C-u" 'identica-user-timeline)
(define-key km "\C-c\C-c" 'identica-conversation-timeline)
(define-key km "\C-c\C-o" 'identica-remote-user-timeline)
(define-key km "\C-c\C-s" 'identica-update-status-interactive)
(define-key km "\C-c\C-d" 'identica-direct-message-interactive)
(define-key km "\C-c\C-m" 'identica-redent)
(define-key km "\C-c\C-h" 'identica-toggle-highlight)
(define-key km "r" 'identica-repeat)
(define-key km "F" 'identica-favorite)
(define-key km "\C-c\C-e" 'identica-erase-old-statuses)
(define-key km "\C-m" 'identica-enter)
(define-key km "R" 'identica-reply-to-user)
(define-key km "A" 'identica-reply-to-all)
(define-key km "\t" 'identica-next-link)
(define-key km [backtab] 'identica-prev-link)
(define-key km [mouse-1] 'identica-click)
(define-key km "\C-c\C-v" 'identica-view-user-page)
(define-key km "q" 'bury-buffer)
(define-key km "e" 'identica-expand-replace-at-point)
(define-key km "j" 'identica-goto-next-status)
(define-key km "k" 'identica-goto-previous-status)
(define-key km "l" 'forward-char)
(define-key km "h" 'backward-char)
(define-key km "0" 'beginning-of-line)
(define-key km "^" 'beginning-of-line-text)
(define-key km "$" 'end-of-line)
(define-key km "n" 'identica-goto-next-status-of-user)
(define-key km "p" 'identica-goto-previous-status-of-user)
(define-key km [backspace] 'scroll-down)
(define-key km " " 'scroll-up)
(define-key km "G" 'end-of-buffer)
(define-key km "g" 'identica-current-timeline)
(define-key km "H" 'beginning-of-buffer)
(define-key km "i" 'identica-icon-mode)
(define-key km "s" 'identica-scroll-mode)
(define-key km "t" 'identica-toggle-proxy)
(define-key km "\C-k" 'identica-delete-notice)
(define-key km "\C-c\C-p" 'identica-toggle-proxy)
nil))
(defvar identica-mode-syntax-table nil "")
(if identica-mode-syntax-table
()
(setq identica-mode-syntax-table (make-syntax-table))
;; (modify-syntax-entry ? "" identica-mode-syntax-table)
(modify-syntax-entry ?\" "w" identica-mode-syntax-table))
(defun identica-mode-init-variables ()
;; (make-variable-buffer-local 'variable)
;; (setq variable nil)
(make-variable-buffer-local 'identica-active-mode)
(set-default 'identica-active-mode t)
(font-lock-mode -1)
(defface identica-username-face
`((t nil)) "" :group 'faces)
(set-face-attribute 'identica-username-face nil :underline t)
(defface identica-reply-face
`((t nil)) "" :group 'faces)
(set-face-attribute 'identica-reply-face nil :background "DarkSlateGray")
(defface identica-stripe-face
`((t nil)) "" :group 'faces)
(set-face-attribute 'identica-stripe-face nil :background "LightSlateGray")
(defface identica-highlight-face
`((t nil)) "" :group 'faces)
(set-face-attribute 'identica-highlight-face nil :background "SlateGray")
(defface identica-uri-face
`((t nil)) "" :group 'faces)
(set-face-attribute 'identica-uri-face nil :underline t)
(defface identica-heart-face
`((t nil)) "" :group 'faces)
(set-face-attribute 'identica-heart-face nil :foreground "firebrick1" :height 2.0)
(add-to-list 'minor-mode-alist '(identica-icon-mode " id-icon"))
(add-to-list 'minor-mode-alist '(identica-scroll-mode " id-scroll"))
;; make face properties nonsticky
(nconc text-property-default-nonsticky
'((face . t)(mouse-face . t)(uri . t)(source . t)(uri-in-text . t)))
(identica-create-account))
(defun identica-create-account ()
"Create an account object based on the various custom variables.
Insert it into the statusnet accounts list.
This needs to be called from any globally-accessable entry point."
(unless (boundp 'statusnet-account-created)
(setq statusnet-account-created t)
(setq statusnet-accounts
(cons (make-statusnet-account
:server statusnet-server
:port statusnet-port
:username identica-username
:auth-mode identica-auth-mode
:password identica-password
:textlimit statusnet-server-textlimit
:oauth-data (if (string= identica-auth-mode "oauth")
(make-statusnet-oauth-data
:consumer-key identica-mode-oauth-consumer-key
:consumer-secret identica-mode-oauth-consumer-secret
:request-url statusnet-request-url
:access-url statusnet-access-url
:authorize-url statusnet-authorize-url
:access-token nil)
nil)
:last-timeline-retrieved nil)
statusnet-accounts))
(setq sn-current-account (car statusnet-accounts))))
(defmacro case-string (str &rest clauses)
`(cond
,@(mapcar
(lambda (clause)
(let ((keylist (car clause))
(body (cdr clause)))
`(,(if (listp keylist)
`(or ,@(mapcar (lambda (key) `(string-equal ,str ,key)) keylist))
't)
,@body)))
clauses)))
;; If you use Emacs21, decode-char 'ucs will fail unless Mule-UCS is loaded.
;; TODO: Show error messages if Emacs 21 without Mule-UCS
(defmacro identica-ucs-to-char (num)
(if (functionp 'ucs-to-char)
`(ucs-to-char ,num)
`(decode-char 'ucs ,num)))
(defvar identica-mode-string identica-method)
(defun identica-set-mode-string (loading)
(with-current-buffer (identica-buffer)
(let ((timeline-url
(concat (or identica-remote-server
(sn-account-server sn-current-account))
"/" identica-method)))
(setq mode-name
(if loading (concat
(if (stringp loading) loading "loading")
" " timeline-url "...")
timeline-url))
(debug-print mode-name))))
(defvar identica-mode-hook nil
"Identica-mode hook.")
(defcustom identica-load-hook nil
"Hook that is run after identica-mode.el has been loaded."
:group 'identica-mode
:type 'hook)
(defun identica-kill-buffer-function ()
(when (eq major-mode 'identica-mode)
(identica-stop)))
(defun identica-autoload-oauth ()
"Autoloads oauth.el when needed."
(autoload 'oauth-authorize-app "oauth")
(autoload 'oauth-hexify-string "oauth")
(autoload 'make-oauth-access-token "oauth"))
(defun identica-mode ()
"Major mode for Identica."
(interactive)
(identica-autoload-oauth)
(switch-to-buffer (identica-buffer))
(buffer-disable-undo (identica-buffer))
(kill-all-local-variables)
(identica-mode-init-variables)
(use-local-map identica-mode-map)
(setq major-mode 'identica-mode)
(setq mode-name identica-mode-string)
(setq mode-line-buffer-identification
`(,(default-value 'mode-line-buffer-identification)
(:eval (identica-mode-line-buffer-identification))))
(identica-update-mode-line)
(set-syntax-table identica-mode-syntax-table)
(font-lock-mode -1)
(if identica-soft-wrap-status
(if (fboundp 'visual-line-mode)
(visual-line-mode t)
(if (fboundp 'longlines-mode)
(longlines-mode t))))
(identica-retrieve-configuration)
(add-hook 'kill-buffer-hook 'identica-kill-buffer-function)
(run-mode-hooks 'identica-mode-hook))
;;;
;;; Basic HTTP functions
;;;
(defun identica-set-proxy (&optional url username passwd server port)
"Sets the proxy authentication variables as required by url library.
When called with no arguments, it reads `identica-mode' proxy
variables to get the authentication parameters.URL is either a string
or parsed URL. If URL is non-nil and valid, proxy authentication
values are read from it. The rest of the arguments can be used to
directly set proxy authentication. This function essentially adds
authentication parameters from one of the above methods to the double
alist `url-http-proxy-basic-auth-storage' and sets `url-using-proxy'."
(let* ((href (if (stringp url)
(url-generic-parse-url url)
url))
(port (or (and href (url-port href))
port identica-proxy-port))
(port (if (integerp port) (int-to-string port) port))
(server (or (and href (url-host href))
server identica-proxy-server))
(server (and server
(concat server (when port (concat ":" port)))))
(file (if href (let ((file-url (url-filename href)))
(cond
((string= "" file-url) "/")
((string-match "/$" file-url) file-url)
(t (url-basepath file-url))))
"Proxy"))
(password (or (and href (url-password href))
passwd identica-proxy-password))
(auth (concat (or (and href (url-user href))
username identica-proxy-user)
(and password (concat ":" password)))))
(when (and identica-proxy-use
(not (string= "" server))
(not (string= "" auth)))
(setq url-using-proxy server)
(let* ((proxy-double-alist
(or (assoc server
url-http-proxy-basic-auth-storage)
(car (push (cons server nil)
url-http-proxy-basic-auth-storage))))
(proxy-auth-alist (assoc file proxy-double-alist)))
(if proxy-auth-alist
(setcdr proxy-auth-alist (base64-encode-string auth))
(setcdr proxy-double-alist
(cons (cons file
(base64-encode-string auth))
(cdr-safe proxy-double-alist))))))))
(defun identica-change-user ()
(interactive)
"Interactive function to instantly change user authentication.
Directly reads parameters from user. This function only sets the
identica-mode variables `(sn-account-username sn-current-account)' and
`(sn-account-password sn-current-account)'.
It is the `identica-set-auth' function that eventually sets the
url library variables according to the above variables which does the
authentication. This will be done automatically in normal use cases
enabling dynamic change of user authentication."
(interactive)
(identica-ask-credentials)
(identica-get-timeline))
(defun identica-ask-credentials ()
"Asks for your username and password."
(setf (sn-account-username sn-current-account)
(read-string (concat "Username [for " (sn-account-server sn-current-account)
":" (int-to-string (sn-account-port sn-current-account)) "]: ")
nil nil (sn-account-username sn-current-account))
(sn-account-password sn-current-account)
(read-passwd "Password: " nil (sn-account-password sn-current-account))))
(defun identica-set-auth (&optional url username passwd server port)
"Sets the authentication parameters as required by url library.
If URL is non-nil and valid, it reads user authentication
parameters from url. If URL is nil, Rest of the arguments can be
used to directly set user authentication.
When called with no arguments, user authentication parameters are
read from identica-mode variables `(sn-account-username sn-current-account)'
`(sn-account-password sn-current-account)' `(sn-account-server sn-current-account)'
`(sn-account-port sn-current-account)'.
The username and password can also be set on ~/.authinfo,
~/.netrc or ~/.authinfo.gpg files for better security.
In this case `(sn-account-password sn-current-account)' should
not be predefined in any .emacs or init.el files, only
`(sn-account-username sn-current-account)' should be set."
(unless (sn-account-username sn-current-account)
(identica-ask-credentials))
(let* ((href (if (stringp url)
(url-generic-parse-url url)
url))
(port (or (and href (url-port href))
port (sn-account-port sn-current-account)))
(port (if (integerp port) (int-to-string port) port))
(server (or (and href (url-host href))
server (sn-account-server sn-current-account)))
(servername server)
(server (and server
(concat server (when port (concat ":" port)))))
(file (if href (let ((file-url (url-filename href)))
(cond
((string= "" file-url) "/")
((string-match "/$" file-url) file-url)
(t (url-basepath file-url))))
"Identi.ca API"))
(auth-user (if (functionp 'auth-source-search)
(plist-get (car (auth-source-search :host servername :max 1)) :user)
(auth-source-user-or-password "login" server "http")))
(auth-pass (if (functionp 'auth-source-search)
(if (functionp (plist-get (car (auth-source-search :host servername :max 1)) :secret))
(funcall (plist-get (car (auth-source-search :host servername :max 1)) :secret))
(plist-get (car (auth-source-search :host servername :max 1)) :secret))
(auth-source-user-or-password "password" server "http")))
(password (or auth-pass (and href (url-password href))
passwd (sn-account-password sn-current-account)))
(auth (concat (or auth-user (and href (url-user href))
username (sn-account-username sn-current-account))
(and password (concat ":" password)))))
(when (and (not (string= "" server))
(not (string= "" auth)))
(let* ((server-double-alist
(or (assoc server
url-http-real-basic-auth-storage)
(car (push (cons server nil)
url-http-real-basic-auth-storage))))
(api-auth-alist (assoc file server-double-alist)))
(if api-auth-alist
(setcdr api-auth-alist (base64-encode-string auth))
(setcdr server-double-alist
(cons (cons file
(base64-encode-string auth))
(cdr-safe server-double-alist))))))))
(defun identica-initialize-oauth ()
"Get authentication token unless we have one stashed already.
Shamelessly stolen from yammer.el"
(let ((filename (concat "~/." (sn-account-server sn-current-account) "-"
(sn-account-username sn-current-account) "-oauth-token")))
(when (file-exists-p filename)
(save-excursion
(find-file filename)
(let ((str (buffer-substring (point-min) (point-max))))
(if (string-match "\\([^:]*\\):\\(.*\\)"
(buffer-substring (point-min) (point-max)))
(setf (sn-oauth-access-token (sn-account-oauth-data sn-current-account))
(make-oauth-access-token
:consumer-key (sn-oauth-consumer-key (sn-account-oauth-data sn-current-account))
:consumer-secret (sn-oauth-consumer-secret (sn-account-oauth-data sn-current-account))
:auth-t (make-oauth-t
:token (match-string 1 str)
:token-secret (match-string 2 str))))))
(save-buffer)
(kill-this-buffer)))
(unless (sn-oauth-access-token (sn-account-oauth-data sn-current-account))
(setf (sn-oauth-access-token (sn-account-oauth-data sn-current-account))
(oauth-authorize-app (sn-oauth-consumer-key (sn-account-oauth-data sn-current-account))
(sn-oauth-consumer-secret (sn-account-oauth-data sn-current-account))
(sn-oauth-request-url (sn-account-oauth-data sn-current-account))
(sn-oauth-access-url (sn-account-oauth-data sn-current-account))
(sn-oauth-authorize-url (sn-account-oauth-data sn-current-account))))
(save-excursion
(find-file filename)
(end-of-buffer)
(let ((token (oauth-access-token-auth-t (sn-oauth-access-token (sn-account-oauth-data sn-current-account)))))
(insert (format "%s:%s\n"
(oauth-t-token token)
(oauth-t-token-secret token))))
(save-buffer)
(kill-this-buffer))))
(sn-oauth-access-token (sn-account-oauth-data sn-current-account)))
(defun identica-http-get
(server auth-mode method-class method &optional parameters sentinel sentinel-arguments)
"Basic function which communicates with server.
METHOD-CLASS and METHOD are parameters for getting dents messages and
other information from SERVER as specified in api documentation.
Third optional arguments specify the additional parameters required by
the above METHOD. It is specified as an alist with parameter name and
its corresponding value SENTINEL represents the callback function to
be called after the http response is completely retrieved.
SENTINEL-ARGUMENTS is the list of arguments (if any) of the SENTINEL
procedure."
(or sentinel (setq sentinel 'identica-http-get-default-sentinel))
(let ((url (concat "http://" server "/api/"
(when (not (string-equal method-class "none"))
(concat method-class "/" ))
method ".xml"
(when parameters
(concat "?"
(mapconcat
(lambda (param-pair)
(format "%s=%s"
(identica-percent-encode (car param-pair))
(identica-percent-encode (cdr param-pair))))
parameters
"&")))))
(url-package-name "emacs-identica-mode")
(url-package-version identica-mode-version)
(url-show-status nil))
(identica-set-proxy)
(unless (equal auth-mode "none")
(if (equal auth-mode "oauth")
(or (sn-oauth-access-token (sn-account-oauth-data sn-current-account))
(identica-initialize-oauth))
(identica-set-auth url)))
(when (get-buffer-process identica-http-buffer)
(delete-process identica-http-buffer)
(kill-buffer identica-http-buffer))
(setq identica-http-buffer
(identica-url-retrieve url sentinel method-class
method parameters sentinel-arguments auth-mode))
(set-buffer identica-buffer)
(set-buffer identica-buffer)
(identica-set-mode-string t)))
(defun identica-render-pending-dents ()
(interactive)
"If at the time an HTTP request for new dents finishes,
identica-buffer is not active, we defer its update, to make sure
we adjust point within the right frame."
(identica-render-timeline)
(when (> identica-new-dents-count 0)
(run-hooks 'identica-new-dents-hook)
(setq identica-new-dents-count 0))
(when identica-display-success-messages
(message "Success: Get")))
(defun identica-http-get-default-sentinel
(&optional status method-class method parameters success-message)
(debug-print (window-buffer))
(let ((error-object (assoc-workaround :error status))
(active-p (eq (window-buffer) (identica-buffer))))
(cond (error-object
(let ((error-data (format "%s" (caddr error-object))))
(when (cond
((string= error-data "deleted\n") t)
((and (string= error-data "404") method
(= 13 (string-match "/" method)))
(message "No Such User: %s" (substring method 14))
t)
((y-or-n-p
(format "Identica-Mode: Network error:%s Retry? "
status))
(identica-http-get (sn-account-server sn-current-account)
(sn-account-auth-mode sn-current-account)
method-class method parameters)
nil))
;; when the network process is deleted by another query