4
Jaiku interface for Gwibber
5
SegPhault (Ryan Paul) - 01/05/2008
9
import urllib2, urllib, support, re, can, simplejson, urlparse
33
NONCE_PARSE = re.compile('.*_nonce" value="([^"]+)".*', re.M | re.S)
34
LINK_MARKUP_PARSE = re.compile("\[([^\]]+)\]\(([^)]+)\)")
37
def __init__(self, client, data):
39
self.account = client.account
40
self.protocol = client.account["protocol"]
41
self.username = client.account["username"]
42
if data.has_key("id"): self.id = data["id"]
43
self.sender = "%s %s" % (data["user"]["first_name"], data["user"]["last_name"])
44
self.sender_nick = data["user"]["nick"]
45
self.sender_id = data["user"]["nick"]
46
self.time = support.parse_time(data["created_at"])
48
if data.has_key("title"):
49
self.text = data["title"]
50
#self.html_string = LINK_MARKUP_PARSE.sub('<a href="\\2">\\1</a>', self.text.replace(
51
# "&", "&").replace("<", "<").replace(">", ">"))
52
self.image = data["user"]["avatar"]
53
self.bgcolor = "message_color"
54
self.url = data["url"]
55
self.profile_url = "http://%s.jaiku.com" % data["user"]["nick"]
56
if data.has_key("icon") and data["icon"] != "": self.icon = data["icon"]
57
self.can_thread = True
58
self.is_reply = re.compile("@%s[\W]+|@%s$" % (self.username, self.username)).search(self.text) or \
59
(urlparse.urlparse(self.url)[1].split(".")[0].strip() == self.username and \
60
self.sender_nick != self.username)
62
class Comment(Message):
63
def __init__(self, client, data):
64
Message.__init__(self, client, data)
65
self.text = data["content"]
66
self.bgcolor = "comment_color"
67
self.is_comment = True
69
self.text = data["content"]
70
#self.html_string = support.LINK_PARSE.sub('<a href="\\1">\\1</a>', LINK_MARKUP_PARSE.sub('<a href="\\2">\\1</a>', self.text.replace(
71
# "&", "&").replace("<", "<").replace(">", ">")))
73
if data.has_key("entry_title"):
74
self.original_title = data["entry_title"]
75
self.title = "<small>Comment by</small> %s <small>on %s</small>" % (
76
self.sender, support.truncate(data["entry_title"],
77
client.account["comment_title_length"] or 20))
79
if data.has_key("comment_id"):
80
self.id = data["comment_id"]
81
else: self.id = data["id"]
84
def __init__(self, acct):
87
def send_enabled(self):
88
return self.account["send_enabled"] and \
89
self.account["username"] != None and \
90
self.account["private:password"] != None
92
def receive_enabled(self):
93
return self.account["receive_enabled"] and \
94
self.account["username"] != None and \
95
self.account["private:password"] != None
97
def get_messages(self):
98
return simplejson.loads(urllib2.urlopen(urllib2.Request(
99
"http://%s.jaiku.com/contacts/feed/json" % self.account["username"],
100
urllib.urlencode({"user": self.account["username"],
101
"personal_key":self.account["private:password"]}))).read())
103
def get_thread_data(self, msg):
104
return simplejson.loads(urllib2.urlopen(urllib2.Request(
105
"%s/json" % ("#" in msg.url and msg.url.split("#")[0] or msg.url),
106
urllib.urlencode({"user": self.account["username"],
107
"personal_key":self.account["private:password"]}))).read())
109
def get_thread(self, msg):
110
thread_content = self.get_thread_data(msg)
111
yield Message(self, thread_content)
112
for data in thread_content["comments"]:
113
yield Comment(self, data)
116
for data in self.get_messages()["stream"]:
117
if data.has_key("id"): yield Message(self, data)
118
else: yield Comment(self, data)
120
def get_nonce(self, msg):
122
page = urllib2.urlopen(urllib2.Request(
123
("#" in msg.url and msg.url.split("#")[0] or msg.url),
124
urllib.urlencode({"user": self.account["username"],
125
"personal_key":self.account["private:password"]}))).read()
127
return NONCE_PARSE.match(page, 1).group(1)
130
def send_thread(self, msg, message):
131
nonce = self.get_nonce(msg)
133
return urllib2.urlopen(urllib2.Request(
134
("#" in msg.url and msg.url.split("#")[0] or msg.url),
135
urllib.urlencode({"user": self.account["username"], "_nonce": nonce,
136
"personal_key":self.account["private:password"], "comment": message}))).read()
138
def send(self, message):
139
return urllib2.urlopen(urllib2.Request(
140
"http://api.jaiku.com/json", urllib.urlencode({"user": self.account["username"],
141
"personal_key":self.account["private:password"],
142
"message": message, "method": "presence.send"}))).read()